blob: d7e422beb9e0613ec6f01d6ce426b43827e8bc29 [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
Stewart Miles568e66f2014-05-01 09:03:27 -070056// Size of the pipe's buffer in frames.
Jean-Michel Trivieec87702012-09-17 09:59:42 -070057#define MAX_PIPE_DEPTH_IN_FRAMES (1024*8)
Stewart Miles568e66f2014-05-01 09:03:27 -070058// Maximum number of frames users of the input and output stream should buffer.
59#define PERIOD_SIZE_IN_FRAMES 1024
Jean-Michel Trivieec87702012-09-17 09:59:42 -070060// The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to
61// the duration of a record buffer at the current record sample rate (of the device, not of
62// the recording itself). Here we have:
63// 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -070064#define MAX_READ_ATTEMPTS 3
Jean-Michel Trivieec87702012-09-17 09:59:42 -070065#define READ_ATTEMPT_SLEEP_MS 5 // 5ms between two read attempts when pipe is empty
Stewart Miles568e66f2014-05-01 09:03:27 -070066#define DEFAULT_SAMPLE_RATE_HZ 48000 // default sample rate
67// See NBAIO_Format frameworks/av/include/media/nbaio/NBAIO.h.
68#define DEFAULT_FORMAT AUDIO_FORMAT_PCM_16_BIT
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070069
Stewart Miles568e66f2014-05-01 09:03:27 -070070// Configuration of the submix pipe.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070071struct submix_config {
72 audio_format_t format;
73 audio_channel_mask_t channel_mask;
Stewart Miles568e66f2014-05-01 09:03:27 -070074 unsigned int sample_rate; // Sample rate for the device in Hz.
75 size_t period_size; // Size of the audio pipe is period_size * period_count in frames.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070076};
77
78struct submix_audio_device {
79 struct audio_hw_device device;
Jean-Michel Trivieec87702012-09-17 09:59:42 -070080 bool output_standby;
81 bool input_standby;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070082 submix_config config;
83 // Pipe variables: they handle the ring buffer that "pipes" audio:
Jean-Michel Trivieec87702012-09-17 09:59:42 -070084 // - from the submix virtual audio output == what needs to be played
85 // remotely, seen as an output for AudioFlinger
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070086 // - to the virtual audio source == what is captured by the component
87 // which "records" the submix / virtual audio source, and handles it as needed.
Jean-Michel Trivieec87702012-09-17 09:59:42 -070088 // A usecase example is one where the component capturing the audio is then sending it over
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070089 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
90 // TV with Wifi Display capabilities), or to a wireless audio player.
Stewart Miles568e66f2014-05-01 09:03:27 -070091 sp<MonoPipe> rsxSink;
Jean-Michel Trivieec87702012-09-17 09:59:42 -070092 sp<MonoPipeReader> rsxSource;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070093
Stewart Miles568e66f2014-05-01 09:03:27 -070094 // Device lock, also used to protect access to submix_audio_device from the input and output
95 // streams.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070096 pthread_mutex_t lock;
97};
98
99struct submix_stream_out {
100 struct audio_stream_out stream;
101 struct submix_audio_device *dev;
102};
103
104struct submix_stream_in {
105 struct audio_stream_in stream;
106 struct submix_audio_device *dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700107 bool output_standby; // output standby state as seen from record thread
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700108
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700109 // wall clock when recording starts
110 struct timespec record_start_time;
111 // how many frames have been requested to be read
112 int64_t read_counter_frames;
113};
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700114
Stewart Milesf645c5e2014-05-01 09:03:27 -0700115// Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the
116// structure.
117static struct submix_stream_out * audio_stream_out_get_submix_stream_out(
118 struct audio_stream_out * const stream)
119{
120 ALOG_ASSERT(stream);
121 return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) -
122 offsetof(struct submix_stream_out, stream));
123}
124
125// Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure.
126static struct submix_stream_out * audio_stream_get_submix_stream_out(
127 struct audio_stream * const stream)
128{
129 ALOG_ASSERT(stream);
130 return audio_stream_out_get_submix_stream_out(
131 reinterpret_cast<struct audio_stream_out *>(stream));
132}
133
134// Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the
135// structure.
136static struct submix_stream_in * audio_stream_in_get_submix_stream_in(
137 struct audio_stream_in * const stream)
138{
139 ALOG_ASSERT(stream);
140 return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) -
141 offsetof(struct submix_stream_in, stream));
142}
143
144// Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure.
145static struct submix_stream_in * audio_stream_get_submix_stream_in(
146 struct audio_stream * const stream)
147{
148 ALOG_ASSERT(stream);
149 return audio_stream_in_get_submix_stream_in(
150 reinterpret_cast<struct audio_stream_in *>(stream));
151}
152
153// Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within
154// the structure.
155static struct submix_audio_device * audio_hw_device_get_submix_audio_device(
156 struct audio_hw_device *device)
157{
158 ALOG_ASSERT(device);
159 return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) -
160 offsetof(struct submix_audio_device, device));
161}
162
Stewart Miles568e66f2014-05-01 09:03:27 -0700163// Get the number of channels referenced by the specified channel_mask. The channel_mask can
164// reference either input or output channels.
165uint32_t get_channel_count_from_mask(const audio_channel_mask_t channel_mask) {
166 if (audio_is_input_channel(channel_mask)) {
167 return popcount(channel_mask & AUDIO_CHANNEL_IN_ALL);
168 } else if (audio_is_output_channel(channel_mask)) {
169 return popcount(channel_mask & AUDIO_CHANNEL_OUT_ALL);
170 }
171 ALOGE("get_channel_count(): No channels specified in channel mask %x", channel_mask);
172 return 0;
173}
174
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700175/* audio HAL functions */
176
177static uint32_t out_get_sample_rate(const struct audio_stream *stream)
178{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700179 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
180 const_cast<struct audio_stream *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700181 const uint32_t out_rate = out->dev->config.sample_rate;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700182 SUBMIX_ALOGV("out_get_sample_rate() returns %u", out_rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700183 return out_rate;
184}
185
186static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
187{
188 if ((rate != 44100) && (rate != 48000)) {
189 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
190 return -ENOSYS;
191 }
Stewart Milesf645c5e2014-05-01 09:03:27 -0700192 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700193 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Stewart Miles568e66f2014-05-01 09:03:27 -0700194 out->dev->config.sample_rate = rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700195 return 0;
196}
197
198static size_t out_get_buffer_size(const struct audio_stream *stream)
199{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700200 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
201 const_cast<struct audio_stream *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700202 const struct submix_config * const config = &out->dev->config;
203 const size_t buffer_size = config->period_size * audio_stream_frame_size(stream);
204 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
205 buffer_size, config->buffer_period_size_frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700206 return buffer_size;
207}
208
209static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
210{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700211 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
212 const_cast<struct audio_stream *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700213 uint32_t channel_mask = out->dev->config.channel_mask;
214 SUBMIX_ALOGV("out_get_channels() returns %08x", channel_mask);
215 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700216}
217
218static audio_format_t out_get_format(const struct audio_stream *stream)
219{
Stewart Miles568e66f2014-05-01 09:03:27 -0700220 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
221 const_cast<struct audio_stream *>(stream));
222 const audio_format_t format = out->dev->config.format;
223 SUBMIX_ALOGV("out_get_format() returns %x", format);
224 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700225}
226
227static int out_set_format(struct audio_stream *stream, audio_format_t format)
228{
Stewart Miles568e66f2014-05-01 09:03:27 -0700229 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
230 if (format != out->dev->config.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700231 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700232 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700233 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700234 SUBMIX_ALOGV("out_set_format(format=%x)", format);
235 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700236}
237
238static int out_standby(struct audio_stream *stream)
239{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700240 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_out(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700241 ALOGI("out_standby()");
242
Stewart Milesf645c5e2014-05-01 09:03:27 -0700243 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700244
Stewart Milesf645c5e2014-05-01 09:03:27 -0700245 rsxadev->output_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700246
Stewart Milesf645c5e2014-05-01 09:03:27 -0700247 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700248
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700249 return 0;
250}
251
252static int out_dump(const struct audio_stream *stream, int fd)
253{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700254 (void)stream;
255 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700256 return 0;
257}
258
259static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
260{
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700261 int exiting = -1;
262 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700263 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700264 // FIXME this is using hard-coded strings but in the future, this functionality will be
265 // converted to use audio HAL extensions required to support tunneling
266 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700267 struct submix_audio_device * const rsxadev =
268 audio_stream_get_submix_stream_out(stream)->dev;
269 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800270 { // using the sink
Stewart Milesf645c5e2014-05-01 09:03:27 -0700271 sp<MonoPipe> sink = rsxadev->rsxSink.get();
272 if (sink == NULL) {
273 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800274 return 0;
275 }
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700276
Stewart Milesc049a0a2014-05-01 09:03:27 -0700277 ALOGI("out_set_parameters(): shutdown");
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800278 sink->shutdown(true);
279 } // done using the sink
Stewart Milesf645c5e2014-05-01 09:03:27 -0700280 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700281 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700282 return 0;
283}
284
285static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
286{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700287 (void)stream;
288 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700289 return strdup("");
290}
291
292static uint32_t out_get_latency(const struct audio_stream_out *stream)
293{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700294 const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
295 const_cast<struct audio_stream_out *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700296 const struct submix_config * const config = &out->dev->config;
297 const uint32_t latency_ms = (MAX_PIPE_DEPTH_IN_FRAMES * 1000) / config->sample_rate;
298 SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u", latency_ms,
299 config->buffer_size_frames, config->common.sample_rate);
300 return latency_ms;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700301}
302
303static int out_set_volume(struct audio_stream_out *stream, float left,
304 float right)
305{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700306 (void)stream;
307 (void)left;
308 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700309 return -ENOSYS;
310}
311
312static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
313 size_t bytes)
314{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700315 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700316 ssize_t written_frames = 0;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700317 const size_t frame_size = audio_stream_frame_size(&stream->common);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700318 struct submix_audio_device * const rsxadev =
319 audio_stream_out_get_submix_stream_out(stream)->dev;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700320 const size_t frames = bytes / frame_size;
321
Stewart Milesf645c5e2014-05-01 09:03:27 -0700322 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700323
Stewart Milesf645c5e2014-05-01 09:03:27 -0700324 rsxadev->output_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700325
Stewart Milesf645c5e2014-05-01 09:03:27 -0700326 sp<MonoPipe> sink = rsxadev->rsxSink.get();
327 if (sink != NULL) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700328 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800329 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700330 pthread_mutex_unlock(&rsxadev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700331 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700332 // the pipe has already been shutdown, this buffer will be lost but we must
333 // simulate timing so we don't drain the output faster than realtime
334 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
335 return bytes;
336 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700337 } else {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700338 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700339 ALOGE("out_write without a pipe!");
340 ALOG_ASSERT("out_write without a pipe!");
341 return 0;
342 }
343
Stewart Milesf645c5e2014-05-01 09:03:27 -0700344 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700345
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700346 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800347
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700348 if (written_frames < 0) {
349 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700350 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700351
Stewart Milesf645c5e2014-05-01 09:03:27 -0700352 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800353 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700354 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700355
356 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700357 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700358 } else {
359 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700360 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700361 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700362 }
363 }
364
Stewart Milesf645c5e2014-05-01 09:03:27 -0700365 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800366 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700367 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700368
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700369 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700370 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700371 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700372 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700373 const ssize_t written_bytes = written_frames * frame_size;
374 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames)", written_bytes, written_frames);
375 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700376}
377
378static int out_get_render_position(const struct audio_stream_out *stream,
379 uint32_t *dsp_frames)
380{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700381 (void)stream;
382 (void)dsp_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700383 return -EINVAL;
384}
385
386static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
387{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700388 (void)stream;
389 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700390 return 0;
391}
392
393static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
394{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700395 (void)stream;
396 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700397 return 0;
398}
399
400static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
401 int64_t *timestamp)
402{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700403 (void)stream;
404 (void)timestamp;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700405 return -EINVAL;
406}
407
408/** audio_stream_in implementation **/
409static uint32_t in_get_sample_rate(const struct audio_stream *stream)
410{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700411 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
412 const_cast<struct audio_stream*>(stream));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700413 SUBMIX_ALOGV("in_get_sample_rate() returns %u", in->dev->config.sample_rate);
Stewart Miles568e66f2014-05-01 09:03:27 -0700414 return in->dev->config.sample_rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700415}
416
417static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
418{
Stewart Miles568e66f2014-05-01 09:03:27 -0700419 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
420 in->dev->config.sample_rate = rate;
421 SUBMIX_ALOGV("in_set_sample_rate() set %u", rate);
422 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700423}
424
425static size_t in_get_buffer_size(const struct audio_stream *stream)
426{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700427 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
428 const_cast<struct audio_stream*>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700429 const size_t buffer_size = in->dev->config.period_size * audio_stream_frame_size(stream);
430 SUBMIX_ALOGV("in_get_buffer_size() returns %zu", buffer_size);
431 return buffer_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700432}
433
434static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
435{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700436 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700437 return AUDIO_CHANNEL_IN_STEREO;
438}
439
440static audio_format_t in_get_format(const struct audio_stream *stream)
441{
Stewart Miles568e66f2014-05-01 09:03:27 -0700442 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
443 const_cast<struct audio_stream*>(stream));
444 const audio_format_t format = in->dev->config.format;
445 SUBMIX_ALOGV("in_get_format() returns %x", format);
446 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700447}
448
449static int in_set_format(struct audio_stream *stream, audio_format_t format)
450{
Stewart Miles568e66f2014-05-01 09:03:27 -0700451 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
452 if (format != in->dev->config.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700453 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700454 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700455 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700456 SUBMIX_ALOGV("in_set_format(format=%x)", format);
457 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700458}
459
460static int in_standby(struct audio_stream *stream)
461{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700462 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_in(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700463 ALOGI("in_standby()");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700464
Stewart Milesf645c5e2014-05-01 09:03:27 -0700465 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700466
Stewart Milesf645c5e2014-05-01 09:03:27 -0700467 rsxadev->input_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700468
Stewart Milesf645c5e2014-05-01 09:03:27 -0700469 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700470
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700471 return 0;
472}
473
474static int in_dump(const struct audio_stream *stream, int fd)
475{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700476 (void)stream;
477 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700478 return 0;
479}
480
481static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
482{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700483 (void)stream;
484 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700485 return 0;
486}
487
488static char * in_get_parameters(const struct audio_stream *stream,
489 const char *keys)
490{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700491 (void)stream;
492 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700493 return strdup("");
494}
495
496static int in_set_gain(struct audio_stream_in *stream, float gain)
497{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700498 (void)stream;
499 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700500 return 0;
501}
502
503static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
504 size_t bytes)
505{
506 ssize_t frames_read = -1977;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700507 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
508 struct submix_audio_device * const rsxadev = in->dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700509 const size_t frame_size = audio_stream_frame_size(&stream->common);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700510 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700511
Stewart Milesc049a0a2014-05-01 09:03:27 -0700512 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700513 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700514
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700515 const bool output_standby_transition = (in->output_standby != in->dev->output_standby);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700516 in->output_standby = rsxadev->output_standby;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700517
Stewart Milesf645c5e2014-05-01 09:03:27 -0700518 if (rsxadev->input_standby || output_standby_transition) {
519 rsxadev->input_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700520 // keep track of when we exit input standby (== first read == start "real recording")
521 // or when we start recording silence, and reset projected time
522 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
523 if (rc == 0) {
524 in->read_counter_frames = 0;
525 }
526 }
527
528 in->read_counter_frames += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700529 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800530
531 {
532 // about to read from audio source
Stewart Milesf645c5e2014-05-01 09:03:27 -0700533 sp<MonoPipeReader> source = rsxadev->rsxSource;
534 if (source == NULL) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800535 ALOGE("no audio pipe yet we're trying to read!");
Stewart Milesf645c5e2014-05-01 09:03:27 -0700536 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800537 usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common));
538 memset(buffer, 0, bytes);
539 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700540 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800541
Stewart Milesf645c5e2014-05-01 09:03:27 -0700542 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800543
544 // read the data from the pipe (it's non blocking)
545 int attempts = 0;
546 char* buff = (char*)buffer;
547 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
548 attempts++;
549 frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS);
550 if (frames_read > 0) {
551 remaining_frames -= frames_read;
552 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700553 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
554 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800555 } else {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700556 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800557 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
558 }
559 }
560 // done using the source
Stewart Milesf645c5e2014-05-01 09:03:27 -0700561 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800562 source.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700563 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700564 }
565
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700566 if (remaining_frames > 0) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700567 SUBMIX_ALOGV(" remaining_frames = %zu", remaining_frames);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700568 memset(((char*)buffer)+ bytes - (remaining_frames * frame_size), 0,
569 remaining_frames * frame_size);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700570 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700571
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700572 // compute how much we need to sleep after reading the data by comparing the wall clock with
573 // the projected time at which we should return.
574 struct timespec time_after_read;// wall clock after reading from the pipe
575 struct timespec record_duration;// observed record duration
576 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
577 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
578 if (rc == 0) {
579 // for how long have we been recording?
580 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
581 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
582 if (record_duration.tv_nsec < 0) {
583 record_duration.tv_sec--;
584 record_duration.tv_nsec += 1000000000;
585 }
586
Stewart Milesf645c5e2014-05-01 09:03:27 -0700587 // read_counter_frames contains the number of frames that have been read since the
588 // beginning of recording (including this call): it's converted to usec and compared to
589 // how long we've been recording for, which gives us how long we must wait to sync the
590 // projected recording time, and the observed recording time.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700591 long projected_vs_observed_offset_us =
592 ((int64_t)(in->read_counter_frames
593 - (record_duration.tv_sec*sample_rate)))
594 * 1000000 / sample_rate
595 - (record_duration.tv_nsec / 1000);
596
Stewart Milesc049a0a2014-05-01 09:03:27 -0700597 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700598 record_duration.tv_sec, record_duration.tv_nsec/1000000,
599 projected_vs_observed_offset_us);
600 if (projected_vs_observed_offset_us > 0) {
601 usleep(projected_vs_observed_offset_us);
602 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700603 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700604
Stewart Milesc049a0a2014-05-01 09:03:27 -0700605 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700606 return bytes;
607
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700608}
609
610static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
611{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700612 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700613 return 0;
614}
615
616static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
617{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700618 (void)stream;
619 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700620 return 0;
621}
622
623static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
624{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700625 (void)stream;
626 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700627 return 0;
628}
629
630static int adev_open_output_stream(struct audio_hw_device *dev,
631 audio_io_handle_t handle,
632 audio_devices_t devices,
633 audio_output_flags_t flags,
634 struct audio_config *config,
635 struct audio_stream_out **stream_out)
636{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700637 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700638 ALOGV("adev_open_output_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700639 struct submix_stream_out *out;
640 int ret;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700641 (void)handle;
642 (void)devices;
643 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700644
645 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
646 if (!out) {
647 ret = -ENOMEM;
648 goto err_open;
649 }
650
651 pthread_mutex_lock(&rsxadev->lock);
652
Stewart Miles568e66f2014-05-01 09:03:27 -0700653 // Initialize the function pointer tables (v-tables).
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700654 out->stream.common.get_sample_rate = out_get_sample_rate;
655 out->stream.common.set_sample_rate = out_set_sample_rate;
656 out->stream.common.get_buffer_size = out_get_buffer_size;
657 out->stream.common.get_channels = out_get_channels;
658 out->stream.common.get_format = out_get_format;
659 out->stream.common.set_format = out_set_format;
660 out->stream.common.standby = out_standby;
661 out->stream.common.dump = out_dump;
662 out->stream.common.set_parameters = out_set_parameters;
663 out->stream.common.get_parameters = out_get_parameters;
664 out->stream.common.add_audio_effect = out_add_audio_effect;
665 out->stream.common.remove_audio_effect = out_remove_audio_effect;
666 out->stream.get_latency = out_get_latency;
667 out->stream.set_volume = out_set_volume;
668 out->stream.write = out_write;
669 out->stream.get_render_position = out_get_render_position;
670 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
671
672 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
673 rsxadev->config.channel_mask = config->channel_mask;
674
Eric Laurent9595f7c2013-08-22 09:55:13 -0700675 if ((config->sample_rate != 48000) && (config->sample_rate != 44100)) {
Stewart Miles568e66f2014-05-01 09:03:27 -0700676 config->sample_rate = DEFAULT_SAMPLE_RATE_HZ;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700677 }
Stewart Miles568e66f2014-05-01 09:03:27 -0700678 rsxadev->config.sample_rate = config->sample_rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700679
680 config->format = AUDIO_FORMAT_PCM_16_BIT;
681 rsxadev->config.format = config->format;
682
Stewart Miles568e66f2014-05-01 09:03:27 -0700683 rsxadev->config.period_size = PERIOD_SIZE_IN_FRAMES;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700684
Stewart Miles568e66f2014-05-01 09:03:27 -0700685 // Store a pointer to the device from the output stream.
686 out->dev = rsxadev;
687 // Return the output stream.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700688 *stream_out = &out->stream;
689
Stewart Miles568e66f2014-05-01 09:03:27 -0700690
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700691 // initialize pipe
692 {
693 ALOGV(" initializing pipe");
Glenn Kasten0538a3e2014-03-07 08:26:16 -0800694 const NBAIO_Format format = Format_from_SR_C(config->sample_rate,
Stewart Miles568e66f2014-05-01 09:03:27 -0700695 get_channel_count_from_mask(config->channel_mask), config->format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700696 const NBAIO_Format offers[1] = {format};
697 size_t numCounterOffers = 0;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700698 // creating a MonoPipe with optional blocking set to true.
699 MonoPipe* sink = new MonoPipe(MAX_PIPE_DEPTH_IN_FRAMES, format, true/*writeCanBlock*/);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700700 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
701 ALOG_ASSERT(index == 0);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700702 MonoPipeReader* source = new MonoPipeReader(sink);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700703 numCounterOffers = 0;
704 index = source->negotiate(offers, 1, NULL, numCounterOffers);
705 ALOG_ASSERT(index == 0);
706 rsxadev->rsxSink = sink;
707 rsxadev->rsxSource = source;
708 }
709
710 pthread_mutex_unlock(&rsxadev->lock);
711
712 return 0;
713
714err_open:
715 *stream_out = NULL;
716 return ret;
717}
718
719static void adev_close_output_stream(struct audio_hw_device *dev,
720 struct audio_stream_out *stream)
721{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700722 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700723 ALOGV("adev_close_output_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700724
725 pthread_mutex_lock(&rsxadev->lock);
726
727 rsxadev->rsxSink.clear();
728 rsxadev->rsxSource.clear();
729 free(stream);
730
731 pthread_mutex_unlock(&rsxadev->lock);
732}
733
734static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
735{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700736 (void)dev;
737 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700738 return -ENOSYS;
739}
740
741static char * adev_get_parameters(const struct audio_hw_device *dev,
742 const char *keys)
743{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700744 (void)dev;
745 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700746 return strdup("");;
747}
748
749static int adev_init_check(const struct audio_hw_device *dev)
750{
751 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -0700752 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700753 return 0;
754}
755
756static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
757{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700758 (void)dev;
759 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700760 return -ENOSYS;
761}
762
763static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
764{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700765 (void)dev;
766 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700767 return -ENOSYS;
768}
769
770static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
771{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700772 (void)dev;
773 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700774 return -ENOSYS;
775}
776
777static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
778{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700779 (void)dev;
780 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700781 return -ENOSYS;
782}
783
784static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
785{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700786 (void)dev;
787 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700788 return -ENOSYS;
789}
790
791static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
792{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700793 (void)dev;
794 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700795 return 0;
796}
797
798static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
799{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700800 (void)dev;
801 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700802 return -ENOSYS;
803}
804
805static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
806{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700807 (void)dev;
808 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700809 return -ENOSYS;
810}
811
812static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
813 const struct audio_config *config)
814{
Stewart Miles568e66f2014-05-01 09:03:27 -0700815 if (audio_is_linear_pcm(config->format)) {
816 const size_t buffer_period_size_frames =
817 audio_hw_device_get_submix_audio_device(const_cast<struct audio_hw_device*>(dev))->
818 config.period_size;
819 const size_t frame_size_in_bytes = get_channel_count_from_mask(config->channel_mask) *
820 audio_bytes_per_sample(config->format);
821 const size_t buffer_size = buffer_period_size_frames * frame_size_in_bytes;
822 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
823 buffer_size, buffer_period_size_frames);
824 return buffer_size;
825 }
826 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700827}
828
829static int adev_open_input_stream(struct audio_hw_device *dev,
830 audio_io_handle_t handle,
831 audio_devices_t devices,
832 struct audio_config *config,
833 struct audio_stream_in **stream_in)
834{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700835 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700836 struct submix_stream_in *in;
837 int ret;
Stewart Miles568e66f2014-05-01 09:03:27 -0700838 ALOGI("adev_open_input_stream()");
Stewart Milesc049a0a2014-05-01 09:03:27 -0700839 (void)handle;
840 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700841
842 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
843 if (!in) {
844 ret = -ENOMEM;
845 goto err_open;
846 }
847
848 pthread_mutex_lock(&rsxadev->lock);
849
Stewart Miles568e66f2014-05-01 09:03:27 -0700850 // Initialize the function pointer tables (v-tables).
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700851 in->stream.common.get_sample_rate = in_get_sample_rate;
852 in->stream.common.set_sample_rate = in_set_sample_rate;
853 in->stream.common.get_buffer_size = in_get_buffer_size;
854 in->stream.common.get_channels = in_get_channels;
855 in->stream.common.get_format = in_get_format;
856 in->stream.common.set_format = in_set_format;
857 in->stream.common.standby = in_standby;
858 in->stream.common.dump = in_dump;
859 in->stream.common.set_parameters = in_set_parameters;
860 in->stream.common.get_parameters = in_get_parameters;
861 in->stream.common.add_audio_effect = in_add_audio_effect;
862 in->stream.common.remove_audio_effect = in_remove_audio_effect;
863 in->stream.set_gain = in_set_gain;
864 in->stream.read = in_read;
865 in->stream.get_input_frames_lost = in_get_input_frames_lost;
866
867 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
868 rsxadev->config.channel_mask = config->channel_mask;
869
Eric Laurent9595f7c2013-08-22 09:55:13 -0700870 if ((config->sample_rate != 48000) && (config->sample_rate != 44100)) {
Stewart Miles568e66f2014-05-01 09:03:27 -0700871 config->sample_rate = DEFAULT_SAMPLE_RATE_HZ;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700872 }
Stewart Miles568e66f2014-05-01 09:03:27 -0700873 rsxadev->config.sample_rate = config->sample_rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700874
875 config->format = AUDIO_FORMAT_PCM_16_BIT;
876 rsxadev->config.format = config->format;
877
Stewart Miles568e66f2014-05-01 09:03:27 -0700878 rsxadev->config.period_size = PERIOD_SIZE_IN_FRAMES;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700879
880 *stream_in = &in->stream;
881
882 in->dev = rsxadev;
883
Stewart Miles568e66f2014-05-01 09:03:27 -0700884 // Initialize the input stream.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700885 in->read_counter_frames = 0;
886 in->output_standby = rsxadev->output_standby;
887
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700888 pthread_mutex_unlock(&rsxadev->lock);
889
890 return 0;
891
892err_open:
893 *stream_in = NULL;
894 return ret;
895}
896
897static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -0700898 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700899{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700900 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700901 ALOGV("adev_close_input_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700902
903 pthread_mutex_lock(&rsxadev->lock);
904
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700905 MonoPipe* sink = rsxadev->rsxSink.get();
906 if (sink != NULL) {
907 ALOGI("shutdown");
908 sink->shutdown(true);
909 }
910
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700911 free(stream);
912
913 pthread_mutex_unlock(&rsxadev->lock);
914}
915
916static int adev_dump(const audio_hw_device_t *device, int fd)
917{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700918 (void)device;
919 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700920 return 0;
921}
922
923static int adev_close(hw_device_t *device)
924{
925 ALOGI("adev_close()");
926 free(device);
927 return 0;
928}
929
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700930static int adev_open(const hw_module_t* module, const char* name,
931 hw_device_t** device)
932{
933 ALOGI("adev_open(name=%s)", name);
934 struct submix_audio_device *rsxadev;
935
936 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
937 return -EINVAL;
938
939 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
940 if (!rsxadev)
941 return -ENOMEM;
942
943 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -0700944 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700945 rsxadev->device.common.module = (struct hw_module_t *) module;
946 rsxadev->device.common.close = adev_close;
947
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700948 rsxadev->device.init_check = adev_init_check;
949 rsxadev->device.set_voice_volume = adev_set_voice_volume;
950 rsxadev->device.set_master_volume = adev_set_master_volume;
951 rsxadev->device.get_master_volume = adev_get_master_volume;
952 rsxadev->device.set_master_mute = adev_set_master_mute;
953 rsxadev->device.get_master_mute = adev_get_master_mute;
954 rsxadev->device.set_mode = adev_set_mode;
955 rsxadev->device.set_mic_mute = adev_set_mic_mute;
956 rsxadev->device.get_mic_mute = adev_get_mic_mute;
957 rsxadev->device.set_parameters = adev_set_parameters;
958 rsxadev->device.get_parameters = adev_get_parameters;
959 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
960 rsxadev->device.open_output_stream = adev_open_output_stream;
961 rsxadev->device.close_output_stream = adev_close_output_stream;
962 rsxadev->device.open_input_stream = adev_open_input_stream;
963 rsxadev->device.close_input_stream = adev_close_input_stream;
964 rsxadev->device.dump = adev_dump;
965
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700966 rsxadev->input_standby = true;
967 rsxadev->output_standby = true;
968
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700969 *device = &rsxadev->device.common;
970
971 return 0;
972}
973
974static struct hw_module_methods_t hal_module_methods = {
975 /* open */ adev_open,
976};
977
978struct audio_module HAL_MODULE_INFO_SYM = {
979 /* common */ {
980 /* tag */ HARDWARE_MODULE_TAG,
981 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
982 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
983 /* id */ AUDIO_HARDWARE_MODULE_ID,
984 /* name */ "Wifi Display audio HAL",
985 /* author */ "The Android Open Source Project",
986 /* methods */ &hal_module_methods,
987 /* dso */ NULL,
988 /* reserved */ { 0 },
989 },
990};
991
992} //namespace android
993
994} //extern "C"