Skip to content

Truly independent web browser

LibWeb: Read back the exact SourceBuffer.timestampOffset that was set

Aliaksandr Kalenikf395bdab6e56
diff --git a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp
index 7898c4748dc..b9cf6012f7f 100644
--- a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp
+++ b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp
@@ -226,7 +226,13 @@ Bindings::AppendMode SourceBuffer::mode() const
// https://w3c.github.io/media-source/#dom-sourcebuffer-timestampoffset
double SourceBuffer::timestamp_offset() const
{
- return m_remote_source_buffer->published_state().timestamp_offset.to_seconds_f64();
+ // NB: The media server keeps the offset in nanoseconds, so reading it back would round the value that was
+ // assigned. Scripts compare the two to decide whether to assign it again, so hand back the assigned value
+ // until coded frame processing moves the offset elsewhere.
+ auto published_timestamp_offset = m_remote_source_buffer->published_state().timestamp_offset;
+ if (published_timestamp_offset == AK::Duration::from_seconds_f64(m_timestamp_offset_assigned_by_script))
+ return m_timestamp_offset_assigned_by_script;
+ return published_timestamp_offset.to_seconds_f64();
}
// https://w3c.github.io/media-source/#dom-sourcebuffer-timestampoffset
@@ -257,6 +263,7 @@ WebIDL::ExceptionOr<void> SourceBuffer::set_timestamp_offset(double timestamp_of
// 6. If the mode attribute equals "sequence", then set the [[group start timestamp]] to new timestamp offset.
// 7. Update the attribute to new timestamp offset.
+ m_timestamp_offset_assigned_by_script = timestamp_offset;
m_remote_source_buffer->set_timestamp_offset(AK::Duration::from_seconds_f64(timestamp_offset));
return {};
diff --git a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h
index c1d99f3dbfe..456079a0e8b 100644
--- a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h
+++ b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h
@@ -119,6 +119,9 @@ private:
// https://w3c.github.io/media-source/#dom-sourcebuffer-updating
bool m_updating { false };
+ // https://w3c.github.io/media-source/#dom-sourcebuffer-timestampoffset
+ double m_timestamp_offset_assigned_by_script { 0 };
+
// https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowstart
double m_append_window_start { 0 };
// https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowend
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-timestamp-offset-round-trips.txt b/Tests/LibWeb/Text/expected/HTML/media-source-timestamp-offset-round-trips.txt
new file mode 100644
index 00000000000..eac16bb9de7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-timestamp-offset-round-trips.txt
@@ -0,0 +1,2 @@
+PASS: timestampOffset reads back -0.036281179138321996
+PASS: timestampOffset reads back 0.3333333333333333
diff --git a/Tests/LibWeb/Text/input/HTML/media-source-timestamp-offset-round-trips.html b/Tests/LibWeb/Text/input/HTML/media-source-timestamp-offset-round-trips.html
new file mode 100644
index 00000000000..cd62fb0f8f8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/media-source-timestamp-offset-round-trips.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<video id="video"></video>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ const mediaSource = new MediaSource();
+ video.src = URL.createObjectURL(mediaSource);
+ mediaSource.addEventListener("sourceopen", () => {
+ const sourceBuffer = mediaSource.addSourceBuffer('video/mp4;codecs="av01.0.00M.08,mp4a.40.2"');
+
+ // An AAC encoder delay of 1600 frames at 44.1 kHz, and a third, are both finer than a nanosecond.
+ for (const value of [-1600 / 44100, 1 / 3]) {
+ sourceBuffer.timestampOffset = value;
+ if (sourceBuffer.timestampOffset === value)
+ println(`PASS: timestampOffset reads back ${value}`);
+ else
+ println(`FAIL: timestampOffset was set to ${value} but reads back ${sourceBuffer.timestampOffset}`);
+ }
+ done();
+ });
+ });
+</script>