ConnectionToServer.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/EventReceiver.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. // Whether there is room in the realtime audio queue for another sample buffer.
  49. bool can_enqueue() const;
  50. void set_self_sample_rate(u32 sample_rate);
  51. virtual void die() override;
  52. Function<void(double volume)> on_client_volume_change;
  53. private:
  54. ConnectionToServer(NonnullOwnPtr<Core::LocalSocket>);
  55. virtual void client_volume_changed(double) override;
  56. // We use this to perform the audio enqueuing on the background thread's event loop
  57. virtual void custom_event(Core::CustomEvent&) override;
  58. void update_good_sleep_time();
  59. // Shared audio buffer: both server and client constantly read and write to/from this.
  60. // This needn't be mutex protected: it's internally multi-threading aware.
  61. OwnPtr<AudioQueue> m_buffer;
  62. // The queue of non-realtime audio provided by the user.
  63. NonnullOwnPtr<UserSampleQueue> m_user_queue;
  64. NonnullRefPtr<Threading::Thread> m_background_audio_enqueuer;
  65. Core::EventLoop* m_enqueuer_loop { nullptr };
  66. Threading::Mutex m_enqueuer_loop_destruction;
  67. // A good amount of time to sleep when the queue is full.
  68. // (Only used for non-realtime enqueues)
  69. timespec m_good_sleep_time {};
  70. };
  71. }