| 1 | /**************************************************************************/ |
| 2 | /* video_stream.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 | |
| 31 | #ifndef VIDEO_STREAM_H |
| 32 | #define VIDEO_STREAM_H |
| 33 | |
| 34 | #include "core/io/file_access.h" |
| 35 | #include "scene/resources/texture.h" |
| 36 | |
| 37 | class VideoStreamPlayback : public Resource { |
| 38 | GDCLASS(VideoStreamPlayback, Resource); |
| 39 | |
| 40 | public: |
| 41 | typedef int (*AudioMixCallback)(void *p_udata, const float *p_data, int p_frames); |
| 42 | |
| 43 | protected: |
| 44 | AudioMixCallback mix_callback = nullptr; |
| 45 | void *mix_udata = nullptr; |
| 46 | mutable int _channel_count = 0; // Used only to assist with bounds checking in mix_audio. |
| 47 | |
| 48 | static void _bind_methods(); |
| 49 | GDVIRTUAL0(_stop); |
| 50 | GDVIRTUAL0(_play); |
| 51 | GDVIRTUAL0RC(bool, _is_playing); |
| 52 | GDVIRTUAL1(_set_paused, bool); |
| 53 | GDVIRTUAL0RC(bool, _is_paused); |
| 54 | GDVIRTUAL0RC(double, _get_length); |
| 55 | GDVIRTUAL0RC(double, _get_playback_position); |
| 56 | GDVIRTUAL1(_seek, double); |
| 57 | GDVIRTUAL1(_set_audio_track, int); |
| 58 | GDVIRTUAL0RC(Ref<Texture2D>, _get_texture); |
| 59 | GDVIRTUAL1(_update, double); |
| 60 | GDVIRTUAL0RC(int, _get_channels); |
| 61 | GDVIRTUAL0RC(int, _get_mix_rate); |
| 62 | |
| 63 | int mix_audio(int num_frames, PackedFloat32Array buffer = {}, int offset = 0); |
| 64 | |
| 65 | public: |
| 66 | VideoStreamPlayback(); |
| 67 | virtual ~VideoStreamPlayback(); |
| 68 | |
| 69 | virtual void stop(); |
| 70 | virtual void play(); |
| 71 | |
| 72 | virtual bool is_playing() const; |
| 73 | |
| 74 | virtual void set_paused(bool p_paused); |
| 75 | virtual bool is_paused() const; |
| 76 | |
| 77 | virtual double get_length() const; |
| 78 | |
| 79 | virtual double get_playback_position() const; |
| 80 | virtual void seek(double p_time); |
| 81 | |
| 82 | virtual void set_audio_track(int p_idx); |
| 83 | |
| 84 | virtual Ref<Texture2D> get_texture() const; |
| 85 | virtual void update(double p_delta); |
| 86 | |
| 87 | virtual void set_mix_callback(AudioMixCallback p_callback, void *p_userdata); |
| 88 | virtual int get_channels() const; |
| 89 | virtual int get_mix_rate() const; |
| 90 | }; |
| 91 | |
| 92 | class VideoStream : public Resource { |
| 93 | GDCLASS(VideoStream, Resource); |
| 94 | OBJ_SAVE_TYPE(VideoStream); |
| 95 | |
| 96 | protected: |
| 97 | static void |
| 98 | _bind_methods(); |
| 99 | |
| 100 | GDVIRTUAL0R(Ref<VideoStreamPlayback>, _instantiate_playback); |
| 101 | |
| 102 | String file; |
| 103 | int audio_track = 0; |
| 104 | |
| 105 | public: |
| 106 | void set_file(const String &p_file); |
| 107 | String get_file(); |
| 108 | |
| 109 | virtual void set_audio_track(int p_track); |
| 110 | virtual Ref<VideoStreamPlayback> instantiate_playback(); |
| 111 | |
| 112 | VideoStream(); |
| 113 | ~VideoStream(); |
| 114 | }; |
| 115 | |
| 116 | #endif // VIDEO_STREAM_H |
| 117 | |