-
Notifications
You must be signed in to change notification settings - Fork 4
/
pcm_oboe.cpp
379 lines (318 loc) · 14.6 KB
/
pcm_oboe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* SPDX-License-Identifier: MPL-2.0
* Copyright © 2024 Cassia Team (https://github.com/cassia-org)
*/
#include <alsa/asoundlib.h>
#include <alsa/pcm.h>
#include <alsa/pcm_external.h>
#include <alsa/pcm_ioplug.h>
#include <oboe/Oboe.h>
#include <initializer_list>
#include <memory>
#include <mutex>
/**
* @brief An ALSA PCM I/O plugin that uses Oboe for playing audio on Android.
* @note This currently only supports playback, capture is not supported.
* @note The default backend is currently OpenSL as AAudio is broken on some devices.
*/
class OboePcm {
private:
std::mutex mutex;
std::shared_ptr<oboe::AudioStream> stream;
constexpr static int64_t TimeoutNanoseconds{36000000000}; //!< An hour in nanoseconds, this is an arbitrarily long timeout that should never be reached.
static int Start(snd_pcm_ioplug_t* ext) {
auto* self{static_cast<OboePcm*>(ext->private_data)};
std::scoped_lock lock{self->mutex};
if (!self->stream)
return -EBADFD; // This should be checked by pcm_ioplug but we'll do it here too.
oboe::Result result{self->stream->requestStart()};
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to start stream: " << oboe::convertToText(result) << std::endl;
return -1;
}
return 0;
}
static int Stop(snd_pcm_ioplug_t* ext) {
auto* self{static_cast<OboePcm*>(ext->private_data)};
std::scoped_lock lock{self->mutex};
if (!self->stream)
return -EBADFD;
oboe::StreamState state{self->stream->getState()};
if (state == oboe::StreamState::Stopped || state == oboe::StreamState::Flushed)
return 0; // We don't need to do anything if the stream is already stopped.
oboe::Result result{self->stream->requestPause()};
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to pause stream: " << oboe::convertToText(result) << std::endl;
return -1;
}
state = self->stream->getState();
while (state != oboe::StreamState::Paused) {
// AAudio documentation states that requestFlush() is valid while the stream is Pausing.
// However, in practice it returns InvalidState, so we'll just wait for the stream to pause.
result = self->stream->waitForStateChange(state, &state, TimeoutNanoseconds);
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to wait for pause: " << oboe::convertToText(result) << std::endl;
return -1;
}
}
result = self->stream->requestFlush();
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to flush stream: " << oboe::convertToText(result) << std::endl;
return -1;
}
state = self->stream->getState();
while (state != oboe::StreamState::Flushed) {
result = self->stream->waitForStateChange(oboe::StreamState::Flushing, &state, TimeoutNanoseconds);
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to wait for flush: " << oboe::convertToText(result) << std::endl;
return -1;
}
}
return 0;
}
static snd_pcm_sframes_t Pointer(snd_pcm_ioplug_t* ext) {
auto* self{static_cast<OboePcm*>(ext->private_data)};
std::scoped_lock lock{self->mutex};
if (!self->stream)
return -EBADFD;
// Note: This function would return an error for any Xruns but we don't bother as Oboe automatically recovers from them.
int64_t framesWritten{self->stream->getFramesWritten()};
if (framesWritten < 0) {
std::cerr << "[ALSA Oboe] Failed to get frames written: " << framesWritten << std::endl;
return -1;
}
// We don't care about the device ring buffer position as Oboe handles writing samples to it.
// Instead, we just need to return the current position relative to the imaginary ALSA buffer size.
return framesWritten % ext->buffer_size;
}
static snd_pcm_sframes_t Transfer(snd_pcm_ioplug_t* ext, const snd_pcm_channel_area_t* areas, snd_pcm_uframes_t offset, snd_pcm_uframes_t size) {
auto* self{static_cast<OboePcm*>(ext->private_data)};
std::unique_lock lock{self->mutex};
if (!self->stream)
return -EBADFD;
if (size == 0)
return 0;
if (self->stream->getState() != oboe::StreamState::Started) {
// ALSA expects us to automatically start the stream if it's not started.
oboe::Result result{self->stream->requestStart()};
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to start stream from transfer: " << oboe::convertToText(result) << std::endl;
return -1;
}
}
auto& firstArea{areas[0]};
auto* address{reinterpret_cast<uint8_t*>(firstArea.addr)};
#ifndef NDEBUG
uint channelOffset{0};
for (unsigned int c{0}; c < ext->channels; ++c) {
auto& area{areas[c]};
if (area.addr != firstArea.addr || area.step != firstArea.step || area.first >= firstArea.step) {
std::cerr << "[ALSA Oboe] Attempt to transfer non-interleaved samples" << std::endl;
return -1;
}
}
#endif
oboe::ResultWithValue<int32_t> result{self->stream->write(address, size, ext->nonblock ? 0 : TimeoutNanoseconds)};
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to write samples to stream: " << oboe::convertToText(result.error()) << std::endl;
return -1;
} else if (result.value() == 0) {
if (!ext->nonblock)
std::cerr << "[ALSA Oboe] Cannot write samples in blocking mode" << std::endl;
return -EAGAIN; // Oboe will return 0 if the stream is non-blocking and there's no space in the buffer.
}
return result.value();
}
static int Close(snd_pcm_ioplug_t* ext) {
if (ext->private_data) {
ext->private_data = nullptr;
auto* self{static_cast<OboePcm*>(ext->private_data)};
delete self;
}
return 0;
}
static int Prepare(snd_pcm_ioplug_t* ext) {
auto* self{static_cast<OboePcm*>(ext->private_data)};
std::scoped_lock lock{self->mutex};
if (self->stream)
return 0;
oboe::AudioStreamBuilder builder;
builder.setUsage(oboe::Usage::Game)
->setDirection(oboe::Direction::Output)
// Note: There is some instability related to using LowLatency mode on certain devices.
// Notably, while running mono 16-bit 48kHz audio on certain QCOM devices, the HAL simply raises a SIGABRT with no logs.
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setSharingMode(oboe::SharingMode::Shared)
->setFormat([fmt = ext->format]() {
switch (fmt) {
case SND_PCM_FORMAT_S16_LE:
return oboe::AudioFormat::I16;
case SND_PCM_FORMAT_FLOAT_LE:
return oboe::AudioFormat::Float;
case SND_PCM_FORMAT_S24_3LE:
return oboe::AudioFormat::I24;
case SND_PCM_FORMAT_S32_LE:
return oboe::AudioFormat::I32;
default:
return oboe::AudioFormat::Invalid;
}
}())
->setFormatConversionAllowed(true)
->setChannelCount(ext->channels)
->setChannelConversionAllowed(true)
->setSampleRate(ext->rate)
->setSampleRateConversionQuality(oboe::SampleRateConversionQuality::Medium)
->setBufferCapacityInFrames(ext->buffer_size)
->setAudioApi(oboe::AudioApi::OpenSLES);
oboe::Result result{builder.openStream(self->stream)};
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to open stream: " << oboe::convertToText(result) << std::endl;
return -1;
}
if (self->stream->getBufferCapacityInFrames() < ext->buffer_size) {
// Note: This should never happen with AAudio, but it's possible with OpenSL ES.
std::cerr << "[ALSA Oboe] Buffer size smaller than requested: " << self->stream->getBufferCapacityInFrames() << " < " << ext->buffer_size << std::endl;
self->stream.reset();
return -EIO;
}
return 0;
}
static int Drain(snd_pcm_ioplug_t* ext) {
auto self{static_cast<OboePcm*>(ext->private_data)};
std::scoped_lock lock{self->mutex};
if (!self->stream)
return -EBADFD;
// We need to wait for the stream to read all samples during a drain.
// According to AAudio documentation, requestStop() guarantees that the stream's contents have been written to the device.
// However, in practice it doesn't seem to be the case, so we'll just poll the frames read until it reaches the frames written.
struct timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
start.tv_sec += 1;
while (true) {
int64_t framesRead{self->stream->getFramesRead()};
if (framesRead < 0) {
std::cerr << "[ALSA Oboe] Failed to get frames read: " << framesRead << std::endl;
return -1;
}
int64_t framesWritten{self->stream->getFramesWritten()};
if (framesWritten < 0) {
std::cerr << "[ALSA Oboe] Failed to get frames written: " << framesWritten << std::endl;
return -1;
}
if (framesRead == framesWritten)
break;
usleep(1000);
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if ((now.tv_sec > start.tv_sec || (now.tv_sec == start.tv_sec && now.tv_nsec > start.tv_nsec)) && framesRead == 0) {
// AAudio has a bug where it won't read any samples until an arbitrary minimum amount of samples have been written.
// We just wait for a second and if no samples have been read, we'll assume that AAudio is broken.
break;
}
}
oboe::Result result{self->stream->requestStop()};
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to stop stream: " << oboe::convertToText(result) << std::endl;
return -1;
}
oboe::StreamState state{self->stream->getState()};
while (state != oboe::StreamState::Stopped) {
result = self->stream->waitForStateChange(state, &state, TimeoutNanoseconds);
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to wait for stop: " << oboe::convertToText(result) << std::endl;
return -1;
}
}
return 0;
}
static int Pause(snd_pcm_ioplug_t* ext, int enable) {
auto self{static_cast<OboePcm*>(ext->private_data)};
std::scoped_lock lock{self->mutex};
if (!self->stream)
return -EBADFD;
oboe::Result result{self->stream->requestPause()};
if (result != oboe::Result::OK) {
std::cerr << "[ALSA Oboe] Failed to pause stream: " << oboe::convertToText(result) << std::endl;
return -1;
}
return 0;
}
constexpr static snd_pcm_ioplug_callback_t Callbacks{
.start = &Start,
.stop = &Stop,
.pointer = &Pointer,
.transfer = &Transfer,
.close = &Close,
.prepare = &Prepare,
.drain = &Drain,
.pause = &Pause,
.resume = &Start,
};
public:
snd_pcm_ioplug_t plug{
.version = SND_PCM_IOPLUG_VERSION,
.name = "ALSA <-> Oboe PCM I/O Plugin",
.mmap_rw = false,
.callback = &Callbacks,
.private_data = this,
};
OboePcm() = default;
int Initialize(const char* name, snd_pcm_stream_t stream, int mode) {
if (stream != SND_PCM_STREAM_PLAYBACK)
return -EINVAL; // We only support playback for now.
int err{snd_pcm_ioplug_create(&plug, name, stream, mode)};
if (err < 0)
return err;
auto setParamList{[io = &plug](int type, std::initializer_list<unsigned int> list) {
return snd_pcm_ioplug_set_param_list(io, type, list.size(), list.begin());
}};
err = setParamList(SND_PCM_IOPLUG_HW_ACCESS, {SND_PCM_ACCESS_RW_INTERLEAVED});
if (err < 0)
return err;
err = setParamList(SND_PCM_IOPLUG_HW_FORMAT, {
SND_PCM_FORMAT_S16_LE,
SND_PCM_FORMAT_FLOAT_LE,
SND_PCM_FORMAT_S24_3LE,
SND_PCM_FORMAT_S32_LE,
});
if (err < 0)
return err;
// We could support more than 2 channels, but it's fairly complicated due to channel mappings.
err = snd_pcm_ioplug_set_param_minmax(&plug, SND_PCM_IOPLUG_HW_CHANNELS, 1, 2);
if (err < 0)
return err;
// Oboe supports any sample rate with sample rate conversion, but we'll limit it to a reasonable range (8kHz - 192kHz).
err = snd_pcm_ioplug_set_param_minmax(&plug, SND_PCM_IOPLUG_HW_RATE, 8000, 192000);
if (err < 0)
return err;
// Oboe will decide the period/buffer size internally after starting the stream and it's not a detail that we can expose properly.
// We set arbitrary values that should be reasonable for most use cases.
err = snd_pcm_ioplug_set_param_minmax(&plug, SND_PCM_IOPLUG_HW_PERIODS, 2, 4);
if (err < 0)
return err;
err = snd_pcm_ioplug_set_param_minmax(&plug, SND_PCM_IOPLUG_HW_BUFFER_BYTES, 32 * 1024, 64 * 1024);
if (err < 0)
return err;
return 0;
}
~OboePcm() {
std::scoped_lock lock{mutex};
stream.reset();
}
};
extern "C" {
SND_PCM_PLUGIN_DEFINE_FUNC(oboe) {
// Note: We don't need to do anything with the config, so we can just ignore it.
OboePcm* plugin{new (std::nothrow) OboePcm{}};
if (!plugin)
return -ENOMEM;
int err{plugin->Initialize(name, stream, mode)};
if (err < 0) {
delete plugin;
return err;
}
*pcmp = plugin->plug.pcm;
return 0;
}
SND_PCM_PLUGIN_SYMBOL(oboe);
}