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