video_stream_gdnative.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*************************************************************************/
  2. /* video_stream_gdnative.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  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_gdnative.h"
  31. #include "core/project_settings.h"
  32. #include "servers/audio_server.h"
  33. VideoDecoderServer *VideoDecoderServer::instance = NULL;
  34. static VideoDecoderServer decoder_server;
  35. const int AUX_BUFFER_SIZE = 1024; // Buffer 1024 samples.
  36. // NOTE: Callbacks for the GDNative libraries.
  37. extern "C" {
  38. godot_int GDAPI godot_videodecoder_file_read(void *ptr, uint8_t *buf, int buf_size) {
  39. // ptr is a FileAccess
  40. FileAccess *file = reinterpret_cast<FileAccess *>(ptr);
  41. // if file exists
  42. if (file) {
  43. long bytes_read = file->get_buffer(buf, buf_size);
  44. // No bytes to read => EOF
  45. if (bytes_read == 0) {
  46. return 0;
  47. }
  48. return bytes_read;
  49. }
  50. return -1;
  51. }
  52. int64_t GDAPI godot_videodecoder_file_seek(void *ptr, int64_t pos, int whence) {
  53. // file
  54. FileAccess *file = reinterpret_cast<FileAccess *>(ptr);
  55. size_t len = file->get_len();
  56. if (file) {
  57. switch (whence) {
  58. case SEEK_SET: {
  59. // Just for explicitness
  60. size_t new_pos = static_cast<size_t>(pos);
  61. if (new_pos > len) {
  62. return -1;
  63. }
  64. file->seek(new_pos);
  65. pos = static_cast<int64_t>(file->get_position());
  66. return pos;
  67. } break;
  68. case SEEK_CUR: {
  69. // Just in case it doesn't exist
  70. if (pos < 0 && -pos > file->get_position()) {
  71. return -1;
  72. }
  73. pos = pos + static_cast<int>(file->get_position());
  74. file->seek(pos);
  75. pos = static_cast<int64_t>(file->get_position());
  76. return pos;
  77. } break;
  78. case SEEK_END: {
  79. // Just in case something goes wrong
  80. if (-pos > len) {
  81. return -1;
  82. }
  83. file->seek_end(pos);
  84. pos = static_cast<int64_t>(file->get_position());
  85. return pos;
  86. } break;
  87. default: {
  88. // Only 4 possible options, hence default = AVSEEK_SIZE
  89. // Asks to return the length of file
  90. return static_cast<int64_t>(len);
  91. } break;
  92. }
  93. }
  94. // In case nothing works out.
  95. return -1;
  96. }
  97. void GDAPI godot_videodecoder_register_decoder(const godot_videodecoder_interface_gdnative *p_interface) {
  98. decoder_server.register_decoder_interface(p_interface);
  99. }
  100. }
  101. // VideoStreamPlaybackGDNative starts here.
  102. bool VideoStreamPlaybackGDNative::open_file(const String &p_file) {
  103. ERR_FAIL_COND_V(interface == NULL, false);
  104. file = FileAccess::open(p_file, FileAccess::READ);
  105. bool file_opened = interface->open_file(data_struct, file);
  106. num_channels = interface->get_channels(data_struct);
  107. mix_rate = interface->get_mix_rate(data_struct);
  108. godot_vector2 vec = interface->get_texture_size(data_struct);
  109. texture_size = *(Vector2 *)&vec;
  110. pcm = (float *)memalloc(num_channels * AUX_BUFFER_SIZE * sizeof(float));
  111. memset(pcm, 0, num_channels * AUX_BUFFER_SIZE * sizeof(float));
  112. pcm_write_idx = -1;
  113. samples_decoded = 0;
  114. texture->create((int)texture_size.width, (int)texture_size.height, Image::FORMAT_RGBA8, Texture::FLAG_FILTER | Texture::FLAG_VIDEO_SURFACE);
  115. return file_opened;
  116. }
  117. void VideoStreamPlaybackGDNative::update(float p_delta) {
  118. if (!playing || paused) {
  119. return;
  120. }
  121. if (!file) {
  122. return;
  123. }
  124. time += p_delta;
  125. ERR_FAIL_COND(interface == NULL);
  126. interface->update(data_struct, p_delta);
  127. if (pcm_write_idx >= 0) {
  128. // Previous remains
  129. int mixed = mix_callback(mix_udata, pcm, samples_decoded);
  130. if (mixed == samples_decoded) {
  131. pcm_write_idx = -1;
  132. } else {
  133. samples_decoded -= mixed;
  134. pcm_write_idx += mixed;
  135. }
  136. }
  137. if (pcm_write_idx < 0) {
  138. samples_decoded = interface->get_audioframe(data_struct, pcm, AUX_BUFFER_SIZE);
  139. pcm_write_idx = mix_callback(mix_udata, pcm, samples_decoded);
  140. if (pcm_write_idx == samples_decoded) {
  141. pcm_write_idx = -1;
  142. } else {
  143. samples_decoded -= pcm_write_idx;
  144. }
  145. }
  146. while (interface->get_playback_position(data_struct) < time) {
  147. update_texture();
  148. }
  149. }
  150. void VideoStreamPlaybackGDNative::update_texture() {
  151. PoolByteArray *pba = (PoolByteArray *)interface->get_videoframe(data_struct);
  152. if (pba == NULL) {
  153. playing = false;
  154. return;
  155. }
  156. Ref<Image> img = memnew(Image(texture_size.width, texture_size.height, 0, Image::FORMAT_RGBA8, *pba));
  157. texture->set_data(img);
  158. }
  159. // ctor and dtor
  160. VideoStreamPlaybackGDNative::VideoStreamPlaybackGDNative() :
  161. texture(Ref<ImageTexture>(memnew(ImageTexture))),
  162. playing(false),
  163. paused(false),
  164. mix_udata(NULL),
  165. mix_callback(NULL),
  166. num_channels(-1),
  167. time(0),
  168. mix_rate(0),
  169. delay_compensation(0),
  170. pcm(NULL),
  171. pcm_write_idx(0),
  172. samples_decoded(0),
  173. file(NULL),
  174. interface(NULL),
  175. data_struct(NULL) {}
  176. VideoStreamPlaybackGDNative::~VideoStreamPlaybackGDNative() {
  177. cleanup();
  178. }
  179. void VideoStreamPlaybackGDNative::cleanup() {
  180. if (data_struct)
  181. interface->destructor(data_struct);
  182. if (pcm)
  183. memfree(pcm);
  184. pcm = NULL;
  185. time = 0;
  186. num_channels = -1;
  187. interface = NULL;
  188. data_struct = NULL;
  189. }
  190. void VideoStreamPlaybackGDNative::set_interface(const godot_videodecoder_interface_gdnative *p_interface) {
  191. ERR_FAIL_COND(p_interface == NULL);
  192. if (interface != NULL) {
  193. cleanup();
  194. }
  195. interface = p_interface;
  196. data_struct = interface->constructor((godot_object *)this);
  197. }
  198. // controls
  199. bool VideoStreamPlaybackGDNative::is_playing() const {
  200. return playing;
  201. }
  202. bool VideoStreamPlaybackGDNative::is_paused() const {
  203. return paused;
  204. }
  205. void VideoStreamPlaybackGDNative::play() {
  206. stop();
  207. playing = true;
  208. delay_compensation = ProjectSettings::get_singleton()->get("audio/video_delay_compensation_ms");
  209. delay_compensation /= 1000.0;
  210. }
  211. void VideoStreamPlaybackGDNative::stop() {
  212. if (playing) {
  213. seek(0);
  214. }
  215. playing = false;
  216. }
  217. void VideoStreamPlaybackGDNative::seek(float p_time) {
  218. ERR_FAIL_COND(interface == NULL);
  219. interface->seek(data_struct, p_time);
  220. }
  221. void VideoStreamPlaybackGDNative::set_paused(bool p_paused) {
  222. paused = p_paused;
  223. }
  224. Ref<Texture> VideoStreamPlaybackGDNative::get_texture() {
  225. return texture;
  226. }
  227. float VideoStreamPlaybackGDNative::get_length() const {
  228. ERR_FAIL_COND_V(interface == NULL, 0);
  229. return interface->get_length(data_struct);
  230. }
  231. float VideoStreamPlaybackGDNative::get_playback_position() const {
  232. ERR_FAIL_COND_V(interface == NULL, 0);
  233. return interface->get_playback_position(data_struct);
  234. }
  235. bool VideoStreamPlaybackGDNative::has_loop() const {
  236. // TODO: Implement looping?
  237. return false;
  238. }
  239. void VideoStreamPlaybackGDNative::set_loop(bool p_enable) {
  240. // Do nothing
  241. }
  242. void VideoStreamPlaybackGDNative::set_audio_track(int p_idx) {
  243. ERR_FAIL_COND(interface == NULL);
  244. interface->set_audio_track(data_struct, p_idx);
  245. }
  246. void VideoStreamPlaybackGDNative::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  247. mix_udata = p_userdata;
  248. mix_callback = p_callback;
  249. }
  250. int VideoStreamPlaybackGDNative::get_channels() const {
  251. ERR_FAIL_COND_V(interface == NULL, 0);
  252. return (num_channels > 0) ? num_channels : 0;
  253. }
  254. int VideoStreamPlaybackGDNative::get_mix_rate() const {
  255. ERR_FAIL_COND_V(interface == NULL, 0);
  256. return mix_rate;
  257. }
  258. /* --- NOTE VideoStreamGDNative starts here. ----- */
  259. Ref<VideoStreamPlayback> VideoStreamGDNative::instance_playback() {
  260. Ref<VideoStreamPlaybackGDNative> pb = memnew(VideoStreamPlaybackGDNative);
  261. VideoDecoderGDNative *decoder = decoder_server.get_decoder(file.get_extension().to_lower());
  262. if (decoder == NULL)
  263. return NULL;
  264. pb->set_interface(decoder->interface);
  265. pb->set_audio_track(audio_track);
  266. if (pb->open_file(file))
  267. return pb;
  268. return NULL;
  269. }
  270. void VideoStreamGDNative::set_file(const String &p_file) {
  271. file = p_file;
  272. }
  273. String VideoStreamGDNative::get_file() {
  274. return file;
  275. }
  276. void VideoStreamGDNative::_bind_methods() {
  277. ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStreamGDNative::set_file);
  278. ClassDB::bind_method(D_METHOD("get_file"), &VideoStreamGDNative::get_file);
  279. ADD_PROPERTY(PropertyInfo(Variant::STRING, "file", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_file", "get_file");
  280. }
  281. void VideoStreamGDNative::set_audio_track(int p_track) {
  282. audio_track = p_track;
  283. }