blob: 014da8ede988b3e6739f340f9bdf89499fc05d2e [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>
Stewart Milese54c12c2014-05-01 09:03:27 -070026#include <sys/limits.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070027
28#include <cutils/log.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070029#include <cutils/properties.h>
Stewart Milesc049a0a2014-05-01 09:03:27 -070030#include <cutils/str_parms.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070031
Stewart Milesc049a0a2014-05-01 09:03:27 -070032#include <hardware/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070033#include <hardware/hardware.h>
34#include <system/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070035
Stewart Milesc049a0a2014-05-01 09:03:27 -070036#include <media/AudioParameter.h>
37#include <media/AudioBufferProvider.h>
Jean-Michel Trivieec87702012-09-17 09:59:42 -070038#include <media/nbaio/MonoPipe.h>
39#include <media/nbaio/MonoPipeReader.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070040
Jean-Michel Trivid4413032012-09-30 11:08:06 -070041#include <utils/String8.h>
Jean-Michel Trivid4413032012-09-30 11:08:06 -070042
Stewart Miles92854f52014-05-01 09:03:27 -070043#define LOG_STREAMS_TO_FILES 0
44#if LOG_STREAMS_TO_FILES
45#include <fcntl.h>
46#include <stdio.h>
47#include <sys/stat.h>
48#endif // LOG_STREAMS_TO_FILES
49
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070050extern "C" {
51
52namespace android {
53
Stewart Milesc049a0a2014-05-01 09:03:27 -070054// Set to 1 to enable extremely verbose logging in this module.
55#define SUBMIX_VERBOSE_LOGGING 0
56#if SUBMIX_VERBOSE_LOGGING
57#define SUBMIX_ALOGV(...) ALOGV(__VA_ARGS__)
58#define SUBMIX_ALOGE(...) ALOGE(__VA_ARGS__)
59#else
60#define SUBMIX_ALOGV(...)
61#define SUBMIX_ALOGE(...)
62#endif // SUBMIX_VERBOSE_LOGGING
63
Stewart Miles3dd36f92014-05-01 09:03:27 -070064// NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe().
65#define DEFAULT_PIPE_SIZE_IN_FRAMES (1024*8)
66// Value used to divide the MonoPipe() buffer into segments that are written to the source and
67// read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer
68// the minimum latency is the MonoPipe buffer size divided by this value.
69#define DEFAULT_PIPE_PERIOD_COUNT 4
Jean-Michel Trivieec87702012-09-17 09:59:42 -070070// The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to
71// the duration of a record buffer at the current record sample rate (of the device, not of
72// the recording itself). Here we have:
73// 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -070074#define MAX_READ_ATTEMPTS 3
Jean-Michel Trivieec87702012-09-17 09:59:42 -070075#define READ_ATTEMPT_SLEEP_MS 5 // 5ms between two read attempts when pipe is empty
Stewart Miles568e66f2014-05-01 09:03:27 -070076#define DEFAULT_SAMPLE_RATE_HZ 48000 // default sample rate
77// See NBAIO_Format frameworks/av/include/media/nbaio/NBAIO.h.
78#define DEFAULT_FORMAT AUDIO_FORMAT_PCM_16_BIT
Stewart Miles3dd36f92014-05-01 09:03:27 -070079// A legacy user of this device does not close the input stream when it shuts down, which
80// results in the application opening a new input stream before closing the old input stream
81// handle it was previously using. Setting this value to 1 allows multiple clients to open
82// multiple input streams from this device. If this option is enabled, each input stream returned
83// is *the same stream* which means that readers will race to read data from these streams.
84#define ENABLE_LEGACY_INPUT_OPEN 1
Stewart Milese54c12c2014-05-01 09:03:27 -070085// Whether channel conversion (16-bit signed PCM mono->stereo, stereo->mono) is enabled.
86#define ENABLE_CHANNEL_CONVERSION 1
Stewart Miles02c2f712014-05-01 09:03:27 -070087// Whether resampling is enabled.
88#define ENABLE_RESAMPLING 1
Stewart Miles92854f52014-05-01 09:03:27 -070089#if LOG_STREAMS_TO_FILES
90// Folder to save stream log files to.
91#define LOG_STREAM_FOLDER "/data/misc/media"
92// Log filenames for input and output streams.
93#define LOG_STREAM_OUT_FILENAME LOG_STREAM_FOLDER "/r_submix_out.raw"
94#define LOG_STREAM_IN_FILENAME LOG_STREAM_FOLDER "/r_submix_in.raw"
95// File permissions for stream log files.
96#define LOG_STREAM_FILE_PERMISSIONS (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
97#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -070098
99// Common limits macros.
100#ifndef min
101#define min(a, b) ((a) < (b) ? (a) : (b))
102#endif // min
Stewart Milese54c12c2014-05-01 09:03:27 -0700103#ifndef max
104#define max(a, b) ((a) > (b) ? (a) : (b))
105#endif // max
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700106
Stewart Miles70726842014-05-01 09:03:27 -0700107// Set *result_variable_ptr to true if value_to_find is present in the array array_to_search,
108// otherwise set *result_variable_ptr to false.
109#define SUBMIX_VALUE_IN_SET(value_to_find, array_to_search, result_variable_ptr) \
110 { \
111 size_t i; \
112 *(result_variable_ptr) = false; \
113 for (i = 0; i < sizeof(array_to_search) / sizeof((array_to_search)[0]); i++) { \
114 if ((value_to_find) == (array_to_search)[i]) { \
115 *(result_variable_ptr) = true; \
116 break; \
117 } \
118 } \
119 }
120
Stewart Miles568e66f2014-05-01 09:03:27 -0700121// Configuration of the submix pipe.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700122struct submix_config {
Stewart Miles70726842014-05-01 09:03:27 -0700123 // Channel mask field in this data structure is set to either input_channel_mask or
124 // output_channel_mask depending upon the last stream to be opened on this device.
125 struct audio_config common;
126 // Input stream and output stream channel masks. This is required since input and output
127 // channel bitfields are not equivalent.
128 audio_channel_mask_t input_channel_mask;
129 audio_channel_mask_t output_channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700130#if ENABLE_RESAMPLING
131 // Input stream and output stream sample rates.
132 uint32_t input_sample_rate;
133 uint32_t output_sample_rate;
134#endif // ENABLE_RESAMPLING
Stewart Milese54c12c2014-05-01 09:03:27 -0700135 size_t pipe_frame_size; // Number of bytes in each audio frame in the pipe.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700136 size_t buffer_size_frames; // Size of the audio pipe in frames.
137 // Maximum number of frames buffered by the input and output streams.
138 size_t buffer_period_size_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700139};
140
141struct submix_audio_device {
142 struct audio_hw_device device;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700143 bool input_standby;
Stewart Miles70726842014-05-01 09:03:27 -0700144 bool output_standby;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700145 submix_config config;
146 // Pipe variables: they handle the ring buffer that "pipes" audio:
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700147 // - from the submix virtual audio output == what needs to be played
148 // remotely, seen as an output for AudioFlinger
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700149 // - to the virtual audio source == what is captured by the component
150 // which "records" the submix / virtual audio source, and handles it as needed.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700151 // A usecase example is one where the component capturing the audio is then sending it over
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700152 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
153 // TV with Wifi Display capabilities), or to a wireless audio player.
Stewart Miles568e66f2014-05-01 09:03:27 -0700154 sp<MonoPipe> rsxSink;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700155 sp<MonoPipeReader> rsxSource;
Stewart Miles02c2f712014-05-01 09:03:27 -0700156#if ENABLE_RESAMPLING
157 // Buffer used as temporary storage for resampled data prior to returning data to the output
158 // stream.
159 int16_t resampler_buffer[DEFAULT_PIPE_SIZE_IN_FRAMES];
160#endif // ENABLE_RESAMPLING
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700161
Stewart Miles3dd36f92014-05-01 09:03:27 -0700162 // Pointers to the current input and output stream instances. rsxSink and rsxSource are
163 // destroyed if both and input and output streams are destroyed.
164 struct submix_stream_out *output;
165 struct submix_stream_in *input;
166
Stewart Miles568e66f2014-05-01 09:03:27 -0700167 // Device lock, also used to protect access to submix_audio_device from the input and output
168 // streams.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700169 pthread_mutex_t lock;
170};
171
172struct submix_stream_out {
173 struct audio_stream_out stream;
174 struct submix_audio_device *dev;
Stewart Miles92854f52014-05-01 09:03:27 -0700175#if LOG_STREAMS_TO_FILES
176 int log_fd;
177#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700178};
179
180struct submix_stream_in {
181 struct audio_stream_in stream;
182 struct submix_audio_device *dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700183 bool output_standby; // output standby state as seen from record thread
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700184
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700185 // wall clock when recording starts
186 struct timespec record_start_time;
187 // how many frames have been requested to be read
188 int64_t read_counter_frames;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700189
190#if ENABLE_LEGACY_INPUT_OPEN
191 // Number of references to this input stream.
192 volatile int32_t ref_count;
193#endif // ENABLE_LEGACY_INPUT_OPEN
Stewart Miles92854f52014-05-01 09:03:27 -0700194#if LOG_STREAMS_TO_FILES
195 int log_fd;
196#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700197};
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700198
Stewart Miles70726842014-05-01 09:03:27 -0700199// Determine whether the specified sample rate is supported by the submix module.
200static bool sample_rate_supported(const uint32_t sample_rate)
201{
202 // Set of sample rates supported by Format_from_SR_C() frameworks/av/media/libnbaio/NAIO.cpp.
203 static const unsigned int supported_sample_rates[] = {
204 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
205 };
206 bool return_value;
207 SUBMIX_VALUE_IN_SET(sample_rate, supported_sample_rates, &return_value);
208 return return_value;
209}
210
211// Determine whether the specified sample rate is supported, if it is return the specified sample
212// rate, otherwise return the default sample rate for the submix module.
213static uint32_t get_supported_sample_rate(uint32_t sample_rate)
214{
215 return sample_rate_supported(sample_rate) ? sample_rate : DEFAULT_SAMPLE_RATE_HZ;
216}
217
218// Determine whether the specified channel in mask is supported by the submix module.
219static bool channel_in_mask_supported(const audio_channel_mask_t channel_in_mask)
220{
221 // Set of channel in masks supported by Format_from_SR_C()
222 // frameworks/av/media/libnbaio/NAIO.cpp.
223 static const audio_channel_mask_t supported_channel_in_masks[] = {
224 AUDIO_CHANNEL_IN_MONO, AUDIO_CHANNEL_IN_STEREO,
225 };
226 bool return_value;
227 SUBMIX_VALUE_IN_SET(channel_in_mask, supported_channel_in_masks, &return_value);
228 return return_value;
229}
230
231// Determine whether the specified channel in mask is supported, if it is return the specified
232// channel in mask, otherwise return the default channel in mask for the submix module.
233static audio_channel_mask_t get_supported_channel_in_mask(
234 const audio_channel_mask_t channel_in_mask)
235{
236 return channel_in_mask_supported(channel_in_mask) ? channel_in_mask :
237 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_IN_STEREO);
238}
239
240// Determine whether the specified channel out mask is supported by the submix module.
241static bool channel_out_mask_supported(const audio_channel_mask_t channel_out_mask)
242{
243 // Set of channel out masks supported by Format_from_SR_C()
244 // frameworks/av/media/libnbaio/NAIO.cpp.
245 static const audio_channel_mask_t supported_channel_out_masks[] = {
246 AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO,
247 };
248 bool return_value;
249 SUBMIX_VALUE_IN_SET(channel_out_mask, supported_channel_out_masks, &return_value);
250 return return_value;
251}
252
253// Determine whether the specified channel out mask is supported, if it is return the specified
254// channel out mask, otherwise return the default channel out mask for the submix module.
255static audio_channel_mask_t get_supported_channel_out_mask(
256 const audio_channel_mask_t channel_out_mask)
257{
258 return channel_out_mask_supported(channel_out_mask) ? channel_out_mask :
259 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_OUT_STEREO);
260}
261
Stewart Milesf645c5e2014-05-01 09:03:27 -0700262// Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the
263// structure.
264static struct submix_stream_out * audio_stream_out_get_submix_stream_out(
265 struct audio_stream_out * const stream)
266{
267 ALOG_ASSERT(stream);
268 return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) -
269 offsetof(struct submix_stream_out, stream));
270}
271
272// Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure.
273static struct submix_stream_out * audio_stream_get_submix_stream_out(
274 struct audio_stream * const stream)
275{
276 ALOG_ASSERT(stream);
277 return audio_stream_out_get_submix_stream_out(
278 reinterpret_cast<struct audio_stream_out *>(stream));
279}
280
281// Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the
282// structure.
283static struct submix_stream_in * audio_stream_in_get_submix_stream_in(
284 struct audio_stream_in * const stream)
285{
286 ALOG_ASSERT(stream);
287 return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) -
288 offsetof(struct submix_stream_in, stream));
289}
290
291// Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure.
292static struct submix_stream_in * audio_stream_get_submix_stream_in(
293 struct audio_stream * const stream)
294{
295 ALOG_ASSERT(stream);
296 return audio_stream_in_get_submix_stream_in(
297 reinterpret_cast<struct audio_stream_in *>(stream));
298}
299
300// Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within
301// the structure.
302static struct submix_audio_device * audio_hw_device_get_submix_audio_device(
303 struct audio_hw_device *device)
304{
305 ALOG_ASSERT(device);
306 return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) -
307 offsetof(struct submix_audio_device, device));
308}
309
Stewart Miles70726842014-05-01 09:03:27 -0700310// Compare an audio_config with input channel mask and an audio_config with output channel mask
311// returning false if they do *not* match, true otherwise.
312static bool audio_config_compare(const audio_config * const input_config,
313 const audio_config * const output_config)
314{
Stewart Milese54c12c2014-05-01 09:03:27 -0700315#if !ENABLE_CHANNEL_CONVERSION
Eric Laurentdd45fd32014-07-01 20:32:28 -0700316 const uint32_t input_channels = audio_channel_count_from_in_mask(input_config->channel_mask);
317 const uint32_t output_channels = audio_channel_count_from_out_mask(output_config->channel_mask);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700318 if (input_channels != output_channels) {
319 ALOGE("audio_config_compare() channel count mismatch input=%d vs. output=%d",
320 input_channels, output_channels);
Stewart Miles70726842014-05-01 09:03:27 -0700321 return false;
322 }
Stewart Milese54c12c2014-05-01 09:03:27 -0700323#endif // !ENABLE_CHANNEL_CONVERSION
Stewart Miles02c2f712014-05-01 09:03:27 -0700324#if ENABLE_RESAMPLING
325 if (input_config->sample_rate != output_config->sample_rate &&
Eric Laurentdd45fd32014-07-01 20:32:28 -0700326 audio_channel_count_from_in_mask(input_config->channel_mask) != 1) {
Stewart Miles02c2f712014-05-01 09:03:27 -0700327#else
Stewart Miles70726842014-05-01 09:03:27 -0700328 if (input_config->sample_rate != output_config->sample_rate) {
Stewart Miles02c2f712014-05-01 09:03:27 -0700329#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700330 ALOGE("audio_config_compare() sample rate mismatch %ul vs. %ul",
331 input_config->sample_rate, output_config->sample_rate);
332 return false;
333 }
334 if (input_config->format != output_config->format) {
335 ALOGE("audio_config_compare() format mismatch %x vs. %x",
336 input_config->format, output_config->format);
337 return false;
338 }
339 // This purposely ignores offload_info as it's not required for the submix device.
340 return true;
341}
342
Stewart Miles3dd36f92014-05-01 09:03:27 -0700343// If one doesn't exist, create a pipe for the submix audio device rsxadev of size
344// buffer_size_frames and optionally associate "in" or "out" with the submix audio device.
345static void submix_audio_device_create_pipe(struct submix_audio_device * const rsxadev,
346 const struct audio_config * const config,
347 const size_t buffer_size_frames,
348 const uint32_t buffer_period_count,
349 struct submix_stream_in * const in,
350 struct submix_stream_out * const out)
351{
352 ALOG_ASSERT(in || out);
353 ALOGV("submix_audio_device_create_pipe()");
354 pthread_mutex_lock(&rsxadev->lock);
355 // Save a reference to the specified input or output stream and the associated channel
356 // mask.
357 if (in) {
358 rsxadev->input = in;
359 rsxadev->config.input_channel_mask = config->channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700360#if ENABLE_RESAMPLING
361 rsxadev->config.input_sample_rate = config->sample_rate;
362 // If the output isn't configured yet, set the output sample rate to the maximum supported
363 // sample rate such that the smallest possible input buffer is created.
364 if (!rsxadev->output) {
365 rsxadev->config.output_sample_rate = 48000;
366 }
367#endif // ENABLE_RESAMPLING
Stewart Miles3dd36f92014-05-01 09:03:27 -0700368 }
369 if (out) {
370 rsxadev->output = out;
371 rsxadev->config.output_channel_mask = config->channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700372#if ENABLE_RESAMPLING
373 rsxadev->config.output_sample_rate = config->sample_rate;
374#endif // ENABLE_RESAMPLING
Stewart Miles3dd36f92014-05-01 09:03:27 -0700375 }
376 // If a pipe isn't associated with the device, create one.
377 if (rsxadev->rsxSink == NULL || rsxadev->rsxSource == NULL) {
378 struct submix_config * const device_config = &rsxadev->config;
Eric Laurentdd45fd32014-07-01 20:32:28 -0700379 uint32_t channel_count;
380 if (out)
381 channel_count = audio_channel_count_from_out_mask(config->channel_mask);
382 else
383 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Stewart Miles10f1a802014-06-09 20:54:37 -0700384#if ENABLE_CHANNEL_CONVERSION
385 // If channel conversion is enabled, allocate enough space for the maximum number of
386 // possible channels stored in the pipe for the situation when the number of channels in
387 // the output stream don't match the number in the input stream.
388 const uint32_t pipe_channel_count = max(channel_count, 2);
389#else
390 const uint32_t pipe_channel_count = channel_count;
391#endif // ENABLE_CHANNEL_CONVERSION
392 const NBAIO_Format format = Format_from_SR_C(config->sample_rate, pipe_channel_count,
393 config->format);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700394 const NBAIO_Format offers[1] = {format};
395 size_t numCounterOffers = 0;
396 // Create a MonoPipe with optional blocking set to true.
397 MonoPipe* sink = new MonoPipe(buffer_size_frames, format, true /*writeCanBlock*/);
398 // Negotiation between the source and sink cannot fail as the device open operation
399 // creates both ends of the pipe using the same audio format.
400 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
401 ALOG_ASSERT(index == 0);
402 MonoPipeReader* source = new MonoPipeReader(sink);
403 numCounterOffers = 0;
404 index = source->negotiate(offers, 1, NULL, numCounterOffers);
405 ALOG_ASSERT(index == 0);
406 ALOGV("submix_audio_device_create_pipe(): created pipe");
407
408 // Save references to the source and sink.
409 ALOG_ASSERT(rsxadev->rsxSink == NULL);
410 ALOG_ASSERT(rsxadev->rsxSource == NULL);
411 rsxadev->rsxSink = sink;
412 rsxadev->rsxSource = source;
413 // Store the sanitized audio format in the device so that it's possible to determine
414 // the format of the pipe source when opening the input device.
415 memcpy(&device_config->common, config, sizeof(device_config->common));
416 device_config->buffer_size_frames = sink->maxFrames();
417 device_config->buffer_period_size_frames = device_config->buffer_size_frames /
418 buffer_period_count;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700419 if (in) device_config->pipe_frame_size = audio_stream_in_frame_size(&in->stream);
420 if (out) device_config->pipe_frame_size = audio_stream_out_frame_size(&out->stream);
Stewart Miles10f1a802014-06-09 20:54:37 -0700421#if ENABLE_CHANNEL_CONVERSION
422 // Calculate the pipe frame size based upon the number of channels.
423 device_config->pipe_frame_size = (device_config->pipe_frame_size * pipe_channel_count) /
424 channel_count;
425#endif // ENABLE_CHANNEL_CONVERSION
Stewart Milese54c12c2014-05-01 09:03:27 -0700426 SUBMIX_ALOGV("submix_audio_device_create_pipe(): pipe frame size %zd, pipe size %zd, "
427 "period size %zd", device_config->pipe_frame_size,
428 device_config->buffer_size_frames, device_config->buffer_period_size_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700429 }
430 pthread_mutex_unlock(&rsxadev->lock);
431}
432
433// Release references to the sink and source. Input and output threads may maintain references
434// to these objects via StrongPointer (sp<MonoPipe> and sp<MonoPipeReader>) which they can use
435// before they shutdown.
436static void submix_audio_device_release_pipe(struct submix_audio_device * const rsxadev)
437{
438 ALOGV("submix_audio_device_release_pipe()");
439 rsxadev->rsxSink.clear();
440 rsxadev->rsxSource.clear();
441}
442
443// Remove references to the specified input and output streams. When the device no longer
444// references input and output streams destroy the associated pipe.
445static void submix_audio_device_destroy_pipe(struct submix_audio_device * const rsxadev,
446 const struct submix_stream_in * const in,
447 const struct submix_stream_out * const out)
448{
449 MonoPipe* sink;
450 pthread_mutex_lock(&rsxadev->lock);
451 ALOGV("submix_audio_device_destroy_pipe()");
452 ALOG_ASSERT(in == NULL || rsxadev->input == in);
453 ALOG_ASSERT(out == NULL || rsxadev->output == out);
454 if (in != NULL) {
455#if ENABLE_LEGACY_INPUT_OPEN
456 const_cast<struct submix_stream_in*>(in)->ref_count--;
457 if (in->ref_count == 0) {
458 rsxadev->input = NULL;
459 }
460 ALOGV("submix_audio_device_destroy_pipe(): input ref_count %d", in->ref_count);
461#else
462 rsxadev->input = NULL;
463#endif // ENABLE_LEGACY_INPUT_OPEN
464 }
465 if (out != NULL) rsxadev->output = NULL;
466 if (rsxadev->input != NULL && rsxadev->output != NULL) {
467 submix_audio_device_release_pipe(rsxadev);
468 ALOGV("submix_audio_device_destroy_pipe(): pipe destroyed");
469 }
470 pthread_mutex_unlock(&rsxadev->lock);
471}
472
Stewart Miles70726842014-05-01 09:03:27 -0700473// Sanitize the user specified audio config for a submix input / output stream.
474static void submix_sanitize_config(struct audio_config * const config, const bool is_input_format)
475{
476 config->channel_mask = is_input_format ? get_supported_channel_in_mask(config->channel_mask) :
477 get_supported_channel_out_mask(config->channel_mask);
478 config->sample_rate = get_supported_sample_rate(config->sample_rate);
479 config->format = DEFAULT_FORMAT;
480}
481
482// Verify a submix input or output stream can be opened.
483static bool submix_open_validate(const struct submix_audio_device * const rsxadev,
484 pthread_mutex_t * const lock,
485 const struct audio_config * const config,
486 const bool opening_input)
487{
Stewart Miles3dd36f92014-05-01 09:03:27 -0700488 bool input_open;
489 bool output_open;
Stewart Miles70726842014-05-01 09:03:27 -0700490 audio_config pipe_config;
491
492 // Query the device for the current audio config and whether input and output streams are open.
493 pthread_mutex_lock(lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700494 output_open = rsxadev->output != NULL;
495 input_open = rsxadev->input != NULL;
Stewart Miles70726842014-05-01 09:03:27 -0700496 memcpy(&pipe_config, &rsxadev->config.common, sizeof(pipe_config));
497 pthread_mutex_unlock(lock);
498
Stewart Miles3dd36f92014-05-01 09:03:27 -0700499 // If the stream is already open, don't open it again.
500 if (opening_input ? !ENABLE_LEGACY_INPUT_OPEN && input_open : output_open) {
501 ALOGE("submix_open_validate(): %s stream already open.", opening_input ? "Input" :
502 "Output");
503 return false;
504 }
505
506 SUBMIX_ALOGV("submix_open_validate(): sample rate=%d format=%x "
507 "%s_channel_mask=%x", config->sample_rate, config->format,
508 opening_input ? "in" : "out", config->channel_mask);
509
510 // If either stream is open, verify the existing audio config the pipe matches the user
Stewart Miles70726842014-05-01 09:03:27 -0700511 // specified config.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700512 if (input_open || output_open) {
Stewart Miles70726842014-05-01 09:03:27 -0700513 const audio_config * const input_config = opening_input ? config : &pipe_config;
514 const audio_config * const output_config = opening_input ? &pipe_config : config;
515 // Get the channel mask of the open device.
516 pipe_config.channel_mask =
517 opening_input ? rsxadev->config.output_channel_mask :
518 rsxadev->config.input_channel_mask;
519 if (!audio_config_compare(input_config, output_config)) {
520 ALOGE("submix_open_validate(): Unsupported format.");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700521 return false;
Stewart Miles70726842014-05-01 09:03:27 -0700522 }
523 }
524 return true;
525}
526
Stewart Milese54c12c2014-05-01 09:03:27 -0700527// Calculate the maximum size of the pipe buffer in frames for the specified stream.
528static size_t calculate_stream_pipe_size_in_frames(const struct audio_stream *stream,
529 const struct submix_config *config,
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700530 const size_t pipe_frames,
531 const size_t stream_frame_size)
Stewart Milese54c12c2014-05-01 09:03:27 -0700532{
Stewart Milese54c12c2014-05-01 09:03:27 -0700533 const size_t pipe_frame_size = config->pipe_frame_size;
534 const size_t max_frame_size = max(stream_frame_size, pipe_frame_size);
535 return (pipe_frames * config->pipe_frame_size) / max_frame_size;
536}
537
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700538/* audio HAL functions */
539
540static uint32_t out_get_sample_rate(const struct audio_stream *stream)
541{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700542 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
543 const_cast<struct audio_stream *>(stream));
Stewart Miles02c2f712014-05-01 09:03:27 -0700544#if ENABLE_RESAMPLING
545 const uint32_t out_rate = out->dev->config.output_sample_rate;
546#else
Stewart Miles70726842014-05-01 09:03:27 -0700547 const uint32_t out_rate = out->dev->config.common.sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700548#endif // ENABLE_RESAMPLING
Stewart Milesc049a0a2014-05-01 09:03:27 -0700549 SUBMIX_ALOGV("out_get_sample_rate() returns %u", out_rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700550 return out_rate;
551}
552
553static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
554{
Stewart Miles02c2f712014-05-01 09:03:27 -0700555 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
556#if ENABLE_RESAMPLING
557 // The sample rate of the stream can't be changed once it's set since this would change the
558 // output buffer size and hence break playback to the shared pipe.
559 if (rate != out->dev->config.output_sample_rate) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700560 ALOGE("out_set_sample_rate() resampling enabled can't change sample rate from "
Stewart Miles02c2f712014-05-01 09:03:27 -0700561 "%u to %u", out->dev->config.output_sample_rate, rate);
562 return -ENOSYS;
563 }
564#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700565 if (!sample_rate_supported(rate)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700566 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
567 return -ENOSYS;
568 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700569 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Stewart Miles70726842014-05-01 09:03:27 -0700570 out->dev->config.common.sample_rate = rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700571 return 0;
572}
573
574static size_t out_get_buffer_size(const struct audio_stream *stream)
575{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700576 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
577 const_cast<struct audio_stream *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700578 const struct submix_config * const config = &out->dev->config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700579 const size_t stream_frame_size =
580 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700581 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700582 stream, config, config->buffer_period_size_frames, stream_frame_size);
583 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Miles568e66f2014-05-01 09:03:27 -0700584 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
Stewart Milese54c12c2014-05-01 09:03:27 -0700585 buffer_size_bytes, buffer_size_frames);
586 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700587}
588
589static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
590{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700591 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
592 const_cast<struct audio_stream *>(stream));
Stewart Miles70726842014-05-01 09:03:27 -0700593 uint32_t channel_mask = out->dev->config.output_channel_mask;
Stewart Miles568e66f2014-05-01 09:03:27 -0700594 SUBMIX_ALOGV("out_get_channels() returns %08x", channel_mask);
595 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700596}
597
598static audio_format_t out_get_format(const struct audio_stream *stream)
599{
Stewart Miles568e66f2014-05-01 09:03:27 -0700600 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
601 const_cast<struct audio_stream *>(stream));
Stewart Miles70726842014-05-01 09:03:27 -0700602 const audio_format_t format = out->dev->config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700603 SUBMIX_ALOGV("out_get_format() returns %x", format);
604 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700605}
606
607static int out_set_format(struct audio_stream *stream, audio_format_t format)
608{
Stewart Miles568e66f2014-05-01 09:03:27 -0700609 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Stewart Miles70726842014-05-01 09:03:27 -0700610 if (format != out->dev->config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700611 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700612 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700613 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700614 SUBMIX_ALOGV("out_set_format(format=%x)", format);
615 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700616}
617
618static int out_standby(struct audio_stream *stream)
619{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700620 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_out(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700621 ALOGI("out_standby()");
622
Stewart Milesf645c5e2014-05-01 09:03:27 -0700623 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700624
Stewart Milesf645c5e2014-05-01 09:03:27 -0700625 rsxadev->output_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700626
Stewart Milesf645c5e2014-05-01 09:03:27 -0700627 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700628
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700629 return 0;
630}
631
632static int out_dump(const struct audio_stream *stream, int fd)
633{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700634 (void)stream;
635 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700636 return 0;
637}
638
639static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
640{
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700641 int exiting = -1;
642 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700643 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700644
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700645 // FIXME this is using hard-coded strings but in the future, this functionality will be
646 // converted to use audio HAL extensions required to support tunneling
647 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700648 struct submix_audio_device * const rsxadev =
649 audio_stream_get_submix_stream_out(stream)->dev;
650 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800651 { // using the sink
Stewart Miles3dd36f92014-05-01 09:03:27 -0700652 sp<MonoPipe> sink = rsxadev->rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700653 if (sink == NULL) {
654 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800655 return 0;
656 }
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700657
Stewart Milesc049a0a2014-05-01 09:03:27 -0700658 ALOGI("out_set_parameters(): shutdown");
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800659 sink->shutdown(true);
660 } // done using the sink
Stewart Milesf645c5e2014-05-01 09:03:27 -0700661 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700662 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700663 return 0;
664}
665
666static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
667{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700668 (void)stream;
669 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700670 return strdup("");
671}
672
673static uint32_t out_get_latency(const struct audio_stream_out *stream)
674{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700675 const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
676 const_cast<struct audio_stream_out *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700677 const struct submix_config * const config = &out->dev->config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700678 const size_t stream_frame_size =
679 audio_stream_out_frame_size(stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700680 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700681 &stream->common, config, config->buffer_size_frames, stream_frame_size);
Stewart Miles10f1a802014-06-09 20:54:37 -0700682 const uint32_t sample_rate = out_get_sample_rate(&stream->common);
683 const uint32_t latency_ms = (buffer_size_frames * 1000) / sample_rate;
Stewart Milese54c12c2014-05-01 09:03:27 -0700684 SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u",
Stewart Miles10f1a802014-06-09 20:54:37 -0700685 latency_ms, buffer_size_frames, sample_rate);
Stewart Miles568e66f2014-05-01 09:03:27 -0700686 return latency_ms;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700687}
688
689static int out_set_volume(struct audio_stream_out *stream, float left,
690 float right)
691{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700692 (void)stream;
693 (void)left;
694 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700695 return -ENOSYS;
696}
697
698static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
699 size_t bytes)
700{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700701 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700702 ssize_t written_frames = 0;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700703 const size_t frame_size = audio_stream_out_frame_size(stream);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700704 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
705 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700706 const size_t frames = bytes / frame_size;
707
Stewart Milesf645c5e2014-05-01 09:03:27 -0700708 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700709
Stewart Milesf645c5e2014-05-01 09:03:27 -0700710 rsxadev->output_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700711
Stewart Miles3dd36f92014-05-01 09:03:27 -0700712 sp<MonoPipe> sink = rsxadev->rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700713 if (sink != NULL) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700714 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800715 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700716 pthread_mutex_unlock(&rsxadev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700717 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700718 // the pipe has already been shutdown, this buffer will be lost but we must
719 // simulate timing so we don't drain the output faster than realtime
720 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
721 return bytes;
722 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700723 } else {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700724 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700725 ALOGE("out_write without a pipe!");
726 ALOG_ASSERT("out_write without a pipe!");
727 return 0;
728 }
729
Stewart Miles2d199fe2014-05-01 09:03:27 -0700730 // If the write to the sink would block when no input stream is present, flush enough frames
731 // from the pipe to make space to write the most recent data.
732 {
733 const size_t availableToWrite = sink->availableToWrite();
734 sp<MonoPipeReader> source = rsxadev->rsxSource;
735 if (rsxadev->input == NULL && availableToWrite < frames) {
736 static uint8_t flush_buffer[64];
737 const size_t flushBufferSizeFrames = sizeof(flush_buffer) / frame_size;
738 size_t frames_to_flush_from_source = frames - availableToWrite;
739 SUBMIX_ALOGV("out_write(): flushing %d frames from the pipe to avoid blocking",
740 frames_to_flush_from_source);
741 while (frames_to_flush_from_source) {
742 const size_t flush_size = min(frames_to_flush_from_source, flushBufferSizeFrames);
743 frames_to_flush_from_source -= flush_size;
744 source->read(flush_buffer, flush_size, AudioBufferProvider::kInvalidPTS);
745 }
746 }
747 }
748
Stewart Milesf645c5e2014-05-01 09:03:27 -0700749 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700750
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700751 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800752
Stewart Miles92854f52014-05-01 09:03:27 -0700753#if LOG_STREAMS_TO_FILES
754 if (out->log_fd >= 0) write(out->log_fd, buffer, written_frames * frame_size);
755#endif // LOG_STREAMS_TO_FILES
756
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700757 if (written_frames < 0) {
758 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700759 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700760
Stewart Milesf645c5e2014-05-01 09:03:27 -0700761 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800762 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700763 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700764
765 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700766 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700767 } else {
768 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700769 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700770 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700771 }
772 }
773
Stewart Milesf645c5e2014-05-01 09:03:27 -0700774 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800775 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700776 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700777
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700778 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700779 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700780 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700781 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700782 const ssize_t written_bytes = written_frames * frame_size;
Stewart Miles02c2f712014-05-01 09:03:27 -0700783 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames", written_bytes, written_frames);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700784 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700785}
786
787static int out_get_render_position(const struct audio_stream_out *stream,
788 uint32_t *dsp_frames)
789{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700790 (void)stream;
791 (void)dsp_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700792 return -EINVAL;
793}
794
795static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
796{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700797 (void)stream;
798 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700799 return 0;
800}
801
802static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
803{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700804 (void)stream;
805 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700806 return 0;
807}
808
809static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
810 int64_t *timestamp)
811{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700812 (void)stream;
813 (void)timestamp;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700814 return -EINVAL;
815}
816
817/** audio_stream_in implementation **/
818static uint32_t in_get_sample_rate(const struct audio_stream *stream)
819{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700820 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
821 const_cast<struct audio_stream*>(stream));
Stewart Miles02c2f712014-05-01 09:03:27 -0700822#if ENABLE_RESAMPLING
823 const uint32_t rate = in->dev->config.input_sample_rate;
824#else
825 const uint32_t rate = in->dev->config.common.sample_rate;
826#endif // ENABLE_RESAMPLING
827 SUBMIX_ALOGV("in_get_sample_rate() returns %u", rate);
828 return rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700829}
830
831static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
832{
Stewart Miles568e66f2014-05-01 09:03:27 -0700833 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Stewart Miles02c2f712014-05-01 09:03:27 -0700834#if ENABLE_RESAMPLING
835 // The sample rate of the stream can't be changed once it's set since this would change the
836 // input buffer size and hence break recording from the shared pipe.
837 if (rate != in->dev->config.input_sample_rate) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700838 ALOGE("in_set_sample_rate() resampling enabled can't change sample rate from "
Stewart Miles02c2f712014-05-01 09:03:27 -0700839 "%u to %u", in->dev->config.input_sample_rate, rate);
840 return -ENOSYS;
841 }
842#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700843 if (!sample_rate_supported(rate)) {
844 ALOGE("in_set_sample_rate(rate=%u) rate unsupported", rate);
845 return -ENOSYS;
846 }
847 in->dev->config.common.sample_rate = rate;
Stewart Miles568e66f2014-05-01 09:03:27 -0700848 SUBMIX_ALOGV("in_set_sample_rate() set %u", rate);
849 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700850}
851
852static size_t in_get_buffer_size(const struct audio_stream *stream)
853{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700854 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
855 const_cast<struct audio_stream*>(stream));
Stewart Milese54c12c2014-05-01 09:03:27 -0700856 const struct submix_config * const config = &in->dev->config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700857 const size_t stream_frame_size =
858 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Stewart Miles02c2f712014-05-01 09:03:27 -0700859 size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700860 stream, config, config->buffer_period_size_frames, stream_frame_size);
Stewart Miles02c2f712014-05-01 09:03:27 -0700861#if ENABLE_RESAMPLING
862 // Scale the size of the buffer based upon the maximum number of frames that could be returned
863 // given the ratio of output to input sample rate.
864 buffer_size_frames = (size_t)(((float)buffer_size_frames *
865 (float)config->input_sample_rate) /
866 (float)config->output_sample_rate);
867#endif // ENABLE_RESAMPLING
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700868 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Milese54c12c2014-05-01 09:03:27 -0700869 SUBMIX_ALOGV("in_get_buffer_size() returns %zu bytes, %zu frames", buffer_size_bytes,
870 buffer_size_frames);
871 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700872}
873
874static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
875{
Stewart Miles70726842014-05-01 09:03:27 -0700876 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
877 const_cast<struct audio_stream*>(stream));
878 const audio_channel_mask_t channel_mask = in->dev->config.input_channel_mask;
879 SUBMIX_ALOGV("in_get_channels() returns %x", channel_mask);
880 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700881}
882
883static audio_format_t in_get_format(const struct audio_stream *stream)
884{
Stewart Miles568e66f2014-05-01 09:03:27 -0700885 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
Stewart Miles70726842014-05-01 09:03:27 -0700886 const_cast<struct audio_stream*>(stream));
887 const audio_format_t format = in->dev->config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700888 SUBMIX_ALOGV("in_get_format() returns %x", format);
889 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700890}
891
892static int in_set_format(struct audio_stream *stream, audio_format_t format)
893{
Stewart Miles568e66f2014-05-01 09:03:27 -0700894 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Stewart Miles70726842014-05-01 09:03:27 -0700895 if (format != in->dev->config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700896 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700897 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700898 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700899 SUBMIX_ALOGV("in_set_format(format=%x)", format);
900 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700901}
902
903static int in_standby(struct audio_stream *stream)
904{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700905 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_in(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700906 ALOGI("in_standby()");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700907
Stewart Milesf645c5e2014-05-01 09:03:27 -0700908 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700909
Stewart Milesf645c5e2014-05-01 09:03:27 -0700910 rsxadev->input_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700911
Stewart Milesf645c5e2014-05-01 09:03:27 -0700912 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700913
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700914 return 0;
915}
916
917static int in_dump(const struct audio_stream *stream, int fd)
918{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700919 (void)stream;
920 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700921 return 0;
922}
923
924static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
925{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700926 (void)stream;
927 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700928 return 0;
929}
930
931static char * in_get_parameters(const struct audio_stream *stream,
932 const char *keys)
933{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700934 (void)stream;
935 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700936 return strdup("");
937}
938
939static int in_set_gain(struct audio_stream_in *stream, float gain)
940{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700941 (void)stream;
942 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700943 return 0;
944}
945
946static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
947 size_t bytes)
948{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700949 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
950 struct submix_audio_device * const rsxadev = in->dev;
Stewart Milese54c12c2014-05-01 09:03:27 -0700951 struct audio_config *format;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700952 const size_t frame_size = audio_stream_in_frame_size(stream);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700953 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700954
Stewart Milesc049a0a2014-05-01 09:03:27 -0700955 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700956 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700957
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700958 const bool output_standby_transition = (in->output_standby != in->dev->output_standby);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700959 in->output_standby = rsxadev->output_standby;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700960
Stewart Milesf645c5e2014-05-01 09:03:27 -0700961 if (rsxadev->input_standby || output_standby_transition) {
962 rsxadev->input_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700963 // keep track of when we exit input standby (== first read == start "real recording")
964 // or when we start recording silence, and reset projected time
965 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
966 if (rc == 0) {
967 in->read_counter_frames = 0;
968 }
969 }
970
971 in->read_counter_frames += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700972 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800973
974 {
975 // about to read from audio source
Stewart Milesf645c5e2014-05-01 09:03:27 -0700976 sp<MonoPipeReader> source = rsxadev->rsxSource;
977 if (source == NULL) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800978 ALOGE("no audio pipe yet we're trying to read!");
Stewart Milesf645c5e2014-05-01 09:03:27 -0700979 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700980 usleep(frames_to_read * 1000000 / in_get_sample_rate(&stream->common));
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800981 memset(buffer, 0, bytes);
982 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700983 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800984
Stewart Milesf645c5e2014-05-01 09:03:27 -0700985 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800986
987 // read the data from the pipe (it's non blocking)
988 int attempts = 0;
989 char* buff = (char*)buffer;
Stewart Milese54c12c2014-05-01 09:03:27 -0700990#if ENABLE_CHANNEL_CONVERSION
991 // Determine whether channel conversion is required.
Eric Laurentdd45fd32014-07-01 20:32:28 -0700992 const uint32_t input_channels = audio_channel_count_from_in_mask(
Stewart Milese54c12c2014-05-01 09:03:27 -0700993 rsxadev->config.input_channel_mask);
Eric Laurentdd45fd32014-07-01 20:32:28 -0700994 const uint32_t output_channels = audio_channel_count_from_out_mask(
Stewart Milese54c12c2014-05-01 09:03:27 -0700995 rsxadev->config.output_channel_mask);
996 if (input_channels != output_channels) {
997 SUBMIX_ALOGV("in_read(): %d output channels will be converted to %d "
998 "input channels", output_channels, input_channels);
999 // Only support 16-bit PCM channel conversion from mono to stereo or stereo to mono.
1000 ALOG_ASSERT(rsxadev->config.common.format == AUDIO_FORMAT_PCM_16_BIT);
1001 ALOG_ASSERT((input_channels == 1 && output_channels == 2) ||
1002 (input_channels == 2 && output_channels == 1));
1003 }
1004#endif // ENABLE_CHANNEL_CONVERSION
1005
Stewart Miles02c2f712014-05-01 09:03:27 -07001006#if ENABLE_RESAMPLING
1007 const uint32_t input_sample_rate = in_get_sample_rate(&stream->common);
1008 const uint32_t output_sample_rate = rsxadev->config.output_sample_rate;
1009 const size_t resampler_buffer_size_frames =
1010 sizeof(rsxadev->resampler_buffer) / sizeof(rsxadev->resampler_buffer[0]);
1011 float resampler_ratio = 1.0f;
1012 // Determine whether resampling is required.
1013 if (input_sample_rate != output_sample_rate) {
1014 resampler_ratio = (float)output_sample_rate / (float)input_sample_rate;
1015 // Only support 16-bit PCM mono resampling.
1016 // NOTE: Resampling is performed after the channel conversion step.
1017 ALOG_ASSERT(rsxadev->config.common.format == AUDIO_FORMAT_PCM_16_BIT);
Eric Laurentdd45fd32014-07-01 20:32:28 -07001018 ALOG_ASSERT(audio_channel_count_from_in_mask(rsxadev->config.input_channel_mask) == 1);
Stewart Miles02c2f712014-05-01 09:03:27 -07001019 }
1020#endif // ENABLE_RESAMPLING
1021
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001022 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
Stewart Miles02c2f712014-05-01 09:03:27 -07001023 ssize_t frames_read = -1977;
Stewart Milese54c12c2014-05-01 09:03:27 -07001024 size_t read_frames = remaining_frames;
Stewart Miles02c2f712014-05-01 09:03:27 -07001025#if ENABLE_RESAMPLING
1026 char* const saved_buff = buff;
1027 if (resampler_ratio != 1.0f) {
1028 // Calculate the number of frames from the pipe that need to be read to generate
1029 // the data for the input stream read.
1030 const size_t frames_required_for_resampler = (size_t)(
1031 (float)read_frames * (float)resampler_ratio);
1032 read_frames = min(frames_required_for_resampler, resampler_buffer_size_frames);
1033 // Read into the resampler buffer.
1034 buff = (char*)rsxadev->resampler_buffer;
1035 }
1036#endif // ENABLE_RESAMPLING
Stewart Milese54c12c2014-05-01 09:03:27 -07001037#if ENABLE_CHANNEL_CONVERSION
1038 if (output_channels == 1 && input_channels == 2) {
1039 // Need to read half the requested frames since the converted output
1040 // data will take twice the space (mono->stereo).
1041 read_frames /= 2;
1042 }
1043#endif // ENABLE_CHANNEL_CONVERSION
1044
1045 SUBMIX_ALOGV("in_read(): frames available to read %zd", source->availableToRead());
1046
1047 frames_read = source->read(buff, read_frames, AudioBufferProvider::kInvalidPTS);
1048
1049 SUBMIX_ALOGV("in_read(): frames read %zd", frames_read);
1050
1051#if ENABLE_CHANNEL_CONVERSION
1052 // Perform in-place channel conversion.
1053 // NOTE: In the following "input stream" refers to the data returned by this function
1054 // and "output stream" refers to the data read from the pipe.
1055 if (input_channels != output_channels && frames_read > 0) {
1056 int16_t *data = (int16_t*)buff;
1057 if (output_channels == 2 && input_channels == 1) {
1058 // Offset into the output stream data in samples.
1059 ssize_t output_stream_offset = 0;
1060 for (ssize_t input_stream_frame = 0; input_stream_frame < frames_read;
1061 input_stream_frame++, output_stream_offset += 2) {
1062 // Average the content from both channels.
1063 data[input_stream_frame] = ((int32_t)data[output_stream_offset] +
1064 (int32_t)data[output_stream_offset + 1]) / 2;
1065 }
1066 } else if (output_channels == 1 && input_channels == 2) {
1067 // Offset into the input stream data in samples.
1068 ssize_t input_stream_offset = (frames_read - 1) * 2;
1069 for (ssize_t output_stream_frame = frames_read - 1; output_stream_frame >= 0;
1070 output_stream_frame--, input_stream_offset -= 2) {
1071 const short sample = data[output_stream_frame];
1072 data[input_stream_offset] = sample;
1073 data[input_stream_offset + 1] = sample;
1074 }
1075 }
1076 }
1077#endif // ENABLE_CHANNEL_CONVERSION
Stewart Miles3dd36f92014-05-01 09:03:27 -07001078
Stewart Miles02c2f712014-05-01 09:03:27 -07001079#if ENABLE_RESAMPLING
1080 if (resampler_ratio != 1.0f) {
1081 SUBMIX_ALOGV("in_read(): resampling %zd frames", frames_read);
1082 const int16_t * const data = (int16_t*)buff;
1083 int16_t * const resampled_buffer = (int16_t*)saved_buff;
1084 // Resample with *no* filtering - if the data from the ouptut stream was really
1085 // sampled at a different rate this will result in very nasty aliasing.
1086 const float output_stream_frames = (float)frames_read;
1087 size_t input_stream_frame = 0;
1088 for (float output_stream_frame = 0.0f;
1089 output_stream_frame < output_stream_frames &&
1090 input_stream_frame < remaining_frames;
1091 output_stream_frame += resampler_ratio, input_stream_frame++) {
1092 resampled_buffer[input_stream_frame] = data[(size_t)output_stream_frame];
1093 }
1094 ALOG_ASSERT(input_stream_frame <= (ssize_t)resampler_buffer_size_frames);
1095 SUBMIX_ALOGV("in_read(): resampler produced %zd frames", input_stream_frame);
1096 frames_read = input_stream_frame;
1097 buff = saved_buff;
1098 }
1099#endif // ENABLE_RESAMPLING
1100
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001101 if (frames_read > 0) {
Stewart Miles92854f52014-05-01 09:03:27 -07001102#if LOG_STREAMS_TO_FILES
1103 if (in->log_fd >= 0) write(in->log_fd, buff, frames_read * frame_size);
1104#endif // LOG_STREAMS_TO_FILES
1105
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001106 remaining_frames -= frames_read;
1107 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001108 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
1109 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001110 } else {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001111 attempts++;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001112 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001113 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
1114 }
1115 }
1116 // done using the source
Stewart Milesf645c5e2014-05-01 09:03:27 -07001117 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001118 source.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -07001119 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001120 }
1121
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001122 if (remaining_frames > 0) {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001123 const size_t remaining_bytes = remaining_frames * frame_size;
Stewart Miles10f1a802014-06-09 20:54:37 -07001124 SUBMIX_ALOGV(" clearing remaining_frames = %zu", remaining_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001125 memset(((char*)buffer)+ bytes - remaining_bytes, 0, remaining_bytes);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001126 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001127
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001128 // compute how much we need to sleep after reading the data by comparing the wall clock with
1129 // the projected time at which we should return.
1130 struct timespec time_after_read;// wall clock after reading from the pipe
1131 struct timespec record_duration;// observed record duration
1132 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
1133 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
1134 if (rc == 0) {
1135 // for how long have we been recording?
1136 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
1137 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
1138 if (record_duration.tv_nsec < 0) {
1139 record_duration.tv_sec--;
1140 record_duration.tv_nsec += 1000000000;
1141 }
1142
Stewart Milesf645c5e2014-05-01 09:03:27 -07001143 // read_counter_frames contains the number of frames that have been read since the
1144 // beginning of recording (including this call): it's converted to usec and compared to
1145 // how long we've been recording for, which gives us how long we must wait to sync the
1146 // projected recording time, and the observed recording time.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001147 long projected_vs_observed_offset_us =
1148 ((int64_t)(in->read_counter_frames
1149 - (record_duration.tv_sec*sample_rate)))
1150 * 1000000 / sample_rate
1151 - (record_duration.tv_nsec / 1000);
1152
Stewart Milesc049a0a2014-05-01 09:03:27 -07001153 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001154 record_duration.tv_sec, record_duration.tv_nsec/1000000,
1155 projected_vs_observed_offset_us);
1156 if (projected_vs_observed_offset_us > 0) {
1157 usleep(projected_vs_observed_offset_us);
1158 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001159 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001160
Stewart Milesc049a0a2014-05-01 09:03:27 -07001161 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001162 return bytes;
1163
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001164}
1165
1166static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1167{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001168 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001169 return 0;
1170}
1171
1172static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1173{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001174 (void)stream;
1175 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001176 return 0;
1177}
1178
1179static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1180{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001181 (void)stream;
1182 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001183 return 0;
1184}
1185
1186static int adev_open_output_stream(struct audio_hw_device *dev,
1187 audio_io_handle_t handle,
1188 audio_devices_t devices,
1189 audio_output_flags_t flags,
1190 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -07001191 struct audio_stream_out **stream_out,
1192 const char *address __unused)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001193{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001194 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001195 ALOGV("adev_open_output_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001196 struct submix_stream_out *out;
Stewart Miles10f1a802014-06-09 20:54:37 -07001197 bool force_pipe_creation = false;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001198 (void)handle;
1199 (void)devices;
1200 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001201
Stewart Miles3dd36f92014-05-01 09:03:27 -07001202 *stream_out = NULL;
1203
Stewart Miles70726842014-05-01 09:03:27 -07001204 // Make sure it's possible to open the device given the current audio config.
1205 submix_sanitize_config(config, false);
1206 if (!submix_open_validate(rsxadev, &rsxadev->lock, config, false)) {
1207 ALOGE("adev_open_output_stream(): Unable to open output stream.");
1208 return -EINVAL;
1209 }
1210
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001211 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
Stewart Miles3dd36f92014-05-01 09:03:27 -07001212 if (!out) return -ENOMEM;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001213
Stewart Miles568e66f2014-05-01 09:03:27 -07001214 // Initialize the function pointer tables (v-tables).
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001215 out->stream.common.get_sample_rate = out_get_sample_rate;
1216 out->stream.common.set_sample_rate = out_set_sample_rate;
1217 out->stream.common.get_buffer_size = out_get_buffer_size;
1218 out->stream.common.get_channels = out_get_channels;
1219 out->stream.common.get_format = out_get_format;
1220 out->stream.common.set_format = out_set_format;
1221 out->stream.common.standby = out_standby;
1222 out->stream.common.dump = out_dump;
1223 out->stream.common.set_parameters = out_set_parameters;
1224 out->stream.common.get_parameters = out_get_parameters;
1225 out->stream.common.add_audio_effect = out_add_audio_effect;
1226 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1227 out->stream.get_latency = out_get_latency;
1228 out->stream.set_volume = out_set_volume;
1229 out->stream.write = out_write;
1230 out->stream.get_render_position = out_get_render_position;
1231 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1232
Stewart Miles10f1a802014-06-09 20:54:37 -07001233#if ENABLE_RESAMPLING
1234 // Recreate the pipe with the correct sample rate so that MonoPipe.write() rate limits
1235 // writes correctly.
1236 force_pipe_creation = rsxadev->config.common.sample_rate != config->sample_rate;
1237#endif // ENABLE_RESAMPLING
1238
1239 // If the sink has been shutdown or pipe recreation is forced (see above), delete the pipe so
1240 // that it's recreated.
Stewart Miles3dd36f92014-05-01 09:03:27 -07001241 pthread_mutex_lock(&rsxadev->lock);
Stewart Miles10f1a802014-06-09 20:54:37 -07001242 if ((rsxadev->rsxSink != NULL && rsxadev->rsxSink->isShutdown()) || force_pipe_creation) {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001243 submix_audio_device_release_pipe(rsxadev);
1244 }
1245 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001246
Stewart Miles568e66f2014-05-01 09:03:27 -07001247 // Store a pointer to the device from the output stream.
1248 out->dev = rsxadev;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001249 // Initialize the pipe.
1250 ALOGV("adev_open_output_stream(): Initializing pipe");
1251 submix_audio_device_create_pipe(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1252 DEFAULT_PIPE_PERIOD_COUNT, NULL, out);
Stewart Miles92854f52014-05-01 09:03:27 -07001253#if LOG_STREAMS_TO_FILES
1254 out->log_fd = open(LOG_STREAM_OUT_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1255 LOG_STREAM_FILE_PERMISSIONS);
1256 ALOGE_IF(out->log_fd < 0, "adev_open_output_stream(): log file open failed %s",
1257 strerror(errno));
1258 ALOGV("adev_open_output_stream(): log_fd = %d", out->log_fd);
1259#endif // LOG_STREAMS_TO_FILES
Stewart Miles568e66f2014-05-01 09:03:27 -07001260 // Return the output stream.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001261 *stream_out = &out->stream;
1262
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001263 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001264}
1265
1266static void adev_close_output_stream(struct audio_hw_device *dev,
1267 struct audio_stream_out *stream)
1268{
Stewart Miles3dd36f92014-05-01 09:03:27 -07001269 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001270 ALOGV("adev_close_output_stream()");
Stewart Miles3dd36f92014-05-01 09:03:27 -07001271 submix_audio_device_destroy_pipe(audio_hw_device_get_submix_audio_device(dev), NULL, out);
Stewart Miles92854f52014-05-01 09:03:27 -07001272#if LOG_STREAMS_TO_FILES
1273 if (out->log_fd >= 0) close(out->log_fd);
1274#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001275 free(out);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001276}
1277
1278static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1279{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001280 (void)dev;
1281 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001282 return -ENOSYS;
1283}
1284
1285static char * adev_get_parameters(const struct audio_hw_device *dev,
1286 const char *keys)
1287{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001288 (void)dev;
1289 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001290 return strdup("");;
1291}
1292
1293static int adev_init_check(const struct audio_hw_device *dev)
1294{
1295 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -07001296 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001297 return 0;
1298}
1299
1300static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1301{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001302 (void)dev;
1303 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001304 return -ENOSYS;
1305}
1306
1307static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1308{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001309 (void)dev;
1310 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001311 return -ENOSYS;
1312}
1313
1314static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
1315{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001316 (void)dev;
1317 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001318 return -ENOSYS;
1319}
1320
1321static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1322{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001323 (void)dev;
1324 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001325 return -ENOSYS;
1326}
1327
1328static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1329{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001330 (void)dev;
1331 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001332 return -ENOSYS;
1333}
1334
1335static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1336{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001337 (void)dev;
1338 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001339 return 0;
1340}
1341
1342static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1343{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001344 (void)dev;
1345 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001346 return -ENOSYS;
1347}
1348
1349static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1350{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001351 (void)dev;
1352 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001353 return -ENOSYS;
1354}
1355
1356static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1357 const struct audio_config *config)
1358{
Stewart Miles568e66f2014-05-01 09:03:27 -07001359 if (audio_is_linear_pcm(config->format)) {
1360 const size_t buffer_period_size_frames =
1361 audio_hw_device_get_submix_audio_device(const_cast<struct audio_hw_device*>(dev))->
Stewart Miles3dd36f92014-05-01 09:03:27 -07001362 config.buffer_period_size_frames;
Eric Laurentdd45fd32014-07-01 20:32:28 -07001363 const size_t frame_size_in_bytes = audio_channel_count_from_in_mask(config->channel_mask) *
Stewart Miles568e66f2014-05-01 09:03:27 -07001364 audio_bytes_per_sample(config->format);
1365 const size_t buffer_size = buffer_period_size_frames * frame_size_in_bytes;
Stewart Miles10f1a802014-06-09 20:54:37 -07001366 SUBMIX_ALOGV("adev_get_input_buffer_size() returns %zu bytes, %zu frames",
Stewart Miles568e66f2014-05-01 09:03:27 -07001367 buffer_size, buffer_period_size_frames);
1368 return buffer_size;
1369 }
1370 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001371}
1372
1373static int adev_open_input_stream(struct audio_hw_device *dev,
1374 audio_io_handle_t handle,
1375 audio_devices_t devices,
1376 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -07001377 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001378 audio_input_flags_t flags __unused,
1379 const char *address __unused,
1380 audio_source_t source __unused)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001381{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001382 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001383 struct submix_stream_in *in;
Stewart Miles568e66f2014-05-01 09:03:27 -07001384 ALOGI("adev_open_input_stream()");
Stewart Milesc049a0a2014-05-01 09:03:27 -07001385 (void)handle;
1386 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001387
Stewart Miles3dd36f92014-05-01 09:03:27 -07001388 *stream_in = NULL;
1389
Stewart Miles70726842014-05-01 09:03:27 -07001390 // Make sure it's possible to open the device given the current audio config.
1391 submix_sanitize_config(config, true);
1392 if (!submix_open_validate(rsxadev, &rsxadev->lock, config, true)) {
1393 ALOGE("adev_open_input_stream(): Unable to open input stream.");
1394 return -EINVAL;
1395 }
1396
Stewart Miles3dd36f92014-05-01 09:03:27 -07001397#if ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001398 pthread_mutex_lock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001399 in = rsxadev->input;
1400 if (in) {
1401 in->ref_count++;
1402 sp<MonoPipe> sink = rsxadev->rsxSink;
1403 ALOG_ASSERT(sink != NULL);
1404 // If the sink has been shutdown, delete the pipe.
1405 if (sink->isShutdown()) submix_audio_device_release_pipe(rsxadev);
1406 }
1407 pthread_mutex_unlock(&rsxadev->lock);
1408#else
1409 in = NULL;
1410#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001411
Stewart Miles3dd36f92014-05-01 09:03:27 -07001412 if (!in) {
1413 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
1414 if (!in) return -ENOMEM;
1415 in->ref_count = 1;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001416
Stewart Miles3dd36f92014-05-01 09:03:27 -07001417 // Initialize the function pointer tables (v-tables).
1418 in->stream.common.get_sample_rate = in_get_sample_rate;
1419 in->stream.common.set_sample_rate = in_set_sample_rate;
1420 in->stream.common.get_buffer_size = in_get_buffer_size;
1421 in->stream.common.get_channels = in_get_channels;
1422 in->stream.common.get_format = in_get_format;
1423 in->stream.common.set_format = in_set_format;
1424 in->stream.common.standby = in_standby;
1425 in->stream.common.dump = in_dump;
1426 in->stream.common.set_parameters = in_set_parameters;
1427 in->stream.common.get_parameters = in_get_parameters;
1428 in->stream.common.add_audio_effect = in_add_audio_effect;
1429 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1430 in->stream.set_gain = in_set_gain;
1431 in->stream.read = in_read;
1432 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1433 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001434
Stewart Miles568e66f2014-05-01 09:03:27 -07001435 // Initialize the input stream.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001436 in->read_counter_frames = 0;
1437 in->output_standby = rsxadev->output_standby;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001438 in->dev = rsxadev;
1439 // Initialize the pipe.
1440 submix_audio_device_create_pipe(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1441 DEFAULT_PIPE_PERIOD_COUNT, in, NULL);
Stewart Miles92854f52014-05-01 09:03:27 -07001442#if LOG_STREAMS_TO_FILES
1443 in->log_fd = open(LOG_STREAM_IN_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1444 LOG_STREAM_FILE_PERMISSIONS);
1445 ALOGE_IF(in->log_fd < 0, "adev_open_input_stream(): log file open failed %s",
1446 strerror(errno));
1447 ALOGV("adev_open_input_stream(): log_fd = %d", in->log_fd);
1448#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001449 // Return the input stream.
1450 *stream_in = &in->stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001451
1452 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001453}
1454
1455static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -07001456 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001457{
Stewart Miles3dd36f92014-05-01 09:03:27 -07001458 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001459 ALOGV("adev_close_input_stream()");
Stewart Miles3dd36f92014-05-01 09:03:27 -07001460 submix_audio_device_destroy_pipe(audio_hw_device_get_submix_audio_device(dev), in, NULL);
Stewart Miles92854f52014-05-01 09:03:27 -07001461#if LOG_STREAMS_TO_FILES
1462 if (in->log_fd >= 0) close(in->log_fd);
1463#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001464#if ENABLE_LEGACY_INPUT_OPEN
1465 if (in->ref_count == 0) free(in);
1466#else
1467 free(in);
1468#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001469}
1470
1471static int adev_dump(const audio_hw_device_t *device, int fd)
1472{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001473 (void)device;
1474 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001475 return 0;
1476}
1477
1478static int adev_close(hw_device_t *device)
1479{
1480 ALOGI("adev_close()");
1481 free(device);
1482 return 0;
1483}
1484
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001485static int adev_open(const hw_module_t* module, const char* name,
1486 hw_device_t** device)
1487{
1488 ALOGI("adev_open(name=%s)", name);
1489 struct submix_audio_device *rsxadev;
1490
1491 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1492 return -EINVAL;
1493
1494 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
1495 if (!rsxadev)
1496 return -ENOMEM;
1497
1498 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -07001499 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001500 rsxadev->device.common.module = (struct hw_module_t *) module;
1501 rsxadev->device.common.close = adev_close;
1502
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001503 rsxadev->device.init_check = adev_init_check;
1504 rsxadev->device.set_voice_volume = adev_set_voice_volume;
1505 rsxadev->device.set_master_volume = adev_set_master_volume;
1506 rsxadev->device.get_master_volume = adev_get_master_volume;
1507 rsxadev->device.set_master_mute = adev_set_master_mute;
1508 rsxadev->device.get_master_mute = adev_get_master_mute;
1509 rsxadev->device.set_mode = adev_set_mode;
1510 rsxadev->device.set_mic_mute = adev_set_mic_mute;
1511 rsxadev->device.get_mic_mute = adev_get_mic_mute;
1512 rsxadev->device.set_parameters = adev_set_parameters;
1513 rsxadev->device.get_parameters = adev_get_parameters;
1514 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
1515 rsxadev->device.open_output_stream = adev_open_output_stream;
1516 rsxadev->device.close_output_stream = adev_close_output_stream;
1517 rsxadev->device.open_input_stream = adev_open_input_stream;
1518 rsxadev->device.close_input_stream = adev_close_input_stream;
1519 rsxadev->device.dump = adev_dump;
1520
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001521 rsxadev->input_standby = true;
1522 rsxadev->output_standby = true;
1523
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001524 *device = &rsxadev->device.common;
1525
1526 return 0;
1527}
1528
1529static struct hw_module_methods_t hal_module_methods = {
1530 /* open */ adev_open,
1531};
1532
1533struct audio_module HAL_MODULE_INFO_SYM = {
1534 /* common */ {
1535 /* tag */ HARDWARE_MODULE_TAG,
1536 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
1537 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
1538 /* id */ AUDIO_HARDWARE_MODULE_ID,
1539 /* name */ "Wifi Display audio HAL",
1540 /* author */ "The Android Open Source Project",
1541 /* methods */ &hal_module_methods,
1542 /* dso */ NULL,
1543 /* reserved */ { 0 },
1544 },
1545};
1546
1547} //namespace android
1548
1549} //extern "C"