video_stream_theora.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*************************************************************************/
  2. /* video_stream_theora.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_theora.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "thirdparty/misc/yuv2rgb.h"
  34. int VideoStreamPlaybackTheora::buffer_data() {
  35. char *buffer = ogg_sync_buffer(&oy, 4096);
  36. #ifdef THEORA_USE_THREAD_STREAMING
  37. int read;
  38. do {
  39. thread_sem->post();
  40. read = MIN(ring_buffer.data_left(), 4096);
  41. if (read) {
  42. ring_buffer.read((uint8_t *)buffer, read);
  43. ogg_sync_wrote(&oy, read);
  44. } else {
  45. OS::get_singleton()->delay_usec(100);
  46. }
  47. } while (read == 0);
  48. return read;
  49. #else
  50. int bytes = file->get_buffer((uint8_t *)buffer, 4096);
  51. ogg_sync_wrote(&oy, bytes);
  52. return (bytes);
  53. #endif
  54. }
  55. int VideoStreamPlaybackTheora::queue_page(ogg_page *page) {
  56. if (theora_p) {
  57. ogg_stream_pagein(&to, page);
  58. if (to.e_o_s) {
  59. theora_eos = true;
  60. }
  61. }
  62. if (vorbis_p) {
  63. ogg_stream_pagein(&vo, page);
  64. if (vo.e_o_s) {
  65. vorbis_eos = true;
  66. }
  67. }
  68. return 0;
  69. }
  70. void VideoStreamPlaybackTheora::video_write() {
  71. th_ycbcr_buffer yuv;
  72. th_decode_ycbcr_out(td, yuv);
  73. int pitch = 4;
  74. frame_data.resize(size.x * size.y * pitch);
  75. {
  76. uint8_t *w = frame_data.ptrw();
  77. char *dst = (char *)w;
  78. //uv_offset=(ti.pic_x/2)+(yuv[1].stride)*(ti.pic_y/2);
  79. if (px_fmt == TH_PF_444) {
  80. yuv444_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data, (uint8_t *)yuv[1].data, (uint8_t *)yuv[2].data, size.x, size.y, yuv[0].stride, yuv[1].stride, size.x << 2);
  81. } else if (px_fmt == TH_PF_422) {
  82. yuv422_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data, (uint8_t *)yuv[1].data, (uint8_t *)yuv[2].data, size.x, size.y, yuv[0].stride, yuv[1].stride, size.x << 2);
  83. } else if (px_fmt == TH_PF_420) {
  84. yuv420_2_rgb8888((uint8_t *)dst, (uint8_t *)yuv[0].data, (uint8_t *)yuv[1].data, (uint8_t *)yuv[2].data, size.x, size.y, yuv[0].stride, yuv[1].stride, size.x << 2);
  85. };
  86. format = Image::FORMAT_RGBA8;
  87. }
  88. Ref<Image> img = memnew(Image(size.x, size.y, 0, Image::FORMAT_RGBA8, frame_data)); //zero copy image creation
  89. texture->update(img, true); //zero copy send to visual server
  90. frames_pending = 1;
  91. }
  92. void VideoStreamPlaybackTheora::clear() {
  93. if (!file) {
  94. return;
  95. }
  96. if (vorbis_p) {
  97. ogg_stream_clear(&vo);
  98. if (vorbis_p >= 3) {
  99. vorbis_block_clear(&vb);
  100. vorbis_dsp_clear(&vd);
  101. };
  102. vorbis_comment_clear(&vc);
  103. vorbis_info_clear(&vi);
  104. vorbis_p = 0;
  105. }
  106. if (theora_p) {
  107. ogg_stream_clear(&to);
  108. th_decode_free(td);
  109. th_comment_clear(&tc);
  110. th_info_clear(&ti);
  111. theora_p = 0;
  112. }
  113. ogg_sync_clear(&oy);
  114. #ifdef THEORA_USE_THREAD_STREAMING
  115. thread_exit = true;
  116. thread_sem->post(); //just in case
  117. thread.wait_to_finish();
  118. ring_buffer.clear();
  119. #endif
  120. theora_p = 0;
  121. vorbis_p = 0;
  122. videobuf_ready = 0;
  123. frames_pending = 0;
  124. videobuf_time = 0;
  125. theora_eos = false;
  126. vorbis_eos = false;
  127. if (file) {
  128. memdelete(file);
  129. }
  130. file = nullptr;
  131. playing = false;
  132. };
  133. void VideoStreamPlaybackTheora::set_file(const String &p_file) {
  134. ERR_FAIL_COND(playing);
  135. ogg_packet op;
  136. th_setup_info *ts = nullptr;
  137. file_name = p_file;
  138. if (file) {
  139. memdelete(file);
  140. }
  141. file = FileAccess::open(p_file, FileAccess::READ);
  142. ERR_FAIL_COND_MSG(!file, "Cannot open file '" + p_file + "'.");
  143. #ifdef THEORA_USE_THREAD_STREAMING
  144. thread_exit = false;
  145. thread_eof = false;
  146. //pre-fill buffer
  147. int to_read = ring_buffer.space_left();
  148. int read = file->get_buffer(read_buffer.ptr(), to_read);
  149. ring_buffer.write(read_buffer.ptr(), read);
  150. thread.start(_streaming_thread, this);
  151. #endif
  152. ogg_sync_init(&oy);
  153. /* init supporting Vorbis structures needed in header parsing */
  154. vorbis_info_init(&vi);
  155. vorbis_comment_init(&vc);
  156. /* init supporting Theora structures needed in header parsing */
  157. th_comment_init(&tc);
  158. th_info_init(&ti);
  159. theora_eos = false;
  160. vorbis_eos = false;
  161. /* Ogg file open; parse the headers */
  162. /* Only interested in Vorbis/Theora streams */
  163. int stateflag = 0;
  164. int audio_track_skip = audio_track;
  165. while (!stateflag) {
  166. int ret = buffer_data();
  167. if (ret == 0) {
  168. break;
  169. }
  170. while (ogg_sync_pageout(&oy, &og) > 0) {
  171. ogg_stream_state test;
  172. /* is this a mandated initial header? If not, stop parsing */
  173. if (!ogg_page_bos(&og)) {
  174. /* don't leak the page; get it into the appropriate stream */
  175. queue_page(&og);
  176. stateflag = 1;
  177. break;
  178. }
  179. ogg_stream_init(&test, ogg_page_serialno(&og));
  180. ogg_stream_pagein(&test, &og);
  181. ogg_stream_packetout(&test, &op);
  182. /* identify the codec: try theora */
  183. if (!theora_p && th_decode_headerin(&ti, &tc, &ts, &op) >= 0) {
  184. /* it is theora */
  185. copymem(&to, &test, sizeof(test));
  186. theora_p = 1;
  187. } else if (!vorbis_p && vorbis_synthesis_headerin(&vi, &vc, &op) >= 0) {
  188. /* it is vorbis */
  189. if (audio_track_skip) {
  190. vorbis_info_clear(&vi);
  191. vorbis_comment_clear(&vc);
  192. ogg_stream_clear(&test);
  193. vorbis_info_init(&vi);
  194. vorbis_comment_init(&vc);
  195. audio_track_skip--;
  196. } else {
  197. copymem(&vo, &test, sizeof(test));
  198. vorbis_p = 1;
  199. }
  200. } else {
  201. /* whatever it is, we don't care about it */
  202. ogg_stream_clear(&test);
  203. }
  204. }
  205. /* fall through to non-bos page parsing */
  206. }
  207. /* we're expecting more header packets. */
  208. while ((theora_p && theora_p < 3) || (vorbis_p && vorbis_p < 3)) {
  209. int ret;
  210. /* look for further theora headers */
  211. while (theora_p && (theora_p < 3) && (ret = ogg_stream_packetout(&to, &op))) {
  212. if (ret < 0) {
  213. fprintf(stderr, "Error parsing Theora stream headers; corrupt stream?\n");
  214. clear();
  215. return;
  216. }
  217. if (!th_decode_headerin(&ti, &tc, &ts, &op)) {
  218. fprintf(stderr, "Error parsing Theora stream headers; corrupt stream?\n");
  219. clear();
  220. return;
  221. }
  222. theora_p++;
  223. }
  224. /* look for more vorbis header packets */
  225. while (vorbis_p && (vorbis_p < 3) && (ret = ogg_stream_packetout(&vo, &op))) {
  226. if (ret < 0) {
  227. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  228. clear();
  229. return;
  230. }
  231. ret = vorbis_synthesis_headerin(&vi, &vc, &op);
  232. if (ret) {
  233. fprintf(stderr, "Error parsing Vorbis stream headers; corrupt stream?\n");
  234. clear();
  235. return;
  236. }
  237. vorbis_p++;
  238. if (vorbis_p == 3) {
  239. break;
  240. }
  241. }
  242. /* The header pages/packets will arrive before anything else we
  243. care about, or the stream is not obeying spec */
  244. if (ogg_sync_pageout(&oy, &og) > 0) {
  245. queue_page(&og); /* demux into the appropriate stream */
  246. } else {
  247. int ret2 = buffer_data(); /* someone needs more data */
  248. if (ret2 == 0) {
  249. fprintf(stderr, "End of file while searching for codec headers.\n");
  250. clear();
  251. return;
  252. }
  253. }
  254. }
  255. /* and now we have it all. initialize decoders */
  256. if (theora_p) {
  257. td = th_decode_alloc(&ti, ts);
  258. px_fmt = ti.pixel_fmt;
  259. switch (ti.pixel_fmt) {
  260. case TH_PF_420:
  261. //printf(" 4:2:0 video\n");
  262. break;
  263. case TH_PF_422:
  264. //printf(" 4:2:2 video\n");
  265. break;
  266. case TH_PF_444:
  267. //printf(" 4:4:4 video\n");
  268. break;
  269. case TH_PF_RSVD:
  270. default:
  271. printf(" video\n (UNKNOWN Chroma sampling!)\n");
  272. break;
  273. }
  274. th_decode_ctl(td, TH_DECCTL_GET_PPLEVEL_MAX, &pp_level_max,
  275. sizeof(pp_level_max));
  276. pp_level = 0;
  277. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof(pp_level));
  278. pp_inc = 0;
  279. int w;
  280. int h;
  281. w = ((ti.pic_x + ti.frame_width + 1) & ~1) - (ti.pic_x & ~1);
  282. h = ((ti.pic_y + ti.frame_height + 1) & ~1) - (ti.pic_y & ~1);
  283. size.x = w;
  284. size.y = h;
  285. Ref<Image> img;
  286. img.instance();
  287. img->create(w, h, false, Image::FORMAT_RGBA8);
  288. } else {
  289. /* tear down the partial theora setup */
  290. th_info_clear(&ti);
  291. th_comment_clear(&tc);
  292. }
  293. th_setup_free(ts);
  294. if (vorbis_p) {
  295. vorbis_synthesis_init(&vd, &vi);
  296. vorbis_block_init(&vd, &vb);
  297. //_setup(vi.channels, vi.rate);
  298. } else {
  299. /* tear down the partial vorbis setup */
  300. vorbis_info_clear(&vi);
  301. vorbis_comment_clear(&vc);
  302. }
  303. playing = false;
  304. buffering = true;
  305. time = 0;
  306. audio_frames_wrote = 0;
  307. };
  308. float VideoStreamPlaybackTheora::get_time() const {
  309. // FIXME: AudioServer output latency was fixed in af9bb0e, previously it used to
  310. // systematically return 0. Now that it gives a proper latency, it broke this
  311. // code where the delay compensation likely never really worked.
  312. return time - /* AudioServer::get_singleton()->get_output_latency() - */ delay_compensation;
  313. };
  314. Ref<Texture2D> VideoStreamPlaybackTheora::get_texture() const {
  315. return texture;
  316. }
  317. void VideoStreamPlaybackTheora::update(float p_delta) {
  318. if (!file) {
  319. return;
  320. }
  321. if (!playing || paused) {
  322. //printf("not playing\n");
  323. return;
  324. };
  325. #ifdef THEORA_USE_THREAD_STREAMING
  326. thread_sem->post();
  327. #endif
  328. time += p_delta;
  329. if (videobuf_time > get_time()) {
  330. return; //no new frames need to be produced
  331. }
  332. bool frame_done = false;
  333. bool audio_done = !vorbis_p;
  334. while (!frame_done || (!audio_done && !vorbis_eos)) {
  335. //a frame needs to be produced
  336. ogg_packet op;
  337. bool no_theora = false;
  338. bool buffer_full = false;
  339. while (vorbis_p && !audio_done && !buffer_full) {
  340. int ret;
  341. float **pcm;
  342. /* if there's pending, decoded audio, grab it */
  343. ret = vorbis_synthesis_pcmout(&vd, &pcm);
  344. if (ret > 0) {
  345. const int AUXBUF_LEN = 4096;
  346. int to_read = ret;
  347. float aux_buffer[AUXBUF_LEN];
  348. while (to_read) {
  349. int m = MIN(AUXBUF_LEN / vi.channels, to_read);
  350. int count = 0;
  351. for (int j = 0; j < m; j++) {
  352. for (int i = 0; i < vi.channels; i++) {
  353. aux_buffer[count++] = pcm[i][j];
  354. }
  355. }
  356. if (mix_callback) {
  357. int mixed = mix_callback(mix_udata, aux_buffer, m);
  358. to_read -= mixed;
  359. if (mixed != m) { //could mix no more
  360. buffer_full = true;
  361. break;
  362. }
  363. } else {
  364. to_read -= m; //just pretend we sent the audio
  365. }
  366. }
  367. vorbis_synthesis_read(&vd, ret - to_read);
  368. audio_frames_wrote += ret - to_read;
  369. } else {
  370. /* no pending audio; is there a pending packet to decode? */
  371. if (ogg_stream_packetout(&vo, &op) > 0) {
  372. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  373. vorbis_synthesis_blockin(&vd, &vb);
  374. }
  375. } else { /* we need more data; break out to suck in another page */
  376. break;
  377. };
  378. }
  379. audio_done = videobuf_time < (audio_frames_wrote / float(vi.rate));
  380. if (buffer_full) {
  381. break;
  382. }
  383. }
  384. while (theora_p && !frame_done) {
  385. /* theora is one in, one out... */
  386. if (ogg_stream_packetout(&to, &op) > 0) {
  387. if (false && pp_inc) {
  388. pp_level += pp_inc;
  389. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level,
  390. sizeof(pp_level));
  391. pp_inc = 0;
  392. }
  393. /*HACK: This should be set after a seek or a gap, but we might not have
  394. a granulepos for the first packet (we only have them for the last
  395. packet on a page), so we just set it as often as we get it.
  396. To do this right, we should back-track from the last packet on the
  397. page and compute the correct granulepos for the first packet after
  398. a seek or a gap.*/
  399. if (op.granulepos >= 0) {
  400. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos,
  401. sizeof(op.granulepos));
  402. }
  403. ogg_int64_t videobuf_granulepos;
  404. if (th_decode_packetin(td, &op, &videobuf_granulepos) == 0) {
  405. videobuf_time = th_granule_time(td, videobuf_granulepos);
  406. //printf("frame time %f, play time %f, ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  407. /* is it already too old to be useful? This is only actually
  408. useful cosmetically after a SIGSTOP. Note that we have to
  409. decode the frame even if we don't show it (for now) due to
  410. keyframing. Soon enough libtheora will be able to deal
  411. with non-keyframe seeks. */
  412. if (videobuf_time >= get_time()) {
  413. frame_done = true;
  414. } else {
  415. /*If we are too slow, reduce the pp level.*/
  416. pp_inc = pp_level > 0 ? -1 : 0;
  417. }
  418. }
  419. } else {
  420. no_theora = true;
  421. break;
  422. }
  423. }
  424. #ifdef THEORA_USE_THREAD_STREAMING
  425. if (file && thread_eof && no_theora && theora_eos && ring_buffer.data_left() == 0) {
  426. #else
  427. if (file && /*!videobuf_ready && */ no_theora && theora_eos) {
  428. #endif
  429. //printf("video done, stopping\n");
  430. stop();
  431. return;
  432. };
  433. if (!frame_done || !audio_done) {
  434. //what's the point of waiting for audio to grab a page?
  435. buffer_data();
  436. while (ogg_sync_pageout(&oy, &og) > 0) {
  437. queue_page(&og);
  438. }
  439. }
  440. /* If playback has begun, top audio buffer off immediately. */
  441. //if(stateflag) audio_write_nonblocking();
  442. /* are we at or past time for this video frame? */
  443. if (videobuf_ready && videobuf_time <= get_time()) {
  444. //video_write();
  445. //videobuf_ready=0;
  446. } else {
  447. //printf("frame at %f not ready (time %f), ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  448. }
  449. float tdiff = videobuf_time - get_time();
  450. /*If we have lots of extra time, increase the post-processing level.*/
  451. if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
  452. pp_inc = pp_level < pp_level_max ? 1 : 0;
  453. } else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
  454. pp_inc = pp_level > 0 ? -1 : 0;
  455. }
  456. }
  457. video_write();
  458. };
  459. void VideoStreamPlaybackTheora::play() {
  460. if (!playing) {
  461. time = 0;
  462. } else {
  463. stop();
  464. }
  465. playing = true;
  466. delay_compensation = ProjectSettings::get_singleton()->get("audio/video_delay_compensation_ms");
  467. delay_compensation /= 1000.0;
  468. };
  469. void VideoStreamPlaybackTheora::stop() {
  470. if (playing) {
  471. clear();
  472. set_file(file_name); //reset
  473. }
  474. playing = false;
  475. time = 0;
  476. };
  477. bool VideoStreamPlaybackTheora::is_playing() const {
  478. return playing;
  479. };
  480. void VideoStreamPlaybackTheora::set_paused(bool p_paused) {
  481. paused = p_paused;
  482. };
  483. bool VideoStreamPlaybackTheora::is_paused() const {
  484. return paused;
  485. };
  486. void VideoStreamPlaybackTheora::set_loop(bool p_enable) {
  487. }
  488. bool VideoStreamPlaybackTheora::has_loop() const {
  489. return false;
  490. };
  491. float VideoStreamPlaybackTheora::get_length() const {
  492. return 0;
  493. };
  494. String VideoStreamPlaybackTheora::get_stream_name() const {
  495. return "";
  496. };
  497. int VideoStreamPlaybackTheora::get_loop_count() const {
  498. return 0;
  499. };
  500. float VideoStreamPlaybackTheora::get_playback_position() const {
  501. return get_time();
  502. };
  503. void VideoStreamPlaybackTheora::seek(float p_time) {
  504. }
  505. void VideoStreamPlaybackTheora::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  506. mix_callback = p_callback;
  507. mix_udata = p_userdata;
  508. }
  509. int VideoStreamPlaybackTheora::get_channels() const {
  510. return vi.channels;
  511. }
  512. void VideoStreamPlaybackTheora::set_audio_track(int p_idx) {
  513. audio_track = p_idx;
  514. }
  515. int VideoStreamPlaybackTheora::get_mix_rate() const {
  516. return vi.rate;
  517. }
  518. #ifdef THEORA_USE_THREAD_STREAMING
  519. void VideoStreamPlaybackTheora::_streaming_thread(void *ud) {
  520. VideoStreamPlaybackTheora *vs = (VideoStreamPlaybackTheora *)ud;
  521. while (!vs->thread_exit) {
  522. //just fill back the buffer
  523. if (!vs->thread_eof) {
  524. int to_read = vs->ring_buffer.space_left();
  525. if (to_read) {
  526. int read = vs->file->get_buffer(vs->read_buffer.ptr(), to_read);
  527. vs->ring_buffer.write(vs->read_buffer.ptr(), read);
  528. vs->thread_eof = vs->file->eof_reached();
  529. }
  530. }
  531. vs->thread_sem->wait();
  532. }
  533. }
  534. #endif
  535. VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
  536. texture = Ref<ImageTexture>(memnew(ImageTexture));
  537. #ifdef THEORA_USE_THREAD_STREAMING
  538. int rb_power = nearest_shift(RB_SIZE_KB * 1024);
  539. ring_buffer.resize(rb_power);
  540. read_buffer.resize(RB_SIZE_KB * 1024);
  541. thread_sem = Semaphore::create();
  542. #endif
  543. };
  544. VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
  545. #ifdef THEORA_USE_THREAD_STREAMING
  546. memdelete(thread_sem);
  547. #endif
  548. clear();
  549. if (file) {
  550. memdelete(file);
  551. }
  552. };
  553. void VideoStreamTheora::_bind_methods() {
  554. ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStreamTheora::set_file);
  555. ClassDB::bind_method(D_METHOD("get_file"), &VideoStreamTheora::get_file);
  556. ADD_PROPERTY(PropertyInfo(Variant::STRING, "file", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_file", "get_file");
  557. }
  558. ////////////
  559. RES ResourceFormatLoaderTheora::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) {
  560. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  561. if (!f) {
  562. if (r_error) {
  563. *r_error = ERR_CANT_OPEN;
  564. }
  565. return RES();
  566. }
  567. VideoStreamTheora *stream = memnew(VideoStreamTheora);
  568. stream->set_file(p_path);
  569. Ref<VideoStreamTheora> ogv_stream = Ref<VideoStreamTheora>(stream);
  570. if (r_error) {
  571. *r_error = OK;
  572. }
  573. f->close();
  574. memdelete(f);
  575. return ogv_stream;
  576. }
  577. void ResourceFormatLoaderTheora::get_recognized_extensions(List<String> *p_extensions) const {
  578. p_extensions->push_back("ogv");
  579. }
  580. bool ResourceFormatLoaderTheora::handles_type(const String &p_type) const {
  581. return ClassDB::is_parent_class(p_type, "VideoStream");
  582. }
  583. String ResourceFormatLoaderTheora::get_resource_type(const String &p_path) const {
  584. String el = p_path.get_extension().to_lower();
  585. if (el == "ogv") {
  586. return "VideoStreamTheora";
  587. }
  588. return "";
  589. }