Skip to content

Commit

Permalink
Bring back video player process_while_paused
Browse files Browse the repository at this point in the history
  • Loading branch information
EIREXE committed Oct 31, 2024
1 parent 4aef046 commit a294584
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
40 changes: 31 additions & 9 deletions scene/gui/video_stream_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void VideoStreamPlayer::_notification(int p_notification) {
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 && !process_while_paused) || playback.is_null() || !playback->is_playing()) {
return;
}

Expand All @@ -151,11 +151,10 @@ void VideoStreamPlayer::_notification(int p_notification) {
double delta = last_audio_time == 0 ? 0 : audio_time - last_audio_time;
last_audio_time = audio_time;

if (delta == 0) {
return;
if (paused) {
delta = 0.0f;
}

playback->update(delta); // playback->is_playing() returns false in the last video frame
playback->update(delta * playback_speed); // playback->is_playing() returns false in the last video frame

if (!playback->is_playing()) {
if (loop) {
Expand All @@ -181,7 +180,7 @@ void VideoStreamPlayer::_notification(int p_notification) {
case NOTIFICATION_PAUSED: {
if (is_playing() && !is_paused()) {
paused_from_tree = true;
if (playback.is_valid()) {
if (playback.is_valid() && !process_while_paused) {
playback->set_paused(true);
set_process_internal(false);
}
Expand Down Expand Up @@ -257,7 +256,7 @@ void VideoStreamPlayer::set_stream(const Ref<VideoStream> &p_stream) {
}

if (!playback.is_null()) {
playback->set_paused(paused);
playback->set_paused(paused && !process_while_paused);
texture = playback->get_texture();

const int channels = playback->get_channels();
Expand All @@ -280,7 +279,6 @@ void VideoStreamPlayer::set_stream(const Ref<VideoStream> &p_stream) {
resampler.clear();
AudioServer::get_singleton()->unlock();
}

queue_redraw();

if (!expand) {
Expand Down Expand Up @@ -346,7 +344,7 @@ void VideoStreamPlayer::set_paused(bool p_paused) {
return;
}

if (playback.is_valid()) {
if (playback.is_valid() && !process_while_paused) {
playback->set_paused(p_paused);
set_process_internal(!p_paused);
}
Expand Down Expand Up @@ -462,6 +460,22 @@ StringName VideoStreamPlayer::get_bus() const {
return SceneStringName(Master);
}

float VideoStreamPlayer::get_playback_speed() const {
return playback_speed;
}

void VideoStreamPlayer::set_playback_speed(float p_playback_speed) {
playback_speed = p_playback_speed;
}

void VideoStreamPlayer::set_process_while_paused(bool p_process_while_paused) {
process_while_paused = p_process_while_paused;
}

bool VideoStreamPlayer::get_process_while_paused() const {
return process_while_paused;
}

void VideoStreamPlayer::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "bus") {
String options;
Expand Down Expand Up @@ -519,6 +533,12 @@ 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_playback_speed", "playback_speed"), &VideoStreamPlayer::set_playback_speed);
ClassDB::bind_method(D_METHOD("get_playback_speed"), &VideoStreamPlayer::get_playback_speed);

ClassDB::bind_method(D_METHOD("set_process_while_paused", "process_while_paused"), &VideoStreamPlayer::set_process_while_paused);
ClassDB::bind_method(D_METHOD("get_process_while_paused"), &VideoStreamPlayer::get_process_while_paused);

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

ADD_SIGNAL(MethodInfo("finished"));
Expand All @@ -533,6 +553,8 @@ void VideoStreamPlayer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
ADD_PROPERTY(PropertyInfo(Variant::INT, "buffering_msec", PROPERTY_HINT_RANGE, "10,1000,suffix:ms"), "set_buffering_msec", "get_buffering_msec");
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::FLOAT, "playback_speed", PROPERTY_HINT_RANGE, "0,2,0.1"), "set_playback_speed", "get_playback_speed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "process_while_paused"), "set_process_while_paused", "get_process_while_paused");

ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
}
Expand Down
8 changes: 8 additions & 0 deletions scene/gui/video_stream_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class VideoStreamPlayer : public Control {
int buffering_ms = 500;
int audio_track = 0;
int bus_index = 0;
float playback_speed = 1.0f;
bool process_while_paused = false;

StringName bus;

Expand Down Expand Up @@ -124,6 +126,12 @@ class VideoStreamPlayer : public Control {
void set_bus(const StringName &p_bus);
StringName get_bus() const;

float get_playback_speed() const;
void set_playback_speed(float p_playback_speed);

void set_process_while_paused(bool p_process_while_paused);
bool get_process_while_paused() const;

VideoStreamPlayer();
~VideoStreamPlayer();
};
Expand Down

0 comments on commit a294584

Please sign in to comment.