video_stream_theora.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* video_stream_theora.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef VIDEO_STREAM_THEORA_H
  31. #define VIDEO_STREAM_THEORA_H
  32. #include "io/resource_loader.h"
  33. #include "os/file_access.h"
  34. #include "os/semaphore.h"
  35. #include "os/thread.h"
  36. #include "ring_buffer.h"
  37. #include "scene/resources/video_stream.h"
  38. #include <theora/theoradec.h>
  39. #include <vorbis/codec.h>
  40. //#define THEORA_USE_THREAD_STREAMING
  41. class VideoStreamPlaybackTheora : public VideoStreamPlayback {
  42. OBJ_TYPE(VideoStreamPlaybackTheora, VideoStreamPlayback);
  43. enum {
  44. MAX_FRAMES = 4,
  45. };
  46. //Image frames[MAX_FRAMES];
  47. Image::Format format;
  48. DVector<uint8_t> frame_data;
  49. int frames_pending;
  50. FileAccess *file;
  51. String file_name;
  52. int audio_frames_wrote;
  53. Point2i size;
  54. int buffer_data();
  55. int queue_page(ogg_page *page);
  56. void video_write(void);
  57. float get_time() const;
  58. bool theora_eos;
  59. bool vorbis_eos;
  60. ogg_sync_state oy;
  61. ogg_page og;
  62. ogg_stream_state vo;
  63. ogg_stream_state to;
  64. th_info ti;
  65. th_comment tc;
  66. th_dec_ctx *td;
  67. vorbis_info vi;
  68. vorbis_dsp_state vd;
  69. vorbis_block vb;
  70. vorbis_comment vc;
  71. th_pixel_fmt px_fmt;
  72. double videobuf_time;
  73. int pp_inc;
  74. int theora_p;
  75. int vorbis_p;
  76. int pp_level_max;
  77. int pp_level;
  78. int videobuf_ready;
  79. bool playing;
  80. bool buffering;
  81. double last_update_time;
  82. double time;
  83. double delay_compensation;
  84. Ref<ImageTexture> texture;
  85. AudioMixCallback mix_callback;
  86. void *mix_udata;
  87. bool paused;
  88. #ifdef THEORA_USE_THREAD_STREAMING
  89. enum {
  90. RB_SIZE_KB = 1024
  91. };
  92. RingBuffer<uint8_t> ring_buffer;
  93. Vector<uint8_t> read_buffer;
  94. bool thread_eof;
  95. Semaphore *thread_sem;
  96. Thread *thread;
  97. volatile bool thread_exit;
  98. static void _streaming_thread(void *ud);
  99. #endif
  100. int audio_track;
  101. protected:
  102. void clear();
  103. public:
  104. virtual void play();
  105. virtual void stop();
  106. virtual bool is_playing() const;
  107. virtual void set_paused(bool p_paused);
  108. virtual bool is_paused(bool p_paused) const;
  109. virtual void set_loop(bool p_enable);
  110. virtual bool has_loop() const;
  111. virtual float get_length() const;
  112. virtual String get_stream_name() const;
  113. virtual int get_loop_count() const;
  114. virtual float get_pos() const;
  115. virtual void seek_pos(float p_time);
  116. void set_file(const String &p_file);
  117. virtual Ref<Texture> get_texture();
  118. virtual void update(float p_delta);
  119. virtual void set_mix_callback(AudioMixCallback p_callback, void *p_userdata);
  120. virtual int get_channels() const;
  121. virtual int get_mix_rate() const;
  122. virtual void set_audio_track(int p_idx);
  123. VideoStreamPlaybackTheora();
  124. ~VideoStreamPlaybackTheora();
  125. };
  126. class VideoStreamTheora : public VideoStream {
  127. OBJ_TYPE(VideoStreamTheora, VideoStream);
  128. String file;
  129. int audio_track;
  130. public:
  131. Ref<VideoStreamPlayback> instance_playback() {
  132. Ref<VideoStreamPlaybackTheora> pb = memnew(VideoStreamPlaybackTheora);
  133. pb->set_audio_track(audio_track);
  134. pb->set_file(file);
  135. return pb;
  136. }
  137. void set_file(const String &p_file) { file = p_file; }
  138. void set_audio_track(int p_track) { audio_track = p_track; }
  139. VideoStreamTheora() { audio_track = 0; }
  140. };
  141. class ResourceFormatLoaderVideoStreamTheora : public ResourceFormatLoader {
  142. public:
  143. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  144. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  145. virtual bool handles_type(const String &p_type) const;
  146. virtual String get_resource_type(const String &p_path) const;
  147. };
  148. #endif