Skip to content

Commit

Permalink
Add playback speed control
Browse files Browse the repository at this point in the history
For now we clamp the playback rate between 0.25x and 2.x, locked in 0.25x
offsets. Reverse playback or more fine-grained control could be added later on.

Fixes #76
  • Loading branch information
philn committed Dec 3, 2023
1 parent 9affb12 commit 5b1d968
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
26 changes: 26 additions & 0 deletions data/net.baseart.Glide.ui
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@
<attribute name="label" translatable="yes">Video track</attribute>
</submenu>
</submenu>
<submenu id="playback-menu">
<attribute name="label" translatable="yes">Playback</attribute>
<section>
<item>
<attribute name="label" translatable="yes">Increase speed</attribute>
<attribute name="action">app.speed-increase</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Decrease speed</attribute>
<attribute name="action">app.speed-decrease</attribute>
</item>
</section>
</submenu>
<submenu id="subtitles-menu">
<attribute name="label" translatable="yes">Subtitles</attribute>
<section>
Expand Down Expand Up @@ -283,6 +296,19 @@ audio-volume-medium-symbolic</property>
<property name="title" translatable="yes">Mute the audio track</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="accelerator">Page_Up</property>
<property name="title" translatable="yes">Increase playback speed</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="accelerator">Page_Down</property>
<property name="title" translatable="yes">Decrease playback speed</property>
</object>
</child>

</object>
</child>
<child>
Expand Down
20 changes: 20 additions & 0 deletions src/channel_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,24 @@ impl ChannelPlayer {
self.gtksink
.send_event(gst::event::Step::new(Buffers::ONE, 1.0, true, false));
}

pub fn playback_rate(&self) -> f64 {
self.player.rate()
}

pub fn increase_speed(&self) {
let rate = self.player.rate();
let offset = 0.25;
if rate + offset <= 2.0 {
self.player.set_rate(rate + offset);
}
}

pub fn decrease_speed(&self) {
let rate = self.player.rate();
let offset = 0.25;
if rate > offset {
self.player.set_rate(rate - offset);
}
}
}
39 changes: 34 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ struct VideoPlayer {
audio_offset_reset_action: gio::SimpleAction,
subtitle_offset_reset_action: gio::SimpleAction,
video_frame_step_action: gio::SimpleAction,
speed_increase_action: gio::SimpleAction,
speed_decrease_action: gio::SimpleAction,
player_receiver: Option<glib::Receiver<PlayerEvent>>,
}

Expand Down Expand Up @@ -177,6 +179,12 @@ impl VideoPlayer {
let video_frame_step_action = gio::SimpleAction::new("video-frame-step", None);
gtk_app.add_action(&video_frame_step_action);

let speed_increase_action = gio::SimpleAction::new("speed-increase", None);
gtk_app.add_action(&speed_increase_action);

let speed_decrease_action = gio::SimpleAction::new("speed-decrease", None);
gtk_app.add_action(&speed_decrease_action);

let about = gio::SimpleAction::new("about", None);
about.connect_activate(move |_, _| {
with_video_player!(video_player {
Expand Down Expand Up @@ -248,6 +256,8 @@ impl VideoPlayer {
audio_offset_reset_action,
subtitle_offset_reset_action,
video_frame_step_action,
speed_increase_action,
speed_decrease_action,
player_receiver: Some(player_receiver),
})
}
Expand Down Expand Up @@ -471,6 +481,18 @@ impl VideoPlayer {
})
});

self.speed_decrease_action.connect_activate(|_, _| {
with_video_player!(video_player {
video_player.player.decrease_speed();
});
});

self.speed_increase_action.connect_activate(|_, _| {
with_video_player!(video_player {
video_player.player.increase_speed();
});
});

let paintable = self.player.paintable();
paintable.connect_invalidate_contents(|p| {
with_video_player!(video_player {
Expand Down Expand Up @@ -542,11 +564,18 @@ impl VideoPlayer {
self.ui_context.set_progress_bar_format_callback(|value| {
let position = gst::ClockTime::from_seconds(value as u64);
with_optional_video_player!(video_player {
if let Some(duration) = video_player.player.duration() {
format!("{position:.0} / {duration:.0}")
} else {
format!("{position:.0}")
}
let status = if let Some(duration) = video_player.player.duration() {
format!("{position:.0} / {duration:.0}")
} else {
format!("{position:.0}")
};
// FIXME: Ideally it'd be nice to show this on an OSD overlay?
let playback_rate = video_player.player.playback_rate();
if playback_rate != 1.0 {
format!("{status} @ {playback_rate}.x")
} else {
status
}
} {
format!("{position:.0}")
})
Expand Down
2 changes: 2 additions & 0 deletions src/ui_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ impl UIContext {
("dump-pipeline", ["<Ctrl>d"]),
("show-shortcuts", ["<Primary>question"]),
("video-frame-step", ["<Primary>n"]),
("speed-increase", ["Page_Up"]),
("speed-decrease", ["Page_Down"]),
];
for (action, accels) in accels_per_action.iter() {
app.set_accels_for_action(&format!("app.{action}"), accels);
Expand Down

0 comments on commit 5b1d968

Please sign in to comment.