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/BufferStream.h>
28#include <AudioServer/ASClientConnection.h>
29#include <AudioServer/ASMixer.h>
30#include <limits>
31#include <pthread.h>
32
33ASMixer::ASMixer()
34 : m_device(Core::File::construct("/dev/audio", this))
35 , m_sound_thread(
36 [this] {
37 mix();
38 return 0;
39 },
40 "AudioServer[mixer]")
41{
42 if (!m_device->open(Core::IODevice::WriteOnly)) {
43 dbgprintf("Can't open audio device: %s\n", m_device->error_string());
44 return;
45 }
46
47 pthread_mutex_init(&m_pending_mutex, nullptr);
48 pthread_cond_init(&m_pending_cond, nullptr);
49
50 m_zero_filled_buffer = (u8*)malloc(4096);
51 bzero(m_zero_filled_buffer, 4096);
52 m_sound_thread.start();
53}
54
55ASMixer::~ASMixer()
56{
57}
58
59NonnullRefPtr<ASBufferQueue> ASMixer::create_queue(ASClientConnection& client)
60{
61 auto queue = adopt(*new ASBufferQueue(client));
62 pthread_mutex_lock(&m_pending_mutex);
63 m_pending_mixing.append(*queue);
64 pthread_cond_signal(&m_pending_cond);
65 pthread_mutex_unlock(&m_pending_mutex);
66 return queue;
67}
68
69void ASMixer::mix()
70{
71 decltype(m_pending_mixing) active_mix_queues;
72
73 for (;;) {
74 if (active_mix_queues.is_empty()) {
75 pthread_mutex_lock(&m_pending_mutex);
76 pthread_cond_wait(&m_pending_cond, &m_pending_mutex);
77 active_mix_queues.append(move(m_pending_mixing));
78 pthread_mutex_unlock(&m_pending_mutex);
79 }
80
81 active_mix_queues.remove_all_matching([&](auto& entry) { return !entry->client(); });
82
83 Audio::Sample mixed_buffer[1024];
84 auto mixed_buffer_length = (int)(sizeof(mixed_buffer) / sizeof(Audio::Sample));
85
86 // Mix the buffers together into the output
87 for (auto& queue : active_mix_queues) {
88 if (!queue->client()) {
89 queue->clear();
90 continue;
91 }
92
93 for (int i = 0; i < mixed_buffer_length; ++i) {
94 auto& mixed_sample = mixed_buffer[i];
95 Audio::Sample sample;
96 if (!queue->get_next_sample(sample))
97 break;
98 mixed_sample += sample;
99 }
100 }
101
102 bool muted = m_muted;
103
104 // output the mixed stuff to the device
105 u8 raw_buffer[4096];
106 auto buffer = ByteBuffer::wrap(muted ? m_zero_filled_buffer : raw_buffer, sizeof(raw_buffer));
107
108 BufferStream stream(buffer);
109 if (!muted) {
110 for (int i = 0; i < mixed_buffer_length; ++i) {
111 auto& mixed_sample = mixed_buffer[i];
112
113 mixed_sample.scale(m_main_volume);
114 mixed_sample.clip();
115
116 i16 out_sample;
117 out_sample = mixed_sample.left * std::numeric_limits<i16>::max();
118 stream << out_sample;
119
120 ASSERT(!stream.at_end()); // we should have enough space for both channels in one buffer!
121 out_sample = mixed_sample.right * std::numeric_limits<i16>::max();
122 stream << out_sample;
123 }
124 }
125
126 if (stream.offset() != 0) {
127 buffer.trim(stream.offset());
128 }
129 m_device->write(buffer);
130 }
131}
132
133void ASMixer::set_muted(bool muted)
134{
135 if (m_muted == muted)
136 return;
137 m_muted = muted;
138 ASClientConnection::for_each([muted](ASClientConnection& client) {
139 client.did_change_muted_state({}, muted);
140 });
141}
142
143ASBufferQueue::ASBufferQueue(ASClientConnection& client)
144 : m_client(client.make_weak_ptr())
145{
146}
147
148void ASBufferQueue::enqueue(NonnullRefPtr<Audio::Buffer>&& buffer)
149{
150 m_remaining_samples += buffer->sample_count();
151 m_queue.enqueue(move(buffer));
152}