blob: 72e1fa550ff72d006b59149dd3252d0f82162db4 [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>
Jiyong Park118f3dc2017-07-04 12:15:40 +090027#include <unistd.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070028
Jean-Michel Trivi25f47512015-05-26 14:18:10 -070029#include <cutils/compiler.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070030#include <cutils/properties.h>
Stewart Milesc049a0a2014-05-01 09:03:27 -070031#include <cutils/str_parms.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070032#include <log/log.h>
33#include <utils/String8.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070034
Stewart Milesc049a0a2014-05-01 09:03:27 -070035#include <hardware/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070036#include <hardware/hardware.h>
37#include <system/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070038
Stewart Milesc049a0a2014-05-01 09:03:27 -070039#include <media/AudioParameter.h>
40#include <media/AudioBufferProvider.h>
Jean-Michel Trivieec87702012-09-17 09:59:42 -070041#include <media/nbaio/MonoPipe.h>
42#include <media/nbaio/MonoPipeReader.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070043
Stewart Miles92854f52014-05-01 09:03:27 -070044#define LOG_STREAMS_TO_FILES 0
45#if LOG_STREAMS_TO_FILES
46#include <fcntl.h>
47#include <stdio.h>
48#include <sys/stat.h>
49#endif // LOG_STREAMS_TO_FILES
50
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070051extern "C" {
52
53namespace android {
54
Mikhail Naganov80179932018-02-15 17:07:19 -080055// Uncomment to enable extremely verbose logging in this module.
56// #define SUBMIX_VERBOSE_LOGGING
57#if defined(SUBMIX_VERBOSE_LOGGING)
Stewart Milesc049a0a2014-05-01 09:03:27 -070058#define SUBMIX_ALOGV(...) ALOGV(__VA_ARGS__)
59#define SUBMIX_ALOGE(...) ALOGE(__VA_ARGS__)
60#else
61#define SUBMIX_ALOGV(...)
62#define SUBMIX_ALOGE(...)
63#endif // SUBMIX_VERBOSE_LOGGING
64
Stewart Miles3dd36f92014-05-01 09:03:27 -070065// NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe().
Jean-Michel Trivibbb3e772015-05-26 15:59:42 -070066#define DEFAULT_PIPE_SIZE_IN_FRAMES (1024*4)
Stewart Miles3dd36f92014-05-01 09:03:27 -070067// Value used to divide the MonoPipe() buffer into segments that are written to the source and
68// read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer
69// the minimum latency is the MonoPipe buffer size divided by this value.
70#define DEFAULT_PIPE_PERIOD_COUNT 4
Jean-Michel Trivieec87702012-09-17 09:59:42 -070071// The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to
72// the duration of a record buffer at the current record sample rate (of the device, not of
73// the recording itself). Here we have:
74// 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -070075#define MAX_READ_ATTEMPTS 3
Jean-Michel Trivieec87702012-09-17 09:59:42 -070076#define READ_ATTEMPT_SLEEP_MS 5 // 5ms between two read attempts when pipe is empty
Stewart Miles568e66f2014-05-01 09:03:27 -070077#define DEFAULT_SAMPLE_RATE_HZ 48000 // default sample rate
78// See NBAIO_Format frameworks/av/include/media/nbaio/NBAIO.h.
79#define DEFAULT_FORMAT AUDIO_FORMAT_PCM_16_BIT
Stewart Miles3dd36f92014-05-01 09:03:27 -070080// A legacy user of this device does not close the input stream when it shuts down, which
81// results in the application opening a new input stream before closing the old input stream
82// handle it was previously using. Setting this value to 1 allows multiple clients to open
83// multiple input streams from this device. If this option is enabled, each input stream returned
84// is *the same stream* which means that readers will race to read data from these streams.
85#define ENABLE_LEGACY_INPUT_OPEN 1
Stewart Milese54c12c2014-05-01 09:03:27 -070086// Whether channel conversion (16-bit signed PCM mono->stereo, stereo->mono) is enabled.
87#define ENABLE_CHANNEL_CONVERSION 1
Stewart Miles02c2f712014-05-01 09:03:27 -070088// Whether resampling is enabled.
89#define ENABLE_RESAMPLING 1
Stewart Miles92854f52014-05-01 09:03:27 -070090#if LOG_STREAMS_TO_FILES
91// Folder to save stream log files to.
Eric Laurent854a10a2016-02-19 14:41:51 -080092#define LOG_STREAM_FOLDER "/data/misc/audioserver"
Stewart Miles92854f52014-05-01 09:03:27 -070093// Log filenames for input and output streams.
94#define LOG_STREAM_OUT_FILENAME LOG_STREAM_FOLDER "/r_submix_out.raw"
95#define LOG_STREAM_IN_FILENAME LOG_STREAM_FOLDER "/r_submix_in.raw"
96// File permissions for stream log files.
97#define LOG_STREAM_FILE_PERMISSIONS (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
98#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi793a8542014-10-14 15:31:51 -070099// limit for number of read error log entries to avoid spamming the logs
100#define MAX_READ_ERROR_LOGS 5
Stewart Miles3dd36f92014-05-01 09:03:27 -0700101
102// Common limits macros.
103#ifndef min
104#define min(a, b) ((a) < (b) ? (a) : (b))
105#endif // min
Stewart Milese54c12c2014-05-01 09:03:27 -0700106#ifndef max
107#define max(a, b) ((a) > (b) ? (a) : (b))
108#endif // max
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700109
Stewart Miles70726842014-05-01 09:03:27 -0700110// Set *result_variable_ptr to true if value_to_find is present in the array array_to_search,
111// otherwise set *result_variable_ptr to false.
112#define SUBMIX_VALUE_IN_SET(value_to_find, array_to_search, result_variable_ptr) \
113 { \
114 size_t i; \
115 *(result_variable_ptr) = false; \
116 for (i = 0; i < sizeof(array_to_search) / sizeof((array_to_search)[0]); i++) { \
117 if ((value_to_find) == (array_to_search)[i]) { \
118 *(result_variable_ptr) = true; \
119 break; \
120 } \
121 } \
122 }
123
Stewart Miles568e66f2014-05-01 09:03:27 -0700124// Configuration of the submix pipe.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700125struct submix_config {
Stewart Miles70726842014-05-01 09:03:27 -0700126 // Channel mask field in this data structure is set to either input_channel_mask or
127 // output_channel_mask depending upon the last stream to be opened on this device.
128 struct audio_config common;
129 // Input stream and output stream channel masks. This is required since input and output
130 // channel bitfields are not equivalent.
131 audio_channel_mask_t input_channel_mask;
132 audio_channel_mask_t output_channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700133#if ENABLE_RESAMPLING
134 // Input stream and output stream sample rates.
135 uint32_t input_sample_rate;
136 uint32_t output_sample_rate;
137#endif // ENABLE_RESAMPLING
Stewart Milese54c12c2014-05-01 09:03:27 -0700138 size_t pipe_frame_size; // Number of bytes in each audio frame in the pipe.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700139 size_t buffer_size_frames; // Size of the audio pipe in frames.
140 // Maximum number of frames buffered by the input and output streams.
141 size_t buffer_period_size_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700142};
143
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800144#define MAX_ROUTES 10
145typedef struct route_config {
146 struct submix_config config;
147 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700148 // Pipe variables: they handle the ring buffer that "pipes" audio:
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700149 // - from the submix virtual audio output == what needs to be played
150 // remotely, seen as an output for AudioFlinger
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700151 // - to the virtual audio source == what is captured by the component
152 // which "records" the submix / virtual audio source, and handles it as needed.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700153 // A usecase example is one where the component capturing the audio is then sending it over
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700154 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
155 // TV with Wifi Display capabilities), or to a wireless audio player.
Stewart Miles568e66f2014-05-01 09:03:27 -0700156 sp<MonoPipe> rsxSink;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700157 sp<MonoPipeReader> rsxSource;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800158 // Pointers to the current input and output stream instances. rsxSink and rsxSource are
159 // destroyed if both and input and output streams are destroyed.
160 struct submix_stream_out *output;
161 struct submix_stream_in *input;
Stewart Miles02c2f712014-05-01 09:03:27 -0700162#if ENABLE_RESAMPLING
163 // Buffer used as temporary storage for resampled data prior to returning data to the output
164 // stream.
165 int16_t resampler_buffer[DEFAULT_PIPE_SIZE_IN_FRAMES];
166#endif // ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800167} route_config_t;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700168
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800169struct submix_audio_device {
170 struct audio_hw_device device;
171 route_config_t routes[MAX_ROUTES];
Stewart Miles568e66f2014-05-01 09:03:27 -0700172 // Device lock, also used to protect access to submix_audio_device from the input and output
173 // streams.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700174 pthread_mutex_t lock;
175};
176
177struct submix_stream_out {
178 struct audio_stream_out stream;
179 struct submix_audio_device *dev;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800180 int route_handle;
181 bool output_standby;
Andy Hung0b93c0a2015-08-10 13:52:34 -0700182 uint64_t frames_written;
183 uint64_t frames_written_since_standby;
Stewart Miles92854f52014-05-01 09:03:27 -0700184#if LOG_STREAMS_TO_FILES
185 int log_fd;
186#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700187};
188
189struct submix_stream_in {
190 struct audio_stream_in stream;
191 struct submix_audio_device *dev;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800192 int route_handle;
193 bool input_standby;
194 bool output_standby_rec_thr; // output standby state as seen from record thread
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700195 // wall clock when recording starts
196 struct timespec record_start_time;
197 // how many frames have been requested to be read
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700198 uint64_t read_counter_frames;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700199
200#if ENABLE_LEGACY_INPUT_OPEN
201 // Number of references to this input stream.
202 volatile int32_t ref_count;
203#endif // ENABLE_LEGACY_INPUT_OPEN
Stewart Miles92854f52014-05-01 09:03:27 -0700204#if LOG_STREAMS_TO_FILES
205 int log_fd;
206#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi793a8542014-10-14 15:31:51 -0700207
Mikhail Naganov80179932018-02-15 17:07:19 -0800208 volatile uint16_t read_error_count;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700209};
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700210
Stewart Miles70726842014-05-01 09:03:27 -0700211// Determine whether the specified sample rate is supported by the submix module.
212static bool sample_rate_supported(const uint32_t sample_rate)
213{
214 // Set of sample rates supported by Format_from_SR_C() frameworks/av/media/libnbaio/NAIO.cpp.
215 static const unsigned int supported_sample_rates[] = {
216 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
217 };
218 bool return_value;
219 SUBMIX_VALUE_IN_SET(sample_rate, supported_sample_rates, &return_value);
220 return return_value;
221}
222
223// Determine whether the specified sample rate is supported, if it is return the specified sample
224// rate, otherwise return the default sample rate for the submix module.
225static uint32_t get_supported_sample_rate(uint32_t sample_rate)
226{
227 return sample_rate_supported(sample_rate) ? sample_rate : DEFAULT_SAMPLE_RATE_HZ;
228}
229
230// Determine whether the specified channel in mask is supported by the submix module.
231static bool channel_in_mask_supported(const audio_channel_mask_t channel_in_mask)
232{
233 // Set of channel in masks supported by Format_from_SR_C()
234 // frameworks/av/media/libnbaio/NAIO.cpp.
235 static const audio_channel_mask_t supported_channel_in_masks[] = {
236 AUDIO_CHANNEL_IN_MONO, AUDIO_CHANNEL_IN_STEREO,
237 };
238 bool return_value;
239 SUBMIX_VALUE_IN_SET(channel_in_mask, supported_channel_in_masks, &return_value);
240 return return_value;
241}
242
243// Determine whether the specified channel in mask is supported, if it is return the specified
244// channel in mask, otherwise return the default channel in mask for the submix module.
245static audio_channel_mask_t get_supported_channel_in_mask(
246 const audio_channel_mask_t channel_in_mask)
247{
248 return channel_in_mask_supported(channel_in_mask) ? channel_in_mask :
249 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_IN_STEREO);
250}
251
252// Determine whether the specified channel out mask is supported by the submix module.
253static bool channel_out_mask_supported(const audio_channel_mask_t channel_out_mask)
254{
255 // Set of channel out masks supported by Format_from_SR_C()
256 // frameworks/av/media/libnbaio/NAIO.cpp.
257 static const audio_channel_mask_t supported_channel_out_masks[] = {
258 AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO,
259 };
260 bool return_value;
261 SUBMIX_VALUE_IN_SET(channel_out_mask, supported_channel_out_masks, &return_value);
262 return return_value;
263}
264
265// Determine whether the specified channel out mask is supported, if it is return the specified
266// channel out mask, otherwise return the default channel out mask for the submix module.
267static audio_channel_mask_t get_supported_channel_out_mask(
268 const audio_channel_mask_t channel_out_mask)
269{
270 return channel_out_mask_supported(channel_out_mask) ? channel_out_mask :
271 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_OUT_STEREO);
272}
273
Stewart Milesf645c5e2014-05-01 09:03:27 -0700274// Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the
275// structure.
276static struct submix_stream_out * audio_stream_out_get_submix_stream_out(
277 struct audio_stream_out * const stream)
278{
279 ALOG_ASSERT(stream);
280 return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) -
281 offsetof(struct submix_stream_out, stream));
282}
283
284// Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure.
285static struct submix_stream_out * audio_stream_get_submix_stream_out(
286 struct audio_stream * const stream)
287{
288 ALOG_ASSERT(stream);
289 return audio_stream_out_get_submix_stream_out(
290 reinterpret_cast<struct audio_stream_out *>(stream));
291}
292
293// Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the
294// structure.
295static struct submix_stream_in * audio_stream_in_get_submix_stream_in(
296 struct audio_stream_in * const stream)
297{
298 ALOG_ASSERT(stream);
299 return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) -
300 offsetof(struct submix_stream_in, stream));
301}
302
303// Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure.
304static struct submix_stream_in * audio_stream_get_submix_stream_in(
305 struct audio_stream * const stream)
306{
307 ALOG_ASSERT(stream);
308 return audio_stream_in_get_submix_stream_in(
309 reinterpret_cast<struct audio_stream_in *>(stream));
310}
311
312// Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within
313// the structure.
314static struct submix_audio_device * audio_hw_device_get_submix_audio_device(
315 struct audio_hw_device *device)
316{
317 ALOG_ASSERT(device);
318 return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) -
319 offsetof(struct submix_audio_device, device));
320}
321
Stewart Miles70726842014-05-01 09:03:27 -0700322// Compare an audio_config with input channel mask and an audio_config with output channel mask
323// returning false if they do *not* match, true otherwise.
324static bool audio_config_compare(const audio_config * const input_config,
325 const audio_config * const output_config)
326{
Stewart Milese54c12c2014-05-01 09:03:27 -0700327#if !ENABLE_CHANNEL_CONVERSION
Eric Laurentdd45fd32014-07-01 20:32:28 -0700328 const uint32_t input_channels = audio_channel_count_from_in_mask(input_config->channel_mask);
329 const uint32_t output_channels = audio_channel_count_from_out_mask(output_config->channel_mask);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700330 if (input_channels != output_channels) {
331 ALOGE("audio_config_compare() channel count mismatch input=%d vs. output=%d",
332 input_channels, output_channels);
Stewart Miles70726842014-05-01 09:03:27 -0700333 return false;
334 }
Stewart Milese54c12c2014-05-01 09:03:27 -0700335#endif // !ENABLE_CHANNEL_CONVERSION
Stewart Miles02c2f712014-05-01 09:03:27 -0700336#if ENABLE_RESAMPLING
337 if (input_config->sample_rate != output_config->sample_rate &&
Eric Laurentdd45fd32014-07-01 20:32:28 -0700338 audio_channel_count_from_in_mask(input_config->channel_mask) != 1) {
Stewart Miles02c2f712014-05-01 09:03:27 -0700339#else
Stewart Miles70726842014-05-01 09:03:27 -0700340 if (input_config->sample_rate != output_config->sample_rate) {
Stewart Miles02c2f712014-05-01 09:03:27 -0700341#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700342 ALOGE("audio_config_compare() sample rate mismatch %ul vs. %ul",
343 input_config->sample_rate, output_config->sample_rate);
344 return false;
345 }
346 if (input_config->format != output_config->format) {
347 ALOGE("audio_config_compare() format mismatch %x vs. %x",
348 input_config->format, output_config->format);
349 return false;
350 }
351 // This purposely ignores offload_info as it's not required for the submix device.
352 return true;
353}
354
Stewart Miles3dd36f92014-05-01 09:03:27 -0700355// If one doesn't exist, create a pipe for the submix audio device rsxadev of size
356// buffer_size_frames and optionally associate "in" or "out" with the submix audio device.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800357// Must be called with lock held on the submix_audio_device
358static void submix_audio_device_create_pipe_l(struct submix_audio_device * const rsxadev,
Stewart Miles3dd36f92014-05-01 09:03:27 -0700359 const struct audio_config * const config,
360 const size_t buffer_size_frames,
361 const uint32_t buffer_period_count,
362 struct submix_stream_in * const in,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800363 struct submix_stream_out * const out,
364 const char *address,
365 int route_idx)
Stewart Miles3dd36f92014-05-01 09:03:27 -0700366{
367 ALOG_ASSERT(in || out);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800368 ALOG_ASSERT(route_idx > -1);
369 ALOG_ASSERT(route_idx < MAX_ROUTES);
370 ALOGD("submix_audio_device_create_pipe_l(addr=%s, idx=%d)", address, route_idx);
371
Stewart Miles3dd36f92014-05-01 09:03:27 -0700372 // Save a reference to the specified input or output stream and the associated channel
373 // mask.
374 if (in) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800375 in->route_handle = route_idx;
376 rsxadev->routes[route_idx].input = in;
377 rsxadev->routes[route_idx].config.input_channel_mask = config->channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700378#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800379 rsxadev->routes[route_idx].config.input_sample_rate = config->sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700380 // If the output isn't configured yet, set the output sample rate to the maximum supported
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800381 // sample rate such that the smallest possible input buffer is created, and put a default
382 // value for channel count
383 if (!rsxadev->routes[route_idx].output) {
384 rsxadev->routes[route_idx].config.output_sample_rate = 48000;
385 rsxadev->routes[route_idx].config.output_channel_mask = AUDIO_CHANNEL_OUT_STEREO;
Stewart Miles02c2f712014-05-01 09:03:27 -0700386 }
387#endif // ENABLE_RESAMPLING
Stewart Miles3dd36f92014-05-01 09:03:27 -0700388 }
389 if (out) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800390 out->route_handle = route_idx;
391 rsxadev->routes[route_idx].output = out;
392 rsxadev->routes[route_idx].config.output_channel_mask = config->channel_mask;
Stewart Miles02c2f712014-05-01 09:03:27 -0700393#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800394 rsxadev->routes[route_idx].config.output_sample_rate = config->sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700395#endif // ENABLE_RESAMPLING
Stewart Miles3dd36f92014-05-01 09:03:27 -0700396 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800397 // Save the address
398 strncpy(rsxadev->routes[route_idx].address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
399 ALOGD(" now using address %s for route %d", rsxadev->routes[route_idx].address, route_idx);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700400 // If a pipe isn't associated with the device, create one.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800401 if (rsxadev->routes[route_idx].rsxSink == NULL || rsxadev->routes[route_idx].rsxSource == NULL)
402 {
403 struct submix_config * const device_config = &rsxadev->routes[route_idx].config;
Eric Laurentdd45fd32014-07-01 20:32:28 -0700404 uint32_t channel_count;
405 if (out)
406 channel_count = audio_channel_count_from_out_mask(config->channel_mask);
407 else
408 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Stewart Miles10f1a802014-06-09 20:54:37 -0700409#if ENABLE_CHANNEL_CONVERSION
410 // If channel conversion is enabled, allocate enough space for the maximum number of
411 // possible channels stored in the pipe for the situation when the number of channels in
412 // the output stream don't match the number in the input stream.
413 const uint32_t pipe_channel_count = max(channel_count, 2);
414#else
415 const uint32_t pipe_channel_count = channel_count;
416#endif // ENABLE_CHANNEL_CONVERSION
417 const NBAIO_Format format = Format_from_SR_C(config->sample_rate, pipe_channel_count,
418 config->format);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700419 const NBAIO_Format offers[1] = {format};
420 size_t numCounterOffers = 0;
Mikhail Naganov1d0e9732018-03-05 12:24:45 -0800421 // Create a MonoPipe with optional blocking set to true.
422 MonoPipe* sink = new MonoPipe(buffer_size_frames, format, true /*writeCanBlock*/);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700423 // Negotiation between the source and sink cannot fail as the device open operation
424 // creates both ends of the pipe using the same audio format.
425 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
426 ALOG_ASSERT(index == 0);
427 MonoPipeReader* source = new MonoPipeReader(sink);
428 numCounterOffers = 0;
429 index = source->negotiate(offers, 1, NULL, numCounterOffers);
430 ALOG_ASSERT(index == 0);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800431 ALOGV("submix_audio_device_create_pipe_l(): created pipe");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700432
433 // Save references to the source and sink.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800434 ALOG_ASSERT(rsxadev->routes[route_idx].rsxSink == NULL);
435 ALOG_ASSERT(rsxadev->routes[route_idx].rsxSource == NULL);
436 rsxadev->routes[route_idx].rsxSink = sink;
437 rsxadev->routes[route_idx].rsxSource = source;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700438 // Store the sanitized audio format in the device so that it's possible to determine
439 // the format of the pipe source when opening the input device.
440 memcpy(&device_config->common, config, sizeof(device_config->common));
441 device_config->buffer_size_frames = sink->maxFrames();
442 device_config->buffer_period_size_frames = device_config->buffer_size_frames /
443 buffer_period_count;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700444 if (in) device_config->pipe_frame_size = audio_stream_in_frame_size(&in->stream);
445 if (out) device_config->pipe_frame_size = audio_stream_out_frame_size(&out->stream);
Stewart Miles10f1a802014-06-09 20:54:37 -0700446#if ENABLE_CHANNEL_CONVERSION
447 // Calculate the pipe frame size based upon the number of channels.
448 device_config->pipe_frame_size = (device_config->pipe_frame_size * pipe_channel_count) /
449 channel_count;
450#endif // ENABLE_CHANNEL_CONVERSION
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800451 SUBMIX_ALOGV("submix_audio_device_create_pipe_l(): pipe frame size %zd, pipe size %zd, "
Stewart Milese54c12c2014-05-01 09:03:27 -0700452 "period size %zd", device_config->pipe_frame_size,
453 device_config->buffer_size_frames, device_config->buffer_period_size_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700454 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700455}
456
457// Release references to the sink and source. Input and output threads may maintain references
458// to these objects via StrongPointer (sp<MonoPipe> and sp<MonoPipeReader>) which they can use
459// before they shutdown.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800460// Must be called with lock held on the submix_audio_device
461static void submix_audio_device_release_pipe_l(struct submix_audio_device * const rsxadev,
462 int route_idx)
Stewart Miles3dd36f92014-05-01 09:03:27 -0700463{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800464 ALOG_ASSERT(route_idx > -1);
465 ALOG_ASSERT(route_idx < MAX_ROUTES);
466 ALOGD("submix_audio_device_release_pipe_l(idx=%d) addr=%s", route_idx,
467 rsxadev->routes[route_idx].address);
468 if (rsxadev->routes[route_idx].rsxSink != 0) {
469 rsxadev->routes[route_idx].rsxSink.clear();
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800470 }
471 if (rsxadev->routes[route_idx].rsxSource != 0) {
472 rsxadev->routes[route_idx].rsxSource.clear();
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800473 }
474 memset(rsxadev->routes[route_idx].address, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
Mikhail Naganov1462c762019-07-26 09:22:34 -0700475#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800476 memset(rsxadev->routes[route_idx].resampler_buffer, 0,
477 sizeof(int16_t) * DEFAULT_PIPE_SIZE_IN_FRAMES);
478#endif
Stewart Miles3dd36f92014-05-01 09:03:27 -0700479}
480
481// Remove references to the specified input and output streams. When the device no longer
482// references input and output streams destroy the associated pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800483// Must be called with lock held on the submix_audio_device
484static void submix_audio_device_destroy_pipe_l(struct submix_audio_device * const rsxadev,
Stewart Miles3dd36f92014-05-01 09:03:27 -0700485 const struct submix_stream_in * const in,
486 const struct submix_stream_out * const out)
487{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800488 ALOGV("submix_audio_device_destroy_pipe_l()");
489 int route_idx = -1;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700490 if (in != NULL) {
Eric Laurent5b78d412019-03-01 18:39:26 -0800491 bool shut_down = false;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700492#if ENABLE_LEGACY_INPUT_OPEN
493 const_cast<struct submix_stream_in*>(in)->ref_count--;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800494 route_idx = in->route_handle;
495 ALOG_ASSERT(rsxadev->routes[route_idx].input == in);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700496 if (in->ref_count == 0) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800497 rsxadev->routes[route_idx].input = NULL;
Eric Laurent5b78d412019-03-01 18:39:26 -0800498 shut_down = true;
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
Mikhail Naganov1462c762019-07-26 09:22:34 -0700502 route_idx = in->route_handle;
503 ALOG_ASSERT(rsxadev->routes[route_idx].input == in);
504 rsxadev->routes[route_idx].input = NULL;
Eric Laurent5b78d412019-03-01 18:39:26 -0800505 shut_down = true;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700506#endif // ENABLE_LEGACY_INPUT_OPEN
Eric Laurent5b78d412019-03-01 18:39:26 -0800507 if (shut_down) {
508 sp <MonoPipe> sink = rsxadev->routes[in->route_handle].rsxSink;
509 if (sink != NULL) {
510 sink->shutdown(true);
511 }
512 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700513 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800514 if (out != NULL) {
515 route_idx = out->route_handle;
516 ALOG_ASSERT(rsxadev->routes[route_idx].output == out);
517 rsxadev->routes[route_idx].output = NULL;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700518 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800519 if (route_idx != -1 &&
520 rsxadev->routes[route_idx].input == NULL && rsxadev->routes[route_idx].output == NULL) {
521 submix_audio_device_release_pipe_l(rsxadev, route_idx);
522 ALOGD("submix_audio_device_destroy_pipe_l(): pipe destroyed");
523 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700524}
525
Stewart Miles70726842014-05-01 09:03:27 -0700526// Sanitize the user specified audio config for a submix input / output stream.
527static void submix_sanitize_config(struct audio_config * const config, const bool is_input_format)
528{
529 config->channel_mask = is_input_format ? get_supported_channel_in_mask(config->channel_mask) :
530 get_supported_channel_out_mask(config->channel_mask);
531 config->sample_rate = get_supported_sample_rate(config->sample_rate);
532 config->format = DEFAULT_FORMAT;
533}
534
535// Verify a submix input or output stream can be opened.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800536// Must be called with lock held on the submix_audio_device
537static bool submix_open_validate_l(const struct submix_audio_device * const rsxadev,
538 int route_idx,
Stewart Miles70726842014-05-01 09:03:27 -0700539 const struct audio_config * const config,
540 const bool opening_input)
541{
Stewart Miles3dd36f92014-05-01 09:03:27 -0700542 bool input_open;
543 bool output_open;
Stewart Miles70726842014-05-01 09:03:27 -0700544 audio_config pipe_config;
545
546 // Query the device for the current audio config and whether input and output streams are open.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800547 output_open = rsxadev->routes[route_idx].output != NULL;
548 input_open = rsxadev->routes[route_idx].input != NULL;
549 memcpy(&pipe_config, &rsxadev->routes[route_idx].config.common, sizeof(pipe_config));
Stewart Miles70726842014-05-01 09:03:27 -0700550
Stewart Miles3dd36f92014-05-01 09:03:27 -0700551 // If the stream is already open, don't open it again.
552 if (opening_input ? !ENABLE_LEGACY_INPUT_OPEN && input_open : output_open) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800553 ALOGE("submix_open_validate_l(): %s stream already open.", opening_input ? "Input" :
Stewart Miles3dd36f92014-05-01 09:03:27 -0700554 "Output");
555 return false;
556 }
557
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800558 SUBMIX_ALOGV("submix_open_validate_l(): sample rate=%d format=%x "
Stewart Miles3dd36f92014-05-01 09:03:27 -0700559 "%s_channel_mask=%x", config->sample_rate, config->format,
560 opening_input ? "in" : "out", config->channel_mask);
561
562 // If either stream is open, verify the existing audio config the pipe matches the user
Stewart Miles70726842014-05-01 09:03:27 -0700563 // specified config.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700564 if (input_open || output_open) {
Stewart Miles70726842014-05-01 09:03:27 -0700565 const audio_config * const input_config = opening_input ? config : &pipe_config;
566 const audio_config * const output_config = opening_input ? &pipe_config : config;
567 // Get the channel mask of the open device.
568 pipe_config.channel_mask =
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800569 opening_input ? rsxadev->routes[route_idx].config.output_channel_mask :
570 rsxadev->routes[route_idx].config.input_channel_mask;
Stewart Miles70726842014-05-01 09:03:27 -0700571 if (!audio_config_compare(input_config, output_config)) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800572 ALOGE("submix_open_validate_l(): Unsupported format.");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700573 return false;
Stewart Miles70726842014-05-01 09:03:27 -0700574 }
575 }
576 return true;
577}
578
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800579// Must be called with lock held on the submix_audio_device
580static status_t submix_get_route_idx_for_address_l(const struct submix_audio_device * const rsxadev,
581 const char* address, /*in*/
582 int *idx /*out*/)
583{
584 // Do we already have a route for this address
585 int route_idx = -1;
586 int route_empty_idx = -1; // index of an empty route slot that can be used if needed
587 for (int i=0 ; i < MAX_ROUTES ; i++) {
588 if (strcmp(rsxadev->routes[i].address, "") == 0) {
589 route_empty_idx = i;
590 }
591 if (strncmp(rsxadev->routes[i].address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
592 route_idx = i;
593 break;
594 }
595 }
596
597 if ((route_idx == -1) && (route_empty_idx == -1)) {
598 ALOGE("Cannot create new route for address %s, max number of routes reached", address);
599 return -ENOMEM;
600 }
601 if (route_idx == -1) {
602 route_idx = route_empty_idx;
603 }
604 *idx = route_idx;
605 return OK;
606}
607
608
Stewart Milese54c12c2014-05-01 09:03:27 -0700609// Calculate the maximum size of the pipe buffer in frames for the specified stream.
610static size_t calculate_stream_pipe_size_in_frames(const struct audio_stream *stream,
611 const struct submix_config *config,
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700612 const size_t pipe_frames,
613 const size_t stream_frame_size)
Stewart Milese54c12c2014-05-01 09:03:27 -0700614{
Stewart Milese54c12c2014-05-01 09:03:27 -0700615 const size_t pipe_frame_size = config->pipe_frame_size;
616 const size_t max_frame_size = max(stream_frame_size, pipe_frame_size);
617 return (pipe_frames * config->pipe_frame_size) / max_frame_size;
618}
619
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700620/* audio HAL functions */
621
622static uint32_t out_get_sample_rate(const struct audio_stream *stream)
623{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700624 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
625 const_cast<struct audio_stream *>(stream));
Stewart Miles02c2f712014-05-01 09:03:27 -0700626#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800627 const uint32_t out_rate = out->dev->routes[out->route_handle].config.output_sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700628#else
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800629 const uint32_t out_rate = out->dev->routes[out->route_handle].config.common.sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700630#endif // ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800631 SUBMIX_ALOGV("out_get_sample_rate() returns %u for addr %s",
632 out_rate, out->dev->routes[out->route_handle].address);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700633 return out_rate;
634}
635
636static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
637{
Stewart Miles02c2f712014-05-01 09:03:27 -0700638 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
639#if ENABLE_RESAMPLING
640 // The sample rate of the stream can't be changed once it's set since this would change the
641 // output buffer size and hence break playback to the shared pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800642 if (rate != out->dev->routes[out->route_handle].config.output_sample_rate) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700643 ALOGE("out_set_sample_rate() resampling enabled can't change sample rate from "
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800644 "%u to %u for addr %s",
645 out->dev->routes[out->route_handle].config.output_sample_rate, rate,
646 out->dev->routes[out->route_handle].address);
Stewart Miles02c2f712014-05-01 09:03:27 -0700647 return -ENOSYS;
648 }
649#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -0700650 if (!sample_rate_supported(rate)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700651 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
652 return -ENOSYS;
653 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700654 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800655 out->dev->routes[out->route_handle].config.common.sample_rate = rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700656 return 0;
657}
658
659static size_t out_get_buffer_size(const struct audio_stream *stream)
660{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700661 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
662 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800663 const struct submix_config * const config = &out->dev->routes[out->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700664 const size_t stream_frame_size =
665 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700666 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700667 stream, config, config->buffer_period_size_frames, stream_frame_size);
668 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Miles568e66f2014-05-01 09:03:27 -0700669 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
Stewart Milese54c12c2014-05-01 09:03:27 -0700670 buffer_size_bytes, buffer_size_frames);
671 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700672}
673
674static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
675{
Stewart Milesf645c5e2014-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));
Mikhail Naganove7276172020-10-01 18:07:59 -0700678 audio_channel_mask_t channel_mask =
679 out->dev->routes[out->route_handle].config.output_channel_mask;
Stewart Miles568e66f2014-05-01 09:03:27 -0700680 SUBMIX_ALOGV("out_get_channels() returns %08x", channel_mask);
681 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700682}
683
684static audio_format_t out_get_format(const struct audio_stream *stream)
685{
Stewart Miles568e66f2014-05-01 09:03:27 -0700686 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
687 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800688 const audio_format_t format = out->dev->routes[out->route_handle].config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700689 SUBMIX_ALOGV("out_get_format() returns %x", format);
690 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700691}
692
693static int out_set_format(struct audio_stream *stream, audio_format_t format)
694{
Stewart Miles568e66f2014-05-01 09:03:27 -0700695 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800696 if (format != out->dev->routes[out->route_handle].config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700697 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700698 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700699 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700700 SUBMIX_ALOGV("out_set_format(format=%x)", format);
701 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700702}
703
704static int out_standby(struct audio_stream *stream)
705{
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700706 ALOGI("out_standby()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800707 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
708 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700709
Stewart Milesf645c5e2014-05-01 09:03:27 -0700710 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700711
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800712 out->output_standby = true;
Andy Hung0b93c0a2015-08-10 13:52:34 -0700713 out->frames_written_since_standby = 0;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700714
Stewart Milesf645c5e2014-05-01 09:03:27 -0700715 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700716
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700717 return 0;
718}
719
720static int out_dump(const struct audio_stream *stream, int fd)
721{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700722 (void)stream;
723 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700724 return 0;
725}
726
727static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
728{
Mikhail Naganov1d0e9732018-03-05 12:24:45 -0800729 int exiting = -1;
730 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700731 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Mikhail Naganov1d0e9732018-03-05 12:24:45 -0800732
733 // FIXME this is using hard-coded strings but in the future, this functionality will be
734 // converted to use audio HAL extensions required to support tunneling
735 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
736 struct submix_audio_device * const rsxadev =
737 audio_stream_get_submix_stream_out(stream)->dev;
738 pthread_mutex_lock(&rsxadev->lock);
739 { // using the sink
740 sp<MonoPipe> sink =
741 rsxadev->routes[audio_stream_get_submix_stream_out(stream)->route_handle]
742 .rsxSink;
743 if (sink == NULL) {
744 pthread_mutex_unlock(&rsxadev->lock);
745 return 0;
746 }
747
748 ALOGD("out_set_parameters(): shutting down MonoPipe sink");
749 sink->shutdown(true);
750 } // done using the sink
751 pthread_mutex_unlock(&rsxadev->lock);
752 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700753 return 0;
754}
755
756static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
757{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700758 (void)stream;
759 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700760 return strdup("");
761}
762
763static uint32_t out_get_latency(const struct audio_stream_out *stream)
764{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700765 const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
766 const_cast<struct audio_stream_out *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800767 const struct submix_config * const config = &out->dev->routes[out->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700768 const size_t stream_frame_size =
769 audio_stream_out_frame_size(stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700770 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700771 &stream->common, config, config->buffer_size_frames, stream_frame_size);
Stewart Miles10f1a802014-06-09 20:54:37 -0700772 const uint32_t sample_rate = out_get_sample_rate(&stream->common);
773 const uint32_t latency_ms = (buffer_size_frames * 1000) / sample_rate;
Stewart Milese54c12c2014-05-01 09:03:27 -0700774 SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u",
Stewart Miles10f1a802014-06-09 20:54:37 -0700775 latency_ms, buffer_size_frames, sample_rate);
Stewart Miles568e66f2014-05-01 09:03:27 -0700776 return latency_ms;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700777}
778
779static int out_set_volume(struct audio_stream_out *stream, float left,
780 float right)
781{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700782 (void)stream;
783 (void)left;
784 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700785 return -ENOSYS;
786}
787
788static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
789 size_t bytes)
790{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700791 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700792 ssize_t written_frames = 0;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700793 const size_t frame_size = audio_stream_out_frame_size(stream);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700794 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
795 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700796 const size_t frames = bytes / frame_size;
797
Stewart Milesf645c5e2014-05-01 09:03:27 -0700798 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700799
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800800 out->output_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700801
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800802 sp<MonoPipe> sink = rsxadev->routes[out->route_handle].rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700803 if (sink != NULL) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700804 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800805 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700806 pthread_mutex_unlock(&rsxadev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700807 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700808 // the pipe has already been shutdown, this buffer will be lost but we must
809 // simulate timing so we don't drain the output faster than realtime
810 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
François Gaffie71832e72019-04-12 10:48:55 +0200811
812 pthread_mutex_lock(&rsxadev->lock);
813 out->frames_written += frames;
814 out->frames_written_since_standby += frames;
815 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700816 return bytes;
817 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700818 } else {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700819 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700820 ALOGE("out_write without a pipe!");
821 ALOG_ASSERT("out_write without a pipe!");
822 return 0;
823 }
824
Eric Laurent77887162019-10-14 13:25:01 -0700825 // If the write to the sink would block, flush enough frames
Stewart Miles2d199fe2014-05-01 09:03:27 -0700826 // from the pipe to make space to write the most recent data.
Eric Laurent77887162019-10-14 13:25:01 -0700827 // We DO NOT block if:
828 // - no peer input stream is present
829 // - the peer input is in standby AFTER having been active.
830 // We DO block if:
831 // - the input was never activated to avoid discarding first frames
832 // in the pipe in case capture start was delayed
Stewart Miles2d199fe2014-05-01 09:03:27 -0700833 {
834 const size_t availableToWrite = sink->availableToWrite();
Eric Laurent2cadb582018-11-02 15:06:38 -0700835 // NOTE: rsxSink has been checked above and sink and source life cycles are synchronized
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800836 sp<MonoPipeReader> source = rsxadev->routes[out->route_handle].rsxSource;
Eric Laurent77887162019-10-14 13:25:01 -0700837 const struct submix_stream_in *in = rsxadev->routes[out->route_handle].input;
838 const bool dont_block = (in == NULL)
839 || (in->input_standby && (in->read_counter_frames != 0));
840 if (dont_block && availableToWrite < frames) {
Stewart Miles2d199fe2014-05-01 09:03:27 -0700841 static uint8_t flush_buffer[64];
842 const size_t flushBufferSizeFrames = sizeof(flush_buffer) / frame_size;
843 size_t frames_to_flush_from_source = frames - availableToWrite;
Mikhail Naganov80179932018-02-15 17:07:19 -0800844 SUBMIX_ALOGV("out_write(): flushing %llu frames from the pipe to avoid blocking",
845 (unsigned long long)frames_to_flush_from_source);
Stewart Miles2d199fe2014-05-01 09:03:27 -0700846 while (frames_to_flush_from_source) {
847 const size_t flush_size = min(frames_to_flush_from_source, flushBufferSizeFrames);
848 frames_to_flush_from_source -= flush_size;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800849 // read does not block
Glenn Kasten04c88492016-01-06 14:05:23 -0800850 source->read(flush_buffer, flush_size);
Stewart Miles2d199fe2014-05-01 09:03:27 -0700851 }
852 }
853 }
854
Stewart Milesf645c5e2014-05-01 09:03:27 -0700855 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700856
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700857 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800858
Stewart Miles92854f52014-05-01 09:03:27 -0700859#if LOG_STREAMS_TO_FILES
860 if (out->log_fd >= 0) write(out->log_fd, buffer, written_frames * frame_size);
861#endif // LOG_STREAMS_TO_FILES
862
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700863 if (written_frames < 0) {
864 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700865 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700866
Stewart Milesf645c5e2014-05-01 09:03:27 -0700867 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800868 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700869 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700870
871 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700872 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700873 } else {
874 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700875 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700876 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700877 }
878 }
879
Stewart Milesf645c5e2014-05-01 09:03:27 -0700880 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800881 sink.clear();
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700882 if (written_frames > 0) {
Andy Hung0b93c0a2015-08-10 13:52:34 -0700883 out->frames_written_since_standby += written_frames;
884 out->frames_written += written_frames;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700885 }
Stewart Milesf645c5e2014-05-01 09:03:27 -0700886 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700887
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700888 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700889 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700890 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700891 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700892 const ssize_t written_bytes = written_frames * frame_size;
Stewart Miles02c2f712014-05-01 09:03:27 -0700893 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames", written_bytes, written_frames);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700894 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700895}
896
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700897static int out_get_presentation_position(const struct audio_stream_out *stream,
898 uint64_t *frames, struct timespec *timestamp)
899{
Andy Hung0b93c0a2015-08-10 13:52:34 -0700900 if (stream == NULL || frames == NULL || timestamp == NULL) {
901 return -EINVAL;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700902 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700903
904 const submix_stream_out *out = audio_stream_out_get_submix_stream_out(
905 const_cast<struct audio_stream_out *>(stream));
906 struct submix_audio_device * const rsxadev = out->dev;
907
908 int ret = -EWOULDBLOCK;
909 pthread_mutex_lock(&rsxadev->lock);
Eric Laurent2cadb582018-11-02 15:06:38 -0700910 sp<MonoPipeReader> source = rsxadev->routes[out->route_handle].rsxSource;
911 if (source == NULL) {
912 ALOGW("%s called on released output", __FUNCTION__);
913 pthread_mutex_unlock(&rsxadev->lock);
914 return -ENODEV;
915 }
916
917 const ssize_t frames_in_pipe = source->availableToRead();
Andy Hung0b93c0a2015-08-10 13:52:34 -0700918 if (CC_UNLIKELY(frames_in_pipe < 0)) {
919 *frames = out->frames_written;
920 ret = 0;
921 } else if (out->frames_written >= (uint64_t)frames_in_pipe) {
922 *frames = out->frames_written - frames_in_pipe;
923 ret = 0;
924 }
925 pthread_mutex_unlock(&rsxadev->lock);
926
927 if (ret == 0) {
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700928 clock_gettime(CLOCK_MONOTONIC, timestamp);
929 }
930
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700931 SUBMIX_ALOGV("out_get_presentation_position() got frames=%llu timestamp sec=%llu",
Mikhail Naganov80179932018-02-15 17:07:19 -0800932 frames ? (unsigned long long)*frames : -1ULL,
933 timestamp ? (unsigned long long)timestamp->tv_sec : -1ULL);
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700934
935 return ret;
936}
937
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700938static int out_get_render_position(const struct audio_stream_out *stream,
939 uint32_t *dsp_frames)
940{
Andy Hung0b93c0a2015-08-10 13:52:34 -0700941 if (stream == NULL || dsp_frames == NULL) {
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700942 return -EINVAL;
943 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700944
945 const submix_stream_out *out = audio_stream_out_get_submix_stream_out(
946 const_cast<struct audio_stream_out *>(stream));
947 struct submix_audio_device * const rsxadev = out->dev;
948
949 pthread_mutex_lock(&rsxadev->lock);
Eric Laurent2cadb582018-11-02 15:06:38 -0700950 sp<MonoPipeReader> source = rsxadev->routes[out->route_handle].rsxSource;
951 if (source == NULL) {
952 ALOGW("%s called on released output", __FUNCTION__);
953 pthread_mutex_unlock(&rsxadev->lock);
954 return -ENODEV;
955 }
956
957 const ssize_t frames_in_pipe = source->availableToRead();
Andy Hung0b93c0a2015-08-10 13:52:34 -0700958 if (CC_UNLIKELY(frames_in_pipe < 0)) {
959 *dsp_frames = (uint32_t)out->frames_written_since_standby;
960 } else {
961 *dsp_frames = out->frames_written_since_standby > (uint64_t) frames_in_pipe ?
962 (uint32_t)(out->frames_written_since_standby - frames_in_pipe) : 0;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700963 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700964 pthread_mutex_unlock(&rsxadev->lock);
965
966 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700967}
968
969static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
970{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700971 (void)stream;
972 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700973 return 0;
974}
975
976static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
977{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700978 (void)stream;
979 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700980 return 0;
981}
982
983static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
984 int64_t *timestamp)
985{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700986 (void)stream;
987 (void)timestamp;
Mikhail Naganov739ce6b2019-11-05 12:33:22 -0800988 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700989}
990
991/** audio_stream_in implementation **/
992static uint32_t in_get_sample_rate(const struct audio_stream *stream)
993{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700994 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
995 const_cast<struct audio_stream*>(stream));
Stewart Miles02c2f712014-05-01 09:03:27 -0700996#if ENABLE_RESAMPLING
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800997 const uint32_t rate = in->dev->routes[in->route_handle].config.input_sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700998#else
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800999 const uint32_t rate = in->dev->routes[in->route_handle].config.common.sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -07001000#endif // ENABLE_RESAMPLING
1001 SUBMIX_ALOGV("in_get_sample_rate() returns %u", rate);
1002 return rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001003}
1004
1005static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1006{
Stewart Miles568e66f2014-05-01 09:03:27 -07001007 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Stewart Miles02c2f712014-05-01 09:03:27 -07001008#if ENABLE_RESAMPLING
1009 // The sample rate of the stream can't be changed once it's set since this would change the
1010 // input buffer size and hence break recording from the shared pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001011 if (rate != in->dev->routes[in->route_handle].config.input_sample_rate) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001012 ALOGE("in_set_sample_rate() resampling enabled can't change sample rate from "
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001013 "%u to %u", in->dev->routes[in->route_handle].config.input_sample_rate, rate);
Stewart Miles02c2f712014-05-01 09:03:27 -07001014 return -ENOSYS;
1015 }
1016#endif // ENABLE_RESAMPLING
Stewart Miles70726842014-05-01 09:03:27 -07001017 if (!sample_rate_supported(rate)) {
1018 ALOGE("in_set_sample_rate(rate=%u) rate unsupported", rate);
1019 return -ENOSYS;
1020 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001021 in->dev->routes[in->route_handle].config.common.sample_rate = rate;
Stewart Miles568e66f2014-05-01 09:03:27 -07001022 SUBMIX_ALOGV("in_set_sample_rate() set %u", rate);
1023 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001024}
1025
1026static size_t in_get_buffer_size(const struct audio_stream *stream)
1027{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001028 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
1029 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001030 const struct submix_config * const config = &in->dev->routes[in->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001031 const size_t stream_frame_size =
1032 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Stewart Miles02c2f712014-05-01 09:03:27 -07001033 size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001034 stream, config, config->buffer_period_size_frames, stream_frame_size);
Stewart Miles02c2f712014-05-01 09:03:27 -07001035#if ENABLE_RESAMPLING
1036 // Scale the size of the buffer based upon the maximum number of frames that could be returned
1037 // given the ratio of output to input sample rate.
1038 buffer_size_frames = (size_t)(((float)buffer_size_frames *
1039 (float)config->input_sample_rate) /
1040 (float)config->output_sample_rate);
1041#endif // ENABLE_RESAMPLING
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001042 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Milese54c12c2014-05-01 09:03:27 -07001043 SUBMIX_ALOGV("in_get_buffer_size() returns %zu bytes, %zu frames", buffer_size_bytes,
1044 buffer_size_frames);
1045 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001046}
1047
1048static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
1049{
Stewart Miles70726842014-05-01 09:03:27 -07001050 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
1051 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001052 const audio_channel_mask_t channel_mask =
1053 in->dev->routes[in->route_handle].config.input_channel_mask;
Stewart Miles70726842014-05-01 09:03:27 -07001054 SUBMIX_ALOGV("in_get_channels() returns %x", channel_mask);
1055 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001056}
1057
1058static audio_format_t in_get_format(const struct audio_stream *stream)
1059{
Stewart Miles568e66f2014-05-01 09:03:27 -07001060 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
Stewart Miles70726842014-05-01 09:03:27 -07001061 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001062 const audio_format_t format = in->dev->routes[in->route_handle].config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -07001063 SUBMIX_ALOGV("in_get_format() returns %x", format);
1064 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001065}
1066
1067static int in_set_format(struct audio_stream *stream, audio_format_t format)
1068{
Stewart Miles568e66f2014-05-01 09:03:27 -07001069 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001070 if (format != in->dev->routes[in->route_handle].config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -07001071 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001072 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001073 }
Stewart Milesc049a0a2014-05-01 09:03:27 -07001074 SUBMIX_ALOGV("in_set_format(format=%x)", format);
1075 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001076}
1077
1078static int in_standby(struct audio_stream *stream)
1079{
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001080 ALOGI("in_standby()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001081 struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
1082 struct submix_audio_device * const rsxadev = in->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001083
Stewart Milesf645c5e2014-05-01 09:03:27 -07001084 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001085
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001086 in->input_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001087
Stewart Milesf645c5e2014-05-01 09:03:27 -07001088 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001089
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001090 return 0;
1091}
1092
1093static int in_dump(const struct audio_stream *stream, int fd)
1094{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001095 (void)stream;
1096 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001097 return 0;
1098}
1099
1100static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1101{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001102 (void)stream;
1103 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001104 return 0;
1105}
1106
1107static char * in_get_parameters(const struct audio_stream *stream,
1108 const char *keys)
1109{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001110 (void)stream;
1111 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001112 return strdup("");
1113}
1114
1115static int in_set_gain(struct audio_stream_in *stream, float gain)
1116{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001117 (void)stream;
1118 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001119 return 0;
1120}
1121
1122static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
1123 size_t bytes)
1124{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001125 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
1126 struct submix_audio_device * const rsxadev = in->dev;
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001127 const size_t frame_size = audio_stream_in_frame_size(stream);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001128 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001129
Stewart Milesc049a0a2014-05-01 09:03:27 -07001130 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
Stewart Milesf645c5e2014-05-01 09:03:27 -07001131 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001132
Jean-Michel Trivi257fde62014-12-09 20:20:15 -08001133 const bool output_standby = rsxadev->routes[in->route_handle].output == NULL
1134 ? true : rsxadev->routes[in->route_handle].output->output_standby;
1135 const bool output_standby_transition = (in->output_standby_rec_thr != output_standby);
1136 in->output_standby_rec_thr = output_standby;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001137
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001138 if (in->input_standby || output_standby_transition) {
1139 in->input_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001140 // keep track of when we exit input standby (== first read == start "real recording")
1141 // or when we start recording silence, and reset projected time
1142 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
1143 if (rc == 0) {
1144 in->read_counter_frames = 0;
1145 }
1146 }
1147
1148 in->read_counter_frames += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001149 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001150
1151 {
1152 // about to read from audio source
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001153 sp<MonoPipeReader> source = rsxadev->routes[in->route_handle].rsxSource;
Stewart Milesf645c5e2014-05-01 09:03:27 -07001154 if (source == NULL) {
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001155 in->read_error_count++;// ok if it rolls over
1156 ALOGE_IF(in->read_error_count < MAX_READ_ERROR_LOGS,
1157 "no audio pipe yet we're trying to read! (not all errors will be logged)");
Stewart Milesf645c5e2014-05-01 09:03:27 -07001158 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001159 usleep(frames_to_read * 1000000 / in_get_sample_rate(&stream->common));
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001160 memset(buffer, 0, bytes);
1161 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001162 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001163
Stewart Milesf645c5e2014-05-01 09:03:27 -07001164 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001165
1166 // read the data from the pipe (it's non blocking)
1167 int attempts = 0;
1168 char* buff = (char*)buffer;
Stewart Milese54c12c2014-05-01 09:03:27 -07001169#if ENABLE_CHANNEL_CONVERSION
1170 // Determine whether channel conversion is required.
Eric Laurentdd45fd32014-07-01 20:32:28 -07001171 const uint32_t input_channels = audio_channel_count_from_in_mask(
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001172 rsxadev->routes[in->route_handle].config.input_channel_mask);
Eric Laurentdd45fd32014-07-01 20:32:28 -07001173 const uint32_t output_channels = audio_channel_count_from_out_mask(
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001174 rsxadev->routes[in->route_handle].config.output_channel_mask);
Stewart Milese54c12c2014-05-01 09:03:27 -07001175 if (input_channels != output_channels) {
1176 SUBMIX_ALOGV("in_read(): %d output channels will be converted to %d "
1177 "input channels", output_channels, input_channels);
1178 // Only support 16-bit PCM channel conversion from mono to stereo or stereo to mono.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001179 ALOG_ASSERT(rsxadev->routes[in->route_handle].config.common.format ==
1180 AUDIO_FORMAT_PCM_16_BIT);
Stewart Milese54c12c2014-05-01 09:03:27 -07001181 ALOG_ASSERT((input_channels == 1 && output_channels == 2) ||
1182 (input_channels == 2 && output_channels == 1));
1183 }
1184#endif // ENABLE_CHANNEL_CONVERSION
1185
Stewart Miles02c2f712014-05-01 09:03:27 -07001186#if ENABLE_RESAMPLING
1187 const uint32_t input_sample_rate = in_get_sample_rate(&stream->common);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001188 const uint32_t output_sample_rate =
1189 rsxadev->routes[in->route_handle].config.output_sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -07001190 const size_t resampler_buffer_size_frames =
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001191 sizeof(rsxadev->routes[in->route_handle].resampler_buffer) /
1192 sizeof(rsxadev->routes[in->route_handle].resampler_buffer[0]);
Stewart Miles02c2f712014-05-01 09:03:27 -07001193 float resampler_ratio = 1.0f;
1194 // Determine whether resampling is required.
1195 if (input_sample_rate != output_sample_rate) {
1196 resampler_ratio = (float)output_sample_rate / (float)input_sample_rate;
1197 // Only support 16-bit PCM mono resampling.
1198 // NOTE: Resampling is performed after the channel conversion step.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001199 ALOG_ASSERT(rsxadev->routes[in->route_handle].config.common.format ==
1200 AUDIO_FORMAT_PCM_16_BIT);
1201 ALOG_ASSERT(audio_channel_count_from_in_mask(
1202 rsxadev->routes[in->route_handle].config.input_channel_mask) == 1);
Stewart Miles02c2f712014-05-01 09:03:27 -07001203 }
1204#endif // ENABLE_RESAMPLING
1205
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001206 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
Stewart Miles02c2f712014-05-01 09:03:27 -07001207 ssize_t frames_read = -1977;
Stewart Milese54c12c2014-05-01 09:03:27 -07001208 size_t read_frames = remaining_frames;
Stewart Miles02c2f712014-05-01 09:03:27 -07001209#if ENABLE_RESAMPLING
1210 char* const saved_buff = buff;
1211 if (resampler_ratio != 1.0f) {
1212 // Calculate the number of frames from the pipe that need to be read to generate
1213 // the data for the input stream read.
1214 const size_t frames_required_for_resampler = (size_t)(
1215 (float)read_frames * (float)resampler_ratio);
1216 read_frames = min(frames_required_for_resampler, resampler_buffer_size_frames);
1217 // Read into the resampler buffer.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001218 buff = (char*)rsxadev->routes[in->route_handle].resampler_buffer;
Stewart Miles02c2f712014-05-01 09:03:27 -07001219 }
1220#endif // ENABLE_RESAMPLING
Stewart Milese54c12c2014-05-01 09:03:27 -07001221#if ENABLE_CHANNEL_CONVERSION
1222 if (output_channels == 1 && input_channels == 2) {
1223 // Need to read half the requested frames since the converted output
1224 // data will take twice the space (mono->stereo).
1225 read_frames /= 2;
1226 }
1227#endif // ENABLE_CHANNEL_CONVERSION
1228
1229 SUBMIX_ALOGV("in_read(): frames available to read %zd", source->availableToRead());
1230
Glenn Kasten04c88492016-01-06 14:05:23 -08001231 frames_read = source->read(buff, read_frames);
Stewart Milese54c12c2014-05-01 09:03:27 -07001232
1233 SUBMIX_ALOGV("in_read(): frames read %zd", frames_read);
1234
1235#if ENABLE_CHANNEL_CONVERSION
1236 // Perform in-place channel conversion.
1237 // NOTE: In the following "input stream" refers to the data returned by this function
1238 // and "output stream" refers to the data read from the pipe.
1239 if (input_channels != output_channels && frames_read > 0) {
1240 int16_t *data = (int16_t*)buff;
1241 if (output_channels == 2 && input_channels == 1) {
1242 // Offset into the output stream data in samples.
1243 ssize_t output_stream_offset = 0;
1244 for (ssize_t input_stream_frame = 0; input_stream_frame < frames_read;
1245 input_stream_frame++, output_stream_offset += 2) {
1246 // Average the content from both channels.
1247 data[input_stream_frame] = ((int32_t)data[output_stream_offset] +
1248 (int32_t)data[output_stream_offset + 1]) / 2;
1249 }
1250 } else if (output_channels == 1 && input_channels == 2) {
1251 // Offset into the input stream data in samples.
1252 ssize_t input_stream_offset = (frames_read - 1) * 2;
1253 for (ssize_t output_stream_frame = frames_read - 1; output_stream_frame >= 0;
1254 output_stream_frame--, input_stream_offset -= 2) {
1255 const short sample = data[output_stream_frame];
1256 data[input_stream_offset] = sample;
1257 data[input_stream_offset + 1] = sample;
1258 }
1259 }
1260 }
1261#endif // ENABLE_CHANNEL_CONVERSION
Stewart Miles3dd36f92014-05-01 09:03:27 -07001262
Stewart Miles02c2f712014-05-01 09:03:27 -07001263#if ENABLE_RESAMPLING
1264 if (resampler_ratio != 1.0f) {
1265 SUBMIX_ALOGV("in_read(): resampling %zd frames", frames_read);
1266 const int16_t * const data = (int16_t*)buff;
1267 int16_t * const resampled_buffer = (int16_t*)saved_buff;
1268 // Resample with *no* filtering - if the data from the ouptut stream was really
1269 // sampled at a different rate this will result in very nasty aliasing.
1270 const float output_stream_frames = (float)frames_read;
1271 size_t input_stream_frame = 0;
1272 for (float output_stream_frame = 0.0f;
1273 output_stream_frame < output_stream_frames &&
1274 input_stream_frame < remaining_frames;
1275 output_stream_frame += resampler_ratio, input_stream_frame++) {
1276 resampled_buffer[input_stream_frame] = data[(size_t)output_stream_frame];
1277 }
1278 ALOG_ASSERT(input_stream_frame <= (ssize_t)resampler_buffer_size_frames);
1279 SUBMIX_ALOGV("in_read(): resampler produced %zd frames", input_stream_frame);
1280 frames_read = input_stream_frame;
1281 buff = saved_buff;
1282 }
1283#endif // ENABLE_RESAMPLING
1284
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001285 if (frames_read > 0) {
Stewart Miles92854f52014-05-01 09:03:27 -07001286#if LOG_STREAMS_TO_FILES
1287 if (in->log_fd >= 0) write(in->log_fd, buff, frames_read * frame_size);
1288#endif // LOG_STREAMS_TO_FILES
1289
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001290 remaining_frames -= frames_read;
1291 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001292 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
1293 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001294 } else {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001295 attempts++;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001296 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001297 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
1298 }
1299 }
1300 // done using the source
Stewart Milesf645c5e2014-05-01 09:03:27 -07001301 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001302 source.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -07001303 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001304 }
1305
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001306 if (remaining_frames > 0) {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001307 const size_t remaining_bytes = remaining_frames * frame_size;
Stewart Miles10f1a802014-06-09 20:54:37 -07001308 SUBMIX_ALOGV(" clearing remaining_frames = %zu", remaining_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001309 memset(((char*)buffer)+ bytes - remaining_bytes, 0, remaining_bytes);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001310 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001311
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001312 // compute how much we need to sleep after reading the data by comparing the wall clock with
1313 // the projected time at which we should return.
1314 struct timespec time_after_read;// wall clock after reading from the pipe
1315 struct timespec record_duration;// observed record duration
1316 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
1317 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
1318 if (rc == 0) {
1319 // for how long have we been recording?
1320 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
1321 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
1322 if (record_duration.tv_nsec < 0) {
1323 record_duration.tv_sec--;
1324 record_duration.tv_nsec += 1000000000;
1325 }
1326
Stewart Milesf645c5e2014-05-01 09:03:27 -07001327 // read_counter_frames contains the number of frames that have been read since the
1328 // beginning of recording (including this call): it's converted to usec and compared to
1329 // how long we've been recording for, which gives us how long we must wait to sync the
1330 // projected recording time, and the observed recording time.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001331 long projected_vs_observed_offset_us =
1332 ((int64_t)(in->read_counter_frames
1333 - (record_duration.tv_sec*sample_rate)))
1334 * 1000000 / sample_rate
1335 - (record_duration.tv_nsec / 1000);
1336
Stewart Milesc049a0a2014-05-01 09:03:27 -07001337 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001338 record_duration.tv_sec, record_duration.tv_nsec/1000000,
1339 projected_vs_observed_offset_us);
1340 if (projected_vs_observed_offset_us > 0) {
1341 usleep(projected_vs_observed_offset_us);
1342 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001343 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001344
Stewart Milesc049a0a2014-05-01 09:03:27 -07001345 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001346 return bytes;
1347
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001348}
1349
1350static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1351{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001352 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001353 return 0;
1354}
1355
1356static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1357{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001358 (void)stream;
1359 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001360 return 0;
1361}
1362
1363static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1364{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001365 (void)stream;
1366 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001367 return 0;
1368}
1369
1370static int adev_open_output_stream(struct audio_hw_device *dev,
1371 audio_io_handle_t handle,
1372 audio_devices_t devices,
1373 audio_output_flags_t flags,
1374 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -07001375 struct audio_stream_out **stream_out,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001376 const char *address)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001377{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001378 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001379 ALOGD("adev_open_output_stream(address=%s)", address);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001380 struct submix_stream_out *out;
Stewart Miles10f1a802014-06-09 20:54:37 -07001381 bool force_pipe_creation = false;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001382 (void)handle;
1383 (void)devices;
1384 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001385
Stewart Miles3dd36f92014-05-01 09:03:27 -07001386 *stream_out = NULL;
1387
Stewart Miles70726842014-05-01 09:03:27 -07001388 // Make sure it's possible to open the device given the current audio config.
1389 submix_sanitize_config(config, false);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001390
1391 int route_idx = -1;
1392
1393 pthread_mutex_lock(&rsxadev->lock);
1394
1395 status_t res = submix_get_route_idx_for_address_l(rsxadev, address, &route_idx);
1396 if (res != OK) {
1397 ALOGE("Error %d looking for address=%s in adev_open_output_stream", res, address);
1398 pthread_mutex_unlock(&rsxadev->lock);
1399 return res;
1400 }
1401
1402 if (!submix_open_validate_l(rsxadev, route_idx, config, false)) {
1403 ALOGE("adev_open_output_stream(): Unable to open output stream for address %s", address);
1404 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles70726842014-05-01 09:03:27 -07001405 return -EINVAL;
1406 }
1407
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001408 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001409 if (!out) {
1410 pthread_mutex_unlock(&rsxadev->lock);
1411 return -ENOMEM;
1412 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001413
Stewart Miles568e66f2014-05-01 09:03:27 -07001414 // Initialize the function pointer tables (v-tables).
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001415 out->stream.common.get_sample_rate = out_get_sample_rate;
1416 out->stream.common.set_sample_rate = out_set_sample_rate;
1417 out->stream.common.get_buffer_size = out_get_buffer_size;
1418 out->stream.common.get_channels = out_get_channels;
1419 out->stream.common.get_format = out_get_format;
1420 out->stream.common.set_format = out_set_format;
1421 out->stream.common.standby = out_standby;
1422 out->stream.common.dump = out_dump;
1423 out->stream.common.set_parameters = out_set_parameters;
1424 out->stream.common.get_parameters = out_get_parameters;
1425 out->stream.common.add_audio_effect = out_add_audio_effect;
1426 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1427 out->stream.get_latency = out_get_latency;
1428 out->stream.set_volume = out_set_volume;
1429 out->stream.write = out_write;
1430 out->stream.get_render_position = out_get_render_position;
1431 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -07001432 out->stream.get_presentation_position = out_get_presentation_position;
1433
Stewart Miles10f1a802014-06-09 20:54:37 -07001434#if ENABLE_RESAMPLING
1435 // Recreate the pipe with the correct sample rate so that MonoPipe.write() rate limits
1436 // writes correctly.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001437 force_pipe_creation = rsxadev->routes[route_idx].config.common.sample_rate
1438 != config->sample_rate;
Stewart Miles10f1a802014-06-09 20:54:37 -07001439#endif // ENABLE_RESAMPLING
1440
1441 // If the sink has been shutdown or pipe recreation is forced (see above), delete the pipe so
1442 // that it's recreated.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001443 if ((rsxadev->routes[route_idx].rsxSink != NULL
1444 && rsxadev->routes[route_idx].rsxSink->isShutdown()) || force_pipe_creation) {
1445 submix_audio_device_release_pipe_l(rsxadev, route_idx);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001446 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001447
Stewart Miles568e66f2014-05-01 09:03:27 -07001448 // Store a pointer to the device from the output stream.
1449 out->dev = rsxadev;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001450 // Initialize the pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001451 ALOGV("adev_open_output_stream(): about to create pipe at index %d", route_idx);
1452 submix_audio_device_create_pipe_l(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1453 DEFAULT_PIPE_PERIOD_COUNT, NULL, out, address, route_idx);
Stewart Miles92854f52014-05-01 09:03:27 -07001454#if LOG_STREAMS_TO_FILES
1455 out->log_fd = open(LOG_STREAM_OUT_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1456 LOG_STREAM_FILE_PERMISSIONS);
1457 ALOGE_IF(out->log_fd < 0, "adev_open_output_stream(): log file open failed %s",
1458 strerror(errno));
1459 ALOGV("adev_open_output_stream(): log_fd = %d", out->log_fd);
1460#endif // LOG_STREAMS_TO_FILES
Stewart Miles568e66f2014-05-01 09:03:27 -07001461 // Return the output stream.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001462 *stream_out = &out->stream;
1463
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001464 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001465 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001466}
1467
1468static void adev_close_output_stream(struct audio_hw_device *dev,
1469 struct audio_stream_out *stream)
1470{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001471 struct submix_audio_device * rsxadev = audio_hw_device_get_submix_audio_device(
1472 const_cast<struct audio_hw_device*>(dev));
Stewart Miles3dd36f92014-05-01 09:03:27 -07001473 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001474
1475 pthread_mutex_lock(&rsxadev->lock);
1476 ALOGD("adev_close_output_stream() addr = %s", rsxadev->routes[out->route_handle].address);
1477 submix_audio_device_destroy_pipe_l(audio_hw_device_get_submix_audio_device(dev), NULL, out);
Stewart Miles92854f52014-05-01 09:03:27 -07001478#if LOG_STREAMS_TO_FILES
1479 if (out->log_fd >= 0) close(out->log_fd);
1480#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001481
1482 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001483 free(out);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001484}
1485
1486static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1487{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001488 (void)dev;
1489 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001490 return -ENOSYS;
1491}
1492
1493static char * adev_get_parameters(const struct audio_hw_device *dev,
1494 const char *keys)
1495{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001496 (void)dev;
1497 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001498 return strdup("");;
1499}
1500
1501static int adev_init_check(const struct audio_hw_device *dev)
1502{
1503 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -07001504 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001505 return 0;
1506}
1507
1508static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1509{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001510 (void)dev;
1511 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001512 return -ENOSYS;
1513}
1514
1515static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1516{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001517 (void)dev;
1518 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001519 return -ENOSYS;
1520}
1521
1522static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
1523{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001524 (void)dev;
1525 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001526 return -ENOSYS;
1527}
1528
1529static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1530{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001531 (void)dev;
1532 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001533 return -ENOSYS;
1534}
1535
1536static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1537{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001538 (void)dev;
1539 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001540 return -ENOSYS;
1541}
1542
1543static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1544{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001545 (void)dev;
1546 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001547 return 0;
1548}
1549
1550static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1551{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001552 (void)dev;
1553 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001554 return -ENOSYS;
1555}
1556
1557static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1558{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001559 (void)dev;
1560 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001561 return -ENOSYS;
1562}
1563
1564static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1565 const struct audio_config *config)
1566{
Stewart Miles568e66f2014-05-01 09:03:27 -07001567 if (audio_is_linear_pcm(config->format)) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001568 size_t max_buffer_period_size_frames = 0;
1569 struct submix_audio_device * rsxadev = audio_hw_device_get_submix_audio_device(
1570 const_cast<struct audio_hw_device*>(dev));
1571 // look for the largest buffer period size
1572 for (int i = 0 ; i < MAX_ROUTES ; i++) {
1573 if (rsxadev->routes[i].config.buffer_period_size_frames > max_buffer_period_size_frames)
1574 {
1575 max_buffer_period_size_frames = rsxadev->routes[i].config.buffer_period_size_frames;
1576 }
1577 }
Eric Laurentdd45fd32014-07-01 20:32:28 -07001578 const size_t frame_size_in_bytes = audio_channel_count_from_in_mask(config->channel_mask) *
Stewart Miles568e66f2014-05-01 09:03:27 -07001579 audio_bytes_per_sample(config->format);
Mikhail Naganov739ce6b2019-11-05 12:33:22 -08001580 if (max_buffer_period_size_frames == 0) {
1581 max_buffer_period_size_frames = DEFAULT_PIPE_SIZE_IN_FRAMES;
1582 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001583 const size_t buffer_size = max_buffer_period_size_frames * frame_size_in_bytes;
Stewart Miles10f1a802014-06-09 20:54:37 -07001584 SUBMIX_ALOGV("adev_get_input_buffer_size() returns %zu bytes, %zu frames",
Mikhail Naganov80179932018-02-15 17:07:19 -08001585 buffer_size, max_buffer_period_size_frames);
Stewart Miles568e66f2014-05-01 09:03:27 -07001586 return buffer_size;
1587 }
1588 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001589}
1590
1591static int adev_open_input_stream(struct audio_hw_device *dev,
1592 audio_io_handle_t handle,
1593 audio_devices_t devices,
1594 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -07001595 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001596 audio_input_flags_t flags __unused,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001597 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001598 audio_source_t source __unused)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001599{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001600 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001601 struct submix_stream_in *in;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001602 ALOGD("adev_open_input_stream(addr=%s)", address);
Stewart Milesc049a0a2014-05-01 09:03:27 -07001603 (void)handle;
1604 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001605
Stewart Miles3dd36f92014-05-01 09:03:27 -07001606 *stream_in = NULL;
1607
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001608 // Do we already have a route for this address
1609 int route_idx = -1;
1610
1611 pthread_mutex_lock(&rsxadev->lock);
1612
1613 status_t res = submix_get_route_idx_for_address_l(rsxadev, address, &route_idx);
1614 if (res != OK) {
Jean-Michel Trivi79fbccf2016-04-05 17:20:29 -07001615 ALOGE("Error %d looking for address=%s in adev_open_input_stream", res, address);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001616 pthread_mutex_unlock(&rsxadev->lock);
1617 return res;
1618 }
1619
Stewart Miles70726842014-05-01 09:03:27 -07001620 // Make sure it's possible to open the device given the current audio config.
1621 submix_sanitize_config(config, true);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001622 if (!submix_open_validate_l(rsxadev, route_idx, config, true)) {
Stewart Miles70726842014-05-01 09:03:27 -07001623 ALOGE("adev_open_input_stream(): Unable to open input stream.");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001624 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles70726842014-05-01 09:03:27 -07001625 return -EINVAL;
1626 }
1627
Stewart Miles3dd36f92014-05-01 09:03:27 -07001628#if ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001629 in = rsxadev->routes[route_idx].input;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001630 if (in) {
1631 in->ref_count++;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001632 sp<MonoPipe> sink = rsxadev->routes[route_idx].rsxSink;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001633 ALOG_ASSERT(sink != NULL);
1634 // If the sink has been shutdown, delete the pipe.
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001635 if (sink != NULL) {
1636 if (sink->isShutdown()) {
1637 ALOGD(" Non-NULL shut down sink when opening input stream, releasing, refcount=%d",
1638 in->ref_count);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001639 submix_audio_device_release_pipe_l(rsxadev, in->route_handle);
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001640 } else {
1641 ALOGD(" Non-NULL sink when opening input stream, refcount=%d", in->ref_count);
1642 }
1643 } else {
1644 ALOGE("NULL sink when opening input stream, refcount=%d", in->ref_count);
1645 }
Stewart Miles3dd36f92014-05-01 09:03:27 -07001646 }
Stewart Miles3dd36f92014-05-01 09:03:27 -07001647#else
1648 in = NULL;
1649#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001650
Stewart Miles3dd36f92014-05-01 09:03:27 -07001651 if (!in) {
1652 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
1653 if (!in) return -ENOMEM;
Mikhail Naganov1462c762019-07-26 09:22:34 -07001654#if ENABLE_LEGACY_INPUT_OPEN
Stewart Miles3dd36f92014-05-01 09:03:27 -07001655 in->ref_count = 1;
Mikhail Naganov1462c762019-07-26 09:22:34 -07001656#endif
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001657
Stewart Miles3dd36f92014-05-01 09:03:27 -07001658 // Initialize the function pointer tables (v-tables).
1659 in->stream.common.get_sample_rate = in_get_sample_rate;
1660 in->stream.common.set_sample_rate = in_set_sample_rate;
1661 in->stream.common.get_buffer_size = in_get_buffer_size;
1662 in->stream.common.get_channels = in_get_channels;
1663 in->stream.common.get_format = in_get_format;
1664 in->stream.common.set_format = in_set_format;
1665 in->stream.common.standby = in_standby;
1666 in->stream.common.dump = in_dump;
1667 in->stream.common.set_parameters = in_set_parameters;
1668 in->stream.common.get_parameters = in_get_parameters;
1669 in->stream.common.add_audio_effect = in_add_audio_effect;
1670 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1671 in->stream.set_gain = in_set_gain;
1672 in->stream.read = in_read;
1673 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001674
1675 in->dev = rsxadev;
1676#if LOG_STREAMS_TO_FILES
1677 in->log_fd = -1;
1678#endif
Stewart Miles3dd36f92014-05-01 09:03:27 -07001679 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001680
Stewart Miles568e66f2014-05-01 09:03:27 -07001681 // Initialize the input stream.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001682 in->read_counter_frames = 0;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001683 in->input_standby = true;
1684 if (rsxadev->routes[route_idx].output != NULL) {
1685 in->output_standby_rec_thr = rsxadev->routes[route_idx].output->output_standby;
1686 } else {
1687 in->output_standby_rec_thr = true;
1688 }
1689
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001690 in->read_error_count = 0;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001691 // Initialize the pipe.
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001692 ALOGV("adev_open_input_stream(): about to create pipe");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001693 submix_audio_device_create_pipe_l(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1694 DEFAULT_PIPE_PERIOD_COUNT, in, NULL, address, route_idx);
Eric Laurent5b78d412019-03-01 18:39:26 -08001695
1696 sp <MonoPipe> sink = rsxadev->routes[route_idx].rsxSink;
1697 if (sink != NULL) {
1698 sink->shutdown(false);
1699 }
1700
Stewart Miles92854f52014-05-01 09:03:27 -07001701#if LOG_STREAMS_TO_FILES
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001702 if (in->log_fd >= 0) close(in->log_fd);
Stewart Miles92854f52014-05-01 09:03:27 -07001703 in->log_fd = open(LOG_STREAM_IN_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1704 LOG_STREAM_FILE_PERMISSIONS);
1705 ALOGE_IF(in->log_fd < 0, "adev_open_input_stream(): log file open failed %s",
1706 strerror(errno));
1707 ALOGV("adev_open_input_stream(): log_fd = %d", in->log_fd);
1708#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001709 // Return the input stream.
1710 *stream_in = &in->stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001711
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001712 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001713 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001714}
1715
1716static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -07001717 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001718{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001719 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
1720
Stewart Miles3dd36f92014-05-01 09:03:27 -07001721 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001722 ALOGD("adev_close_input_stream()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001723 pthread_mutex_lock(&rsxadev->lock);
1724 submix_audio_device_destroy_pipe_l(rsxadev, in, NULL);
Stewart Miles92854f52014-05-01 09:03:27 -07001725#if LOG_STREAMS_TO_FILES
1726 if (in->log_fd >= 0) close(in->log_fd);
1727#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001728#if ENABLE_LEGACY_INPUT_OPEN
1729 if (in->ref_count == 0) free(in);
1730#else
1731 free(in);
1732#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001733
1734 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001735}
1736
1737static int adev_dump(const audio_hw_device_t *device, int fd)
1738{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001739 const struct submix_audio_device * rsxadev = //audio_hw_device_get_submix_audio_device(device);
1740 reinterpret_cast<const struct submix_audio_device *>(
1741 reinterpret_cast<const uint8_t *>(device) -
1742 offsetof(struct submix_audio_device, device));
1743 char msg[100];
Mikhail Naganov80179932018-02-15 17:07:19 -08001744 int n = snprintf(msg, sizeof(msg), "\nReroute submix audio module:\n");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001745 write(fd, &msg, n);
1746 for (int i=0 ; i < MAX_ROUTES ; i++) {
Mikhail Naganov1462c762019-07-26 09:22:34 -07001747#if ENABLE_RESAMPLING
Mikhail Naganov80179932018-02-15 17:07:19 -08001748 n = snprintf(msg, sizeof(msg), " route[%d] rate in=%d out=%d, addr=[%s]\n", i,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001749 rsxadev->routes[i].config.input_sample_rate,
1750 rsxadev->routes[i].config.output_sample_rate,
1751 rsxadev->routes[i].address);
Mikhail Naganov1462c762019-07-26 09:22:34 -07001752#else
1753 n = snprintf(msg, sizeof(msg), " route[%d], rate=%d addr=[%s]\n", i,
1754 rsxadev->routes[i].config.common.sample_rate,
1755 rsxadev->routes[i].address);
1756#endif
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001757 write(fd, &msg, n);
1758 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001759 return 0;
1760}
1761
1762static int adev_close(hw_device_t *device)
1763{
1764 ALOGI("adev_close()");
1765 free(device);
1766 return 0;
1767}
1768
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001769static int adev_open(const hw_module_t* module, const char* name,
1770 hw_device_t** device)
1771{
1772 ALOGI("adev_open(name=%s)", name);
1773 struct submix_audio_device *rsxadev;
1774
1775 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1776 return -EINVAL;
1777
1778 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
1779 if (!rsxadev)
1780 return -ENOMEM;
1781
1782 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -07001783 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001784 rsxadev->device.common.module = (struct hw_module_t *) module;
1785 rsxadev->device.common.close = adev_close;
1786
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001787 rsxadev->device.init_check = adev_init_check;
1788 rsxadev->device.set_voice_volume = adev_set_voice_volume;
1789 rsxadev->device.set_master_volume = adev_set_master_volume;
1790 rsxadev->device.get_master_volume = adev_get_master_volume;
1791 rsxadev->device.set_master_mute = adev_set_master_mute;
1792 rsxadev->device.get_master_mute = adev_get_master_mute;
1793 rsxadev->device.set_mode = adev_set_mode;
1794 rsxadev->device.set_mic_mute = adev_set_mic_mute;
1795 rsxadev->device.get_mic_mute = adev_get_mic_mute;
1796 rsxadev->device.set_parameters = adev_set_parameters;
1797 rsxadev->device.get_parameters = adev_get_parameters;
1798 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
1799 rsxadev->device.open_output_stream = adev_open_output_stream;
1800 rsxadev->device.close_output_stream = adev_close_output_stream;
1801 rsxadev->device.open_input_stream = adev_open_input_stream;
1802 rsxadev->device.close_input_stream = adev_close_input_stream;
1803 rsxadev->device.dump = adev_dump;
1804
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001805 for (int i=0 ; i < MAX_ROUTES ; i++) {
1806 memset(&rsxadev->routes[i], 0, sizeof(route_config));
1807 strcpy(rsxadev->routes[i].address, "");
1808 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001809
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001810 *device = &rsxadev->device.common;
1811
1812 return 0;
1813}
1814
1815static struct hw_module_methods_t hal_module_methods = {
1816 /* open */ adev_open,
1817};
1818
1819struct audio_module HAL_MODULE_INFO_SYM = {
1820 /* common */ {
1821 /* tag */ HARDWARE_MODULE_TAG,
1822 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
1823 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
1824 /* id */ AUDIO_HARDWARE_MODULE_ID,
1825 /* name */ "Wifi Display audio HAL",
1826 /* author */ "The Android Open Source Project",
1827 /* methods */ &hal_module_methods,
1828 /* dso */ NULL,
1829 /* reserved */ { 0 },
1830 },
1831};
1832
1833} //namespace android
1834
1835} //extern "C"