blob: 9ab28593a8d9a1b090b0a55f3fb5016207ed149d [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
jiabin4bc5f252021-06-02 18:31:13 +0000435static bool are_all_devices_found(unsigned int num_devices_to_find,
436 const int cards_to_find[],
437 const int devices_to_find[],
438 unsigned int num_devices,
439 const int cards[],
440 const int devices[]) {
441 for (unsigned int i = 0; i < num_devices_to_find; ++i) {
442 unsigned int j = 0;
443 for (; j < num_devices; ++j) {
444 if (cards_to_find[i] == cards[j] && devices_to_find[i] == devices[j]) {
445 break;
446 }
447 }
448 if (j >= num_devices) {
449 return false;
450 }
451 }
452 return true;
453}
454
455static bool are_devices_the_same(unsigned int left_num_devices,
456 const int left_cards[],
457 const int left_devices[],
458 unsigned int right_num_devices,
459 const int right_cards[],
460 const int right_devices[]) {
461 if (left_num_devices != right_num_devices) {
462 return false;
463 }
464 return are_all_devices_found(left_num_devices, left_cards, left_devices,
465 right_num_devices, right_cards, right_devices) &&
466 are_all_devices_found(right_num_devices, right_cards, right_devices,
467 left_num_devices, left_cards, left_devices);
468}
469
Paul McLeaneedc92e2013-12-19 15:46:15 -0800470/*
471 * HAl Functions
472 */
Simon Wilson19957a32012-04-06 16:17:12 -0700473/**
474 * NOTE: when multiple mutexes have to be acquired, always respect the
475 * following order: hw device > out stream
476 */
477
jiabin8b265c92020-10-21 15:16:40 -0700478static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
479 if (list_empty(alsa_devices)) {
480 return NULL;
481 }
482 return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
483}
484
jiabin400bbe02020-10-28 13:56:59 -0700485/**
486 * Must be called with holding the stream's lock.
487 */
488static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
489{
490 if (!*standby) {
491 struct listnode *node;
492 list_for_each (node, alsa_devices) {
493 struct alsa_device_info *device_info =
494 node_to_item(node, struct alsa_device_info, list_node);
495 proxy_close(&device_info->proxy);
496 }
497 *standby = true;
498 }
499}
500
501static void stream_clear_devices(struct listnode *alsa_devices)
502{
503 struct listnode *node, *temp;
504 struct alsa_device_info *device_info = NULL;
505 list_for_each_safe (node, temp, alsa_devices) {
506 device_info = node_to_item(node, struct alsa_device_info, list_node);
507 if (device_info != NULL) {
508 list_remove(&device_info->list_node);
509 free(device_info);
510 }
511 }
512}
513
514static int stream_set_new_devices(struct pcm_config *config,
515 struct listnode *alsa_devices,
516 unsigned int num_devices,
517 const int cards[],
518 const int devices[],
519 int direction)
520{
521 int status = 0;
522 stream_clear_devices(alsa_devices);
523
524 for (unsigned int i = 0; i < num_devices; ++i) {
525 struct alsa_device_info *device_info =
526 (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
527 profile_init(&device_info->profile, direction);
528 device_info->profile.card = cards[i];
529 device_info->profile.device = devices[i];
530 status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
531 if (status != 0) {
532 ALOGE("%s failed to read device info card=%d;device=%d",
533 __func__, cards[i], devices[i]);
534 goto exit;
535 }
536 status = proxy_prepare(&device_info->proxy, &device_info->profile, config);
537 if (status != 0) {
538 ALOGE("%s failed to prepare device card=%d;device=%d",
539 __func__, cards[i], devices[i]);
540 goto exit;
541 }
542 list_add_tail(alsa_devices, &device_info->list_node);
543 }
544
545exit:
546 if (status != 0) {
547 stream_clear_devices(alsa_devices);
548 }
549 return status;
550}
551
jiabin8b265c92020-10-21 15:16:40 -0700552static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
553 struct listnode *node;
554 size_t i = 0;
555 list_for_each(node, alsa_devices) {
556 struct alsa_device_info *device_info =
557 node_to_item(node, struct alsa_device_info, list_node);
558 dprintf(fd, "Output Profile %zu:\n", i);
559 profile_dump(&device_info->profile, fd);
560
561 dprintf(fd, "Output Proxy %zu:\n", i);
562 proxy_dump(&device_info->proxy, fd);
563 }
564}
565
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700566/*
567 * OUT functions
568 */
Simon Wilson19957a32012-04-06 16:17:12 -0700569static uint32_t out_get_sample_rate(const struct audio_stream *stream)
570{
jiabin8b265c92020-10-21 15:16:40 -0700571 struct alsa_device_info *device_info = stream_get_first_alsa_device(
572 &((struct stream_out*)stream)->alsa_devices);
573 if (device_info == NULL) {
574 ALOGW("%s device info is null", __func__);
575 return 0;
576 }
577 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700578 ALOGV("out_get_sample_rate() = %d", rate);
579 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700580}
581
582static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
583{
584 return 0;
585}
586
587static size_t out_get_buffer_size(const struct audio_stream *stream)
588{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700589 const struct stream_out* out = (const struct stream_out*)stream;
jiabin8b265c92020-10-21 15:16:40 -0700590 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
591 if (device_info == NULL) {
592 ALOGW("%s device info is null", __func__);
593 return 0;
594 }
595 return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
Simon Wilson19957a32012-04-06 16:17:12 -0700596}
597
598static uint32_t out_get_channels(const struct audio_stream *stream)
599{
Andy Hung03576be2014-07-21 21:16:45 -0700600 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700601 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700602}
603
604static audio_format_t out_get_format(const struct audio_stream *stream)
605{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700606 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
607 * Relies on the framework to provide data in the specified format.
608 * This could change in the future.
609 */
jiabin8b265c92020-10-21 15:16:40 -0700610 struct alsa_device_info *device_info = stream_get_first_alsa_device(
611 &((struct stream_out*)stream)->alsa_devices);
612 if (device_info == NULL) {
613 ALOGW("%s device info is null", __func__);
614 return AUDIO_FORMAT_DEFAULT;
615 }
616 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700617 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700618}
619
620static int out_set_format(struct audio_stream *stream, audio_format_t format)
621{
622 return 0;
623}
624
625static int out_standby(struct audio_stream *stream)
626{
627 struct stream_out *out = (struct stream_out *)stream;
628
Paul McLean994ac072016-06-02 15:33:24 -0600629 stream_lock(&out->lock);
jiabin400bbe02020-10-28 13:56:59 -0700630 device_lock(out->adev);
631 stream_standby_l(&out->alsa_devices, &out->standby);
632 device_unlock(out->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600633 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700634 return 0;
635}
636
Paul McLean6a75e4e2016-05-25 14:09:02 -0600637static int out_dump(const struct audio_stream *stream, int fd) {
638 const struct stream_out* out_stream = (const struct stream_out*) stream;
639
640 if (out_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -0700641 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600642 }
643
Simon Wilson19957a32012-04-06 16:17:12 -0700644 return 0;
645}
646
jiabin400bbe02020-10-28 13:56:59 -0700647static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -0700648{
Paul McLean65ec72b2015-02-18 09:13:24 -0800649 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800650
jiabin400bbe02020-10-28 13:56:59 -0700651 // The set parameters here only matters when the routing devices are changed.
652 // When the device version is not less than 3.0, the framework will use create
653 // audio patch API instead of set parameters to chanage audio routing.
654 return 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700655}
656
Paul McLeanf62d75e2014-07-11 15:14:19 -0700657static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
658{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700659 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600660 stream_lock(&out->lock);
jiabin8b265c92020-10-21 15:16:40 -0700661 struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
662 char *params_str = NULL;
663 if (device_info != NULL) {
664 params_str = device_get_parameters(&device_info->profile, keys);
665 }
Paul McLean994ac072016-06-02 15:33:24 -0600666 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700667 return params_str;
668}
669
Simon Wilson19957a32012-04-06 16:17:12 -0700670static uint32_t out_get_latency(const struct audio_stream_out *stream)
671{
jiabin8b265c92020-10-21 15:16:40 -0700672 struct alsa_device_info *device_info = stream_get_first_alsa_device(
673 &((struct stream_out*)stream)->alsa_devices);
674 if (device_info == NULL) {
675 ALOGW("%s device info is null", __func__);
676 return 0;
677 }
678 return proxy_get_latency(&device_info->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700679}
680
Paul McLean30f41852014-04-16 15:44:20 -0700681static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700682{
683 return -ENOSYS;
684}
685
Paul McLeaneedc92e2013-12-19 15:46:15 -0800686/* must be called with hw device and output stream mutexes locked */
687static int start_output_stream(struct stream_out *out)
688{
jiabin8b265c92020-10-21 15:16:40 -0700689 int status = 0;
690 struct listnode *node;
691 list_for_each(node, &out->alsa_devices) {
692 struct alsa_device_info *device_info =
693 node_to_item(node, struct alsa_device_info, list_node);
694 ALOGV("start_output_stream(card:%d device:%d)",
695 device_info->profile.card, device_info->profile.device);
696 status = proxy_open(&device_info->proxy);
697 if (status != 0) {
698 ALOGE("%s failed to open device(card: %d device: %d)",
699 __func__, device_info->profile.card, device_info->profile.device);
700 goto exit;
701 }
702 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800703
jiabin8b265c92020-10-21 15:16:40 -0700704exit:
705 if (status != 0) {
706 list_for_each(node, &out->alsa_devices) {
707 struct alsa_device_info *device_info =
708 node_to_item(node, struct alsa_device_info, list_node);
709 proxy_close(&device_info->proxy);
710 }
711
712 }
713 return status;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800714}
715
716static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700717{
718 int ret;
719 struct stream_out *out = (struct stream_out *)stream;
720
Paul McLean994ac072016-06-02 15:33:24 -0600721 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700722 if (out->standby) {
723 ret = start_output_stream(out);
724 if (ret != 0) {
725 goto err;
726 }
727 out->standby = false;
728 }
Eric Laurent05333d42014-08-04 20:29:17 -0700729
jiabin8b265c92020-10-21 15:16:40 -0700730 struct listnode* node;
731 list_for_each(node, &out->alsa_devices) {
732 struct alsa_device_info* device_info =
733 node_to_item(node, struct alsa_device_info, list_node);
734 alsa_device_proxy* proxy = &device_info->proxy;
735 const void * write_buff = buffer;
736 int num_write_buff_bytes = bytes;
737 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
738 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
739 if (num_device_channels != num_req_channels) {
740 /* allocate buffer */
741 const size_t required_conversion_buffer_size =
742 bytes * num_device_channels / num_req_channels;
743 if (required_conversion_buffer_size > out->conversion_buffer_size) {
744 out->conversion_buffer_size = required_conversion_buffer_size;
745 out->conversion_buffer = realloc(out->conversion_buffer,
746 out->conversion_buffer_size);
747 }
748 /* convert data */
749 const audio_format_t audio_format = out_get_format(&(out->stream.common));
750 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
751 num_write_buff_bytes =
752 adjust_channels(write_buff, num_req_channels,
753 out->conversion_buffer, num_device_channels,
754 sample_size_in_bytes, num_write_buff_bytes);
755 write_buff = out->conversion_buffer;
Andy Hung03576be2014-07-21 21:16:45 -0700756 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800757
jiabin8b265c92020-10-21 15:16:40 -0700758 if (write_buff != NULL && num_write_buff_bytes != 0) {
759 proxy_write(proxy, write_buff, num_write_buff_bytes);
760 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800761 }
Simon Wilson19957a32012-04-06 16:17:12 -0700762
Paul McLean994ac072016-06-02 15:33:24 -0600763 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700764
765 return bytes;
766
767err:
Paul McLean994ac072016-06-02 15:33:24 -0600768 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700769 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700770 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700771 out_get_sample_rate(&stream->common));
772 }
773
774 return bytes;
775}
776
Paul McLean30f41852014-04-16 15:44:20 -0700777static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700778{
779 return -EINVAL;
780}
781
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700782static int out_get_presentation_position(const struct audio_stream_out *stream,
783 uint64_t *frames, struct timespec *timestamp)
784{
Andy Hungc9515ce2015-08-04 15:05:19 -0700785 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600786 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700787
jiabin8b265c92020-10-21 15:16:40 -0700788 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
789 const int ret = device_info == NULL ? -ENODEV :
790 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
Paul McLean994ac072016-06-02 15:33:24 -0600791 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700792 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700793}
794
Simon Wilson19957a32012-04-06 16:17:12 -0700795static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
796{
797 return 0;
798}
799
800static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
801{
802 return 0;
803}
804
Paul McLean30f41852014-04-16 15:44:20 -0700805static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700806{
807 return -EINVAL;
808}
809
Paul McLean76dba682016-06-01 12:29:11 -0600810static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700811 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600812 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700813 audio_output_flags_t flags,
814 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700815 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700816 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700817{
Paul McLean76dba682016-06-01 12:29:11 -0600818 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
819 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800820
Simon Wilson19957a32012-04-06 16:17:12 -0700821 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700822
823 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600824 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700825 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600826 }
Simon Wilson19957a32012-04-06 16:17:12 -0700827
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700828 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700829 out->stream.common.get_sample_rate = out_get_sample_rate;
830 out->stream.common.set_sample_rate = out_set_sample_rate;
831 out->stream.common.get_buffer_size = out_get_buffer_size;
832 out->stream.common.get_channels = out_get_channels;
833 out->stream.common.get_format = out_get_format;
834 out->stream.common.set_format = out_set_format;
835 out->stream.common.standby = out_standby;
836 out->stream.common.dump = out_dump;
837 out->stream.common.set_parameters = out_set_parameters;
838 out->stream.common.get_parameters = out_get_parameters;
839 out->stream.common.add_audio_effect = out_add_audio_effect;
840 out->stream.common.remove_audio_effect = out_remove_audio_effect;
841 out->stream.get_latency = out_get_latency;
842 out->stream.set_volume = out_set_volume;
843 out->stream.write = out_write;
844 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700845 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700846 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
847
jiabin400bbe02020-10-28 13:56:59 -0700848 out->handle = handle;
849
Paul McLean994ac072016-06-02 15:33:24 -0600850 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700851
Paul McLean76dba682016-06-01 12:29:11 -0600852 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700853
jiabin8b265c92020-10-21 15:16:40 -0700854 list_init(&out->alsa_devices);
855 struct alsa_device_info *device_info =
856 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
857 profile_init(&device_info->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700858
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700859 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700860 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800861
Paul McLean0f1753e2014-12-02 12:36:45 -0700862 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700863 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700864
jiabin8b265c92020-10-21 15:16:40 -0700865 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700866
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700867 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800868
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700869 /* Rate */
870 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700871 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
872 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700873 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800874 } else {
jiabin8b265c92020-10-21 15:16:40 -0700875 proxy_config.rate = config->sample_rate =
876 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700877 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800878 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800879
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600880 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -0700881 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600882 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600883 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600884
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700885 /* Format */
886 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -0700887 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700888 config->format = audio_format_from_pcm_format(proxy_config.format);
889 } else {
890 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -0700891 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700892 proxy_config.format = fmt;
893 } else {
jiabin8b265c92020-10-21 15:16:40 -0700894 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700895 config->format = audio_format_from_pcm_format(proxy_config.format);
896 ret = -EINVAL;
897 }
898 }
899
900 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600901 bool calc_mask = false;
902 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
903 /* query case */
jiabin8b265c92020-10-21 15:16:40 -0700904 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -0600905 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700906 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600907 /* explicit case */
908 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700909 }
Paul McLean698dbd72015-11-03 12:24:30 -0800910
Paul McLean64345f82016-06-06 13:26:05 -0600911 /* The Framework is currently limited to no more than this number of channels */
912 if (out->hal_channel_count > FCC_8) {
913 out->hal_channel_count = FCC_8;
914 calc_mask = true;
915 }
916
917 if (calc_mask) {
918 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -0700919 * 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 -0600920 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -0700921 /* position mask for mono and stereo*/
922 ? audio_channel_out_mask_from_count(out->hal_channel_count)
923 /* otherwise indexed */
924 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
Paul McLean64345f82016-06-06 13:26:05 -0600925 }
926
Andy Hung182ddc72015-05-05 23:37:30 -0700927 out->hal_channel_mask = config->channel_mask;
928
Paul McLean64345f82016-06-06 13:26:05 -0600929 // Validate the "logical" channel count against support in the "actual" profile.
930 // if they differ, choose the "actual" number of channels *closest* to the "logical".
931 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -0700932 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -0700933 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
934 proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config);
jiabin400bbe02020-10-28 13:56:59 -0700935 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -0700936
937 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700938
Paul McLean96c4d302017-12-05 09:50:26 -0800939 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
940 * So clear any errors that may have occurred above.
941 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700942 ret = 0;
943
Paul McLeaneedc92e2013-12-19 15:46:15 -0800944 out->conversion_buffer = NULL;
945 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700946
947 out->standby = true;
948
Paul McLean6a75e4e2016-05-25 14:09:02 -0600949 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600950 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600951
Simon Wilson19957a32012-04-06 16:17:12 -0700952 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700953
954 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700955}
956
Paul McLean76dba682016-06-01 12:29:11 -0600957static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700958 struct audio_stream_out *stream)
959{
960 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -0600961
jiabin400bbe02020-10-28 13:56:59 -0700962 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700963 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -0700964 stream_standby_l(&out->alsa_devices, &out->standby);
965 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800966
967 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700968
Paul McLeaneedc92e2013-12-19 15:46:15 -0800969 out->conversion_buffer = NULL;
970 out->conversion_buffer_size = 0;
971
Paul McLean994ac072016-06-02 15:33:24 -0600972 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700973 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -0600974 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600975 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700976 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -0600977
Simon Wilson19957a32012-04-06 16:17:12 -0700978 free(stream);
979}
980
Paul McLean76dba682016-06-01 12:29:11 -0600981static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700982 const struct audio_config *config)
983{
984 /* TODO This needs to be calculated based on format/channels/rate */
985 return 320;
986}
987
988/*
989 * IN functions
990 */
991static uint32_t in_get_sample_rate(const struct audio_stream *stream)
992{
jiabin8b265c92020-10-21 15:16:40 -0700993 struct alsa_device_info *device_info = stream_get_first_alsa_device(
994 &((const struct stream_in *)stream)->alsa_devices);
995 if (device_info == NULL) {
996 ALOGW("%s device info is null", __func__);
997 return 0;
998 }
999 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001000 ALOGV("in_get_sample_rate() = %d", rate);
1001 return rate;
1002}
1003
1004static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1005{
1006 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
1007 return -ENOSYS;
1008}
1009
1010static size_t in_get_buffer_size(const struct audio_stream *stream)
1011{
1012 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -07001013 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1014 if (device_info == NULL) {
1015 ALOGW("%s device info is null", __func__);
1016 return 0;
1017 }
1018 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001019}
1020
1021static uint32_t in_get_channels(const struct audio_stream *stream)
1022{
Paul McLean2cfd81b2014-09-15 12:32:23 -07001023 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -07001024 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001025}
1026
1027static audio_format_t in_get_format(const struct audio_stream *stream)
1028{
jiabin8b265c92020-10-21 15:16:40 -07001029 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1030 &((const struct stream_in *)stream)->alsa_devices);
1031 if (device_info == NULL) {
1032 ALOGW("%s device info is null", __func__);
1033 return AUDIO_FORMAT_DEFAULT;
1034 }
1035 alsa_device_proxy *proxy = &device_info->proxy;
Andy Hung780f1f82015-04-22 22:14:39 -07001036 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1037 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001038}
1039
1040static int in_set_format(struct audio_stream *stream, audio_format_t format)
1041{
1042 ALOGV("in_set_format(%d) - NOPE", format);
1043
1044 return -ENOSYS;
1045}
1046
1047static int in_standby(struct audio_stream *stream)
1048{
1049 struct stream_in *in = (struct stream_in *)stream;
1050
Paul McLean994ac072016-06-02 15:33:24 -06001051 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -07001052 device_lock(in->adev);
1053 stream_standby_l(&in->alsa_devices, &in->standby);
1054 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -06001055 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001056 return 0;
1057}
1058
1059static int in_dump(const struct audio_stream *stream, int fd)
1060{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001061 const struct stream_in* in_stream = (const struct stream_in*)stream;
1062 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -07001063 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001064 }
1065
1066 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001067}
1068
1069static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1070{
Paul McLean65ec72b2015-02-18 09:13:24 -08001071 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001072
jiabin400bbe02020-10-28 13:56:59 -07001073 // The set parameters here only matters when the routing devices are changed.
1074 // When the device version higher than 3.0, the framework will use create_audio_patch
1075 // API instead of set_parameters to change audio routing.
1076 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001077}
1078
1079static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1080{
1081 struct stream_in *in = (struct stream_in *)stream;
1082
Paul McLean994ac072016-06-02 15:33:24 -06001083 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001084 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1085 char *params_str = NULL;
1086 if (device_info != NULL) {
1087 params_str = device_get_parameters(&device_info->profile, keys);
1088 }
Paul McLean994ac072016-06-02 15:33:24 -06001089 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001090
1091 return params_str;
1092}
1093
1094static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1095{
1096 return 0;
1097}
1098
1099static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1100{
1101 return 0;
1102}
1103
1104static int in_set_gain(struct audio_stream_in *stream, float gain)
1105{
1106 return 0;
1107}
1108
1109/* must be called with hw device and output stream mutexes locked */
1110static int start_input_stream(struct stream_in *in)
1111{
jiabin8b265c92020-10-21 15:16:40 -07001112 // Only care about the first device as only one input device is allowed.
1113 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1114 if (device_info == NULL) {
1115 return -ENODEV;
1116 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001117
jiabin8b265c92020-10-21 15:16:40 -07001118 ALOGV("start_input_stream(card:%d device:%d)",
1119 device_info->profile.card, device_info->profile.device);
1120 return proxy_open(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001121}
1122
1123/* TODO mutex stuff here (see out_write) */
1124static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1125{
1126 size_t num_read_buff_bytes = 0;
1127 void * read_buff = buffer;
1128 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001129 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001130
1131 struct stream_in * in = (struct stream_in *)stream;
1132
Paul McLean994ac072016-06-02 15:33:24 -06001133 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001134 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001135 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001136 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001137 goto err;
1138 }
1139 in->standby = false;
1140 }
1141
jiabin8b265c92020-10-21 15:16:40 -07001142 // Only care about the first device as only one input device is allowed.
1143 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1144 if (device_info == NULL) {
1145 return 0;
1146 }
1147
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001148 /*
1149 * OK, we need to figure out how much data to read to be able to output the requested
1150 * number of bytes in the HAL format (16-bit, stereo).
1151 */
1152 num_read_buff_bytes = bytes;
jiabin8b265c92020-10-21 15:16:40 -07001153 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001154 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001155
1156 if (num_device_channels != num_req_channels) {
1157 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1158 }
1159
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001160 /* Setup/Realloc the conversion buffer (if necessary). */
1161 if (num_read_buff_bytes != bytes) {
1162 if (num_read_buff_bytes > in->conversion_buffer_size) {
1163 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1164 (and do these conversions themselves) */
1165 in->conversion_buffer_size = num_read_buff_bytes;
1166 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1167 }
1168 read_buff = in->conversion_buffer;
1169 }
1170
jiabin8b265c92020-10-21 15:16:40 -07001171 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001172 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001173 if (num_device_channels != num_req_channels) {
1174 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1175
1176 out_buff = buffer;
1177 /* Num Channels conversion */
1178 if (num_device_channels != num_req_channels) {
1179 audio_format_t audio_format = in_get_format(&(in->stream.common));
1180 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1181
1182 num_read_buff_bytes =
1183 adjust_channels(read_buff, num_device_channels,
1184 out_buff, num_req_channels,
1185 sample_size_in_bytes, num_read_buff_bytes);
1186 }
1187 }
Eric Laurent253def92014-09-14 12:18:18 -07001188
Paul McLean76dba682016-06-01 12:29:11 -06001189 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1190 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -07001191 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301192 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001193 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001194 }
1195
1196err:
Paul McLean994ac072016-06-02 15:33:24 -06001197 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001198 return num_read_buff_bytes;
1199}
1200
1201static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1202{
1203 return 0;
1204}
1205
Andy Hungfa6b4a62018-06-04 19:14:22 -07001206static int in_get_capture_position(const struct audio_stream_in *stream,
1207 int64_t *frames, int64_t *time)
1208{
1209 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1210 stream_lock(&in->lock);
1211
jiabin8b265c92020-10-21 15:16:40 -07001212 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1213
1214 const int ret = device_info == NULL ? -ENODEV
1215 : proxy_get_capture_position(&device_info->proxy, frames, time);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001216
1217 stream_unlock(&in->lock);
1218 return ret;
1219}
1220
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001221static int in_get_active_microphones(const struct audio_stream_in *stream,
1222 struct audio_microphone_characteristic_t *mic_array,
1223 size_t *mic_count) {
1224 (void)stream;
1225 (void)mic_array;
1226 (void)mic_count;
1227
1228 return -ENOSYS;
1229}
1230
1231static int in_set_microphone_direction(const struct audio_stream_in *stream,
1232 audio_microphone_direction_t dir) {
1233 (void)stream;
1234 (void)dir;
1235 ALOGV("---- in_set_microphone_direction()");
1236 return -ENOSYS;
1237}
1238
1239static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1240 (void)zoom;
1241 ALOGV("---- in_set_microphone_field_dimension()");
1242 return -ENOSYS;
1243}
1244
Paul McLean76dba682016-06-01 12:29:11 -06001245static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001246 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001247 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001248 struct audio_config *config,
1249 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001250 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001251 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001252 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001253{
Paul McLean1d585cc2016-05-24 11:28:10 -06001254 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001255 config->sample_rate, config->channel_mask, config->format);
1256
Andy Hungb10ce6d2017-10-27 19:37:58 -07001257 /* Pull out the card/device pair */
1258 int32_t card, device;
1259 if (!parse_card_device_params(address, &card, &device)) {
1260 ALOGW("%s fail - invalid address %s", __func__, address);
1261 *stream_in = NULL;
1262 return -EINVAL;
1263 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001264
Andy Hungb10ce6d2017-10-27 19:37:58 -07001265 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001266 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001267 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001268 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001269 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001270
1271 /* setup function pointers */
1272 in->stream.common.get_sample_rate = in_get_sample_rate;
1273 in->stream.common.set_sample_rate = in_set_sample_rate;
1274 in->stream.common.get_buffer_size = in_get_buffer_size;
1275 in->stream.common.get_channels = in_get_channels;
1276 in->stream.common.get_format = in_get_format;
1277 in->stream.common.set_format = in_set_format;
1278 in->stream.common.standby = in_standby;
1279 in->stream.common.dump = in_dump;
1280 in->stream.common.set_parameters = in_set_parameters;
1281 in->stream.common.get_parameters = in_get_parameters;
1282 in->stream.common.add_audio_effect = in_add_audio_effect;
1283 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1284
1285 in->stream.set_gain = in_set_gain;
1286 in->stream.read = in_read;
1287 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001288 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001289
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001290 in->stream.get_active_microphones = in_get_active_microphones;
1291 in->stream.set_microphone_direction = in_set_microphone_direction;
1292 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1293
jiabin400bbe02020-10-28 13:56:59 -07001294 in->handle = handle;
1295
Paul McLean994ac072016-06-02 15:33:24 -06001296 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001297
Paul McLean76dba682016-06-01 12:29:11 -06001298 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001299
jiabin8b265c92020-10-21 15:16:40 -07001300 list_init(&in->alsa_devices);
1301 struct alsa_device_info *device_info =
1302 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1303 profile_init(&device_info->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001304
jiabin400bbe02020-10-28 13:56:59 -07001305 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001306
Andy Hungb10ce6d2017-10-27 19:37:58 -07001307 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001308 device_lock(in->adev);
1309 int num_open_inputs = in->adev->inputs_open;
1310 device_unlock(in->adev);
1311
Andy Hungb10ce6d2017-10-27 19:37:58 -07001312 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001313 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001314 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001315 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1316 __func__, card, device);
1317 ret = -EINVAL;
1318 }
1319 } else {
1320 /* Read input profile only if necessary */
jiabin8b265c92020-10-21 15:16:40 -07001321 device_info->profile.card = card;
1322 device_info->profile.device = device;
1323 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001324 ALOGW("%s fail - cannot read profile", __func__);
1325 ret = -EINVAL;
1326 }
1327 }
1328 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001329 free(in);
1330 *stream_in = NULL;
1331 return ret;
1332 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001333
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001334 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001335 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001336 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001337 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001338 }
1339
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001340 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001341 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001342 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001343 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001344 if (highest_rate == 0) {
1345 ret = -EINVAL; /* error with device */
1346 } else {
jiabin400bbe02020-10-28 13:56:59 -07001347 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001348 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001349 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001350 /* Changing the requested rate */
1351 ret = -EINVAL;
1352 } else {
1353 /* Everything AOK! */
1354 ret = 0;
1355 }
1356 }
1357 }
jiabin8b265c92020-10-21 15:16:40 -07001358 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001359 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001360 } else {
jiabin400bbe02020-10-28 13:56:59 -07001361 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001362 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001363 ret = -EINVAL;
1364 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001365
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001366 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001367 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001368 in->config.format = profile_get_default_format(&device_info->profile);
1369 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001370 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001371 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001372 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001373 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001374 } else {
jiabin400bbe02020-10-28 13:56:59 -07001375 in->config.format = profile_get_default_format(&device_info->profile);
1376 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001377 ret = -EINVAL;
1378 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001379 }
1380
Paul McLean2cfd81b2014-09-15 12:32:23 -07001381 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001382 bool calc_mask = false;
1383 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1384 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001385 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001386 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001387 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001388 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001389 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1390 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001391
Paul McLean64345f82016-06-06 13:26:05 -06001392 /* The Framework is currently limited to no more than this number of channels */
1393 if (in->hal_channel_count > FCC_8) {
1394 in->hal_channel_count = FCC_8;
1395 calc_mask = true;
1396 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001397
Paul McLean64345f82016-06-06 13:26:05 -06001398 if (calc_mask) {
1399 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001400 * 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 -06001401 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1402 /* position mask for mono & stereo */
1403 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1404 /* otherwise indexed */
1405 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001406
Paul McLean64345f82016-06-06 13:26:05 -06001407 // if we change the mask...
1408 if (in->hal_channel_mask != config->channel_mask &&
1409 config->channel_mask != AUDIO_CHANNEL_NONE) {
1410 config->channel_mask = in->hal_channel_mask;
1411 ret = -EINVAL;
1412 }
1413 } else {
1414 in->hal_channel_mask = config->channel_mask;
1415 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001416
Paul McLean64345f82016-06-06 13:26:05 -06001417 if (ret == 0) {
1418 // Validate the "logical" channel count against support in the "actual" profile.
1419 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1420 // and store THAT in proxy_config.channels
jiabin400bbe02020-10-28 13:56:59 -07001421 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001422 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabin400bbe02020-10-28 13:56:59 -07001423 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config);
Eric Laurent981f7742017-05-03 12:44:26 -07001424 if (ret == 0) {
1425 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001426
Eric Laurent981f7742017-05-03 12:44:26 -07001427 in->conversion_buffer = NULL;
1428 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001429
Eric Laurent981f7742017-05-03 12:44:26 -07001430 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001431
Eric Laurent981f7742017-05-03 12:44:26 -07001432 /* Save this for adev_dump() */
1433 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1434 } else {
1435 ALOGW("proxy_prepare error %d", ret);
jiabin8b265c92020-10-21 15:16:40 -07001436 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001437 config->channel_mask = channel_count <= FCC_2
1438 ? audio_channel_in_mask_from_count(channel_count)
1439 : audio_channel_mask_for_index_assignment_from_count(channel_count);
jiabin8b265c92020-10-21 15:16:40 -07001440 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1441 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001442 }
1443 }
Paul McLean64345f82016-06-06 13:26:05 -06001444
Eric Laurent981f7742017-05-03 12:44:26 -07001445 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001446 // Deallocate this stream on error, because AudioFlinger won't call
1447 // adev_close_input_stream() in this case.
1448 *stream_in = NULL;
1449 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001450 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001451 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001452
jiabin8b265c92020-10-21 15:16:40 -07001453 list_add_tail(&in->alsa_devices, &device_info->list_node);
1454
Andy Hungb10ce6d2017-10-27 19:37:58 -07001455 device_lock(in->adev);
1456 ++in->adev->inputs_open;
1457 device_unlock(in->adev);
1458
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001459 return ret;
1460}
1461
Paul McLean76dba682016-06-01 12:29:11 -06001462static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1463 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001464{
1465 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001466
jiabin400bbe02020-10-28 13:56:59 -07001467 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001468 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001469 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001470 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001471 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1472 if (device_info != NULL) {
1473 ALOGV("adev_close_input_stream(c:%d d:%d)",
1474 device_info->profile.card, device_info->profile.device);
1475 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001476 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1477 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001478
1479 stream_standby_l(&in->alsa_devices, &in->standby);
1480
Andy Hungb10ce6d2017-10-27 19:37:58 -07001481 device_unlock(in->adev);
1482
jiabin400bbe02020-10-28 13:56:59 -07001483 stream_clear_devices(&in->alsa_devices);
1484 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001485
1486 free(in->conversion_buffer);
1487
1488 free(stream);
1489}
1490
1491/*
1492 * ADEV Functions
1493 */
Paul McLean76dba682016-06-01 12:29:11 -06001494static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001495{
1496 return 0;
1497}
1498
Paul McLean76dba682016-06-01 12:29:11 -06001499static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001500{
1501 return strdup("");
1502}
1503
Paul McLean76dba682016-06-01 12:29:11 -06001504static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001505{
1506 return 0;
1507}
1508
Paul McLean76dba682016-06-01 12:29:11 -06001509static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001510{
1511 return -ENOSYS;
1512}
1513
Paul McLean76dba682016-06-01 12:29:11 -06001514static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001515{
1516 return -ENOSYS;
1517}
1518
Paul McLean76dba682016-06-01 12:29:11 -06001519static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001520{
1521 return 0;
1522}
1523
Paul McLean76dba682016-06-01 12:29:11 -06001524static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001525{
Paul McLean76dba682016-06-01 12:29:11 -06001526 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001527 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001528 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001529 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001530 return -ENOSYS;
1531}
1532
Paul McLean76dba682016-06-01 12:29:11 -06001533static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001534{
1535 return -ENOSYS;
1536}
1537
jiabin400bbe02020-10-28 13:56:59 -07001538static int adev_create_audio_patch(struct audio_hw_device *dev,
1539 unsigned int num_sources,
1540 const struct audio_port_config *sources,
1541 unsigned int num_sinks,
1542 const struct audio_port_config *sinks,
1543 audio_patch_handle_t *handle) {
1544 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1545 // Only accept mix->device and device->mix cases. In that case, the number of sources
1546 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1547 return -EINVAL;
1548 }
1549
1550 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1551 // If source is a device, the number of sinks should be 1.
1552 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1553 return -EINVAL;
1554 }
1555 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1556 // If source is a mix, all sinks should be device.
1557 for (unsigned int i = 0; i < num_sinks; i++) {
1558 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1559 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1560 return -EINVAL;
1561 }
1562 }
1563 } else {
1564 // All other cases are invalid.
1565 return -EINVAL;
1566 }
1567
1568 struct audio_device* adev = (struct audio_device*) dev;
1569 bool generatedPatchHandle = false;
1570 device_lock(adev);
1571 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1572 *handle = ++adev->next_patch_handle;
1573 generatedPatchHandle = true;
1574 }
1575
1576 int cards[AUDIO_PATCH_PORTS_MAX];
1577 int devices[AUDIO_PATCH_PORTS_MAX];
1578 const struct audio_port_config *port_configs =
1579 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1580 int num_configs = 0;
1581 audio_io_handle_t io_handle = 0;
1582 bool wasStandby = true;
1583 int direction = PCM_OUT;
1584 audio_patch_handle_t *patch_handle = NULL;
1585 struct listnode *alsa_devices = NULL;
1586 struct stream_lock *lock = NULL;
1587 struct pcm_config *config = NULL;
1588 struct stream_in *in = NULL;
1589 struct stream_out *out = NULL;
1590
1591 unsigned int num_saved_devices = 0;
1592 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1593 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1594
1595 struct listnode *node;
1596
1597 // Only handle patches for mix->devices and device->mix case.
1598 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1599 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1600 if (in == NULL) {
1601 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1602 device_unlock(adev);
1603 return -EINVAL;
1604 }
1605
1606 direction = PCM_IN;
1607 wasStandby = in->standby;
1608 io_handle = in->handle;
1609 num_configs = num_sources;
1610 patch_handle = &in->patch_handle;
1611 alsa_devices = &in->alsa_devices;
1612 lock = &in->lock;
1613 config = &in->config;
1614 } else {
1615 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1616 if (out == NULL) {
1617 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1618 device_unlock(adev);
1619 return -EINVAL;
1620 }
1621
1622 direction = PCM_OUT;
1623 wasStandby = out->standby;
1624 io_handle = out->handle;
1625 num_configs = num_sinks;
1626 patch_handle = &out->patch_handle;
1627 alsa_devices = &out->alsa_devices;
1628 lock = &out->lock;
1629 config = &out->config;
1630 }
1631
1632 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1633 if (!generatedPatchHandle && *patch_handle != *handle) {
1634 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1635 "with handle(%d) when creating audio patch",
1636 __func__, *handle, *patch_handle, io_handle);
1637 device_unlock(adev);
1638 return -EINVAL;
1639 }
1640 device_unlock(adev);
1641
1642 for (unsigned int i = 0; i < num_configs; ++i) {
1643 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1644 ALOGE("%s, failed to parse card and device %s",
1645 __func__, port_configs[i].ext.device.address);
1646 return -EINVAL;
1647 }
1648 }
1649
1650 stream_lock(lock);
1651 list_for_each (node, alsa_devices) {
1652 struct alsa_device_info *device_info =
1653 node_to_item(node, struct alsa_device_info, list_node);
1654 saved_cards[num_saved_devices] = device_info->profile.card;
1655 saved_devices[num_saved_devices++] = device_info->profile.device;
1656 }
1657
jiabin4bc5f252021-06-02 18:31:13 +00001658 if (are_devices_the_same(
1659 num_configs, cards, devices, num_saved_devices, saved_cards, saved_devices)) {
1660 // The new devices are the same as original ones. No need to update.
1661 stream_unlock(lock);
1662 return 0;
1663 }
1664
jiabin400bbe02020-10-28 13:56:59 -07001665 device_lock(adev);
1666 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1667 device_unlock(adev);
1668
1669 int ret = stream_set_new_devices(config, alsa_devices, num_configs, cards, devices, direction);
1670
1671 if (ret != 0) {
1672 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1673 stream_set_new_devices(
1674 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction);
1675 } else {
1676 *patch_handle = *handle;
1677 }
1678 if (!wasStandby) {
1679 device_lock(adev);
1680 if (in != NULL) {
1681 start_input_stream(in);
1682 }
1683 if (out != NULL) {
1684 start_output_stream(out);
1685 }
1686 device_unlock(adev);
1687 }
1688 stream_unlock(lock);
1689 return ret;
1690}
1691
1692static int adev_release_audio_patch(struct audio_hw_device *dev,
1693 audio_patch_handle_t patch_handle)
1694{
1695 struct audio_device* adev = (struct audio_device*) dev;
1696
1697 device_lock(adev);
1698 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1699 device_unlock(adev);
1700 if (out != NULL) {
1701 stream_lock(&out->lock);
1702 device_lock(adev);
1703 stream_standby_l(&out->alsa_devices, &out->standby);
1704 device_unlock(adev);
1705 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1706 stream_unlock(&out->lock);
1707 return 0;
1708 }
1709
1710 device_lock(adev);
1711 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1712 device_unlock(adev);
1713 if (in != NULL) {
1714 stream_lock(&in->lock);
1715 device_lock(adev);
1716 stream_standby_l(&in->alsa_devices, &in->standby);
1717 device_unlock(adev);
1718 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1719 stream_unlock(&in->lock);
1720 return 0;
1721 }
1722
1723 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1724 return -EINVAL;
1725}
1726
1727static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1728{
1729 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1730 return -EINVAL;
1731 }
1732
1733 alsa_device_profile profile;
1734 const bool is_output = audio_is_output_device(port->ext.device.type);
1735 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1736 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1737 return -EINVAL;
1738 }
1739
1740 if (!profile_read_device_info(&profile)) {
1741 return -ENOENT;
1742 }
1743
1744 port->num_formats = 0;;
1745 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1746 profile.formats[i] != 0; ++i) {
1747 audio_format_t format = audio_format_from(profile.formats[i]);
1748 if (format != AUDIO_FORMAT_INVALID) {
1749 port->formats[port->num_formats++] = format;
1750 }
1751 }
1752
jiabina635fcf2021-01-11 11:34:50 -08001753 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1754 port->num_channel_masks = populate_channel_mask_from_profile(
1755 &profile, is_output, port->channel_masks);
jiabin400bbe02020-10-28 13:56:59 -07001756
jiabina635fcf2021-01-11 11:34:50 -08001757 return 0;
1758}
1759
1760static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1761{
1762 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1763 return -EINVAL;
jiabin400bbe02020-10-28 13:56:59 -07001764 }
jiabina635fcf2021-01-11 11:34:50 -08001765
1766 alsa_device_profile profile;
1767 const bool is_output = audio_is_output_device(port->ext.device.type);
1768 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1769 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1770 return -EINVAL;
1771 }
1772
1773 if (!profile_read_device_info(&profile)) {
1774 return -ENOENT;
1775 }
1776
1777 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1778 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1779 &profile, is_output, channel_masks);
1780 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1781 const unsigned int num_sample_rates =
1782 populate_sample_rates_from_profile(&profile, sample_rates);
1783 port->num_audio_profiles = 0;;
1784 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1785 profile.formats[i] != 0; ++i) {
1786 audio_format_t format = audio_format_from(profile.formats[i]);
1787 if (format == AUDIO_FORMAT_INVALID) {
1788 continue;
1789 }
1790 const unsigned int j = port->num_audio_profiles++;
1791 port->audio_profiles[j].format = format;
1792 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1793 memcpy(port->audio_profiles[j].sample_rates,
1794 sample_rates,
1795 num_sample_rates * sizeof(unsigned int));
1796 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1797 memcpy(port->audio_profiles[j].channel_masks,
1798 channel_masks,
1799 num_channel_masks* sizeof(audio_channel_mask_t));
1800 }
1801
jiabin400bbe02020-10-28 13:56:59 -07001802 return 0;
1803}
1804
Paul McLean76dba682016-06-01 12:29:11 -06001805static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001806{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001807 dprintf(fd, "\nUSB audio module:\n");
1808
1809 struct audio_device* adev = (struct audio_device*)device;
1810 const int kNumRetries = 3;
1811 const int kSleepTimeMS = 500;
1812
Paul McLean994ac072016-06-02 15:33:24 -06001813 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001814 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001815 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001816 sleep(kSleepTimeMS);
1817 retry--;
1818 }
1819
1820 if (retry > 0) {
1821 if (list_empty(&adev->output_stream_list)) {
1822 dprintf(fd, " No output streams.\n");
1823 } else {
1824 struct listnode* node;
1825 list_for_each(node, &adev->output_stream_list) {
1826 struct audio_stream* stream =
1827 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1828 out_dump(stream, fd);
1829 }
1830 }
1831
1832 if (list_empty(&adev->input_stream_list)) {
1833 dprintf(fd, "\n No input streams.\n");
1834 } else {
1835 struct listnode* node;
1836 list_for_each(node, &adev->input_stream_list) {
1837 struct audio_stream* stream =
1838 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1839 in_dump(stream, fd);
1840 }
1841 }
1842
Paul McLean994ac072016-06-02 15:33:24 -06001843 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001844 } else {
1845 // Couldn't lock
1846 dprintf(fd, " Could not obtain device lock.\n");
1847 }
1848
Simon Wilson19957a32012-04-06 16:17:12 -07001849 return 0;
1850}
1851
1852static int adev_close(hw_device_t *device)
1853{
Simon Wilson19957a32012-04-06 16:17:12 -07001854 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001855
Simon Wilson19957a32012-04-06 16:17:12 -07001856 return 0;
1857}
1858
Paul McLean30f41852014-04-16 15:44:20 -07001859static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001860{
Simon Wilson19957a32012-04-06 16:17:12 -07001861 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1862 return -EINVAL;
1863
Paul McLeaneedc92e2013-12-19 15:46:15 -08001864 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001865 if (!adev)
1866 return -ENOMEM;
1867
Paul McLeancf5310a2018-08-22 14:33:12 -07001868 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001869
Paul McLean6a75e4e2016-05-25 14:09:02 -06001870 list_init(&adev->output_stream_list);
1871 list_init(&adev->input_stream_list);
1872
Simon Wilson19957a32012-04-06 16:17:12 -07001873 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabina635fcf2021-01-11 11:34:50 -08001874 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001875 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001876 adev->hw_device.common.close = adev_close;
1877
Simon Wilson19957a32012-04-06 16:17:12 -07001878 adev->hw_device.init_check = adev_init_check;
1879 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1880 adev->hw_device.set_master_volume = adev_set_master_volume;
1881 adev->hw_device.set_mode = adev_set_mode;
1882 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1883 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1884 adev->hw_device.set_parameters = adev_set_parameters;
1885 adev->hw_device.get_parameters = adev_get_parameters;
1886 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1887 adev->hw_device.open_output_stream = adev_open_output_stream;
1888 adev->hw_device.close_output_stream = adev_close_output_stream;
1889 adev->hw_device.open_input_stream = adev_open_input_stream;
1890 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07001891 adev->hw_device.create_audio_patch = adev_create_audio_patch;
1892 adev->hw_device.release_audio_patch = adev_release_audio_patch;
1893 adev->hw_device.get_audio_port = adev_get_audio_port;
jiabina635fcf2021-01-11 11:34:50 -08001894 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
Simon Wilson19957a32012-04-06 16:17:12 -07001895 adev->hw_device.dump = adev_dump;
1896
1897 *device = &adev->hw_device.common;
1898
1899 return 0;
1900}
1901
1902static struct hw_module_methods_t hal_module_methods = {
1903 .open = adev_open,
1904};
1905
1906struct audio_module HAL_MODULE_INFO_SYM = {
1907 .common = {
1908 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001909 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1910 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001911 .id = AUDIO_HARDWARE_MODULE_ID,
1912 .name = "USB audio HW HAL",
1913 .author = "The Android Open Source Project",
1914 .methods = &hal_module_methods,
1915 },
1916};