TestPlaybackStream.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2023, Gregory Bertilson <zaggy1024@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Math.h>
  7. #include <AK/MemoryStream.h>
  8. #include <AK/WeakPtr.h>
  9. #include <LibAudio/PlaybackStream.h>
  10. #include <LibCore/EventLoop.h>
  11. #include <LibTest/TestSuite.h>
  12. #include <unistd.h>
  13. #if defined(HAVE_PULSEAUDIO)
  14. # include <LibAudio/PulseAudioWrappers.h>
  15. #endif
  16. // FIXME: CI doesn't run an AudioServer currently. Creating one in /etc/SystemServer.ini does not
  17. // allow this test to pass since CI runs in a Shell that will setsid() if it finds that the
  18. // current session ID is 0, and AudioServer's socket address depends on the current sid.
  19. // If we can fix that, this test can run on CI.
  20. // https://github.com/SerenityOS/serenity/issues/20538
  21. #define STREAM_TEST TEST_CASE
  22. STREAM_TEST(create_and_destroy_playback_stream)
  23. {
  24. Core::EventLoop event_loop;
  25. bool has_implementation = false;
  26. #if defined(HAVE_PULSEAUDIO) || defined(AK_OS_MACOS)
  27. has_implementation = true;
  28. #endif
  29. {
  30. auto stream_result = Audio::PlaybackStream::create(Audio::OutputState::Playing, 44100, 2, 100, [](Bytes buffer, Audio::PcmSampleFormat format, size_t sample_count) -> ReadonlyBytes {
  31. VERIFY(format == Audio::PcmSampleFormat::Float32);
  32. FixedMemoryStream writing_stream { buffer };
  33. for (size_t i = 0; i < sample_count; i++) {
  34. MUST(writing_stream.write_value(0.0f));
  35. MUST(writing_stream.write_value(0.0f));
  36. }
  37. return buffer.trim(writing_stream.offset());
  38. });
  39. EXPECT_EQ(!stream_result.is_error(), has_implementation);
  40. usleep(10000);
  41. }
  42. #if defined(HAVE_PULSEAUDIO)
  43. VERIFY(!Audio::PulseAudioContext::weak_instance());
  44. #endif
  45. }