blob: bc60484fba82d90937ed66e661692e8971748d0a [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
Stewart Milesf645c5e2014-05-01 09:03:27 -0700109// Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the
110// structure.
111static struct submix_stream_out * audio_stream_out_get_submix_stream_out(
112 struct audio_stream_out * const stream)
113{
114 ALOG_ASSERT(stream);
115 return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) -
116 offsetof(struct submix_stream_out, stream));
117}
118
119// Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure.
120static struct submix_stream_out * audio_stream_get_submix_stream_out(
121 struct audio_stream * const stream)
122{
123 ALOG_ASSERT(stream);
124 return audio_stream_out_get_submix_stream_out(
125 reinterpret_cast<struct audio_stream_out *>(stream));
126}
127
128// Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the
129// structure.
130static struct submix_stream_in * audio_stream_in_get_submix_stream_in(
131 struct audio_stream_in * const stream)
132{
133 ALOG_ASSERT(stream);
134 return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) -
135 offsetof(struct submix_stream_in, stream));
136}
137
138// Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure.
139static struct submix_stream_in * audio_stream_get_submix_stream_in(
140 struct audio_stream * const stream)
141{
142 ALOG_ASSERT(stream);
143 return audio_stream_in_get_submix_stream_in(
144 reinterpret_cast<struct audio_stream_in *>(stream));
145}
146
147// Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within
148// the structure.
149static struct submix_audio_device * audio_hw_device_get_submix_audio_device(
150 struct audio_hw_device *device)
151{
152 ALOG_ASSERT(device);
153 return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) -
154 offsetof(struct submix_audio_device, device));
155}
156
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700157/* audio HAL functions */
158
159static uint32_t out_get_sample_rate(const struct audio_stream *stream)
160{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700161 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
162 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700163 uint32_t out_rate = out->dev->config.rate;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700164 SUBMIX_ALOGV("out_get_sample_rate() returns %u", out_rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700165 return out_rate;
166}
167
168static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
169{
170 if ((rate != 44100) && (rate != 48000)) {
171 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
172 return -ENOSYS;
173 }
Stewart Milesf645c5e2014-05-01 09:03:27 -0700174 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700175 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700176 out->dev->config.rate = rate;
177 return 0;
178}
179
180static size_t out_get_buffer_size(const struct audio_stream *stream)
181{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700182 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
183 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700184 const struct submix_config& config_out = out->dev->config;
185 size_t buffer_size = config_out.period_size * popcount(config_out.channel_mask)
186 * sizeof(int16_t); // only PCM 16bit
Stewart Milesc049a0a2014-05-01 09:03:27 -0700187 SUBMIX_ALOGV("out_get_buffer_size() returns %u, period size=%u",
188 buffer_size, config_out.period_size);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700189 return buffer_size;
190}
191
192static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
193{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700194 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
195 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700196 uint32_t channels = out->dev->config.channel_mask;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700197 SUBMIX_ALOGV("out_get_channels() returns %08x", channels);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700198 return channels;
199}
200
201static audio_format_t out_get_format(const struct audio_stream *stream)
202{
203 return AUDIO_FORMAT_PCM_16_BIT;
204}
205
206static int out_set_format(struct audio_stream *stream, audio_format_t format)
207{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700208 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700209 if (format != AUDIO_FORMAT_PCM_16_BIT) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700210 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700211 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700212 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700213 SUBMIX_ALOGV("out_set_format(format=%x)", format);
214 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700215}
216
217static int out_standby(struct audio_stream *stream)
218{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700219 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_out(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700220 ALOGI("out_standby()");
221
Stewart Milesf645c5e2014-05-01 09:03:27 -0700222 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700223
Stewart Milesf645c5e2014-05-01 09:03:27 -0700224 rsxadev->output_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700225
Stewart Milesf645c5e2014-05-01 09:03:27 -0700226 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700227
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700228 return 0;
229}
230
231static int out_dump(const struct audio_stream *stream, int fd)
232{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700233 (void)stream;
234 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700235 return 0;
236}
237
238static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
239{
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700240 int exiting = -1;
241 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700242 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700243 // FIXME this is using hard-coded strings but in the future, this functionality will be
244 // converted to use audio HAL extensions required to support tunneling
245 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700246 struct submix_audio_device * const rsxadev =
247 audio_stream_get_submix_stream_out(stream)->dev;
248 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800249 { // using the sink
Stewart Milesf645c5e2014-05-01 09:03:27 -0700250 sp<MonoPipe> sink = rsxadev->rsxSink.get();
251 if (sink == NULL) {
252 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800253 return 0;
254 }
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700255
Stewart Milesc049a0a2014-05-01 09:03:27 -0700256 ALOGI("out_set_parameters(): shutdown");
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800257 sink->shutdown(true);
258 } // done using the sink
Stewart Milesf645c5e2014-05-01 09:03:27 -0700259 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700260 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700261 return 0;
262}
263
264static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
265{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700266 (void)stream;
267 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700268 return strdup("");
269}
270
271static uint32_t out_get_latency(const struct audio_stream_out *stream)
272{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700273 const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
274 const_cast<struct audio_stream_out *>(stream));
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700275 const struct submix_config * config_out = &(out->dev->config);
276 uint32_t latency = (MAX_PIPE_DEPTH_IN_FRAMES * 1000) / config_out->rate;
277 ALOGV("out_get_latency() returns %u", latency);
278 return latency;
279}
280
281static int out_set_volume(struct audio_stream_out *stream, float left,
282 float right)
283{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700284 (void)stream;
285 (void)left;
286 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700287 return -ENOSYS;
288}
289
290static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
291 size_t bytes)
292{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700293 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700294 ssize_t written_frames = 0;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700295 const size_t frame_size = audio_stream_frame_size(&stream->common);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700296 struct submix_audio_device * const rsxadev =
297 audio_stream_out_get_submix_stream_out(stream)->dev;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700298 const size_t frames = bytes / frame_size;
299
Stewart Milesf645c5e2014-05-01 09:03:27 -0700300 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700301
Stewart Milesf645c5e2014-05-01 09:03:27 -0700302 rsxadev->output_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700303
Stewart Milesf645c5e2014-05-01 09:03:27 -0700304 sp<MonoPipe> sink = rsxadev->rsxSink.get();
305 if (sink != NULL) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700306 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800307 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700308 pthread_mutex_unlock(&rsxadev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700309 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700310 // the pipe has already been shutdown, this buffer will be lost but we must
311 // simulate timing so we don't drain the output faster than realtime
312 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
313 return bytes;
314 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700315 } else {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700316 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700317 ALOGE("out_write without a pipe!");
318 ALOG_ASSERT("out_write without a pipe!");
319 return 0;
320 }
321
Stewart Milesf645c5e2014-05-01 09:03:27 -0700322 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700323
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700324 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800325
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700326 if (written_frames < 0) {
327 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700328 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700329
Stewart Milesf645c5e2014-05-01 09:03:27 -0700330 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800331 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700332 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700333
334 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700335 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700336 } else {
337 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700338 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700339 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700340 }
341 }
342
Stewart Milesf645c5e2014-05-01 09:03:27 -0700343 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800344 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700345 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700346
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700347 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700348 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700349 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700350 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700351 const ssize_t written_bytes = written_frames * frame_size;
352 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames)", written_bytes, written_frames);
353 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700354}
355
356static int out_get_render_position(const struct audio_stream_out *stream,
357 uint32_t *dsp_frames)
358{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700359 (void)stream;
360 (void)dsp_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700361 return -EINVAL;
362}
363
364static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
365{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700366 (void)stream;
367 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700368 return 0;
369}
370
371static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
372{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700373 (void)stream;
374 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700375 return 0;
376}
377
378static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
379 int64_t *timestamp)
380{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700381 (void)stream;
382 (void)timestamp;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700383 return -EINVAL;
384}
385
386/** audio_stream_in implementation **/
387static uint32_t in_get_sample_rate(const struct audio_stream *stream)
388{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700389 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
390 const_cast<struct audio_stream*>(stream));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700391 SUBMIX_ALOGV("in_get_sample_rate() returns %u", in->dev->config.sample_rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700392 return in->dev->config.rate;
393}
394
395static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
396{
397 return -ENOSYS;
398}
399
400static size_t in_get_buffer_size(const struct audio_stream *stream)
401{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700402 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
403 const_cast<struct audio_stream*>(stream));
Colin Cross5685a082014-04-18 15:45:42 -0700404 ALOGV("in_get_buffer_size() returns %zu",
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700405 in->dev->config.period_size * audio_stream_frame_size(stream));
406 return in->dev->config.period_size * audio_stream_frame_size(stream);
407}
408
409static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
410{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700411 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700412 return AUDIO_CHANNEL_IN_STEREO;
413}
414
415static audio_format_t in_get_format(const struct audio_stream *stream)
416{
417 return AUDIO_FORMAT_PCM_16_BIT;
418}
419
420static int in_set_format(struct audio_stream *stream, audio_format_t format)
421{
422 if (format != AUDIO_FORMAT_PCM_16_BIT) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700423 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700424 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700425 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700426 SUBMIX_ALOGV("in_set_format(format=%x)", format);
427 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700428}
429
430static int in_standby(struct audio_stream *stream)
431{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700432 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_in(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700433 ALOGI("in_standby()");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700434
Stewart Milesf645c5e2014-05-01 09:03:27 -0700435 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700436
Stewart Milesf645c5e2014-05-01 09:03:27 -0700437 rsxadev->input_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700438
Stewart Milesf645c5e2014-05-01 09:03:27 -0700439 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700440
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700441 return 0;
442}
443
444static int in_dump(const struct audio_stream *stream, int fd)
445{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700446 (void)stream;
447 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700448 return 0;
449}
450
451static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
452{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700453 (void)stream;
454 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700455 return 0;
456}
457
458static char * in_get_parameters(const struct audio_stream *stream,
459 const char *keys)
460{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700461 (void)stream;
462 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700463 return strdup("");
464}
465
466static int in_set_gain(struct audio_stream_in *stream, float gain)
467{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700468 (void)stream;
469 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700470 return 0;
471}
472
473static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
474 size_t bytes)
475{
476 ssize_t frames_read = -1977;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700477 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
478 struct submix_audio_device * const rsxadev = in->dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700479 const size_t frame_size = audio_stream_frame_size(&stream->common);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700480 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700481
Stewart Milesc049a0a2014-05-01 09:03:27 -0700482 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700483 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700484
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700485 const bool output_standby_transition = (in->output_standby != in->dev->output_standby);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700486 in->output_standby = rsxadev->output_standby;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700487
Stewart Milesf645c5e2014-05-01 09:03:27 -0700488 if (rsxadev->input_standby || output_standby_transition) {
489 rsxadev->input_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700490 // keep track of when we exit input standby (== first read == start "real recording")
491 // or when we start recording silence, and reset projected time
492 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
493 if (rc == 0) {
494 in->read_counter_frames = 0;
495 }
496 }
497
498 in->read_counter_frames += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700499 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800500
501 {
502 // about to read from audio source
Stewart Milesf645c5e2014-05-01 09:03:27 -0700503 sp<MonoPipeReader> source = rsxadev->rsxSource;
504 if (source == NULL) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800505 ALOGE("no audio pipe yet we're trying to read!");
Stewart Milesf645c5e2014-05-01 09:03:27 -0700506 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800507 usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common));
508 memset(buffer, 0, bytes);
509 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700510 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800511
Stewart Milesf645c5e2014-05-01 09:03:27 -0700512 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800513
514 // read the data from the pipe (it's non blocking)
515 int attempts = 0;
516 char* buff = (char*)buffer;
517 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
518 attempts++;
519 frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS);
520 if (frames_read > 0) {
521 remaining_frames -= frames_read;
522 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700523 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
524 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800525 } else {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700526 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800527 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
528 }
529 }
530 // done using the source
Stewart Milesf645c5e2014-05-01 09:03:27 -0700531 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800532 source.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700533 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700534 }
535
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700536 if (remaining_frames > 0) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700537 SUBMIX_ALOGV(" remaining_frames = %zu", remaining_frames);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700538 memset(((char*)buffer)+ bytes - (remaining_frames * frame_size), 0,
539 remaining_frames * frame_size);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700540 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700541
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700542 // compute how much we need to sleep after reading the data by comparing the wall clock with
543 // the projected time at which we should return.
544 struct timespec time_after_read;// wall clock after reading from the pipe
545 struct timespec record_duration;// observed record duration
546 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
547 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
548 if (rc == 0) {
549 // for how long have we been recording?
550 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
551 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
552 if (record_duration.tv_nsec < 0) {
553 record_duration.tv_sec--;
554 record_duration.tv_nsec += 1000000000;
555 }
556
Stewart Milesf645c5e2014-05-01 09:03:27 -0700557 // read_counter_frames contains the number of frames that have been read since the
558 // beginning of recording (including this call): it's converted to usec and compared to
559 // how long we've been recording for, which gives us how long we must wait to sync the
560 // projected recording time, and the observed recording time.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700561 long projected_vs_observed_offset_us =
562 ((int64_t)(in->read_counter_frames
563 - (record_duration.tv_sec*sample_rate)))
564 * 1000000 / sample_rate
565 - (record_duration.tv_nsec / 1000);
566
Stewart Milesc049a0a2014-05-01 09:03:27 -0700567 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700568 record_duration.tv_sec, record_duration.tv_nsec/1000000,
569 projected_vs_observed_offset_us);
570 if (projected_vs_observed_offset_us > 0) {
571 usleep(projected_vs_observed_offset_us);
572 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700573 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700574
Stewart Milesc049a0a2014-05-01 09:03:27 -0700575 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700576 return bytes;
577
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700578}
579
580static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
581{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700582 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700583 return 0;
584}
585
586static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
587{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700588 (void)stream;
589 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700590 return 0;
591}
592
593static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
594{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700595 (void)stream;
596 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700597 return 0;
598}
599
600static int adev_open_output_stream(struct audio_hw_device *dev,
601 audio_io_handle_t handle,
602 audio_devices_t devices,
603 audio_output_flags_t flags,
604 struct audio_config *config,
605 struct audio_stream_out **stream_out)
606{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700607 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700608 ALOGV("adev_open_output_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700609 struct submix_stream_out *out;
610 int ret;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700611 (void)handle;
612 (void)devices;
613 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700614
615 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
616 if (!out) {
617 ret = -ENOMEM;
618 goto err_open;
619 }
620
621 pthread_mutex_lock(&rsxadev->lock);
622
623 out->stream.common.get_sample_rate = out_get_sample_rate;
624 out->stream.common.set_sample_rate = out_set_sample_rate;
625 out->stream.common.get_buffer_size = out_get_buffer_size;
626 out->stream.common.get_channels = out_get_channels;
627 out->stream.common.get_format = out_get_format;
628 out->stream.common.set_format = out_set_format;
629 out->stream.common.standby = out_standby;
630 out->stream.common.dump = out_dump;
631 out->stream.common.set_parameters = out_set_parameters;
632 out->stream.common.get_parameters = out_get_parameters;
633 out->stream.common.add_audio_effect = out_add_audio_effect;
634 out->stream.common.remove_audio_effect = out_remove_audio_effect;
635 out->stream.get_latency = out_get_latency;
636 out->stream.set_volume = out_set_volume;
637 out->stream.write = out_write;
638 out->stream.get_render_position = out_get_render_position;
639 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
640
641 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
642 rsxadev->config.channel_mask = config->channel_mask;
643
Eric Laurent9595f7c2013-08-22 09:55:13 -0700644 if ((config->sample_rate != 48000) && (config->sample_rate != 44100)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700645 config->sample_rate = DEFAULT_RATE_HZ;
646 }
647 rsxadev->config.rate = config->sample_rate;
648
649 config->format = AUDIO_FORMAT_PCM_16_BIT;
650 rsxadev->config.format = config->format;
651
652 rsxadev->config.period_size = 1024;
653 rsxadev->config.period_count = 4;
654 out->dev = rsxadev;
655
656 *stream_out = &out->stream;
657
658 // initialize pipe
659 {
660 ALOGV(" initializing pipe");
Glenn Kasten0538a3e2014-03-07 08:26:16 -0800661 const NBAIO_Format format = Format_from_SR_C(config->sample_rate,
662 popcount(config->channel_mask), config->format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700663 const NBAIO_Format offers[1] = {format};
664 size_t numCounterOffers = 0;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700665 // creating a MonoPipe with optional blocking set to true.
666 MonoPipe* sink = new MonoPipe(MAX_PIPE_DEPTH_IN_FRAMES, format, true/*writeCanBlock*/);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700667 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
668 ALOG_ASSERT(index == 0);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700669 MonoPipeReader* source = new MonoPipeReader(sink);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700670 numCounterOffers = 0;
671 index = source->negotiate(offers, 1, NULL, numCounterOffers);
672 ALOG_ASSERT(index == 0);
673 rsxadev->rsxSink = sink;
674 rsxadev->rsxSource = source;
675 }
676
677 pthread_mutex_unlock(&rsxadev->lock);
678
679 return 0;
680
681err_open:
682 *stream_out = NULL;
683 return ret;
684}
685
686static void adev_close_output_stream(struct audio_hw_device *dev,
687 struct audio_stream_out *stream)
688{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700689 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700690 ALOGV("adev_close_output_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700691
692 pthread_mutex_lock(&rsxadev->lock);
693
694 rsxadev->rsxSink.clear();
695 rsxadev->rsxSource.clear();
696 free(stream);
697
698 pthread_mutex_unlock(&rsxadev->lock);
699}
700
701static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
702{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700703 (void)dev;
704 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700705 return -ENOSYS;
706}
707
708static char * adev_get_parameters(const struct audio_hw_device *dev,
709 const char *keys)
710{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700711 (void)dev;
712 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700713 return strdup("");;
714}
715
716static int adev_init_check(const struct audio_hw_device *dev)
717{
718 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -0700719 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700720 return 0;
721}
722
723static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
724{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700725 (void)dev;
726 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700727 return -ENOSYS;
728}
729
730static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
731{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700732 (void)dev;
733 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700734 return -ENOSYS;
735}
736
737static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
738{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700739 (void)dev;
740 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700741 return -ENOSYS;
742}
743
744static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
745{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700746 (void)dev;
747 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700748 return -ENOSYS;
749}
750
751static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
752{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700753 (void)dev;
754 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700755 return -ENOSYS;
756}
757
758static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
759{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700760 (void)dev;
761 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700762 return 0;
763}
764
765static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
766{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700767 (void)dev;
768 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700769 return -ENOSYS;
770}
771
772static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
773{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700774 (void)dev;
775 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700776 return -ENOSYS;
777}
778
779static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
780 const struct audio_config *config)
781{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700782 (void)dev;
783 (void)config;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700784 //### TODO correlate this with pipe parameters
785 return 4096;
786}
787
788static int adev_open_input_stream(struct audio_hw_device *dev,
789 audio_io_handle_t handle,
790 audio_devices_t devices,
791 struct audio_config *config,
792 struct audio_stream_in **stream_in)
793{
794 ALOGI("adev_open_input_stream()");
Stewart Milesf645c5e2014-05-01 09:03:27 -0700795 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700796 struct submix_stream_in *in;
797 int ret;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700798 (void)handle;
799 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700800
801 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
802 if (!in) {
803 ret = -ENOMEM;
804 goto err_open;
805 }
806
807 pthread_mutex_lock(&rsxadev->lock);
808
809 in->stream.common.get_sample_rate = in_get_sample_rate;
810 in->stream.common.set_sample_rate = in_set_sample_rate;
811 in->stream.common.get_buffer_size = in_get_buffer_size;
812 in->stream.common.get_channels = in_get_channels;
813 in->stream.common.get_format = in_get_format;
814 in->stream.common.set_format = in_set_format;
815 in->stream.common.standby = in_standby;
816 in->stream.common.dump = in_dump;
817 in->stream.common.set_parameters = in_set_parameters;
818 in->stream.common.get_parameters = in_get_parameters;
819 in->stream.common.add_audio_effect = in_add_audio_effect;
820 in->stream.common.remove_audio_effect = in_remove_audio_effect;
821 in->stream.set_gain = in_set_gain;
822 in->stream.read = in_read;
823 in->stream.get_input_frames_lost = in_get_input_frames_lost;
824
825 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
826 rsxadev->config.channel_mask = config->channel_mask;
827
Eric Laurent9595f7c2013-08-22 09:55:13 -0700828 if ((config->sample_rate != 48000) && (config->sample_rate != 44100)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700829 config->sample_rate = DEFAULT_RATE_HZ;
830 }
831 rsxadev->config.rate = config->sample_rate;
832
833 config->format = AUDIO_FORMAT_PCM_16_BIT;
834 rsxadev->config.format = config->format;
835
836 rsxadev->config.period_size = 1024;
837 rsxadev->config.period_count = 4;
838
839 *stream_in = &in->stream;
840
841 in->dev = rsxadev;
842
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700843 in->read_counter_frames = 0;
844 in->output_standby = rsxadev->output_standby;
845
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700846 pthread_mutex_unlock(&rsxadev->lock);
847
848 return 0;
849
850err_open:
851 *stream_in = NULL;
852 return ret;
853}
854
855static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -0700856 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700857{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700858 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700859 ALOGV("adev_close_input_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700860
861 pthread_mutex_lock(&rsxadev->lock);
862
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700863 MonoPipe* sink = rsxadev->rsxSink.get();
864 if (sink != NULL) {
865 ALOGI("shutdown");
866 sink->shutdown(true);
867 }
868
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700869 free(stream);
870
871 pthread_mutex_unlock(&rsxadev->lock);
872}
873
874static int adev_dump(const audio_hw_device_t *device, int fd)
875{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700876 (void)device;
877 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700878 return 0;
879}
880
881static int adev_close(hw_device_t *device)
882{
883 ALOGI("adev_close()");
884 free(device);
885 return 0;
886}
887
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700888static int adev_open(const hw_module_t* module, const char* name,
889 hw_device_t** device)
890{
891 ALOGI("adev_open(name=%s)", name);
892 struct submix_audio_device *rsxadev;
893
894 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
895 return -EINVAL;
896
897 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
898 if (!rsxadev)
899 return -ENOMEM;
900
901 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -0700902 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700903 rsxadev->device.common.module = (struct hw_module_t *) module;
904 rsxadev->device.common.close = adev_close;
905
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700906 rsxadev->device.init_check = adev_init_check;
907 rsxadev->device.set_voice_volume = adev_set_voice_volume;
908 rsxadev->device.set_master_volume = adev_set_master_volume;
909 rsxadev->device.get_master_volume = adev_get_master_volume;
910 rsxadev->device.set_master_mute = adev_set_master_mute;
911 rsxadev->device.get_master_mute = adev_get_master_mute;
912 rsxadev->device.set_mode = adev_set_mode;
913 rsxadev->device.set_mic_mute = adev_set_mic_mute;
914 rsxadev->device.get_mic_mute = adev_get_mic_mute;
915 rsxadev->device.set_parameters = adev_set_parameters;
916 rsxadev->device.get_parameters = adev_get_parameters;
917 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
918 rsxadev->device.open_output_stream = adev_open_output_stream;
919 rsxadev->device.close_output_stream = adev_close_output_stream;
920 rsxadev->device.open_input_stream = adev_open_input_stream;
921 rsxadev->device.close_input_stream = adev_close_input_stream;
922 rsxadev->device.dump = adev_dump;
923
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700924 rsxadev->input_standby = true;
925 rsxadev->output_standby = true;
926
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700927 *device = &rsxadev->device.common;
928
929 return 0;
930}
931
932static struct hw_module_methods_t hal_module_methods = {
933 /* open */ adev_open,
934};
935
936struct audio_module HAL_MODULE_INFO_SYM = {
937 /* common */ {
938 /* tag */ HARDWARE_MODULE_TAG,
939 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
940 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
941 /* id */ AUDIO_HARDWARE_MODULE_ID,
942 /* name */ "Wifi Display audio HAL",
943 /* author */ "The Android Open Source Project",
944 /* methods */ &hal_module_methods,
945 /* dso */ NULL,
946 /* reserved */ { 0 },
947 },
948};
949
950} //namespace android
951
952} //extern "C"