Skip to content

Truly independent web browser

LibMedia+LibMediaClient+Tests: Share seekable steps between managers

Zaggy10247f34916cbec9
diff --git a/Libraries/LibMedia/MediaTimeline.h b/Libraries/LibMedia/MediaTimeline.h
new file mode 100644
index 00000000000..3f570dedc55
--- /dev/null
+++ b/Libraries/LibMedia/MediaTimeline.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/Time.h>
+#include <LibMedia/TimeRanges.h>
+
+namespace Media {
+
+struct MediaTimeline {
+ // AK::Duration::max() means the duration is unbounded, as a live stream's is.
+ AK::Duration duration;
+ Optional<TimeRanges::Range> live_seekable_range;
+};
+
+}
diff --git a/Libraries/LibMedia/PlaybackManager.cpp b/Libraries/LibMedia/PlaybackManager.cpp
index 050cc1117ca..018da372c7b 100644
--- a/Libraries/LibMedia/PlaybackManager.cpp
+++ b/Libraries/LibMedia/PlaybackManager.cpp
@@ -453,19 +453,19 @@ void PlaybackManager::check_for_demuxed_duration_change(AK::Duration duration)
{
if (m_duration_is_set_by_owner)
return;
- if (m_duration == duration)
+ if (m_timeline.duration == duration)
return;
- m_duration = duration;
+ m_timeline.duration = duration;
if (on_duration_change)
- on_duration_change(m_duration);
+ on_duration_change(m_timeline.duration);
}
void PlaybackManager::set_duration(AK::Duration duration)
{
m_duration_is_set_by_owner = true;
- if (m_duration == duration)
+ if (m_timeline.duration == duration)
return;
- m_duration = duration;
+ m_timeline.duration = duration;
dispatch_buffered_ranges_change();
}
@@ -802,14 +802,61 @@ AvailableData PlaybackManager::available_data()
return m_handler->available_data();
}
-BufferedRanges PlaybackManager::buffered_time_ranges() const
+// https://w3c.github.io/media-source/#htmlmediaelement-extensions-seekable
+// NB: The spec defines these ranges for a media element with a MediaSource attached; they apply to every source alike.
+Optional<TimeRanges::Range> PlaybackManager::seekable_time_range(TimeRanges const& buffered, MediaTimeline const& timeline)
+{
+ // -> If recent duration equals positive Infinity:
+ if (timeline.duration == AK::Duration::max()) {
+ // 1. If recent live seekable range is not empty:
+ if (timeline.live_seekable_range.has_value()) {
+ // 1. Let union ranges be the union of recent live seekable range and the HTMLMediaElement's buffered
+ // attribute.
+ // 2. Return a single range with a start time equal to the earliest start time in union ranges and an end
+ // time equal to the highest end time in union ranges and abort these steps.
+ auto union_start = timeline.live_seekable_range->start;
+ auto union_end = timeline.live_seekable_range->end;
+ if (!buffered.is_empty()) {
+ union_start = min(union_start, buffered[0].start);
+ union_end = max(union_end, buffered.highest_end_time());
+ }
+ return TimeRanges::Range { union_start, union_end };
+ }
+
+ // 2. If the HTMLMediaElement's buffered attribute returns an empty TimeRanges object, then return an empty
+ // TimeRanges object and abort these steps.
+ if (buffered.is_empty())
+ return {};
+
+ // 3. Return a single range with a start time of 0 and an end time equal to the highest end time reported by the
+ // HTMLMediaElement's buffered attribute.
+ return TimeRanges::Range { AK::Duration::zero(), buffered.highest_end_time() };
+ }
+
+ // -> Otherwise:
+ // Return a single range with a start time of 0 and an end time equal to recent duration.
+ return TimeRanges::Range { AK::Duration::zero(), timeline.duration };
+}
+
+Vector<CombinedScanState, 2> PlaybackManager::enabled_source_scan_states() const
{
- Vector<CombinedScanState, 2> active_sources;
+ Vector<CombinedScanState, 2> enabled_sources;
for (auto const& demuxer : m_demuxers) {
if (source_has_enabled_track(demuxer))
- active_sources.append(scan_state_for_enabled_tracks(demuxer));
+ enabled_sources.append(scan_state_for_enabled_tracks(demuxer));
}
- return DemuxerScanState::buffered_ranges_of_sources(active_sources);
+ return enabled_sources;
+}
+
+BufferedRanges PlaybackManager::buffered_time_ranges() const
+{
+ return DemuxerScanState::buffered_ranges_of_sources(enabled_source_scan_states());
+}
+
+Optional<TimeRanges::Range> PlaybackManager::seekable_time_ranges() const
+{
+ auto buffered = DemuxerScanState::buffered_ranges_of_sources(enabled_source_scan_states());
+ return seekable_time_range(buffered.time_ranges, m_timeline);
}
CombinedScanState PlaybackManager::scan_state_for_enabled_tracks(Demuxer const& demuxer) const
diff --git a/Libraries/LibMedia/PlaybackManager.h b/Libraries/LibMedia/PlaybackManager.h
index fb1912fd28b..5c2e57fe5ab 100644
--- a/Libraries/LibMedia/PlaybackManager.h
+++ b/Libraries/LibMedia/PlaybackManager.h
@@ -24,6 +24,7 @@
#include <LibMedia/Forward.h>
#include <LibMedia/MediaClock.h>
#include <LibMedia/MediaTime.h>
+#include <LibMedia/MediaTimeline.h>
#include <LibMedia/PipelineStatus.h>
#include <LibMedia/PlaybackStates/Forward.h>
#include <LibMedia/PlaybackStates/PlaybackState.h>
@@ -60,10 +61,13 @@ public:
using AudioTracks = Vector<Track, EXPECTED_AUDIO_TRACK_COUNT>;
+ // Shared with the proxies of a playback manager in another process, so that both report the same ranges.
+ static Optional<TimeRanges::Range> seekable_time_range(TimeRanges const& buffered, MediaTimeline const&);
+
static NonnullOwnPtr<PlaybackManager> create(NonnullRefPtr<AudioOutputFactory>);
~PlaybackManager();
- AK::Duration duration() const { return m_duration; }
+ AK::Duration duration() const { return m_timeline.duration; }
void set_duration(AK::Duration);
AK::Duration current_time() const;
@@ -98,6 +102,8 @@ public:
PlaybackState state();
AvailableData available_data();
BufferedRanges buffered_time_ranges() const;
+ // This is never more than one range, and that range may start and end at the same time.
+ Optional<TimeRanges::Range> seekable_time_ranges() const;
// Whether the playback state can only change through further input, such as more data or a seek.
bool playback_state_is_settled() const { return m_playback_state_is_settled; }
// Called after data is added, so that the playback state is known to be unsettled before the addition is reported
@@ -192,6 +198,7 @@ private:
bool source_has_enabled_track(Demuxer const&) const;
Optional<AK::Duration> verified_end_time_for_source(Demuxer const&) const;
CombinedScanState scan_state_for_enabled_tracks(Demuxer const&) const;
+ Vector<CombinedScanState, 2> enabled_source_scan_states() const;
void update_pipeline_state();
void hand_out_latest_checkpoint();
bool every_waiting_sink_has_reported_the_latest_checkpoint() const;
@@ -274,7 +281,7 @@ private:
Optional<Track> m_preferred_video_track;
Optional<Track> m_preferred_audio_track;
- AK::Duration m_duration;
+ MediaTimeline m_timeline;
bool m_duration_is_set_by_owner { false };
Optional<AK::UnixDateTime> m_start_time_realtime;
diff --git a/Libraries/LibMediaClient/RemotePlaybackManager.cpp b/Libraries/LibMediaClient/RemotePlaybackManager.cpp
index dd7b835f625..a806d5e3db3 100644
--- a/Libraries/LibMediaClient/RemotePlaybackManager.cpp
+++ b/Libraries/LibMediaClient/RemotePlaybackManager.cpp
@@ -7,6 +7,7 @@
#include <AK/AnyOf.h>
#include <AK/Debug.h>
#include <LibCore/EventLoop.h>
+#include <LibMedia/PlaybackManager.h>
#include <LibMediaClient/Client.h>
#include <LibMediaClient/RemoteMediaStream.h>
#include <LibMediaClient/RemotePlaybackManager.h>
@@ -69,7 +70,7 @@ bool RemotePlaybackManager::can_send() const
void RemotePlaybackManager::set_duration(AK::Duration duration)
{
m_duration_is_set_by_owner = true;
- m_duration = duration;
+ m_timeline.duration = duration;
if (can_send())
m_client->async_set_duration(m_session_id, duration);
dispatch_seekable_ranges_change();
@@ -80,7 +81,7 @@ AK::Duration RemotePlaybackManager::current_time() const
AK::Duration time;
switch (m_state) {
case Media::PlaybackState::Ended:
- return m_duration;
+ return m_timeline.duration;
case Media::PlaybackState::Seeking:
time = m_seek_timestamp;
break;
@@ -91,7 +92,7 @@ AK::Duration RemotePlaybackManager::current_time() const
time = m_seek_timestamp;
break;
}
- return min(time, m_duration);
+ return min(time, m_timeline.duration);
}
Media::VideoSinkHandle RemotePlaybackManager::reserve_video_sink_handle(Media::Track const& track)
@@ -268,61 +269,34 @@ bool RemotePlaybackManager::source_has_enabled_track(ReadonlySpan<Media::Track>
// The ranges are calculated here from the state of each source, so that enabling or disabling tracks and reaching the
// end of a source buffer's stream are reflected at once.
-Media::BufferedRanges RemotePlaybackManager::buffered_time_ranges() const
+Vector<Media::CombinedScanState> RemotePlaybackManager::enabled_source_scan_states() const
{
- Vector<Media::CombinedScanState> active_sources;
+ Vector<Media::CombinedScanState> enabled_sources;
for (auto const& [stream_id, source] : m_media_stream_sources) {
if (source_has_enabled_track(source.audio_tracks, source.video_tracks))
- active_sources.append(scan_state_for_enabled_tracks(source.scan_state, source.audio_tracks, source.video_tracks));
+ enabled_sources.append(scan_state_for_enabled_tracks(source.scan_state, source.audio_tracks, source.video_tracks));
}
for (auto const& [source_buffer_id, source_buffer] : m_source_buffers) {
if (source_has_enabled_track(source_buffer->audio_tracks(), source_buffer->video_tracks()))
- active_sources.append(scan_state_for_enabled_tracks(source_buffer->scan_state(), source_buffer->audio_tracks(), source_buffer->video_tracks()));
+ enabled_sources.append(scan_state_for_enabled_tracks(source_buffer->scan_state(), source_buffer->audio_tracks(), source_buffer->video_tracks()));
}
- return Media::DemuxerScanState::buffered_ranges_of_sources(active_sources);
+ return enabled_sources;
}
-// https://w3c.github.io/media-source/#htmlmediaelement-extensions-seekable
-// NB: The spec defines these ranges for a media element with a MediaSource attached; they apply to every source alike.
-Optional<Media::TimeRanges::Range> RemotePlaybackManager::seekable_time_ranges() const
+Media::BufferedRanges RemotePlaybackManager::buffered_time_ranges() const
{
- // -> If recent duration equals positive Infinity:
- if (m_duration == AK::Duration::max()) {
- auto buffered = buffered_time_ranges().time_ranges;
-
- // 1. If recent live seekable range is not empty:
- if (m_live_seekable_range.has_value()) {
- // 1. Let union ranges be the union of recent live seekable range and the HTMLMediaElement's buffered
- // attribute.
- // 2. Return a single range with a start time equal to the earliest start time in union ranges and an end
- // time equal to the highest end time in union ranges and abort these steps.
- auto start = m_live_seekable_range->start;
- auto end = m_live_seekable_range->end;
- if (!buffered.is_empty()) {
- start = min(start, buffered[0].start);
- end = max(end, buffered.highest_end_time());
- }
- return Media::TimeRanges::Range { start, end };
- }
-
- // 2. If the HTMLMediaElement's buffered attribute returns an empty TimeRanges object, then return an empty
- // TimeRanges object and abort these steps.
- if (buffered.is_empty())
- return {};
-
- // 3. Return a single range with a start time of 0 and an end time equal to the highest end time reported by the
- // HTMLMediaElement's buffered attribute.
- return Media::TimeRanges::Range { AK::Duration::zero(), buffered.highest_end_time() };
- }
+ return Media::DemuxerScanState::buffered_ranges_of_sources(enabled_source_scan_states());
+}
- // -> Otherwise:
- // Return a single range with a start time of 0 and an end time equal to recent duration.
- return Media::TimeRanges::Range { AK::Duration::zero(), m_duration };
+Optional<Media::TimeRanges::Range> RemotePlaybackManager::seekable_time_ranges() const
+{
+ auto buffered = Media::DemuxerScanState::buffered_ranges_of_sources(enabled_source_scan_states());
+ return Media::PlaybackManager::seekable_time_range(buffered.time_ranges, m_timeline);
}
void RemotePlaybackManager::set_live_seekable_range(Optional<Media::TimeRanges::Range> range)
{
- m_live_seekable_range = range;
+ m_timeline.live_seekable_range = range;
dispatch_seekable_ranges_change();
}
@@ -415,7 +389,7 @@ void RemotePlaybackManager::duration_changed(Badge<Client>, AK::Duration duratio
// A demuxed duration reported before the server saw the owner's must not replace it.
if (m_duration_is_set_by_owner)
return;
- m_duration = duration;
+ m_timeline.duration = duration;
if (on_duration_change)
on_duration_change(duration);
dispatch_seekable_ranges_change();
diff --git a/Libraries/LibMediaClient/RemotePlaybackManager.h b/Libraries/LibMediaClient/RemotePlaybackManager.h
index 19497fed13f..38b0cc56772 100644
--- a/Libraries/LibMediaClient/RemotePlaybackManager.h
+++ b/Libraries/LibMediaClient/RemotePlaybackManager.h
@@ -24,6 +24,7 @@
#include <LibMedia/Forward.h>
#include <LibMedia/MediaSourceExtensions/SourceBufferProcessor.h>
#include <LibMedia/MediaTime.h>
+#include <LibMedia/MediaTimeline.h>
#include <LibMedia/PlaybackStates/AvailableData.h>
#include <LibMedia/PlaybackStates/PlaybackState.h>
#include <LibMedia/SeekMode.h>
@@ -52,7 +53,7 @@ public:
u64 session_id() const { return m_session_id; }
Client* client() { return m_client.ptr(); }
- AK::Duration duration() const { return m_duration; }
+ AK::Duration duration() const { return m_timeline.duration; }
void set_duration(AK::Duration);
AK::Duration current_time() const;
@@ -89,8 +90,7 @@ public:
// Whether the playback state can only change through further input, such as more data or a seek.
bool playback_state_is_settled() const;
Media::BufferedRanges buffered_time_ranges() const;
- // The range playback can seek to, if any. AK::Duration::max() as the duration means it's unbounded, as a live
- // stream's is. NB: This is never more than one range, and that range may start and end at the same time.
+ // This is never more than one range, and that range may start and end at the same time.
Optional<Media::TimeRanges::Range> seekable_time_ranges() const;
// The range a live stream's player says can be sought to, besides the buffered ranges.
void set_live_seekable_range(Optional<Media::TimeRanges::Range>);
@@ -158,14 +158,14 @@ private:
void dispatch_seekable_ranges_change() const;
bool source_has_enabled_track(ReadonlySpan<Media::Track> audio_tracks, ReadonlySpan<Media::Track> video_tracks) const;
Media::CombinedScanState scan_state_for_enabled_tracks(Media::DemuxerScanState const&, ReadonlySpan<Media::Track> audio_tracks, ReadonlySpan<Media::Track> video_tracks) const;
+ Vector<Media::CombinedScanState> enabled_source_scan_states() const;
RefPtr<Client> m_client;
u64 m_session_id { 0 };
Optional<Media::MediaTimeReader> m_time_reader;
- AK::Duration m_duration;
+ Media::MediaTimeline m_timeline;
bool m_duration_is_set_by_owner { false };
- Optional<Media::TimeRanges::Range> m_live_seekable_range;
Optional<AK::UnixDateTime> m_start_time_realtime;
bool m_is_in_error_state { false };
Vector<Media::Track> m_audio_tracks;
diff --git a/Tests/LibMedia/TestTimeRanges.cpp b/Tests/LibMedia/TestTimeRanges.cpp
index bf9bc9fefb3..880e7a7f9af 100644
--- a/Tests/LibMedia/TestTimeRanges.cpp
+++ b/Tests/LibMedia/TestTimeRanges.cpp
@@ -6,6 +6,7 @@
#include <AK/Array.h>
#include <LibMedia/DemuxerScanState.h>
+#include <LibMedia/PlaybackManager.h>
#include <LibMedia/TimeRanges.h>
#include <LibTest/TestCase.h>
@@ -563,3 +564,21 @@ TEST_CASE(range_at_or_after_at_range_boundary)
EXPECT_EQ(ranges.range_at_or_after(ms(500)), (TimeRanges::Range { ms(0), ms(500) }));
EXPECT_EQ(ranges.range_at_or_after(ms(501)), (TimeRanges::Range { ms(700), ms(1000) }));
}
+
+TEST_CASE(seekable_range_follows_the_duration_and_the_live_seekable_range)
+{
+ Media::MediaTimeline timeline;
+ timeline.duration = ms(12000);
+ auto buffered = TimeRanges { { ms(2000), ms(10000) } };
+ EXPECT_EQ(Media::PlaybackManager::seekable_time_range(buffered, timeline), (TimeRanges::Range { ms(0), ms(12000) }));
+
+ // An unbounded duration ends the range where the buffered ranges end.
+ timeline.duration = AK::Duration::max();
+ EXPECT_EQ(Media::PlaybackManager::seekable_time_range(buffered, timeline), (TimeRanges::Range { ms(0), ms(10000) }));
+ EXPECT(!Media::PlaybackManager::seekable_time_range({}, timeline).has_value());
+
+ // A live seekable range is joined with the buffered ranges.
+ timeline.live_seekable_range = TimeRanges::Range { ms(5000), ms(20000) };
+ buffered = TimeRanges { { ms(8000), ms(25000) } };
+ EXPECT_EQ(Media::PlaybackManager::seekable_time_range(buffered, timeline), (TimeRanges::Range { ms(5000), ms(25000) }));
+}