video_stream_gdnative.h 6.6 KB

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