blob: 6c538cee84dc1ccdb2f02884ed9c1dc21410dcdb [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
Jean-Michel Trivi25f47512015-05-26 14:18:10 -070028#include <cutils/compiler.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>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070031#include <log/log.h>
32#include <utils/String8.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070033
Stewart Milesc049a0a2014-05-01 09:03:27 -070034#include <hardware/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070035#include <hardware/hardware.h>
36#include <system/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070037
Stewart Milesc049a0a2014-05-01 09:03:27 -070038#include <media/AudioParameter.h>
39#include <media/AudioBufferProvider.h>
Jean-Michel Trivieec87702012-09-17 09:59:42 -070040#include <media/nbaio/MonoPipe.h>
41#include <media/nbaio/MonoPipeReader.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -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().
Jean-Michel Trivibbb3e772015-05-26 15:59:42 -070065#define DEFAULT_PIPE_SIZE_IN_FRAMES (1024*4)
Stewart Miles3dd36f92014-05-01 09:03:27 -070066// 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.
Eric Laurent854a10a2016-02-19 14:41:51 -080091#define LOG_STREAM_FOLDER "/data/misc/audioserver"
Stewart Miles92854f52014-05-01 09:03:27 -070092// 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
Jean-Michel Trivi793a8542014-10-14 15:31:51 -070098// limit for number of read error log entries to avoid spamming the logs
99#define MAX_READ_ERROR_LOGS 5
Stewart Miles3dd36f92014-05-01 09:03:27 -0700100
101// Common limits macros.
102#ifndef min
103#define min(a, b) ((a) < (b) ? (a) : (b))
104#endif // min
Stewart Milese54c12c2014-05-01 09:03:27 -0700105#ifndef max
106#define max(a, b) ((a) > (b) ? (a) : (b))
107#endif // max
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700108
Stewart Miles70726842014-05-01 09:03:27 -0700109// Set *result_variable_ptr to true if value_to_find is present in the array array_to_search,
110// otherwise set *result_variable_ptr to false.
111#define SUBMIX_VALUE_IN_SET(value_to_find, array_to_search, result_variable_ptr) \
112 { \
113 size_t i; \
114 *(result_variable_ptr) = false; \
115 for (i = 0; i < sizeof(array_to_search) / sizeof((array_to_search)[0]); i++) { \
116 if ((value_to_find) == (array_to_search)[i]) { \
117 *(result_variable_ptr) = true; \
118 break; \
119 } \
120 } \
121 }
122
Stewart Miles568e66f2014-05-01 09:03:27 -0700123// Configuration of the submix pipe.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700124struct submix_config {
Stewart Miles70726842014-05-01 09:03:27 -0700125 // Channel mask field in this data structure is set to either input_channel_mask or
126 // output_channel_mask depending upon the last stream to be opened on this device.
127 struct audio_config common;
128 // Input stream and output stream channel masks. This is required since input and output
129 // channel bitfields are not equivalent.
130 audio_channel_mask_t input_channel_mask;
131 audio_channel_mask_t output_channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700132#if ENABLE_RESAMPLING
133 // Input stream and output stream sample rates.
134 uint32_t input_sample_rate;
135 uint32_t output_sample_rate;
136#endif // ENABLE_RESAMPLING
Stewart Milese54c12c2014-05-01 09:03:27 -0700137 size_t pipe_frame_size; // Number of bytes in each audio frame in the pipe.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700138 size_t buffer_size_frames; // Size of the audio pipe in frames.
139 // Maximum number of frames buffered by the input and output streams.
140 size_t buffer_period_size_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700141};
142
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800143#define MAX_ROUTES 10
144typedef struct route_config {
145 struct submix_config config;
146 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700147 // Pipe variables: they handle the ring buffer that "pipes" audio:
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700148 // - from the submix virtual audio output == what needs to be played
149 // remotely, seen as an output for AudioFlinger
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700150 // - to the virtual audio source == what is captured by the component
151 // which "records" the submix / virtual audio source, and handles it as needed.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700152 // A usecase example is one where the component capturing the audio is then sending it over
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700153 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
154 // TV with Wifi Display capabilities), or to a wireless audio player.
Stewart Miles568e66f2014-05-01 09:03:27 -0700155 sp<MonoPipe> rsxSink;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700156 sp<MonoPipeReader> rsxSource;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800157 // Pointers to the current input and output stream instances. rsxSink and rsxSource are
158 // destroyed if both and input and output streams are destroyed.
159 struct submix_stream_out *output;
160 struct submix_stream_in *input;
Stewart Miles02c2f712014-05-01 09:03:27 -0700161#if ENABLE_RESAMPLING
162 // Buffer used as temporary storage for resampled data prior to returning data to the output
163 // stream.
164 int16_t resampler_buffer[DEFAULT_PIPE_SIZE_IN_FRAMES];
165#endif // ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800166} route_config_t;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700167
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800168struct submix_audio_device {
169 struct audio_hw_device device;
170 route_config_t routes[MAX_ROUTES];
Stewart Miles568e66f2014-05-01 09:03:27 -0700171 // Device lock, also used to protect access to submix_audio_device from the input and output
172 // streams.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700173 pthread_mutex_t lock;
174};
175
176struct submix_stream_out {
177 struct audio_stream_out stream;
178 struct submix_audio_device *dev;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800179 int route_handle;
180 bool output_standby;
Andy Hung0b93c0a2015-08-10 13:52:34 -0700181 uint64_t frames_written;
182 uint64_t frames_written_since_standby;
Stewart Miles92854f52014-05-01 09:03:27 -0700183#if LOG_STREAMS_TO_FILES
184 int log_fd;
185#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700186};
187
188struct submix_stream_in {
189 struct audio_stream_in stream;
190 struct submix_audio_device *dev;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800191 int route_handle;
192 bool input_standby;
193 bool output_standby_rec_thr; // output standby state as seen from record thread
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700194 // wall clock when recording starts
195 struct timespec record_start_time;
196 // how many frames have been requested to be read
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700197 uint64_t read_counter_frames;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700198
199#if ENABLE_LEGACY_INPUT_OPEN
200 // Number of references to this input stream.
201 volatile int32_t ref_count;
202#endif // ENABLE_LEGACY_INPUT_OPEN
Stewart Miles92854f52014-05-01 09:03:27 -0700203#if LOG_STREAMS_TO_FILES
204 int log_fd;
205#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi793a8542014-10-14 15:31:51 -0700206
207 volatile int16_t read_error_count;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700208};
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700209
Stewart Miles70726842014-05-01 09:03:27 -0700210// Determine whether the specified sample rate is supported by the submix module.
211static bool sample_rate_supported(const uint32_t sample_rate)
212{
213 // Set of sample rates supported by Format_from_SR_C() frameworks/av/media/libnbaio/NAIO.cpp.
214 static const unsigned int supported_sample_rates[] = {
215 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
216 };
217 bool return_value;
218 SUBMIX_VALUE_IN_SET(sample_rate, supported_sample_rates, &return_value);
219 return return_value;
220}
221
222// Determine whether the specified sample rate is supported, if it is return the specified sample
223// rate, otherwise return the default sample rate for the submix module.
224static uint32_t get_supported_sample_rate(uint32_t sample_rate)
225{
226 return sample_rate_supported(sample_rate) ? sample_rate : DEFAULT_SAMPLE_RATE_HZ;
227}
228
229// Determine whether the specified channel in mask is supported by the submix module.
230static bool channel_in_mask_supported(const audio_channel_mask_t channel_in_mask)
231{
232 // Set of channel in masks supported by Format_from_SR_C()
233 // frameworks/av/media/libnbaio/NAIO.cpp.
234 static const audio_channel_mask_t supported_channel_in_masks[] = {
235 AUDIO_CHANNEL_IN_MONO, AUDIO_CHANNEL_IN_STEREO,
236 };
237 bool return_value;
238 SUBMIX_VALUE_IN_SET(channel_in_mask, supported_channel_in_masks, &return_value);
239 return return_value;
240}
241
242// Determine whether the specified channel in mask is supported, if it is return the specified
243// channel in mask, otherwise return the default channel in mask for the submix module.
244static audio_channel_mask_t get_supported_channel_in_mask(
245 const audio_channel_mask_t channel_in_mask)
246{
247 return channel_in_mask_supported(channel_in_mask) ? channel_in_mask :
248 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_IN_STEREO);
249}
250
251// Determine whether the specified channel out mask is supported by the submix module.
252static bool channel_out_mask_supported(const audio_channel_mask_t channel_out_mask)
253{
254 // Set of channel out masks supported by Format_from_SR_C()
255 // frameworks/av/media/libnbaio/NAIO.cpp.
256 static const audio_channel_mask_t supported_channel_out_masks[] = {
257 AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO,
258 };
259 bool return_value;
260 SUBMIX_VALUE_IN_SET(channel_out_mask, supported_channel_out_masks, &return_value);
261 return return_value;
262}
263
264// Determine whether the specified channel out mask is supported, if it is return the specified
265// channel out mask, otherwise return the default channel out mask for the submix module.
266static audio_channel_mask_t get_supported_channel_out_mask(
267 const audio_channel_mask_t channel_out_mask)
268{
269 return channel_out_mask_supported(channel_out_mask) ? channel_out_mask :
270 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_OUT_STEREO);
271}
272
Stewart Milesf645c5e2014-05-01 09:03:27 -0700273// Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the
274// structure.
275static struct submix_stream_out * audio_stream_out_get_submix_stream_out(
276 struct audio_stream_out * const stream)
277{
278 ALOG_ASSERT(stream);
279 return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) -
280 offsetof(struct submix_stream_out, stream));
281}
282
283// Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure.
284static struct submix_stream_out * audio_stream_get_submix_stream_out(
285 struct audio_stream * const stream)
286{
287 ALOG_ASSERT(stream);
288 return audio_stream_out_get_submix_stream_out(
289 reinterpret_cast<struct audio_stream_out *>(stream));
290}
291
292// Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the
293// structure.
294static struct submix_stream_in * audio_stream_in_get_submix_stream_in(
295 struct audio_stream_in * const stream)
296{
297 ALOG_ASSERT(stream);
298 return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) -
299 offsetof(struct submix_stream_in, stream));
300}
301
302// Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure.
303static struct submix_stream_in * audio_stream_get_submix_stream_in(
304 struct audio_stream * const stream)
305{
306 ALOG_ASSERT(stream);
307 return audio_stream_in_get_submix_stream_in(
308 reinterpret_cast<struct audio_stream_in *>(stream));
309}
310
311// Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within
312// the structure.
313static struct submix_audio_device * audio_hw_device_get_submix_audio_device(
314 struct audio_hw_device *device)
315{
316 ALOG_ASSERT(device);
317 return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) -
318 offsetof(struct submix_audio_device, device));
319}
320
Stewart Miles70726842014-05-01 09:03:27 -0700321// Compare an audio_config with input channel mask and an audio_config with output channel mask
322// returning false if they do *not* match, true otherwise.
323static bool audio_config_compare(const audio_config * const input_config,
324 const audio_config * const output_config)
325{
Stewart Milese54c12c2014-05-01 09:03:27 -0700326#if !ENABLE_CHANNEL_CONVERSION
Eric Laurentdd45fd32014-07-01 20:32:28 -0700327 const uint32_t input_channels = audio_channel_count_from_in_mask(input_config->channel_mask);
328 const uint32_t output_channels = audio_channel_count_from_out_mask(output_config->channel_mask);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700329 if (input_channels != output_channels) {
330 ALOGE("audio_config_compare() channel count mismatch input=%d vs. output=%d",
331 input_channels, output_channels);
Stewart Miles70726842014-05-01 09:03:27 -0700332 return false;
333 }
Stewart Milese54c12c2014-05-01 09:03:27 -0700334#endif // !ENABLE_CHANNEL_CONVERSION
Stewart Miles02c2f712014-05-01 09:03:27 -0700335#if ENABLE_RESAMPLING
336 if (input_config->sample_rate != output_config->sample_rate &&
Eric Laurentdd45fd32014-07-01 20:32:28 -0700337 audio_channel_count_from_in_mask(input_config->channel_mask) != 1) {
Stewart Miles02c2f712014-05-01 09:03:27 -0700338#else
Stewart Miles70726842014-05-01 09:03:27 -0700339 if (input_config->sample_rate != output_config->sample_rate) {
Stewart Miles02c2f712014-05-01 09:03:27 -0700340#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700341 ALOGE("audio_config_compare() sample rate mismatch %ul vs. %ul",
342 input_config->sample_rate, output_config->sample_rate);
343 return false;
344 }
345 if (input_config->format != output_config->format) {
346 ALOGE("audio_config_compare() format mismatch %x vs. %x",
347 input_config->format, output_config->format);
348 return false;
349 }
350 // This purposely ignores offload_info as it's not required for the submix device.
351 return true;
352}
353
Stewart Miles3dd36f92014-05-01 09:03:27 -0700354// If one doesn't exist, create a pipe for the submix audio device rsxadev of size
355// buffer_size_frames and optionally associate "in" or "out" with the submix audio device.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800356// Must be called with lock held on the submix_audio_device
357static void submix_audio_device_create_pipe_l(struct submix_audio_device * const rsxadev,
Stewart Miles3dd36f92014-05-01 09:03:27 -0700358 const struct audio_config * const config,
359 const size_t buffer_size_frames,
360 const uint32_t buffer_period_count,
361 struct submix_stream_in * const in,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800362 struct submix_stream_out * const out,
363 const char *address,
364 int route_idx)
Stewart Miles3dd36f92014-05-01 09:03:27 -0700365{
366 ALOG_ASSERT(in || out);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800367 ALOG_ASSERT(route_idx > -1);
368 ALOG_ASSERT(route_idx < MAX_ROUTES);
369 ALOGD("submix_audio_device_create_pipe_l(addr=%s, idx=%d)", address, route_idx);
370
Stewart Miles3dd36f92014-05-01 09:03:27 -0700371 // Save a reference to the specified input or output stream and the associated channel
372 // mask.
373 if (in) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800374 in->route_handle = route_idx;
375 rsxadev->routes[route_idx].input = in;
376 rsxadev->routes[route_idx].config.input_channel_mask = config->channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700377#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800378 rsxadev->routes[route_idx].config.input_sample_rate = config->sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700379 // If the output isn't configured yet, set the output sample rate to the maximum supported
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800380 // sample rate such that the smallest possible input buffer is created, and put a default
381 // value for channel count
382 if (!rsxadev->routes[route_idx].output) {
383 rsxadev->routes[route_idx].config.output_sample_rate = 48000;
384 rsxadev->routes[route_idx].config.output_channel_mask = AUDIO_CHANNEL_OUT_STEREO;
Stewart Miles02c2f712014-05-01 09:03:27 -0700385 }
386#endif // ENABLE_RESAMPLING
Stewart Miles3dd36f92014-05-01 09:03:27 -0700387 }
388 if (out) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800389 out->route_handle = route_idx;
390 rsxadev->routes[route_idx].output = out;
391 rsxadev->routes[route_idx].config.output_channel_mask = config->channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700392#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800393 rsxadev->routes[route_idx].config.output_sample_rate = config->sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700394#endif // ENABLE_RESAMPLING
Stewart Miles3dd36f92014-05-01 09:03:27 -0700395 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800396 // Save the address
397 strncpy(rsxadev->routes[route_idx].address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
398 ALOGD(" now using address %s for route %d", rsxadev->routes[route_idx].address, route_idx);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700399 // If a pipe isn't associated with the device, create one.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800400 if (rsxadev->routes[route_idx].rsxSink == NULL || rsxadev->routes[route_idx].rsxSource == NULL)
401 {
402 struct submix_config * const device_config = &rsxadev->routes[route_idx].config;
Eric Laurentdd45fd32014-07-01 20:32:28 -0700403 uint32_t channel_count;
404 if (out)
405 channel_count = audio_channel_count_from_out_mask(config->channel_mask);
406 else
407 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Stewart Miles10f1a802014-06-09 20:54:37 -0700408#if ENABLE_CHANNEL_CONVERSION
409 // If channel conversion is enabled, allocate enough space for the maximum number of
410 // possible channels stored in the pipe for the situation when the number of channels in
411 // the output stream don't match the number in the input stream.
412 const uint32_t pipe_channel_count = max(channel_count, 2);
413#else
414 const uint32_t pipe_channel_count = channel_count;
415#endif // ENABLE_CHANNEL_CONVERSION
416 const NBAIO_Format format = Format_from_SR_C(config->sample_rate, pipe_channel_count,
417 config->format);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700418 const NBAIO_Format offers[1] = {format};
419 size_t numCounterOffers = 0;
420 // Create a MonoPipe with optional blocking set to true.
421 MonoPipe* sink = new MonoPipe(buffer_size_frames, format, true /*writeCanBlock*/);
422 // Negotiation between the source and sink cannot fail as the device open operation
423 // creates both ends of the pipe using the same audio format.
424 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
425 ALOG_ASSERT(index == 0);
426 MonoPipeReader* source = new MonoPipeReader(sink);
427 numCounterOffers = 0;
428 index = source->negotiate(offers, 1, NULL, numCounterOffers);
429 ALOG_ASSERT(index == 0);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800430 ALOGV("submix_audio_device_create_pipe_l(): created pipe");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700431
432 // Save references to the source and sink.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800433 ALOG_ASSERT(rsxadev->routes[route_idx].rsxSink == NULL);
434 ALOG_ASSERT(rsxadev->routes[route_idx].rsxSource == NULL);
435 rsxadev->routes[route_idx].rsxSink = sink;
436 rsxadev->routes[route_idx].rsxSource = source;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700437 // Store the sanitized audio format in the device so that it's possible to determine
438 // the format of the pipe source when opening the input device.
439 memcpy(&device_config->common, config, sizeof(device_config->common));
440 device_config->buffer_size_frames = sink->maxFrames();
441 device_config->buffer_period_size_frames = device_config->buffer_size_frames /
442 buffer_period_count;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700443 if (in) device_config->pipe_frame_size = audio_stream_in_frame_size(&in->stream);
444 if (out) device_config->pipe_frame_size = audio_stream_out_frame_size(&out->stream);
Stewart Miles10f1a802014-06-09 20:54:37 -0700445#if ENABLE_CHANNEL_CONVERSION
446 // Calculate the pipe frame size based upon the number of channels.
447 device_config->pipe_frame_size = (device_config->pipe_frame_size * pipe_channel_count) /
448 channel_count;
449#endif // ENABLE_CHANNEL_CONVERSION
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800450 SUBMIX_ALOGV("submix_audio_device_create_pipe_l(): pipe frame size %zd, pipe size %zd, "
Stewart Milese54c12c2014-05-01 09:03:27 -0700451 "period size %zd", device_config->pipe_frame_size,
452 device_config->buffer_size_frames, device_config->buffer_period_size_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700453 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700454}
455
456// Release references to the sink and source. Input and output threads may maintain references
457// to these objects via StrongPointer (sp<MonoPipe> and sp<MonoPipeReader>) which they can use
458// before they shutdown.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800459// Must be called with lock held on the submix_audio_device
460static void submix_audio_device_release_pipe_l(struct submix_audio_device * const rsxadev,
461 int route_idx)
Stewart Miles3dd36f92014-05-01 09:03:27 -0700462{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800463 ALOG_ASSERT(route_idx > -1);
464 ALOG_ASSERT(route_idx < MAX_ROUTES);
465 ALOGD("submix_audio_device_release_pipe_l(idx=%d) addr=%s", route_idx,
466 rsxadev->routes[route_idx].address);
467 if (rsxadev->routes[route_idx].rsxSink != 0) {
468 rsxadev->routes[route_idx].rsxSink.clear();
469 rsxadev->routes[route_idx].rsxSink = 0;
470 }
471 if (rsxadev->routes[route_idx].rsxSource != 0) {
472 rsxadev->routes[route_idx].rsxSource.clear();
473 rsxadev->routes[route_idx].rsxSource = 0;
474 }
475 memset(rsxadev->routes[route_idx].address, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
476#ifdef ENABLE_RESAMPLING
477 memset(rsxadev->routes[route_idx].resampler_buffer, 0,
478 sizeof(int16_t) * DEFAULT_PIPE_SIZE_IN_FRAMES);
479#endif
Stewart Miles3dd36f92014-05-01 09:03:27 -0700480}
481
482// Remove references to the specified input and output streams. When the device no longer
483// references input and output streams destroy the associated pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800484// Must be called with lock held on the submix_audio_device
485static void submix_audio_device_destroy_pipe_l(struct submix_audio_device * const rsxadev,
Stewart Miles3dd36f92014-05-01 09:03:27 -0700486 const struct submix_stream_in * const in,
487 const struct submix_stream_out * const out)
488{
489 MonoPipe* sink;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800490 ALOGV("submix_audio_device_destroy_pipe_l()");
491 int route_idx = -1;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700492 if (in != NULL) {
493#if ENABLE_LEGACY_INPUT_OPEN
494 const_cast<struct submix_stream_in*>(in)->ref_count--;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800495 route_idx = in->route_handle;
496 ALOG_ASSERT(rsxadev->routes[route_idx].input == in);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700497 if (in->ref_count == 0) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800498 rsxadev->routes[route_idx].input = NULL;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700499 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800500 ALOGV("submix_audio_device_destroy_pipe_l(): input ref_count %d", in->ref_count);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700501#else
502 rsxadev->input = NULL;
503#endif // ENABLE_LEGACY_INPUT_OPEN
504 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800505 if (out != NULL) {
506 route_idx = out->route_handle;
507 ALOG_ASSERT(rsxadev->routes[route_idx].output == out);
508 rsxadev->routes[route_idx].output = NULL;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700509 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800510 if (route_idx != -1 &&
511 rsxadev->routes[route_idx].input == NULL && rsxadev->routes[route_idx].output == NULL) {
512 submix_audio_device_release_pipe_l(rsxadev, route_idx);
513 ALOGD("submix_audio_device_destroy_pipe_l(): pipe destroyed");
514 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700515}
516
Stewart Miles70726842014-05-01 09:03:27 -0700517// Sanitize the user specified audio config for a submix input / output stream.
518static void submix_sanitize_config(struct audio_config * const config, const bool is_input_format)
519{
520 config->channel_mask = is_input_format ? get_supported_channel_in_mask(config->channel_mask) :
521 get_supported_channel_out_mask(config->channel_mask);
522 config->sample_rate = get_supported_sample_rate(config->sample_rate);
523 config->format = DEFAULT_FORMAT;
524}
525
526// Verify a submix input or output stream can be opened.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800527// Must be called with lock held on the submix_audio_device
528static bool submix_open_validate_l(const struct submix_audio_device * const rsxadev,
529 int route_idx,
Stewart Miles70726842014-05-01 09:03:27 -0700530 const struct audio_config * const config,
531 const bool opening_input)
532{
Stewart Miles3dd36f92014-05-01 09:03:27 -0700533 bool input_open;
534 bool output_open;
Stewart Miles70726842014-05-01 09:03:27 -0700535 audio_config pipe_config;
536
537 // Query the device for the current audio config and whether input and output streams are open.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800538 output_open = rsxadev->routes[route_idx].output != NULL;
539 input_open = rsxadev->routes[route_idx].input != NULL;
540 memcpy(&pipe_config, &rsxadev->routes[route_idx].config.common, sizeof(pipe_config));
Stewart Miles70726842014-05-01 09:03:27 -0700541
Stewart Miles3dd36f92014-05-01 09:03:27 -0700542 // If the stream is already open, don't open it again.
543 if (opening_input ? !ENABLE_LEGACY_INPUT_OPEN && input_open : output_open) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800544 ALOGE("submix_open_validate_l(): %s stream already open.", opening_input ? "Input" :
Stewart Miles3dd36f92014-05-01 09:03:27 -0700545 "Output");
546 return false;
547 }
548
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800549 SUBMIX_ALOGV("submix_open_validate_l(): sample rate=%d format=%x "
Stewart Miles3dd36f92014-05-01 09:03:27 -0700550 "%s_channel_mask=%x", config->sample_rate, config->format,
551 opening_input ? "in" : "out", config->channel_mask);
552
553 // If either stream is open, verify the existing audio config the pipe matches the user
Stewart Miles70726842014-05-01 09:03:27 -0700554 // specified config.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700555 if (input_open || output_open) {
Stewart Miles70726842014-05-01 09:03:27 -0700556 const audio_config * const input_config = opening_input ? config : &pipe_config;
557 const audio_config * const output_config = opening_input ? &pipe_config : config;
558 // Get the channel mask of the open device.
559 pipe_config.channel_mask =
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800560 opening_input ? rsxadev->routes[route_idx].config.output_channel_mask :
561 rsxadev->routes[route_idx].config.input_channel_mask;
Stewart Miles70726842014-05-01 09:03:27 -0700562 if (!audio_config_compare(input_config, output_config)) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800563 ALOGE("submix_open_validate_l(): Unsupported format.");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700564 return false;
Stewart Miles70726842014-05-01 09:03:27 -0700565 }
566 }
567 return true;
568}
569
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800570// Must be called with lock held on the submix_audio_device
571static status_t submix_get_route_idx_for_address_l(const struct submix_audio_device * const rsxadev,
572 const char* address, /*in*/
573 int *idx /*out*/)
574{
575 // Do we already have a route for this address
576 int route_idx = -1;
577 int route_empty_idx = -1; // index of an empty route slot that can be used if needed
578 for (int i=0 ; i < MAX_ROUTES ; i++) {
579 if (strcmp(rsxadev->routes[i].address, "") == 0) {
580 route_empty_idx = i;
581 }
582 if (strncmp(rsxadev->routes[i].address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
583 route_idx = i;
584 break;
585 }
586 }
587
588 if ((route_idx == -1) && (route_empty_idx == -1)) {
589 ALOGE("Cannot create new route for address %s, max number of routes reached", address);
590 return -ENOMEM;
591 }
592 if (route_idx == -1) {
593 route_idx = route_empty_idx;
594 }
595 *idx = route_idx;
596 return OK;
597}
598
599
Stewart Milese54c12c2014-05-01 09:03:27 -0700600// Calculate the maximum size of the pipe buffer in frames for the specified stream.
601static size_t calculate_stream_pipe_size_in_frames(const struct audio_stream *stream,
602 const struct submix_config *config,
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700603 const size_t pipe_frames,
604 const size_t stream_frame_size)
Stewart Milese54c12c2014-05-01 09:03:27 -0700605{
Stewart Milese54c12c2014-05-01 09:03:27 -0700606 const size_t pipe_frame_size = config->pipe_frame_size;
607 const size_t max_frame_size = max(stream_frame_size, pipe_frame_size);
608 return (pipe_frames * config->pipe_frame_size) / max_frame_size;
609}
610
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700611/* audio HAL functions */
612
613static uint32_t out_get_sample_rate(const struct audio_stream *stream)
614{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700615 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
616 const_cast<struct audio_stream *>(stream));
Stewart Miles02c2f712014-05-01 09:03:27 -0700617#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800618 const uint32_t out_rate = out->dev->routes[out->route_handle].config.output_sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700619#else
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800620 const uint32_t out_rate = out->dev->routes[out->route_handle].config.common.sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700621#endif // ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800622 SUBMIX_ALOGV("out_get_sample_rate() returns %u for addr %s",
623 out_rate, out->dev->routes[out->route_handle].address);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700624 return out_rate;
625}
626
627static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
628{
Stewart Miles02c2f712014-05-01 09:03:27 -0700629 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
630#if ENABLE_RESAMPLING
631 // The sample rate of the stream can't be changed once it's set since this would change the
632 // output buffer size and hence break playback to the shared pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800633 if (rate != out->dev->routes[out->route_handle].config.output_sample_rate) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700634 ALOGE("out_set_sample_rate() resampling enabled can't change sample rate from "
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800635 "%u to %u for addr %s",
636 out->dev->routes[out->route_handle].config.output_sample_rate, rate,
637 out->dev->routes[out->route_handle].address);
Stewart Miles02c2f712014-05-01 09:03:27 -0700638 return -ENOSYS;
639 }
640#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700641 if (!sample_rate_supported(rate)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700642 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
643 return -ENOSYS;
644 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700645 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800646 out->dev->routes[out->route_handle].config.common.sample_rate = rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700647 return 0;
648}
649
650static size_t out_get_buffer_size(const struct audio_stream *stream)
651{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700652 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
653 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800654 const struct submix_config * const config = &out->dev->routes[out->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700655 const size_t stream_frame_size =
656 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700657 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700658 stream, config, config->buffer_period_size_frames, stream_frame_size);
659 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Miles568e66f2014-05-01 09:03:27 -0700660 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
Stewart Milese54c12c2014-05-01 09:03:27 -0700661 buffer_size_bytes, buffer_size_frames);
662 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700663}
664
665static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
666{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700667 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
668 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800669 uint32_t channel_mask = out->dev->routes[out->route_handle].config.output_channel_mask;
Stewart Miles568e66f2014-05-01 09:03:27 -0700670 SUBMIX_ALOGV("out_get_channels() returns %08x", channel_mask);
671 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700672}
673
674static audio_format_t out_get_format(const struct audio_stream *stream)
675{
Stewart Miles568e66f2014-05-01 09:03:27 -0700676 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
677 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800678 const audio_format_t format = out->dev->routes[out->route_handle].config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700679 SUBMIX_ALOGV("out_get_format() returns %x", format);
680 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700681}
682
683static int out_set_format(struct audio_stream *stream, audio_format_t format)
684{
Stewart Miles568e66f2014-05-01 09:03:27 -0700685 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800686 if (format != out->dev->routes[out->route_handle].config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700687 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700688 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700689 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700690 SUBMIX_ALOGV("out_set_format(format=%x)", format);
691 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700692}
693
694static int out_standby(struct audio_stream *stream)
695{
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700696 ALOGI("out_standby()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800697 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
698 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700699
Stewart Milesf645c5e2014-05-01 09:03:27 -0700700 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700701
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800702 out->output_standby = true;
Andy Hung0b93c0a2015-08-10 13:52:34 -0700703 out->frames_written_since_standby = 0;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700704
Stewart Milesf645c5e2014-05-01 09:03:27 -0700705 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700706
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700707 return 0;
708}
709
710static int out_dump(const struct audio_stream *stream, int fd)
711{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700712 (void)stream;
713 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700714 return 0;
715}
716
717static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
718{
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700719 int exiting = -1;
720 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700721 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700722
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700723 // FIXME this is using hard-coded strings but in the future, this functionality will be
724 // converted to use audio HAL extensions required to support tunneling
725 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700726 struct submix_audio_device * const rsxadev =
727 audio_stream_get_submix_stream_out(stream)->dev;
728 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800729 { // using the sink
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800730 sp<MonoPipe> sink =
731 rsxadev->routes[audio_stream_get_submix_stream_out(stream)->route_handle]
732 .rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700733 if (sink == NULL) {
734 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800735 return 0;
736 }
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700737
Jean-Michel Trivi793a8542014-10-14 15:31:51 -0700738 ALOGD("out_set_parameters(): shutting down MonoPipe sink");
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800739 sink->shutdown(true);
740 } // done using the sink
Stewart Milesf645c5e2014-05-01 09:03:27 -0700741 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700742 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700743 return 0;
744}
745
746static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
747{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700748 (void)stream;
749 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700750 return strdup("");
751}
752
753static uint32_t out_get_latency(const struct audio_stream_out *stream)
754{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700755 const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
756 const_cast<struct audio_stream_out *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800757 const struct submix_config * const config = &out->dev->routes[out->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700758 const size_t stream_frame_size =
759 audio_stream_out_frame_size(stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700760 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700761 &stream->common, config, config->buffer_size_frames, stream_frame_size);
Stewart Miles10f1a802014-06-09 20:54:37 -0700762 const uint32_t sample_rate = out_get_sample_rate(&stream->common);
763 const uint32_t latency_ms = (buffer_size_frames * 1000) / sample_rate;
Stewart Milese54c12c2014-05-01 09:03:27 -0700764 SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u",
Stewart Miles10f1a802014-06-09 20:54:37 -0700765 latency_ms, buffer_size_frames, sample_rate);
Stewart Miles568e66f2014-05-01 09:03:27 -0700766 return latency_ms;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700767}
768
769static int out_set_volume(struct audio_stream_out *stream, float left,
770 float right)
771{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700772 (void)stream;
773 (void)left;
774 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700775 return -ENOSYS;
776}
777
778static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
779 size_t bytes)
780{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700781 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700782 ssize_t written_frames = 0;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700783 const size_t frame_size = audio_stream_out_frame_size(stream);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700784 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
785 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700786 const size_t frames = bytes / frame_size;
787
Stewart Milesf645c5e2014-05-01 09:03:27 -0700788 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700789
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800790 out->output_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700791
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800792 sp<MonoPipe> sink = rsxadev->routes[out->route_handle].rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700793 if (sink != NULL) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700794 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800795 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700796 pthread_mutex_unlock(&rsxadev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700797 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700798 // the pipe has already been shutdown, this buffer will be lost but we must
799 // simulate timing so we don't drain the output faster than realtime
800 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
801 return bytes;
802 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700803 } else {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700804 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700805 ALOGE("out_write without a pipe!");
806 ALOG_ASSERT("out_write without a pipe!");
807 return 0;
808 }
809
Stewart Miles2d199fe2014-05-01 09:03:27 -0700810 // If the write to the sink would block when no input stream is present, flush enough frames
811 // from the pipe to make space to write the most recent data.
812 {
813 const size_t availableToWrite = sink->availableToWrite();
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800814 sp<MonoPipeReader> source = rsxadev->routes[out->route_handle].rsxSource;
815 if (rsxadev->routes[out->route_handle].input == NULL && availableToWrite < frames) {
Stewart Miles2d199fe2014-05-01 09:03:27 -0700816 static uint8_t flush_buffer[64];
817 const size_t flushBufferSizeFrames = sizeof(flush_buffer) / frame_size;
818 size_t frames_to_flush_from_source = frames - availableToWrite;
819 SUBMIX_ALOGV("out_write(): flushing %d frames from the pipe to avoid blocking",
820 frames_to_flush_from_source);
821 while (frames_to_flush_from_source) {
822 const size_t flush_size = min(frames_to_flush_from_source, flushBufferSizeFrames);
823 frames_to_flush_from_source -= flush_size;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800824 // read does not block
Glenn Kasten04c88492016-01-06 14:05:23 -0800825 source->read(flush_buffer, flush_size);
Stewart Miles2d199fe2014-05-01 09:03:27 -0700826 }
827 }
828 }
829
Stewart Milesf645c5e2014-05-01 09:03:27 -0700830 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700831
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700832 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800833
Stewart Miles92854f52014-05-01 09:03:27 -0700834#if LOG_STREAMS_TO_FILES
835 if (out->log_fd >= 0) write(out->log_fd, buffer, written_frames * frame_size);
836#endif // LOG_STREAMS_TO_FILES
837
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700838 if (written_frames < 0) {
839 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700840 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700841
Stewart Milesf645c5e2014-05-01 09:03:27 -0700842 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800843 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700844 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700845
846 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700847 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700848 } else {
849 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700850 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700851 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700852 }
853 }
854
Stewart Milesf645c5e2014-05-01 09:03:27 -0700855 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800856 sink.clear();
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700857 if (written_frames > 0) {
Andy Hung0b93c0a2015-08-10 13:52:34 -0700858 out->frames_written_since_standby += written_frames;
859 out->frames_written += written_frames;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700860 }
Stewart Milesf645c5e2014-05-01 09:03:27 -0700861 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700862
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700863 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700864 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700865 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700866 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700867 const ssize_t written_bytes = written_frames * frame_size;
Stewart Miles02c2f712014-05-01 09:03:27 -0700868 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames", written_bytes, written_frames);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700869 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700870}
871
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700872static int out_get_presentation_position(const struct audio_stream_out *stream,
873 uint64_t *frames, struct timespec *timestamp)
874{
Andy Hung0b93c0a2015-08-10 13:52:34 -0700875 if (stream == NULL || frames == NULL || timestamp == NULL) {
876 return -EINVAL;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700877 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700878
879 const submix_stream_out *out = audio_stream_out_get_submix_stream_out(
880 const_cast<struct audio_stream_out *>(stream));
881 struct submix_audio_device * const rsxadev = out->dev;
882
883 int ret = -EWOULDBLOCK;
884 pthread_mutex_lock(&rsxadev->lock);
885 const ssize_t frames_in_pipe =
886 rsxadev->routes[out->route_handle].rsxSource->availableToRead();
887 if (CC_UNLIKELY(frames_in_pipe < 0)) {
888 *frames = out->frames_written;
889 ret = 0;
890 } else if (out->frames_written >= (uint64_t)frames_in_pipe) {
891 *frames = out->frames_written - frames_in_pipe;
892 ret = 0;
893 }
894 pthread_mutex_unlock(&rsxadev->lock);
895
896 if (ret == 0) {
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700897 clock_gettime(CLOCK_MONOTONIC, timestamp);
898 }
899
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700900 SUBMIX_ALOGV("out_get_presentation_position() got frames=%llu timestamp sec=%llu",
901 frames ? *frames : -1, timestamp ? timestamp->tv_sec : -1);
902
903 return ret;
904}
905
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700906static int out_get_render_position(const struct audio_stream_out *stream,
907 uint32_t *dsp_frames)
908{
Andy Hung0b93c0a2015-08-10 13:52:34 -0700909 if (stream == NULL || dsp_frames == NULL) {
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700910 return -EINVAL;
911 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700912
913 const submix_stream_out *out = audio_stream_out_get_submix_stream_out(
914 const_cast<struct audio_stream_out *>(stream));
915 struct submix_audio_device * const rsxadev = out->dev;
916
917 pthread_mutex_lock(&rsxadev->lock);
918 const ssize_t frames_in_pipe =
919 rsxadev->routes[out->route_handle].rsxSource->availableToRead();
920 if (CC_UNLIKELY(frames_in_pipe < 0)) {
921 *dsp_frames = (uint32_t)out->frames_written_since_standby;
922 } else {
923 *dsp_frames = out->frames_written_since_standby > (uint64_t) frames_in_pipe ?
924 (uint32_t)(out->frames_written_since_standby - frames_in_pipe) : 0;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700925 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700926 pthread_mutex_unlock(&rsxadev->lock);
927
928 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700929}
930
931static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
932{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700933 (void)stream;
934 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700935 return 0;
936}
937
938static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
939{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700940 (void)stream;
941 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700942 return 0;
943}
944
945static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
946 int64_t *timestamp)
947{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700948 (void)stream;
949 (void)timestamp;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700950 return -EINVAL;
951}
952
953/** audio_stream_in implementation **/
954static uint32_t in_get_sample_rate(const struct audio_stream *stream)
955{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700956 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
957 const_cast<struct audio_stream*>(stream));
Stewart Miles02c2f712014-05-01 09:03:27 -0700958#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800959 const uint32_t rate = in->dev->routes[in->route_handle].config.input_sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700960#else
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800961 const uint32_t rate = in->dev->routes[in->route_handle].config.common.sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700962#endif // ENABLE_RESAMPLING
963 SUBMIX_ALOGV("in_get_sample_rate() returns %u", rate);
964 return rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700965}
966
967static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
968{
Stewart Miles568e66f2014-05-01 09:03:27 -0700969 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Stewart Miles02c2f712014-05-01 09:03:27 -0700970#if ENABLE_RESAMPLING
971 // The sample rate of the stream can't be changed once it's set since this would change the
972 // input buffer size and hence break recording from the shared pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800973 if (rate != in->dev->routes[in->route_handle].config.input_sample_rate) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700974 ALOGE("in_set_sample_rate() resampling enabled can't change sample rate from "
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800975 "%u to %u", in->dev->routes[in->route_handle].config.input_sample_rate, rate);
Stewart Miles02c2f712014-05-01 09:03:27 -0700976 return -ENOSYS;
977 }
978#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700979 if (!sample_rate_supported(rate)) {
980 ALOGE("in_set_sample_rate(rate=%u) rate unsupported", rate);
981 return -ENOSYS;
982 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800983 in->dev->routes[in->route_handle].config.common.sample_rate = rate;
Stewart Miles568e66f2014-05-01 09:03:27 -0700984 SUBMIX_ALOGV("in_set_sample_rate() set %u", rate);
985 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700986}
987
988static size_t in_get_buffer_size(const struct audio_stream *stream)
989{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700990 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
991 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800992 const struct submix_config * const config = &in->dev->routes[in->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700993 const size_t stream_frame_size =
994 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Stewart Miles02c2f712014-05-01 09:03:27 -0700995 size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700996 stream, config, config->buffer_period_size_frames, stream_frame_size);
Stewart Miles02c2f712014-05-01 09:03:27 -0700997#if ENABLE_RESAMPLING
998 // Scale the size of the buffer based upon the maximum number of frames that could be returned
999 // given the ratio of output to input sample rate.
1000 buffer_size_frames = (size_t)(((float)buffer_size_frames *
1001 (float)config->input_sample_rate) /
1002 (float)config->output_sample_rate);
1003#endif // ENABLE_RESAMPLING
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001004 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Milese54c12c2014-05-01 09:03:27 -07001005 SUBMIX_ALOGV("in_get_buffer_size() returns %zu bytes, %zu frames", buffer_size_bytes,
1006 buffer_size_frames);
1007 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001008}
1009
1010static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
1011{
Stewart Miles70726842014-05-01 09:03:27 -07001012 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
1013 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001014 const audio_channel_mask_t channel_mask =
1015 in->dev->routes[in->route_handle].config.input_channel_mask;
Stewart Miles70726842014-05-01 09:03:27 -07001016 SUBMIX_ALOGV("in_get_channels() returns %x", channel_mask);
1017 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001018}
1019
1020static audio_format_t in_get_format(const struct audio_stream *stream)
1021{
Stewart Miles568e66f2014-05-01 09:03:27 -07001022 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
Stewart Miles70726842014-05-01 09:03:27 -07001023 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001024 const audio_format_t format = in->dev->routes[in->route_handle].config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -07001025 SUBMIX_ALOGV("in_get_format() returns %x", format);
1026 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001027}
1028
1029static int in_set_format(struct audio_stream *stream, audio_format_t format)
1030{
Stewart Miles568e66f2014-05-01 09:03:27 -07001031 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001032 if (format != in->dev->routes[in->route_handle].config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -07001033 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001034 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001035 }
Stewart Milesc049a0a2014-05-01 09:03:27 -07001036 SUBMIX_ALOGV("in_set_format(format=%x)", format);
1037 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001038}
1039
1040static int in_standby(struct audio_stream *stream)
1041{
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001042 ALOGI("in_standby()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001043 struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
1044 struct submix_audio_device * const rsxadev = in->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001045
Stewart Milesf645c5e2014-05-01 09:03:27 -07001046 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001047
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001048 in->input_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001049
Stewart Milesf645c5e2014-05-01 09:03:27 -07001050 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001051
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001052 return 0;
1053}
1054
1055static int in_dump(const struct audio_stream *stream, int fd)
1056{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001057 (void)stream;
1058 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001059 return 0;
1060}
1061
1062static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1063{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001064 (void)stream;
1065 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001066 return 0;
1067}
1068
1069static char * in_get_parameters(const struct audio_stream *stream,
1070 const char *keys)
1071{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001072 (void)stream;
1073 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001074 return strdup("");
1075}
1076
1077static int in_set_gain(struct audio_stream_in *stream, float gain)
1078{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001079 (void)stream;
1080 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001081 return 0;
1082}
1083
1084static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
1085 size_t bytes)
1086{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001087 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
1088 struct submix_audio_device * const rsxadev = in->dev;
Stewart Milese54c12c2014-05-01 09:03:27 -07001089 struct audio_config *format;
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001090 const size_t frame_size = audio_stream_in_frame_size(stream);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001091 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001092
Stewart Milesc049a0a2014-05-01 09:03:27 -07001093 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
Stewart Milesf645c5e2014-05-01 09:03:27 -07001094 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001095
Jean-Michel Trivi257fde62014-12-09 20:20:15 -08001096 const bool output_standby = rsxadev->routes[in->route_handle].output == NULL
1097 ? true : rsxadev->routes[in->route_handle].output->output_standby;
1098 const bool output_standby_transition = (in->output_standby_rec_thr != output_standby);
1099 in->output_standby_rec_thr = output_standby;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001100
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001101 if (in->input_standby || output_standby_transition) {
1102 in->input_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001103 // keep track of when we exit input standby (== first read == start "real recording")
1104 // or when we start recording silence, and reset projected time
1105 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
1106 if (rc == 0) {
1107 in->read_counter_frames = 0;
1108 }
1109 }
1110
1111 in->read_counter_frames += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001112 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001113
1114 {
1115 // about to read from audio source
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001116 sp<MonoPipeReader> source = rsxadev->routes[in->route_handle].rsxSource;
Stewart Milesf645c5e2014-05-01 09:03:27 -07001117 if (source == NULL) {
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001118 in->read_error_count++;// ok if it rolls over
1119 ALOGE_IF(in->read_error_count < MAX_READ_ERROR_LOGS,
1120 "no audio pipe yet we're trying to read! (not all errors will be logged)");
Stewart Milesf645c5e2014-05-01 09:03:27 -07001121 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001122 usleep(frames_to_read * 1000000 / in_get_sample_rate(&stream->common));
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001123 memset(buffer, 0, bytes);
1124 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001125 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001126
Stewart Milesf645c5e2014-05-01 09:03:27 -07001127 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001128
1129 // read the data from the pipe (it's non blocking)
1130 int attempts = 0;
1131 char* buff = (char*)buffer;
Stewart Milese54c12c2014-05-01 09:03:27 -07001132#if ENABLE_CHANNEL_CONVERSION
1133 // Determine whether channel conversion is required.
Eric Laurentdd45fd32014-07-01 20:32:28 -07001134 const uint32_t input_channels = audio_channel_count_from_in_mask(
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001135 rsxadev->routes[in->route_handle].config.input_channel_mask);
Eric Laurentdd45fd32014-07-01 20:32:28 -07001136 const uint32_t output_channels = audio_channel_count_from_out_mask(
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001137 rsxadev->routes[in->route_handle].config.output_channel_mask);
Stewart Milese54c12c2014-05-01 09:03:27 -07001138 if (input_channels != output_channels) {
1139 SUBMIX_ALOGV("in_read(): %d output channels will be converted to %d "
1140 "input channels", output_channels, input_channels);
1141 // Only support 16-bit PCM channel conversion from mono to stereo or stereo to mono.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001142 ALOG_ASSERT(rsxadev->routes[in->route_handle].config.common.format ==
1143 AUDIO_FORMAT_PCM_16_BIT);
Stewart Milese54c12c2014-05-01 09:03:27 -07001144 ALOG_ASSERT((input_channels == 1 && output_channels == 2) ||
1145 (input_channels == 2 && output_channels == 1));
1146 }
1147#endif // ENABLE_CHANNEL_CONVERSION
1148
Stewart Miles02c2f712014-05-01 09:03:27 -07001149#if ENABLE_RESAMPLING
1150 const uint32_t input_sample_rate = in_get_sample_rate(&stream->common);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001151 const uint32_t output_sample_rate =
1152 rsxadev->routes[in->route_handle].config.output_sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -07001153 const size_t resampler_buffer_size_frames =
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001154 sizeof(rsxadev->routes[in->route_handle].resampler_buffer) /
1155 sizeof(rsxadev->routes[in->route_handle].resampler_buffer[0]);
Stewart Miles02c2f712014-05-01 09:03:27 -07001156 float resampler_ratio = 1.0f;
1157 // Determine whether resampling is required.
1158 if (input_sample_rate != output_sample_rate) {
1159 resampler_ratio = (float)output_sample_rate / (float)input_sample_rate;
1160 // Only support 16-bit PCM mono resampling.
1161 // NOTE: Resampling is performed after the channel conversion step.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001162 ALOG_ASSERT(rsxadev->routes[in->route_handle].config.common.format ==
1163 AUDIO_FORMAT_PCM_16_BIT);
1164 ALOG_ASSERT(audio_channel_count_from_in_mask(
1165 rsxadev->routes[in->route_handle].config.input_channel_mask) == 1);
Stewart Miles02c2f712014-05-01 09:03:27 -07001166 }
1167#endif // ENABLE_RESAMPLING
1168
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001169 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
Stewart Miles02c2f712014-05-01 09:03:27 -07001170 ssize_t frames_read = -1977;
Stewart Milese54c12c2014-05-01 09:03:27 -07001171 size_t read_frames = remaining_frames;
Stewart Miles02c2f712014-05-01 09:03:27 -07001172#if ENABLE_RESAMPLING
1173 char* const saved_buff = buff;
1174 if (resampler_ratio != 1.0f) {
1175 // Calculate the number of frames from the pipe that need to be read to generate
1176 // the data for the input stream read.
1177 const size_t frames_required_for_resampler = (size_t)(
1178 (float)read_frames * (float)resampler_ratio);
1179 read_frames = min(frames_required_for_resampler, resampler_buffer_size_frames);
1180 // Read into the resampler buffer.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001181 buff = (char*)rsxadev->routes[in->route_handle].resampler_buffer;
Stewart Miles02c2f712014-05-01 09:03:27 -07001182 }
1183#endif // ENABLE_RESAMPLING
Stewart Milese54c12c2014-05-01 09:03:27 -07001184#if ENABLE_CHANNEL_CONVERSION
1185 if (output_channels == 1 && input_channels == 2) {
1186 // Need to read half the requested frames since the converted output
1187 // data will take twice the space (mono->stereo).
1188 read_frames /= 2;
1189 }
1190#endif // ENABLE_CHANNEL_CONVERSION
1191
1192 SUBMIX_ALOGV("in_read(): frames available to read %zd", source->availableToRead());
1193
Glenn Kasten04c88492016-01-06 14:05:23 -08001194 frames_read = source->read(buff, read_frames);
Stewart Milese54c12c2014-05-01 09:03:27 -07001195
1196 SUBMIX_ALOGV("in_read(): frames read %zd", frames_read);
1197
1198#if ENABLE_CHANNEL_CONVERSION
1199 // Perform in-place channel conversion.
1200 // NOTE: In the following "input stream" refers to the data returned by this function
1201 // and "output stream" refers to the data read from the pipe.
1202 if (input_channels != output_channels && frames_read > 0) {
1203 int16_t *data = (int16_t*)buff;
1204 if (output_channels == 2 && input_channels == 1) {
1205 // Offset into the output stream data in samples.
1206 ssize_t output_stream_offset = 0;
1207 for (ssize_t input_stream_frame = 0; input_stream_frame < frames_read;
1208 input_stream_frame++, output_stream_offset += 2) {
1209 // Average the content from both channels.
1210 data[input_stream_frame] = ((int32_t)data[output_stream_offset] +
1211 (int32_t)data[output_stream_offset + 1]) / 2;
1212 }
1213 } else if (output_channels == 1 && input_channels == 2) {
1214 // Offset into the input stream data in samples.
1215 ssize_t input_stream_offset = (frames_read - 1) * 2;
1216 for (ssize_t output_stream_frame = frames_read - 1; output_stream_frame >= 0;
1217 output_stream_frame--, input_stream_offset -= 2) {
1218 const short sample = data[output_stream_frame];
1219 data[input_stream_offset] = sample;
1220 data[input_stream_offset + 1] = sample;
1221 }
1222 }
1223 }
1224#endif // ENABLE_CHANNEL_CONVERSION
Stewart Miles3dd36f92014-05-01 09:03:27 -07001225
Stewart Miles02c2f712014-05-01 09:03:27 -07001226#if ENABLE_RESAMPLING
1227 if (resampler_ratio != 1.0f) {
1228 SUBMIX_ALOGV("in_read(): resampling %zd frames", frames_read);
1229 const int16_t * const data = (int16_t*)buff;
1230 int16_t * const resampled_buffer = (int16_t*)saved_buff;
1231 // Resample with *no* filtering - if the data from the ouptut stream was really
1232 // sampled at a different rate this will result in very nasty aliasing.
1233 const float output_stream_frames = (float)frames_read;
1234 size_t input_stream_frame = 0;
1235 for (float output_stream_frame = 0.0f;
1236 output_stream_frame < output_stream_frames &&
1237 input_stream_frame < remaining_frames;
1238 output_stream_frame += resampler_ratio, input_stream_frame++) {
1239 resampled_buffer[input_stream_frame] = data[(size_t)output_stream_frame];
1240 }
1241 ALOG_ASSERT(input_stream_frame <= (ssize_t)resampler_buffer_size_frames);
1242 SUBMIX_ALOGV("in_read(): resampler produced %zd frames", input_stream_frame);
1243 frames_read = input_stream_frame;
1244 buff = saved_buff;
1245 }
1246#endif // ENABLE_RESAMPLING
1247
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001248 if (frames_read > 0) {
Stewart Miles92854f52014-05-01 09:03:27 -07001249#if LOG_STREAMS_TO_FILES
1250 if (in->log_fd >= 0) write(in->log_fd, buff, frames_read * frame_size);
1251#endif // LOG_STREAMS_TO_FILES
1252
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001253 remaining_frames -= frames_read;
1254 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001255 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
1256 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001257 } else {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001258 attempts++;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001259 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001260 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
1261 }
1262 }
1263 // done using the source
Stewart Milesf645c5e2014-05-01 09:03:27 -07001264 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001265 source.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -07001266 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001267 }
1268
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001269 if (remaining_frames > 0) {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001270 const size_t remaining_bytes = remaining_frames * frame_size;
Stewart Miles10f1a802014-06-09 20:54:37 -07001271 SUBMIX_ALOGV(" clearing remaining_frames = %zu", remaining_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001272 memset(((char*)buffer)+ bytes - remaining_bytes, 0, remaining_bytes);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001273 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001274
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001275 // compute how much we need to sleep after reading the data by comparing the wall clock with
1276 // the projected time at which we should return.
1277 struct timespec time_after_read;// wall clock after reading from the pipe
1278 struct timespec record_duration;// observed record duration
1279 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
1280 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
1281 if (rc == 0) {
1282 // for how long have we been recording?
1283 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
1284 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
1285 if (record_duration.tv_nsec < 0) {
1286 record_duration.tv_sec--;
1287 record_duration.tv_nsec += 1000000000;
1288 }
1289
Stewart Milesf645c5e2014-05-01 09:03:27 -07001290 // read_counter_frames contains the number of frames that have been read since the
1291 // beginning of recording (including this call): it's converted to usec and compared to
1292 // how long we've been recording for, which gives us how long we must wait to sync the
1293 // projected recording time, and the observed recording time.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001294 long projected_vs_observed_offset_us =
1295 ((int64_t)(in->read_counter_frames
1296 - (record_duration.tv_sec*sample_rate)))
1297 * 1000000 / sample_rate
1298 - (record_duration.tv_nsec / 1000);
1299
Stewart Milesc049a0a2014-05-01 09:03:27 -07001300 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001301 record_duration.tv_sec, record_duration.tv_nsec/1000000,
1302 projected_vs_observed_offset_us);
1303 if (projected_vs_observed_offset_us > 0) {
1304 usleep(projected_vs_observed_offset_us);
1305 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001306 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001307
Stewart Milesc049a0a2014-05-01 09:03:27 -07001308 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001309 return bytes;
1310
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001311}
1312
1313static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1314{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001315 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001316 return 0;
1317}
1318
1319static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1320{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001321 (void)stream;
1322 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001323 return 0;
1324}
1325
1326static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1327{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001328 (void)stream;
1329 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001330 return 0;
1331}
1332
1333static int adev_open_output_stream(struct audio_hw_device *dev,
1334 audio_io_handle_t handle,
1335 audio_devices_t devices,
1336 audio_output_flags_t flags,
1337 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -07001338 struct audio_stream_out **stream_out,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001339 const char *address)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001340{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001341 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001342 ALOGD("adev_open_output_stream(address=%s)", address);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001343 struct submix_stream_out *out;
Stewart Miles10f1a802014-06-09 20:54:37 -07001344 bool force_pipe_creation = false;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001345 (void)handle;
1346 (void)devices;
1347 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001348
Stewart Miles3dd36f92014-05-01 09:03:27 -07001349 *stream_out = NULL;
1350
Stewart Miles70726842014-05-01 09:03:27 -07001351 // Make sure it's possible to open the device given the current audio config.
1352 submix_sanitize_config(config, false);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001353
1354 int route_idx = -1;
1355
1356 pthread_mutex_lock(&rsxadev->lock);
1357
1358 status_t res = submix_get_route_idx_for_address_l(rsxadev, address, &route_idx);
1359 if (res != OK) {
1360 ALOGE("Error %d looking for address=%s in adev_open_output_stream", res, address);
1361 pthread_mutex_unlock(&rsxadev->lock);
1362 return res;
1363 }
1364
1365 if (!submix_open_validate_l(rsxadev, route_idx, config, false)) {
1366 ALOGE("adev_open_output_stream(): Unable to open output stream for address %s", address);
1367 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles70726842014-05-01 09:03:27 -07001368 return -EINVAL;
1369 }
1370
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001371 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001372 if (!out) {
1373 pthread_mutex_unlock(&rsxadev->lock);
1374 return -ENOMEM;
1375 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001376
Stewart Miles568e66f2014-05-01 09:03:27 -07001377 // Initialize the function pointer tables (v-tables).
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001378 out->stream.common.get_sample_rate = out_get_sample_rate;
1379 out->stream.common.set_sample_rate = out_set_sample_rate;
1380 out->stream.common.get_buffer_size = out_get_buffer_size;
1381 out->stream.common.get_channels = out_get_channels;
1382 out->stream.common.get_format = out_get_format;
1383 out->stream.common.set_format = out_set_format;
1384 out->stream.common.standby = out_standby;
1385 out->stream.common.dump = out_dump;
1386 out->stream.common.set_parameters = out_set_parameters;
1387 out->stream.common.get_parameters = out_get_parameters;
1388 out->stream.common.add_audio_effect = out_add_audio_effect;
1389 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1390 out->stream.get_latency = out_get_latency;
1391 out->stream.set_volume = out_set_volume;
1392 out->stream.write = out_write;
1393 out->stream.get_render_position = out_get_render_position;
1394 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -07001395 out->stream.get_presentation_position = out_get_presentation_position;
1396
Stewart Miles10f1a802014-06-09 20:54:37 -07001397#if ENABLE_RESAMPLING
1398 // Recreate the pipe with the correct sample rate so that MonoPipe.write() rate limits
1399 // writes correctly.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001400 force_pipe_creation = rsxadev->routes[route_idx].config.common.sample_rate
1401 != config->sample_rate;
Stewart Miles10f1a802014-06-09 20:54:37 -07001402#endif // ENABLE_RESAMPLING
1403
1404 // If the sink has been shutdown or pipe recreation is forced (see above), delete the pipe so
1405 // that it's recreated.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001406 if ((rsxadev->routes[route_idx].rsxSink != NULL
1407 && rsxadev->routes[route_idx].rsxSink->isShutdown()) || force_pipe_creation) {
1408 submix_audio_device_release_pipe_l(rsxadev, route_idx);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001409 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001410
Stewart Miles568e66f2014-05-01 09:03:27 -07001411 // Store a pointer to the device from the output stream.
1412 out->dev = rsxadev;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001413 // Initialize the pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001414 ALOGV("adev_open_output_stream(): about to create pipe at index %d", route_idx);
1415 submix_audio_device_create_pipe_l(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1416 DEFAULT_PIPE_PERIOD_COUNT, NULL, out, address, route_idx);
Stewart Miles92854f52014-05-01 09:03:27 -07001417#if LOG_STREAMS_TO_FILES
1418 out->log_fd = open(LOG_STREAM_OUT_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1419 LOG_STREAM_FILE_PERMISSIONS);
1420 ALOGE_IF(out->log_fd < 0, "adev_open_output_stream(): log file open failed %s",
1421 strerror(errno));
1422 ALOGV("adev_open_output_stream(): log_fd = %d", out->log_fd);
1423#endif // LOG_STREAMS_TO_FILES
Stewart Miles568e66f2014-05-01 09:03:27 -07001424 // Return the output stream.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001425 *stream_out = &out->stream;
1426
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001427 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001428 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001429}
1430
1431static void adev_close_output_stream(struct audio_hw_device *dev,
1432 struct audio_stream_out *stream)
1433{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001434 struct submix_audio_device * rsxadev = audio_hw_device_get_submix_audio_device(
1435 const_cast<struct audio_hw_device*>(dev));
Stewart Miles3dd36f92014-05-01 09:03:27 -07001436 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001437
1438 pthread_mutex_lock(&rsxadev->lock);
1439 ALOGD("adev_close_output_stream() addr = %s", rsxadev->routes[out->route_handle].address);
1440 submix_audio_device_destroy_pipe_l(audio_hw_device_get_submix_audio_device(dev), NULL, out);
Stewart Miles92854f52014-05-01 09:03:27 -07001441#if LOG_STREAMS_TO_FILES
1442 if (out->log_fd >= 0) close(out->log_fd);
1443#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001444
1445 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001446 free(out);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001447}
1448
1449static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1450{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001451 (void)dev;
1452 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001453 return -ENOSYS;
1454}
1455
1456static char * adev_get_parameters(const struct audio_hw_device *dev,
1457 const char *keys)
1458{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001459 (void)dev;
1460 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001461 return strdup("");;
1462}
1463
1464static int adev_init_check(const struct audio_hw_device *dev)
1465{
1466 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -07001467 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001468 return 0;
1469}
1470
1471static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1472{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001473 (void)dev;
1474 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001475 return -ENOSYS;
1476}
1477
1478static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1479{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001480 (void)dev;
1481 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001482 return -ENOSYS;
1483}
1484
1485static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
1486{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001487 (void)dev;
1488 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001489 return -ENOSYS;
1490}
1491
1492static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1493{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001494 (void)dev;
1495 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001496 return -ENOSYS;
1497}
1498
1499static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1500{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001501 (void)dev;
1502 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001503 return -ENOSYS;
1504}
1505
1506static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1507{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001508 (void)dev;
1509 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001510 return 0;
1511}
1512
1513static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1514{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001515 (void)dev;
1516 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001517 return -ENOSYS;
1518}
1519
1520static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1521{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001522 (void)dev;
1523 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001524 return -ENOSYS;
1525}
1526
1527static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1528 const struct audio_config *config)
1529{
Stewart Miles568e66f2014-05-01 09:03:27 -07001530 if (audio_is_linear_pcm(config->format)) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001531 size_t max_buffer_period_size_frames = 0;
1532 struct submix_audio_device * rsxadev = audio_hw_device_get_submix_audio_device(
1533 const_cast<struct audio_hw_device*>(dev));
1534 // look for the largest buffer period size
1535 for (int i = 0 ; i < MAX_ROUTES ; i++) {
1536 if (rsxadev->routes[i].config.buffer_period_size_frames > max_buffer_period_size_frames)
1537 {
1538 max_buffer_period_size_frames = rsxadev->routes[i].config.buffer_period_size_frames;
1539 }
1540 }
Eric Laurentdd45fd32014-07-01 20:32:28 -07001541 const size_t frame_size_in_bytes = audio_channel_count_from_in_mask(config->channel_mask) *
Stewart Miles568e66f2014-05-01 09:03:27 -07001542 audio_bytes_per_sample(config->format);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001543 const size_t buffer_size = max_buffer_period_size_frames * frame_size_in_bytes;
Stewart Miles10f1a802014-06-09 20:54:37 -07001544 SUBMIX_ALOGV("adev_get_input_buffer_size() returns %zu bytes, %zu frames",
Stewart Miles568e66f2014-05-01 09:03:27 -07001545 buffer_size, buffer_period_size_frames);
1546 return buffer_size;
1547 }
1548 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001549}
1550
1551static int adev_open_input_stream(struct audio_hw_device *dev,
1552 audio_io_handle_t handle,
1553 audio_devices_t devices,
1554 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -07001555 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001556 audio_input_flags_t flags __unused,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001557 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001558 audio_source_t source __unused)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001559{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001560 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001561 struct submix_stream_in *in;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001562 ALOGD("adev_open_input_stream(addr=%s)", address);
Stewart Milesc049a0a2014-05-01 09:03:27 -07001563 (void)handle;
1564 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001565
Stewart Miles3dd36f92014-05-01 09:03:27 -07001566 *stream_in = NULL;
1567
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001568 // Do we already have a route for this address
1569 int route_idx = -1;
1570
1571 pthread_mutex_lock(&rsxadev->lock);
1572
1573 status_t res = submix_get_route_idx_for_address_l(rsxadev, address, &route_idx);
1574 if (res != OK) {
Jean-Michel Trivi79fbccf2016-04-05 17:20:29 -07001575 ALOGE("Error %d looking for address=%s in adev_open_input_stream", res, address);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001576 pthread_mutex_unlock(&rsxadev->lock);
1577 return res;
1578 }
1579
Stewart Miles70726842014-05-01 09:03:27 -07001580 // Make sure it's possible to open the device given the current audio config.
1581 submix_sanitize_config(config, true);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001582 if (!submix_open_validate_l(rsxadev, route_idx, config, true)) {
Stewart Miles70726842014-05-01 09:03:27 -07001583 ALOGE("adev_open_input_stream(): Unable to open input stream.");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001584 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles70726842014-05-01 09:03:27 -07001585 return -EINVAL;
1586 }
1587
Stewart Miles3dd36f92014-05-01 09:03:27 -07001588#if ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001589 in = rsxadev->routes[route_idx].input;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001590 if (in) {
1591 in->ref_count++;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001592 sp<MonoPipe> sink = rsxadev->routes[route_idx].rsxSink;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001593 ALOG_ASSERT(sink != NULL);
1594 // If the sink has been shutdown, delete the pipe.
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001595 if (sink != NULL) {
1596 if (sink->isShutdown()) {
1597 ALOGD(" Non-NULL shut down sink when opening input stream, releasing, refcount=%d",
1598 in->ref_count);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001599 submix_audio_device_release_pipe_l(rsxadev, in->route_handle);
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001600 } else {
1601 ALOGD(" Non-NULL sink when opening input stream, refcount=%d", in->ref_count);
1602 }
1603 } else {
1604 ALOGE("NULL sink when opening input stream, refcount=%d", in->ref_count);
1605 }
Stewart Miles3dd36f92014-05-01 09:03:27 -07001606 }
Stewart Miles3dd36f92014-05-01 09:03:27 -07001607#else
1608 in = NULL;
1609#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001610
Stewart Miles3dd36f92014-05-01 09:03:27 -07001611 if (!in) {
1612 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
1613 if (!in) return -ENOMEM;
1614 in->ref_count = 1;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001615
Stewart Miles3dd36f92014-05-01 09:03:27 -07001616 // Initialize the function pointer tables (v-tables).
1617 in->stream.common.get_sample_rate = in_get_sample_rate;
1618 in->stream.common.set_sample_rate = in_set_sample_rate;
1619 in->stream.common.get_buffer_size = in_get_buffer_size;
1620 in->stream.common.get_channels = in_get_channels;
1621 in->stream.common.get_format = in_get_format;
1622 in->stream.common.set_format = in_set_format;
1623 in->stream.common.standby = in_standby;
1624 in->stream.common.dump = in_dump;
1625 in->stream.common.set_parameters = in_set_parameters;
1626 in->stream.common.get_parameters = in_get_parameters;
1627 in->stream.common.add_audio_effect = in_add_audio_effect;
1628 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1629 in->stream.set_gain = in_set_gain;
1630 in->stream.read = in_read;
1631 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001632
1633 in->dev = rsxadev;
1634#if LOG_STREAMS_TO_FILES
1635 in->log_fd = -1;
1636#endif
Stewart Miles3dd36f92014-05-01 09:03:27 -07001637 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001638
Stewart Miles568e66f2014-05-01 09:03:27 -07001639 // Initialize the input stream.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001640 in->read_counter_frames = 0;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001641 in->input_standby = true;
1642 if (rsxadev->routes[route_idx].output != NULL) {
1643 in->output_standby_rec_thr = rsxadev->routes[route_idx].output->output_standby;
1644 } else {
1645 in->output_standby_rec_thr = true;
1646 }
1647
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001648 in->read_error_count = 0;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001649 // Initialize the pipe.
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001650 ALOGV("adev_open_input_stream(): about to create pipe");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001651 submix_audio_device_create_pipe_l(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1652 DEFAULT_PIPE_PERIOD_COUNT, in, NULL, address, route_idx);
Stewart Miles92854f52014-05-01 09:03:27 -07001653#if LOG_STREAMS_TO_FILES
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001654 if (in->log_fd >= 0) close(in->log_fd);
Stewart Miles92854f52014-05-01 09:03:27 -07001655 in->log_fd = open(LOG_STREAM_IN_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1656 LOG_STREAM_FILE_PERMISSIONS);
1657 ALOGE_IF(in->log_fd < 0, "adev_open_input_stream(): log file open failed %s",
1658 strerror(errno));
1659 ALOGV("adev_open_input_stream(): log_fd = %d", in->log_fd);
1660#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001661 // Return the input stream.
1662 *stream_in = &in->stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001663
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001664 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001665 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001666}
1667
1668static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -07001669 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001670{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001671 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
1672
Stewart Miles3dd36f92014-05-01 09:03:27 -07001673 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001674 ALOGD("adev_close_input_stream()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001675 pthread_mutex_lock(&rsxadev->lock);
1676 submix_audio_device_destroy_pipe_l(rsxadev, in, NULL);
Stewart Miles92854f52014-05-01 09:03:27 -07001677#if LOG_STREAMS_TO_FILES
1678 if (in->log_fd >= 0) close(in->log_fd);
1679#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001680#if ENABLE_LEGACY_INPUT_OPEN
1681 if (in->ref_count == 0) free(in);
1682#else
1683 free(in);
1684#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001685
1686 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001687}
1688
1689static int adev_dump(const audio_hw_device_t *device, int fd)
1690{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001691 const struct submix_audio_device * rsxadev = //audio_hw_device_get_submix_audio_device(device);
1692 reinterpret_cast<const struct submix_audio_device *>(
1693 reinterpret_cast<const uint8_t *>(device) -
1694 offsetof(struct submix_audio_device, device));
1695 char msg[100];
1696 int n = sprintf(msg, "\nReroute submix audio module:\n");
1697 write(fd, &msg, n);
1698 for (int i=0 ; i < MAX_ROUTES ; i++) {
1699 n = sprintf(msg, " route[%d] rate in=%d out=%d, addr=[%s]\n", i,
1700 rsxadev->routes[i].config.input_sample_rate,
1701 rsxadev->routes[i].config.output_sample_rate,
1702 rsxadev->routes[i].address);
1703 write(fd, &msg, n);
1704 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001705 return 0;
1706}
1707
1708static int adev_close(hw_device_t *device)
1709{
1710 ALOGI("adev_close()");
1711 free(device);
1712 return 0;
1713}
1714
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001715static int adev_open(const hw_module_t* module, const char* name,
1716 hw_device_t** device)
1717{
1718 ALOGI("adev_open(name=%s)", name);
1719 struct submix_audio_device *rsxadev;
1720
1721 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1722 return -EINVAL;
1723
1724 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
1725 if (!rsxadev)
1726 return -ENOMEM;
1727
1728 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -07001729 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001730 rsxadev->device.common.module = (struct hw_module_t *) module;
1731 rsxadev->device.common.close = adev_close;
1732
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001733 rsxadev->device.init_check = adev_init_check;
1734 rsxadev->device.set_voice_volume = adev_set_voice_volume;
1735 rsxadev->device.set_master_volume = adev_set_master_volume;
1736 rsxadev->device.get_master_volume = adev_get_master_volume;
1737 rsxadev->device.set_master_mute = adev_set_master_mute;
1738 rsxadev->device.get_master_mute = adev_get_master_mute;
1739 rsxadev->device.set_mode = adev_set_mode;
1740 rsxadev->device.set_mic_mute = adev_set_mic_mute;
1741 rsxadev->device.get_mic_mute = adev_get_mic_mute;
1742 rsxadev->device.set_parameters = adev_set_parameters;
1743 rsxadev->device.get_parameters = adev_get_parameters;
1744 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
1745 rsxadev->device.open_output_stream = adev_open_output_stream;
1746 rsxadev->device.close_output_stream = adev_close_output_stream;
1747 rsxadev->device.open_input_stream = adev_open_input_stream;
1748 rsxadev->device.close_input_stream = adev_close_input_stream;
1749 rsxadev->device.dump = adev_dump;
1750
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001751 for (int i=0 ; i < MAX_ROUTES ; i++) {
1752 memset(&rsxadev->routes[i], 0, sizeof(route_config));
1753 strcpy(rsxadev->routes[i].address, "");
1754 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001755
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001756 *device = &rsxadev->device.common;
1757
1758 return 0;
1759}
1760
1761static struct hw_module_methods_t hal_module_methods = {
1762 /* open */ adev_open,
1763};
1764
1765struct audio_module HAL_MODULE_INFO_SYM = {
1766 /* common */ {
1767 /* tag */ HARDWARE_MODULE_TAG,
1768 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
1769 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
1770 /* id */ AUDIO_HARDWARE_MODULE_ID,
1771 /* name */ "Wifi Display audio HAL",
1772 /* author */ "The Android Open Source Project",
1773 /* methods */ &hal_module_methods,
1774 /* dso */ NULL,
1775 /* reserved */ { 0 },
1776 },
1777};
1778
1779} //namespace android
1780
1781} //extern "C"