video_stream_theora.h 5.6 KB

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