ConnectionToServer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Concepts.h>
  9. #include <AK/FixedArray.h>
  10. #include <AK/NonnullOwnPtr.h>
  11. #include <AK/OwnPtr.h>
  12. #include <LibAudio/Queue.h>
  13. #include <LibAudio/UserSampleQueue.h>
  14. #include <LibCore/EventLoop.h>
  15. #include <LibCore/Object.h>
  16. #include <LibIPC/ConnectionToServer.h>
  17. #include <LibThreading/Mutex.h>
  18. #include <LibThreading/Thread.h>
  19. #include <Userland/Services/AudioServer/AudioClientEndpoint.h>
  20. #include <Userland/Services/AudioServer/AudioServerEndpoint.h>
  21. namespace Audio {
  22. class ConnectionToServer final
  23. : public IPC::ConnectionToServer<AudioClientEndpoint, AudioServerEndpoint>
  24. , public AudioClientEndpoint {
  25. IPC_CLIENT_CONNECTION(ConnectionToServer, "/tmp/session/%sid/portal/audio"sv)
  26. public:
  27. virtual ~ConnectionToServer() override;
  28. // Both of these APIs are for convenience and when you don't care about real-time behavior.
  29. // They will not work properly in conjunction with realtime_enqueue.
  30. // If you don't refill the buffer in time with this API, the last shared buffer write is zero-padded to play all of the samples.
  31. template<ArrayLike<Sample> Samples>
  32. ErrorOr<void> async_enqueue(Samples&& samples)
  33. {
  34. return async_enqueue(TRY(FixedArray<Sample>::create(samples.span())));
  35. }
  36. ErrorOr<void> async_enqueue(FixedArray<Sample>&& samples);
  37. void clear_client_buffer();
  38. // Returns immediately with the appropriate status if the buffer is full; use in conjunction with remaining_buffers to get low latency.
  39. ErrorOr<void, AudioQueue::QueueStatus> realtime_enqueue(Array<Sample, AUDIO_BUFFER_SIZE> samples);
  40. ErrorOr<void> blocking_realtime_enqueue(Array<Sample, AUDIO_BUFFER_SIZE> samples, Function<void()> wait_function);
  41. // This information can be deducted from the shared audio buffer.
  42. unsigned total_played_samples() const;
  43. // How many samples remain in m_enqueued_samples.
  44. unsigned remaining_samples();
  45. // How many buffers (i.e. short sample arrays) the server hasn't played yet.
  46. // Non-realtime code needn't worry about this.
  47. size_t remaining_buffers() const;
  48. virtual void die() override;
  49. Function<void(bool muted)> on_main_mix_muted_state_change;
  50. Function<void(double volume)> on_main_mix_volume_change;
  51. Function<void(double volume)> on_client_volume_change;
  52. private:
  53. ConnectionToServer(NonnullOwnPtr<Core::LocalSocket>);
  54. virtual void main_mix_muted_state_changed(bool) override;
  55. virtual void main_mix_volume_changed(double) override;
  56. virtual void client_volume_changed(double) override;
  57. // We use this to perform the audio enqueuing on the background thread's event loop
  58. virtual void custom_event(Core::CustomEvent&) override;
  59. // FIXME: This should be called every time the sample rate changes, but we just cautiously call it on every non-realtime enqueue.
  60. void update_good_sleep_time();
  61. // Shared audio buffer: both server and client constantly read and write to/from this.
  62. // This needn't be mutex protected: it's internally multi-threading aware.
  63. OwnPtr<AudioQueue> m_buffer;
  64. // The queue of non-realtime audio provided by the user.
  65. NonnullOwnPtr<UserSampleQueue> m_user_queue;
  66. NonnullRefPtr<Threading::Thread> m_background_audio_enqueuer;
  67. Core::EventLoop* m_enqueuer_loop { nullptr };
  68. Threading::Mutex m_enqueuer_loop_destruction;
  69. // A good amount of time to sleep when the queue is full.
  70. // (Only used for non-realtime enqueues)
  71. timespec m_good_sleep_time {};
  72. };
  73. }