diff --git a/Tests/LibMedia/CMakeLists.txt b/Tests/LibMedia/CMakeLists.txt index 0116596ac7..1b3b1982a8 100644 --- a/Tests/LibMedia/CMakeLists.txt +++ b/Tests/LibMedia/CMakeLists.txt @@ -1,6 +1,7 @@ include(audio) set(TEST_SOURCES + TestAudioDataProvider.cpp TestH264Decode.cpp TestParseMatroska.cpp TestPlaybackStream.cpp diff --git a/Tests/LibMedia/TestAudioDataProvider.cpp b/Tests/LibMedia/TestAudioDataProvider.cpp new file mode 100644 index 0000000000..6fab1a5e9f --- /dev/null +++ b/Tests/LibMedia/TestAudioDataProvider.cpp @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2025, Gregory Bertilson + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +TEST_CASE(xiph_lacing) +{ + // FIXME: 530 samples are marked to be discarded by the DiscardPadding element. + decode_audio("test-webm-xiph-lacing.mka"sv, 48'000, 2, 578038 + 530); +} diff --git a/Tests/LibMedia/TestMediaCommon.h b/Tests/LibMedia/TestMediaCommon.h index 9718efac81..df5fef6d42 100644 --- a/Tests/LibMedia/TestMediaCommon.h +++ b/Tests/LibMedia/TestMediaCommon.h @@ -1,17 +1,22 @@ /* - * Copyright (c) 2022, Gregory Bertilson + * Copyright (c) 2022-2025, Gregory Bertilson * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include - #include +#include +#include #include +#include +#include +#include +#include #include #include +#include template static inline void decode_video(StringView path, size_t expected_frame_count, T create_decoder) @@ -56,3 +61,57 @@ static inline void decode_video(StringView path, size_t expected_frame_count, T VERIFY_NOT_REACHED(); } + +static inline void decode_audio(StringView path, u32 sample_rate, u8 channel_count, size_t expected_sample_count) +{ + Core::EventLoop loop; + + auto mapped_file = TRY_OR_FAIL(Core::MappedFile::map(path)); + auto demuxer = MUST([&] -> Media::DecoderErrorOr> { + auto matroska_result = Media::Matroska::MatroskaDemuxer::from_data(mapped_file->bytes()); + if (!matroska_result.is_error()) + return matroska_result.release_value(); + return Media::FFmpeg::FFmpegDemuxer::from_data(mapped_file->bytes()); + }()); + auto mutexed_demuxer = make_ref_counted(demuxer); + auto track = TRY_OR_FAIL(demuxer->get_preferred_track_for_type(Media::TrackType::Audio)); + VERIFY(track.has_value()); + auto provider = TRY_OR_FAIL(Media::AudioDataProvider::try_create(mutexed_demuxer, track.release_value())); + + auto reached_end = false; + provider->set_error_handler([&](Media::DecoderError&& error) { + if (error.category() == Media::DecoderErrorCategory::EndOfStream) { + reached_end = true; + return; + } + FAIL("An error occurred while decoding."); + }); + + auto time_limit = AK::Duration::from_seconds(1); + auto start_time = MonotonicTime::now_coarse(); + + size_t sample_count = 0; + + while (true) { + auto block = provider->retrieve_block(); + if (block.is_empty()) { + if (reached_end) + break; + } else { + EXPECT_EQ(block.sample_rate(), sample_rate); + EXPECT_EQ(block.channel_count(), channel_count); + + sample_count += block.sample_count(); + } + + if (MonotonicTime::now_coarse() - start_time >= time_limit) { + FAIL("Decoding timed out."); + return; + } + + loop.pump(Core::EventLoop::WaitMode::PollForEvents); + } + + VERIFY(reached_end); + EXPECT_EQ(sample_count, expected_sample_count); +} diff --git a/Tests/LibMedia/TestVorbisDecode.cpp b/Tests/LibMedia/TestVorbisDecode.cpp index a439027e5e..33d57df3e5 100644 --- a/Tests/LibMedia/TestVorbisDecode.cpp +++ b/Tests/LibMedia/TestVorbisDecode.cpp @@ -5,68 +5,10 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include -#include -#include -#include -#include -#include -#include -#include - -static void run_test(StringView file_name, int const num_samples, int const channels, u32 const rate) -{ - Core::EventLoop loop; - - ByteString in_path = ByteString::formatted("{}", file_name); - - auto mapped_file = TRY_OR_FAIL(Core::MappedFile::map(in_path)); - auto demuxer = TRY_OR_FAIL(Media::FFmpeg::FFmpegDemuxer::from_data(mapped_file->bytes())); - auto mutexed_demuxer = make_ref_counted(demuxer); - auto track = TRY_OR_FAIL(demuxer->get_preferred_track_for_type(Media::TrackType::Audio)); - VERIFY(track.has_value()); - auto provider = TRY_OR_FAIL(Media::AudioDataProvider::try_create(mutexed_demuxer, track.release_value())); - - auto reached_end = false; - provider->set_error_handler([&](Media::DecoderError&& error) { - if (error.category() == Media::DecoderErrorCategory::EndOfStream) { - reached_end = true; - return; - } - FAIL("An error occurred while decoding."); - }); - - auto time_limit = AK::Duration::from_seconds(1); - auto start_time = MonotonicTime::now_coarse(); - - i64 sample_count = 0; - - while (true) { - auto block = provider->retrieve_block(); - if (block.is_empty()) { - if (reached_end) - break; - } else { - EXPECT_EQ(block.sample_rate(), rate); - EXPECT_EQ(block.channel_count(), channels); - - sample_count += block.sample_count(); - } - - if (MonotonicTime::now_coarse() - start_time >= time_limit) { - FAIL("Decoding timed out."); - return; - } - - loop.pump(Core::EventLoop::WaitMode::PollForEvents); - } - - VERIFY(reached_end); - EXPECT_EQ(sample_count, num_samples); -} +#include TEST_CASE(44_1Khz_stereo) { - // NOTE: 96 samples are marked to be discarded, but AudioDataProvider currently is not aware of this. - run_test("vorbis/44_1Khz_stereo.ogg"sv, 352800 + 96, 2, 44100); + // FIXME: 96 samples are marked to be discarded, but AudioDataProvider currently is not aware of this. + decode_audio("vorbis/44_1Khz_stereo.ogg"sv, 44100, 2, 352800 + 96); } diff --git a/Tests/LibMedia/test-webm-xiph-lacing.mka b/Tests/LibMedia/test-webm-xiph-lacing.mka new file mode 100644 index 0000000000..48f870e41c Binary files /dev/null and b/Tests/LibMedia/test-webm-xiph-lacing.mka differ