ladybird/Tests/LibAudio/TestPlaybackStream.cpp
Zaggy1024 50c73d02f0 LibAudio: Add a test for creating and destructing a PlaybackStream
This will ensure that we don't leak any memory while playing back
audio.

There is an expectation value in the test that is only set to true when
PulseAudio is present for the moment. When any new implementation is
added for other libraries/platforms, we should hopefully get a CI
failure due to unexpected success in creating the `PlaybackStream`.

To ensure that we clean up our PulseAudio connection whenever audio
output is not needed, add `PulseAudioContext::weak_instance()` to allow
us to check whether an instance exists without creating one.
2023-08-07 10:40:34 -06:00

44 lines
1.2 KiB
C++

/*
* Copyright (c) 2023, Gregory Bertilson <zaggy1024@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Math.h>
#include <AK/MemoryStream.h>
#include <AK/WeakPtr.h>
#include <LibAudio/PlaybackStream.h>
#include <LibTest/TestSuite.h>
#include <unistd.h>
#if defined(HAVE_PULSEAUDIO)
# include <LibAudio/PulseAudioWrappers.h>
#endif
TEST_CASE(create_and_destroy_playback_stream)
{
bool has_implementation = false;
#if defined(HAVE_PULSEAUDIO)
has_implementation = true;
#endif
{
auto stream_result = Audio::PlaybackStream::create(Audio::OutputState::Playing, 44100, 2, 100, [](Bytes buffer, Audio::PcmSampleFormat format, size_t sample_count) -> ReadonlyBytes {
VERIFY(format == Audio::PcmSampleFormat::Float32);
FixedMemoryStream writing_stream { buffer };
for (size_t i = 0; i < sample_count; i++) {
MUST(writing_stream.write_value(0.0f));
MUST(writing_stream.write_value(0.0f));
}
return buffer.trim(writing_stream.offset());
});
EXPECT_EQ(!stream_result.is_error(), has_implementation);
usleep(10000);
}
#if defined(HAVE_PULSEAUDIO)
VERIFY(!Audio::PulseAudioContext::weak_instance());
#endif
}