video_stream_theora.cpp 19 KB

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