Skip to content

Truly independent web browser

LibMedia+LibWeb: Move seek clamping steps to PlaybackManager

Zaggy1024c7490c7c928a
diff --git a/Libraries/LibMedia/PlaybackManager.cpp b/Libraries/LibMedia/PlaybackManager.cpp
index d57894fc211..2c03914d75d 100644
--- a/Libraries/LibMedia/PlaybackManager.cpp
+++ b/Libraries/LibMedia/PlaybackManager.cpp
@@ -785,8 +785,65 @@ void PlaybackManager::pause()
m_handler->pause();
}
+Optional<PlaybackManager::SeekTarget> PlaybackManager::decide_seek_target(AK::Duration timestamp, SeekMode mode) const
+{
+ // https://html.spec.whatwg.org/multipage/media.html#dom-media-seek
+ // NB: The timestamp variable stands in for the new playback position.
+ // These are the seek steps that run in parallel.
+
+ // 6. If the new playback position is later than the end of the media resource, then let it be the end of the media
+ // resource instead.
+ timestamp = min(timestamp, duration());
+
+ // 7. If the new playback position is less than the earliest possible position, let it be that position instead.
+ timestamp = max(timestamp, earliest_possible_position());
+
+ // 8. If the (possibly now changed) new playback position is not in one of the ranges given in the seekable
+ // attribute, then let it be the position in one of the ranges given in the seekable attribute that is the
+ // nearest to the new playback position. If two positions both satisfy that constraint (i.e. the new playback
+ // position is exactly in the middle between two ranges in the seekable attribute), then use the position that
+ // is closest to the current playback position. If there are no ranges given in the seekable attribute, then set
+ // the seeking IDL attribute to false and return.
+ auto seekable = seekable_time_ranges();
+ if (!seekable.has_value())
+ return OptionalNone();
+
+ // NB: We only have one range, so no tie-breaker is needed, and we can just clamp to it.
+ timestamp = max(timestamp, seekable.value().start);
+ timestamp = min(timestamp, seekable.value().end);
+
+ // 9. If the approximate-for-speed flag is set, adjust the new playback position to a value that will allow for
+ // playback to resume promptly. If new playback position before this step is before current playback position,
+ // then the adjusted new playback position must also be before the current playback position. Similarly, if the
+ // new playback position before this step is after current playback position, then the adjusted new playback
+ // position must also be after the current playback position.
+ if (mode == SeekMode::ApproximateForSpeed) {
+ auto current_playback_position = current_time();
+ if (timestamp < current_playback_position)
+ mode = Media::SeekMode::FastBefore;
+ else if (timestamp > current_playback_position)
+ mode = Media::SeekMode::FastAfter;
+ else
+ mode = Media::SeekMode::Accurate;
+ }
+
+ return SeekTarget { timestamp, mode };
+}
+
void PlaybackManager::seek(AK::Duration timestamp, SeekMode mode)
{
+ auto target = decide_seek_target(timestamp, mode);
+ if (!target.has_value()) {
+ dbgln_if(PLAYBACK_MANAGER_DEBUG, "PlaybackManager({:p}): Seek was skipped", this);
+ if (on_seek_decided)
+ on_seek_decided(SeekDecision::Skip);
+ return;
+ }
+ if (on_seek_decided)
+ on_seek_decided(SeekDecision::Seek);
+ timestamp = target.value().timestamp;
+ mode = target.value().mode;
+
dbgln_if(PLAYBACK_MANAGER_DEBUG, "PlaybackManager({:p}): Seek to {} ({}) from {}", this, timestamp, mode, m_handler->state());
m_handler->seek(timestamp, mode);
m_is_in_error_state = false;
diff --git a/Libraries/LibMedia/PlaybackManager.h b/Libraries/LibMedia/PlaybackManager.h
index 45b09e91b5e..655a190b71f 100644
--- a/Libraries/LibMedia/PlaybackManager.h
+++ b/Libraries/LibMedia/PlaybackManager.h
@@ -120,6 +120,8 @@ public:
Function<void()> on_playback_state_settled_change;
Function<void(AK::Duration)> on_duration_change;
Function<void()> on_buffered_ranges_change;
+ // Called for each seek, before any state change the seek causes.
+ Function<void(SeekDecision)> on_seek_decided;
Function<void(DecoderError&&)> on_error;
struct AddedTracks {
@@ -258,6 +260,13 @@ private:
void replace_state_handler(Args&&... args);
inline void dispatch_state_change() const;
+ struct SeekTarget {
+ AK::Duration timestamp;
+ SeekMode mode;
+ };
+
+ Optional<SeekTarget> decide_seek_target(AK::Duration, SeekMode) const;
+
OwnPtr<PlaybackStateHandler> m_handler;
NonnullRefPtr<WeakPlaybackManagerLink> m_weak_link;
diff --git a/Libraries/LibMedia/PlaybackStates/PlaybackState.h b/Libraries/LibMedia/PlaybackStates/PlaybackState.h
index 3cf5b3f1092..65018cbc9b1 100644
--- a/Libraries/LibMedia/PlaybackStates/PlaybackState.h
+++ b/Libraries/LibMedia/PlaybackStates/PlaybackState.h
@@ -19,6 +19,8 @@ enum class PlaybackState : u8 {
Suspended,
Seeking,
Ended,
+ // Only RemotePlaybackManager enters this, until the server decides whether a seek goes ahead.
+ BeginningSeek,
};
constexpr StringView playback_state_to_string(PlaybackState state)
@@ -34,6 +36,8 @@ constexpr StringView playback_state_to_string(PlaybackState state)
return "Paused"sv;
case PlaybackState::Suspended:
return "Suspended"sv;
+ case PlaybackState::BeginningSeek:
+ return "BeginningSeek"sv;
case PlaybackState::Seeking:
return "Seeking"sv;
case PlaybackState::Ended:
diff --git a/Libraries/LibMedia/SeekMode.h b/Libraries/LibMedia/SeekMode.h
index f019bda15d7..890acdeb2b5 100644
--- a/Libraries/LibMedia/SeekMode.h
+++ b/Libraries/LibMedia/SeekMode.h
@@ -14,15 +14,24 @@ namespace Media {
enum class SeekMode : u8 {
Accurate,
+ ApproximateForSpeed,
FastBefore,
FastAfter,
};
+enum class SeekDecision : u8 {
+ Seek,
+ // Nothing is seekable, so the playback position stays where it is.
+ Skip,
+};
+
constexpr StringView seek_mode_to_string(SeekMode seek_mode)
{
switch (seek_mode) {
case SeekMode::Accurate:
return "Accurate"sv;
+ case SeekMode::ApproximateForSpeed:
+ return "ApproximateForSpeed"sv;
case SeekMode::FastBefore:
return "FastBefore"sv;
case SeekMode::FastAfter:
diff --git a/Libraries/LibMediaClient/Client.cpp b/Libraries/LibMediaClient/Client.cpp
index 53fb1a05385..0dfe06a4fe9 100644
--- a/Libraries/LibMediaClient/Client.cpp
+++ b/Libraries/LibMediaClient/Client.cpp
@@ -318,6 +318,12 @@ void Client::playback_session_duration_changed(u64 session_id, AK::Duration dura
playback_manager->duration_changed({}, duration);
}
+void Client::playback_session_seek_decided(u64 session_id, u64 seek_request_id, Media::SeekDecision decision)
+{
+ if (auto* playback_manager = find_playback_manager(session_id))
+ playback_manager->seek_decided({}, seek_request_id, decision);
+}
+
void Client::playback_session_state_changed(u64 session_id, u64 applied_seek_request_id, Media::PlaybackState state, bool is_playing, Media::AvailableData available_data, bool playback_state_is_settled, AK::Duration current_time)
{
if (state > Media::PlaybackState::Ended || available_data > Media::AvailableData::Future) {
diff --git a/Libraries/LibMediaClient/Client.h b/Libraries/LibMediaClient/Client.h
index 0d01d9e7dc0..a9b881f5cab 100644
--- a/Libraries/LibMediaClient/Client.h
+++ b/Libraries/LibMediaClient/Client.h
@@ -100,6 +100,7 @@ private:
virtual void playback_session_media_source_added(u64 session_id, Optional<u64> stream_id, Vector<Media::Track> audio_tracks, Vector<Media::Track> video_tracks, Optional<Media::Track> preferred_audio_track, Optional<Media::Track> preferred_video_track, Optional<UnixDateTime> start_time_realtime, AK::Duration earliest_possible_position, Media::DemuxerScanState scan_state) override;
virtual void playback_session_media_stream_source_failed(u64 session_id, u64 stream_id, Media::DecoderError error) override;
virtual void playback_session_duration_changed(u64 session_id, AK::Duration duration) override;
+ virtual void playback_session_seek_decided(u64 session_id, u64 seek_request_id, Media::SeekDecision decision) override;
virtual void playback_session_state_changed(u64 session_id, u64 applied_seek_request_id, Media::PlaybackState state, bool is_playing, Media::AvailableData available_data, bool playback_state_is_settled, AK::Duration current_time) override;
virtual void playback_session_media_stream_scan_state_changed(u64 session_id, u64 stream_id, Media::DemuxerScanState scan_state) override;
virtual void playback_session_error(u64 session_id, Media::DecoderError error) override;
diff --git a/Libraries/LibMediaClient/RemotePlaybackManager.cpp b/Libraries/LibMediaClient/RemotePlaybackManager.cpp
index 33a411537af..e04af2a9fb7 100644
--- a/Libraries/LibMediaClient/RemotePlaybackManager.cpp
+++ b/Libraries/LibMediaClient/RemotePlaybackManager.cpp
@@ -79,9 +79,10 @@ void RemotePlaybackManager::set_duration(AK::Duration duration)
AK::Duration RemotePlaybackManager::current_time() const
{
AK::Duration time;
- switch (m_state) {
+ switch (state()) {
case Media::PlaybackState::Ended:
return m_timeline.duration;
+ case Media::PlaybackState::BeginningSeek:
case Media::PlaybackState::Seeking:
time = m_seek_timestamp;
break;
@@ -99,7 +100,7 @@ Media::VideoSinkHandle RemotePlaybackManager::reserve_video_sink_handle(Media::T
{
auto handle = Media::allocate_video_sink_handle();
m_video_sinks.set(handle, VideoSink { track });
- bool resume_ended_playback = m_state != Media::PlaybackState::Ended;
+ bool resume_ended_playback = state() != Media::PlaybackState::Ended;
advance_seek_request_id();
if (can_send())
m_client->async_reserve_video_sink(m_session_id, m_latest_seek_request_id, track, handle, resume_ended_playback);
@@ -131,7 +132,7 @@ void RemotePlaybackManager::set_video_resize_handler(Media::VideoSinkHandle hand
void RemotePlaybackManager::enable_an_audio_track(Media::Track const& track)
{
// Once the end has been observed here, the element is paused at the duration, so the server must not resume.
- bool resume_ended_playback = m_state != Media::PlaybackState::Ended;
+ bool resume_ended_playback = state() != Media::PlaybackState::Ended;
m_enabled_audio_tracks.set(track);
advance_seek_request_id();
if (can_send())
@@ -182,16 +183,24 @@ void RemotePlaybackManager::seek(AK::Duration timestamp, Media::SeekMode mode)
{
m_seek_timestamp = timestamp;
advance_seek_request_id();
+ m_request_id_of_latest_seek = m_latest_seek_request_id;
for (auto& [id, source_buffer] : m_source_buffers)
source_buffer->invalidate_evictable_bytes({});
- dbgln_if(PLAYBACK_MANAGER_DEBUG, "RemotePlaybackManager({}): Requesting seek {} to {} ({}) in {}", m_session_id, m_latest_seek_request_id, timestamp, mode, m_state);
+ dbgln_if(PLAYBACK_MANAGER_DEBUG, "RemotePlaybackManager({}): Requesting seek {} to {} ({}) in {}", m_session_id, m_latest_seek_request_id, timestamp, mode, state());
if (can_send())
m_client->async_seek(m_session_id, m_latest_seek_request_id, timestamp, mode);
- if (m_state == Media::PlaybackState::Seeking)
- return;
- m_state = Media::PlaybackState::Seeking;
- m_available_data = Media::AvailableData::Current;
- dispatch_state_change();
+ auto previous_state = state();
+ m_seek_is_beginning = true;
+ if (state() != previous_state)
+ dispatch_state_change();
+}
+
+Media::PlaybackState RemotePlaybackManager::state() const
+{
+ // The server only reports a seek's state once it has decided that the seek goes ahead.
+ if (m_seek_is_beginning && m_state != Media::PlaybackState::Seeking)
+ return Media::PlaybackState::BeginningSeek;
+ return m_state;
}
void RemotePlaybackManager::set_volume(double volume)
@@ -398,13 +407,24 @@ void RemotePlaybackManager::duration_changed(Badge<Client>, AK::Duration duratio
dispatch_seekable_ranges_change();
}
+void RemotePlaybackManager::seek_decided(Badge<Client>, u64 seek_request_id, Media::SeekDecision decision)
+{
+ if (seek_request_id != m_request_id_of_latest_seek)
+ return;
+ // The server never left the state it was in for a skipped seek.
+ if (decision == Media::SeekDecision::Skip)
+ m_seek_is_beginning = false;
+ if (on_seek_decided)
+ on_seek_decided(decision);
+}
+
void RemotePlaybackManager::state_changed(Badge<Client>, u64 applied_seek_request_id, Media::PlaybackState state, bool is_playing, Media::AvailableData available_data, bool server_playback_state_is_settled, AK::Duration current_time)
{
if (applied_seek_request_id != m_latest_seek_request_id) {
dbgln_if(PLAYBACK_MANAGER_DEBUG, "RemotePlaybackManager({}): Dropping {} reported after seek {}, latest is {}", m_session_id, state, applied_seek_request_id, m_latest_seek_request_id);
return;
}
- dbgln_if(PLAYBACK_MANAGER_DEBUG, "RemotePlaybackManager({}): Server reports {} playing={} available={} settled={} time={} (mirror was {})", m_session_id, state, is_playing, to_underlying(available_data), server_playback_state_is_settled, current_time, m_state);
+ dbgln_if(PLAYBACK_MANAGER_DEBUG, "RemotePlaybackManager({}): Server reports {} playing={} available={} settled={} time={} (mirror was {})", m_session_id, state, is_playing, to_underlying(available_data), server_playback_state_is_settled, current_time, this->state());
if (state == Media::PlaybackState::Seeking)
m_seek_timestamp = current_time;
auto was_settled = playback_state_is_settled();
@@ -413,10 +433,11 @@ void RemotePlaybackManager::state_changed(Badge<Client>, u64 applied_seek_reques
m_is_playing = is_playing;
m_available_data = available_data;
dispatch_playback_state_settled_change_if_changed(was_settled);
- if (m_state == state)
- return;
+ auto previous_state = this->state();
+ m_seek_is_beginning = false;
m_state = state;
- dispatch_state_change();
+ if (this->state() != previous_state)
+ dispatch_state_change();
}
void RemotePlaybackManager::media_stream_scan_state_changed(Badge<Client>, u64 stream_id, Media::DemuxerScanState scan_state)
diff --git a/Libraries/LibMediaClient/RemotePlaybackManager.h b/Libraries/LibMediaClient/RemotePlaybackManager.h
index 8fe6a45b81a..cdc68d02257 100644
--- a/Libraries/LibMediaClient/RemotePlaybackManager.h
+++ b/Libraries/LibMediaClient/RemotePlaybackManager.h
@@ -86,7 +86,7 @@ public:
void seek(AK::Duration timestamp, Media::SeekMode);
bool is_playing() const { return m_is_playing; }
- Media::PlaybackState state() const { return m_state; }
+ Media::PlaybackState state() const;
Media::AvailableData available_data() const { return m_available_data; }
// Whether the playback state can only change through further input, such as more data or a seek.
bool playback_state_is_settled() const;
@@ -104,6 +104,8 @@ public:
Function<void()> on_playback_state_settled_change;
Function<void(AK::Duration)> on_duration_change;
Function<void()> on_buffered_ranges_change;
+ // Called for each seek, before any state change the seek causes.
+ Function<void(Media::SeekDecision)> on_seek_decided;
// Called whenever an input of the seekable ranges changes: the duration, the buffered ranges, or the live seekable
// range.
Function<void()> on_seekable_ranges_change;
@@ -118,6 +120,7 @@ public:
void media_source_added(Badge<Client>, Optional<u64> stream_id, Vector<Media::Track> audio_tracks, Vector<Media::Track> video_tracks, Optional<Media::Track> preferred_audio_track, Optional<Media::Track> preferred_video_track, Optional<AK::UnixDateTime> start_time_realtime, AK::Duration earliest_possible_position, Media::DemuxerScanState);
void media_stream_source_failed(Badge<Client>, u64 stream_id, Media::DecoderError);
void duration_changed(Badge<Client>, AK::Duration);
+ void seek_decided(Badge<Client>, u64 seek_request_id, Media::SeekDecision);
void state_changed(Badge<Client>, u64 applied_seek_request_id, Media::PlaybackState, bool is_playing, Media::AvailableData, bool server_playback_state_is_settled, AK::Duration current_time);
void media_stream_scan_state_changed(Badge<Client>, u64 stream_id, Media::DemuxerScanState);
void error(Badge<Client>, Media::DecoderError);
@@ -181,6 +184,10 @@ private:
// A seek reports its target as the current time until the server has chosen where it lands. State reported
// before the server applied the latest seek request is stale, and is ignored.
u64 m_latest_seek_request_id { 0 };
+ // Track changes also advance the seek request ID, so they don't supersede the decision for the latest seek.
+ u64 m_request_id_of_latest_seek { 0 };
+ // Set from a seek until the server reports its state after applying the seek, unless the seek is skipped.
+ bool m_seek_is_beginning { false };
u64 m_latest_reported_seek_request_id { 0 };
bool m_server_playback_state_is_settled { true };
AK::Duration m_seek_timestamp;
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index 051bfeb3270..d659ef10c24 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -288,6 +288,7 @@ void HTMLMediaElement::finalize()
if (m_playback_manager) {
m_playback_manager->on_playback_state_change = nullptr;
+ m_playback_manager->on_seek_decided = nullptr;
m_playback_manager->on_playback_state_settled_change = nullptr;
m_playback_manager->on_buffered_ranges_change = nullptr;
m_playback_manager->on_seekable_ranges_change = nullptr;
@@ -2644,6 +2645,9 @@ void HTMLMediaElement::set_up_playback_manager_for_remote()
m_playback_manager->on_playback_state_change = GC::weak_callback(*this, [](auto& self) {
self.on_playback_manager_state_change();
});
+ m_playback_manager->on_seek_decided = GC::weak_callback(*this, [](auto& self, Media::SeekDecision decision) {
+ self.on_playback_manager_seek_decided(decision);
+ });
m_playback_manager->on_playback_state_settled_change = GC::weak_callback(*this, [](auto& self) {
self.update_pending_activity();
});
@@ -2728,6 +2732,9 @@ void HTMLMediaElement::set_up_playback_manager_for_local(GC::Ref<GC::Function<vo
m_playback_manager->on_playback_state_change = GC::weak_callback(*this, [](auto& self) {
self.on_playback_manager_state_change();
});
+ m_playback_manager->on_seek_decided = GC::weak_callback(*this, [](auto& self, Media::SeekDecision decision) {
+ self.on_playback_manager_seek_decided(decision);
+ });
m_playback_manager->on_playback_state_settled_change = GC::weak_callback(*this, [](auto& self) {
self.update_pending_activity();
});
@@ -2928,6 +2935,7 @@ void HTMLMediaElement::forget_media_resource_specific_tracks()
// this; the error and emptied events, fired by the algorithms that invoke this one, can be used instead.
if (m_playback_manager) {
m_playback_manager->on_playback_state_change = nullptr;
+ m_playback_manager->on_seek_decided = nullptr;
m_playback_manager->on_playback_state_settled_change = nullptr;
}
release_active_video_sink();
@@ -3124,6 +3132,12 @@ void HTMLMediaElement::update_ready_state()
if (!m_playback_manager)
return;
+ // AD-HOC: The playback manager reports a seek's target as the current position before the seek has been decided,
+ // but the seek only sets the current playback position, which informs the ready state after queuing the
+ // seeking event.
+ if (m_playback_manager->state() == Media::PlaybackState::BeginningSeek)
+ return;
+
// -> If the HTMLMediaElement's readyState attribute is HAVE_NOTHING:
if (m_ready_state == ReadyState::HaveNothing) {
// 1. Abort these steps.
@@ -3263,7 +3277,7 @@ void HTMLMediaElement::on_playback_manager_state_change()
VERIFY(m_playback_manager);
auto state = m_playback_manager->state();
- if (seeking() && state != Media::PlaybackState::Seeking)
+ if (seeking() && !first_is_one_of(state, Media::PlaybackState::BeginningSeek, Media::PlaybackState::Seeking))
finish_seeking_element();
start_or_stop_playback_position_update_timer();
@@ -3411,83 +3425,19 @@ void HTMLMediaElement::seek_element(double playback_position, MediaSeekMode seek
// NOTE: We implement the async nature of seeking through PlaybackManager, and the steps here are implemented as options
// that define how PlaybackManager's seeking behaves.
- auto new_playback_position = playback_position;
-
- // 6. If the new playback position is later than the end of the media resource, then let it be the end of the media resource instead.
- new_playback_position = min(new_playback_position, m_duration);
-
- // 7. If the new playback position is less than the earliest possible position, let it be that position instead.
- new_playback_position = max(new_playback_position, earliest_possible_position());
-
- // 8. If the (possibly now changed) new playback position is not in one of the ranges given in the seekable attribute,
- auto time_ranges = seekable();
- if (!time_ranges->in_range(new_playback_position)) {
- // then let it be the position in one of the ranges given in the seekable attribute that is the nearest to the new
- // playback position.
-
- // If there are no ranges given in the seekable attribute, then set the seeking IDL attribute to false and return.
- if (time_ranges->length() == 0) {
- set_seeking(false);
- return;
- }
-
- double nearest_point;
- Optional<double> other_nearest_point = {};
- double distance = INFINITY;
- for (size_t i = 0; i < time_ranges->length(); i++) {
- for (double point : { MUST(time_ranges->start(i)), MUST(time_ranges->end(i)) }) {
- auto point_distance = abs(new_playback_position - point);
- if (point_distance < distance) {
- nearest_point = point;
- other_nearest_point = {};
- distance = point_distance;
- } else if (point_distance == distance) {
- other_nearest_point = point;
- }
- }
- }
-
- // If two positions both satisfy that constraint (i.e. the new playback position is exactly in the middle between two ranges
- // in the seekable attribute), then use the position that is closest to the current playback position.
- if (other_nearest_point.has_value()) {
- auto current_position = current_playback_position();
- auto nearest_point_distance = abs(current_position - nearest_point);
- auto other_nearest_point_distance = abs(current_position - other_nearest_point.value());
- if (nearest_point_distance < other_nearest_point_distance) {
- new_playback_position = nearest_point;
- } else {
- new_playback_position = other_nearest_point.value();
- }
- } else {
- new_playback_position = nearest_point;
- }
- }
-
- // 9. If the approximate-for-speed flag is set, adjust the new playback position to a value that will allow for playback to resume
- // promptly. If new playback position before this step is before current playback position, then the adjusted new playback position
- // must also be before the current playback position. Similarly, if the new playback position before this step is after current
- // playback position, then the adjusted new playback position must also be after the current playback position.
- auto manager_seek_mode = Media::SeekMode::Accurate;
- if (seek_mode == MediaSeekMode::ApproximateForSpeed) {
- if (new_playback_position < current_playback_position())
- manager_seek_mode = Media::SeekMode::FastBefore;
- else if (new_playback_position > current_playback_position())
- manager_seek_mode = Media::SeekMode::FastAfter;
- }
+ // NB: Steps 5 through 9 are handled by PlaybackManager.
// 10. Queue a media element task given the media element to fire an event named seeking at the element.
- queue_a_media_element_task(GC::create_function(GC::Heap::the(), [](HTMLMediaElement& self) {
- self.dispatch_event(DOM::Event::create(HTML::relevant_global_object(self), HTML::EventNames::seeking));
- }));
+ // NB: This happens once the playback manager has decided whether the seek goes ahead, in
+ // on_playback_manager_seek_decided().
// 11. Set the current playback position to the new playback position.
if (m_playback_manager) {
- AK::Duration new_playback_position_as_duration;
- if (new_playback_position == m_duration)
- new_playback_position_as_duration = m_playback_manager->duration();
- else
- new_playback_position_as_duration = AK::Duration::from_seconds_f64(new_playback_position);
- m_playback_manager->seek(new_playback_position_as_duration, manager_seek_mode);
+ auto timestamp = AK::Duration::from_seconds_f64(playback_position);
+ auto manager_seek_mode = Media::SeekMode::Accurate;
+ if (seek_mode == MediaSeekMode::ApproximateForSpeed)
+ manager_seek_mode = Media::SeekMode::ApproximateForSpeed;
+ m_playback_manager->seek(timestamp, manager_seek_mode);
}
// AD-HOC: Set the official playback position to the requested position, as the currentTime setter would. Doing it
@@ -3514,6 +3464,28 @@ void HTMLMediaElement::seek_element(double playback_position, MediaSeekMode seek
}
// https://html.spec.whatwg.org/multipage/media.html#dom-media-seek
+// https://html.spec.whatwg.org/multipage/media.html#dom-media-seek
+void HTMLMediaElement::on_playback_manager_seek_decided(Media::SeekDecision decision)
+{
+ // NB: The playback manager runs steps 6 through 9, and reports whether step 8 returned.
+ switch (decision) {
+ case Media::SeekDecision::Skip:
+ // 8. ... If there are no ranges given in the seekable attribute, then set the seeking IDL attribute to false and
+ // return.
+ set_seeking(false);
+ // NB: The official playback position was set to the requested position, but the playback position didn't move.
+ set_official_playback_position(current_playback_position());
+ return;
+ case Media::SeekDecision::Seek:
+ // 10. Queue a media element task given the media element to fire an event named seeking at the element.
+ queue_a_media_element_task(GC::create_function(GC::Heap::the(), [](HTMLMediaElement& self) {
+ self.dispatch_event(DOM::Event::create(HTML::relevant_global_object(self), HTML::EventNames::seeking));
+ }));
+ return;
+ }
+ VERIFY_NOT_REACHED();
+}
+
void HTMLMediaElement::finish_seeking_element()
{
document().page().active_media_session_tracker().mark_dirty();
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.h
index ac7ac4ed171..af8f90d7876 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h
@@ -21,6 +21,7 @@
#include <LibGfx/Rect.h>
#include <LibJS/Forward.h>
#include <LibMedia/Forward.h>
+#include <LibMedia/SeekMode.h>
#include <LibMedia/VideoSinkHandle.h>
#include <LibMediaClient/Forward.h>
#include <LibWeb/DOM/Document.h>
@@ -316,6 +317,7 @@ private:
void on_video_track_added(Media::Track const&);
void on_metadata_parsed(SourceType);
void on_playback_manager_state_change();
+ void on_playback_manager_seek_decided(Media::SeekDecision);
void upon_current_playback_position_possibly_changed();
void start_or_stop_playback_position_update_timer();
void play_element();
diff --git a/Services/MediaServer/MediaClient.ipc b/Services/MediaServer/MediaClient.ipc
index 6cd03412a98..ff59dc9cbf4 100644
--- a/Services/MediaServer/MediaClient.ipc
+++ b/Services/MediaServer/MediaClient.ipc
@@ -9,6 +9,7 @@
#include <LibMedia/MediaTime.h>
#include <LibMedia/PlaybackStates/AvailableData.h>
#include <LibMedia/PlaybackStates/PlaybackState.h>
+#include <LibMedia/SeekMode.h>
#include <LibMedia/TimeRanges.h>
#include <LibMedia/Track.h>
#include <LibMediaCodecs/VideoFrameMetadata.h>
@@ -33,6 +34,7 @@ endpoint MediaClient
playback_session_media_source_added(u64 session_id, Optional<u64> stream_id, Vector<Media::Track> audio_tracks, Vector<Media::Track> video_tracks, Optional<Media::Track> preferred_audio_track, Optional<Media::Track> preferred_video_track, Optional<UnixDateTime> start_time_realtime, AK::Duration earliest_possible_position, Media::DemuxerScanState scan_state) =|
playback_session_media_stream_source_failed(u64 session_id, u64 stream_id, Media::DecoderError error) =|
playback_session_duration_changed(u64 session_id, AK::Duration duration) =|
+ playback_session_seek_decided(u64 session_id, u64 seek_request_id, Media::SeekDecision decision) =|
playback_session_state_changed(u64 session_id, u64 applied_seek_request_id, Media::PlaybackState state, bool is_playing, Media::AvailableData available_data, bool playback_state_is_settled, AK::Duration current_time) =|
playback_session_media_stream_scan_state_changed(u64 session_id, u64 stream_id, Media::DemuxerScanState scan_state) =|
playback_session_error(u64 session_id, Media::DecoderError error) =|
diff --git a/Services/MediaServer/PlaybackSession.cpp b/Services/MediaServer/PlaybackSession.cpp
index a1c898e4a81..19fe38ec7ed 100644
--- a/Services/MediaServer/PlaybackSession.cpp
+++ b/Services/MediaServer/PlaybackSession.cpp
@@ -37,6 +37,9 @@ PlaybackSession::PlaybackSession(ConnectionFromClient& connection, u64 id, Audio
m_manager->on_buffered_ranges_change = [this] {
report_media_stream_scan_states();
};
+ m_manager->on_seek_decided = [this](Media::SeekDecision decision) {
+ m_connection.async_playback_session_seek_decided(m_id, m_applied_seek_request_id, decision);
+ };
m_manager->on_error = [this](Media::DecoderError&& error) {
m_connection.async_playback_session_error(m_id, move(error));
};
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 04518384d50..e0658ef993d 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -215,6 +215,7 @@ Text/input/HTML/media-source-buffered.html
Text/input/HTML/media-source-duration.html
Text/input/HTML/media-source-seek-past-seekable-ranges.html
Text/input/HTML/media-source-seek-without-seekable-ranges.html
+Text/input/HTML/media-source-seek-into-gap-orders-seeking-before-waiting.html
Text/input/HTML/media-source-duration-truncation.html
Text/input/HTML/media-source-ended-cleared-by-seek-after-remove.html
Text/input/HTML/media-source-frame-rate-change.html
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-ended-cleared-by-seek-after-remove.txt b/Tests/LibWeb/Text/expected/HTML/media-source-ended-cleared-by-seek-after-remove.txt
index e6848ac60f7..8d29a3a5a96 100644
--- a/Tests/LibWeb/Text/expected/HTML/media-source-ended-cleared-by-seek-after-remove.txt
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-ended-cleared-by-seek-after-remove.txt
@@ -2,7 +2,7 @@ after ending: ended=true
after remove: ended=true, readyState=HAVE_METADATA
after setting currentTime: ended=false
after play(): ended=false
-seeking: ended=false
play: ended=false
waiting: ended=false
+seeking: ended=false
playing: ended=false
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-seek-into-gap-orders-seeking-before-waiting.txt b/Tests/LibWeb/Text/expected/HTML/media-source-seek-into-gap-orders-seeking-before-waiting.txt
new file mode 100644
index 00000000000..08c761f594b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-seek-into-gap-orders-seeking-before-waiting.txt
@@ -0,0 +1,2 @@
+seeking event
+waiting event
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-seek-without-seekable-ranges.txt b/Tests/LibWeb/Text/expected/HTML/media-source-seek-without-seekable-ranges.txt
index 8a301a0e541..b22e636985b 100644
--- a/Tests/LibWeb/Text/expected/HTML/media-source-seek-without-seekable-ranges.txt
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-seek-without-seekable-ranges.txt
@@ -1,5 +1,5 @@
seekable ranges: 0
-after setting currentTime: seeking false
+after setting currentTime: seeking true
after the seek is skipped: currentTime 0
seekable ranges after appending media: 1
seeking event, seeking true, currentTime 1
diff --git a/Tests/LibWeb/Text/input/HTML/media-source-seek-into-gap-orders-seeking-before-waiting.html b/Tests/LibWeb/Text/input/HTML/media-source-seek-into-gap-orders-seeking-before-waiting.html
new file mode 100644
index 00000000000..6d4a1c9e853
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/media-source-seek-into-gap-orders-seeking-before-waiting.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script>internals.setAutoplayPolicy("allow-audio-and-video");</script>
+<video id="video"></video>
+<script src="../include.js"></script>
+<script>
+ // A seek queues its seeking event before it moves the playback position, and only the move can lower readyState,
+ // so a seek into unbuffered data while playing fires seeking before waiting.
+ asyncTest(done => {
+ const video = document.getElementById("video");
+ const mediaSource = new MediaSource();
+ video.src = URL.createObjectURL(mediaSource);
+
+ mediaSource.addEventListener("sourceopen", async () => {
+ const sourceBuffer = mediaSource.addSourceBuffer('video/webm;codecs="vp9"');
+ const response = await fetch("../../../Assets/vp9-frame-rate-change.webm");
+ const data = new Uint8Array(await response.arrayBuffer());
+ await new Promise(resolve => {
+ sourceBuffer.addEventListener("updateend", resolve, { once: true });
+ sourceBuffer.appendBuffer(data);
+ });
+
+ // Leave the end of the timeline seekable but unbuffered.
+ mediaSource.duration = 10;
+ await video.play();
+
+ for (const eventName of ["seeking", "waiting"])
+ video.addEventListener(eventName, () => println(`${eventName} event`), { once: true });
+ video.currentTime = 5;
+ await new Promise(resolve => video.addEventListener("waiting", resolve, { once: true }));
+ done();
+ });
+ });
+</script>