video_stream_gdnative.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**************************************************************************/
  2. /* video_stream_gdnative.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_GDNATIVE_H
  31. #define VIDEO_STREAM_GDNATIVE_H
  32. #include "../gdnative.h"
  33. #include "core/os/file_access.h"
  34. #include "scene/resources/texture.h"
  35. #include "scene/resources/video_stream.h"
  36. struct VideoDecoderGDNative {
  37. const godot_videodecoder_interface_gdnative *interface;
  38. String plugin_name;
  39. Vector<String> supported_extensions;
  40. VideoDecoderGDNative() :
  41. interface(nullptr),
  42. plugin_name("none") {}
  43. VideoDecoderGDNative(const godot_videodecoder_interface_gdnative *p_interface) :
  44. interface(p_interface),
  45. plugin_name(p_interface->get_plugin_name()) {
  46. _get_supported_extensions();
  47. }
  48. private:
  49. void _get_supported_extensions() {
  50. supported_extensions.clear();
  51. int num_ext;
  52. const char **supported_ext = interface->get_supported_extensions(&num_ext);
  53. for (int i = 0; i < num_ext; i++) {
  54. supported_extensions.push_back(supported_ext[i]);
  55. }
  56. }
  57. };
  58. class VideoDecoderServer {
  59. private:
  60. Vector<VideoDecoderGDNative *> decoders;
  61. Map<String, int> extensions;
  62. static VideoDecoderServer *instance;
  63. public:
  64. static VideoDecoderServer *get_instance() {
  65. return instance;
  66. }
  67. const Map<String, int> &get_extensions() {
  68. return extensions;
  69. }
  70. void register_decoder_interface(const godot_videodecoder_interface_gdnative *p_interface) {
  71. VideoDecoderGDNative *decoder = memnew(VideoDecoderGDNative(p_interface));
  72. int index = decoders.size();
  73. for (int i = 0; i < decoder->supported_extensions.size(); i++) {
  74. extensions[decoder->supported_extensions[i]] = index;
  75. }
  76. decoders.push_back(decoder);
  77. }
  78. VideoDecoderGDNative *get_decoder(const String &extension) {
  79. if (extensions.size() == 0 || !extensions.has(extension)) {
  80. return nullptr;
  81. }
  82. return decoders[extensions[extension]];
  83. }
  84. VideoDecoderServer() {
  85. instance = this;
  86. }
  87. ~VideoDecoderServer() {
  88. for (int i = 0; i < decoders.size(); i++) {
  89. memdelete(decoders[i]);
  90. }
  91. decoders.clear();
  92. instance = nullptr;
  93. }
  94. };
  95. class VideoStreamPlaybackGDNative : public VideoStreamPlayback {
  96. GDCLASS(VideoStreamPlaybackGDNative, VideoStreamPlayback);
  97. Ref<ImageTexture> texture;
  98. bool playing;
  99. bool paused;
  100. Vector2 texture_size;
  101. void *mix_udata;
  102. AudioMixCallback mix_callback;
  103. int num_channels;
  104. float time;
  105. bool seek_backward;
  106. int mix_rate;
  107. double delay_compensation;
  108. float *pcm;
  109. int pcm_write_idx;
  110. int samples_decoded;
  111. void cleanup();
  112. void update_texture();
  113. protected:
  114. String file_name;
  115. FileAccess *file;
  116. const godot_videodecoder_interface_gdnative *interface;
  117. void *data_struct;
  118. public:
  119. VideoStreamPlaybackGDNative();
  120. ~VideoStreamPlaybackGDNative();
  121. void set_interface(const godot_videodecoder_interface_gdnative *p_interface);
  122. bool open_file(const String &p_file);
  123. virtual void stop();
  124. virtual void play();
  125. virtual bool is_playing() const;
  126. virtual void set_paused(bool p_paused);
  127. virtual bool is_paused() const;
  128. virtual void set_loop(bool p_enable);
  129. virtual bool has_loop() const;
  130. virtual float get_length() const;
  131. virtual float get_playback_position() const;
  132. virtual void seek(float p_time);
  133. virtual void set_audio_track(int p_idx);
  134. //virtual int mix(int16_t* p_buffer,int p_frames)=0;
  135. virtual Ref<Texture> get_texture() const;
  136. virtual void update(float p_delta);
  137. virtual void set_mix_callback(AudioMixCallback p_callback, void *p_userdata);
  138. virtual int get_channels() const;
  139. virtual int get_mix_rate() const;
  140. };
  141. class VideoStreamGDNative : public VideoStream {
  142. GDCLASS(VideoStreamGDNative, VideoStream);
  143. String file;
  144. int audio_track;
  145. protected:
  146. static void
  147. _bind_methods();
  148. public:
  149. void set_file(const String &p_file);
  150. String get_file();
  151. virtual void set_audio_track(int p_track);
  152. virtual Ref<VideoStreamPlayback> instance_playback();
  153. VideoStreamGDNative() {}
  154. };
  155. class ResourceFormatLoaderVideoStreamGDNative : public ResourceFormatLoader {
  156. public:
  157. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_no_subresource_cache = false);
  158. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  159. virtual bool handles_type(const String &p_type) const;
  160. virtual String get_resource_type(const String &p_path) const;
  161. };
  162. #endif // VIDEO_STREAM_GDNATIVE_H