video_stream_gdnative.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /**************************************************************************/
  2. /* video_stream_gdnative.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_gdnative.h"
  31. #include "core/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. int64_t bytes_read = file->get_buffer(buf, buf_size);
  44. return bytes_read;
  45. }
  46. return -1;
  47. }
  48. int64_t GDAPI godot_videodecoder_file_seek(void *ptr, int64_t pos, int whence) {
  49. // file
  50. FileAccess *file = reinterpret_cast<FileAccess *>(ptr);
  51. if (file) {
  52. int64_t len = file->get_len();
  53. switch (whence) {
  54. case SEEK_SET: {
  55. if (pos > len) {
  56. return -1;
  57. }
  58. file->seek(pos);
  59. return file->get_position();
  60. } break;
  61. case SEEK_CUR: {
  62. // Just in case it doesn't exist
  63. if (pos < 0 && -pos > (int64_t)file->get_position()) {
  64. return -1;
  65. }
  66. file->seek(file->get_position() + pos);
  67. return file->get_position();
  68. } break;
  69. case SEEK_END: {
  70. // Just in case something goes wrong
  71. if (-pos > len) {
  72. return -1;
  73. }
  74. file->seek_end(pos);
  75. return file->get_position();
  76. } break;
  77. default: {
  78. // Only 4 possible options, hence default = AVSEEK_SIZE
  79. // Asks to return the length of file
  80. return len;
  81. } break;
  82. }
  83. }
  84. // In case nothing works out.
  85. return -1;
  86. }
  87. void GDAPI godot_videodecoder_register_decoder(const godot_videodecoder_interface_gdnative *p_interface) {
  88. decoder_server.register_decoder_interface(p_interface);
  89. }
  90. }
  91. // VideoStreamPlaybackGDNative starts here.
  92. bool VideoStreamPlaybackGDNative::open_file(const String &p_file) {
  93. ERR_FAIL_COND_V(interface == nullptr, false);
  94. file = FileAccess::open(p_file, FileAccess::READ);
  95. bool file_opened = interface->open_file(data_struct, file);
  96. if (file_opened) {
  97. num_channels = interface->get_channels(data_struct);
  98. mix_rate = interface->get_mix_rate(data_struct);
  99. godot_vector2 vec = interface->get_texture_size(data_struct);
  100. texture_size = *(Vector2 *)&vec;
  101. // Only do memset if num_channels > 0 otherwise it will crash.
  102. if (num_channels > 0) {
  103. pcm = (float *)memalloc(num_channels * AUX_BUFFER_SIZE * sizeof(float));
  104. memset(pcm, 0, num_channels * AUX_BUFFER_SIZE * sizeof(float));
  105. }
  106. pcm_write_idx = -1;
  107. samples_decoded = 0;
  108. texture->create((int)texture_size.width, (int)texture_size.height, Image::FORMAT_RGBA8, Texture::FLAG_FILTER | Texture::FLAG_VIDEO_SURFACE);
  109. }
  110. return file_opened;
  111. }
  112. void VideoStreamPlaybackGDNative::update(float p_delta) {
  113. if (!playing || paused) {
  114. return;
  115. }
  116. if (!file) {
  117. return;
  118. }
  119. time += p_delta;
  120. ERR_FAIL_COND(interface == nullptr);
  121. interface->update(data_struct, p_delta);
  122. // Don't mix if there's no audio (num_channels == 0).
  123. if (mix_callback && num_channels > 0) {
  124. if (pcm_write_idx >= 0) {
  125. // Previous remains
  126. int mixed = mix_callback(mix_udata, pcm + pcm_write_idx * num_channels, samples_decoded);
  127. if (mixed == samples_decoded) {
  128. pcm_write_idx = -1;
  129. } else {
  130. samples_decoded -= mixed;
  131. pcm_write_idx += mixed;
  132. }
  133. }
  134. if (pcm_write_idx < 0) {
  135. samples_decoded = interface->get_audioframe(data_struct, pcm, AUX_BUFFER_SIZE);
  136. pcm_write_idx = mix_callback(mix_udata, pcm, samples_decoded);
  137. if (pcm_write_idx == samples_decoded) {
  138. pcm_write_idx = -1;
  139. } else {
  140. samples_decoded -= pcm_write_idx;
  141. }
  142. }
  143. }
  144. if (seek_backward) {
  145. update_texture();
  146. seek_backward = false;
  147. }
  148. while (interface->get_playback_position(data_struct) < time && playing) {
  149. update_texture();
  150. }
  151. }
  152. void VideoStreamPlaybackGDNative::update_texture() {
  153. PoolByteArray *pba = (PoolByteArray *)interface->get_videoframe(data_struct);
  154. if (pba == nullptr) {
  155. playing = false;
  156. return;
  157. }
  158. Ref<Image> img = memnew(Image(texture_size.width, texture_size.height, 0, Image::FORMAT_RGBA8, *pba));
  159. texture->set_data(img);
  160. }
  161. // ctor and dtor
  162. VideoStreamPlaybackGDNative::VideoStreamPlaybackGDNative() :
  163. texture(Ref<ImageTexture>(memnew(ImageTexture))),
  164. playing(false),
  165. paused(false),
  166. mix_udata(nullptr),
  167. mix_callback(nullptr),
  168. num_channels(-1),
  169. time(0),
  170. seek_backward(false),
  171. mix_rate(0),
  172. delay_compensation(0),
  173. pcm(nullptr),
  174. pcm_write_idx(0),
  175. samples_decoded(0),
  176. file(nullptr),
  177. interface(nullptr),
  178. data_struct(nullptr) {}
  179. VideoStreamPlaybackGDNative::~VideoStreamPlaybackGDNative() {
  180. cleanup();
  181. }
  182. void VideoStreamPlaybackGDNative::cleanup() {
  183. if (data_struct) {
  184. interface->destructor(data_struct);
  185. }
  186. if (pcm) {
  187. memfree(pcm);
  188. }
  189. if (file) {
  190. file->close();
  191. memdelete(file);
  192. file = nullptr;
  193. }
  194. pcm = nullptr;
  195. time = 0;
  196. num_channels = -1;
  197. interface = nullptr;
  198. data_struct = nullptr;
  199. }
  200. void VideoStreamPlaybackGDNative::set_interface(const godot_videodecoder_interface_gdnative *p_interface) {
  201. ERR_FAIL_COND(p_interface == nullptr);
  202. if (interface != nullptr) {
  203. cleanup();
  204. }
  205. interface = p_interface;
  206. data_struct = interface->constructor((godot_object *)this);
  207. }
  208. // controls
  209. bool VideoStreamPlaybackGDNative::is_playing() const {
  210. return playing;
  211. }
  212. bool VideoStreamPlaybackGDNative::is_paused() const {
  213. return paused;
  214. }
  215. void VideoStreamPlaybackGDNative::play() {
  216. stop();
  217. playing = true;
  218. delay_compensation = ProjectSettings::get_singleton()->get("audio/video_delay_compensation_ms");
  219. delay_compensation /= 1000.0;
  220. }
  221. void VideoStreamPlaybackGDNative::stop() {
  222. if (playing) {
  223. seek(0);
  224. }
  225. playing = false;
  226. }
  227. void VideoStreamPlaybackGDNative::seek(float p_time) {
  228. ERR_FAIL_COND(interface == nullptr);
  229. interface->seek(data_struct, p_time);
  230. if (p_time < time) {
  231. seek_backward = true;
  232. }
  233. time = p_time;
  234. // reset audio buffers
  235. memset(pcm, 0, num_channels * AUX_BUFFER_SIZE * sizeof(float));
  236. pcm_write_idx = -1;
  237. samples_decoded = 0;
  238. }
  239. void VideoStreamPlaybackGDNative::set_paused(bool p_paused) {
  240. paused = p_paused;
  241. }
  242. Ref<Texture> VideoStreamPlaybackGDNative::get_texture() const {
  243. return texture;
  244. }
  245. float VideoStreamPlaybackGDNative::get_length() const {
  246. ERR_FAIL_COND_V(interface == nullptr, 0);
  247. return interface->get_length(data_struct);
  248. }
  249. float VideoStreamPlaybackGDNative::get_playback_position() const {
  250. ERR_FAIL_COND_V(interface == nullptr, 0);
  251. return interface->get_playback_position(data_struct);
  252. }
  253. bool VideoStreamPlaybackGDNative::has_loop() const {
  254. // TODO: Implement looping?
  255. return false;
  256. }
  257. void VideoStreamPlaybackGDNative::set_loop(bool p_enable) {
  258. // Do nothing
  259. }
  260. void VideoStreamPlaybackGDNative::set_audio_track(int p_idx) {
  261. ERR_FAIL_COND(interface == nullptr);
  262. interface->set_audio_track(data_struct, p_idx);
  263. }
  264. void VideoStreamPlaybackGDNative::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  265. mix_udata = p_userdata;
  266. mix_callback = p_callback;
  267. }
  268. int VideoStreamPlaybackGDNative::get_channels() const {
  269. ERR_FAIL_COND_V(interface == nullptr, 0);
  270. return (num_channels > 0) ? num_channels : 0;
  271. }
  272. int VideoStreamPlaybackGDNative::get_mix_rate() const {
  273. ERR_FAIL_COND_V(interface == nullptr, 0);
  274. return mix_rate;
  275. }
  276. /* --- NOTE VideoStreamGDNative starts here. ----- */
  277. Ref<VideoStreamPlayback> VideoStreamGDNative::instance_playback() {
  278. Ref<VideoStreamPlaybackGDNative> pb = memnew(VideoStreamPlaybackGDNative);
  279. VideoDecoderGDNative *decoder = decoder_server.get_decoder(file.get_extension().to_lower());
  280. if (decoder == nullptr) {
  281. return nullptr;
  282. }
  283. pb->set_interface(decoder->interface);
  284. pb->set_audio_track(audio_track);
  285. if (pb->open_file(file)) {
  286. return pb;
  287. }
  288. return nullptr;
  289. }
  290. void VideoStreamGDNative::set_file(const String &p_file) {
  291. file = p_file;
  292. }
  293. String VideoStreamGDNative::get_file() {
  294. return file;
  295. }
  296. void VideoStreamGDNative::_bind_methods() {
  297. ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStreamGDNative::set_file);
  298. ClassDB::bind_method(D_METHOD("get_file"), &VideoStreamGDNative::get_file);
  299. ADD_PROPERTY(PropertyInfo(Variant::STRING, "file", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_file", "get_file");
  300. }
  301. void VideoStreamGDNative::set_audio_track(int p_track) {
  302. audio_track = p_track;
  303. }
  304. /* --- NOTE ResourceFormatLoaderVideoStreamGDNative starts here. ----- */
  305. RES ResourceFormatLoaderVideoStreamGDNative::load(const String &p_path, const String &p_original_path, Error *r_error) {
  306. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  307. if (!f) {
  308. if (r_error) {
  309. *r_error = ERR_CANT_OPEN;
  310. }
  311. return RES();
  312. }
  313. memdelete(f);
  314. VideoStreamGDNative *stream = memnew(VideoStreamGDNative);
  315. stream->set_file(p_path);
  316. Ref<VideoStreamGDNative> ogv_stream = Ref<VideoStreamGDNative>(stream);
  317. if (r_error) {
  318. *r_error = OK;
  319. }
  320. return ogv_stream;
  321. }
  322. void ResourceFormatLoaderVideoStreamGDNative::get_recognized_extensions(List<String> *p_extensions) const {
  323. Map<String, int>::Element *el = VideoDecoderServer::get_instance()->get_extensions().front();
  324. while (el) {
  325. p_extensions->push_back(el->key());
  326. el = el->next();
  327. }
  328. }
  329. bool ResourceFormatLoaderVideoStreamGDNative::handles_type(const String &p_type) const {
  330. return ClassDB::is_parent_class(p_type, "VideoStream");
  331. }
  332. String ResourceFormatLoaderVideoStreamGDNative::get_resource_type(const String &p_path) const {
  333. String el = p_path.get_extension().to_lower();
  334. if (VideoDecoderServer::get_instance()->get_extensions().has(el)) {
  335. return "VideoStreamGDNative";
  336. }
  337. return "";
  338. }