Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/SharedBuffer.h>
28#include <LibAudio/Buffer.h>
29#include <LibAudio/ClientConnection.h>
30
31namespace Audio {
32
33ClientConnection::ClientConnection()
34 : IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>(*this, "/tmp/portal/audio")
35{
36}
37
38void ClientConnection::handshake()
39{
40 auto response = send_sync<Messages::AudioServer::Greet>();
41 set_my_client_id(response->client_id());
42}
43
44void ClientConnection::enqueue(const Buffer& buffer)
45{
46 for (;;) {
47 const_cast<Buffer&>(buffer).shared_buffer().share_with(server_pid());
48 auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
49 if (response->success())
50 break;
51 sleep(1);
52 }
53}
54
55bool ClientConnection::try_enqueue(const Buffer& buffer)
56{
57 const_cast<Buffer&>(buffer).shared_buffer().share_with(server_pid());
58 auto response = send_sync<Messages::AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
59 return response->success();
60}
61
62bool ClientConnection::get_muted()
63{
64 return send_sync<Messages::AudioServer::GetMuted>()->muted();
65}
66
67void ClientConnection::set_muted(bool muted)
68{
69 send_sync<Messages::AudioServer::SetMuted>(muted);
70}
71
72int ClientConnection::get_main_mix_volume()
73{
74 return send_sync<Messages::AudioServer::GetMainMixVolume>()->volume();
75}
76
77void ClientConnection::set_main_mix_volume(int volume)
78{
79 send_sync<Messages::AudioServer::SetMainMixVolume>(volume);
80}
81
82int ClientConnection::get_remaining_samples()
83{
84 return send_sync<Messages::AudioServer::GetRemainingSamples>()->remaining_samples();
85}
86
87int ClientConnection::get_played_samples()
88{
89 return send_sync<Messages::AudioServer::GetPlayedSamples>()->played_samples();
90}
91
92void ClientConnection::set_paused(bool paused)
93{
94 send_sync<Messages::AudioServer::SetPaused>(paused);
95}
96
97void ClientConnection::clear_buffer(bool paused)
98{
99 send_sync<Messages::AudioServer::ClearBuffer>(paused);
100}
101
102int ClientConnection::get_playing_buffer()
103{
104 return send_sync<Messages::AudioServer::GetPlayingBuffer>()->buffer_id();
105}
106
107void ClientConnection::handle(const Messages::AudioClient::FinishedPlayingBuffer& message)
108{
109 if (on_finish_playing_buffer)
110 on_finish_playing_buffer(message.buffer_id());
111}
112
113void ClientConnection::handle(const Messages::AudioClient::MutedStateChanged& message)
114{
115 if (on_muted_state_change)
116 on_muted_state_change(message.muted());
117}
118
119}