blob: 3073041aa88f4095758870c7a5c34ed962b4d74a [file] [log] [blame]
Simon Wilson19957a32012-04-06 16:17:12 -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
Paul McLean9ab869a2015-01-13 09:37:06 -080017#define LOG_TAG "modules.usbaudio.audio_hal"
Paul McLeancf5310a2018-08-22 14:33:12 -070018/* #define LOG_NDEBUG 0 */
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070021#include <inttypes.h>
Simon Wilson19957a32012-04-06 16:17:12 -070022#include <pthread.h>
23#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070024#include <stdlib.h>
jiabin400bbe02020-10-28 13:56:59 -070025#include <string.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070026#include <sys/time.h>
Tri Vo00a86732017-06-27 11:33:36 -070027#include <unistd.h>
Simon Wilson19957a32012-04-06 16:17:12 -070028
Mark Salyzyn88e458a2014-04-28 12:30:44 -070029#include <log/log.h>
Paul McLean6a75e4e2016-05-25 14:09:02 -060030#include <cutils/list.h>
Simon Wilson19957a32012-04-06 16:17:12 -070031#include <cutils/str_parms.h>
32#include <cutils/properties.h>
33
Simon Wilson19957a32012-04-06 16:17:12 -070034#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070035#include <hardware/audio_alsaops.h>
36#include <hardware/hardware.h>
37
38#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070039
40#include <tinyalsa/asoundlib.h>
41
Paul McLeanc2201152014-07-16 13:46:07 -070042#include <audio_utils/channels.h>
43
Paul McLeanc88e6ae2014-07-16 09:48:34 -070044#include "alsa_device_profile.h"
45#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080046#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070047
Paul McLean1d585cc2016-05-24 11:28:10 -060048/* Lock play & record samples rates at or above this threshold */
49#define RATELOCK_THRESHOLD 96000
50
Paul McLeancfdbd6b2018-07-27 11:16:02 -060051#define max(a, b) ((a) > (b) ? (a) : (b))
52#define min(a, b) ((a) < (b) ? (a) : (b))
53
Simon Wilson19957a32012-04-06 16:17:12 -070054struct audio_device {
55 struct audio_hw_device hw_device;
56
57 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080058
59 /* output */
Paul McLean6a75e4e2016-05-25 14:09:02 -060060 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080061
62 /* input */
Paul McLean6a75e4e2016-05-25 14:09:02 -060063 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080064
Paul McLean1d585cc2016-05-24 11:28:10 -060065 /* lock input & output sample rates */
66 /*FIXME - How do we address multiple output streams? */
Paul McLeancfdbd6b2018-07-27 11:16:02 -060067 uint32_t device_sample_rate; // this should be a rate that is common to both input & output
Paul McLean1d585cc2016-05-24 11:28:10 -060068
Eric Laurent253def92014-09-14 12:18:18 -070069 bool mic_muted;
70
Andy Hungb10ce6d2017-10-27 19:37:58 -070071 int32_t inputs_open; /* number of input streams currently open. */
jiabin400bbe02020-10-28 13:56:59 -070072
73 audio_patch_handle_t next_patch_handle; // Increase 1 when create audio patch
Simon Wilson19957a32012-04-06 16:17:12 -070074};
75
Paul McLean994ac072016-06-02 15:33:24 -060076struct stream_lock {
77 pthread_mutex_t lock; /* see note below on mutex acquisition order */
78 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
79};
80
jiabin8b265c92020-10-21 15:16:40 -070081struct alsa_device_info {
82 alsa_device_profile profile; /* The profile of the ALSA device */
83 alsa_device_proxy proxy; /* The state */
84 struct listnode list_node;
85};
86
Simon Wilson19957a32012-04-06 16:17:12 -070087struct stream_out {
88 struct audio_stream_out stream;
89
Paul McLeancf5310a2018-08-22 14:33:12 -070090 struct stream_lock lock;
Paul McLean994ac072016-06-02 15:33:24 -060091
Paul McLeaneedc92e2013-12-19 15:46:15 -080092 bool standby;
93
Paul McLean76dba682016-06-01 12:29:11 -060094 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070095
jiabin8b265c92020-10-21 15:16:40 -070096 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
Paul McLeaneedc92e2013-12-19 15:46:15 -080097
Andy Hung03576be2014-07-21 21:16:45 -070098 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
99 * This may differ from the device channel count when
100 * the device is not compatible with AudioFlinger
101 * capabilities, e.g. exposes too many channels or
102 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600103 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
104 * so the proxy doesn't have a channel_mask, but
105 * audio HALs need to talk about channel masks
106 * so expose the one calculated by
107 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700108
Paul McLean6a75e4e2016-05-25 14:09:02 -0600109 struct listnode list_node;
110
Paul McLeaneedc92e2013-12-19 15:46:15 -0800111 void * conversion_buffer; /* any conversions are put into here
112 * they could come from here too if
113 * there was a previous conversion */
114 size_t conversion_buffer_size; /* in bytes */
jiabin400bbe02020-10-28 13:56:59 -0700115
116 struct pcm_config config;
117
118 audio_io_handle_t handle; // Unique constant for a stream
119
120 audio_patch_handle_t patch_handle; // Patch handle for this stream
Paul McLeaneedc92e2013-12-19 15:46:15 -0800121};
122
Paul McLeaneedc92e2013-12-19 15:46:15 -0800123struct stream_in {
124 struct audio_stream_in stream;
125
Paul McLean994ac072016-06-02 15:33:24 -0600126 struct stream_lock lock;
127
Simon Wilson19957a32012-04-06 16:17:12 -0700128 bool standby;
129
Paul McLean76dba682016-06-01 12:29:11 -0600130 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800131
jiabin8b265c92020-10-21 15:16:40 -0700132 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700133
Paul McLean2cfd81b2014-09-15 12:32:23 -0700134 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
135 * This may differ from the device channel count when
136 * the device is not compatible with AudioFlinger
137 * capabilities, e.g. exposes too many channels or
138 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600139 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
140 * so the proxy doesn't have a channel_mask, but
141 * audio HALs need to talk about channel masks
142 * so expose the one calculated by
143 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700144
Paul McLean6a75e4e2016-05-25 14:09:02 -0600145 struct listnode list_node;
146
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700147 /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
Paul McLean30f41852014-04-16 15:44:20 -0700148 void * conversion_buffer; /* any conversions are put into here
149 * they could come from here too if
150 * there was a previous conversion */
151 size_t conversion_buffer_size; /* in bytes */
jiabin400bbe02020-10-28 13:56:59 -0700152
153 struct pcm_config config;
154
155 audio_io_handle_t handle; // Unique identifier for a stream
156
157 audio_patch_handle_t patch_handle; // Patch handle for this stream
Simon Wilson19957a32012-04-06 16:17:12 -0700158};
159
jiabin400bbe02020-10-28 13:56:59 -0700160// Map channel count to output channel mask
161static const audio_channel_mask_t OUT_CHANNEL_MASKS_MAP[] = {
162 AUDIO_CHANNEL_NONE, /* 0 */
163 AUDIO_CHANNEL_OUT_MONO, /* 1 */
164 AUDIO_CHANNEL_OUT_STEREO, /* 2 */
165 AUDIO_CHANNEL_OUT_2POINT1, /* 3 */
166 AUDIO_CHANNEL_OUT_QUAD, /* 4 */
167 AUDIO_CHANNEL_OUT_PENTA, /* 5 */
168 AUDIO_CHANNEL_OUT_5POINT1, /* 6 */
169 AUDIO_CHANNEL_OUT_6POINT1, /* 7 */
170 AUDIO_CHANNEL_OUT_7POINT1 /* 8 */
171 /* channel counts greater than this are not considered */
172};
173static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP);
174
175// Map channel count to input channel mask
176static const audio_channel_mask_t IN_CHANNEL_MASKS_MAP[] = {
177 AUDIO_CHANNEL_NONE, /* 0 */
178 AUDIO_CHANNEL_IN_MONO, /* 1 */
179 AUDIO_CHANNEL_IN_STEREO, /* 2 */
180 /* channel counts greater than this are not considered */
181};
182static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP);
183
184// Map channel count to index mask
185static const audio_channel_mask_t CHANNEL_INDEX_MASKS_MAP[] = {
186 AUDIO_CHANNEL_NONE, /* 0 */
187 AUDIO_CHANNEL_INDEX_MASK_1, /* 1 */
188 AUDIO_CHANNEL_INDEX_MASK_2, /* 2 */
189 AUDIO_CHANNEL_INDEX_MASK_3, /* 3 */
190 AUDIO_CHANNEL_INDEX_MASK_4, /* 4 */
191 AUDIO_CHANNEL_INDEX_MASK_5, /* 5 */
192 AUDIO_CHANNEL_INDEX_MASK_6, /* 6 */
193 AUDIO_CHANNEL_INDEX_MASK_7, /* 7 */
194 AUDIO_CHANNEL_INDEX_MASK_8 /* 8 */
195 /* channel counts greater than this are not considered */
196};
197static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP);
198
199
200
Paul McLean994ac072016-06-02 15:33:24 -0600201/*
202 * Locking Helpers
203 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800204/*
Eric Laurent70318092015-06-19 17:49:17 -0700205 * NOTE: when multiple mutexes have to be acquired, always take the
206 * stream_in or stream_out mutex first, followed by the audio_device mutex.
207 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
208 * higher priority playback or capture thread.
209 */
210
Paul McLean994ac072016-06-02 15:33:24 -0600211static void stream_lock_init(struct stream_lock *lock) {
212 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
213 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
214}
215
216static void stream_lock(struct stream_lock *lock) {
jiabin400bbe02020-10-28 13:56:59 -0700217 if (lock == NULL) {
218 return;
219 }
Paul McLean994ac072016-06-02 15:33:24 -0600220 pthread_mutex_lock(&lock->pre_lock);
221 pthread_mutex_lock(&lock->lock);
222 pthread_mutex_unlock(&lock->pre_lock);
223}
224
225static void stream_unlock(struct stream_lock *lock) {
226 pthread_mutex_unlock(&lock->lock);
227}
228
229static void device_lock(struct audio_device *adev) {
230 pthread_mutex_lock(&adev->lock);
231}
232
233static int device_try_lock(struct audio_device *adev) {
234 return pthread_mutex_trylock(&adev->lock);
235}
236
237static void device_unlock(struct audio_device *adev) {
238 pthread_mutex_unlock(&adev->lock);
239}
240
241/*
242 * streams list management
243 */
244static void adev_add_stream_to_list(
245 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
246 device_lock(adev);
247
248 list_add_tail(list, stream_node);
249
250 device_unlock(adev);
251}
252
jiabin400bbe02020-10-28 13:56:59 -0700253static struct stream_out* adev_get_stream_out_by_io_handle_l(
254 struct audio_device* adev, audio_io_handle_t handle) {
255 struct listnode *node;
256 list_for_each (node, &adev->output_stream_list) {
257 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
258 if (out->handle == handle) {
259 return out;
260 }
261 }
262 return NULL;
263}
Paul McLean994ac072016-06-02 15:33:24 -0600264
jiabin400bbe02020-10-28 13:56:59 -0700265static struct stream_in* adev_get_stream_in_by_io_handle_l(
266 struct audio_device* adev, audio_io_handle_t handle) {
267 struct listnode *node;
268 list_for_each (node, &adev->input_stream_list) {
269 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
270 if (in->handle == handle) {
271 return in;
272 }
273 }
274 return NULL;
275}
Paul McLean994ac072016-06-02 15:33:24 -0600276
jiabin400bbe02020-10-28 13:56:59 -0700277static struct stream_out* adev_get_stream_out_by_patch_handle_l(
278 struct audio_device* adev, audio_patch_handle_t patch_handle) {
279 struct listnode *node;
280 list_for_each (node, &adev->output_stream_list) {
281 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
282 if (out->patch_handle == patch_handle) {
283 return out;
284 }
285 }
286 return NULL;
287}
288
289static struct stream_in* adev_get_stream_in_by_patch_handle_l(
290 struct audio_device* adev, audio_patch_handle_t patch_handle) {
291 struct listnode *node;
292 list_for_each (node, &adev->input_stream_list) {
293 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
294 if (in->patch_handle == patch_handle) {
295 return in;
296 }
297 }
298 return NULL;
Paul McLean994ac072016-06-02 15:33:24 -0600299}
300
Eric Laurent70318092015-06-19 17:49:17 -0700301/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700302 * Extract the card and device numbers from the supplied key/value pairs.
303 * kvpairs A null-terminated string containing the key/value pairs or card and device.
304 * i.e. "card=1;device=42"
305 * card A pointer to a variable to receive the parsed-out card number.
306 * device A pointer to a variable to receive the parsed-out device number.
307 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
308 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800309 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700310 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800311static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700312{
313 struct str_parms * parms = str_parms_create_str(kvpairs);
314 char value[32];
315 int param_val;
316
317 // initialize to "undefined" state.
318 *card = -1;
319 *device = -1;
320
321 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
322 if (param_val >= 0) {
323 *card = atoi(value);
324 }
325
326 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
327 if (param_val >= 0) {
328 *device = atoi(value);
329 }
330
331 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800332
333 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700334}
335
Andy Hung4e6a1c02017-10-27 20:31:46 -0700336static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800337{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700338 if (profile->card < 0 || profile->device < 0) {
339 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800340 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700341
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700342 struct str_parms *query = str_parms_create_str(keys);
343 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700344
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700345 /* These keys are from hardware/libhardware/include/audio.h */
346 /* supported sample rates */
347 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
348 char* rates_list = profile_get_sample_rate_strs(profile);
349 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
350 rates_list);
351 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700352 }
353
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700354 /* supported channel counts */
355 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
356 char* channels_list = profile_get_channel_count_strs(profile);
357 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
358 channels_list);
359 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700360 }
361
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700362 /* supported sample formats */
363 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
364 char * format_params = profile_get_format_strs(profile);
365 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
366 format_params);
367 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700368 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700369 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700370
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700371 char* result_str = str_parms_to_str(result);
372 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700373
Paul McLean65ec72b2015-02-18 09:13:24 -0800374 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700375
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700376 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800377}
378
jiabin400bbe02020-10-28 13:56:59 -0700379static audio_format_t audio_format_from(enum pcm_format format)
380{
381 switch (format) {
382 case PCM_FORMAT_S16_LE:
383 return AUDIO_FORMAT_PCM_16_BIT;
384 case PCM_FORMAT_S32_LE:
385 return AUDIO_FORMAT_PCM_32_BIT;
386 case PCM_FORMAT_S8:
387 return AUDIO_FORMAT_PCM_8_BIT;
388 case PCM_FORMAT_S24_LE:
389 return AUDIO_FORMAT_PCM_8_24_BIT;
390 case PCM_FORMAT_S24_3LE:
391 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
392 default:
393 return AUDIO_FORMAT_INVALID;
394 }
395}
396
jiabina635fcf2021-01-11 11:34:50 -0800397static unsigned int populate_channel_mask_from_profile(const alsa_device_profile* profile,
398 bool is_output,
399 audio_channel_mask_t channel_masks[])
400{
401 unsigned int num_channel_masks = 0;
402 const audio_channel_mask_t* channel_masks_map =
403 is_output ? OUT_CHANNEL_MASKS_MAP : IN_CHANNEL_MASKS_MAP;
404 const int channel_masks_size = is_output ? OUT_CHANNEL_MASKS_SIZE : IN_CHANNEL_MASKS_SIZE;
405 unsigned int channel_count = 0;
406 for (size_t i = 0; i < min(channel_masks_size, AUDIO_PORT_MAX_CHANNEL_MASKS) &&
407 (channel_count = profile->channel_counts[i]) != 0 &&
408 num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS; ++i) {
409 if (channel_count < channel_masks_size &&
410 channel_masks_map[channel_count] != AUDIO_CHANNEL_NONE) {
411 channel_masks[num_channel_masks++] = channel_masks_map[channel_count];
412 if (num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
413 break;
414 }
415 }
416 if (channel_count < CHANNEL_INDEX_MASKS_SIZE &&
417 CHANNEL_INDEX_MASKS_MAP[channel_count] != AUDIO_CHANNEL_NONE) {
418 channel_masks[num_channel_masks++] = CHANNEL_INDEX_MASKS_MAP[channel_count];
419 }
420 }
421 return num_channel_masks;
422}
423
424static unsigned int populate_sample_rates_from_profile(const alsa_device_profile* profile,
425 unsigned int sample_rates[])
426{
427 unsigned int num_sample_rates = 0;
428 for (;num_sample_rates < min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
429 profile->sample_rates[num_sample_rates] != 0; num_sample_rates++) {
430 sample_rates[num_sample_rates] = profile->sample_rates[num_sample_rates];
431 }
432 return num_sample_rates;
433}
434
Paul McLeaneedc92e2013-12-19 15:46:15 -0800435/*
436 * HAl Functions
437 */
Simon Wilson19957a32012-04-06 16:17:12 -0700438/**
439 * NOTE: when multiple mutexes have to be acquired, always respect the
440 * following order: hw device > out stream
441 */
442
jiabin8b265c92020-10-21 15:16:40 -0700443static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
444 if (list_empty(alsa_devices)) {
445 return NULL;
446 }
447 return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
448}
449
jiabin400bbe02020-10-28 13:56:59 -0700450/**
451 * Must be called with holding the stream's lock.
452 */
453static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
454{
455 if (!*standby) {
456 struct listnode *node;
457 list_for_each (node, alsa_devices) {
458 struct alsa_device_info *device_info =
459 node_to_item(node, struct alsa_device_info, list_node);
460 proxy_close(&device_info->proxy);
461 }
462 *standby = true;
463 }
464}
465
466static void stream_clear_devices(struct listnode *alsa_devices)
467{
468 struct listnode *node, *temp;
469 struct alsa_device_info *device_info = NULL;
470 list_for_each_safe (node, temp, alsa_devices) {
471 device_info = node_to_item(node, struct alsa_device_info, list_node);
472 if (device_info != NULL) {
473 list_remove(&device_info->list_node);
474 free(device_info);
475 }
476 }
477}
478
479static int stream_set_new_devices(struct pcm_config *config,
480 struct listnode *alsa_devices,
481 unsigned int num_devices,
482 const int cards[],
483 const int devices[],
484 int direction)
485{
486 int status = 0;
487 stream_clear_devices(alsa_devices);
488
489 for (unsigned int i = 0; i < num_devices; ++i) {
490 struct alsa_device_info *device_info =
491 (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
492 profile_init(&device_info->profile, direction);
493 device_info->profile.card = cards[i];
494 device_info->profile.device = devices[i];
495 status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
496 if (status != 0) {
497 ALOGE("%s failed to read device info card=%d;device=%d",
498 __func__, cards[i], devices[i]);
499 goto exit;
500 }
501 status = proxy_prepare(&device_info->proxy, &device_info->profile, config);
502 if (status != 0) {
503 ALOGE("%s failed to prepare device card=%d;device=%d",
504 __func__, cards[i], devices[i]);
505 goto exit;
506 }
507 list_add_tail(alsa_devices, &device_info->list_node);
508 }
509
510exit:
511 if (status != 0) {
512 stream_clear_devices(alsa_devices);
513 }
514 return status;
515}
516
jiabin8b265c92020-10-21 15:16:40 -0700517static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
518 struct listnode *node;
519 size_t i = 0;
520 list_for_each(node, alsa_devices) {
521 struct alsa_device_info *device_info =
522 node_to_item(node, struct alsa_device_info, list_node);
jiabin0269a9d2021-06-02 20:27:38 +0000523 const char* direction = device_info->profile.direction == PCM_OUT ? "Output" : "Input";
524 dprintf(fd, "%s Profile %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700525 profile_dump(&device_info->profile, fd);
526
jiabin0269a9d2021-06-02 20:27:38 +0000527 dprintf(fd, "%s Proxy %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700528 proxy_dump(&device_info->proxy, fd);
529 }
530}
531
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700532/*
533 * OUT functions
534 */
Simon Wilson19957a32012-04-06 16:17:12 -0700535static uint32_t out_get_sample_rate(const struct audio_stream *stream)
536{
jiabin8b265c92020-10-21 15:16:40 -0700537 struct alsa_device_info *device_info = stream_get_first_alsa_device(
538 &((struct stream_out*)stream)->alsa_devices);
539 if (device_info == NULL) {
540 ALOGW("%s device info is null", __func__);
541 return 0;
542 }
543 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700544 ALOGV("out_get_sample_rate() = %d", rate);
545 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700546}
547
548static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
549{
550 return 0;
551}
552
553static size_t out_get_buffer_size(const struct audio_stream *stream)
554{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700555 const struct stream_out* out = (const struct stream_out*)stream;
jiabin8b265c92020-10-21 15:16:40 -0700556 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
557 if (device_info == NULL) {
558 ALOGW("%s device info is null", __func__);
559 return 0;
560 }
561 return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
Simon Wilson19957a32012-04-06 16:17:12 -0700562}
563
564static uint32_t out_get_channels(const struct audio_stream *stream)
565{
Andy Hung03576be2014-07-21 21:16:45 -0700566 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700567 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700568}
569
570static audio_format_t out_get_format(const struct audio_stream *stream)
571{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700572 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
573 * Relies on the framework to provide data in the specified format.
574 * This could change in the future.
575 */
jiabin8b265c92020-10-21 15:16:40 -0700576 struct alsa_device_info *device_info = stream_get_first_alsa_device(
577 &((struct stream_out*)stream)->alsa_devices);
578 if (device_info == NULL) {
579 ALOGW("%s device info is null", __func__);
580 return AUDIO_FORMAT_DEFAULT;
581 }
582 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700583 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700584}
585
586static int out_set_format(struct audio_stream *stream, audio_format_t format)
587{
588 return 0;
589}
590
591static int out_standby(struct audio_stream *stream)
592{
593 struct stream_out *out = (struct stream_out *)stream;
594
Paul McLean994ac072016-06-02 15:33:24 -0600595 stream_lock(&out->lock);
jiabin400bbe02020-10-28 13:56:59 -0700596 device_lock(out->adev);
597 stream_standby_l(&out->alsa_devices, &out->standby);
598 device_unlock(out->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600599 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700600 return 0;
601}
602
Paul McLean6a75e4e2016-05-25 14:09:02 -0600603static int out_dump(const struct audio_stream *stream, int fd) {
604 const struct stream_out* out_stream = (const struct stream_out*) stream;
605
606 if (out_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -0700607 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600608 }
609
Simon Wilson19957a32012-04-06 16:17:12 -0700610 return 0;
611}
612
jiabin400bbe02020-10-28 13:56:59 -0700613static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -0700614{
Paul McLean65ec72b2015-02-18 09:13:24 -0800615 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800616
jiabin400bbe02020-10-28 13:56:59 -0700617 // The set parameters here only matters when the routing devices are changed.
618 // When the device version is not less than 3.0, the framework will use create
619 // audio patch API instead of set parameters to chanage audio routing.
620 return 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700621}
622
Paul McLeanf62d75e2014-07-11 15:14:19 -0700623static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
624{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700625 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600626 stream_lock(&out->lock);
jiabin8b265c92020-10-21 15:16:40 -0700627 struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
628 char *params_str = NULL;
629 if (device_info != NULL) {
630 params_str = device_get_parameters(&device_info->profile, keys);
631 }
Paul McLean994ac072016-06-02 15:33:24 -0600632 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700633 return params_str;
634}
635
Simon Wilson19957a32012-04-06 16:17:12 -0700636static uint32_t out_get_latency(const struct audio_stream_out *stream)
637{
jiabin8b265c92020-10-21 15:16:40 -0700638 struct alsa_device_info *device_info = stream_get_first_alsa_device(
639 &((struct stream_out*)stream)->alsa_devices);
640 if (device_info == NULL) {
641 ALOGW("%s device info is null", __func__);
642 return 0;
643 }
644 return proxy_get_latency(&device_info->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700645}
646
Paul McLean30f41852014-04-16 15:44:20 -0700647static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700648{
649 return -ENOSYS;
650}
651
Paul McLeaneedc92e2013-12-19 15:46:15 -0800652/* must be called with hw device and output stream mutexes locked */
653static int start_output_stream(struct stream_out *out)
654{
jiabin8b265c92020-10-21 15:16:40 -0700655 int status = 0;
656 struct listnode *node;
657 list_for_each(node, &out->alsa_devices) {
658 struct alsa_device_info *device_info =
659 node_to_item(node, struct alsa_device_info, list_node);
660 ALOGV("start_output_stream(card:%d device:%d)",
661 device_info->profile.card, device_info->profile.device);
662 status = proxy_open(&device_info->proxy);
663 if (status != 0) {
664 ALOGE("%s failed to open device(card: %d device: %d)",
665 __func__, device_info->profile.card, device_info->profile.device);
666 goto exit;
667 }
668 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800669
jiabin8b265c92020-10-21 15:16:40 -0700670exit:
671 if (status != 0) {
672 list_for_each(node, &out->alsa_devices) {
673 struct alsa_device_info *device_info =
674 node_to_item(node, struct alsa_device_info, list_node);
675 proxy_close(&device_info->proxy);
676 }
677
678 }
679 return status;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800680}
681
682static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700683{
684 int ret;
685 struct stream_out *out = (struct stream_out *)stream;
686
Paul McLean994ac072016-06-02 15:33:24 -0600687 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700688 if (out->standby) {
689 ret = start_output_stream(out);
690 if (ret != 0) {
691 goto err;
692 }
693 out->standby = false;
694 }
Eric Laurent05333d42014-08-04 20:29:17 -0700695
jiabin8b265c92020-10-21 15:16:40 -0700696 struct listnode* node;
697 list_for_each(node, &out->alsa_devices) {
698 struct alsa_device_info* device_info =
699 node_to_item(node, struct alsa_device_info, list_node);
700 alsa_device_proxy* proxy = &device_info->proxy;
701 const void * write_buff = buffer;
702 int num_write_buff_bytes = bytes;
703 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
704 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
705 if (num_device_channels != num_req_channels) {
706 /* allocate buffer */
707 const size_t required_conversion_buffer_size =
708 bytes * num_device_channels / num_req_channels;
709 if (required_conversion_buffer_size > out->conversion_buffer_size) {
710 out->conversion_buffer_size = required_conversion_buffer_size;
711 out->conversion_buffer = realloc(out->conversion_buffer,
712 out->conversion_buffer_size);
713 }
714 /* convert data */
715 const audio_format_t audio_format = out_get_format(&(out->stream.common));
716 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
717 num_write_buff_bytes =
718 adjust_channels(write_buff, num_req_channels,
719 out->conversion_buffer, num_device_channels,
720 sample_size_in_bytes, num_write_buff_bytes);
721 write_buff = out->conversion_buffer;
Andy Hung03576be2014-07-21 21:16:45 -0700722 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800723
jiabin8b265c92020-10-21 15:16:40 -0700724 if (write_buff != NULL && num_write_buff_bytes != 0) {
725 proxy_write(proxy, write_buff, num_write_buff_bytes);
726 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800727 }
Simon Wilson19957a32012-04-06 16:17:12 -0700728
Paul McLean994ac072016-06-02 15:33:24 -0600729 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700730
731 return bytes;
732
733err:
Paul McLean994ac072016-06-02 15:33:24 -0600734 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700735 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700736 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700737 out_get_sample_rate(&stream->common));
738 }
739
740 return bytes;
741}
742
Paul McLean30f41852014-04-16 15:44:20 -0700743static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700744{
745 return -EINVAL;
746}
747
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700748static int out_get_presentation_position(const struct audio_stream_out *stream,
749 uint64_t *frames, struct timespec *timestamp)
750{
Andy Hungc9515ce2015-08-04 15:05:19 -0700751 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600752 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700753
jiabin8b265c92020-10-21 15:16:40 -0700754 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
755 const int ret = device_info == NULL ? -ENODEV :
756 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
Paul McLean994ac072016-06-02 15:33:24 -0600757 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700758 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700759}
760
Simon Wilson19957a32012-04-06 16:17:12 -0700761static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
762{
763 return 0;
764}
765
766static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
767{
768 return 0;
769}
770
Paul McLean30f41852014-04-16 15:44:20 -0700771static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700772{
773 return -EINVAL;
774}
775
Paul McLean76dba682016-06-01 12:29:11 -0600776static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700777 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600778 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700779 audio_output_flags_t flags,
780 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700781 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700782 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700783{
Paul McLean76dba682016-06-01 12:29:11 -0600784 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
785 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800786
Simon Wilson19957a32012-04-06 16:17:12 -0700787 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700788
789 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600790 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700791 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600792 }
Simon Wilson19957a32012-04-06 16:17:12 -0700793
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700794 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700795 out->stream.common.get_sample_rate = out_get_sample_rate;
796 out->stream.common.set_sample_rate = out_set_sample_rate;
797 out->stream.common.get_buffer_size = out_get_buffer_size;
798 out->stream.common.get_channels = out_get_channels;
799 out->stream.common.get_format = out_get_format;
800 out->stream.common.set_format = out_set_format;
801 out->stream.common.standby = out_standby;
802 out->stream.common.dump = out_dump;
803 out->stream.common.set_parameters = out_set_parameters;
804 out->stream.common.get_parameters = out_get_parameters;
805 out->stream.common.add_audio_effect = out_add_audio_effect;
806 out->stream.common.remove_audio_effect = out_remove_audio_effect;
807 out->stream.get_latency = out_get_latency;
808 out->stream.set_volume = out_set_volume;
809 out->stream.write = out_write;
810 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700811 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700812 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
813
jiabin400bbe02020-10-28 13:56:59 -0700814 out->handle = handle;
815
Paul McLean994ac072016-06-02 15:33:24 -0600816 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700817
Paul McLean76dba682016-06-01 12:29:11 -0600818 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700819
jiabin8b265c92020-10-21 15:16:40 -0700820 list_init(&out->alsa_devices);
821 struct alsa_device_info *device_info =
822 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
823 profile_init(&device_info->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700824
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700825 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700826 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800827
Paul McLean0f1753e2014-12-02 12:36:45 -0700828 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700829 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700830
jiabin8b265c92020-10-21 15:16:40 -0700831 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700832
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700833 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800834
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700835 /* Rate */
836 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700837 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
838 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700839 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800840 } else {
jiabin8b265c92020-10-21 15:16:40 -0700841 proxy_config.rate = config->sample_rate =
842 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700843 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800844 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800845
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600846 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -0700847 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600848 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600849 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600850
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700851 /* Format */
852 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -0700853 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700854 config->format = audio_format_from_pcm_format(proxy_config.format);
855 } else {
856 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -0700857 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700858 proxy_config.format = fmt;
859 } else {
jiabin8b265c92020-10-21 15:16:40 -0700860 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700861 config->format = audio_format_from_pcm_format(proxy_config.format);
862 ret = -EINVAL;
863 }
864 }
865
866 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600867 bool calc_mask = false;
868 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
869 /* query case */
jiabin8b265c92020-10-21 15:16:40 -0700870 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -0600871 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700872 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600873 /* explicit case */
874 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700875 }
Paul McLean698dbd72015-11-03 12:24:30 -0800876
Paul McLean64345f82016-06-06 13:26:05 -0600877 /* The Framework is currently limited to no more than this number of channels */
878 if (out->hal_channel_count > FCC_8) {
879 out->hal_channel_count = FCC_8;
880 calc_mask = true;
881 }
882
883 if (calc_mask) {
884 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -0700885 * or the specified mask isn't valid for this device, or is more than the FW can handle */
Paul McLean64345f82016-06-06 13:26:05 -0600886 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -0700887 /* position mask for mono and stereo*/
888 ? audio_channel_out_mask_from_count(out->hal_channel_count)
889 /* otherwise indexed */
890 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
Paul McLean64345f82016-06-06 13:26:05 -0600891 }
892
Andy Hung182ddc72015-05-05 23:37:30 -0700893 out->hal_channel_mask = config->channel_mask;
894
Paul McLean64345f82016-06-06 13:26:05 -0600895 // Validate the "logical" channel count against support in the "actual" profile.
896 // if they differ, choose the "actual" number of channels *closest* to the "logical".
897 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -0700898 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -0700899 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
900 proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config);
jiabin400bbe02020-10-28 13:56:59 -0700901 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -0700902
903 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700904
Paul McLean96c4d302017-12-05 09:50:26 -0800905 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
906 * So clear any errors that may have occurred above.
907 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700908 ret = 0;
909
Paul McLeaneedc92e2013-12-19 15:46:15 -0800910 out->conversion_buffer = NULL;
911 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700912
913 out->standby = true;
914
Paul McLean6a75e4e2016-05-25 14:09:02 -0600915 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600916 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600917
Simon Wilson19957a32012-04-06 16:17:12 -0700918 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700919
920 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700921}
922
Paul McLean76dba682016-06-01 12:29:11 -0600923static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700924 struct audio_stream_out *stream)
925{
926 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -0600927
jiabin400bbe02020-10-28 13:56:59 -0700928 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700929 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -0700930 stream_standby_l(&out->alsa_devices, &out->standby);
931 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800932
933 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700934
Paul McLeaneedc92e2013-12-19 15:46:15 -0800935 out->conversion_buffer = NULL;
936 out->conversion_buffer_size = 0;
937
Paul McLean994ac072016-06-02 15:33:24 -0600938 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700939 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -0600940 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600941 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700942 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -0600943
Simon Wilson19957a32012-04-06 16:17:12 -0700944 free(stream);
945}
946
Paul McLean76dba682016-06-01 12:29:11 -0600947static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700948 const struct audio_config *config)
949{
950 /* TODO This needs to be calculated based on format/channels/rate */
951 return 320;
952}
953
954/*
955 * IN functions
956 */
957static uint32_t in_get_sample_rate(const struct audio_stream *stream)
958{
jiabin8b265c92020-10-21 15:16:40 -0700959 struct alsa_device_info *device_info = stream_get_first_alsa_device(
960 &((const struct stream_in *)stream)->alsa_devices);
961 if (device_info == NULL) {
962 ALOGW("%s device info is null", __func__);
963 return 0;
964 }
965 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700966 ALOGV("in_get_sample_rate() = %d", rate);
967 return rate;
968}
969
970static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
971{
972 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
973 return -ENOSYS;
974}
975
976static size_t in_get_buffer_size(const struct audio_stream *stream)
977{
978 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -0700979 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
980 if (device_info == NULL) {
981 ALOGW("%s device info is null", __func__);
982 return 0;
983 }
984 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700985}
986
987static uint32_t in_get_channels(const struct audio_stream *stream)
988{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700989 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700990 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700991}
992
993static audio_format_t in_get_format(const struct audio_stream *stream)
994{
jiabin8b265c92020-10-21 15:16:40 -0700995 struct alsa_device_info *device_info = stream_get_first_alsa_device(
996 &((const struct stream_in *)stream)->alsa_devices);
997 if (device_info == NULL) {
998 ALOGW("%s device info is null", __func__);
999 return AUDIO_FORMAT_DEFAULT;
1000 }
1001 alsa_device_proxy *proxy = &device_info->proxy;
Andy Hung780f1f82015-04-22 22:14:39 -07001002 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1003 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001004}
1005
1006static int in_set_format(struct audio_stream *stream, audio_format_t format)
1007{
1008 ALOGV("in_set_format(%d) - NOPE", format);
1009
1010 return -ENOSYS;
1011}
1012
1013static int in_standby(struct audio_stream *stream)
1014{
1015 struct stream_in *in = (struct stream_in *)stream;
1016
Paul McLean994ac072016-06-02 15:33:24 -06001017 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -07001018 device_lock(in->adev);
1019 stream_standby_l(&in->alsa_devices, &in->standby);
1020 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -06001021 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001022 return 0;
1023}
1024
1025static int in_dump(const struct audio_stream *stream, int fd)
1026{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001027 const struct stream_in* in_stream = (const struct stream_in*)stream;
1028 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -07001029 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001030 }
1031
1032 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001033}
1034
1035static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1036{
Paul McLean65ec72b2015-02-18 09:13:24 -08001037 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001038
jiabin400bbe02020-10-28 13:56:59 -07001039 // The set parameters here only matters when the routing devices are changed.
1040 // When the device version higher than 3.0, the framework will use create_audio_patch
1041 // API instead of set_parameters to change audio routing.
1042 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001043}
1044
1045static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1046{
1047 struct stream_in *in = (struct stream_in *)stream;
1048
Paul McLean994ac072016-06-02 15:33:24 -06001049 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001050 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1051 char *params_str = NULL;
1052 if (device_info != NULL) {
1053 params_str = device_get_parameters(&device_info->profile, keys);
1054 }
Paul McLean994ac072016-06-02 15:33:24 -06001055 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001056
1057 return params_str;
1058}
1059
1060static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1061{
1062 return 0;
1063}
1064
1065static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1066{
1067 return 0;
1068}
1069
1070static int in_set_gain(struct audio_stream_in *stream, float gain)
1071{
1072 return 0;
1073}
1074
1075/* must be called with hw device and output stream mutexes locked */
1076static int start_input_stream(struct stream_in *in)
1077{
jiabin8b265c92020-10-21 15:16:40 -07001078 // Only care about the first device as only one input device is allowed.
1079 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1080 if (device_info == NULL) {
1081 return -ENODEV;
1082 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001083
jiabin8b265c92020-10-21 15:16:40 -07001084 ALOGV("start_input_stream(card:%d device:%d)",
1085 device_info->profile.card, device_info->profile.device);
1086 return proxy_open(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001087}
1088
1089/* TODO mutex stuff here (see out_write) */
1090static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1091{
1092 size_t num_read_buff_bytes = 0;
1093 void * read_buff = buffer;
1094 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001095 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001096
1097 struct stream_in * in = (struct stream_in *)stream;
1098
Paul McLean994ac072016-06-02 15:33:24 -06001099 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001100 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001101 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001102 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001103 goto err;
1104 }
1105 in->standby = false;
1106 }
1107
jiabin8b265c92020-10-21 15:16:40 -07001108 // Only care about the first device as only one input device is allowed.
1109 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1110 if (device_info == NULL) {
1111 return 0;
1112 }
1113
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001114 /*
1115 * OK, we need to figure out how much data to read to be able to output the requested
1116 * number of bytes in the HAL format (16-bit, stereo).
1117 */
1118 num_read_buff_bytes = bytes;
jiabin8b265c92020-10-21 15:16:40 -07001119 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001120 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001121
1122 if (num_device_channels != num_req_channels) {
1123 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1124 }
1125
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001126 /* Setup/Realloc the conversion buffer (if necessary). */
1127 if (num_read_buff_bytes != bytes) {
1128 if (num_read_buff_bytes > in->conversion_buffer_size) {
1129 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1130 (and do these conversions themselves) */
1131 in->conversion_buffer_size = num_read_buff_bytes;
1132 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1133 }
1134 read_buff = in->conversion_buffer;
1135 }
1136
jiabin8b265c92020-10-21 15:16:40 -07001137 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001138 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001139 if (num_device_channels != num_req_channels) {
1140 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1141
1142 out_buff = buffer;
1143 /* Num Channels conversion */
1144 if (num_device_channels != num_req_channels) {
1145 audio_format_t audio_format = in_get_format(&(in->stream.common));
1146 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1147
1148 num_read_buff_bytes =
1149 adjust_channels(read_buff, num_device_channels,
1150 out_buff, num_req_channels,
1151 sample_size_in_bytes, num_read_buff_bytes);
1152 }
1153 }
Eric Laurent253def92014-09-14 12:18:18 -07001154
Paul McLean76dba682016-06-01 12:29:11 -06001155 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1156 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -07001157 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301158 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001159 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001160 }
1161
1162err:
Paul McLean994ac072016-06-02 15:33:24 -06001163 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001164 return num_read_buff_bytes;
1165}
1166
1167static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1168{
1169 return 0;
1170}
1171
Andy Hungfa6b4a62018-06-04 19:14:22 -07001172static int in_get_capture_position(const struct audio_stream_in *stream,
1173 int64_t *frames, int64_t *time)
1174{
1175 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1176 stream_lock(&in->lock);
1177
jiabin8b265c92020-10-21 15:16:40 -07001178 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1179
1180 const int ret = device_info == NULL ? -ENODEV
1181 : proxy_get_capture_position(&device_info->proxy, frames, time);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001182
1183 stream_unlock(&in->lock);
1184 return ret;
1185}
1186
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001187static int in_get_active_microphones(const struct audio_stream_in *stream,
1188 struct audio_microphone_characteristic_t *mic_array,
1189 size_t *mic_count) {
1190 (void)stream;
1191 (void)mic_array;
1192 (void)mic_count;
1193
1194 return -ENOSYS;
1195}
1196
1197static int in_set_microphone_direction(const struct audio_stream_in *stream,
1198 audio_microphone_direction_t dir) {
1199 (void)stream;
1200 (void)dir;
1201 ALOGV("---- in_set_microphone_direction()");
1202 return -ENOSYS;
1203}
1204
1205static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1206 (void)zoom;
1207 ALOGV("---- in_set_microphone_field_dimension()");
1208 return -ENOSYS;
1209}
1210
Paul McLean76dba682016-06-01 12:29:11 -06001211static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001212 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001213 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001214 struct audio_config *config,
1215 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001216 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001217 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001218 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001219{
Paul McLean1d585cc2016-05-24 11:28:10 -06001220 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001221 config->sample_rate, config->channel_mask, config->format);
1222
Andy Hungb10ce6d2017-10-27 19:37:58 -07001223 /* Pull out the card/device pair */
1224 int32_t card, device;
1225 if (!parse_card_device_params(address, &card, &device)) {
1226 ALOGW("%s fail - invalid address %s", __func__, address);
1227 *stream_in = NULL;
1228 return -EINVAL;
1229 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001230
Andy Hungb10ce6d2017-10-27 19:37:58 -07001231 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001232 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001233 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001234 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001235 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001236
1237 /* setup function pointers */
1238 in->stream.common.get_sample_rate = in_get_sample_rate;
1239 in->stream.common.set_sample_rate = in_set_sample_rate;
1240 in->stream.common.get_buffer_size = in_get_buffer_size;
1241 in->stream.common.get_channels = in_get_channels;
1242 in->stream.common.get_format = in_get_format;
1243 in->stream.common.set_format = in_set_format;
1244 in->stream.common.standby = in_standby;
1245 in->stream.common.dump = in_dump;
1246 in->stream.common.set_parameters = in_set_parameters;
1247 in->stream.common.get_parameters = in_get_parameters;
1248 in->stream.common.add_audio_effect = in_add_audio_effect;
1249 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1250
1251 in->stream.set_gain = in_set_gain;
1252 in->stream.read = in_read;
1253 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001254 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001255
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001256 in->stream.get_active_microphones = in_get_active_microphones;
1257 in->stream.set_microphone_direction = in_set_microphone_direction;
1258 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1259
jiabin400bbe02020-10-28 13:56:59 -07001260 in->handle = handle;
1261
Paul McLean994ac072016-06-02 15:33:24 -06001262 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001263
Paul McLean76dba682016-06-01 12:29:11 -06001264 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001265
jiabin8b265c92020-10-21 15:16:40 -07001266 list_init(&in->alsa_devices);
1267 struct alsa_device_info *device_info =
1268 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1269 profile_init(&device_info->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001270
jiabin400bbe02020-10-28 13:56:59 -07001271 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001272
Andy Hungb10ce6d2017-10-27 19:37:58 -07001273 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001274 device_lock(in->adev);
1275 int num_open_inputs = in->adev->inputs_open;
1276 device_unlock(in->adev);
1277
Andy Hungb10ce6d2017-10-27 19:37:58 -07001278 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001279 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001280 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001281 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1282 __func__, card, device);
1283 ret = -EINVAL;
1284 }
1285 } else {
1286 /* Read input profile only if necessary */
jiabin8b265c92020-10-21 15:16:40 -07001287 device_info->profile.card = card;
1288 device_info->profile.device = device;
1289 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001290 ALOGW("%s fail - cannot read profile", __func__);
1291 ret = -EINVAL;
1292 }
1293 }
1294 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001295 free(in);
1296 *stream_in = NULL;
1297 return ret;
1298 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001299
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001300 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001301 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001302 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001303 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001304 }
1305
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001306 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001307 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001308 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001309 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001310 if (highest_rate == 0) {
1311 ret = -EINVAL; /* error with device */
1312 } else {
jiabin400bbe02020-10-28 13:56:59 -07001313 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001314 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001315 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001316 /* Changing the requested rate */
1317 ret = -EINVAL;
1318 } else {
1319 /* Everything AOK! */
1320 ret = 0;
1321 }
1322 }
1323 }
jiabin8b265c92020-10-21 15:16:40 -07001324 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001325 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001326 } else {
jiabin400bbe02020-10-28 13:56:59 -07001327 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001328 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001329 ret = -EINVAL;
1330 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001331
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001332 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001333 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001334 in->config.format = profile_get_default_format(&device_info->profile);
1335 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001336 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001337 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001338 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001339 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001340 } else {
jiabin400bbe02020-10-28 13:56:59 -07001341 in->config.format = profile_get_default_format(&device_info->profile);
1342 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001343 ret = -EINVAL;
1344 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001345 }
1346
Paul McLean2cfd81b2014-09-15 12:32:23 -07001347 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001348 bool calc_mask = false;
1349 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1350 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001351 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001352 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001353 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001354 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001355 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1356 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001357
Paul McLean64345f82016-06-06 13:26:05 -06001358 /* The Framework is currently limited to no more than this number of channels */
1359 if (in->hal_channel_count > FCC_8) {
1360 in->hal_channel_count = FCC_8;
1361 calc_mask = true;
1362 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001363
Paul McLean64345f82016-06-06 13:26:05 -06001364 if (calc_mask) {
1365 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001366 * or the specified mask isn't valid for this device, or is more than the FW can handle */
Paul McLean64345f82016-06-06 13:26:05 -06001367 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1368 /* position mask for mono & stereo */
1369 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1370 /* otherwise indexed */
1371 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001372
Paul McLean64345f82016-06-06 13:26:05 -06001373 // if we change the mask...
1374 if (in->hal_channel_mask != config->channel_mask &&
1375 config->channel_mask != AUDIO_CHANNEL_NONE) {
1376 config->channel_mask = in->hal_channel_mask;
1377 ret = -EINVAL;
1378 }
1379 } else {
1380 in->hal_channel_mask = config->channel_mask;
1381 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001382
Paul McLean64345f82016-06-06 13:26:05 -06001383 if (ret == 0) {
1384 // Validate the "logical" channel count against support in the "actual" profile.
1385 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1386 // and store THAT in proxy_config.channels
jiabin400bbe02020-10-28 13:56:59 -07001387 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001388 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabin400bbe02020-10-28 13:56:59 -07001389 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config);
Eric Laurent981f7742017-05-03 12:44:26 -07001390 if (ret == 0) {
1391 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001392
Eric Laurent981f7742017-05-03 12:44:26 -07001393 in->conversion_buffer = NULL;
1394 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001395
Eric Laurent981f7742017-05-03 12:44:26 -07001396 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001397
Eric Laurent981f7742017-05-03 12:44:26 -07001398 /* Save this for adev_dump() */
1399 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1400 } else {
1401 ALOGW("proxy_prepare error %d", ret);
jiabin8b265c92020-10-21 15:16:40 -07001402 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001403 config->channel_mask = channel_count <= FCC_2
1404 ? audio_channel_in_mask_from_count(channel_count)
1405 : audio_channel_mask_for_index_assignment_from_count(channel_count);
jiabin8b265c92020-10-21 15:16:40 -07001406 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1407 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001408 }
1409 }
Paul McLean64345f82016-06-06 13:26:05 -06001410
Eric Laurent981f7742017-05-03 12:44:26 -07001411 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001412 // Deallocate this stream on error, because AudioFlinger won't call
1413 // adev_close_input_stream() in this case.
1414 *stream_in = NULL;
1415 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001416 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001417 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001418
jiabin8b265c92020-10-21 15:16:40 -07001419 list_add_tail(&in->alsa_devices, &device_info->list_node);
1420
Andy Hungb10ce6d2017-10-27 19:37:58 -07001421 device_lock(in->adev);
1422 ++in->adev->inputs_open;
1423 device_unlock(in->adev);
1424
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001425 return ret;
1426}
1427
Paul McLean76dba682016-06-01 12:29:11 -06001428static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1429 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001430{
1431 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001432
jiabin400bbe02020-10-28 13:56:59 -07001433 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001434 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001435 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001436 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001437 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1438 if (device_info != NULL) {
1439 ALOGV("adev_close_input_stream(c:%d d:%d)",
1440 device_info->profile.card, device_info->profile.device);
1441 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001442 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1443 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001444
1445 stream_standby_l(&in->alsa_devices, &in->standby);
1446
Andy Hungb10ce6d2017-10-27 19:37:58 -07001447 device_unlock(in->adev);
1448
jiabin400bbe02020-10-28 13:56:59 -07001449 stream_clear_devices(&in->alsa_devices);
1450 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001451
1452 free(in->conversion_buffer);
1453
1454 free(stream);
1455}
1456
1457/*
1458 * ADEV Functions
1459 */
Paul McLean76dba682016-06-01 12:29:11 -06001460static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001461{
1462 return 0;
1463}
1464
Paul McLean76dba682016-06-01 12:29:11 -06001465static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001466{
1467 return strdup("");
1468}
1469
Paul McLean76dba682016-06-01 12:29:11 -06001470static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001471{
1472 return 0;
1473}
1474
Paul McLean76dba682016-06-01 12:29:11 -06001475static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001476{
1477 return -ENOSYS;
1478}
1479
Paul McLean76dba682016-06-01 12:29:11 -06001480static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001481{
1482 return -ENOSYS;
1483}
1484
Paul McLean76dba682016-06-01 12:29:11 -06001485static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001486{
1487 return 0;
1488}
1489
Paul McLean76dba682016-06-01 12:29:11 -06001490static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001491{
Paul McLean76dba682016-06-01 12:29:11 -06001492 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001493 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001494 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001495 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001496 return -ENOSYS;
1497}
1498
Paul McLean76dba682016-06-01 12:29:11 -06001499static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001500{
1501 return -ENOSYS;
1502}
1503
jiabin400bbe02020-10-28 13:56:59 -07001504static int adev_create_audio_patch(struct audio_hw_device *dev,
1505 unsigned int num_sources,
1506 const struct audio_port_config *sources,
1507 unsigned int num_sinks,
1508 const struct audio_port_config *sinks,
1509 audio_patch_handle_t *handle) {
1510 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1511 // Only accept mix->device and device->mix cases. In that case, the number of sources
1512 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1513 return -EINVAL;
1514 }
1515
1516 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1517 // If source is a device, the number of sinks should be 1.
1518 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1519 return -EINVAL;
1520 }
1521 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1522 // If source is a mix, all sinks should be device.
1523 for (unsigned int i = 0; i < num_sinks; i++) {
1524 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1525 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1526 return -EINVAL;
1527 }
1528 }
1529 } else {
1530 // All other cases are invalid.
1531 return -EINVAL;
1532 }
1533
1534 struct audio_device* adev = (struct audio_device*) dev;
1535 bool generatedPatchHandle = false;
1536 device_lock(adev);
1537 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1538 *handle = ++adev->next_patch_handle;
1539 generatedPatchHandle = true;
1540 }
1541
1542 int cards[AUDIO_PATCH_PORTS_MAX];
1543 int devices[AUDIO_PATCH_PORTS_MAX];
1544 const struct audio_port_config *port_configs =
1545 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1546 int num_configs = 0;
1547 audio_io_handle_t io_handle = 0;
1548 bool wasStandby = true;
1549 int direction = PCM_OUT;
1550 audio_patch_handle_t *patch_handle = NULL;
1551 struct listnode *alsa_devices = NULL;
1552 struct stream_lock *lock = NULL;
1553 struct pcm_config *config = NULL;
1554 struct stream_in *in = NULL;
1555 struct stream_out *out = NULL;
1556
1557 unsigned int num_saved_devices = 0;
1558 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1559 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1560
1561 struct listnode *node;
1562
1563 // Only handle patches for mix->devices and device->mix case.
1564 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1565 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1566 if (in == NULL) {
1567 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1568 device_unlock(adev);
1569 return -EINVAL;
1570 }
1571
1572 direction = PCM_IN;
1573 wasStandby = in->standby;
1574 io_handle = in->handle;
1575 num_configs = num_sources;
1576 patch_handle = &in->patch_handle;
1577 alsa_devices = &in->alsa_devices;
1578 lock = &in->lock;
1579 config = &in->config;
1580 } else {
1581 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1582 if (out == NULL) {
1583 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1584 device_unlock(adev);
1585 return -EINVAL;
1586 }
1587
1588 direction = PCM_OUT;
1589 wasStandby = out->standby;
1590 io_handle = out->handle;
1591 num_configs = num_sinks;
1592 patch_handle = &out->patch_handle;
1593 alsa_devices = &out->alsa_devices;
1594 lock = &out->lock;
1595 config = &out->config;
1596 }
1597
1598 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1599 if (!generatedPatchHandle && *patch_handle != *handle) {
1600 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1601 "with handle(%d) when creating audio patch",
1602 __func__, *handle, *patch_handle, io_handle);
1603 device_unlock(adev);
1604 return -EINVAL;
1605 }
1606 device_unlock(adev);
1607
1608 for (unsigned int i = 0; i < num_configs; ++i) {
1609 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1610 ALOGE("%s, failed to parse card and device %s",
1611 __func__, port_configs[i].ext.device.address);
1612 return -EINVAL;
1613 }
1614 }
1615
1616 stream_lock(lock);
1617 list_for_each (node, alsa_devices) {
1618 struct alsa_device_info *device_info =
1619 node_to_item(node, struct alsa_device_info, list_node);
1620 saved_cards[num_saved_devices] = device_info->profile.card;
1621 saved_devices[num_saved_devices++] = device_info->profile.device;
1622 }
1623
1624 device_lock(adev);
1625 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1626 device_unlock(adev);
1627
1628 int ret = stream_set_new_devices(config, alsa_devices, num_configs, cards, devices, direction);
1629
1630 if (ret != 0) {
1631 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1632 stream_set_new_devices(
1633 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction);
1634 } else {
1635 *patch_handle = *handle;
1636 }
1637 if (!wasStandby) {
1638 device_lock(adev);
1639 if (in != NULL) {
1640 start_input_stream(in);
1641 }
1642 if (out != NULL) {
1643 start_output_stream(out);
1644 }
1645 device_unlock(adev);
1646 }
1647 stream_unlock(lock);
1648 return ret;
1649}
1650
1651static int adev_release_audio_patch(struct audio_hw_device *dev,
1652 audio_patch_handle_t patch_handle)
1653{
1654 struct audio_device* adev = (struct audio_device*) dev;
1655
1656 device_lock(adev);
1657 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1658 device_unlock(adev);
1659 if (out != NULL) {
1660 stream_lock(&out->lock);
1661 device_lock(adev);
1662 stream_standby_l(&out->alsa_devices, &out->standby);
1663 device_unlock(adev);
1664 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1665 stream_unlock(&out->lock);
1666 return 0;
1667 }
1668
1669 device_lock(adev);
1670 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1671 device_unlock(adev);
1672 if (in != NULL) {
1673 stream_lock(&in->lock);
1674 device_lock(adev);
1675 stream_standby_l(&in->alsa_devices, &in->standby);
1676 device_unlock(adev);
1677 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1678 stream_unlock(&in->lock);
1679 return 0;
1680 }
1681
1682 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1683 return -EINVAL;
1684}
1685
1686static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1687{
1688 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1689 return -EINVAL;
1690 }
1691
1692 alsa_device_profile profile;
1693 const bool is_output = audio_is_output_device(port->ext.device.type);
1694 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1695 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1696 return -EINVAL;
1697 }
1698
1699 if (!profile_read_device_info(&profile)) {
1700 return -ENOENT;
1701 }
1702
1703 port->num_formats = 0;;
1704 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1705 profile.formats[i] != 0; ++i) {
1706 audio_format_t format = audio_format_from(profile.formats[i]);
1707 if (format != AUDIO_FORMAT_INVALID) {
1708 port->formats[port->num_formats++] = format;
1709 }
1710 }
1711
jiabina635fcf2021-01-11 11:34:50 -08001712 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1713 port->num_channel_masks = populate_channel_mask_from_profile(
1714 &profile, is_output, port->channel_masks);
jiabin400bbe02020-10-28 13:56:59 -07001715
jiabina635fcf2021-01-11 11:34:50 -08001716 return 0;
1717}
1718
1719static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1720{
1721 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1722 return -EINVAL;
jiabin400bbe02020-10-28 13:56:59 -07001723 }
jiabina635fcf2021-01-11 11:34:50 -08001724
1725 alsa_device_profile profile;
1726 const bool is_output = audio_is_output_device(port->ext.device.type);
1727 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1728 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1729 return -EINVAL;
1730 }
1731
1732 if (!profile_read_device_info(&profile)) {
1733 return -ENOENT;
1734 }
1735
1736 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1737 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1738 &profile, is_output, channel_masks);
1739 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1740 const unsigned int num_sample_rates =
1741 populate_sample_rates_from_profile(&profile, sample_rates);
1742 port->num_audio_profiles = 0;;
1743 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1744 profile.formats[i] != 0; ++i) {
1745 audio_format_t format = audio_format_from(profile.formats[i]);
1746 if (format == AUDIO_FORMAT_INVALID) {
1747 continue;
1748 }
1749 const unsigned int j = port->num_audio_profiles++;
1750 port->audio_profiles[j].format = format;
1751 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1752 memcpy(port->audio_profiles[j].sample_rates,
1753 sample_rates,
1754 num_sample_rates * sizeof(unsigned int));
1755 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1756 memcpy(port->audio_profiles[j].channel_masks,
1757 channel_masks,
1758 num_channel_masks* sizeof(audio_channel_mask_t));
1759 }
1760
jiabin400bbe02020-10-28 13:56:59 -07001761 return 0;
1762}
1763
Paul McLean76dba682016-06-01 12:29:11 -06001764static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001765{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001766 dprintf(fd, "\nUSB audio module:\n");
1767
1768 struct audio_device* adev = (struct audio_device*)device;
1769 const int kNumRetries = 3;
1770 const int kSleepTimeMS = 500;
1771
Paul McLean994ac072016-06-02 15:33:24 -06001772 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001773 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001774 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001775 sleep(kSleepTimeMS);
1776 retry--;
1777 }
1778
1779 if (retry > 0) {
1780 if (list_empty(&adev->output_stream_list)) {
1781 dprintf(fd, " No output streams.\n");
1782 } else {
1783 struct listnode* node;
1784 list_for_each(node, &adev->output_stream_list) {
1785 struct audio_stream* stream =
1786 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1787 out_dump(stream, fd);
1788 }
1789 }
1790
1791 if (list_empty(&adev->input_stream_list)) {
1792 dprintf(fd, "\n No input streams.\n");
1793 } else {
1794 struct listnode* node;
1795 list_for_each(node, &adev->input_stream_list) {
1796 struct audio_stream* stream =
1797 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1798 in_dump(stream, fd);
1799 }
1800 }
1801
Paul McLean994ac072016-06-02 15:33:24 -06001802 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001803 } else {
1804 // Couldn't lock
1805 dprintf(fd, " Could not obtain device lock.\n");
1806 }
1807
Simon Wilson19957a32012-04-06 16:17:12 -07001808 return 0;
1809}
1810
1811static int adev_close(hw_device_t *device)
1812{
Simon Wilson19957a32012-04-06 16:17:12 -07001813 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001814
Simon Wilson19957a32012-04-06 16:17:12 -07001815 return 0;
1816}
1817
Paul McLean30f41852014-04-16 15:44:20 -07001818static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001819{
Simon Wilson19957a32012-04-06 16:17:12 -07001820 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1821 return -EINVAL;
1822
Paul McLeaneedc92e2013-12-19 15:46:15 -08001823 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001824 if (!adev)
1825 return -ENOMEM;
1826
Paul McLeancf5310a2018-08-22 14:33:12 -07001827 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001828
Paul McLean6a75e4e2016-05-25 14:09:02 -06001829 list_init(&adev->output_stream_list);
1830 list_init(&adev->input_stream_list);
1831
Simon Wilson19957a32012-04-06 16:17:12 -07001832 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabina635fcf2021-01-11 11:34:50 -08001833 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001834 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001835 adev->hw_device.common.close = adev_close;
1836
Simon Wilson19957a32012-04-06 16:17:12 -07001837 adev->hw_device.init_check = adev_init_check;
1838 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1839 adev->hw_device.set_master_volume = adev_set_master_volume;
1840 adev->hw_device.set_mode = adev_set_mode;
1841 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1842 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1843 adev->hw_device.set_parameters = adev_set_parameters;
1844 adev->hw_device.get_parameters = adev_get_parameters;
1845 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1846 adev->hw_device.open_output_stream = adev_open_output_stream;
1847 adev->hw_device.close_output_stream = adev_close_output_stream;
1848 adev->hw_device.open_input_stream = adev_open_input_stream;
1849 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07001850 adev->hw_device.create_audio_patch = adev_create_audio_patch;
1851 adev->hw_device.release_audio_patch = adev_release_audio_patch;
1852 adev->hw_device.get_audio_port = adev_get_audio_port;
jiabina635fcf2021-01-11 11:34:50 -08001853 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
Simon Wilson19957a32012-04-06 16:17:12 -07001854 adev->hw_device.dump = adev_dump;
1855
1856 *device = &adev->hw_device.common;
1857
1858 return 0;
1859}
1860
1861static struct hw_module_methods_t hal_module_methods = {
1862 .open = adev_open,
1863};
1864
1865struct audio_module HAL_MODULE_INFO_SYM = {
1866 .common = {
1867 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001868 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1869 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001870 .id = AUDIO_HARDWARE_MODULE_ID,
1871 .name = "USB audio HW HAL",
1872 .author = "The Android Open Source Project",
1873 .methods = &hal_module_methods,
1874 },
1875};