video_stream.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // VideoStreamPlayback starts here.
  32. void VideoStreamPlayback::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("mix_audio", "num_frames", "buffer", "offset"), &VideoStreamPlayback::mix_audio, DEFVAL(PackedFloat32Array()), DEFVAL(0));
  34. GDVIRTUAL_BIND(_stop);
  35. GDVIRTUAL_BIND(_play);
  36. GDVIRTUAL_BIND(_is_playing);
  37. GDVIRTUAL_BIND(_set_paused, "paused");
  38. GDVIRTUAL_BIND(_is_paused);
  39. GDVIRTUAL_BIND(_get_length);
  40. GDVIRTUAL_BIND(_get_playback_position);
  41. GDVIRTUAL_BIND(_seek, "time");
  42. GDVIRTUAL_BIND(_set_audio_track, "idx");
  43. GDVIRTUAL_BIND(_get_texture);
  44. GDVIRTUAL_BIND(_update, "delta");
  45. GDVIRTUAL_BIND(_get_channels);
  46. GDVIRTUAL_BIND(_get_mix_rate);
  47. }
  48. VideoStreamPlayback::VideoStreamPlayback() {
  49. }
  50. VideoStreamPlayback::~VideoStreamPlayback() {
  51. }
  52. void VideoStreamPlayback::stop() {
  53. GDVIRTUAL_CALL(_stop);
  54. }
  55. void VideoStreamPlayback::play() {
  56. GDVIRTUAL_CALL(_play);
  57. }
  58. bool VideoStreamPlayback::is_playing() const {
  59. bool ret;
  60. if (GDVIRTUAL_CALL(_is_playing, ret)) {
  61. return ret;
  62. }
  63. return false;
  64. }
  65. void VideoStreamPlayback::set_paused(bool p_paused) {
  66. GDVIRTUAL_CALL(_set_paused, p_paused);
  67. }
  68. bool VideoStreamPlayback::is_paused() const {
  69. bool ret;
  70. if (GDVIRTUAL_CALL(_is_paused, ret)) {
  71. return ret;
  72. }
  73. return false;
  74. }
  75. double VideoStreamPlayback::get_length() const {
  76. double ret;
  77. if (GDVIRTUAL_CALL(_get_length, ret)) {
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. double VideoStreamPlayback::get_playback_position() const {
  83. double ret;
  84. if (GDVIRTUAL_CALL(_get_playback_position, ret)) {
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. void VideoStreamPlayback::seek(double p_time) {
  90. GDVIRTUAL_CALL(_seek, p_time);
  91. }
  92. void VideoStreamPlayback::set_audio_track(int p_idx) {
  93. GDVIRTUAL_CALL(_set_audio_track, p_idx);
  94. }
  95. Ref<Texture2D> VideoStreamPlayback::get_texture() const {
  96. Ref<Texture2D> ret;
  97. if (GDVIRTUAL_CALL(_get_texture, ret)) {
  98. return ret;
  99. }
  100. return nullptr;
  101. }
  102. void VideoStreamPlayback::update(double p_delta) {
  103. GDVIRTUAL_CALL(_update, p_delta);
  104. }
  105. void VideoStreamPlayback::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  106. mix_callback = p_callback;
  107. mix_udata = p_userdata;
  108. }
  109. int VideoStreamPlayback::get_channels() const {
  110. int ret;
  111. if (GDVIRTUAL_CALL(_get_channels, ret)) {
  112. _channel_count = ret;
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. int VideoStreamPlayback::get_mix_rate() const {
  118. int ret;
  119. if (GDVIRTUAL_CALL(_get_mix_rate, ret)) {
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. int VideoStreamPlayback::mix_audio(int num_frames, PackedFloat32Array buffer, int offset) {
  125. if (num_frames <= 0) {
  126. return 0;
  127. }
  128. if (!mix_callback) {
  129. return -1;
  130. }
  131. ERR_FAIL_INDEX_V(offset, buffer.size(), -1);
  132. ERR_FAIL_INDEX_V((_channel_count < 1 ? 1 : _channel_count) * num_frames - 1, buffer.size() - offset, -1);
  133. return mix_callback(mix_udata, buffer.ptr() + offset, num_frames);
  134. }
  135. /* --- NOTE VideoStream starts here. ----- */
  136. Ref<VideoStreamPlayback> VideoStream::instantiate_playback() {
  137. Ref<VideoStreamPlayback> ret;
  138. if (GDVIRTUAL_CALL(_instantiate_playback, ret)) {
  139. ERR_FAIL_COND_V_MSG(ret.is_null(), nullptr, "Plugin returned null playback");
  140. ret->set_audio_track(audio_track);
  141. return ret;
  142. }
  143. return nullptr;
  144. }
  145. void VideoStream::set_file(const String &p_file) {
  146. file = p_file;
  147. emit_changed();
  148. }
  149. String VideoStream::get_file() {
  150. return file;
  151. }
  152. void VideoStream::_bind_methods() {
  153. ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStream::set_file);
  154. ClassDB::bind_method(D_METHOD("get_file"), &VideoStream::get_file);
  155. ADD_PROPERTY(PropertyInfo(Variant::STRING, "file"), "set_file", "get_file");
  156. GDVIRTUAL_BIND(_instantiate_playback);
  157. }
  158. VideoStream::VideoStream() {
  159. }
  160. VideoStream::~VideoStream() {
  161. }
  162. void VideoStream::set_audio_track(int p_track) {
  163. audio_track = p_track;
  164. }