Files
ladybird/Tests/LibMedia/TestPlaybackStream.cpp

58 lines
2.0 KiB
C++
Raw Permalink Normal View History

/*
* Copyright (c) 2023-2025, Gregory Bertilson <gregory@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Math.h>
#include <AK/MemoryStream.h>
#include <AK/WeakPtr.h>
#include <LibCore/EventLoop.h>
2025-08-04 01:59:24 -07:00
#include <LibCore/System.h>
#include <LibMedia/Audio/PlaybackStream.h>
#include <LibTest/TestSuite.h>
#if defined(HAVE_PULSEAUDIO)
# include <LibMedia/Audio/PulseAudioWrappers.h>
#endif
// We are unable to create a playback stream on windows without an audio output device, so this would fail in CI
#if !defined(AK_OS_WINDOWS)
TEST_CASE(create_and_destroy_playback_stream)
{
Core::EventLoop event_loop;
bool has_implementation = false;
# if defined(HAVE_PULSEAUDIO) || defined(AK_OS_MACOS)
has_implementation = true;
# endif
{
auto stream_result = Audio::PlaybackStream::create(Audio::OutputState::Playing, 100, [](Audio::SampleSpecification) {}, [](Span<float> buffer) -> ReadonlySpan<float> { return buffer.trim(0); });
if (has_implementation) {
auto stream = stream_result.release_value();
EXPECT_EQ(stream->total_time_played(), AK::Duration::zero());
for (int i = 0; i < 5; i++) {
stream->resume()->when_rejected([](Error const&) { VERIFY_NOT_REACHED(); });
stream->drain_buffer_and_suspend()->when_rejected([](Error const&) { VERIFY_NOT_REACHED(); });
}
} else {
EXPECT(stream_result.is_error());
dbgln("Failed to create playback stream: {}", stream_result.error());
}
}
# if defined(HAVE_PULSEAUDIO)
// The PulseAudio context is kept alive by the PlaybackStream's control thread, which blocks on
// some operations, so it won't necessarily be destroyed immediately.
auto wait_start = MonotonicTime::now_coarse();
while (Audio::PulseAudioContext::is_connected()) {
if (MonotonicTime::now_coarse() - wait_start > AK::Duration::from_milliseconds(100))
VERIFY_NOT_REACHED();
}
# endif
}
#endif