Skip to content

Truly independent web browser

LibMedia: End the stream when a media seek overflows the mixer window

Zaggy1024033d8f3297b8
diff --git a/Libraries/LibMedia/Processors/AudioMixer.cpp b/Libraries/LibMedia/Processors/AudioMixer.cpp
index 9de0d6dd803..d2e498f28b1 100644
--- a/Libraries/LibMedia/Processors/AudioMixer.cpp
+++ b/Libraries/LibMedia/Processors/AudioMixer.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Checked.h>
#include <LibCore/EventLoop.h>
#include <LibMedia/PipelineStatus.h>
#include <LibMedia/Processors/AudioMixer.h>
@@ -210,7 +211,14 @@ PipelineStatus AudioMixer::mix_into_output_block_while_locked()
auto max_frame_count = AudioBlock::max_frame_count(channel_count);
auto buffer_start_frame = m_next_frame_to_write;
- auto frames_end_cap = buffer_start_frame + static_cast<i64>(max_frame_count);
+ Checked<i64> checked_frames_end_cap = buffer_start_frame;
+ checked_frames_end_cap += static_cast<i64>(max_frame_count);
+ // A seek to the end of representable time has no frames left to mix.
+ if (checked_frames_end_cap.has_overflow()) {
+ drop_buffered_data_while_locked();
+ return PipelineStatus::EndOfStream;
+ }
+ auto frames_end_cap = checked_frames_end_cap.value();
auto combined_status_after_mix = PipelineStatus::EndOfStream;
i64 latest_mixed_frame = frames_end_cap;
diff --git a/Tests/LibMedia/CMakeLists.txt b/Tests/LibMedia/CMakeLists.txt
index 410db642f88..527fa716e5f 100644
--- a/Tests/LibMedia/CMakeLists.txt
+++ b/Tests/LibMedia/CMakeLists.txt
@@ -4,6 +4,7 @@ set(TEST_SOURCES
TestADTSReader.cpp
TestAudioBlockTimingRing.cpp
TestAudioDiscardIntervals.cpp
+ TestAudioMixer.cpp
TestBitReader.cpp
TestBufferedRanges.cpp
TestCICP.cpp
diff --git a/Tests/LibMedia/TestAudioMixer.cpp b/Tests/LibMedia/TestAudioMixer.cpp
new file mode 100644
index 00000000000..cc5a2fb30e4
--- /dev/null
+++ b/Tests/LibMedia/TestAudioMixer.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2026, Gregory Bertilson <[email protected]>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibAudio/ChannelMap.h>
+#include <LibAudio/SampleSpecification.h>
+#include <LibMedia/Processors/AudioMixer.h>
+#include <LibMedia/Producers/AudioProducer.h>
+#include <LibTest/TestCase.h>
+
+namespace {
+
+// An input that always has one block of silence to mix.
+class ScriptedAudioProducer final : public Media::AudioProducer {
+public:
+ void start() override { }
+ void consume() override { m_block.clear(); }
+ void set_wake_handler(Media::PipelineWakeHandler) override { }
+ void seek(AK::Duration) override { }
+
+ ErrorOr<void> set_output_sample_specification(Audio::SampleSpecification specification) override
+ {
+ m_specification = specification;
+ return {};
+ }
+
+ Media::AudioProducerOutput peek() override
+ {
+ if (m_block.is_empty()) {
+ m_block.initialize(m_specification, static_cast<i64>(0), 128);
+ for (size_t channel = 0; channel < m_specification.channel_count(); ++channel)
+ m_block.channel_data(channel).fill(0.0f);
+ }
+ return { &m_block, Media::PipelineStatus::HaveData };
+ }
+
+private:
+ Audio::SampleSpecification m_specification;
+ Media::AudioBlock m_block;
+};
+
+}
+
+// A seek to a timestamp whose frame index saturates must not overflow the mixer's frame window arithmetic.
+TEST_CASE(seek_to_unrepresentable_timestamp_ends_the_stream)
+{
+ auto mixer = MUST(Media::AudioMixer::try_create());
+ MUST(mixer->connect_input(adopt_ref(*new ScriptedAudioProducer())));
+ MUST(mixer->set_output_sample_specification(Audio::SampleSpecification(48'000, Audio::ChannelMap::stereo())));
+
+ mixer->seek(AK::Duration::max());
+ mixer->start();
+
+ auto output = mixer->peek();
+ EXPECT_EQ(output.status, Media::PipelineStatus::EndOfStream);
+ EXPECT_EQ(output.block, nullptr);
+}