video_stream_gdnative.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  32. #include "servers/audio_server.h"
  33. VideoDecoderServer *VideoDecoderServer::instance = nullptr;
  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. if (file) {
  56. size_t len = file->get_len();
  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 && (size_t)-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 ((size_t)-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 == nullptr, false);
  104. file = FileAccess::open(p_file, FileAccess::READ);
  105. bool file_opened = interface->open_file(data_struct, file);
  106. if (file_opened) {
  107. num_channels = interface->get_channels(data_struct);
  108. mix_rate = interface->get_mix_rate(data_struct);
  109. godot_vector2 vec = interface->get_texture_size(data_struct);
  110. texture_size = *(Vector2 *)&vec;
  111. // Only do memset if num_channels > 0 otherwise it will crash.
  112. if (num_channels > 0) {
  113. pcm = (float *)memalloc(num_channels * AUX_BUFFER_SIZE * sizeof(float));
  114. memset(pcm, 0, num_channels * AUX_BUFFER_SIZE * sizeof(float));
  115. }
  116. pcm_write_idx = -1;
  117. samples_decoded = 0;
  118. Ref<Image> img;
  119. img.instance();
  120. img->create((int)texture_size.width, false, (int)texture_size.height, Image::FORMAT_RGBA8);
  121. texture->create_from_image(img);
  122. }
  123. return file_opened;
  124. }
  125. void VideoStreamPlaybackGDNative::update(float p_delta) {
  126. if (!playing || paused) {
  127. return;
  128. }
  129. if (!file) {
  130. return;
  131. }
  132. time += p_delta;
  133. ERR_FAIL_COND(interface == nullptr);
  134. interface->update(data_struct, p_delta);
  135. // Don't mix if there's no audio (num_channels == 0).
  136. if (mix_callback && num_channels > 0) {
  137. if (pcm_write_idx >= 0) {
  138. // Previous remains
  139. int mixed = mix_callback(mix_udata, pcm + pcm_write_idx * num_channels, samples_decoded);
  140. if (mixed == samples_decoded) {
  141. pcm_write_idx = -1;
  142. } else {
  143. samples_decoded -= mixed;
  144. pcm_write_idx += mixed;
  145. }
  146. }
  147. if (pcm_write_idx < 0) {
  148. samples_decoded = interface->get_audioframe(data_struct, pcm, AUX_BUFFER_SIZE);
  149. pcm_write_idx = mix_callback(mix_udata, pcm, samples_decoded);
  150. if (pcm_write_idx == samples_decoded) {
  151. pcm_write_idx = -1;
  152. } else {
  153. samples_decoded -= pcm_write_idx;
  154. }
  155. }
  156. }
  157. if (seek_backward) {
  158. update_texture();
  159. seek_backward = false;
  160. }
  161. while (interface->get_playback_position(data_struct) < time && playing) {
  162. update_texture();
  163. }
  164. }
  165. void VideoStreamPlaybackGDNative::update_texture() {
  166. PackedByteArray *pba = (PackedByteArray *)interface->get_videoframe(data_struct);
  167. if (pba == nullptr) {
  168. playing = false;
  169. return;
  170. }
  171. Ref<Image> img = memnew(Image(texture_size.width, texture_size.height, 0, Image::FORMAT_RGBA8, *pba));
  172. texture->update(img, true);
  173. }
  174. // ctor and dtor
  175. VideoStreamPlaybackGDNative::VideoStreamPlaybackGDNative() :
  176. texture(Ref<ImageTexture>(memnew(ImageTexture))) {}
  177. VideoStreamPlaybackGDNative::~VideoStreamPlaybackGDNative() {
  178. cleanup();
  179. }
  180. void VideoStreamPlaybackGDNative::cleanup() {
  181. if (data_struct) {
  182. interface->destructor(data_struct);
  183. }
  184. if (pcm) {
  185. memfree(pcm);
  186. }
  187. if (file) {
  188. file->close();
  189. memdelete(file);
  190. file = nullptr;
  191. }
  192. pcm = nullptr;
  193. time = 0;
  194. num_channels = -1;
  195. interface = nullptr;
  196. data_struct = nullptr;
  197. }
  198. void VideoStreamPlaybackGDNative::set_interface(const godot_videodecoder_interface_gdnative *p_interface) {
  199. ERR_FAIL_COND(p_interface == nullptr);
  200. if (interface != nullptr) {
  201. cleanup();
  202. }
  203. interface = p_interface;
  204. data_struct = interface->constructor((godot_object *)this);
  205. }
  206. // controls
  207. bool VideoStreamPlaybackGDNative::is_playing() const {
  208. return playing;
  209. }
  210. bool VideoStreamPlaybackGDNative::is_paused() const {
  211. return paused;
  212. }
  213. void VideoStreamPlaybackGDNative::play() {
  214. stop();
  215. playing = true;
  216. delay_compensation = ProjectSettings::get_singleton()->get("audio/video_delay_compensation_ms");
  217. delay_compensation /= 1000.0;
  218. }
  219. void VideoStreamPlaybackGDNative::stop() {
  220. if (playing) {
  221. seek(0);
  222. }
  223. playing = false;
  224. }
  225. void VideoStreamPlaybackGDNative::seek(float p_time) {
  226. ERR_FAIL_COND(interface == nullptr);
  227. interface->seek(data_struct, p_time);
  228. if (p_time < time) {
  229. seek_backward = true;
  230. }
  231. time = p_time;
  232. // reset audio buffers
  233. memset(pcm, 0, num_channels * AUX_BUFFER_SIZE * sizeof(float));
  234. pcm_write_idx = -1;
  235. samples_decoded = 0;
  236. }
  237. void VideoStreamPlaybackGDNative::set_paused(bool p_paused) {
  238. paused = p_paused;
  239. }
  240. Ref<Texture2D> VideoStreamPlaybackGDNative::get_texture() const {
  241. return texture;
  242. }
  243. float VideoStreamPlaybackGDNative::get_length() const {
  244. ERR_FAIL_COND_V(interface == nullptr, 0);
  245. return interface->get_length(data_struct);
  246. }
  247. float VideoStreamPlaybackGDNative::get_playback_position() const {
  248. ERR_FAIL_COND_V(interface == nullptr, 0);
  249. return interface->get_playback_position(data_struct);
  250. }
  251. bool VideoStreamPlaybackGDNative::has_loop() const {
  252. // TODO: Implement looping?
  253. return false;
  254. }
  255. void VideoStreamPlaybackGDNative::set_loop(bool p_enable) {
  256. // Do nothing
  257. }
  258. void VideoStreamPlaybackGDNative::set_audio_track(int p_idx) {
  259. ERR_FAIL_COND(interface == nullptr);
  260. interface->set_audio_track(data_struct, p_idx);
  261. }
  262. void VideoStreamPlaybackGDNative::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  263. mix_udata = p_userdata;
  264. mix_callback = p_callback;
  265. }
  266. int VideoStreamPlaybackGDNative::get_channels() const {
  267. ERR_FAIL_COND_V(interface == nullptr, 0);
  268. return (num_channels > 0) ? num_channels : 0;
  269. }
  270. int VideoStreamPlaybackGDNative::get_mix_rate() const {
  271. ERR_FAIL_COND_V(interface == nullptr, 0);
  272. return mix_rate;
  273. }
  274. /* --- NOTE VideoStreamGDNative starts here. ----- */
  275. Ref<VideoStreamPlayback> VideoStreamGDNative::instance_playback() {
  276. Ref<VideoStreamPlaybackGDNative> pb = memnew(VideoStreamPlaybackGDNative);
  277. VideoDecoderGDNative *decoder = decoder_server.get_decoder(file.get_extension().to_lower());
  278. if (decoder == nullptr) {
  279. return nullptr;
  280. }
  281. pb->set_interface(decoder->interface);
  282. pb->set_audio_track(audio_track);
  283. if (pb->open_file(file)) {
  284. return pb;
  285. }
  286. return nullptr;
  287. }
  288. void VideoStreamGDNative::set_file(const String &p_file) {
  289. file = p_file;
  290. }
  291. String VideoStreamGDNative::get_file() {
  292. return file;
  293. }
  294. void VideoStreamGDNative::_bind_methods() {
  295. ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStreamGDNative::set_file);
  296. ClassDB::bind_method(D_METHOD("get_file"), &VideoStreamGDNative::get_file);
  297. ADD_PROPERTY(PropertyInfo(Variant::STRING, "file", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_file", "get_file");
  298. }
  299. void VideoStreamGDNative::set_audio_track(int p_track) {
  300. audio_track = p_track;
  301. }
  302. /* --- NOTE ResourceFormatLoaderVideoStreamGDNative starts here. ----- */
  303. RES ResourceFormatLoaderVideoStreamGDNative::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  304. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  305. if (!f) {
  306. if (r_error) {
  307. *r_error = ERR_CANT_OPEN;
  308. }
  309. return RES();
  310. }
  311. memdelete(f);
  312. VideoStreamGDNative *stream = memnew(VideoStreamGDNative);
  313. stream->set_file(p_path);
  314. Ref<VideoStreamGDNative> ogv_stream = Ref<VideoStreamGDNative>(stream);
  315. if (r_error) {
  316. *r_error = OK;
  317. }
  318. return ogv_stream;
  319. }
  320. void ResourceFormatLoaderVideoStreamGDNative::get_recognized_extensions(List<String> *p_extensions) const {
  321. Map<String, int>::Element *el = VideoDecoderServer::get_instance()->get_extensions().front();
  322. while (el) {
  323. p_extensions->push_back(el->key());
  324. el = el->next();
  325. }
  326. }
  327. bool ResourceFormatLoaderVideoStreamGDNative::handles_type(const String &p_type) const {
  328. return ClassDB::is_parent_class(p_type, "VideoStream");
  329. }
  330. String ResourceFormatLoaderVideoStreamGDNative::get_resource_type(const String &p_path) const {
  331. String el = p_path.get_extension().to_lower();
  332. if (VideoDecoderServer::get_instance()->get_extensions().has(el)) {
  333. return "VideoStreamGDNative";
  334. }
  335. return "";
  336. }