| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  video_stream.cpp                                                      */ | 
|---|
| 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 | #include "video_stream.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "core/config/project_settings.h" | 
|---|
| 34 | #include "servers/audio_server.h" | 
|---|
| 35 |  | 
|---|
| 36 | // VideoStreamPlayback starts here. | 
|---|
| 37 |  | 
|---|
| 38 | void VideoStreamPlayback::_bind_methods() { | 
|---|
| 39 | ClassDB::bind_method(D_METHOD( "mix_audio", "num_frames", "buffer", "offset"), &VideoStreamPlayback::mix_audio, DEFVAL(PackedFloat32Array()), DEFVAL(0)); | 
|---|
| 40 | GDVIRTUAL_BIND(_stop); | 
|---|
| 41 | GDVIRTUAL_BIND(_play); | 
|---|
| 42 | GDVIRTUAL_BIND(_is_playing); | 
|---|
| 43 | GDVIRTUAL_BIND(_set_paused, "paused"); | 
|---|
| 44 | GDVIRTUAL_BIND(_is_paused); | 
|---|
| 45 | GDVIRTUAL_BIND(_get_length); | 
|---|
| 46 | GDVIRTUAL_BIND(_get_playback_position); | 
|---|
| 47 | GDVIRTUAL_BIND(_seek, "time"); | 
|---|
| 48 | GDVIRTUAL_BIND(_set_audio_track, "idx"); | 
|---|
| 49 | GDVIRTUAL_BIND(_get_texture); | 
|---|
| 50 | GDVIRTUAL_BIND(_update, "delta"); | 
|---|
| 51 | GDVIRTUAL_BIND(_get_channels); | 
|---|
| 52 | GDVIRTUAL_BIND(_get_mix_rate); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | VideoStreamPlayback::VideoStreamPlayback() { | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | VideoStreamPlayback::~VideoStreamPlayback() { | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | void VideoStreamPlayback::stop() { | 
|---|
| 62 | GDVIRTUAL_CALL(_stop); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void VideoStreamPlayback::play() { | 
|---|
| 66 | GDVIRTUAL_CALL(_play); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | bool VideoStreamPlayback::is_playing() const { | 
|---|
| 70 | bool ret; | 
|---|
| 71 | if (GDVIRTUAL_CALL(_is_playing, ret)) { | 
|---|
| 72 | return ret; | 
|---|
| 73 | } | 
|---|
| 74 | return false; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void VideoStreamPlayback::set_paused(bool p_paused) { | 
|---|
| 78 | GDVIRTUAL_CALL(_set_paused, p_paused); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | bool VideoStreamPlayback::is_paused() const { | 
|---|
| 82 | bool ret; | 
|---|
| 83 | if (GDVIRTUAL_CALL(_is_paused, ret)) { | 
|---|
| 84 | return ret; | 
|---|
| 85 | } | 
|---|
| 86 | return false; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | double VideoStreamPlayback::get_length() const { | 
|---|
| 90 | double ret; | 
|---|
| 91 | if (GDVIRTUAL_CALL(_get_length, ret)) { | 
|---|
| 92 | return ret; | 
|---|
| 93 | } | 
|---|
| 94 | return 0; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | double VideoStreamPlayback::get_playback_position() const { | 
|---|
| 98 | double ret; | 
|---|
| 99 | if (GDVIRTUAL_CALL(_get_playback_position, ret)) { | 
|---|
| 100 | return ret; | 
|---|
| 101 | } | 
|---|
| 102 | return 0; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void VideoStreamPlayback::seek(double p_time) { | 
|---|
| 106 | GDVIRTUAL_CALL(_seek, p_time); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | void VideoStreamPlayback::set_audio_track(int p_idx) { | 
|---|
| 110 | GDVIRTUAL_CALL(_set_audio_track, p_idx); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | Ref<Texture2D> VideoStreamPlayback::get_texture() const { | 
|---|
| 114 | Ref<Texture2D> ret; | 
|---|
| 115 | if (GDVIRTUAL_CALL(_get_texture, ret)) { | 
|---|
| 116 | return ret; | 
|---|
| 117 | } | 
|---|
| 118 | return nullptr; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | void VideoStreamPlayback::update(double p_delta) { | 
|---|
| 122 | if (!GDVIRTUAL_CALL(_update, p_delta)) { | 
|---|
| 123 | ERR_FAIL_MSG( "VideoStreamPlayback::update unimplemented"); | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void VideoStreamPlayback::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) { | 
|---|
| 128 | mix_callback = p_callback; | 
|---|
| 129 | mix_udata = p_userdata; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | int VideoStreamPlayback::get_channels() const { | 
|---|
| 133 | int ret; | 
|---|
| 134 | if (GDVIRTUAL_CALL(_get_channels, ret)) { | 
|---|
| 135 | _channel_count = ret; | 
|---|
| 136 | return ret; | 
|---|
| 137 | } | 
|---|
| 138 | return 0; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | int VideoStreamPlayback::get_mix_rate() const { | 
|---|
| 142 | int ret; | 
|---|
| 143 | if (GDVIRTUAL_CALL(_get_mix_rate, ret)) { | 
|---|
| 144 | return ret; | 
|---|
| 145 | } | 
|---|
| 146 | return 0; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | int VideoStreamPlayback::mix_audio(int num_frames, PackedFloat32Array buffer, int offset) { | 
|---|
| 150 | if (num_frames <= 0) { | 
|---|
| 151 | return 0; | 
|---|
| 152 | } | 
|---|
| 153 | if (!mix_callback) { | 
|---|
| 154 | return -1; | 
|---|
| 155 | } | 
|---|
| 156 | ERR_FAIL_INDEX_V(offset, buffer.size(), -1); | 
|---|
| 157 | ERR_FAIL_INDEX_V((_channel_count < 1 ? 1 : _channel_count) * num_frames - 1, buffer.size() - offset, -1); | 
|---|
| 158 | return mix_callback(mix_udata, buffer.ptr() + offset, num_frames); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | /* --- NOTE VideoStream starts here. ----- */ | 
|---|
| 162 |  | 
|---|
| 163 | Ref<VideoStreamPlayback> VideoStream::instantiate_playback() { | 
|---|
| 164 | Ref<VideoStreamPlayback> ret; | 
|---|
| 165 | if (GDVIRTUAL_CALL(_instantiate_playback, ret)) { | 
|---|
| 166 | ERR_FAIL_COND_V_MSG(ret.is_null(), nullptr, "Plugin returned null playback"); | 
|---|
| 167 | ret->set_audio_track(audio_track); | 
|---|
| 168 | return ret; | 
|---|
| 169 | } | 
|---|
| 170 | return nullptr; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | void VideoStream::set_file(const String &p_file) { | 
|---|
| 174 | file = p_file; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | String VideoStream::get_file() { | 
|---|
| 178 | return file; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | void VideoStream::_bind_methods() { | 
|---|
| 182 | ClassDB::bind_method(D_METHOD( "set_file", "file"), &VideoStream::set_file); | 
|---|
| 183 | ClassDB::bind_method(D_METHOD( "get_file"), &VideoStream::get_file); | 
|---|
| 184 |  | 
|---|
| 185 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "file"), "set_file", "get_file"); | 
|---|
| 186 |  | 
|---|
| 187 | GDVIRTUAL_BIND(_instantiate_playback); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | VideoStream::VideoStream() { | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | VideoStream::~VideoStream() { | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | void VideoStream::set_audio_track(int p_track) { | 
|---|
| 197 | audio_track = p_track; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|