diff options
Diffstat (limited to 'scene/gui/video_player.cpp')
-rw-r--r-- | scene/gui/video_player.cpp | 136 |
1 files changed, 47 insertions, 89 deletions
diff --git a/scene/gui/video_player.cpp b/scene/gui/video_player.cpp index ac1e4a5629..0590ae2415 100644 --- a/scene/gui/video_player.cpp +++ b/scene/gui/video_player.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -35,7 +35,6 @@ #include "servers/audio_server.h" int VideoPlayer::sp_get_channel_count() const { - if (playback.is_null()) { return 0; } @@ -44,7 +43,6 @@ int VideoPlayer::sp_get_channel_count() const { } bool VideoPlayer::mix(AudioFrame *p_buffer, int p_frames) { - // Check the amount resampler can really handle. // If it cannot, wait "wait_resampler_phase_limit" times. // This mechanism contributes to smoother pause/unpause operation. @@ -59,7 +57,6 @@ bool VideoPlayer::mix(AudioFrame *p_buffer, int p_frames) { // Called from main thread (eg VideoStreamPlaybackWebm::update) int VideoPlayer::_audio_mix_callback(void *p_udata, const float *p_data, int p_frames) { - ERR_FAIL_NULL_V(p_udata, 0); ERR_FAIL_NULL_V(p_data, 0); @@ -79,14 +76,12 @@ int VideoPlayer::_audio_mix_callback(void *p_udata, const float *p_data, int p_f } void VideoPlayer::_mix_audios(void *p_self) { - ERR_FAIL_NULL(p_self); reinterpret_cast<VideoPlayer *>(p_self)->_mix_audio(); } // Called from audio thread void VideoPlayer::_mix_audio() { - if (!stream.is_valid()) { return; } @@ -98,8 +93,9 @@ void VideoPlayer::_mix_audio() { int buffer_size = mix_buffer.size(); // Resample - if (!mix(buffer, buffer_size)) + if (!mix(buffer, buffer_size)) { return; + } AudioFrame vol = AudioFrame(volume, volume); @@ -110,7 +106,6 @@ void VideoPlayer::_mix_audio() { ERR_FAIL_COND(!target); for (int j = 0; j < buffer_size; j++) { - target[j] += buffer[j] * vol; } @@ -123,7 +118,6 @@ void VideoPlayer::_mix_audio() { } for (int j = 0; j < buffer_size; j++) { - AudioFrame frame = buffer[j] * vol; for (int k = 0; k < cc; k++) { targets[k][j] += frame; @@ -133,11 +127,8 @@ void VideoPlayer::_mix_audio() { } void VideoPlayer::_notification(int p_notification) { - switch (p_notification) { - case NOTIFICATION_ENTER_TREE: { - AudioServer::get_singleton()->add_callback(_mix_audios, this); if (stream.is_valid() && autoplay && !Engine::get_singleton()->is_editor_hint()) { @@ -147,25 +138,25 @@ void VideoPlayer::_notification(int p_notification) { } break; case NOTIFICATION_EXIT_TREE: { - AudioServer::get_singleton()->remove_callback(_mix_audios, this); } break; case NOTIFICATION_INTERNAL_PROCESS: { - bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus); - if (stream.is_null() || paused || playback.is_null() || !playback->is_playing()) + if (stream.is_null() || paused || playback.is_null() || !playback->is_playing()) { return; + } double audio_time = USEC_TO_SEC(OS::get_singleton()->get_ticks_usec()); double delta = last_audio_time == 0 ? 0 : audio_time - last_audio_time; last_audio_time = audio_time; - if (delta == 0) + if (delta == 0) { return; + } playback->update(delta); // playback->is_playing() returns false in the last video frame @@ -176,11 +167,12 @@ void VideoPlayer::_notification(int p_notification) { } break; case NOTIFICATION_DRAW: { - - if (texture.is_null()) + if (texture.is_null()) { return; - if (texture->get_width() == 0) + } + if (texture->get_width() == 0) { return; + } Size2 s = expand ? get_size() : texture->get_size(); draw_texture_rect(texture, Rect2(Point2(), s), false); @@ -190,32 +182,28 @@ void VideoPlayer::_notification(int p_notification) { }; Size2 VideoPlayer::get_minimum_size() const { - - if (!expand && !texture.is_null()) + if (!expand && !texture.is_null()) { return texture->get_size(); - else + } else { return Size2(); + } } void VideoPlayer::set_expand(bool p_expand) { - expand = p_expand; update(); minimum_size_changed(); } bool VideoPlayer::has_expand() const { - return expand; } void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) { - stop(); + AudioServer::get_singleton()->lock(); mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size()); - AudioServer::get_singleton()->unlock(); - stream = p_stream; if (stream.is_valid()) { stream->set_audio_track(audio_track); @@ -223,6 +211,7 @@ void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) { } else { playback = Ref<VideoStreamPlayback>(); } + AudioServer::get_singleton()->unlock(); if (!playback.is_null()) { playback->set_loop(loops); @@ -232,14 +221,16 @@ void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) { const int channels = playback->get_channels(); AudioServer::get_singleton()->lock(); - if (channels > 0) + if (channels > 0) { resampler.setup(channels, playback->get_mix_rate(), AudioServer::get_singleton()->get_mix_rate(), buffering_ms, 0); - else + } else { resampler.clear(); + } AudioServer::get_singleton()->unlock(); - if (channels > 0) + if (channels > 0) { playback->set_mix_callback(_audio_mix_callback, this); + } } else { texture.unref(); @@ -256,15 +247,14 @@ void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) { }; Ref<VideoStream> VideoPlayer::get_stream() const { - return stream; }; void VideoPlayer::play() { - ERR_FAIL_COND(!is_inside_tree()); - if (playback.is_null()) + if (playback.is_null()) { return; + } playback->stop(); playback->play(); set_process_internal(true); @@ -274,11 +264,12 @@ void VideoPlayer::play() { }; void VideoPlayer::stop() { - - if (!is_inside_tree()) + if (!is_inside_tree()) { return; - if (playback.is_null()) + } + if (playback.is_null()) { return; + } playback->stop(); // AudioServer::get_singleton()->stream_set_active(stream_rid,false); @@ -288,15 +279,14 @@ void VideoPlayer::stop() { }; bool VideoPlayer::is_playing() const { - - if (playback.is_null()) + if (playback.is_null()) { return false; + } return playback->is_playing(); }; void VideoPlayer::set_paused(bool p_paused) { - paused = p_paused; if (playback.is_valid()) { playback->set_paused(p_paused); @@ -306,17 +296,14 @@ void VideoPlayer::set_paused(bool p_paused) { }; bool VideoPlayer::is_paused() const { - return paused; } void VideoPlayer::set_buffering_msec(int p_msec) { - buffering_ms = p_msec; } int VideoPlayer::get_buffering_msec() const { - return buffering_ms; } @@ -325,76 +312,70 @@ void VideoPlayer::set_audio_track(int p_track) { } int VideoPlayer::get_audio_track() const { - return audio_track; } void VideoPlayer::set_volume(float p_vol) { - volume = p_vol; }; float VideoPlayer::get_volume() const { - return volume; }; void VideoPlayer::set_volume_db(float p_db) { - - if (p_db < -79) + if (p_db < -79) { set_volume(0); - else + } else { set_volume(Math::db2linear(p_db)); + } }; float VideoPlayer::get_volume_db() const { - - if (volume == 0) + if (volume == 0) { return -80; - else + } else { return Math::linear2db(volume); + } }; String VideoPlayer::get_stream_name() const { - - if (stream.is_null()) + if (stream.is_null()) { return "<No Stream>"; + } return stream->get_name(); }; float VideoPlayer::get_stream_position() const { - - if (playback.is_null()) + if (playback.is_null()) { return 0; + } return playback->get_playback_position(); }; void VideoPlayer::set_stream_position(float p_position) { - - if (playback.is_valid()) + if (playback.is_valid()) { playback->seek(p_position); + } } Ref<Texture2D> VideoPlayer::get_video_texture() const { - - if (playback.is_valid()) + if (playback.is_valid()) { return playback->get_texture(); + } return Ref<Texture2D>(); } void VideoPlayer::set_autoplay(bool p_enable) { - autoplay = p_enable; }; bool VideoPlayer::has_autoplay() const { - return autoplay; }; void VideoPlayer::set_bus(const StringName &p_bus) { - //if audio is active, must lock this AudioServer::get_singleton()->lock(); bus = p_bus; @@ -402,7 +383,6 @@ void VideoPlayer::set_bus(const StringName &p_bus) { } StringName VideoPlayer::get_bus() const { - for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { if (AudioServer::get_singleton()->get_bus_name(i) == bus) { return bus; @@ -412,13 +392,12 @@ StringName VideoPlayer::get_bus() const { } void VideoPlayer::_validate_property(PropertyInfo &p_property) const { - if (p_property.name == "bus") { - String options; for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { - if (i > 0) + if (i > 0) { options += ","; + } String name = AudioServer::get_singleton()->get_bus_name(i); options += name; } @@ -428,7 +407,6 @@ void VideoPlayer::_validate_property(PropertyInfo &p_property) const { } void VideoPlayer::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_stream", "stream"), &VideoPlayer::set_stream); ClassDB::bind_method(D_METHOD("get_stream"), &VideoPlayer::get_stream); @@ -484,29 +462,9 @@ void VideoPlayer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus"); } -VideoPlayer::VideoPlayer() { - - volume = 1; - loops = false; - paused = false; - autoplay = false; - expand = true; - - audio_track = 0; - bus_index = 0; - - buffering_ms = 500; - - // internal_stream.player=this; - // stream_rid=AudioServer::get_singleton()->audio_stream_create(&internal_stream); - last_audio_time = 0; - - wait_resampler = 0; - wait_resampler_limit = 2; -}; +VideoPlayer::VideoPlayer() {} VideoPlayer::~VideoPlayer() { - // if (stream_rid.is_valid()) // AudioServer::get_singleton()->free(stream_rid); resampler.clear(); //Not necessary here, but make in consistent with other "stream_player" classes |