Debian Codegraph @

scene/gui/video_stream_player.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* video_stream_player.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_player.h"
32
33 #include "core/os/os.h"
34 #include "servers/audio/audio_server.h"
35
36 int VideoStreamPlayer::sp_get_channel_count() const {
37 if (playback.is_null()) {
38 return 0;
39 }
40
41 return playback->get_channels();
42 }
43
44 bool VideoStreamPlayer::mix(AudioFrame *p_buffer, int p_frames) {
45 // Check the amount resampler can really handle.
46 // If it cannot, wait "wait_resampler_phase_limit" times.
47 // This mechanism contributes to smoother pause/unpause operation.
48 if (p_frames <= resampler.get_num_of_ready_frames() ||
49 wait_resampler_limit <= wait_resampler) {
50 wait_resampler = 0;
51 return resampler.mix(p_buffer, p_frames);
52 }
53 wait_resampler++;
54 return false;
55 }
56
57 // Called from main thread (e.g. VideoStreamPlaybackTheora::update).
58 int VideoStreamPlayer::_audio_mix_callback(void *p_udata, const float *p_data, int p_frames) {
59 ERR_FAIL_NULL_V(p_udata, 0);
60 ERR_FAIL_NULL_V(p_data, 0);
61
62 VideoStreamPlayer *vp = static_cast<VideoStreamPlayer *>(p_udata);
63
64 int todo = MIN(vp->resampler.get_writer_space(), p_frames);
65
66 float *wb = vp->resampler.get_write_buffer();
67 int c = vp->resampler.get_channel_count();
68
69 for (int i = 0; i < todo * c; i++) {
70 wb[i] = p_data[i];
71 }
72 vp->resampler.write(todo);
73
74 return todo;
75 }
76
77 void VideoStreamPlayer::_mix_audios(void *p_self) {
78 ERR_FAIL_NULL(p_self);
79 static_cast<VideoStreamPlayer *>(p_self)->_mix_audio();
80 }
81
82 // Called from audio thread
83 void VideoStreamPlayer::_mix_audio() {
84 if (stream.is_null()) {
85 return;
86 }
87 if (playback.is_null() || !playback->is_playing() || playback->is_paused()) {
88 return;
89 }
90
91 AudioFrame *buffer = mix_buffer.ptrw();
92 int buffer_size = mix_buffer.size();
93
94 // Resample
95 if (!mix(buffer, buffer_size)) {
96 return;
97 }
98
99 AudioFrame vol = AudioFrame(volume, volume);
100
101 int cc = AudioServer::get_singleton()->get_channel_count();
102
103 if (cc == 1) {
104 AudioFrame *target = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, 0);
105 ERR_FAIL_NULL(target);
106
107 for (int j = 0; j < buffer_size; j++) {
108 target[j] += buffer[j] * vol;
109 }
110
111 } else {
112 AudioFrame *targets[4];
113
114 for (int k = 0; k < cc; k++) {
115 targets[k] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, k);
116 ERR_FAIL_NULL(targets[k]);
117 }
118
119 for (int j = 0; j < buffer_size; j++) {
120 AudioFrame frame = buffer[j] * vol;
121 for (int k = 0; k < cc; k++) {
122 targets[k][j] += frame;
123 }
124 }
125 }
126 }
127
128 void VideoStreamPlayer::_notification(int p_notification) {
129 switch (p_notification) {
130 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
131 RID ae = get_accessibility_element();
132 ERR_FAIL_COND(ae.is_null());
133
134 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_VIDEO);
135 } break;
136
137 case NOTIFICATION_ENTER_TREE: {
138 AudioServer::get_singleton()->add_mix_callback(_mix_audios, this);
139
140 if (stream.is_valid() && autoplay && !Engine::get_singleton()->is_editor_hint()) {
141 play();
142 }
143 } break;
144
145 case NOTIFICATION_EXIT_TREE: {
146 stop();
147 AudioServer::get_singleton()->remove_mix_callback(_mix_audios, this);
148 } break;
149
150 case NOTIFICATION_INTERNAL_PROCESS: {
151 bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus);
152
153 if (stream.is_null() || paused || playback.is_null() || !playback->is_playing()) {
154 return;
155 }
156
157 double delta = first_frame ? 0 : get_process_delta_time();
158 first_frame = false;
159
160 resampler.set_playback_speed(Engine::get_singleton()->get_effective_time_scale() * speed_scale);
161
162 playback->update(delta * speed_scale); // playback->is_playing() returns false in the last video frame
163
164 if (!playback->is_playing()) {
165 resampler.flush();
166 if (loop) {
167 play();
168 return;
169 }
170 emit_signal(SceneStringName(finished));
171 }
172 } break;
173
174 case NOTIFICATION_DRAW: {
175 if (texture.is_null()) {
176 return;
177 }
178 if (texture->get_width() == 0) {
179 return;
180 }
181
182 Size2 s = expand ? get_size() : texture_size;
183 draw_texture_rect(texture, Rect2(Point2(), s), false);
184 } break;
185
186 case NOTIFICATION_SUSPENDED:
187 case NOTIFICATION_PAUSED: {
188 if (is_playing() && !is_paused()) {
189 paused_from_tree = true;
190 if (playback.is_valid()) {
191 playback->set_paused(true);
192 set_process_internal(false);
193 }
194 }
195 } break;
196
197 case NOTIFICATION_UNSUSPENDED: {
198 if (get_tree()->is_paused()) {
199 break;
200 }
201 [[fallthrough]];
202 }
203
204 case NOTIFICATION_UNPAUSED: {
205 if (paused_from_tree) {
206 paused_from_tree = false;
207 if (playback.is_valid()) {
208 playback->set_paused(false);
209 set_process_internal(true);
210 }
211 }
212 } break;
213 }
214 }
215
216 void VideoStreamPlayer::texture_changed(const Ref<Texture2D> &p_texture) {
217 const Size2 new_texture_size = p_texture.is_valid() ? p_texture->get_size() : Size2();
218
219 if (new_texture_size == texture_size) {
220 return;
221 }
222
223 texture_size = new_texture_size;
224
225 queue_redraw();
226
227 if (!expand) {
228 update_minimum_size();
229 }
230 }
231
232 Size2 VideoStreamPlayer::get_minimum_size() const {
233 if (!expand && texture.is_valid()) {
234 return texture_size;
235 } else {
236 return Size2();
237 }
238 }
239
240 void VideoStreamPlayer::set_expand(bool p_expand) {
241 if (expand == p_expand) {
242 return;
243 }
244
245 expand = p_expand;
246 queue_redraw();
247 update_minimum_size();
248 }
249
250 bool VideoStreamPlayer::has_expand() const {
251 return expand;
252 }
253
254 void VideoStreamPlayer::set_loop(bool p_loop) {
255 loop = p_loop;
256 }
257
258 bool VideoStreamPlayer::has_loop() const {
259 return loop;
260 }
261
262 void VideoStreamPlayer::set_stream(const Ref<VideoStream> &p_stream) {
263 stop();
264
265 // Make sure to handle stream changes seamlessly, e.g. when done via
266 // translation remapping.
267 if (stream.is_valid()) {
268 stream->disconnect_changed(callable_mp(this, &VideoStreamPlayer::set_stream));
269 }
270
271 AudioServer::get_singleton()->lock();
272 mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
273 stream = p_stream;
274 if (stream.is_valid()) {
275 stream->set_audio_track(audio_track);
276 playback = stream->instantiate_playback();
277 } else {
278 playback = Ref<VideoStreamPlayback>();
279 }
280 AudioServer::get_singleton()->unlock();
281
282 if (stream.is_valid()) {
283 stream->connect_changed(callable_mp(this, &VideoStreamPlayer::set_stream).bind(stream));
284 }
285
286 if (texture.is_valid()) {
287 texture->disconnect_changed(callable_mp(this, &VideoStreamPlayer::texture_changed));
288 }
289
290 if (playback.is_valid()) {
291 playback->set_paused(paused);
292 texture = playback->get_texture();
293
294 if (texture.is_valid()) {
295 texture_size = texture->get_size();
296 texture->connect_changed(callable_mp(this, &VideoStreamPlayer::texture_changed).bind(texture));
297 }
298
299 const int channels = playback->get_channels();
300
301 AudioServer::get_singleton()->lock();
302 if (channels > 0) {
303 resampler.setup(channels, playback->get_mix_rate(), AudioServer::get_singleton()->get_mix_rate(), buffering_ms, 0);
304 } else {
305 resampler.clear();
306 }
307 AudioServer::get_singleton()->unlock();
308
309 if (channels > 0) {
310 playback->set_mix_callback(_audio_mix_callback, this);
311 }
312
313 } else {
314 texture.unref();
315 AudioServer::get_singleton()->lock();
316 resampler.clear();
317 AudioServer::get_singleton()->unlock();
318 }
319
320 queue_redraw();
321
322 if (!expand) {
323 update_minimum_size();
324 }
325 }
326
327 Ref<VideoStream> VideoStreamPlayer::get_stream() const {
328 return stream;
329 }
330
331 void VideoStreamPlayer::play() {
332 ERR_FAIL_COND(!is_inside_tree());
333 if (playback.is_null()) {
334 return;
335 }
336 playback->play();
337 set_process_internal(true);
338 first_frame = true;
339
340 if (!can_process()) {
341 _notification(NOTIFICATION_PAUSED);
342 }
343 }
344
345 void VideoStreamPlayer::stop() {
346 if (!is_inside_tree()) {
347 return;
348 }
349 if (playback.is_null()) {
350 return;
351 }
352
353 playback->stop();
354 resampler.flush();
355 set_process_internal(false);
356 }
357
358 bool VideoStreamPlayer::is_playing() const {
359 if (playback.is_null()) {
360 return false;
361 }
362
363 return playback->is_playing();
364 }
365
366 void VideoStreamPlayer::set_paused(bool p_paused) {
367 if (paused == p_paused) {
368 return;
369 }
370
371 paused = p_paused;
372 if (!p_paused && !can_process()) {
373 paused_from_tree = true;
374 return;
375 } else if (p_paused && paused_from_tree) {
376 paused_from_tree = false;
377 return;
378 }
379
380 if (playback.is_valid()) {
381 playback->set_paused(p_paused);
382 set_process_internal(!p_paused);
383 }
384 }
385
386 bool VideoStreamPlayer::is_paused() const {
387 return paused;
388 }
389
390 void VideoStreamPlayer::set_buffering_msec(int p_msec) {
391 buffering_ms = p_msec;
392 }
393
394 int VideoStreamPlayer::get_buffering_msec() const {
395 return buffering_ms;
396 }
397
398 void VideoStreamPlayer::set_audio_track(int p_track) {
399 audio_track = p_track;
400 if (stream.is_valid()) {
401 stream->set_audio_track(audio_track);
402 }
403 if (playback.is_valid()) {
404 playback->set_audio_track(audio_track);
405 }
406 }
407
408 int VideoStreamPlayer::get_audio_track() const {
409 return audio_track;
410 }
411
412 void VideoStreamPlayer::set_volume(float p_vol) {
413 volume = p_vol;
414 }
415
416 float VideoStreamPlayer::get_volume() const {
417 return volume;
418 }
419
420 void VideoStreamPlayer::set_volume_db(float p_db) {
421 if (p_db < -79) {
422 set_volume(0);
423 } else {
424 set_volume(Math::db_to_linear(p_db));
425 }
426 }
427
428 float VideoStreamPlayer::get_volume_db() const {
429 if (volume == 0) {
430 return -80;
431 } else {
432 return Math::linear_to_db(volume);
433 }
434 }
435
436 void VideoStreamPlayer::set_speed_scale(float p_speed_scale) {
437 ERR_FAIL_COND(p_speed_scale < 0.0);
438 speed_scale = p_speed_scale;
439 }
440
441 float VideoStreamPlayer::get_speed_scale() const {
442 return speed_scale;
443 }
444
445 String VideoStreamPlayer::get_stream_name() const {
446 if (stream.is_null()) {
447 return "<No Stream>";
448 }
449 return stream->get_name();
450 }
451
452 double VideoStreamPlayer::get_stream_length() const {
453 if (playback.is_null()) {
454 return 0;
455 }
456 return playback->get_length();
457 }
458
459 double VideoStreamPlayer::get_stream_position() const {
460 if (playback.is_null()) {
461 return 0;
462 }
463 return playback->get_playback_position();
464 }
465
466 void VideoStreamPlayer::set_stream_position(double p_position) {
467 if (playback.is_valid()) {
468 resampler.flush();
469 playback->seek(p_position);
470 first_frame = true;
471 }
472 }
473
474 Ref<Texture2D> VideoStreamPlayer::get_video_texture() const {
475 if (playback.is_valid()) {
476 return playback->get_texture();
477 }
478
479 return Ref<Texture2D>();
480 }
481
482 void VideoStreamPlayer::set_autoplay(bool p_enable) {
483 autoplay = p_enable;
484 }
485
486 bool VideoStreamPlayer::has_autoplay() const {
487 return autoplay;
488 }
489
490 void VideoStreamPlayer::set_bus(const StringName &p_bus) {
491 // If audio is active, must lock this.
492 AudioServer::get_singleton()->lock();
493 bus = p_bus;
494 AudioServer::get_singleton()->unlock();
495 }
496
497 StringName VideoStreamPlayer::get_bus() const {
498 for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
499 if (AudioServer::get_singleton()->get_bus_name(i) == bus) {
500 return bus;
501 }
502 }
503 return SceneStringName(Master);
504 }
505
506 void VideoStreamPlayer::_validate_property(PropertyInfo &p_property) const {
507 if (!Engine::get_singleton()->is_editor_hint()) {
508 return;
509 }
510 if (p_property.name == "bus") {
511 String options;
512 for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
513 if (i > 0) {
514 options += ",";
515 }
516 String name = AudioServer::get_singleton()->get_bus_name(i);
517 options += name;
518 }
519
520 p_property.hint_string = options;
521 }
522 }
523
524 void VideoStreamPlayer::_bind_methods() {
525 ClassDB::bind_method(D_METHOD("set_stream", "stream"), &VideoStreamPlayer::set_stream);
526 ClassDB::bind_method(D_METHOD("get_stream"), &VideoStreamPlayer::get_stream);
527
528 ClassDB::bind_method(D_METHOD("play"), &VideoStreamPlayer::play);
529 ClassDB::bind_method(D_METHOD("stop"), &VideoStreamPlayer::stop);
530
531 ClassDB::bind_method(D_METHOD("is_playing"), &VideoStreamPlayer::is_playing);
532
533 ClassDB::bind_method(D_METHOD("set_paused", "paused"), &VideoStreamPlayer::set_paused);
534 ClassDB::bind_method(D_METHOD("is_paused"), &VideoStreamPlayer::is_paused);
535
536 ClassDB::bind_method(D_METHOD("set_loop", "loop"), &VideoStreamPlayer::set_loop);
537 ClassDB::bind_method(D_METHOD("has_loop"), &VideoStreamPlayer::has_loop);
538
539 ClassDB::bind_method(D_METHOD("set_volume", "volume"), &VideoStreamPlayer::set_volume);
540 ClassDB::bind_method(D_METHOD("get_volume"), &VideoStreamPlayer::get_volume);
541
542 ClassDB::bind_method(D_METHOD("set_volume_db", "db"), &VideoStreamPlayer::set_volume_db);
543 ClassDB::bind_method(D_METHOD("get_volume_db"), &VideoStreamPlayer::get_volume_db);
544
545 ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &VideoStreamPlayer::set_speed_scale);
546 ClassDB::bind_method(D_METHOD("get_speed_scale"), &VideoStreamPlayer::get_speed_scale);
547
548 ClassDB::bind_method(D_METHOD("set_audio_track", "track"), &VideoStreamPlayer::set_audio_track);
549 ClassDB::bind_method(D_METHOD("get_audio_track"), &VideoStreamPlayer::get_audio_track);
550
551 ClassDB::bind_method(D_METHOD("get_stream_name"), &VideoStreamPlayer::get_stream_name);
552 ClassDB::bind_method(D_METHOD("get_stream_length"), &VideoStreamPlayer::get_stream_length);
553
554 ClassDB::bind_method(D_METHOD("set_stream_position", "position"), &VideoStreamPlayer::set_stream_position);
555 ClassDB::bind_method(D_METHOD("get_stream_position"), &VideoStreamPlayer::get_stream_position);
556
557 ClassDB::bind_method(D_METHOD("set_autoplay", "enabled"), &VideoStreamPlayer::set_autoplay);
558 ClassDB::bind_method(D_METHOD("has_autoplay"), &VideoStreamPlayer::has_autoplay);
559
560 ClassDB::bind_method(D_METHOD("set_expand", "enable"), &VideoStreamPlayer::set_expand);
561 ClassDB::bind_method(D_METHOD("has_expand"), &VideoStreamPlayer::has_expand);
562
563 ClassDB::bind_method(D_METHOD("set_buffering_msec", "msec"), &VideoStreamPlayer::set_buffering_msec);
564 ClassDB::bind_method(D_METHOD("get_buffering_msec"), &VideoStreamPlayer::get_buffering_msec);
565
566 ClassDB::bind_method(D_METHOD("set_bus", "bus"), &VideoStreamPlayer::set_bus);
567 ClassDB::bind_method(D_METHOD("get_bus"), &VideoStreamPlayer::get_bus);
568
569 ClassDB::bind_method(D_METHOD("get_video_texture"), &VideoStreamPlayer::get_video_texture);
570
571 ADD_SIGNAL(MethodInfo("finished"));
572
573 ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_track", PROPERTY_HINT_RANGE, "0,128,1"), "set_audio_track", "get_audio_track");
574 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "VideoStream"), "set_stream", "get_stream");
575 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01,suffix:dB"), "set_volume_db", "get_volume_db");
576 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume", PROPERTY_HINT_RANGE, "0,15,0.01,exp", PROPERTY_USAGE_NONE), "set_volume", "get_volume");
577 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,4,0.001,or_greater"), "set_speed_scale", "get_speed_scale");
578 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "has_autoplay");
579 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_paused", "is_paused");
580 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
581 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
582 ADD_PROPERTY(PropertyInfo(Variant::INT, "buffering_msec", PROPERTY_HINT_RANGE, "10,1000,suffix:ms"), "set_buffering_msec", "get_buffering_msec");
583 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stream_position", PROPERTY_HINT_RANGE, "0,1280000,0.1", PROPERTY_USAGE_NONE), "set_stream_position", "get_stream_position");
584
585 ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
586 }
587
588 VideoStreamPlayer::~VideoStreamPlayer() {
589 resampler.clear(); // Not necessary here, but make in consistent with other "stream_player" classes.
590 }