video_stream.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**************************************************************************/
  2. /* video_stream.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "video_stream.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/audio_server.h"
  33. // VideoStreamPlayback starts here.
  34. void VideoStreamPlayback::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("mix_audio", "num_frames", "buffer", "offset"), &VideoStreamPlayback::mix_audio, DEFVAL(PackedFloat32Array()), DEFVAL(0));
  36. GDVIRTUAL_BIND(_stop);
  37. GDVIRTUAL_BIND(_play);
  38. GDVIRTUAL_BIND(_is_playing);
  39. GDVIRTUAL_BIND(_set_paused, "paused");
  40. GDVIRTUAL_BIND(_is_paused);
  41. GDVIRTUAL_BIND(_get_length);
  42. GDVIRTUAL_BIND(_get_playback_position);
  43. GDVIRTUAL_BIND(_seek, "time");
  44. GDVIRTUAL_BIND(_set_audio_track, "idx");
  45. GDVIRTUAL_BIND(_get_texture);
  46. GDVIRTUAL_BIND(_update, "delta");
  47. GDVIRTUAL_BIND(_get_channels);
  48. GDVIRTUAL_BIND(_get_mix_rate);
  49. }
  50. VideoStreamPlayback::VideoStreamPlayback() {
  51. }
  52. VideoStreamPlayback::~VideoStreamPlayback() {
  53. }
  54. void VideoStreamPlayback::stop() {
  55. GDVIRTUAL_CALL(_stop);
  56. }
  57. void VideoStreamPlayback::play() {
  58. GDVIRTUAL_CALL(_play);
  59. }
  60. bool VideoStreamPlayback::is_playing() const {
  61. bool ret;
  62. if (GDVIRTUAL_CALL(_is_playing, ret)) {
  63. return ret;
  64. }
  65. return false;
  66. }
  67. void VideoStreamPlayback::set_paused(bool p_paused) {
  68. GDVIRTUAL_CALL(_set_paused, p_paused);
  69. }
  70. bool VideoStreamPlayback::is_paused() const {
  71. bool ret;
  72. if (GDVIRTUAL_CALL(_is_paused, ret)) {
  73. return ret;
  74. }
  75. return false;
  76. }
  77. double VideoStreamPlayback::get_length() const {
  78. double ret;
  79. if (GDVIRTUAL_CALL(_get_length, ret)) {
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. double VideoStreamPlayback::get_playback_position() const {
  85. double ret;
  86. if (GDVIRTUAL_CALL(_get_playback_position, ret)) {
  87. return ret;
  88. }
  89. return 0;
  90. }
  91. void VideoStreamPlayback::seek(double p_time) {
  92. GDVIRTUAL_CALL(_seek, p_time);
  93. }
  94. void VideoStreamPlayback::set_audio_track(int p_idx) {
  95. GDVIRTUAL_CALL(_set_audio_track, p_idx);
  96. }
  97. Ref<Texture2D> VideoStreamPlayback::get_texture() const {
  98. Ref<Texture2D> ret;
  99. if (GDVIRTUAL_CALL(_get_texture, ret)) {
  100. return ret;
  101. }
  102. return nullptr;
  103. }
  104. void VideoStreamPlayback::update(double p_delta) {
  105. GDVIRTUAL_CALL(_update, p_delta);
  106. }
  107. void VideoStreamPlayback::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  108. mix_callback = p_callback;
  109. mix_udata = p_userdata;
  110. }
  111. int VideoStreamPlayback::get_channels() const {
  112. int ret;
  113. if (GDVIRTUAL_CALL(_get_channels, ret)) {
  114. _channel_count = ret;
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. int VideoStreamPlayback::get_mix_rate() const {
  120. int ret;
  121. if (GDVIRTUAL_CALL(_get_mix_rate, ret)) {
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. int VideoStreamPlayback::mix_audio(int num_frames, PackedFloat32Array buffer, int offset) {
  127. if (num_frames <= 0) {
  128. return 0;
  129. }
  130. if (!mix_callback) {
  131. return -1;
  132. }
  133. ERR_FAIL_INDEX_V(offset, buffer.size(), -1);
  134. ERR_FAIL_INDEX_V((_channel_count < 1 ? 1 : _channel_count) * num_frames - 1, buffer.size() - offset, -1);
  135. return mix_callback(mix_udata, buffer.ptr() + offset, num_frames);
  136. }
  137. /* --- NOTE VideoStream starts here. ----- */
  138. Ref<VideoStreamPlayback> VideoStream::instantiate_playback() {
  139. Ref<VideoStreamPlayback> ret;
  140. if (GDVIRTUAL_CALL(_instantiate_playback, ret)) {
  141. ERR_FAIL_COND_V_MSG(ret.is_null(), nullptr, "Plugin returned null playback");
  142. ret->set_audio_track(audio_track);
  143. return ret;
  144. }
  145. return nullptr;
  146. }
  147. void VideoStream::set_file(const String &p_file) {
  148. file = p_file;
  149. emit_changed();
  150. }
  151. String VideoStream::get_file() {
  152. return file;
  153. }
  154. void VideoStream::_bind_methods() {
  155. ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStream::set_file);
  156. ClassDB::bind_method(D_METHOD("get_file"), &VideoStream::get_file);
  157. ADD_PROPERTY(PropertyInfo(Variant::STRING, "file"), "set_file", "get_file");
  158. GDVIRTUAL_BIND(_instantiate_playback);
  159. }
  160. VideoStream::VideoStream() {
  161. }
  162. VideoStream::~VideoStream() {
  163. }
  164. void VideoStream::set_audio_track(int p_track) {
  165. audio_track = p_track;
  166. }