VideoPlayer/LibVideo: Seek accurately when scrolling in the seek bar

Fast seeking does not work correctly when seeking in small increments,
so it is necessary to use accurate seeking when using certain actions.

The PlaybackManager has been changed to accept the seek mode as a
parameter to `seek_to_timestamp` to facilitate this. This now means
that it no longer has to track a seek mode preference.
This commit is contained in:
Zaggy1024 2023-02-06 04:11:32 -06:00 committed by Andreas Kling
parent 1f650088a0
commit b0db56cd39
Notes: sideshowbarker 2024-07-17 06:45:52 +09:00
4 changed files with 6 additions and 21 deletions

View file

@ -56,7 +56,8 @@ ErrorOr<void> VideoPlayerWidget::setup_interface()
auto progress = value / static_cast<double>(m_seek_slider->max());
auto duration = m_playback_manager->duration().to_milliseconds();
Time timestamp = Time::from_milliseconds(static_cast<i64>(round(progress * static_cast<double>(duration))));
m_playback_manager->seek_to_timestamp(timestamp);
auto seek_mode_to_use = m_seek_slider->knob_dragging() ? seek_mode() : Video::PlaybackManager::SeekMode::Accurate;
m_playback_manager->seek_to_timestamp(timestamp, seek_mode_to_use);
set_current_timestamp(m_playback_manager->current_playback_time());
};
@ -100,7 +101,6 @@ void VideoPlayerWidget::open_file(StringView filename)
close_file();
m_playback_manager = load_file_result.release_value();
update_seek_slider_max();
update_seek_mode();
resume_playback();
}
@ -277,13 +277,6 @@ void VideoPlayerWidget::set_seek_mode(Video::PlaybackManager::SeekMode seek_mode
m_use_fast_seeking->set_checked(seek_mode == Video::PlaybackManager::SeekMode::Fast);
}
void VideoPlayerWidget::update_seek_mode()
{
if (!m_playback_manager)
return;
m_playback_manager->set_seek_mode(seek_mode());
}
ErrorOr<void> VideoPlayerWidget::initialize_menubar(GUI::Window& window)
{
// File menu
@ -303,9 +296,7 @@ ErrorOr<void> VideoPlayerWidget::initialize_menubar(GUI::Window& window)
// FIXME: Maybe seek mode should be in an options dialog instead. The playback menu may get crowded.
// For now, leave it here for convenience.
m_use_fast_seeking = GUI::Action::create_checkable("&Fast Seeking", [&](auto&) {
update_seek_mode();
});
m_use_fast_seeking = GUI::Action::create_checkable("&Fast Seeking", [&](auto&) {});
TRY(playback_menu->try_add_action(*m_use_fast_seeking));
set_seek_mode(Video::PlaybackManager::DEFAULT_SEEK_MODE);

View file

@ -46,7 +46,6 @@ private:
void set_current_timestamp(Time);
void set_time_label(Time);
void on_decoding_error(Video::DecoderError const&);
void update_seek_mode();
void cycle_sizing_modes();

View file

@ -109,9 +109,9 @@ void PlaybackManager::timer_callback()
TRY_OR_FATAL_ERROR(m_playback_handler->on_timer_callback());
}
void PlaybackManager::seek_to_timestamp(Time target_timestamp)
void PlaybackManager::seek_to_timestamp(Time target_timestamp, SeekMode seek_mode)
{
TRY_OR_FATAL_ERROR(m_playback_handler->seek(target_timestamp, m_seek_mode));
TRY_OR_FATAL_ERROR(m_playback_handler->seek(target_timestamp, seek_mode));
}
Optional<Time> PlaybackManager::seek_demuxer_to_most_recent_keyframe(Time timestamp, Optional<Time> earliest_available_sample)

View file

@ -98,15 +98,12 @@ public:
void resume_playback();
void pause_playback();
void restart_playback();
void seek_to_timestamp(Time);
void seek_to_timestamp(Time, SeekMode = DEFAULT_SEEK_MODE);
bool is_playing() const
{
return m_playback_handler->is_playing();
}
SeekMode seek_mode() { return m_seek_mode; }
void set_seek_mode(SeekMode mode) { m_seek_mode = mode; }
u64 number_of_skipped_frames() const { return m_skipped_frames; }
void on_decoder_error(DecoderError error);
@ -140,8 +137,6 @@ private:
Time m_last_present_in_media_time = Time::zero();
SeekMode m_seek_mode = DEFAULT_SEEK_MODE;
NonnullOwnPtr<Demuxer> m_demuxer;
Track m_selected_video_track;
NonnullOwnPtr<VideoDecoder> m_decoder;