mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 13:50:00 +00:00
LibMedia+Tests: Adapt TestVorbisDecode to use AudioDataProvider
This commit is contained in:
parent
4cf0a77d38
commit
d17e7fd921
Notes:
github-actions[bot]
2025-10-28 00:32:29 +00:00
Author: https://github.com/Zaggy1024
Commit: d17e7fd921
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6410
Reviewed-by: https://github.com/R-Goc
Reviewed-by: https://github.com/gmta ✅
1 changed files with 54 additions and 11 deletions
|
|
@ -1,29 +1,72 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
|
||||
* Copyright (c) 2025, Gregory Bertilson <gregory@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibMedia/Audio/Loader.h>
|
||||
#include <AK/Time.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/MappedFile.h>
|
||||
#include <LibMedia/DecoderError.h>
|
||||
#include <LibMedia/FFmpeg/FFmpegDemuxer.h>
|
||||
#include <LibMedia/MutexedDemuxer.h>
|
||||
#include <LibMedia/Providers/AudioDataProvider.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
static void run_test(StringView file_name, int const num_samples, int const channels, u32 const rate)
|
||||
{
|
||||
constexpr auto format = "ogg";
|
||||
constexpr int bits = 32;
|
||||
Core::EventLoop loop;
|
||||
|
||||
ByteString in_path = ByteString::formatted("vorbis/{}", file_name);
|
||||
ByteString in_path = ByteString::formatted("{}", file_name);
|
||||
|
||||
auto loader = TRY_OR_FAIL(Audio::Loader::create(in_path));
|
||||
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<Media::MutexedDemuxer>(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()));
|
||||
|
||||
EXPECT_EQ(loader->format_name(), format);
|
||||
EXPECT_EQ(loader->sample_rate(), rate);
|
||||
EXPECT_EQ(loader->num_channels(), channels);
|
||||
EXPECT_EQ(loader->bits_per_sample(), bits);
|
||||
EXPECT_EQ(loader->total_samples(), num_samples);
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE(44_1Khz_stereo)
|
||||
{
|
||||
run_test("44_1Khz_stereo.ogg"sv, 352800, 2, 44100);
|
||||
// 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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue