blob: 42d3b98d2fe5af134487bab25f04e15ebb8571d4 [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
Jean-Michel Trivia33c1652021-07-08 11:21:33 -070086
Stewart Miles92854f52014-05-01 09:03:27 -070087#if LOG_STREAMS_TO_FILES
88// Folder to save stream log files to.
Eric Laurent854a10a2016-02-19 14:41:51 -080089#define LOG_STREAM_FOLDER "/data/misc/audioserver"
Stewart Miles92854f52014-05-01 09:03:27 -070090// Log filenames for input and output streams.
91#define LOG_STREAM_OUT_FILENAME LOG_STREAM_FOLDER "/r_submix_out.raw"
92#define LOG_STREAM_IN_FILENAME LOG_STREAM_FOLDER "/r_submix_in.raw"
93// File permissions for stream log files.
94#define LOG_STREAM_FILE_PERMISSIONS (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
95#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi793a8542014-10-14 15:31:51 -070096// limit for number of read error log entries to avoid spamming the logs
97#define MAX_READ_ERROR_LOGS 5
Stewart Miles3dd36f92014-05-01 09:03:27 -070098
99// Common limits macros.
100#ifndef min
101#define min(a, b) ((a) < (b) ? (a) : (b))
102#endif // min
Stewart Milese54c12c2014-05-01 09:03:27 -0700103#ifndef max
104#define max(a, b) ((a) > (b) ? (a) : (b))
105#endif // max
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700106
Stewart Miles70726842014-05-01 09:03:27 -0700107// Set *result_variable_ptr to true if value_to_find is present in the array array_to_search,
108// otherwise set *result_variable_ptr to false.
109#define SUBMIX_VALUE_IN_SET(value_to_find, array_to_search, result_variable_ptr) \
110 { \
111 size_t i; \
112 *(result_variable_ptr) = false; \
113 for (i = 0; i < sizeof(array_to_search) / sizeof((array_to_search)[0]); i++) { \
114 if ((value_to_find) == (array_to_search)[i]) { \
115 *(result_variable_ptr) = true; \
116 break; \
117 } \
118 } \
119 }
120
Stewart Miles568e66f2014-05-01 09:03:27 -0700121// Configuration of the submix pipe.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700122struct submix_config {
Stewart Miles70726842014-05-01 09:03:27 -0700123 // Channel mask field in this data structure is set to either input_channel_mask or
124 // output_channel_mask depending upon the last stream to be opened on this device.
125 struct audio_config common;
126 // Input stream and output stream channel masks. This is required since input and output
127 // channel bitfields are not equivalent.
128 audio_channel_mask_t input_channel_mask;
129 audio_channel_mask_t output_channel_mask;
Stewart Milese54c12c2014-05-01 09:03:27 -0700130 size_t pipe_frame_size; // Number of bytes in each audio frame in the pipe.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700131 size_t buffer_size_frames; // Size of the audio pipe in frames.
132 // Maximum number of frames buffered by the input and output streams.
133 size_t buffer_period_size_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700134};
135
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800136#define MAX_ROUTES 10
137typedef struct route_config {
138 struct submix_config config;
139 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700140 // Pipe variables: they handle the ring buffer that "pipes" audio:
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700141 // - from the submix virtual audio output == what needs to be played
142 // remotely, seen as an output for AudioFlinger
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700143 // - to the virtual audio source == what is captured by the component
144 // which "records" the submix / virtual audio source, and handles it as needed.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700145 // A usecase example is one where the component capturing the audio is then sending it over
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700146 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
147 // TV with Wifi Display capabilities), or to a wireless audio player.
Stewart Miles568e66f2014-05-01 09:03:27 -0700148 sp<MonoPipe> rsxSink;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700149 sp<MonoPipeReader> rsxSource;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800150 // Pointers to the current input and output stream instances. rsxSink and rsxSource are
151 // destroyed if both and input and output streams are destroyed.
152 struct submix_stream_out *output;
153 struct submix_stream_in *input;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800154} route_config_t;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700155
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800156struct submix_audio_device {
157 struct audio_hw_device device;
158 route_config_t routes[MAX_ROUTES];
Stewart Miles568e66f2014-05-01 09:03:27 -0700159 // Device lock, also used to protect access to submix_audio_device from the input and output
160 // streams.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700161 pthread_mutex_t lock;
162};
163
164struct submix_stream_out {
165 struct audio_stream_out stream;
166 struct submix_audio_device *dev;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800167 int route_handle;
168 bool output_standby;
Andy Hung0b93c0a2015-08-10 13:52:34 -0700169 uint64_t frames_written;
170 uint64_t frames_written_since_standby;
Stewart Miles92854f52014-05-01 09:03:27 -0700171#if LOG_STREAMS_TO_FILES
172 int log_fd;
173#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700174};
175
176struct submix_stream_in {
177 struct audio_stream_in stream;
178 struct submix_audio_device *dev;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800179 int route_handle;
180 bool input_standby;
181 bool output_standby_rec_thr; // output standby state as seen from record thread
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700182 // wall clock when recording starts
183 struct timespec record_start_time;
184 // how many frames have been requested to be read
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700185 uint64_t read_counter_frames;
Mikhail Naganov8c97d242021-03-11 13:24:35 -0800186 uint64_t read_counter_frames_since_standby;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700187
188#if ENABLE_LEGACY_INPUT_OPEN
189 // Number of references to this input stream.
190 volatile int32_t ref_count;
191#endif // ENABLE_LEGACY_INPUT_OPEN
Stewart Miles92854f52014-05-01 09:03:27 -0700192#if LOG_STREAMS_TO_FILES
193 int log_fd;
194#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi793a8542014-10-14 15:31:51 -0700195
Mikhail Naganov80179932018-02-15 17:07:19 -0800196 volatile uint16_t read_error_count;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700197};
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700198
Stewart Miles70726842014-05-01 09:03:27 -0700199// Determine whether the specified sample rate is supported by the submix module.
200static bool sample_rate_supported(const uint32_t sample_rate)
201{
202 // Set of sample rates supported by Format_from_SR_C() frameworks/av/media/libnbaio/NAIO.cpp.
203 static const unsigned int supported_sample_rates[] = {
204 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
205 };
206 bool return_value;
207 SUBMIX_VALUE_IN_SET(sample_rate, supported_sample_rates, &return_value);
208 return return_value;
209}
210
211// Determine whether the specified sample rate is supported, if it is return the specified sample
212// rate, otherwise return the default sample rate for the submix module.
213static uint32_t get_supported_sample_rate(uint32_t sample_rate)
214{
215 return sample_rate_supported(sample_rate) ? sample_rate : DEFAULT_SAMPLE_RATE_HZ;
216}
217
218// Determine whether the specified channel in mask is supported by the submix module.
219static bool channel_in_mask_supported(const audio_channel_mask_t channel_in_mask)
220{
221 // Set of channel in masks supported by Format_from_SR_C()
222 // frameworks/av/media/libnbaio/NAIO.cpp.
223 static const audio_channel_mask_t supported_channel_in_masks[] = {
224 AUDIO_CHANNEL_IN_MONO, AUDIO_CHANNEL_IN_STEREO,
225 };
226 bool return_value;
227 SUBMIX_VALUE_IN_SET(channel_in_mask, supported_channel_in_masks, &return_value);
228 return return_value;
229}
230
231// Determine whether the specified channel in mask is supported, if it is return the specified
232// channel in mask, otherwise return the default channel in mask for the submix module.
233static audio_channel_mask_t get_supported_channel_in_mask(
234 const audio_channel_mask_t channel_in_mask)
235{
236 return channel_in_mask_supported(channel_in_mask) ? channel_in_mask :
237 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_IN_STEREO);
238}
239
240// Determine whether the specified channel out mask is supported by the submix module.
241static bool channel_out_mask_supported(const audio_channel_mask_t channel_out_mask)
242{
243 // Set of channel out masks supported by Format_from_SR_C()
244 // frameworks/av/media/libnbaio/NAIO.cpp.
245 static const audio_channel_mask_t supported_channel_out_masks[] = {
246 AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO,
247 };
248 bool return_value;
249 SUBMIX_VALUE_IN_SET(channel_out_mask, supported_channel_out_masks, &return_value);
250 return return_value;
251}
252
253// Determine whether the specified channel out mask is supported, if it is return the specified
254// channel out mask, otherwise return the default channel out mask for the submix module.
255static audio_channel_mask_t get_supported_channel_out_mask(
256 const audio_channel_mask_t channel_out_mask)
257{
258 return channel_out_mask_supported(channel_out_mask) ? channel_out_mask :
259 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_OUT_STEREO);
260}
261
Stewart Milesf645c5e2014-05-01 09:03:27 -0700262// Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the
263// structure.
264static struct submix_stream_out * audio_stream_out_get_submix_stream_out(
265 struct audio_stream_out * const stream)
266{
267 ALOG_ASSERT(stream);
268 return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) -
269 offsetof(struct submix_stream_out, stream));
270}
271
272// Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure.
273static struct submix_stream_out * audio_stream_get_submix_stream_out(
274 struct audio_stream * const stream)
275{
276 ALOG_ASSERT(stream);
277 return audio_stream_out_get_submix_stream_out(
278 reinterpret_cast<struct audio_stream_out *>(stream));
279}
280
281// Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the
282// structure.
283static struct submix_stream_in * audio_stream_in_get_submix_stream_in(
284 struct audio_stream_in * const stream)
285{
286 ALOG_ASSERT(stream);
287 return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) -
288 offsetof(struct submix_stream_in, stream));
289}
290
291// Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure.
292static struct submix_stream_in * audio_stream_get_submix_stream_in(
293 struct audio_stream * const stream)
294{
295 ALOG_ASSERT(stream);
296 return audio_stream_in_get_submix_stream_in(
297 reinterpret_cast<struct audio_stream_in *>(stream));
298}
299
300// Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within
301// the structure.
302static struct submix_audio_device * audio_hw_device_get_submix_audio_device(
303 struct audio_hw_device *device)
304{
305 ALOG_ASSERT(device);
306 return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) -
307 offsetof(struct submix_audio_device, device));
308}
309
Stewart Miles70726842014-05-01 09:03:27 -0700310// Compare an audio_config with input channel mask and an audio_config with output channel mask
311// returning false if they do *not* match, true otherwise.
312static bool audio_config_compare(const audio_config * const input_config,
313 const audio_config * const output_config)
314{
Eric Laurentdd45fd32014-07-01 20:32:28 -0700315 const uint32_t input_channels = audio_channel_count_from_in_mask(input_config->channel_mask);
316 const uint32_t output_channels = audio_channel_count_from_out_mask(output_config->channel_mask);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700317 if (input_channels != output_channels) {
318 ALOGE("audio_config_compare() channel count mismatch input=%d vs. output=%d",
319 input_channels, output_channels);
Stewart Miles70726842014-05-01 09:03:27 -0700320 return false;
321 }
Jean-Michel Trivia33c1652021-07-08 11:21:33 -0700322
Stewart Miles70726842014-05-01 09:03:27 -0700323 if (input_config->sample_rate != output_config->sample_rate) {
324 ALOGE("audio_config_compare() sample rate mismatch %ul vs. %ul",
325 input_config->sample_rate, output_config->sample_rate);
326 return false;
327 }
328 if (input_config->format != output_config->format) {
329 ALOGE("audio_config_compare() format mismatch %x vs. %x",
330 input_config->format, output_config->format);
331 return false;
332 }
333 // This purposely ignores offload_info as it's not required for the submix device.
334 return true;
335}
336
Stewart Miles3dd36f92014-05-01 09:03:27 -0700337// If one doesn't exist, create a pipe for the submix audio device rsxadev of size
338// buffer_size_frames and optionally associate "in" or "out" with the submix audio device.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800339// Must be called with lock held on the submix_audio_device
340static void submix_audio_device_create_pipe_l(struct submix_audio_device * const rsxadev,
Stewart Miles3dd36f92014-05-01 09:03:27 -0700341 const struct audio_config * const config,
342 const size_t buffer_size_frames,
343 const uint32_t buffer_period_count,
344 struct submix_stream_in * const in,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800345 struct submix_stream_out * const out,
346 const char *address,
347 int route_idx)
Stewart Miles3dd36f92014-05-01 09:03:27 -0700348{
349 ALOG_ASSERT(in || out);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800350 ALOG_ASSERT(route_idx > -1);
351 ALOG_ASSERT(route_idx < MAX_ROUTES);
352 ALOGD("submix_audio_device_create_pipe_l(addr=%s, idx=%d)", address, route_idx);
353
Stewart Miles3dd36f92014-05-01 09:03:27 -0700354 // Save a reference to the specified input or output stream and the associated channel
355 // mask.
356 if (in) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800357 in->route_handle = route_idx;
358 rsxadev->routes[route_idx].input = in;
359 rsxadev->routes[route_idx].config.input_channel_mask = config->channel_mask;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700360 }
361 if (out) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800362 out->route_handle = route_idx;
363 rsxadev->routes[route_idx].output = out;
364 rsxadev->routes[route_idx].config.output_channel_mask = config->channel_mask;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700365 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800366 // Save the address
367 strncpy(rsxadev->routes[route_idx].address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
368 ALOGD(" now using address %s for route %d", rsxadev->routes[route_idx].address, route_idx);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700369 // If a pipe isn't associated with the device, create one.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800370 if (rsxadev->routes[route_idx].rsxSink == NULL || rsxadev->routes[route_idx].rsxSource == NULL)
371 {
372 struct submix_config * const device_config = &rsxadev->routes[route_idx].config;
Eric Laurentdd45fd32014-07-01 20:32:28 -0700373 uint32_t channel_count;
Jean-Michel Trivia33c1652021-07-08 11:21:33 -0700374 if (out) {
Eric Laurentdd45fd32014-07-01 20:32:28 -0700375 channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Jean-Michel Trivia33c1652021-07-08 11:21:33 -0700376 } else {
Eric Laurentdd45fd32014-07-01 20:32:28 -0700377 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Jean-Michel Trivia33c1652021-07-08 11:21:33 -0700378 }
379
Stewart Miles10f1a802014-06-09 20:54:37 -0700380 const uint32_t pipe_channel_count = channel_count;
Jean-Michel Trivia33c1652021-07-08 11:21:33 -0700381
Stewart Miles10f1a802014-06-09 20:54:37 -0700382 const NBAIO_Format format = Format_from_SR_C(config->sample_rate, pipe_channel_count,
383 config->format);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700384 const NBAIO_Format offers[1] = {format};
385 size_t numCounterOffers = 0;
Mikhail Naganov1d0e9732018-03-05 12:24:45 -0800386 // Create a MonoPipe with optional blocking set to true.
387 MonoPipe* sink = new MonoPipe(buffer_size_frames, format, true /*writeCanBlock*/);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700388 // Negotiation between the source and sink cannot fail as the device open operation
389 // creates both ends of the pipe using the same audio format.
390 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
391 ALOG_ASSERT(index == 0);
392 MonoPipeReader* source = new MonoPipeReader(sink);
393 numCounterOffers = 0;
394 index = source->negotiate(offers, 1, NULL, numCounterOffers);
395 ALOG_ASSERT(index == 0);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800396 ALOGV("submix_audio_device_create_pipe_l(): created pipe");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700397
398 // Save references to the source and sink.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800399 ALOG_ASSERT(rsxadev->routes[route_idx].rsxSink == NULL);
400 ALOG_ASSERT(rsxadev->routes[route_idx].rsxSource == NULL);
401 rsxadev->routes[route_idx].rsxSink = sink;
402 rsxadev->routes[route_idx].rsxSource = source;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700403 // Store the sanitized audio format in the device so that it's possible to determine
404 // the format of the pipe source when opening the input device.
405 memcpy(&device_config->common, config, sizeof(device_config->common));
406 device_config->buffer_size_frames = sink->maxFrames();
407 device_config->buffer_period_size_frames = device_config->buffer_size_frames /
408 buffer_period_count;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700409 if (in) device_config->pipe_frame_size = audio_stream_in_frame_size(&in->stream);
410 if (out) device_config->pipe_frame_size = audio_stream_out_frame_size(&out->stream);
Jean-Michel Trivia33c1652021-07-08 11:21:33 -0700411
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800412 SUBMIX_ALOGV("submix_audio_device_create_pipe_l(): pipe frame size %zd, pipe size %zd, "
Stewart Milese54c12c2014-05-01 09:03:27 -0700413 "period size %zd", device_config->pipe_frame_size,
414 device_config->buffer_size_frames, device_config->buffer_period_size_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700415 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700416}
417
418// Release references to the sink and source. Input and output threads may maintain references
419// to these objects via StrongPointer (sp<MonoPipe> and sp<MonoPipeReader>) which they can use
420// before they shutdown.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800421// Must be called with lock held on the submix_audio_device
422static void submix_audio_device_release_pipe_l(struct submix_audio_device * const rsxadev,
423 int route_idx)
Stewart Miles3dd36f92014-05-01 09:03:27 -0700424{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800425 ALOG_ASSERT(route_idx > -1);
426 ALOG_ASSERT(route_idx < MAX_ROUTES);
427 ALOGD("submix_audio_device_release_pipe_l(idx=%d) addr=%s", route_idx,
428 rsxadev->routes[route_idx].address);
429 if (rsxadev->routes[route_idx].rsxSink != 0) {
430 rsxadev->routes[route_idx].rsxSink.clear();
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800431 }
432 if (rsxadev->routes[route_idx].rsxSource != 0) {
433 rsxadev->routes[route_idx].rsxSource.clear();
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800434 }
435 memset(rsxadev->routes[route_idx].address, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700436}
437
438// Remove references to the specified input and output streams. When the device no longer
439// references input and output streams destroy the associated pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800440// Must be called with lock held on the submix_audio_device
441static void submix_audio_device_destroy_pipe_l(struct submix_audio_device * const rsxadev,
Stewart Miles3dd36f92014-05-01 09:03:27 -0700442 const struct submix_stream_in * const in,
443 const struct submix_stream_out * const out)
444{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800445 ALOGV("submix_audio_device_destroy_pipe_l()");
446 int route_idx = -1;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700447 if (in != NULL) {
Eric Laurent5b78d412019-03-01 18:39:26 -0800448 bool shut_down = false;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700449#if ENABLE_LEGACY_INPUT_OPEN
450 const_cast<struct submix_stream_in*>(in)->ref_count--;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800451 route_idx = in->route_handle;
452 ALOG_ASSERT(rsxadev->routes[route_idx].input == in);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700453 if (in->ref_count == 0) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800454 rsxadev->routes[route_idx].input = NULL;
Eric Laurent5b78d412019-03-01 18:39:26 -0800455 shut_down = true;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700456 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800457 ALOGV("submix_audio_device_destroy_pipe_l(): input ref_count %d", in->ref_count);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700458#else
Mikhail Naganov1462c762019-07-26 09:22:34 -0700459 route_idx = in->route_handle;
460 ALOG_ASSERT(rsxadev->routes[route_idx].input == in);
461 rsxadev->routes[route_idx].input = NULL;
Eric Laurent5b78d412019-03-01 18:39:26 -0800462 shut_down = true;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700463#endif // ENABLE_LEGACY_INPUT_OPEN
Eric Laurent5b78d412019-03-01 18:39:26 -0800464 if (shut_down) {
465 sp <MonoPipe> sink = rsxadev->routes[in->route_handle].rsxSink;
466 if (sink != NULL) {
467 sink->shutdown(true);
468 }
469 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700470 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800471 if (out != NULL) {
472 route_idx = out->route_handle;
473 ALOG_ASSERT(rsxadev->routes[route_idx].output == out);
474 rsxadev->routes[route_idx].output = NULL;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700475 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800476 if (route_idx != -1 &&
477 rsxadev->routes[route_idx].input == NULL && rsxadev->routes[route_idx].output == NULL) {
478 submix_audio_device_release_pipe_l(rsxadev, route_idx);
479 ALOGD("submix_audio_device_destroy_pipe_l(): pipe destroyed");
480 }
Stewart Miles3dd36f92014-05-01 09:03:27 -0700481}
482
Stewart Miles70726842014-05-01 09:03:27 -0700483// Sanitize the user specified audio config for a submix input / output stream.
484static void submix_sanitize_config(struct audio_config * const config, const bool is_input_format)
485{
486 config->channel_mask = is_input_format ? get_supported_channel_in_mask(config->channel_mask) :
487 get_supported_channel_out_mask(config->channel_mask);
488 config->sample_rate = get_supported_sample_rate(config->sample_rate);
489 config->format = DEFAULT_FORMAT;
490}
491
492// Verify a submix input or output stream can be opened.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800493// Must be called with lock held on the submix_audio_device
494static bool submix_open_validate_l(const struct submix_audio_device * const rsxadev,
495 int route_idx,
Stewart Miles70726842014-05-01 09:03:27 -0700496 const struct audio_config * const config,
497 const bool opening_input)
498{
Stewart Miles3dd36f92014-05-01 09:03:27 -0700499 bool input_open;
500 bool output_open;
Stewart Miles70726842014-05-01 09:03:27 -0700501 audio_config pipe_config;
502
503 // Query the device for the current audio config and whether input and output streams are open.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800504 output_open = rsxadev->routes[route_idx].output != NULL;
505 input_open = rsxadev->routes[route_idx].input != NULL;
506 memcpy(&pipe_config, &rsxadev->routes[route_idx].config.common, sizeof(pipe_config));
Stewart Miles70726842014-05-01 09:03:27 -0700507
Stewart Miles3dd36f92014-05-01 09:03:27 -0700508 // If the stream is already open, don't open it again.
509 if (opening_input ? !ENABLE_LEGACY_INPUT_OPEN && input_open : output_open) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800510 ALOGE("submix_open_validate_l(): %s stream already open.", opening_input ? "Input" :
Stewart Miles3dd36f92014-05-01 09:03:27 -0700511 "Output");
512 return false;
513 }
514
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800515 SUBMIX_ALOGV("submix_open_validate_l(): sample rate=%d format=%x "
Stewart Miles3dd36f92014-05-01 09:03:27 -0700516 "%s_channel_mask=%x", config->sample_rate, config->format,
517 opening_input ? "in" : "out", config->channel_mask);
518
519 // If either stream is open, verify the existing audio config the pipe matches the user
Stewart Miles70726842014-05-01 09:03:27 -0700520 // specified config.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700521 if (input_open || output_open) {
Stewart Miles70726842014-05-01 09:03:27 -0700522 const audio_config * const input_config = opening_input ? config : &pipe_config;
523 const audio_config * const output_config = opening_input ? &pipe_config : config;
524 // Get the channel mask of the open device.
525 pipe_config.channel_mask =
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800526 opening_input ? rsxadev->routes[route_idx].config.output_channel_mask :
527 rsxadev->routes[route_idx].config.input_channel_mask;
Stewart Miles70726842014-05-01 09:03:27 -0700528 if (!audio_config_compare(input_config, output_config)) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800529 ALOGE("submix_open_validate_l(): Unsupported format.");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700530 return false;
Stewart Miles70726842014-05-01 09:03:27 -0700531 }
532 }
533 return true;
534}
535
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800536// Must be called with lock held on the submix_audio_device
537static status_t submix_get_route_idx_for_address_l(const struct submix_audio_device * const rsxadev,
538 const char* address, /*in*/
539 int *idx /*out*/)
540{
541 // Do we already have a route for this address
542 int route_idx = -1;
543 int route_empty_idx = -1; // index of an empty route slot that can be used if needed
544 for (int i=0 ; i < MAX_ROUTES ; i++) {
545 if (strcmp(rsxadev->routes[i].address, "") == 0) {
546 route_empty_idx = i;
547 }
548 if (strncmp(rsxadev->routes[i].address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
549 route_idx = i;
550 break;
551 }
552 }
553
554 if ((route_idx == -1) && (route_empty_idx == -1)) {
555 ALOGE("Cannot create new route for address %s, max number of routes reached", address);
556 return -ENOMEM;
557 }
558 if (route_idx == -1) {
559 route_idx = route_empty_idx;
560 }
561 *idx = route_idx;
562 return OK;
563}
564
565
Stewart Milese54c12c2014-05-01 09:03:27 -0700566// Calculate the maximum size of the pipe buffer in frames for the specified stream.
567static size_t calculate_stream_pipe_size_in_frames(const struct audio_stream *stream,
568 const struct submix_config *config,
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700569 const size_t pipe_frames,
570 const size_t stream_frame_size)
Stewart Milese54c12c2014-05-01 09:03:27 -0700571{
Stewart Milese54c12c2014-05-01 09:03:27 -0700572 const size_t pipe_frame_size = config->pipe_frame_size;
573 const size_t max_frame_size = max(stream_frame_size, pipe_frame_size);
574 return (pipe_frames * config->pipe_frame_size) / max_frame_size;
575}
576
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700577/* audio HAL functions */
578
579static uint32_t out_get_sample_rate(const struct audio_stream *stream)
580{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700581 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
582 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800583 const uint32_t out_rate = out->dev->routes[out->route_handle].config.common.sample_rate;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800584 SUBMIX_ALOGV("out_get_sample_rate() returns %u for addr %s",
585 out_rate, out->dev->routes[out->route_handle].address);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700586 return out_rate;
587}
588
589static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
590{
Stewart Miles02c2f712014-05-01 09:03:27 -0700591 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Stewart Miles70726842014-05-01 09:03:27 -0700592 if (!sample_rate_supported(rate)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700593 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
594 return -ENOSYS;
595 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700596 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800597 out->dev->routes[out->route_handle].config.common.sample_rate = rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700598 return 0;
599}
600
601static size_t out_get_buffer_size(const struct audio_stream *stream)
602{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700603 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
604 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800605 const struct submix_config * const config = &out->dev->routes[out->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700606 const size_t stream_frame_size =
607 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700608 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700609 stream, config, config->buffer_period_size_frames, stream_frame_size);
610 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Miles568e66f2014-05-01 09:03:27 -0700611 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
Stewart Milese54c12c2014-05-01 09:03:27 -0700612 buffer_size_bytes, buffer_size_frames);
613 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700614}
615
616static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
617{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700618 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
619 const_cast<struct audio_stream *>(stream));
Mikhail Naganove7276172020-10-01 18:07:59 -0700620 audio_channel_mask_t channel_mask =
621 out->dev->routes[out->route_handle].config.output_channel_mask;
Stewart Miles568e66f2014-05-01 09:03:27 -0700622 SUBMIX_ALOGV("out_get_channels() returns %08x", channel_mask);
623 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700624}
625
626static audio_format_t out_get_format(const struct audio_stream *stream)
627{
Stewart Miles568e66f2014-05-01 09:03:27 -0700628 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
629 const_cast<struct audio_stream *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800630 const audio_format_t format = out->dev->routes[out->route_handle].config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700631 SUBMIX_ALOGV("out_get_format() returns %x", format);
632 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700633}
634
635static int out_set_format(struct audio_stream *stream, audio_format_t format)
636{
Stewart Miles568e66f2014-05-01 09:03:27 -0700637 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800638 if (format != out->dev->routes[out->route_handle].config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700639 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700640 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700641 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700642 SUBMIX_ALOGV("out_set_format(format=%x)", format);
643 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700644}
645
646static int out_standby(struct audio_stream *stream)
647{
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700648 ALOGI("out_standby()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800649 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
650 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700651
Stewart Milesf645c5e2014-05-01 09:03:27 -0700652 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700653
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800654 out->output_standby = true;
Andy Hung0b93c0a2015-08-10 13:52:34 -0700655 out->frames_written_since_standby = 0;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700656
Stewart Milesf645c5e2014-05-01 09:03:27 -0700657 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700658
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700659 return 0;
660}
661
662static int out_dump(const struct audio_stream *stream, int fd)
663{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700664 (void)stream;
665 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700666 return 0;
667}
668
669static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
670{
Mikhail Naganov1d0e9732018-03-05 12:24:45 -0800671 int exiting = -1;
672 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700673 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Mikhail Naganov1d0e9732018-03-05 12:24:45 -0800674
675 // FIXME this is using hard-coded strings but in the future, this functionality will be
676 // converted to use audio HAL extensions required to support tunneling
677 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
678 struct submix_audio_device * const rsxadev =
679 audio_stream_get_submix_stream_out(stream)->dev;
680 pthread_mutex_lock(&rsxadev->lock);
681 { // using the sink
682 sp<MonoPipe> sink =
683 rsxadev->routes[audio_stream_get_submix_stream_out(stream)->route_handle]
684 .rsxSink;
685 if (sink == NULL) {
686 pthread_mutex_unlock(&rsxadev->lock);
687 return 0;
688 }
689
690 ALOGD("out_set_parameters(): shutting down MonoPipe sink");
691 sink->shutdown(true);
692 } // done using the sink
693 pthread_mutex_unlock(&rsxadev->lock);
694 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700695 return 0;
696}
697
698static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
699{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700700 (void)stream;
701 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700702 return strdup("");
703}
704
705static uint32_t out_get_latency(const struct audio_stream_out *stream)
706{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700707 const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
708 const_cast<struct audio_stream_out *>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800709 const struct submix_config * const config = &out->dev->routes[out->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700710 const size_t stream_frame_size =
711 audio_stream_out_frame_size(stream);
Stewart Milese54c12c2014-05-01 09:03:27 -0700712 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700713 &stream->common, config, config->buffer_size_frames, stream_frame_size);
Stewart Miles10f1a802014-06-09 20:54:37 -0700714 const uint32_t sample_rate = out_get_sample_rate(&stream->common);
715 const uint32_t latency_ms = (buffer_size_frames * 1000) / sample_rate;
Stewart Milese54c12c2014-05-01 09:03:27 -0700716 SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u",
Stewart Miles10f1a802014-06-09 20:54:37 -0700717 latency_ms, buffer_size_frames, sample_rate);
Stewart Miles568e66f2014-05-01 09:03:27 -0700718 return latency_ms;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700719}
720
721static int out_set_volume(struct audio_stream_out *stream, float left,
722 float right)
723{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700724 (void)stream;
725 (void)left;
726 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700727 return -ENOSYS;
728}
729
730static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
731 size_t bytes)
732{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700733 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700734 ssize_t written_frames = 0;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700735 const size_t frame_size = audio_stream_out_frame_size(stream);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700736 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
737 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700738 const size_t frames = bytes / frame_size;
739
Stewart Milesf645c5e2014-05-01 09:03:27 -0700740 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700741
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800742 out->output_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700743
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800744 sp<MonoPipe> sink = rsxadev->routes[out->route_handle].rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700745 if (sink != NULL) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700746 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800747 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700748 pthread_mutex_unlock(&rsxadev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700749 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700750 // the pipe has already been shutdown, this buffer will be lost but we must
751 // simulate timing so we don't drain the output faster than realtime
752 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
François Gaffie71832e72019-04-12 10:48:55 +0200753
754 pthread_mutex_lock(&rsxadev->lock);
755 out->frames_written += frames;
756 out->frames_written_since_standby += frames;
757 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700758 return bytes;
759 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700760 } else {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700761 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700762 ALOGE("out_write without a pipe!");
763 ALOG_ASSERT("out_write without a pipe!");
764 return 0;
765 }
766
Eric Laurent77887162019-10-14 13:25:01 -0700767 // If the write to the sink would block, flush enough frames
Stewart Miles2d199fe2014-05-01 09:03:27 -0700768 // from the pipe to make space to write the most recent data.
Eric Laurent77887162019-10-14 13:25:01 -0700769 // We DO NOT block if:
770 // - no peer input stream is present
771 // - the peer input is in standby AFTER having been active.
772 // We DO block if:
773 // - the input was never activated to avoid discarding first frames
774 // in the pipe in case capture start was delayed
Stewart Miles2d199fe2014-05-01 09:03:27 -0700775 {
776 const size_t availableToWrite = sink->availableToWrite();
Eric Laurent2cadb582018-11-02 15:06:38 -0700777 // NOTE: rsxSink has been checked above and sink and source life cycles are synchronized
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800778 sp<MonoPipeReader> source = rsxadev->routes[out->route_handle].rsxSource;
Eric Laurent77887162019-10-14 13:25:01 -0700779 const struct submix_stream_in *in = rsxadev->routes[out->route_handle].input;
780 const bool dont_block = (in == NULL)
Mikhail Naganov8c97d242021-03-11 13:24:35 -0800781 || (in->input_standby && (in->read_counter_frames_since_standby != 0));
Eric Laurent77887162019-10-14 13:25:01 -0700782 if (dont_block && availableToWrite < frames) {
Stewart Miles2d199fe2014-05-01 09:03:27 -0700783 static uint8_t flush_buffer[64];
784 const size_t flushBufferSizeFrames = sizeof(flush_buffer) / frame_size;
785 size_t frames_to_flush_from_source = frames - availableToWrite;
Mikhail Naganov80179932018-02-15 17:07:19 -0800786 SUBMIX_ALOGV("out_write(): flushing %llu frames from the pipe to avoid blocking",
787 (unsigned long long)frames_to_flush_from_source);
Stewart Miles2d199fe2014-05-01 09:03:27 -0700788 while (frames_to_flush_from_source) {
789 const size_t flush_size = min(frames_to_flush_from_source, flushBufferSizeFrames);
790 frames_to_flush_from_source -= flush_size;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800791 // read does not block
Glenn Kasten04c88492016-01-06 14:05:23 -0800792 source->read(flush_buffer, flush_size);
Stewart Miles2d199fe2014-05-01 09:03:27 -0700793 }
794 }
795 }
796
Stewart Milesf645c5e2014-05-01 09:03:27 -0700797 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700798
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700799 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800800
Stewart Miles92854f52014-05-01 09:03:27 -0700801#if LOG_STREAMS_TO_FILES
802 if (out->log_fd >= 0) write(out->log_fd, buffer, written_frames * frame_size);
803#endif // LOG_STREAMS_TO_FILES
804
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700805 if (written_frames < 0) {
806 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700807 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700808
Stewart Milesf645c5e2014-05-01 09:03:27 -0700809 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800810 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700811 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700812
813 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700814 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700815 } else {
816 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700817 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700818 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700819 }
820 }
821
Stewart Milesf645c5e2014-05-01 09:03:27 -0700822 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800823 sink.clear();
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700824 if (written_frames > 0) {
Andy Hung0b93c0a2015-08-10 13:52:34 -0700825 out->frames_written_since_standby += written_frames;
826 out->frames_written += written_frames;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700827 }
Stewart Milesf645c5e2014-05-01 09:03:27 -0700828 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700829
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700830 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700831 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700832 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700833 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700834 const ssize_t written_bytes = written_frames * frame_size;
Stewart Miles02c2f712014-05-01 09:03:27 -0700835 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames", written_bytes, written_frames);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700836 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700837}
838
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700839static int out_get_presentation_position(const struct audio_stream_out *stream,
840 uint64_t *frames, struct timespec *timestamp)
841{
Andy Hung0b93c0a2015-08-10 13:52:34 -0700842 if (stream == NULL || frames == NULL || timestamp == NULL) {
843 return -EINVAL;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700844 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700845
846 const submix_stream_out *out = audio_stream_out_get_submix_stream_out(
847 const_cast<struct audio_stream_out *>(stream));
848 struct submix_audio_device * const rsxadev = out->dev;
849
850 int ret = -EWOULDBLOCK;
851 pthread_mutex_lock(&rsxadev->lock);
Eric Laurent2cadb582018-11-02 15:06:38 -0700852 sp<MonoPipeReader> source = rsxadev->routes[out->route_handle].rsxSource;
853 if (source == NULL) {
854 ALOGW("%s called on released output", __FUNCTION__);
855 pthread_mutex_unlock(&rsxadev->lock);
856 return -ENODEV;
857 }
858
859 const ssize_t frames_in_pipe = source->availableToRead();
Andy Hung0b93c0a2015-08-10 13:52:34 -0700860 if (CC_UNLIKELY(frames_in_pipe < 0)) {
861 *frames = out->frames_written;
862 ret = 0;
863 } else if (out->frames_written >= (uint64_t)frames_in_pipe) {
864 *frames = out->frames_written - frames_in_pipe;
865 ret = 0;
866 }
867 pthread_mutex_unlock(&rsxadev->lock);
868
869 if (ret == 0) {
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700870 clock_gettime(CLOCK_MONOTONIC, timestamp);
871 }
872
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700873 SUBMIX_ALOGV("out_get_presentation_position() got frames=%llu timestamp sec=%llu",
Mikhail Naganov80179932018-02-15 17:07:19 -0800874 frames ? (unsigned long long)*frames : -1ULL,
875 timestamp ? (unsigned long long)timestamp->tv_sec : -1ULL);
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700876
877 return ret;
878}
879
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700880static int out_get_render_position(const struct audio_stream_out *stream,
881 uint32_t *dsp_frames)
882{
Andy Hung0b93c0a2015-08-10 13:52:34 -0700883 if (stream == NULL || dsp_frames == NULL) {
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700884 return -EINVAL;
885 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700886
887 const submix_stream_out *out = audio_stream_out_get_submix_stream_out(
888 const_cast<struct audio_stream_out *>(stream));
889 struct submix_audio_device * const rsxadev = out->dev;
890
891 pthread_mutex_lock(&rsxadev->lock);
Eric Laurent2cadb582018-11-02 15:06:38 -0700892 sp<MonoPipeReader> source = rsxadev->routes[out->route_handle].rsxSource;
893 if (source == NULL) {
894 ALOGW("%s called on released output", __FUNCTION__);
895 pthread_mutex_unlock(&rsxadev->lock);
896 return -ENODEV;
897 }
898
899 const ssize_t frames_in_pipe = source->availableToRead();
Andy Hung0b93c0a2015-08-10 13:52:34 -0700900 if (CC_UNLIKELY(frames_in_pipe < 0)) {
901 *dsp_frames = (uint32_t)out->frames_written_since_standby;
902 } else {
903 *dsp_frames = out->frames_written_since_standby > (uint64_t) frames_in_pipe ?
904 (uint32_t)(out->frames_written_since_standby - frames_in_pipe) : 0;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -0700905 }
Andy Hung0b93c0a2015-08-10 13:52:34 -0700906 pthread_mutex_unlock(&rsxadev->lock);
907
908 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700909}
910
911static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
912{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700913 (void)stream;
914 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700915 return 0;
916}
917
918static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
919{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700920 (void)stream;
921 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700922 return 0;
923}
924
925static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
926 int64_t *timestamp)
927{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700928 (void)stream;
929 (void)timestamp;
Mikhail Naganov739ce6b2019-11-05 12:33:22 -0800930 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700931}
932
933/** audio_stream_in implementation **/
934static uint32_t in_get_sample_rate(const struct audio_stream *stream)
935{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700936 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
937 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800938 const uint32_t rate = in->dev->routes[in->route_handle].config.common.sample_rate;
Stewart Miles02c2f712014-05-01 09:03:27 -0700939 SUBMIX_ALOGV("in_get_sample_rate() returns %u", rate);
940 return rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700941}
942
943static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
944{
Stewart Miles568e66f2014-05-01 09:03:27 -0700945 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Stewart Miles70726842014-05-01 09:03:27 -0700946 if (!sample_rate_supported(rate)) {
947 ALOGE("in_set_sample_rate(rate=%u) rate unsupported", rate);
948 return -ENOSYS;
949 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800950 in->dev->routes[in->route_handle].config.common.sample_rate = rate;
Stewart Miles568e66f2014-05-01 09:03:27 -0700951 SUBMIX_ALOGV("in_set_sample_rate() set %u", rate);
952 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700953}
954
955static size_t in_get_buffer_size(const struct audio_stream *stream)
956{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700957 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
958 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800959 const struct submix_config * const config = &in->dev->routes[in->route_handle].config;
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700960 const size_t stream_frame_size =
961 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Stewart Miles02c2f712014-05-01 09:03:27 -0700962 size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700963 stream, config, config->buffer_period_size_frames, stream_frame_size);
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700964 const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
Stewart Milese54c12c2014-05-01 09:03:27 -0700965 SUBMIX_ALOGV("in_get_buffer_size() returns %zu bytes, %zu frames", buffer_size_bytes,
966 buffer_size_frames);
967 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700968}
969
970static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
971{
Stewart Miles70726842014-05-01 09:03:27 -0700972 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
973 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800974 const audio_channel_mask_t channel_mask =
975 in->dev->routes[in->route_handle].config.input_channel_mask;
Stewart Miles70726842014-05-01 09:03:27 -0700976 SUBMIX_ALOGV("in_get_channels() returns %x", channel_mask);
977 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700978}
979
980static audio_format_t in_get_format(const struct audio_stream *stream)
981{
Stewart Miles568e66f2014-05-01 09:03:27 -0700982 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
Stewart Miles70726842014-05-01 09:03:27 -0700983 const_cast<struct audio_stream*>(stream));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800984 const audio_format_t format = in->dev->routes[in->route_handle].config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700985 SUBMIX_ALOGV("in_get_format() returns %x", format);
986 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700987}
988
989static int in_set_format(struct audio_stream *stream, audio_format_t format)
990{
Stewart Miles568e66f2014-05-01 09:03:27 -0700991 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -0800992 if (format != in->dev->routes[in->route_handle].config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700993 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700994 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700995 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700996 SUBMIX_ALOGV("in_set_format(format=%x)", format);
997 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700998}
999
1000static int in_standby(struct audio_stream *stream)
1001{
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001002 ALOGI("in_standby()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001003 struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
1004 struct submix_audio_device * const rsxadev = in->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001005
Stewart Milesf645c5e2014-05-01 09:03:27 -07001006 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001007
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001008 in->input_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001009
Stewart Milesf645c5e2014-05-01 09:03:27 -07001010 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001011
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001012 return 0;
1013}
1014
1015static int in_dump(const struct audio_stream *stream, int fd)
1016{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001017 (void)stream;
1018 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001019 return 0;
1020}
1021
1022static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1023{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001024 (void)stream;
1025 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001026 return 0;
1027}
1028
1029static char * in_get_parameters(const struct audio_stream *stream,
1030 const char *keys)
1031{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001032 (void)stream;
1033 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001034 return strdup("");
1035}
1036
1037static int in_set_gain(struct audio_stream_in *stream, float gain)
1038{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001039 (void)stream;
1040 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001041 return 0;
1042}
1043
1044static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
1045 size_t bytes)
1046{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001047 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
1048 struct submix_audio_device * const rsxadev = in->dev;
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001049 const size_t frame_size = audio_stream_in_frame_size(stream);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001050 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001051
Stewart Milesc049a0a2014-05-01 09:03:27 -07001052 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
Stewart Milesf645c5e2014-05-01 09:03:27 -07001053 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001054
Jean-Michel Trivi257fde62014-12-09 20:20:15 -08001055 const bool output_standby = rsxadev->routes[in->route_handle].output == NULL
1056 ? true : rsxadev->routes[in->route_handle].output->output_standby;
1057 const bool output_standby_transition = (in->output_standby_rec_thr != output_standby);
1058 in->output_standby_rec_thr = output_standby;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001059
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001060 if (in->input_standby || output_standby_transition) {
1061 in->input_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001062 // keep track of when we exit input standby (== first read == start "real recording")
1063 // or when we start recording silence, and reset projected time
1064 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
1065 if (rc == 0) {
Mikhail Naganov8c97d242021-03-11 13:24:35 -08001066 in->read_counter_frames_since_standby = 0;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001067 }
1068 }
1069
1070 in->read_counter_frames += frames_to_read;
Mikhail Naganov8c97d242021-03-11 13:24:35 -08001071 in->read_counter_frames_since_standby += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001072 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001073
1074 {
1075 // about to read from audio source
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001076 sp<MonoPipeReader> source = rsxadev->routes[in->route_handle].rsxSource;
Stewart Milesf645c5e2014-05-01 09:03:27 -07001077 if (source == NULL) {
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001078 in->read_error_count++;// ok if it rolls over
1079 ALOGE_IF(in->read_error_count < MAX_READ_ERROR_LOGS,
1080 "no audio pipe yet we're trying to read! (not all errors will be logged)");
Stewart Milesf645c5e2014-05-01 09:03:27 -07001081 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001082 usleep(frames_to_read * 1000000 / in_get_sample_rate(&stream->common));
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001083 memset(buffer, 0, bytes);
1084 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001085 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001086
Stewart Milesf645c5e2014-05-01 09:03:27 -07001087 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001088
1089 // read the data from the pipe (it's non blocking)
1090 int attempts = 0;
1091 char* buff = (char*)buffer;
Stewart Miles02c2f712014-05-01 09:03:27 -07001092
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001093 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
Stewart Miles02c2f712014-05-01 09:03:27 -07001094 ssize_t frames_read = -1977;
Stewart Milese54c12c2014-05-01 09:03:27 -07001095 size_t read_frames = remaining_frames;
Stewart Milese54c12c2014-05-01 09:03:27 -07001096
1097 SUBMIX_ALOGV("in_read(): frames available to read %zd", source->availableToRead());
1098
Glenn Kasten04c88492016-01-06 14:05:23 -08001099 frames_read = source->read(buff, read_frames);
Stewart Milese54c12c2014-05-01 09:03:27 -07001100
1101 SUBMIX_ALOGV("in_read(): frames read %zd", frames_read);
1102
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001103 if (frames_read > 0) {
Stewart Miles92854f52014-05-01 09:03:27 -07001104#if LOG_STREAMS_TO_FILES
1105 if (in->log_fd >= 0) write(in->log_fd, buff, frames_read * frame_size);
1106#endif // LOG_STREAMS_TO_FILES
1107
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001108 remaining_frames -= frames_read;
1109 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001110 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
1111 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001112 } else {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001113 attempts++;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001114 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001115 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
1116 }
1117 }
1118 // done using the source
Stewart Milesf645c5e2014-05-01 09:03:27 -07001119 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -08001120 source.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -07001121 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001122 }
1123
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001124 if (remaining_frames > 0) {
Stewart Miles3dd36f92014-05-01 09:03:27 -07001125 const size_t remaining_bytes = remaining_frames * frame_size;
Stewart Miles10f1a802014-06-09 20:54:37 -07001126 SUBMIX_ALOGV(" clearing remaining_frames = %zu", remaining_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001127 memset(((char*)buffer)+ bytes - remaining_bytes, 0, remaining_bytes);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001128 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001129
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001130 // compute how much we need to sleep after reading the data by comparing the wall clock with
1131 // the projected time at which we should return.
1132 struct timespec time_after_read;// wall clock after reading from the pipe
1133 struct timespec record_duration;// observed record duration
1134 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
1135 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
1136 if (rc == 0) {
1137 // for how long have we been recording?
1138 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
1139 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
1140 if (record_duration.tv_nsec < 0) {
1141 record_duration.tv_sec--;
1142 record_duration.tv_nsec += 1000000000;
1143 }
1144
Mikhail Naganov8c97d242021-03-11 13:24:35 -08001145 // read_counter_frames_since_standby contains the number of frames that have been read since
1146 // the beginning of recording (including this call): it's converted to usec and compared to
Stewart Milesf645c5e2014-05-01 09:03:27 -07001147 // how long we've been recording for, which gives us how long we must wait to sync the
1148 // projected recording time, and the observed recording time.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001149 long projected_vs_observed_offset_us =
Mikhail Naganov8c97d242021-03-11 13:24:35 -08001150 ((int64_t)(in->read_counter_frames_since_standby
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001151 - (record_duration.tv_sec*sample_rate)))
1152 * 1000000 / sample_rate
1153 - (record_duration.tv_nsec / 1000);
1154
Stewart Milesc049a0a2014-05-01 09:03:27 -07001155 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001156 record_duration.tv_sec, record_duration.tv_nsec/1000000,
1157 projected_vs_observed_offset_us);
1158 if (projected_vs_observed_offset_us > 0) {
1159 usleep(projected_vs_observed_offset_us);
1160 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001161 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001162
Stewart Milesc049a0a2014-05-01 09:03:27 -07001163 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001164 return bytes;
1165
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001166}
1167
1168static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1169{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001170 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001171 return 0;
1172}
1173
Mikhail Naganov8c97d242021-03-11 13:24:35 -08001174static int in_get_capture_position(const struct audio_stream_in *stream,
1175 int64_t *frames, int64_t *time)
1176{
1177 if (stream == NULL || frames == NULL || time == NULL) {
1178 return -EINVAL;
1179 }
1180
1181 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(
1182 (struct audio_stream_in*)stream);
1183 struct submix_audio_device * const rsxadev = in->dev;
1184
1185 pthread_mutex_lock(&rsxadev->lock);
1186 sp<MonoPipeReader> source = rsxadev->routes[in->route_handle].rsxSource;
1187 if (source == NULL) {
1188 ALOGW("%s called on released input", __FUNCTION__);
1189 pthread_mutex_unlock(&rsxadev->lock);
1190 return -ENODEV;
1191 }
1192 *frames = in->read_counter_frames;
1193 const ssize_t frames_in_pipe = source->availableToRead();
1194 pthread_mutex_unlock(&rsxadev->lock);
1195 if (frames_in_pipe > 0) {
1196 *frames += frames_in_pipe;
1197 }
1198
1199 struct timespec timestamp;
1200 clock_gettime(CLOCK_MONOTONIC, &timestamp);
1201 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec;
1202 return 0;
1203}
1204
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001205static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1206{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001207 (void)stream;
1208 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001209 return 0;
1210}
1211
1212static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1213{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001214 (void)stream;
1215 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001216 return 0;
1217}
1218
1219static int adev_open_output_stream(struct audio_hw_device *dev,
1220 audio_io_handle_t handle,
1221 audio_devices_t devices,
1222 audio_output_flags_t flags,
1223 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -07001224 struct audio_stream_out **stream_out,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001225 const char *address)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001226{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001227 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001228 ALOGD("adev_open_output_stream(address=%s)", address);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001229 struct submix_stream_out *out;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001230 (void)handle;
1231 (void)devices;
1232 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001233
Stewart Miles3dd36f92014-05-01 09:03:27 -07001234 *stream_out = NULL;
1235
Stewart Miles70726842014-05-01 09:03:27 -07001236 // Make sure it's possible to open the device given the current audio config.
1237 submix_sanitize_config(config, false);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001238
1239 int route_idx = -1;
1240
1241 pthread_mutex_lock(&rsxadev->lock);
1242
1243 status_t res = submix_get_route_idx_for_address_l(rsxadev, address, &route_idx);
1244 if (res != OK) {
1245 ALOGE("Error %d looking for address=%s in adev_open_output_stream", res, address);
1246 pthread_mutex_unlock(&rsxadev->lock);
1247 return res;
1248 }
1249
1250 if (!submix_open_validate_l(rsxadev, route_idx, config, false)) {
1251 ALOGE("adev_open_output_stream(): Unable to open output stream for address %s", address);
1252 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles70726842014-05-01 09:03:27 -07001253 return -EINVAL;
1254 }
1255
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001256 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001257 if (!out) {
1258 pthread_mutex_unlock(&rsxadev->lock);
1259 return -ENOMEM;
1260 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001261
Stewart Miles568e66f2014-05-01 09:03:27 -07001262 // Initialize the function pointer tables (v-tables).
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001263 out->stream.common.get_sample_rate = out_get_sample_rate;
1264 out->stream.common.set_sample_rate = out_set_sample_rate;
1265 out->stream.common.get_buffer_size = out_get_buffer_size;
1266 out->stream.common.get_channels = out_get_channels;
1267 out->stream.common.get_format = out_get_format;
1268 out->stream.common.set_format = out_set_format;
1269 out->stream.common.standby = out_standby;
1270 out->stream.common.dump = out_dump;
1271 out->stream.common.set_parameters = out_set_parameters;
1272 out->stream.common.get_parameters = out_get_parameters;
1273 out->stream.common.add_audio_effect = out_add_audio_effect;
1274 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1275 out->stream.get_latency = out_get_latency;
1276 out->stream.set_volume = out_set_volume;
1277 out->stream.write = out_write;
1278 out->stream.get_render_position = out_get_render_position;
1279 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Jean-Michel Trivi25f47512015-05-26 14:18:10 -07001280 out->stream.get_presentation_position = out_get_presentation_position;
1281
Stewart Miles10f1a802014-06-09 20:54:37 -07001282 // If the sink has been shutdown or pipe recreation is forced (see above), delete the pipe so
1283 // that it's recreated.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001284 if ((rsxadev->routes[route_idx].rsxSink != NULL
Greg Kaiser3f7bbbb2022-01-05 07:00:42 -08001285 && rsxadev->routes[route_idx].rsxSink->isShutdown())) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001286 submix_audio_device_release_pipe_l(rsxadev, route_idx);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001287 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001288
Stewart Miles568e66f2014-05-01 09:03:27 -07001289 // Store a pointer to the device from the output stream.
1290 out->dev = rsxadev;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001291 // Initialize the pipe.
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001292 ALOGV("adev_open_output_stream(): about to create pipe at index %d", route_idx);
1293 submix_audio_device_create_pipe_l(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1294 DEFAULT_PIPE_PERIOD_COUNT, NULL, out, address, route_idx);
Stewart Miles92854f52014-05-01 09:03:27 -07001295#if LOG_STREAMS_TO_FILES
1296 out->log_fd = open(LOG_STREAM_OUT_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1297 LOG_STREAM_FILE_PERMISSIONS);
1298 ALOGE_IF(out->log_fd < 0, "adev_open_output_stream(): log file open failed %s",
1299 strerror(errno));
1300 ALOGV("adev_open_output_stream(): log_fd = %d", out->log_fd);
1301#endif // LOG_STREAMS_TO_FILES
Stewart Miles568e66f2014-05-01 09:03:27 -07001302 // Return the output stream.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001303 *stream_out = &out->stream;
1304
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001305 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001306 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001307}
1308
1309static void adev_close_output_stream(struct audio_hw_device *dev,
1310 struct audio_stream_out *stream)
1311{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001312 struct submix_audio_device * rsxadev = audio_hw_device_get_submix_audio_device(
1313 const_cast<struct audio_hw_device*>(dev));
Stewart Miles3dd36f92014-05-01 09:03:27 -07001314 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001315
1316 pthread_mutex_lock(&rsxadev->lock);
1317 ALOGD("adev_close_output_stream() addr = %s", rsxadev->routes[out->route_handle].address);
1318 submix_audio_device_destroy_pipe_l(audio_hw_device_get_submix_audio_device(dev), NULL, out);
Stewart Miles92854f52014-05-01 09:03:27 -07001319#if LOG_STREAMS_TO_FILES
1320 if (out->log_fd >= 0) close(out->log_fd);
1321#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001322
1323 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001324 free(out);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001325}
1326
1327static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1328{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001329 (void)dev;
1330 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001331 return -ENOSYS;
1332}
1333
1334static char * adev_get_parameters(const struct audio_hw_device *dev,
1335 const char *keys)
1336{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001337 (void)dev;
1338 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001339 return strdup("");;
1340}
1341
1342static int adev_init_check(const struct audio_hw_device *dev)
1343{
1344 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -07001345 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001346 return 0;
1347}
1348
1349static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1350{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001351 (void)dev;
1352 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001353 return -ENOSYS;
1354}
1355
1356static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1357{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001358 (void)dev;
1359 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001360 return -ENOSYS;
1361}
1362
1363static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
1364{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001365 (void)dev;
1366 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001367 return -ENOSYS;
1368}
1369
1370static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1371{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001372 (void)dev;
1373 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001374 return -ENOSYS;
1375}
1376
1377static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1378{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001379 (void)dev;
1380 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001381 return -ENOSYS;
1382}
1383
1384static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1385{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001386 (void)dev;
1387 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001388 return 0;
1389}
1390
1391static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1392{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001393 (void)dev;
1394 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001395 return -ENOSYS;
1396}
1397
1398static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1399{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001400 (void)dev;
1401 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001402 return -ENOSYS;
1403}
1404
1405static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1406 const struct audio_config *config)
1407{
Stewart Miles568e66f2014-05-01 09:03:27 -07001408 if (audio_is_linear_pcm(config->format)) {
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001409 size_t max_buffer_period_size_frames = 0;
1410 struct submix_audio_device * rsxadev = audio_hw_device_get_submix_audio_device(
1411 const_cast<struct audio_hw_device*>(dev));
1412 // look for the largest buffer period size
1413 for (int i = 0 ; i < MAX_ROUTES ; i++) {
1414 if (rsxadev->routes[i].config.buffer_period_size_frames > max_buffer_period_size_frames)
1415 {
1416 max_buffer_period_size_frames = rsxadev->routes[i].config.buffer_period_size_frames;
1417 }
1418 }
Eric Laurentdd45fd32014-07-01 20:32:28 -07001419 const size_t frame_size_in_bytes = audio_channel_count_from_in_mask(config->channel_mask) *
Stewart Miles568e66f2014-05-01 09:03:27 -07001420 audio_bytes_per_sample(config->format);
Mikhail Naganov739ce6b2019-11-05 12:33:22 -08001421 if (max_buffer_period_size_frames == 0) {
1422 max_buffer_period_size_frames = DEFAULT_PIPE_SIZE_IN_FRAMES;
1423 }
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001424 const size_t buffer_size = max_buffer_period_size_frames * frame_size_in_bytes;
Stewart Miles10f1a802014-06-09 20:54:37 -07001425 SUBMIX_ALOGV("adev_get_input_buffer_size() returns %zu bytes, %zu frames",
Mikhail Naganov80179932018-02-15 17:07:19 -08001426 buffer_size, max_buffer_period_size_frames);
Stewart Miles568e66f2014-05-01 09:03:27 -07001427 return buffer_size;
1428 }
1429 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001430}
1431
1432static int adev_open_input_stream(struct audio_hw_device *dev,
1433 audio_io_handle_t handle,
1434 audio_devices_t devices,
1435 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -07001436 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001437 audio_input_flags_t flags __unused,
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001438 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001439 audio_source_t source __unused)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001440{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001441 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001442 struct submix_stream_in *in;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001443 ALOGD("adev_open_input_stream(addr=%s)", address);
Stewart Milesc049a0a2014-05-01 09:03:27 -07001444 (void)handle;
1445 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001446
Stewart Miles3dd36f92014-05-01 09:03:27 -07001447 *stream_in = NULL;
1448
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001449 // Do we already have a route for this address
1450 int route_idx = -1;
1451
1452 pthread_mutex_lock(&rsxadev->lock);
1453
1454 status_t res = submix_get_route_idx_for_address_l(rsxadev, address, &route_idx);
1455 if (res != OK) {
Jean-Michel Trivi79fbccf2016-04-05 17:20:29 -07001456 ALOGE("Error %d looking for address=%s in adev_open_input_stream", res, address);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001457 pthread_mutex_unlock(&rsxadev->lock);
1458 return res;
1459 }
1460
Stewart Miles70726842014-05-01 09:03:27 -07001461 // Make sure it's possible to open the device given the current audio config.
1462 submix_sanitize_config(config, true);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001463 if (!submix_open_validate_l(rsxadev, route_idx, config, true)) {
Stewart Miles70726842014-05-01 09:03:27 -07001464 ALOGE("adev_open_input_stream(): Unable to open input stream.");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001465 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles70726842014-05-01 09:03:27 -07001466 return -EINVAL;
1467 }
1468
Stewart Miles3dd36f92014-05-01 09:03:27 -07001469#if ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001470 in = rsxadev->routes[route_idx].input;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001471 if (in) {
1472 in->ref_count++;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001473 sp<MonoPipe> sink = rsxadev->routes[route_idx].rsxSink;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001474 ALOG_ASSERT(sink != NULL);
1475 // If the sink has been shutdown, delete the pipe.
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001476 if (sink != NULL) {
1477 if (sink->isShutdown()) {
1478 ALOGD(" Non-NULL shut down sink when opening input stream, releasing, refcount=%d",
1479 in->ref_count);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001480 submix_audio_device_release_pipe_l(rsxadev, in->route_handle);
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001481 } else {
1482 ALOGD(" Non-NULL sink when opening input stream, refcount=%d", in->ref_count);
1483 }
1484 } else {
1485 ALOGE("NULL sink when opening input stream, refcount=%d", in->ref_count);
1486 }
Stewart Miles3dd36f92014-05-01 09:03:27 -07001487 }
Stewart Miles3dd36f92014-05-01 09:03:27 -07001488#else
1489 in = NULL;
1490#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001491
Stewart Miles3dd36f92014-05-01 09:03:27 -07001492 if (!in) {
1493 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
1494 if (!in) return -ENOMEM;
Mikhail Naganov1462c762019-07-26 09:22:34 -07001495#if ENABLE_LEGACY_INPUT_OPEN
Stewart Miles3dd36f92014-05-01 09:03:27 -07001496 in->ref_count = 1;
Mikhail Naganov1462c762019-07-26 09:22:34 -07001497#endif
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001498
Stewart Miles3dd36f92014-05-01 09:03:27 -07001499 // Initialize the function pointer tables (v-tables).
1500 in->stream.common.get_sample_rate = in_get_sample_rate;
1501 in->stream.common.set_sample_rate = in_set_sample_rate;
1502 in->stream.common.get_buffer_size = in_get_buffer_size;
1503 in->stream.common.get_channels = in_get_channels;
1504 in->stream.common.get_format = in_get_format;
1505 in->stream.common.set_format = in_set_format;
1506 in->stream.common.standby = in_standby;
1507 in->stream.common.dump = in_dump;
1508 in->stream.common.set_parameters = in_set_parameters;
1509 in->stream.common.get_parameters = in_get_parameters;
1510 in->stream.common.add_audio_effect = in_add_audio_effect;
1511 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1512 in->stream.set_gain = in_set_gain;
1513 in->stream.read = in_read;
1514 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Mikhail Naganov8c97d242021-03-11 13:24:35 -08001515 in->stream.get_capture_position = in_get_capture_position;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001516
1517 in->dev = rsxadev;
1518#if LOG_STREAMS_TO_FILES
1519 in->log_fd = -1;
1520#endif
Stewart Miles3dd36f92014-05-01 09:03:27 -07001521 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001522
Stewart Miles568e66f2014-05-01 09:03:27 -07001523 // Initialize the input stream.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001524 in->read_counter_frames = 0;
Mikhail Naganov8c97d242021-03-11 13:24:35 -08001525 in->read_counter_frames_since_standby = 0;
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001526 in->input_standby = true;
1527 if (rsxadev->routes[route_idx].output != NULL) {
1528 in->output_standby_rec_thr = rsxadev->routes[route_idx].output->output_standby;
1529 } else {
1530 in->output_standby_rec_thr = true;
1531 }
1532
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001533 in->read_error_count = 0;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001534 // Initialize the pipe.
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001535 ALOGV("adev_open_input_stream(): about to create pipe");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001536 submix_audio_device_create_pipe_l(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1537 DEFAULT_PIPE_PERIOD_COUNT, in, NULL, address, route_idx);
Eric Laurent5b78d412019-03-01 18:39:26 -08001538
1539 sp <MonoPipe> sink = rsxadev->routes[route_idx].rsxSink;
1540 if (sink != NULL) {
1541 sink->shutdown(false);
1542 }
1543
Stewart Miles92854f52014-05-01 09:03:27 -07001544#if LOG_STREAMS_TO_FILES
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001545 if (in->log_fd >= 0) close(in->log_fd);
Stewart Miles92854f52014-05-01 09:03:27 -07001546 in->log_fd = open(LOG_STREAM_IN_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1547 LOG_STREAM_FILE_PERMISSIONS);
1548 ALOGE_IF(in->log_fd < 0, "adev_open_input_stream(): log file open failed %s",
1549 strerror(errno));
1550 ALOGV("adev_open_input_stream(): log_fd = %d", in->log_fd);
1551#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001552 // Return the input stream.
1553 *stream_in = &in->stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001554
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001555 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001556 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001557}
1558
1559static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -07001560 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001561{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001562 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
1563
Stewart Miles3dd36f92014-05-01 09:03:27 -07001564 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
Jean-Michel Trivi793a8542014-10-14 15:31:51 -07001565 ALOGD("adev_close_input_stream()");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001566 pthread_mutex_lock(&rsxadev->lock);
1567 submix_audio_device_destroy_pipe_l(rsxadev, in, NULL);
Stewart Miles92854f52014-05-01 09:03:27 -07001568#if LOG_STREAMS_TO_FILES
1569 if (in->log_fd >= 0) close(in->log_fd);
1570#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001571#if ENABLE_LEGACY_INPUT_OPEN
1572 if (in->ref_count == 0) free(in);
1573#else
1574 free(in);
1575#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001576
1577 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001578}
1579
1580static int adev_dump(const audio_hw_device_t *device, int fd)
1581{
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001582 const struct submix_audio_device * rsxadev = //audio_hw_device_get_submix_audio_device(device);
1583 reinterpret_cast<const struct submix_audio_device *>(
1584 reinterpret_cast<const uint8_t *>(device) -
1585 offsetof(struct submix_audio_device, device));
1586 char msg[100];
Mikhail Naganov80179932018-02-15 17:07:19 -08001587 int n = snprintf(msg, sizeof(msg), "\nReroute submix audio module:\n");
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001588 write(fd, &msg, n);
1589 for (int i=0 ; i < MAX_ROUTES ; i++) {
Mikhail Naganov1462c762019-07-26 09:22:34 -07001590 n = snprintf(msg, sizeof(msg), " route[%d], rate=%d addr=[%s]\n", i,
1591 rsxadev->routes[i].config.common.sample_rate,
1592 rsxadev->routes[i].address);
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001593 write(fd, &msg, n);
1594 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001595 return 0;
1596}
1597
1598static int adev_close(hw_device_t *device)
1599{
1600 ALOGI("adev_close()");
1601 free(device);
1602 return 0;
1603}
1604
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001605static int adev_open(const hw_module_t* module, const char* name,
1606 hw_device_t** device)
1607{
1608 ALOGI("adev_open(name=%s)", name);
1609 struct submix_audio_device *rsxadev;
1610
1611 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1612 return -EINVAL;
1613
1614 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
1615 if (!rsxadev)
1616 return -ENOMEM;
1617
1618 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -07001619 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001620 rsxadev->device.common.module = (struct hw_module_t *) module;
1621 rsxadev->device.common.close = adev_close;
1622
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001623 rsxadev->device.init_check = adev_init_check;
1624 rsxadev->device.set_voice_volume = adev_set_voice_volume;
1625 rsxadev->device.set_master_volume = adev_set_master_volume;
1626 rsxadev->device.get_master_volume = adev_get_master_volume;
1627 rsxadev->device.set_master_mute = adev_set_master_mute;
1628 rsxadev->device.get_master_mute = adev_get_master_mute;
1629 rsxadev->device.set_mode = adev_set_mode;
1630 rsxadev->device.set_mic_mute = adev_set_mic_mute;
1631 rsxadev->device.get_mic_mute = adev_get_mic_mute;
1632 rsxadev->device.set_parameters = adev_set_parameters;
1633 rsxadev->device.get_parameters = adev_get_parameters;
1634 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
1635 rsxadev->device.open_output_stream = adev_open_output_stream;
1636 rsxadev->device.close_output_stream = adev_close_output_stream;
1637 rsxadev->device.open_input_stream = adev_open_input_stream;
1638 rsxadev->device.close_input_stream = adev_close_input_stream;
1639 rsxadev->device.dump = adev_dump;
1640
Jean-Michel Trivib73bc862014-11-14 09:05:20 -08001641 for (int i=0 ; i < MAX_ROUTES ; i++) {
1642 memset(&rsxadev->routes[i], 0, sizeof(route_config));
1643 strcpy(rsxadev->routes[i].address, "");
1644 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001645
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001646 *device = &rsxadev->device.common;
1647
1648 return 0;
1649}
1650
1651static struct hw_module_methods_t hal_module_methods = {
1652 /* open */ adev_open,
1653};
1654
1655struct audio_module HAL_MODULE_INFO_SYM = {
1656 /* common */ {
1657 /* tag */ HARDWARE_MODULE_TAG,
1658 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
1659 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
1660 /* id */ AUDIO_HARDWARE_MODULE_ID,
1661 /* name */ "Wifi Display audio HAL",
1662 /* author */ "The Android Open Source Project",
1663 /* methods */ &hal_module_methods,
1664 /* dso */ NULL,
1665 /* reserved */ { 0 },
1666 },
1667};
1668
1669} //namespace android
1670
1671} //extern "C"