video_stream_theora.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/os/os.h"
  32. #include "core/project_settings.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. uint64_t 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. PoolVector<uint8_t>::Write w = frame_data.write();
  77. char *dst = (char *)w.ptr();
  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->set_data(img); //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. uint64_t 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. memcpy(&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. memcpy(&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. texture->create(w, h, Image::FORMAT_RGBA8, Texture::FLAG_FILTER | Texture::FLAG_VIDEO_SURFACE);
  286. } else {
  287. /* tear down the partial theora setup */
  288. th_info_clear(&ti);
  289. th_comment_clear(&tc);
  290. }
  291. th_setup_free(ts);
  292. if (vorbis_p) {
  293. vorbis_synthesis_init(&vd, &vi);
  294. vorbis_block_init(&vd, &vb);
  295. //_setup(vi.channels, vi.rate);
  296. } else {
  297. /* tear down the partial vorbis setup */
  298. vorbis_info_clear(&vi);
  299. vorbis_comment_clear(&vc);
  300. }
  301. playing = false;
  302. buffering = true;
  303. time = 0;
  304. audio_frames_wrote = 0;
  305. };
  306. float VideoStreamPlaybackTheora::get_time() const {
  307. // FIXME: AudioServer output latency was fixed in af9bb0e, previously it used to
  308. // systematically return 0. Now that it gives a proper latency, it broke this
  309. // code where the delay compensation likely never really worked.
  310. return time - /* AudioServer::get_singleton()->get_output_latency() - */ delay_compensation;
  311. };
  312. Ref<Texture> VideoStreamPlaybackTheora::get_texture() const {
  313. return texture;
  314. }
  315. void VideoStreamPlaybackTheora::update(float p_delta) {
  316. if (!file) {
  317. return;
  318. }
  319. if (!playing || paused) {
  320. //printf("not playing\n");
  321. return;
  322. };
  323. #ifdef THEORA_USE_THREAD_STREAMING
  324. thread_sem.post();
  325. #endif
  326. time += p_delta;
  327. if (videobuf_time > get_time()) {
  328. return; //no new frames need to be produced
  329. }
  330. bool frame_done = false;
  331. bool audio_done = !vorbis_p;
  332. while (!frame_done || (!audio_done && !vorbis_eos)) {
  333. //a frame needs to be produced
  334. ogg_packet op;
  335. bool no_theora = false;
  336. bool buffer_full = false;
  337. while (vorbis_p && !audio_done && !buffer_full) {
  338. int ret;
  339. float **pcm;
  340. /* if there's pending, decoded audio, grab it */
  341. ret = vorbis_synthesis_pcmout(&vd, &pcm);
  342. if (ret > 0) {
  343. const int AUXBUF_LEN = 4096;
  344. int to_read = ret;
  345. float aux_buffer[AUXBUF_LEN];
  346. while (to_read) {
  347. int m = MIN(AUXBUF_LEN / vi.channels, to_read);
  348. int count = 0;
  349. for (int j = 0; j < m; j++) {
  350. for (int i = 0; i < vi.channels; i++) {
  351. aux_buffer[count++] = pcm[i][j];
  352. }
  353. }
  354. if (mix_callback) {
  355. int mixed = mix_callback(mix_udata, aux_buffer, m);
  356. to_read -= mixed;
  357. if (mixed != m) { //could mix no more
  358. buffer_full = true;
  359. break;
  360. }
  361. } else {
  362. to_read -= m; //just pretend we sent the audio
  363. }
  364. }
  365. vorbis_synthesis_read(&vd, ret - to_read);
  366. audio_frames_wrote += ret - to_read;
  367. } else {
  368. /* no pending audio; is there a pending packet to decode? */
  369. if (ogg_stream_packetout(&vo, &op) > 0) {
  370. if (vorbis_synthesis(&vb, &op) == 0) { /* test for success! */
  371. vorbis_synthesis_blockin(&vd, &vb);
  372. }
  373. } else { /* we need more data; break out to suck in another page */
  374. break;
  375. };
  376. }
  377. audio_done = videobuf_time < (audio_frames_wrote / float(vi.rate));
  378. if (buffer_full) {
  379. break;
  380. }
  381. }
  382. while (theora_p && !frame_done) {
  383. /* theora is one in, one out... */
  384. if (ogg_stream_packetout(&to, &op) > 0) {
  385. if (false && pp_inc) {
  386. pp_level += pp_inc;
  387. th_decode_ctl(td, TH_DECCTL_SET_PPLEVEL, &pp_level,
  388. sizeof(pp_level));
  389. pp_inc = 0;
  390. }
  391. /*HACK: This should be set after a seek or a gap, but we might not have
  392. a granulepos for the first packet (we only have them for the last
  393. packet on a page), so we just set it as often as we get it.
  394. To do this right, we should back-track from the last packet on the
  395. page and compute the correct granulepos for the first packet after
  396. a seek or a gap.*/
  397. if (op.granulepos >= 0) {
  398. th_decode_ctl(td, TH_DECCTL_SET_GRANPOS, &op.granulepos,
  399. sizeof(op.granulepos));
  400. }
  401. ogg_int64_t videobuf_granulepos;
  402. if (th_decode_packetin(td, &op, &videobuf_granulepos) == 0) {
  403. videobuf_time = th_granule_time(td, videobuf_granulepos);
  404. //printf("frame time %f, play time %f, ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  405. /* is it already too old to be useful? This is only actually
  406. useful cosmetically after a SIGSTOP. Note that we have to
  407. decode the frame even if we don't show it (for now) due to
  408. keyframing. Soon enough libtheora will be able to deal
  409. with non-keyframe seeks. */
  410. if (videobuf_time >= get_time()) {
  411. frame_done = true;
  412. } else {
  413. /*If we are too slow, reduce the pp level.*/
  414. pp_inc = pp_level > 0 ? -1 : 0;
  415. }
  416. }
  417. } else {
  418. no_theora = true;
  419. break;
  420. }
  421. }
  422. #ifdef THEORA_USE_THREAD_STREAMING
  423. if (file && thread_eof && no_theora && theora_eos && ring_buffer.data_left() == 0) {
  424. #else
  425. if (file && /*!videobuf_ready && */ no_theora && theora_eos) {
  426. #endif
  427. //printf("video done, stopping\n");
  428. stop();
  429. return;
  430. };
  431. if (!frame_done || !audio_done) {
  432. //what's the point of waiting for audio to grab a page?
  433. buffer_data();
  434. while (ogg_sync_pageout(&oy, &og) > 0) {
  435. queue_page(&og);
  436. }
  437. }
  438. /* If playback has begun, top audio buffer off immediately. */
  439. //if(stateflag) audio_write_nonblocking();
  440. /* are we at or past time for this video frame? */
  441. if (videobuf_ready && videobuf_time <= get_time()) {
  442. //video_write();
  443. //videobuf_ready=0;
  444. } else {
  445. //printf("frame at %f not ready (time %f), ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
  446. }
  447. float tdiff = videobuf_time - get_time();
  448. /*If we have lots of extra time, increase the post-processing level.*/
  449. if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator) {
  450. pp_inc = pp_level < pp_level_max ? 1 : 0;
  451. } else if (tdiff < ti.fps_denominator * 0.05 / ti.fps_numerator) {
  452. pp_inc = pp_level > 0 ? -1 : 0;
  453. }
  454. }
  455. video_write();
  456. };
  457. void VideoStreamPlaybackTheora::play() {
  458. if (!playing) {
  459. time = 0;
  460. } else {
  461. stop();
  462. }
  463. playing = true;
  464. delay_compensation = ProjectSettings::get_singleton()->get("audio/video_delay_compensation_ms");
  465. delay_compensation /= 1000.0;
  466. };
  467. void VideoStreamPlaybackTheora::stop() {
  468. if (playing) {
  469. clear();
  470. set_file(file_name); //reset
  471. }
  472. playing = false;
  473. time = 0;
  474. };
  475. bool VideoStreamPlaybackTheora::is_playing() const {
  476. return playing;
  477. };
  478. void VideoStreamPlaybackTheora::set_paused(bool p_paused) {
  479. paused = p_paused;
  480. };
  481. bool VideoStreamPlaybackTheora::is_paused() const {
  482. return paused;
  483. };
  484. void VideoStreamPlaybackTheora::set_loop(bool p_enable){
  485. };
  486. bool VideoStreamPlaybackTheora::has_loop() const {
  487. return false;
  488. };
  489. float VideoStreamPlaybackTheora::get_length() const {
  490. return 0;
  491. };
  492. String VideoStreamPlaybackTheora::get_stream_name() const {
  493. return "";
  494. };
  495. int VideoStreamPlaybackTheora::get_loop_count() const {
  496. return 0;
  497. };
  498. float VideoStreamPlaybackTheora::get_playback_position() const {
  499. return get_time();
  500. };
  501. void VideoStreamPlaybackTheora::seek(float p_time) {
  502. WARN_PRINT_ONCE("Seeking in Theora and WebM videos is not implemented yet (it's only supported for GDNative-provided video streams).");
  503. }
  504. void VideoStreamPlaybackTheora::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
  505. mix_callback = p_callback;
  506. mix_udata = p_userdata;
  507. }
  508. int VideoStreamPlaybackTheora::get_channels() const {
  509. return vi.channels;
  510. }
  511. void VideoStreamPlaybackTheora::set_audio_track(int p_idx) {
  512. audio_track = p_idx;
  513. }
  514. int VideoStreamPlaybackTheora::get_mix_rate() const {
  515. return vi.rate;
  516. }
  517. #ifdef THEORA_USE_THREAD_STREAMING
  518. void VideoStreamPlaybackTheora::_streaming_thread(void *ud) {
  519. VideoStreamPlaybackTheora *vs = (VideoStreamPlaybackTheora *)ud;
  520. while (!vs->thread_exit) {
  521. //just fill back the buffer
  522. if (!vs->thread_eof) {
  523. int to_read = vs->ring_buffer.space_left();
  524. if (to_read > 0) {
  525. uint64_t read = vs->file->get_buffer(vs->read_buffer.ptr(), to_read);
  526. vs->ring_buffer.write(vs->read_buffer.ptr(), read);
  527. vs->thread_eof = vs->file->eof_reached();
  528. }
  529. }
  530. vs->thread_sem.wait();
  531. }
  532. }
  533. #endif
  534. VideoStreamPlaybackTheora::VideoStreamPlaybackTheora() {
  535. file = nullptr;
  536. theora_p = 0;
  537. vorbis_p = 0;
  538. videobuf_ready = 0;
  539. playing = false;
  540. frames_pending = 0;
  541. videobuf_time = 0;
  542. paused = false;
  543. buffering = false;
  544. texture = Ref<ImageTexture>(memnew(ImageTexture));
  545. mix_callback = nullptr;
  546. mix_udata = nullptr;
  547. audio_track = 0;
  548. delay_compensation = 0;
  549. audio_frames_wrote = 0;
  550. #ifdef THEORA_USE_THREAD_STREAMING
  551. int rb_power = nearest_shift(RB_SIZE_KB * 1024);
  552. ring_buffer.resize(rb_power);
  553. read_buffer.resize(RB_SIZE_KB * 1024);
  554. thread_exit = false;
  555. thread_eof = false;
  556. #endif
  557. };
  558. VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora() {
  559. clear();
  560. if (file) {
  561. memdelete(file);
  562. }
  563. };
  564. void VideoStreamTheora::_bind_methods() {
  565. ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStreamTheora::set_file);
  566. ClassDB::bind_method(D_METHOD("get_file"), &VideoStreamTheora::get_file);
  567. ADD_PROPERTY(PropertyInfo(Variant::STRING, "file", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_file", "get_file");
  568. }
  569. ////////////
  570. RES ResourceFormatLoaderTheora::load(const String &p_path, const String &p_original_path, Error *r_error) {
  571. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  572. if (!f) {
  573. if (r_error) {
  574. *r_error = ERR_CANT_OPEN;
  575. }
  576. return RES();
  577. }
  578. VideoStreamTheora *stream = memnew(VideoStreamTheora);
  579. stream->set_file(p_path);
  580. Ref<VideoStreamTheora> ogv_stream = Ref<VideoStreamTheora>(stream);
  581. if (r_error) {
  582. *r_error = OK;
  583. }
  584. f->close();
  585. memdelete(f);
  586. return ogv_stream;
  587. }
  588. void ResourceFormatLoaderTheora::get_recognized_extensions(List<String> *p_extensions) const {
  589. p_extensions->push_back("ogv");
  590. }
  591. bool ResourceFormatLoaderTheora::handles_type(const String &p_type) const {
  592. return ClassDB::is_parent_class(p_type, "VideoStream");
  593. }
  594. String ResourceFormatLoaderTheora::get_resource_type(const String &p_path) const {
  595. String el = p_path.get_extension().to_lower();
  596. if (el == "ogv") {
  597. return "VideoStreamTheora";
  598. }
  599. return "";
  600. }