video_stream_theora.h 5.6 KB

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