Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/classes/VideoStreamPlayer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<member name="paused" type="bool" setter="set_paused" getter="is_paused" default="false">
If [code]true[/code], the video is paused.
</member>
<member name="playback_update_mode" type="int" setter="set_update_mode" getter="get_update_mode" enum="VideoStreamPlayer.UpdateMode" default="1">
The update mode for advancing the video playback.
</member>
<member name="speed_scale" type="float" setter="set_speed_scale" getter="get_speed_scale" default="1.0">
The stream's current speed scale. [code]1.0[/code] is the normal speed, while [code]2.0[/code] is double speed and [code]0.5[/code] is half speed. A speed scale of [code]0.0[/code] pauses the video, similar to setting [member paused] to [code]true[/code].
</member>
Expand All @@ -96,4 +99,15 @@
</description>
</signal>
</signals>
<constants>
<constant name="UPDATE_DISABLED" value="0" enum="UpdateMode">
Do not update the video stream player. Acts similar to [member paused] but keeps the current playback state without changing any playback properties.
</constant>
<constant name="UPDATE_WHEN_VISIBLE" value="1" enum="UpdateMode">
Only update and advance the video playback state while the video stream player is visible. Visibility is determined by the [CanvasItem] visibility notifier and [SceneTree] node visibility. Effectively the video will automatically act as if paused while off-screen or hidden saving performance and resuming when it becomes visible again.
</constant>
<constant name="UPDATE_ALWAYS" value="2" enum="UpdateMode">
Always update the video stream player even while hidden and off-screen.
</constant>
</constants>
</class>
46 changes: 46 additions & 0 deletions scene/gui/video_stream_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,14 @@ void VideoStreamPlayer::_notification(int p_notification) {
if (stream.is_valid() && autoplay && !Engine::get_singleton()->is_editor_hint()) {
play();
}
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, get_rect(), callable_mp(this, &VideoStreamPlayer::_visibility_enter), callable_mp(this, &VideoStreamPlayer::_visibility_exit));

} break;

case NOTIFICATION_EXIT_TREE: {
stop();
AudioServer::get_singleton()->remove_mix_callback(_mix_audios, this);
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), false, Rect2(), Callable(), Callable());
} break;

case NOTIFICATION_INTERNAL_PROCESS: {
Expand All @@ -154,6 +157,10 @@ void VideoStreamPlayer::_notification(int p_notification) {
return;
}

if (update_mode == VideoStreamPlayer::UPDATE_DISABLED || (update_mode == VideoStreamPlayer::UPDATE_WHEN_VISIBLE && (!on_screen || !is_visible_in_tree()))) {
return;
}

double delta = first_frame ? 0 : get_process_delta_time();
first_frame = false;

Expand Down Expand Up @@ -181,6 +188,7 @@ void VideoStreamPlayer::_notification(int p_notification) {

Size2 s = expand ? get_size() : texture_size;
draw_texture_rect(texture, Rect2(Point2(), s), false);

} break;

case NOTIFICATION_SUSPENDED:
Expand Down Expand Up @@ -227,6 +235,10 @@ void VideoStreamPlayer::texture_changed(const Ref<Texture2D> &p_texture) {
if (!expand) {
update_minimum_size();
}

if (is_inside_tree()) {
RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, get_rect(), callable_mp(this, &VideoStreamPlayer::_visibility_enter), callable_mp(this, &VideoStreamPlayer::_visibility_exit));
}
}

Size2 VideoStreamPlayer::get_minimum_size() const {
Expand Down Expand Up @@ -503,6 +515,32 @@ StringName VideoStreamPlayer::get_bus() const {
return SceneStringName(Master);
}

void VideoStreamPlayer::set_update_mode(UpdateMode p_mode) {
ERR_MAIN_THREAD_GUARD;
update_mode = p_mode;
}

VideoStreamPlayer::UpdateMode VideoStreamPlayer::get_update_mode() const {
ERR_READ_THREAD_GUARD_V(UPDATE_DISABLED);
return update_mode;
}

void VideoStreamPlayer::_visibility_enter() {
if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
return;
}

on_screen = true;
}

void VideoStreamPlayer::_visibility_exit() {
if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
return;
}

on_screen = false;
}

void VideoStreamPlayer::_validate_property(PropertyInfo &p_property) const {
if (!Engine::get_singleton()->is_editor_hint()) {
return;
Expand Down Expand Up @@ -566,6 +604,9 @@ void VideoStreamPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_bus", "bus"), &VideoStreamPlayer::set_bus);
ClassDB::bind_method(D_METHOD("get_bus"), &VideoStreamPlayer::get_bus);

ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &VideoStreamPlayer::set_update_mode);
ClassDB::bind_method(D_METHOD("get_update_mode"), &VideoStreamPlayer::get_update_mode);

ClassDB::bind_method(D_METHOD("get_video_texture"), &VideoStreamPlayer::get_video_texture);

ADD_SIGNAL(MethodInfo("finished"));
Expand All @@ -583,6 +624,11 @@ void VideoStreamPlayer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stream_position", PROPERTY_HINT_RANGE, "0,1280000,0.1", PROPERTY_USAGE_NONE), "set_stream_position", "get_stream_position");

ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_update_mode", PROPERTY_HINT_ENUM, "Disabled,When Visible,Always"), "set_update_mode", "get_update_mode");

BIND_ENUM_CONSTANT(UPDATE_DISABLED);
BIND_ENUM_CONSTANT(UPDATE_WHEN_VISIBLE);
BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
}

VideoStreamPlayer::~VideoStreamPlayer() {
Expand Down
19 changes: 19 additions & 0 deletions scene/gui/video_stream_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ class VideoStreamPlayer : public Control {
static int _audio_mix_callback(void *p_udata, const float *p_data, int p_frames);
static void _mix_audios(void *p_self);

public:
enum UpdateMode {
UPDATE_DISABLED,
UPDATE_WHEN_VISIBLE,
UPDATE_ALWAYS
};

private:
UpdateMode update_mode = UPDATE_WHEN_VISIBLE;

bool on_screen = false;
void _visibility_enter();
void _visibility_exit();

protected:
static void _bind_methods();
void _notification(int p_notification);
Expand Down Expand Up @@ -121,5 +135,10 @@ class VideoStreamPlayer : public Control {
void set_bus(const StringName &p_bus);
StringName get_bus() const;

void set_update_mode(UpdateMode p_mode);
UpdateMode get_update_mode() const;

~VideoStreamPlayer();
};

VARIANT_ENUM_CAST(VideoStreamPlayer::UpdateMode);