blob: d26ade1a54cc0a384fcf853442e0a02af49773f7 [file] [log] [blame]
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "r_submix"
Jean-Michel Trivi35a2c162012-09-17 10:13:26 -070018//#define LOG_NDEBUG 0
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070019
20#include <errno.h>
21#include <pthread.h>
22#include <stdint.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070023#include <stdlib.h>
Stewart Milesc049a0a2014-05-01 09:03:27 -070024#include <sys/param.h>
25#include <sys/time.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070026
27#include <cutils/log.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070028#include <cutils/properties.h>
Stewart Milesc049a0a2014-05-01 09:03:27 -070029#include <cutils/str_parms.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070030
Stewart Milesc049a0a2014-05-01 09:03:27 -070031#include <hardware/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070032#include <hardware/hardware.h>
33#include <system/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070034
Stewart Milesc049a0a2014-05-01 09:03:27 -070035#include <media/AudioParameter.h>
36#include <media/AudioBufferProvider.h>
Jean-Michel Trivieec87702012-09-17 09:59:42 -070037#include <media/nbaio/MonoPipe.h>
38#include <media/nbaio/MonoPipeReader.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070039
Jean-Michel Trivid4413032012-09-30 11:08:06 -070040#include <utils/String8.h>
Jean-Michel Trivid4413032012-09-30 11:08:06 -070041
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070042extern "C" {
43
44namespace android {
45
Stewart Milesc049a0a2014-05-01 09:03:27 -070046// Set to 1 to enable extremely verbose logging in this module.
47#define SUBMIX_VERBOSE_LOGGING 0
48#if SUBMIX_VERBOSE_LOGGING
49#define SUBMIX_ALOGV(...) ALOGV(__VA_ARGS__)
50#define SUBMIX_ALOGE(...) ALOGE(__VA_ARGS__)
51#else
52#define SUBMIX_ALOGV(...)
53#define SUBMIX_ALOGE(...)
54#endif // SUBMIX_VERBOSE_LOGGING
55
Jean-Michel Trivieec87702012-09-17 09:59:42 -070056#define MAX_PIPE_DEPTH_IN_FRAMES (1024*8)
57// The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to
58// the duration of a record buffer at the current record sample rate (of the device, not of
59// the recording itself). Here we have:
60// 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -070061#define MAX_READ_ATTEMPTS 3
Jean-Michel Trivieec87702012-09-17 09:59:42 -070062#define READ_ATTEMPT_SLEEP_MS 5 // 5ms between two read attempts when pipe is empty
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070063#define DEFAULT_RATE_HZ 48000 // default sample rate
64
65struct submix_config {
66 audio_format_t format;
67 audio_channel_mask_t channel_mask;
68 unsigned int rate; // sample rate for the device
69 unsigned int period_size; // size of the audio pipe is period_size * period_count in frames
70 unsigned int period_count;
71};
72
73struct submix_audio_device {
74 struct audio_hw_device device;
Jean-Michel Trivieec87702012-09-17 09:59:42 -070075 bool output_standby;
76 bool input_standby;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070077 submix_config config;
78 // Pipe variables: they handle the ring buffer that "pipes" audio:
Jean-Michel Trivieec87702012-09-17 09:59:42 -070079 // - from the submix virtual audio output == what needs to be played
80 // remotely, seen as an output for AudioFlinger
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070081 // - to the virtual audio source == what is captured by the component
82 // which "records" the submix / virtual audio source, and handles it as needed.
Jean-Michel Trivieec87702012-09-17 09:59:42 -070083 // A usecase example is one where the component capturing the audio is then sending it over
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070084 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
85 // TV with Wifi Display capabilities), or to a wireless audio player.
Jean-Michel Trivieec87702012-09-17 09:59:42 -070086 sp<MonoPipe> rsxSink;
87 sp<MonoPipeReader> rsxSource;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070088
Jean-Michel Trivieec87702012-09-17 09:59:42 -070089 // device lock, also used to protect access to the audio pipe
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070090 pthread_mutex_t lock;
91};
92
93struct submix_stream_out {
94 struct audio_stream_out stream;
95 struct submix_audio_device *dev;
96};
97
98struct submix_stream_in {
99 struct audio_stream_in stream;
100 struct submix_audio_device *dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700101 bool output_standby; // output standby state as seen from record thread
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700102
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700103 // wall clock when recording starts
104 struct timespec record_start_time;
105 // how many frames have been requested to be read
106 int64_t read_counter_frames;
107};
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700108
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700109/* audio HAL functions */
110
111static uint32_t out_get_sample_rate(const struct audio_stream *stream)
112{
113 const struct submix_stream_out *out =
114 reinterpret_cast<const struct submix_stream_out *>(stream);
115 uint32_t out_rate = out->dev->config.rate;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700116 SUBMIX_ALOGV("out_get_sample_rate() returns %u", out_rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700117 return out_rate;
118}
119
120static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
121{
122 if ((rate != 44100) && (rate != 48000)) {
123 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
124 return -ENOSYS;
125 }
126 struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700127 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700128 out->dev->config.rate = rate;
129 return 0;
130}
131
132static size_t out_get_buffer_size(const struct audio_stream *stream)
133{
134 const struct submix_stream_out *out =
135 reinterpret_cast<const struct submix_stream_out *>(stream);
136 const struct submix_config& config_out = out->dev->config;
137 size_t buffer_size = config_out.period_size * popcount(config_out.channel_mask)
138 * sizeof(int16_t); // only PCM 16bit
Stewart Milesc049a0a2014-05-01 09:03:27 -0700139 SUBMIX_ALOGV("out_get_buffer_size() returns %u, period size=%u",
140 buffer_size, config_out.period_size);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700141 return buffer_size;
142}
143
144static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
145{
146 const struct submix_stream_out *out =
147 reinterpret_cast<const struct submix_stream_out *>(stream);
148 uint32_t channels = out->dev->config.channel_mask;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700149 SUBMIX_ALOGV("out_get_channels() returns %08x", channels);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700150 return channels;
151}
152
153static audio_format_t out_get_format(const struct audio_stream *stream)
154{
155 return AUDIO_FORMAT_PCM_16_BIT;
156}
157
158static int out_set_format(struct audio_stream *stream, audio_format_t format)
159{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700160 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700161 if (format != AUDIO_FORMAT_PCM_16_BIT) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700162 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700163 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700164 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700165 SUBMIX_ALOGV("out_set_format(format=%x)", format);
166 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700167}
168
169static int out_standby(struct audio_stream *stream)
170{
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700171 ALOGI("out_standby()");
172
173 const struct submix_stream_out *out = reinterpret_cast<const struct submix_stream_out *>(stream);
174
175 pthread_mutex_lock(&out->dev->lock);
176
177 out->dev->output_standby = true;
178
179 pthread_mutex_unlock(&out->dev->lock);
180
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700181 return 0;
182}
183
184static int out_dump(const struct audio_stream *stream, int fd)
185{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700186 (void)stream;
187 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700188 return 0;
189}
190
191static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
192{
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700193 int exiting = -1;
194 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700195 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700196 // FIXME this is using hard-coded strings but in the future, this functionality will be
197 // converted to use audio HAL extensions required to support tunneling
198 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
199 const struct submix_stream_out *out =
200 reinterpret_cast<const struct submix_stream_out *>(stream);
201
202 pthread_mutex_lock(&out->dev->lock);
203
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800204 { // using the sink
205 sp<MonoPipe> sink = out->dev->rsxSink.get();
206 if (sink == 0) {
207 pthread_mutex_unlock(&out->dev->lock);
208 return 0;
209 }
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700210
Stewart Milesc049a0a2014-05-01 09:03:27 -0700211 ALOGI("out_set_parameters(): shutdown");
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800212 sink->shutdown(true);
213 } // done using the sink
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700214
215 pthread_mutex_unlock(&out->dev->lock);
216 }
217
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700218 return 0;
219}
220
221static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
222{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700223 (void)stream;
224 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700225 return strdup("");
226}
227
228static uint32_t out_get_latency(const struct audio_stream_out *stream)
229{
230 const struct submix_stream_out *out =
231 reinterpret_cast<const struct submix_stream_out *>(stream);
232 const struct submix_config * config_out = &(out->dev->config);
233 uint32_t latency = (MAX_PIPE_DEPTH_IN_FRAMES * 1000) / config_out->rate;
234 ALOGV("out_get_latency() returns %u", latency);
235 return latency;
236}
237
238static int out_set_volume(struct audio_stream_out *stream, float left,
239 float right)
240{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700241 (void)stream;
242 (void)left;
243 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700244 return -ENOSYS;
245}
246
247static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
248 size_t bytes)
249{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700250 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700251 ssize_t written_frames = 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700252 struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream);
253
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700254 const size_t frame_size = audio_stream_frame_size(&stream->common);
255 const size_t frames = bytes / frame_size;
256
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700257 pthread_mutex_lock(&out->dev->lock);
258
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700259 out->dev->output_standby = false;
260
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800261 sp<MonoPipe> sink = out->dev->rsxSink.get();
262 if (sink != 0) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700263 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800264 sink.clear();
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700265 pthread_mutex_unlock(&out->dev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700266 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700267 // the pipe has already been shutdown, this buffer will be lost but we must
268 // simulate timing so we don't drain the output faster than realtime
269 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
270 return bytes;
271 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700272 } else {
273 pthread_mutex_unlock(&out->dev->lock);
274 ALOGE("out_write without a pipe!");
275 ALOG_ASSERT("out_write without a pipe!");
276 return 0;
277 }
278
279 pthread_mutex_unlock(&out->dev->lock);
280
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700281 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800282
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700283 if (written_frames < 0) {
284 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700285 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700286
287 pthread_mutex_lock(&out->dev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800288 sink.clear();
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700289 pthread_mutex_unlock(&out->dev->lock);
290
291 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700292 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700293 } else {
294 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700295 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700296 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700297 }
298 }
299
300 pthread_mutex_lock(&out->dev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800301 sink.clear();
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700302 pthread_mutex_unlock(&out->dev->lock);
303
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700304 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700305 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700306 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700307 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700308 const ssize_t written_bytes = written_frames * frame_size;
309 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames)", written_bytes, written_frames);
310 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700311}
312
313static int out_get_render_position(const struct audio_stream_out *stream,
314 uint32_t *dsp_frames)
315{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700316 (void)stream;
317 (void)dsp_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700318 return -EINVAL;
319}
320
321static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
322{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700323 (void)stream;
324 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700325 return 0;
326}
327
328static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
329{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700330 (void)stream;
331 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700332 return 0;
333}
334
335static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
336 int64_t *timestamp)
337{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700338 (void)stream;
339 (void)timestamp;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700340 return -EINVAL;
341}
342
343/** audio_stream_in implementation **/
344static uint32_t in_get_sample_rate(const struct audio_stream *stream)
345{
346 const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700347 SUBMIX_ALOGV("in_get_sample_rate() returns %u", in->dev->config.sample_rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700348 return in->dev->config.rate;
349}
350
351static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
352{
353 return -ENOSYS;
354}
355
356static size_t in_get_buffer_size(const struct audio_stream *stream)
357{
358 const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
Colin Cross5685a082014-04-18 15:45:42 -0700359 ALOGV("in_get_buffer_size() returns %zu",
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700360 in->dev->config.period_size * audio_stream_frame_size(stream));
361 return in->dev->config.period_size * audio_stream_frame_size(stream);
362}
363
364static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
365{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700366 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700367 return AUDIO_CHANNEL_IN_STEREO;
368}
369
370static audio_format_t in_get_format(const struct audio_stream *stream)
371{
372 return AUDIO_FORMAT_PCM_16_BIT;
373}
374
375static int in_set_format(struct audio_stream *stream, audio_format_t format)
376{
377 if (format != AUDIO_FORMAT_PCM_16_BIT) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700378 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700379 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700380 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700381 SUBMIX_ALOGV("in_set_format(format=%x)", format);
382 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700383}
384
385static int in_standby(struct audio_stream *stream)
386{
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700387 ALOGI("in_standby()");
388 const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
389
390 pthread_mutex_lock(&in->dev->lock);
391
392 in->dev->input_standby = true;
393
394 pthread_mutex_unlock(&in->dev->lock);
395
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700396 return 0;
397}
398
399static int in_dump(const struct audio_stream *stream, int fd)
400{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700401 (void)stream;
402 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700403 return 0;
404}
405
406static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
407{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700408 (void)stream;
409 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700410 return 0;
411}
412
413static char * in_get_parameters(const struct audio_stream *stream,
414 const char *keys)
415{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700416 (void)stream;
417 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700418 return strdup("");
419}
420
421static int in_set_gain(struct audio_stream_in *stream, float gain)
422{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700423 (void)stream;
424 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700425 return 0;
426}
427
428static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
429 size_t bytes)
430{
431 ssize_t frames_read = -1977;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700432 struct submix_stream_in *in = reinterpret_cast<struct submix_stream_in *>(stream);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700433 const size_t frame_size = audio_stream_frame_size(&stream->common);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700434 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700435
Stewart Milesc049a0a2014-05-01 09:03:27 -0700436 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
437
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700438 pthread_mutex_lock(&in->dev->lock);
439
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700440 const bool output_standby_transition = (in->output_standby != in->dev->output_standby);
441 in->output_standby = in->dev->output_standby;
442
443 if (in->dev->input_standby || output_standby_transition) {
444 in->dev->input_standby = false;
445 // keep track of when we exit input standby (== first read == start "real recording")
446 // or when we start recording silence, and reset projected time
447 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
448 if (rc == 0) {
449 in->read_counter_frames = 0;
450 }
451 }
452
453 in->read_counter_frames += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700454 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800455
456 {
457 // about to read from audio source
458 sp<MonoPipeReader> source = in->dev->rsxSource.get();
459 if (source == 0) {
460 ALOGE("no audio pipe yet we're trying to read!");
461 pthread_mutex_unlock(&in->dev->lock);
462 usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common));
463 memset(buffer, 0, bytes);
464 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700465 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800466
467 pthread_mutex_unlock(&in->dev->lock);
468
469 // read the data from the pipe (it's non blocking)
470 int attempts = 0;
471 char* buff = (char*)buffer;
472 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
473 attempts++;
474 frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS);
475 if (frames_read > 0) {
476 remaining_frames -= frames_read;
477 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700478 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
479 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800480 } else {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700481 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800482 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
483 }
484 }
485 // done using the source
486 pthread_mutex_lock(&in->dev->lock);
487 source.clear();
488 pthread_mutex_unlock(&in->dev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700489 }
490
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700491 if (remaining_frames > 0) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700492 SUBMIX_ALOGV(" remaining_frames = %zu", remaining_frames);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700493 memset(((char*)buffer)+ bytes - (remaining_frames * frame_size), 0,
494 remaining_frames * frame_size);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700495 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700496
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700497 // compute how much we need to sleep after reading the data by comparing the wall clock with
498 // the projected time at which we should return.
499 struct timespec time_after_read;// wall clock after reading from the pipe
500 struct timespec record_duration;// observed record duration
501 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
502 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
503 if (rc == 0) {
504 // for how long have we been recording?
505 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
506 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
507 if (record_duration.tv_nsec < 0) {
508 record_duration.tv_sec--;
509 record_duration.tv_nsec += 1000000000;
510 }
511
512 // read_counter_frames contains the number of frames that have been read since the beginning
513 // of recording (including this call): it's converted to usec and compared to how long we've
514 // been recording for, which gives us how long we must wait to sync the projected recording
515 // time, and the observed recording time
516 long projected_vs_observed_offset_us =
517 ((int64_t)(in->read_counter_frames
518 - (record_duration.tv_sec*sample_rate)))
519 * 1000000 / sample_rate
520 - (record_duration.tv_nsec / 1000);
521
Stewart Milesc049a0a2014-05-01 09:03:27 -0700522 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700523 record_duration.tv_sec, record_duration.tv_nsec/1000000,
524 projected_vs_observed_offset_us);
525 if (projected_vs_observed_offset_us > 0) {
526 usleep(projected_vs_observed_offset_us);
527 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700528 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700529
Stewart Milesc049a0a2014-05-01 09:03:27 -0700530 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700531 return bytes;
532
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700533}
534
535static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
536{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700537 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700538 return 0;
539}
540
541static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
542{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700543 (void)stream;
544 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700545 return 0;
546}
547
548static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
549{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700550 (void)stream;
551 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700552 return 0;
553}
554
555static int adev_open_output_stream(struct audio_hw_device *dev,
556 audio_io_handle_t handle,
557 audio_devices_t devices,
558 audio_output_flags_t flags,
559 struct audio_config *config,
560 struct audio_stream_out **stream_out)
561{
562 ALOGV("adev_open_output_stream()");
563 struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
564 struct submix_stream_out *out;
565 int ret;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700566 (void)handle;
567 (void)devices;
568 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700569
570 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
571 if (!out) {
572 ret = -ENOMEM;
573 goto err_open;
574 }
575
576 pthread_mutex_lock(&rsxadev->lock);
577
578 out->stream.common.get_sample_rate = out_get_sample_rate;
579 out->stream.common.set_sample_rate = out_set_sample_rate;
580 out->stream.common.get_buffer_size = out_get_buffer_size;
581 out->stream.common.get_channels = out_get_channels;
582 out->stream.common.get_format = out_get_format;
583 out->stream.common.set_format = out_set_format;
584 out->stream.common.standby = out_standby;
585 out->stream.common.dump = out_dump;
586 out->stream.common.set_parameters = out_set_parameters;
587 out->stream.common.get_parameters = out_get_parameters;
588 out->stream.common.add_audio_effect = out_add_audio_effect;
589 out->stream.common.remove_audio_effect = out_remove_audio_effect;
590 out->stream.get_latency = out_get_latency;
591 out->stream.set_volume = out_set_volume;
592 out->stream.write = out_write;
593 out->stream.get_render_position = out_get_render_position;
594 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
595
596 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
597 rsxadev->config.channel_mask = config->channel_mask;
598
Eric Laurent9595f7c2013-08-22 09:55:13 -0700599 if ((config->sample_rate != 48000) && (config->sample_rate != 44100)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700600 config->sample_rate = DEFAULT_RATE_HZ;
601 }
602 rsxadev->config.rate = config->sample_rate;
603
604 config->format = AUDIO_FORMAT_PCM_16_BIT;
605 rsxadev->config.format = config->format;
606
607 rsxadev->config.period_size = 1024;
608 rsxadev->config.period_count = 4;
609 out->dev = rsxadev;
610
611 *stream_out = &out->stream;
612
613 // initialize pipe
614 {
615 ALOGV(" initializing pipe");
Glenn Kasten0538a3e2014-03-07 08:26:16 -0800616 const NBAIO_Format format = Format_from_SR_C(config->sample_rate,
617 popcount(config->channel_mask), config->format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700618 const NBAIO_Format offers[1] = {format};
619 size_t numCounterOffers = 0;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700620 // creating a MonoPipe with optional blocking set to true.
621 MonoPipe* sink = new MonoPipe(MAX_PIPE_DEPTH_IN_FRAMES, format, true/*writeCanBlock*/);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700622 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
623 ALOG_ASSERT(index == 0);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700624 MonoPipeReader* source = new MonoPipeReader(sink);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700625 numCounterOffers = 0;
626 index = source->negotiate(offers, 1, NULL, numCounterOffers);
627 ALOG_ASSERT(index == 0);
628 rsxadev->rsxSink = sink;
629 rsxadev->rsxSource = source;
630 }
631
632 pthread_mutex_unlock(&rsxadev->lock);
633
634 return 0;
635
636err_open:
637 *stream_out = NULL;
638 return ret;
639}
640
641static void adev_close_output_stream(struct audio_hw_device *dev,
642 struct audio_stream_out *stream)
643{
644 ALOGV("adev_close_output_stream()");
645 struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
646
647 pthread_mutex_lock(&rsxadev->lock);
648
649 rsxadev->rsxSink.clear();
650 rsxadev->rsxSource.clear();
651 free(stream);
652
653 pthread_mutex_unlock(&rsxadev->lock);
654}
655
656static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
657{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700658 (void)dev;
659 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700660 return -ENOSYS;
661}
662
663static char * adev_get_parameters(const struct audio_hw_device *dev,
664 const char *keys)
665{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700666 (void)dev;
667 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700668 return strdup("");;
669}
670
671static int adev_init_check(const struct audio_hw_device *dev)
672{
673 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -0700674 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700675 return 0;
676}
677
678static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
679{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700680 (void)dev;
681 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700682 return -ENOSYS;
683}
684
685static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
686{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700687 (void)dev;
688 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700689 return -ENOSYS;
690}
691
692static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
693{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700694 (void)dev;
695 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700696 return -ENOSYS;
697}
698
699static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
700{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700701 (void)dev;
702 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700703 return -ENOSYS;
704}
705
706static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
707{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700708 (void)dev;
709 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700710 return -ENOSYS;
711}
712
713static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
714{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700715 (void)dev;
716 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700717 return 0;
718}
719
720static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
721{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700722 (void)dev;
723 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700724 return -ENOSYS;
725}
726
727static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
728{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700729 (void)dev;
730 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700731 return -ENOSYS;
732}
733
734static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
735 const struct audio_config *config)
736{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700737 (void)dev;
738 (void)config;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700739 //### TODO correlate this with pipe parameters
740 return 4096;
741}
742
743static int adev_open_input_stream(struct audio_hw_device *dev,
744 audio_io_handle_t handle,
745 audio_devices_t devices,
746 struct audio_config *config,
747 struct audio_stream_in **stream_in)
748{
749 ALOGI("adev_open_input_stream()");
750
751 struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
752 struct submix_stream_in *in;
753 int ret;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700754 (void)handle;
755 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700756
757 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
758 if (!in) {
759 ret = -ENOMEM;
760 goto err_open;
761 }
762
763 pthread_mutex_lock(&rsxadev->lock);
764
765 in->stream.common.get_sample_rate = in_get_sample_rate;
766 in->stream.common.set_sample_rate = in_set_sample_rate;
767 in->stream.common.get_buffer_size = in_get_buffer_size;
768 in->stream.common.get_channels = in_get_channels;
769 in->stream.common.get_format = in_get_format;
770 in->stream.common.set_format = in_set_format;
771 in->stream.common.standby = in_standby;
772 in->stream.common.dump = in_dump;
773 in->stream.common.set_parameters = in_set_parameters;
774 in->stream.common.get_parameters = in_get_parameters;
775 in->stream.common.add_audio_effect = in_add_audio_effect;
776 in->stream.common.remove_audio_effect = in_remove_audio_effect;
777 in->stream.set_gain = in_set_gain;
778 in->stream.read = in_read;
779 in->stream.get_input_frames_lost = in_get_input_frames_lost;
780
781 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
782 rsxadev->config.channel_mask = config->channel_mask;
783
Eric Laurent9595f7c2013-08-22 09:55:13 -0700784 if ((config->sample_rate != 48000) && (config->sample_rate != 44100)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700785 config->sample_rate = DEFAULT_RATE_HZ;
786 }
787 rsxadev->config.rate = config->sample_rate;
788
789 config->format = AUDIO_FORMAT_PCM_16_BIT;
790 rsxadev->config.format = config->format;
791
792 rsxadev->config.period_size = 1024;
793 rsxadev->config.period_count = 4;
794
795 *stream_in = &in->stream;
796
797 in->dev = rsxadev;
798
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700799 in->read_counter_frames = 0;
800 in->output_standby = rsxadev->output_standby;
801
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700802 pthread_mutex_unlock(&rsxadev->lock);
803
804 return 0;
805
806err_open:
807 *stream_in = NULL;
808 return ret;
809}
810
811static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -0700812 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700813{
814 ALOGV("adev_close_input_stream()");
815 struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
816
817 pthread_mutex_lock(&rsxadev->lock);
818
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700819 MonoPipe* sink = rsxadev->rsxSink.get();
820 if (sink != NULL) {
821 ALOGI("shutdown");
822 sink->shutdown(true);
823 }
824
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700825 free(stream);
826
827 pthread_mutex_unlock(&rsxadev->lock);
828}
829
830static int adev_dump(const audio_hw_device_t *device, int fd)
831{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700832 (void)device;
833 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700834 return 0;
835}
836
837static int adev_close(hw_device_t *device)
838{
839 ALOGI("adev_close()");
840 free(device);
841 return 0;
842}
843
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700844static int adev_open(const hw_module_t* module, const char* name,
845 hw_device_t** device)
846{
847 ALOGI("adev_open(name=%s)", name);
848 struct submix_audio_device *rsxadev;
849
850 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
851 return -EINVAL;
852
853 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
854 if (!rsxadev)
855 return -ENOMEM;
856
857 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -0700858 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700859 rsxadev->device.common.module = (struct hw_module_t *) module;
860 rsxadev->device.common.close = adev_close;
861
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700862 rsxadev->device.init_check = adev_init_check;
863 rsxadev->device.set_voice_volume = adev_set_voice_volume;
864 rsxadev->device.set_master_volume = adev_set_master_volume;
865 rsxadev->device.get_master_volume = adev_get_master_volume;
866 rsxadev->device.set_master_mute = adev_set_master_mute;
867 rsxadev->device.get_master_mute = adev_get_master_mute;
868 rsxadev->device.set_mode = adev_set_mode;
869 rsxadev->device.set_mic_mute = adev_set_mic_mute;
870 rsxadev->device.get_mic_mute = adev_get_mic_mute;
871 rsxadev->device.set_parameters = adev_set_parameters;
872 rsxadev->device.get_parameters = adev_get_parameters;
873 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
874 rsxadev->device.open_output_stream = adev_open_output_stream;
875 rsxadev->device.close_output_stream = adev_close_output_stream;
876 rsxadev->device.open_input_stream = adev_open_input_stream;
877 rsxadev->device.close_input_stream = adev_close_input_stream;
878 rsxadev->device.dump = adev_dump;
879
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700880 rsxadev->input_standby = true;
881 rsxadev->output_standby = true;
882
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700883 *device = &rsxadev->device.common;
884
885 return 0;
886}
887
888static struct hw_module_methods_t hal_module_methods = {
889 /* open */ adev_open,
890};
891
892struct audio_module HAL_MODULE_INFO_SYM = {
893 /* common */ {
894 /* tag */ HARDWARE_MODULE_TAG,
895 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
896 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
897 /* id */ AUDIO_HARDWARE_MODULE_ID,
898 /* name */ "Wifi Display audio HAL",
899 /* author */ "The Android Open Source Project",
900 /* methods */ &hal_module_methods,
901 /* dso */ NULL,
902 /* reserved */ { 0 },
903 },
904};
905
906} //namespace android
907
908} //extern "C"