1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "modules.usbaudio.audio_hal"
18 /* #define LOG_NDEBUG 0 */
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <math.h>
23 #include <pthread.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29
30 #include <log/log.h>
31 #include <cutils/list.h>
32 #include <cutils/str_parms.h>
33 #include <cutils/properties.h>
34
35 #include <hardware/audio.h>
36 #include <hardware/audio_alsaops.h>
37 #include <hardware/hardware.h>
38
39 #include <system/audio.h>
40
41 #include <tinyalsa/asoundlib.h>
42
43 #include <audio_utils/channels.h>
44
45 #include "alsa_device_profile.h"
46 #include "alsa_device_proxy.h"
47 #include "alsa_logging.h"
48
49 /* Lock play & record samples rates at or above this threshold */
50 #define RATELOCK_THRESHOLD 96000
51
52 #define max(a, b) ((a) > (b) ? (a) : (b))
53 #define min(a, b) ((a) < (b) ? (a) : (b))
54
55 struct audio_device {
56 struct audio_hw_device hw_device;
57
58 pthread_mutex_t lock; /* see note below on mutex acquisition order */
59
60 /* output */
61 struct listnode output_stream_list;
62
63 /* input */
64 struct listnode input_stream_list;
65
66 /* lock input & output sample rates */
67 /*FIXME - How do we address multiple output streams? */
68 uint32_t device_sample_rate; // this should be a rate that is common to both input & output
69
70 bool mic_muted;
71
72 int32_t inputs_open; /* number of input streams currently open. */
73
74 audio_patch_handle_t next_patch_handle; // Increase 1 when create audio patch
75 };
76
77 struct stream_lock {
78 pthread_mutex_t lock; /* see note below on mutex acquisition order */
79 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
80 };
81
82 struct alsa_device_info {
83 alsa_device_profile profile; /* The profile of the ALSA device */
84 alsa_device_proxy proxy; /* The state */
85 struct listnode list_node;
86 };
87
88 struct stream_out {
89 struct audio_stream_out stream;
90
91 struct stream_lock lock;
92
93 bool standby;
94
95 struct audio_device *adev; /* hardware information - only using this for the lock */
96
97 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
98
99 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
100 * This may differ from the device channel count when
101 * the device is not compatible with AudioFlinger
102 * capabilities, e.g. exposes too many channels or
103 * too few channels. */
104 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
105 * so the proxy doesn't have a channel_mask, but
106 * audio HALs need to talk about channel masks
107 * so expose the one calculated by
108 * adev_open_output_stream */
109
110 struct listnode list_node;
111
112 void * conversion_buffer; /* any conversions are put into here
113 * they could come from here too if
114 * there was a previous conversion */
115 size_t conversion_buffer_size; /* in bytes */
116
117 struct pcm_config config;
118
119 audio_io_handle_t handle; // Unique constant for a stream
120
121 audio_patch_handle_t patch_handle; // Patch handle for this stream
122
123 bool is_bit_perfect; // True if the stream is open with bit-perfect output flag
124
125 // Mixer information used for volume handling
126 struct mixer* mixer;
127 struct mixer_ctl* volume_ctl;
128 int volume_ctl_num_values;
129 int max_volume_level;
130 int min_volume_level;
131 };
132
133 struct stream_in {
134 struct audio_stream_in stream;
135
136 struct stream_lock lock;
137
138 bool standby;
139
140 struct audio_device *adev; /* hardware information - only using this for the lock */
141
142 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
143
144 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
145 * This may differ from the device channel count when
146 * the device is not compatible with AudioFlinger
147 * capabilities, e.g. exposes too many channels or
148 * too few channels. */
149 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
150 * so the proxy doesn't have a channel_mask, but
151 * audio HALs need to talk about channel masks
152 * so expose the one calculated by
153 * adev_open_input_stream */
154
155 struct listnode list_node;
156
157 /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
158 void * conversion_buffer; /* any conversions are put into here
159 * they could come from here too if
160 * there was a previous conversion */
161 size_t conversion_buffer_size; /* in bytes */
162
163 struct pcm_config config;
164
165 audio_io_handle_t handle; // Unique identifier for a stream
166
167 audio_patch_handle_t patch_handle; // Patch handle for this stream
168 };
169
170 // Map channel count to output channel mask
171 static const audio_channel_mask_t OUT_CHANNEL_MASKS_MAP[FCC_24 + 1] = {
172 [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted)
173 // != AUDIO_CHANNEL_INVALID == 0xC0000000u
174
175 [1] = AUDIO_CHANNEL_OUT_MONO,
176 [2] = AUDIO_CHANNEL_OUT_STEREO,
177 [3] = AUDIO_CHANNEL_OUT_2POINT1,
178 [4] = AUDIO_CHANNEL_OUT_QUAD,
179 [5] = AUDIO_CHANNEL_OUT_PENTA,
180 [6] = AUDIO_CHANNEL_OUT_5POINT1,
181 [7] = AUDIO_CHANNEL_OUT_6POINT1,
182 [8] = AUDIO_CHANNEL_OUT_7POINT1,
183
184 [9 ... 11] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
185
186 [12] = AUDIO_CHANNEL_OUT_7POINT1POINT4,
187
188 [13 ... 23] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
189
190 [24] = AUDIO_CHANNEL_OUT_22POINT2,
191 };
192 static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP);
193
194 // Map channel count to input channel mask
195 static const audio_channel_mask_t IN_CHANNEL_MASKS_MAP[] = {
196 AUDIO_CHANNEL_NONE, /* 0 */
197 AUDIO_CHANNEL_IN_MONO, /* 1 */
198 AUDIO_CHANNEL_IN_STEREO, /* 2 */
199 /* channel counts greater than this are not considered */
200 };
201 static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP);
202
203 // Map channel count to index mask
204 static const audio_channel_mask_t CHANNEL_INDEX_MASKS_MAP[FCC_24 + 1] = {
205 [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
206
207 [1] = AUDIO_CHANNEL_INDEX_MASK_1,
208 [2] = AUDIO_CHANNEL_INDEX_MASK_2,
209 [3] = AUDIO_CHANNEL_INDEX_MASK_3,
210 [4] = AUDIO_CHANNEL_INDEX_MASK_4,
211 [5] = AUDIO_CHANNEL_INDEX_MASK_5,
212 [6] = AUDIO_CHANNEL_INDEX_MASK_6,
213 [7] = AUDIO_CHANNEL_INDEX_MASK_7,
214 [8] = AUDIO_CHANNEL_INDEX_MASK_8,
215
216 [9] = AUDIO_CHANNEL_INDEX_MASK_9,
217 [10] = AUDIO_CHANNEL_INDEX_MASK_10,
218 [11] = AUDIO_CHANNEL_INDEX_MASK_11,
219 [12] = AUDIO_CHANNEL_INDEX_MASK_12,
220 [13] = AUDIO_CHANNEL_INDEX_MASK_13,
221 [14] = AUDIO_CHANNEL_INDEX_MASK_14,
222 [15] = AUDIO_CHANNEL_INDEX_MASK_15,
223 [16] = AUDIO_CHANNEL_INDEX_MASK_16,
224
225 [17] = AUDIO_CHANNEL_INDEX_MASK_17,
226 [18] = AUDIO_CHANNEL_INDEX_MASK_18,
227 [19] = AUDIO_CHANNEL_INDEX_MASK_19,
228 [20] = AUDIO_CHANNEL_INDEX_MASK_20,
229 [21] = AUDIO_CHANNEL_INDEX_MASK_21,
230 [22] = AUDIO_CHANNEL_INDEX_MASK_22,
231 [23] = AUDIO_CHANNEL_INDEX_MASK_23,
232 [24] = AUDIO_CHANNEL_INDEX_MASK_24,
233 };
234 static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP);
235
236 static const char* ALL_VOLUME_CONTROL_NAMES[] = {
237 "PCM Playback Volume",
238 "Headset Playback Volume",
239 "Headphone Playback Volume",
240 "Master Playback Volume",
241 };
242 static const int VOLUME_CONTROL_NAMES_NUM = AUDIO_ARRAY_SIZE(ALL_VOLUME_CONTROL_NAMES);
243
244 /*
245 * Locking Helpers
246 */
247 /*
248 * NOTE: when multiple mutexes have to be acquired, always take the
249 * stream_in or stream_out mutex first, followed by the audio_device mutex.
250 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
251 * higher priority playback or capture thread.
252 */
253
stream_lock_init(struct stream_lock * lock)254 static void stream_lock_init(struct stream_lock *lock) {
255 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
256 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
257 }
258
stream_lock(struct stream_lock * lock)259 static void stream_lock(struct stream_lock *lock) {
260 if (lock == NULL) {
261 return;
262 }
263 pthread_mutex_lock(&lock->pre_lock);
264 pthread_mutex_lock(&lock->lock);
265 pthread_mutex_unlock(&lock->pre_lock);
266 }
267
stream_unlock(struct stream_lock * lock)268 static void stream_unlock(struct stream_lock *lock) {
269 pthread_mutex_unlock(&lock->lock);
270 }
271
device_lock(struct audio_device * adev)272 static void device_lock(struct audio_device *adev) {
273 pthread_mutex_lock(&adev->lock);
274 }
275
device_try_lock(struct audio_device * adev)276 static int device_try_lock(struct audio_device *adev) {
277 return pthread_mutex_trylock(&adev->lock);
278 }
279
device_unlock(struct audio_device * adev)280 static void device_unlock(struct audio_device *adev) {
281 pthread_mutex_unlock(&adev->lock);
282 }
283
284 /*
285 * streams list management
286 */
adev_add_stream_to_list(struct audio_device * adev,struct listnode * list,struct listnode * stream_node)287 static void adev_add_stream_to_list(
288 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
289 device_lock(adev);
290
291 list_add_tail(list, stream_node);
292
293 device_unlock(adev);
294 }
295
adev_get_stream_out_by_io_handle_l(struct audio_device * adev,audio_io_handle_t handle)296 static struct stream_out* adev_get_stream_out_by_io_handle_l(
297 struct audio_device* adev, audio_io_handle_t handle) {
298 struct listnode *node;
299 list_for_each (node, &adev->output_stream_list) {
300 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
301 if (out->handle == handle) {
302 return out;
303 }
304 }
305 return NULL;
306 }
307
adev_get_stream_in_by_io_handle_l(struct audio_device * adev,audio_io_handle_t handle)308 static struct stream_in* adev_get_stream_in_by_io_handle_l(
309 struct audio_device* adev, audio_io_handle_t handle) {
310 struct listnode *node;
311 list_for_each (node, &adev->input_stream_list) {
312 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
313 if (in->handle == handle) {
314 return in;
315 }
316 }
317 return NULL;
318 }
319
adev_get_stream_out_by_patch_handle_l(struct audio_device * adev,audio_patch_handle_t patch_handle)320 static struct stream_out* adev_get_stream_out_by_patch_handle_l(
321 struct audio_device* adev, audio_patch_handle_t patch_handle) {
322 struct listnode *node;
323 list_for_each (node, &adev->output_stream_list) {
324 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
325 if (out->patch_handle == patch_handle) {
326 return out;
327 }
328 }
329 return NULL;
330 }
331
adev_get_stream_in_by_patch_handle_l(struct audio_device * adev,audio_patch_handle_t patch_handle)332 static struct stream_in* adev_get_stream_in_by_patch_handle_l(
333 struct audio_device* adev, audio_patch_handle_t patch_handle) {
334 struct listnode *node;
335 list_for_each (node, &adev->input_stream_list) {
336 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
337 if (in->patch_handle == patch_handle) {
338 return in;
339 }
340 }
341 return NULL;
342 }
343
344 /*
345 * Extract the card and device numbers from the supplied key/value pairs.
346 * kvpairs A null-terminated string containing the key/value pairs or card and device.
347 * i.e. "card=1;device=42"
348 * card A pointer to a variable to receive the parsed-out card number.
349 * device A pointer to a variable to receive the parsed-out device number.
350 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
351 * associated key/value pair is not found in the provided string.
352 * Return true if the kvpairs string contain a card/device spec, false otherwise.
353 */
parse_card_device_params(const char * kvpairs,int * card,int * device)354 static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
355 {
356 struct str_parms * parms = str_parms_create_str(kvpairs);
357 char value[32];
358 int param_val;
359
360 // initialize to "undefined" state.
361 *card = -1;
362 *device = -1;
363
364 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
365 if (param_val >= 0) {
366 *card = atoi(value);
367 }
368
369 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
370 if (param_val >= 0) {
371 *device = atoi(value);
372 }
373
374 str_parms_destroy(parms);
375
376 return *card >= 0 && *device >= 0;
377 }
378
device_get_parameters(const alsa_device_profile * profile,const char * keys)379 static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
380 {
381 if (profile->card < 0 || profile->device < 0) {
382 return strdup("");
383 }
384
385 struct str_parms *query = str_parms_create_str(keys);
386 struct str_parms *result = str_parms_create();
387
388 /* These keys are from hardware/libhardware/include/audio.h */
389 /* supported sample rates */
390 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
391 char* rates_list = profile_get_sample_rate_strs(profile);
392 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
393 rates_list);
394 free(rates_list);
395 }
396
397 /* supported channel counts */
398 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
399 char* channels_list = profile_get_channel_count_strs(profile);
400 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
401 channels_list);
402 free(channels_list);
403 }
404
405 /* supported sample formats */
406 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
407 char * format_params = profile_get_format_strs(profile);
408 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
409 format_params);
410 free(format_params);
411 }
412 str_parms_destroy(query);
413
414 char* result_str = str_parms_to_str(result);
415 str_parms_destroy(result);
416
417 ALOGV("device_get_parameters = %s", result_str);
418
419 return result_str;
420 }
421
audio_format_from(enum pcm_format format)422 static audio_format_t audio_format_from(enum pcm_format format)
423 {
424 switch (format) {
425 case PCM_FORMAT_S16_LE:
426 return AUDIO_FORMAT_PCM_16_BIT;
427 case PCM_FORMAT_S32_LE:
428 return AUDIO_FORMAT_PCM_32_BIT;
429 case PCM_FORMAT_S8:
430 return AUDIO_FORMAT_PCM_8_BIT;
431 case PCM_FORMAT_S24_LE:
432 return AUDIO_FORMAT_PCM_8_24_BIT;
433 case PCM_FORMAT_S24_3LE:
434 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
435 default:
436 return AUDIO_FORMAT_INVALID;
437 }
438 }
439
populate_channel_mask_from_profile(const alsa_device_profile * profile,bool is_output,audio_channel_mask_t channel_masks[])440 static unsigned int populate_channel_mask_from_profile(const alsa_device_profile* profile,
441 bool is_output,
442 audio_channel_mask_t channel_masks[])
443 {
444 unsigned int num_channel_masks = 0;
445 const audio_channel_mask_t* channel_masks_map =
446 is_output ? OUT_CHANNEL_MASKS_MAP : IN_CHANNEL_MASKS_MAP;
447 int channel_masks_size = is_output ? OUT_CHANNEL_MASKS_SIZE : IN_CHANNEL_MASKS_SIZE;
448 if (channel_masks_size > FCC_LIMIT + 1) {
449 channel_masks_size = FCC_LIMIT + 1;
450 }
451 unsigned int channel_count = 0;
452 for (size_t i = 0; i < min(channel_masks_size, AUDIO_PORT_MAX_CHANNEL_MASKS) &&
453 (channel_count = profile->channel_counts[i]) != 0 &&
454 num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS; ++i) {
455 if (channel_count < channel_masks_size &&
456 channel_masks_map[channel_count] != AUDIO_CHANNEL_NONE) {
457 channel_masks[num_channel_masks++] = channel_masks_map[channel_count];
458 if (num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
459 break;
460 }
461 }
462 if (channel_count < CHANNEL_INDEX_MASKS_SIZE &&
463 CHANNEL_INDEX_MASKS_MAP[channel_count] != AUDIO_CHANNEL_NONE) {
464 channel_masks[num_channel_masks++] = CHANNEL_INDEX_MASKS_MAP[channel_count];
465 }
466 }
467 return num_channel_masks;
468 }
469
populate_sample_rates_from_profile(const alsa_device_profile * profile,unsigned int sample_rates[])470 static unsigned int populate_sample_rates_from_profile(const alsa_device_profile* profile,
471 unsigned int sample_rates[])
472 {
473 unsigned int num_sample_rates = 0;
474 for (;num_sample_rates < min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
475 profile->sample_rates[num_sample_rates] != 0; num_sample_rates++) {
476 sample_rates[num_sample_rates] = profile->sample_rates[num_sample_rates];
477 }
478 return num_sample_rates;
479 }
480
are_all_devices_found(unsigned int num_devices_to_find,const int cards_to_find[],const int devices_to_find[],unsigned int num_devices,const int cards[],const int devices[])481 static bool are_all_devices_found(unsigned int num_devices_to_find,
482 const int cards_to_find[],
483 const int devices_to_find[],
484 unsigned int num_devices,
485 const int cards[],
486 const int devices[]) {
487 for (unsigned int i = 0; i < num_devices_to_find; ++i) {
488 unsigned int j = 0;
489 for (; j < num_devices; ++j) {
490 if (cards_to_find[i] == cards[j] && devices_to_find[i] == devices[j]) {
491 break;
492 }
493 }
494 if (j >= num_devices) {
495 return false;
496 }
497 }
498 return true;
499 }
500
are_devices_the_same(unsigned int left_num_devices,const int left_cards[],const int left_devices[],unsigned int right_num_devices,const int right_cards[],const int right_devices[])501 static bool are_devices_the_same(unsigned int left_num_devices,
502 const int left_cards[],
503 const int left_devices[],
504 unsigned int right_num_devices,
505 const int right_cards[],
506 const int right_devices[]) {
507 if (left_num_devices != right_num_devices) {
508 return false;
509 }
510 return are_all_devices_found(left_num_devices, left_cards, left_devices,
511 right_num_devices, right_cards, right_devices) &&
512 are_all_devices_found(right_num_devices, right_cards, right_devices,
513 left_num_devices, left_cards, left_devices);
514 }
515
out_stream_find_mixer_volume_control(struct stream_out * out,int card)516 static void out_stream_find_mixer_volume_control(struct stream_out* out, int card) {
517 out->mixer = mixer_open(card);
518 if (out->mixer == NULL) {
519 ALOGI("%s, no mixer found for card=%d", __func__, card);
520 return;
521 }
522 unsigned int num_ctls = mixer_get_num_ctls(out->mixer);
523 for (int i = 0; i < VOLUME_CONTROL_NAMES_NUM; ++i) {
524 for (unsigned int j = 0; j < num_ctls; ++j) {
525 struct mixer_ctl *ctl = mixer_get_ctl(out->mixer, j);
526 enum mixer_ctl_type ctl_type = mixer_ctl_get_type(ctl);
527 if (strcasestr(mixer_ctl_get_name(ctl), ALL_VOLUME_CONTROL_NAMES[i]) == NULL ||
528 ctl_type != MIXER_CTL_TYPE_INT) {
529 continue;
530 }
531 ALOGD("%s, mixer volume control(%s) found", __func__, ALL_VOLUME_CONTROL_NAMES[i]);
532 out->volume_ctl_num_values = mixer_ctl_get_num_values(ctl);
533 if (out->volume_ctl_num_values <= 0) {
534 ALOGE("%s the num(%d) of volume ctl values is wrong",
535 __func__, out->volume_ctl_num_values);
536 out->volume_ctl_num_values = 0;
537 continue;
538 }
539 out->max_volume_level = mixer_ctl_get_range_max(ctl);
540 out->min_volume_level = mixer_ctl_get_range_min(ctl);
541 if (out->max_volume_level < out->min_volume_level) {
542 ALOGE("%s the max volume level(%d) is less than min volume level(%d)",
543 __func__, out->max_volume_level, out->min_volume_level);
544 out->max_volume_level = 0;
545 out->min_volume_level = 0;
546 continue;
547 }
548 out->volume_ctl = ctl;
549 return;
550 }
551 }
552 ALOGI("%s, no volume control found", __func__);
553 }
554
555 /*
556 * HAl Functions
557 */
558 /**
559 * NOTE: when multiple mutexes have to be acquired, always respect the
560 * following order: hw device > out stream
561 */
562
stream_get_first_alsa_device(const struct listnode * alsa_devices)563 static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
564 if (list_empty(alsa_devices)) {
565 return NULL;
566 }
567 return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
568 }
569
570 /**
571 * Must be called with holding the stream's lock.
572 */
stream_standby_l(struct listnode * alsa_devices,bool * standby)573 static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
574 {
575 if (!*standby) {
576 struct listnode *node;
577 list_for_each (node, alsa_devices) {
578 struct alsa_device_info *device_info =
579 node_to_item(node, struct alsa_device_info, list_node);
580 proxy_close(&device_info->proxy);
581 }
582 *standby = true;
583 }
584 }
585
stream_clear_devices(struct listnode * alsa_devices)586 static void stream_clear_devices(struct listnode *alsa_devices)
587 {
588 struct listnode *node, *temp;
589 struct alsa_device_info *device_info = NULL;
590 list_for_each_safe (node, temp, alsa_devices) {
591 device_info = node_to_item(node, struct alsa_device_info, list_node);
592 if (device_info != NULL) {
593 list_remove(&device_info->list_node);
594 free(device_info);
595 }
596 }
597 }
598
stream_set_new_devices(struct pcm_config * config,struct listnode * alsa_devices,unsigned int num_devices,const int cards[],const int devices[],int direction,bool is_bit_perfect)599 static int stream_set_new_devices(struct pcm_config *config,
600 struct listnode *alsa_devices,
601 unsigned int num_devices,
602 const int cards[],
603 const int devices[],
604 int direction,
605 bool is_bit_perfect)
606 {
607 int status = 0;
608 stream_clear_devices(alsa_devices);
609
610 for (unsigned int i = 0; i < num_devices; ++i) {
611 struct alsa_device_info *device_info =
612 (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
613 profile_init(&device_info->profile, direction);
614 device_info->profile.card = cards[i];
615 device_info->profile.device = devices[i];
616 status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
617 if (status != 0) {
618 ALOGE("%s failed to read device info card=%d;device=%d",
619 __func__, cards[i], devices[i]);
620 goto exit;
621 }
622 status = proxy_prepare(&device_info->proxy, &device_info->profile, config, is_bit_perfect);
623 if (status != 0) {
624 ALOGE("%s failed to prepare device card=%d;device=%d",
625 __func__, cards[i], devices[i]);
626 goto exit;
627 }
628 list_add_tail(alsa_devices, &device_info->list_node);
629 }
630
631 exit:
632 if (status != 0) {
633 stream_clear_devices(alsa_devices);
634 }
635 return status;
636 }
637
stream_dump_alsa_devices(const struct listnode * alsa_devices,int fd)638 static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
639 struct listnode *node;
640 size_t i = 0;
641 list_for_each(node, alsa_devices) {
642 struct alsa_device_info *device_info =
643 node_to_item(node, struct alsa_device_info, list_node);
644 const char* direction = device_info->profile.direction == PCM_OUT ? "Output" : "Input";
645 dprintf(fd, "%s Profile %zu:\n", direction, i);
646 profile_dump(&device_info->profile, fd);
647
648 dprintf(fd, "%s Proxy %zu:\n", direction, i);
649 proxy_dump(&device_info->proxy, fd);
650 }
651 }
652
653 /*
654 * OUT functions
655 */
out_get_sample_rate(const struct audio_stream * stream)656 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
657 {
658 struct alsa_device_info *device_info = stream_get_first_alsa_device(
659 &((struct stream_out*)stream)->alsa_devices);
660 if (device_info == NULL) {
661 ALOGW("%s device info is null", __func__);
662 return 0;
663 }
664 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
665 ALOGV("out_get_sample_rate() = %d", rate);
666 return rate;
667 }
668
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)669 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
670 {
671 return 0;
672 }
673
out_get_buffer_size(const struct audio_stream * stream)674 static size_t out_get_buffer_size(const struct audio_stream *stream)
675 {
676 const struct stream_out* out = (const struct stream_out*)stream;
677 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
678 if (device_info == NULL) {
679 ALOGW("%s device info is null", __func__);
680 return 0;
681 }
682 return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
683 }
684
out_get_channels(const struct audio_stream * stream)685 static uint32_t out_get_channels(const struct audio_stream *stream)
686 {
687 const struct stream_out *out = (const struct stream_out*)stream;
688 return out->hal_channel_mask;
689 }
690
out_get_format(const struct audio_stream * stream)691 static audio_format_t out_get_format(const struct audio_stream *stream)
692 {
693 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
694 * Relies on the framework to provide data in the specified format.
695 * This could change in the future.
696 */
697 struct alsa_device_info *device_info = stream_get_first_alsa_device(
698 &((struct stream_out*)stream)->alsa_devices);
699 if (device_info == NULL) {
700 ALOGW("%s device info is null", __func__);
701 return AUDIO_FORMAT_DEFAULT;
702 }
703 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
704 return format;
705 }
706
out_set_format(struct audio_stream * stream,audio_format_t format)707 static int out_set_format(struct audio_stream *stream, audio_format_t format)
708 {
709 return 0;
710 }
711
out_standby(struct audio_stream * stream)712 static int out_standby(struct audio_stream *stream)
713 {
714 struct stream_out *out = (struct stream_out *)stream;
715
716 stream_lock(&out->lock);
717 device_lock(out->adev);
718 stream_standby_l(&out->alsa_devices, &out->standby);
719 device_unlock(out->adev);
720 stream_unlock(&out->lock);
721 return 0;
722 }
723
out_dump(const struct audio_stream * stream,int fd)724 static int out_dump(const struct audio_stream *stream, int fd) {
725 const struct stream_out* out_stream = (const struct stream_out*) stream;
726
727 if (out_stream != NULL) {
728 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
729 }
730
731 return 0;
732 }
733
out_set_parameters(struct audio_stream * stream __unused,const char * kvpairs)734 static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
735 {
736 ALOGV("out_set_parameters() keys:%s", kvpairs);
737
738 // The set parameters here only matters when the routing devices are changed.
739 // When the device version is not less than 3.0, the framework will use create
740 // audio patch API instead of set parameters to chanage audio routing.
741 return 0;
742 }
743
out_get_parameters(const struct audio_stream * stream,const char * keys)744 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
745 {
746 struct stream_out *out = (struct stream_out *)stream;
747 stream_lock(&out->lock);
748 struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
749 char *params_str = NULL;
750 if (device_info != NULL) {
751 params_str = device_get_parameters(&device_info->profile, keys);
752 }
753 stream_unlock(&out->lock);
754 return params_str;
755 }
756
out_get_latency(const struct audio_stream_out * stream)757 static uint32_t out_get_latency(const struct audio_stream_out *stream)
758 {
759 struct alsa_device_info *device_info = stream_get_first_alsa_device(
760 &((struct stream_out*)stream)->alsa_devices);
761 if (device_info == NULL) {
762 ALOGW("%s device info is null", __func__);
763 return 0;
764 }
765 return proxy_get_latency(&device_info->proxy);
766 }
767
out_set_volume(struct audio_stream_out * stream,float left,float right)768 static int out_set_volume(struct audio_stream_out *stream, float left, float right)
769 {
770 struct stream_out *out = (struct stream_out *)stream;
771 int result = -ENOSYS;
772 stream_lock(&out->lock);
773 if (out->volume_ctl != NULL) {
774 int left_volume =
775 out->min_volume_level + ceil((out->max_volume_level - out->min_volume_level) * left);
776 int right_volume =
777 out->min_volume_level + ceil((out->max_volume_level - out->min_volume_level) * right);
778 int volumes[out->volume_ctl_num_values];
779 if (out->volume_ctl_num_values == 1) {
780 volumes[0] = left_volume;
781 } else {
782 volumes[0] = left_volume;
783 volumes[1] = right_volume;
784 for (int i = 2; i < out->volume_ctl_num_values; ++i) {
785 volumes[i] = left_volume;
786 }
787 }
788 result = mixer_ctl_set_array(out->volume_ctl, volumes, out->volume_ctl_num_values);
789 if (result != 0) {
790 ALOGE("%s error=%d left=%f right=%f", __func__, result, left, right);
791 }
792 }
793 stream_unlock(&out->lock);
794 return result;
795 }
796
797 /* must be called with hw device and output stream mutexes locked */
start_output_stream(struct stream_out * out)798 static int start_output_stream(struct stream_out *out)
799 {
800 int status = 0;
801 struct listnode *node;
802 list_for_each(node, &out->alsa_devices) {
803 struct alsa_device_info *device_info =
804 node_to_item(node, struct alsa_device_info, list_node);
805 ALOGV("start_output_stream(card:%d device:%d)",
806 device_info->profile.card, device_info->profile.device);
807 status = proxy_open(&device_info->proxy);
808 if (status != 0) {
809 ALOGE("%s failed to open device(card: %d device: %d)",
810 __func__, device_info->profile.card, device_info->profile.device);
811 goto exit;
812 }
813 }
814
815 exit:
816 if (status != 0) {
817 list_for_each(node, &out->alsa_devices) {
818 struct alsa_device_info *device_info =
819 node_to_item(node, struct alsa_device_info, list_node);
820 proxy_close(&device_info->proxy);
821 }
822
823 }
824 return status;
825 }
826
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)827 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
828 {
829 int ret;
830 struct stream_out *out = (struct stream_out *)stream;
831
832 stream_lock(&out->lock);
833 if (out->standby) {
834 ret = start_output_stream(out);
835 if (ret != 0) {
836 goto err;
837 }
838 out->standby = false;
839 }
840
841 struct listnode* node;
842 list_for_each(node, &out->alsa_devices) {
843 struct alsa_device_info* device_info =
844 node_to_item(node, struct alsa_device_info, list_node);
845 alsa_device_proxy* proxy = &device_info->proxy;
846 const void * write_buff = buffer;
847 int num_write_buff_bytes = bytes;
848 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
849 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
850 if (num_device_channels != num_req_channels) {
851 /* allocate buffer */
852 const size_t required_conversion_buffer_size =
853 bytes * num_device_channels / num_req_channels;
854 if (required_conversion_buffer_size > out->conversion_buffer_size) {
855 out->conversion_buffer_size = required_conversion_buffer_size;
856 out->conversion_buffer = realloc(out->conversion_buffer,
857 out->conversion_buffer_size);
858 }
859 /* convert data */
860 const audio_format_t audio_format = out_get_format(&(out->stream.common));
861 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
862 num_write_buff_bytes =
863 adjust_channels(write_buff, num_req_channels,
864 out->conversion_buffer, num_device_channels,
865 sample_size_in_bytes, num_write_buff_bytes);
866 write_buff = out->conversion_buffer;
867 }
868
869 if (write_buff != NULL && num_write_buff_bytes != 0) {
870 proxy_write(proxy, write_buff, num_write_buff_bytes);
871 }
872 }
873
874 stream_unlock(&out->lock);
875
876 return bytes;
877
878 err:
879 stream_unlock(&out->lock);
880 if (ret != 0) {
881 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
882 out_get_sample_rate(&stream->common));
883 }
884
885 return bytes;
886 }
887
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)888 static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
889 {
890 return -EINVAL;
891 }
892
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)893 static int out_get_presentation_position(const struct audio_stream_out *stream,
894 uint64_t *frames, struct timespec *timestamp)
895 {
896 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
897 stream_lock(&out->lock);
898
899 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
900 const int ret = device_info == NULL ? -ENODEV :
901 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
902 stream_unlock(&out->lock);
903 return ret;
904 }
905
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)906 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
907 {
908 return 0;
909 }
910
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)911 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
912 {
913 return 0;
914 }
915
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)916 static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
917 {
918 return -EINVAL;
919 }
920
adev_open_output_stream(struct audio_hw_device * hw_dev,audio_io_handle_t handle,audio_devices_t devicesSpec __unused,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address)921 static int adev_open_output_stream(struct audio_hw_device *hw_dev,
922 audio_io_handle_t handle,
923 audio_devices_t devicesSpec __unused,
924 audio_output_flags_t flags,
925 struct audio_config *config,
926 struct audio_stream_out **stream_out,
927 const char *address /*__unused*/)
928 {
929 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
930 handle, devicesSpec, flags, address);
931
932 const bool is_bit_perfect = ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE);
933 if (is_bit_perfect && (config->format == AUDIO_FORMAT_DEFAULT ||
934 config->sample_rate == 0 ||
935 config->channel_mask == AUDIO_CHANNEL_NONE)) {
936 ALOGE("%s request bit perfect playback, config(format=%#x, sample_rate=%u, "
937 "channel_mask=%#x) must be specified", __func__, config->format,
938 config->sample_rate, config->channel_mask);
939 return -EINVAL;
940 }
941
942 struct stream_out *out;
943
944 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
945 if (out == NULL) {
946 return -ENOMEM;
947 }
948
949 /* setup function pointers */
950 out->stream.common.get_sample_rate = out_get_sample_rate;
951 out->stream.common.set_sample_rate = out_set_sample_rate;
952 out->stream.common.get_buffer_size = out_get_buffer_size;
953 out->stream.common.get_channels = out_get_channels;
954 out->stream.common.get_format = out_get_format;
955 out->stream.common.set_format = out_set_format;
956 out->stream.common.standby = out_standby;
957 out->stream.common.dump = out_dump;
958 out->stream.common.set_parameters = out_set_parameters;
959 out->stream.common.get_parameters = out_get_parameters;
960 out->stream.common.add_audio_effect = out_add_audio_effect;
961 out->stream.common.remove_audio_effect = out_remove_audio_effect;
962 out->stream.get_latency = out_get_latency;
963 out->stream.set_volume = out_set_volume;
964 out->stream.write = out_write;
965 out->stream.get_render_position = out_get_render_position;
966 out->stream.get_presentation_position = out_get_presentation_position;
967 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
968
969 out->handle = handle;
970
971 stream_lock_init(&out->lock);
972
973 out->adev = (struct audio_device *)hw_dev;
974
975 list_init(&out->alsa_devices);
976 struct alsa_device_info *device_info =
977 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
978 profile_init(&device_info->profile, PCM_OUT);
979
980 // build this to hand to the alsa_device_proxy
981 struct pcm_config proxy_config = {};
982
983 /* Pull out the card/device pair */
984 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
985
986 profile_read_device_info(&device_info->profile);
987
988 int ret = 0;
989
990 /* Rate */
991 if (config->sample_rate == 0) {
992 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
993 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
994 proxy_config.rate = config->sample_rate;
995 } else {
996 ret = -EINVAL;
997 if (is_bit_perfect) {
998 ALOGE("%s requesting bit-perfect but the sample rate(%u) is not valid",
999 __func__, config->sample_rate);
1000 return ret;
1001 }
1002 proxy_config.rate = config->sample_rate =
1003 profile_get_default_sample_rate(&device_info->profile);
1004 }
1005
1006 /* TODO: This is a problem if the input does not support this rate */
1007 device_lock(out->adev);
1008 out->adev->device_sample_rate = config->sample_rate;
1009 device_unlock(out->adev);
1010
1011 /* Format */
1012 if (config->format == AUDIO_FORMAT_DEFAULT) {
1013 proxy_config.format = profile_get_default_format(&device_info->profile);
1014 config->format = audio_format_from_pcm_format(proxy_config.format);
1015 } else {
1016 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1017 if (profile_is_format_valid(&device_info->profile, fmt)) {
1018 proxy_config.format = fmt;
1019 } else {
1020 ret = -EINVAL;
1021 if (is_bit_perfect) {
1022 ALOGE("%s request bit-perfect but the format(%#x) is not valid",
1023 __func__, config->format);
1024 return ret;
1025 }
1026 proxy_config.format = profile_get_default_format(&device_info->profile);
1027 config->format = audio_format_from_pcm_format(proxy_config.format);
1028 }
1029 }
1030
1031 /* Channels */
1032 bool calc_mask = false;
1033 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1034 /* query case */
1035 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
1036 calc_mask = true;
1037 } else {
1038 /* explicit case */
1039 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
1040 }
1041
1042 /* The Framework is currently limited to no more than this number of channels */
1043 if (out->hal_channel_count > FCC_LIMIT) {
1044 out->hal_channel_count = FCC_LIMIT;
1045 calc_mask = true;
1046 }
1047
1048 if (calc_mask) {
1049 /* need to calculate the mask from channel count either because this is the query case
1050 * or the specified mask isn't valid for this device, or is more than the FW can handle */
1051 config->channel_mask = out->hal_channel_count <= FCC_2
1052 /* position mask for mono and stereo*/
1053 ? audio_channel_out_mask_from_count(out->hal_channel_count)
1054 /* otherwise indexed */
1055 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
1056 }
1057
1058 out->hal_channel_mask = config->channel_mask;
1059
1060 // Validate the "logical" channel count against support in the "actual" profile.
1061 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1062 // and store THAT in proxy_config.channels
1063 proxy_config.channels =
1064 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
1065 if (is_bit_perfect && proxy_config.channels != out->hal_channel_count) {
1066 ALOGE("%s request bit-perfect, but channel mask(%#x) cannot find exact match",
1067 __func__, config->channel_mask);
1068 return -EINVAL;
1069 }
1070
1071 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config, is_bit_perfect);
1072 if (is_bit_perfect && ret != 0) {
1073 ALOGE("%s failed to prepare proxy for bit-perfect playback, err=%d", __func__, ret);
1074 return ret;
1075 }
1076 out->config = proxy_config;
1077
1078 list_add_tail(&out->alsa_devices, &device_info->list_node);
1079
1080 if ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE) {
1081 out_stream_find_mixer_volume_control(out, device_info->profile.card);
1082 }
1083
1084 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
1085 * So clear any errors that may have occurred above.
1086 */
1087 ret = 0;
1088
1089 out->conversion_buffer = NULL;
1090 out->conversion_buffer_size = 0;
1091
1092 out->standby = true;
1093
1094 /* Save the stream for adev_dump() */
1095 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
1096
1097 *stream_out = &out->stream;
1098
1099 return ret;
1100 }
1101
adev_close_output_stream(struct audio_hw_device * hw_dev,struct audio_stream_out * stream)1102 static void adev_close_output_stream(struct audio_hw_device *hw_dev,
1103 struct audio_stream_out *stream)
1104 {
1105 struct stream_out *out = (struct stream_out *)stream;
1106
1107 stream_lock(&out->lock);
1108 /* Close the pcm device */
1109 stream_standby_l(&out->alsa_devices, &out->standby);
1110 stream_clear_devices(&out->alsa_devices);
1111
1112 free(out->conversion_buffer);
1113
1114 out->conversion_buffer = NULL;
1115 out->conversion_buffer_size = 0;
1116
1117 if (out->volume_ctl != NULL) {
1118 for (int i = 0; i < out->volume_ctl_num_values; ++i) {
1119 mixer_ctl_set_value(out->volume_ctl, i, out->max_volume_level);
1120 }
1121 out->volume_ctl = NULL;
1122 }
1123 if (out->mixer != NULL) {
1124 mixer_close(out->mixer);
1125 out->mixer = NULL;
1126 }
1127
1128 device_lock(out->adev);
1129 list_remove(&out->list_node);
1130 out->adev->device_sample_rate = 0;
1131 device_unlock(out->adev);
1132 stream_unlock(&out->lock);
1133
1134 free(stream);
1135 }
1136
adev_get_input_buffer_size(const struct audio_hw_device * hw_dev,const struct audio_config * config)1137 static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
1138 const struct audio_config *config)
1139 {
1140 /* TODO This needs to be calculated based on format/channels/rate */
1141 return 320;
1142 }
1143
1144 /*
1145 * IN functions
1146 */
in_get_sample_rate(const struct audio_stream * stream)1147 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1148 {
1149 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1150 &((const struct stream_in *)stream)->alsa_devices);
1151 if (device_info == NULL) {
1152 ALOGW("%s device info is null", __func__);
1153 return 0;
1154 }
1155 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
1156 ALOGV("in_get_sample_rate() = %d", rate);
1157 return rate;
1158 }
1159
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)1160 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1161 {
1162 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
1163 return -ENOSYS;
1164 }
1165
in_get_buffer_size(const struct audio_stream * stream)1166 static size_t in_get_buffer_size(const struct audio_stream *stream)
1167 {
1168 const struct stream_in * in = ((const struct stream_in*)stream);
1169 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1170 if (device_info == NULL) {
1171 ALOGW("%s device info is null", __func__);
1172 return 0;
1173 }
1174 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
1175 }
1176
in_get_channels(const struct audio_stream * stream)1177 static uint32_t in_get_channels(const struct audio_stream *stream)
1178 {
1179 const struct stream_in *in = (const struct stream_in*)stream;
1180 return in->hal_channel_mask;
1181 }
1182
in_get_format(const struct audio_stream * stream)1183 static audio_format_t in_get_format(const struct audio_stream *stream)
1184 {
1185 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1186 &((const struct stream_in *)stream)->alsa_devices);
1187 if (device_info == NULL) {
1188 ALOGW("%s device info is null", __func__);
1189 return AUDIO_FORMAT_DEFAULT;
1190 }
1191 alsa_device_proxy *proxy = &device_info->proxy;
1192 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1193 return format;
1194 }
1195
in_set_format(struct audio_stream * stream,audio_format_t format)1196 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1197 {
1198 ALOGV("in_set_format(%d) - NOPE", format);
1199
1200 return -ENOSYS;
1201 }
1202
in_standby(struct audio_stream * stream)1203 static int in_standby(struct audio_stream *stream)
1204 {
1205 struct stream_in *in = (struct stream_in *)stream;
1206
1207 stream_lock(&in->lock);
1208 device_lock(in->adev);
1209 stream_standby_l(&in->alsa_devices, &in->standby);
1210 device_unlock(in->adev);
1211 stream_unlock(&in->lock);
1212 return 0;
1213 }
1214
in_dump(const struct audio_stream * stream,int fd)1215 static int in_dump(const struct audio_stream *stream, int fd)
1216 {
1217 const struct stream_in* in_stream = (const struct stream_in*)stream;
1218 if (in_stream != NULL) {
1219 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
1220 }
1221
1222 return 0;
1223 }
1224
in_set_parameters(struct audio_stream * stream,const char * kvpairs)1225 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1226 {
1227 ALOGV("in_set_parameters() keys:%s", kvpairs);
1228
1229 // The set parameters here only matters when the routing devices are changed.
1230 // When the device version higher than 3.0, the framework will use create_audio_patch
1231 // API instead of set_parameters to change audio routing.
1232 return 0;
1233 }
1234
in_get_parameters(const struct audio_stream * stream,const char * keys)1235 static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1236 {
1237 struct stream_in *in = (struct stream_in *)stream;
1238
1239 stream_lock(&in->lock);
1240 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1241 char *params_str = NULL;
1242 if (device_info != NULL) {
1243 params_str = device_get_parameters(&device_info->profile, keys);
1244 }
1245 stream_unlock(&in->lock);
1246
1247 return params_str;
1248 }
1249
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1250 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1251 {
1252 return 0;
1253 }
1254
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1255 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1256 {
1257 return 0;
1258 }
1259
in_set_gain(struct audio_stream_in * stream,float gain)1260 static int in_set_gain(struct audio_stream_in *stream, float gain)
1261 {
1262 return 0;
1263 }
1264
1265 /* must be called with hw device and output stream mutexes locked */
start_input_stream(struct stream_in * in)1266 static int start_input_stream(struct stream_in *in)
1267 {
1268 // Only care about the first device as only one input device is allowed.
1269 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1270 if (device_info == NULL) {
1271 return -ENODEV;
1272 }
1273
1274 ALOGV("start_input_stream(card:%d device:%d)",
1275 device_info->profile.card, device_info->profile.device);
1276 return proxy_open(&device_info->proxy);
1277 }
1278
1279 /* TODO mutex stuff here (see out_write) */
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)1280 static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1281 {
1282 size_t num_read_buff_bytes = 0;
1283 void * read_buff = buffer;
1284 void * out_buff = buffer;
1285 int ret = 0;
1286
1287 struct stream_in * in = (struct stream_in *)stream;
1288
1289 stream_lock(&in->lock);
1290 if (in->standby) {
1291 ret = start_input_stream(in);
1292 if (ret != 0) {
1293 goto err;
1294 }
1295 in->standby = false;
1296 }
1297
1298 // Only care about the first device as only one input device is allowed.
1299 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1300 if (device_info == NULL) {
1301 return 0;
1302 }
1303
1304 /*
1305 * OK, we need to figure out how much data to read to be able to output the requested
1306 * number of bytes in the HAL format (16-bit, stereo).
1307 */
1308 num_read_buff_bytes = bytes;
1309 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
1310 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
1311
1312 if (num_device_channels != num_req_channels) {
1313 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1314 }
1315
1316 /* Setup/Realloc the conversion buffer (if necessary). */
1317 if (num_read_buff_bytes != bytes) {
1318 if (num_read_buff_bytes > in->conversion_buffer_size) {
1319 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1320 (and do these conversions themselves) */
1321 in->conversion_buffer_size = num_read_buff_bytes;
1322 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1323 }
1324 read_buff = in->conversion_buffer;
1325 }
1326
1327 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
1328 if (ret == 0) {
1329 if (num_device_channels != num_req_channels) {
1330 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1331
1332 out_buff = buffer;
1333 /* Num Channels conversion */
1334 if (num_device_channels != num_req_channels) {
1335 audio_format_t audio_format = in_get_format(&(in->stream.common));
1336 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1337
1338 num_read_buff_bytes =
1339 adjust_channels(read_buff, num_device_channels,
1340 out_buff, num_req_channels,
1341 sample_size_in_bytes, num_read_buff_bytes);
1342 }
1343 }
1344
1345 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1346 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
1347 memset(buffer, 0, num_read_buff_bytes);
1348 } else {
1349 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
1350 }
1351
1352 err:
1353 stream_unlock(&in->lock);
1354 return num_read_buff_bytes;
1355 }
1356
in_get_input_frames_lost(struct audio_stream_in * stream)1357 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1358 {
1359 return 0;
1360 }
1361
in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)1362 static int in_get_capture_position(const struct audio_stream_in *stream,
1363 int64_t *frames, int64_t *time)
1364 {
1365 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1366 stream_lock(&in->lock);
1367
1368 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1369
1370 const int ret = device_info == NULL ? -ENODEV
1371 : proxy_get_capture_position(&device_info->proxy, frames, time);
1372
1373 stream_unlock(&in->lock);
1374 return ret;
1375 }
1376
in_get_active_microphones(const struct audio_stream_in * stream,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)1377 static int in_get_active_microphones(const struct audio_stream_in *stream,
1378 struct audio_microphone_characteristic_t *mic_array,
1379 size_t *mic_count) {
1380 (void)stream;
1381 (void)mic_array;
1382 (void)mic_count;
1383
1384 return -ENOSYS;
1385 }
1386
in_set_microphone_direction(const struct audio_stream_in * stream,audio_microphone_direction_t dir)1387 static int in_set_microphone_direction(const struct audio_stream_in *stream,
1388 audio_microphone_direction_t dir) {
1389 (void)stream;
1390 (void)dir;
1391 ALOGV("---- in_set_microphone_direction()");
1392 return -ENOSYS;
1393 }
1394
in_set_microphone_field_dimension(const struct audio_stream_in * stream,float zoom)1395 static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1396 (void)zoom;
1397 ALOGV("---- in_set_microphone_field_dimension()");
1398 return -ENOSYS;
1399 }
1400
adev_open_input_stream(struct audio_hw_device * hw_dev,audio_io_handle_t handle,audio_devices_t devicesSpec __unused,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags __unused,const char * address,audio_source_t source __unused)1401 static int adev_open_input_stream(struct audio_hw_device *hw_dev,
1402 audio_io_handle_t handle,
1403 audio_devices_t devicesSpec __unused,
1404 struct audio_config *config,
1405 struct audio_stream_in **stream_in,
1406 audio_input_flags_t flags __unused,
1407 const char *address,
1408 audio_source_t source __unused)
1409 {
1410 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
1411 config->sample_rate, config->channel_mask, config->format);
1412
1413 /* Pull out the card/device pair */
1414 int32_t card, device;
1415 if (!parse_card_device_params(address, &card, &device)) {
1416 ALOGW("%s fail - invalid address %s", __func__, address);
1417 *stream_in = NULL;
1418 return -EINVAL;
1419 }
1420
1421 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1422 if (in == NULL) {
1423 *stream_in = NULL;
1424 return -ENOMEM;
1425 }
1426
1427 /* setup function pointers */
1428 in->stream.common.get_sample_rate = in_get_sample_rate;
1429 in->stream.common.set_sample_rate = in_set_sample_rate;
1430 in->stream.common.get_buffer_size = in_get_buffer_size;
1431 in->stream.common.get_channels = in_get_channels;
1432 in->stream.common.get_format = in_get_format;
1433 in->stream.common.set_format = in_set_format;
1434 in->stream.common.standby = in_standby;
1435 in->stream.common.dump = in_dump;
1436 in->stream.common.set_parameters = in_set_parameters;
1437 in->stream.common.get_parameters = in_get_parameters;
1438 in->stream.common.add_audio_effect = in_add_audio_effect;
1439 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1440
1441 in->stream.set_gain = in_set_gain;
1442 in->stream.read = in_read;
1443 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1444 in->stream.get_capture_position = in_get_capture_position;
1445
1446 in->stream.get_active_microphones = in_get_active_microphones;
1447 in->stream.set_microphone_direction = in_set_microphone_direction;
1448 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1449
1450 in->handle = handle;
1451
1452 stream_lock_init(&in->lock);
1453
1454 in->adev = (struct audio_device *)hw_dev;
1455
1456 list_init(&in->alsa_devices);
1457 struct alsa_device_info *device_info =
1458 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1459 profile_init(&device_info->profile, PCM_IN);
1460
1461 memset(&in->config, 0, sizeof(in->config));
1462
1463 int ret = 0;
1464 device_lock(in->adev);
1465 int num_open_inputs = in->adev->inputs_open;
1466 device_unlock(in->adev);
1467
1468 /* Check if an input stream is already open */
1469 if (num_open_inputs > 0) {
1470 if (!profile_is_cached_for(&device_info->profile, card, device)) {
1471 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1472 __func__, card, device);
1473 ret = -EINVAL;
1474 }
1475 } else {
1476 /* Read input profile only if necessary */
1477 device_info->profile.card = card;
1478 device_info->profile.device = device;
1479 if (!profile_read_device_info(&device_info->profile)) {
1480 ALOGW("%s fail - cannot read profile", __func__);
1481 ret = -EINVAL;
1482 }
1483 }
1484 if (ret != 0) {
1485 free(in);
1486 *stream_in = NULL;
1487 return ret;
1488 }
1489
1490 /* Rate */
1491 int request_config_rate = config->sample_rate;
1492 if (config->sample_rate == 0) {
1493 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
1494 }
1495
1496 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
1497 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
1498 if (config->sample_rate != in->adev->device_sample_rate) {
1499 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
1500 if (highest_rate == 0) {
1501 ret = -EINVAL; /* error with device */
1502 } else {
1503 in->config.rate = config->sample_rate =
1504 min(highest_rate, in->adev->device_sample_rate);
1505 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
1506 /* Changing the requested rate */
1507 ret = -EINVAL;
1508 } else {
1509 /* Everything AOK! */
1510 ret = 0;
1511 }
1512 }
1513 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
1514 in->config.rate = config->sample_rate;
1515 }
1516 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
1517 in->config.rate = config->sample_rate;
1518 } else {
1519 in->config.rate = config->sample_rate =
1520 profile_get_default_sample_rate(&device_info->profile);
1521 ret = -EINVAL;
1522 }
1523
1524 /* Format */
1525 if (config->format == AUDIO_FORMAT_DEFAULT) {
1526 in->config.format = profile_get_default_format(&device_info->profile);
1527 config->format = audio_format_from_pcm_format(in->config.format);
1528 } else {
1529 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1530 if (profile_is_format_valid(&device_info->profile, fmt)) {
1531 in->config.format = fmt;
1532 } else {
1533 in->config.format = profile_get_default_format(&device_info->profile);
1534 config->format = audio_format_from_pcm_format(in->config.format);
1535 ret = -EINVAL;
1536 }
1537 }
1538
1539 /* Channels */
1540 bool calc_mask = false;
1541 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1542 /* query case */
1543 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
1544 calc_mask = true;
1545 } else {
1546 /* explicit case */
1547 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1548 }
1549
1550 /* The Framework is currently limited to no more than this number of channels */
1551 if (in->hal_channel_count > FCC_LIMIT) {
1552 in->hal_channel_count = FCC_LIMIT;
1553 calc_mask = true;
1554 }
1555
1556 if (calc_mask) {
1557 /* need to calculate the mask from channel count either because this is the query case
1558 * or the specified mask isn't valid for this device, or is more than the FW can handle */
1559 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1560 /* position mask for mono & stereo */
1561 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1562 /* otherwise indexed */
1563 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
1564
1565 // if we change the mask...
1566 if (in->hal_channel_mask != config->channel_mask &&
1567 config->channel_mask != AUDIO_CHANNEL_NONE) {
1568 config->channel_mask = in->hal_channel_mask;
1569 ret = -EINVAL;
1570 }
1571 } else {
1572 in->hal_channel_mask = config->channel_mask;
1573 }
1574
1575 if (ret == 0) {
1576 // Validate the "logical" channel count against support in the "actual" profile.
1577 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1578 // and store THAT in proxy_config.channels
1579 in->config.channels =
1580 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
1581 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config,
1582 false /*require_exact_match*/);
1583 if (ret == 0) {
1584 in->standby = true;
1585
1586 in->conversion_buffer = NULL;
1587 in->conversion_buffer_size = 0;
1588
1589 *stream_in = &in->stream;
1590
1591 /* Save this for adev_dump() */
1592 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1593 } else {
1594 ALOGW("proxy_prepare error %d", ret);
1595 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
1596 config->channel_mask = channel_count <= FCC_2
1597 ? audio_channel_in_mask_from_count(channel_count)
1598 : audio_channel_mask_for_index_assignment_from_count(channel_count);
1599 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1600 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
1601 }
1602 }
1603
1604 if (ret != 0) {
1605 // Deallocate this stream on error, because AudioFlinger won't call
1606 // adev_close_input_stream() in this case.
1607 *stream_in = NULL;
1608 free(in);
1609 return ret;
1610 }
1611
1612 list_add_tail(&in->alsa_devices, &device_info->list_node);
1613
1614 device_lock(in->adev);
1615 ++in->adev->inputs_open;
1616 device_unlock(in->adev);
1617
1618 return ret;
1619 }
1620
adev_close_input_stream(struct audio_hw_device * hw_dev,struct audio_stream_in * stream)1621 static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1622 struct audio_stream_in *stream)
1623 {
1624 struct stream_in *in = (struct stream_in *)stream;
1625
1626 stream_lock(&in->lock);
1627 device_lock(in->adev);
1628 list_remove(&in->list_node);
1629 --in->adev->inputs_open;
1630 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1631 if (device_info != NULL) {
1632 ALOGV("adev_close_input_stream(c:%d d:%d)",
1633 device_info->profile.card, device_info->profile.device);
1634 }
1635 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1636 "invalid inputs_open: %d", in->adev->inputs_open);
1637
1638 stream_standby_l(&in->alsa_devices, &in->standby);
1639
1640 device_unlock(in->adev);
1641
1642 stream_clear_devices(&in->alsa_devices);
1643 stream_unlock(&in->lock);
1644
1645 free(in->conversion_buffer);
1646
1647 free(stream);
1648 }
1649
1650 /*
1651 * ADEV Functions
1652 */
adev_set_parameters(struct audio_hw_device * hw_dev,const char * kvpairs)1653 static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
1654 {
1655 return 0;
1656 }
1657
adev_get_parameters(const struct audio_hw_device * hw_dev,const char * keys)1658 static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
1659 {
1660 return strdup("");
1661 }
1662
adev_init_check(const struct audio_hw_device * hw_dev)1663 static int adev_init_check(const struct audio_hw_device *hw_dev)
1664 {
1665 return 0;
1666 }
1667
adev_set_voice_volume(struct audio_hw_device * hw_dev,float volume)1668 static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
1669 {
1670 return -ENOSYS;
1671 }
1672
adev_set_master_volume(struct audio_hw_device * hw_dev,float volume)1673 static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
1674 {
1675 return -ENOSYS;
1676 }
1677
adev_set_mode(struct audio_hw_device * hw_dev,audio_mode_t mode)1678 static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
1679 {
1680 return 0;
1681 }
1682
adev_set_mic_mute(struct audio_hw_device * hw_dev,bool state)1683 static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
1684 {
1685 struct audio_device * adev = (struct audio_device *)hw_dev;
1686 device_lock(adev);
1687 adev->mic_muted = state;
1688 device_unlock(adev);
1689 return -ENOSYS;
1690 }
1691
adev_get_mic_mute(const struct audio_hw_device * hw_dev,bool * state)1692 static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
1693 {
1694 return -ENOSYS;
1695 }
1696
adev_create_audio_patch(struct audio_hw_device * dev,unsigned int num_sources,const struct audio_port_config * sources,unsigned int num_sinks,const struct audio_port_config * sinks,audio_patch_handle_t * handle)1697 static int adev_create_audio_patch(struct audio_hw_device *dev,
1698 unsigned int num_sources,
1699 const struct audio_port_config *sources,
1700 unsigned int num_sinks,
1701 const struct audio_port_config *sinks,
1702 audio_patch_handle_t *handle) {
1703 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1704 // Only accept mix->device and device->mix cases. In that case, the number of sources
1705 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1706 return -EINVAL;
1707 }
1708
1709 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1710 // If source is a device, the number of sinks should be 1.
1711 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1712 return -EINVAL;
1713 }
1714 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1715 // If source is a mix, all sinks should be device.
1716 for (unsigned int i = 0; i < num_sinks; i++) {
1717 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1718 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1719 return -EINVAL;
1720 }
1721 }
1722 } else {
1723 // All other cases are invalid.
1724 return -EINVAL;
1725 }
1726
1727 struct audio_device* adev = (struct audio_device*) dev;
1728 bool generatedPatchHandle = false;
1729 device_lock(adev);
1730 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1731 *handle = ++adev->next_patch_handle;
1732 generatedPatchHandle = true;
1733 }
1734
1735 int cards[AUDIO_PATCH_PORTS_MAX];
1736 int devices[AUDIO_PATCH_PORTS_MAX];
1737 const struct audio_port_config *port_configs =
1738 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1739 int num_configs = 0;
1740 audio_io_handle_t io_handle = 0;
1741 bool wasStandby = true;
1742 int direction = PCM_OUT;
1743 audio_patch_handle_t *patch_handle = NULL;
1744 struct listnode *alsa_devices = NULL;
1745 struct stream_lock *lock = NULL;
1746 struct pcm_config *config = NULL;
1747 struct stream_in *in = NULL;
1748 struct stream_out *out = NULL;
1749 bool is_bit_perfect = false;
1750
1751 unsigned int num_saved_devices = 0;
1752 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1753 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1754
1755 struct listnode *node;
1756
1757 // Only handle patches for mix->devices and device->mix case.
1758 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1759 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1760 if (in == NULL) {
1761 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1762 device_unlock(adev);
1763 return -EINVAL;
1764 }
1765
1766 direction = PCM_IN;
1767 wasStandby = in->standby;
1768 io_handle = in->handle;
1769 num_configs = num_sources;
1770 patch_handle = &in->patch_handle;
1771 alsa_devices = &in->alsa_devices;
1772 lock = &in->lock;
1773 config = &in->config;
1774 } else {
1775 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1776 if (out == NULL) {
1777 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1778 device_unlock(adev);
1779 return -EINVAL;
1780 }
1781
1782 direction = PCM_OUT;
1783 wasStandby = out->standby;
1784 io_handle = out->handle;
1785 num_configs = num_sinks;
1786 patch_handle = &out->patch_handle;
1787 alsa_devices = &out->alsa_devices;
1788 lock = &out->lock;
1789 config = &out->config;
1790 is_bit_perfect = out->is_bit_perfect;
1791 }
1792
1793 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1794 if (!generatedPatchHandle && *patch_handle != *handle) {
1795 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1796 "with handle(%d) when creating audio patch",
1797 __func__, *handle, *patch_handle, io_handle);
1798 device_unlock(adev);
1799 return -EINVAL;
1800 }
1801 device_unlock(adev);
1802
1803 for (unsigned int i = 0; i < num_configs; ++i) {
1804 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1805 ALOGE("%s, failed to parse card and device %s",
1806 __func__, port_configs[i].ext.device.address);
1807 return -EINVAL;
1808 }
1809 }
1810
1811 stream_lock(lock);
1812 list_for_each (node, alsa_devices) {
1813 struct alsa_device_info *device_info =
1814 node_to_item(node, struct alsa_device_info, list_node);
1815 saved_cards[num_saved_devices] = device_info->profile.card;
1816 saved_devices[num_saved_devices++] = device_info->profile.device;
1817 }
1818
1819 if (are_devices_the_same(
1820 num_configs, cards, devices, num_saved_devices, saved_cards, saved_devices)) {
1821 // The new devices are the same as original ones. No need to update.
1822 stream_unlock(lock);
1823 return 0;
1824 }
1825
1826 device_lock(adev);
1827 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1828 device_unlock(adev);
1829
1830 // Timestamps:
1831 // Audio timestamps assume continuous PCM frame counts which are maintained
1832 // with the device proxy.transferred variable. Technically it would be better
1833 // associated with in or out stream, not the device; here we save and restore
1834 // using the first alsa device as a simplification.
1835 uint64_t saved_transferred_frames = 0;
1836 struct alsa_device_info *device_info = stream_get_first_alsa_device(alsa_devices);
1837 if (device_info != NULL) saved_transferred_frames = device_info->proxy.transferred;
1838
1839 int ret = stream_set_new_devices(
1840 config, alsa_devices, num_configs, cards, devices, direction, is_bit_perfect);
1841
1842 if (ret != 0) {
1843 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1844 stream_set_new_devices(
1845 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction,
1846 is_bit_perfect);
1847 } else {
1848 *patch_handle = *handle;
1849 }
1850
1851 // Timestamps: Restore transferred frames.
1852 if (saved_transferred_frames != 0) {
1853 device_info = stream_get_first_alsa_device(alsa_devices);
1854 if (device_info != NULL) device_info->proxy.transferred = saved_transferred_frames;
1855 }
1856
1857 if (!wasStandby) {
1858 device_lock(adev);
1859 if (in != NULL) {
1860 ret = start_input_stream(in);
1861 if (!ret) {
1862 in->standby = false;
1863 }
1864 }
1865 if (out != NULL) {
1866 ret = start_output_stream(out);
1867 if (!ret) {
1868 out->standby = false;
1869 }
1870 }
1871 device_unlock(adev);
1872 }
1873 stream_unlock(lock);
1874 return ret;
1875 }
1876
adev_release_audio_patch(struct audio_hw_device * dev,audio_patch_handle_t patch_handle)1877 static int adev_release_audio_patch(struct audio_hw_device *dev,
1878 audio_patch_handle_t patch_handle)
1879 {
1880 struct audio_device* adev = (struct audio_device*) dev;
1881
1882 device_lock(adev);
1883 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1884 device_unlock(adev);
1885 if (out != NULL) {
1886 stream_lock(&out->lock);
1887 device_lock(adev);
1888 stream_standby_l(&out->alsa_devices, &out->standby);
1889 device_unlock(adev);
1890 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1891 stream_unlock(&out->lock);
1892 return 0;
1893 }
1894
1895 device_lock(adev);
1896 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1897 device_unlock(adev);
1898 if (in != NULL) {
1899 stream_lock(&in->lock);
1900 device_lock(adev);
1901 stream_standby_l(&in->alsa_devices, &in->standby);
1902 device_unlock(adev);
1903 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1904 stream_unlock(&in->lock);
1905 return 0;
1906 }
1907
1908 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1909 return -EINVAL;
1910 }
1911
adev_get_audio_port(struct audio_hw_device * dev,struct audio_port * port)1912 static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1913 {
1914 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1915 return -EINVAL;
1916 }
1917
1918 alsa_device_profile profile;
1919 const bool is_output = audio_is_output_device(port->ext.device.type);
1920 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1921 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1922 return -EINVAL;
1923 }
1924
1925 if (!profile_read_device_info(&profile)) {
1926 return -ENOENT;
1927 }
1928
1929 port->num_formats = 0;;
1930 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1931 profile.formats[i] != 0; ++i) {
1932 audio_format_t format = audio_format_from(profile.formats[i]);
1933 if (format != AUDIO_FORMAT_INVALID) {
1934 port->formats[port->num_formats++] = format;
1935 }
1936 }
1937
1938 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1939 port->num_channel_masks = populate_channel_mask_from_profile(
1940 &profile, is_output, port->channel_masks);
1941
1942 return 0;
1943 }
1944
adev_get_audio_port_v7(struct audio_hw_device * dev,struct audio_port_v7 * port)1945 static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1946 {
1947 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1948 return -EINVAL;
1949 }
1950
1951 alsa_device_profile profile;
1952 const bool is_output = audio_is_output_device(port->ext.device.type);
1953 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1954 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1955 return -EINVAL;
1956 }
1957
1958 if (!profile_read_device_info(&profile)) {
1959 return -ENOENT;
1960 }
1961
1962 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1963 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1964 &profile, is_output, channel_masks);
1965 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1966 const unsigned int num_sample_rates =
1967 populate_sample_rates_from_profile(&profile, sample_rates);
1968 port->num_audio_profiles = 0;;
1969 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1970 profile.formats[i] != 0; ++i) {
1971 audio_format_t format = audio_format_from(profile.formats[i]);
1972 if (format == AUDIO_FORMAT_INVALID) {
1973 continue;
1974 }
1975 const unsigned int j = port->num_audio_profiles++;
1976 port->audio_profiles[j].format = format;
1977 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1978 memcpy(port->audio_profiles[j].sample_rates,
1979 sample_rates,
1980 num_sample_rates * sizeof(unsigned int));
1981 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1982 memcpy(port->audio_profiles[j].channel_masks,
1983 channel_masks,
1984 num_channel_masks* sizeof(audio_channel_mask_t));
1985 }
1986
1987 return 0;
1988 }
1989
adev_dump(const struct audio_hw_device * device,int fd)1990 static int adev_dump(const struct audio_hw_device *device, int fd)
1991 {
1992 dprintf(fd, "\nUSB audio module:\n");
1993
1994 struct audio_device* adev = (struct audio_device*)device;
1995 const int kNumRetries = 3;
1996 const int kSleepTimeMS = 500;
1997
1998 // use device_try_lock() in case we dumpsys during a deadlock
1999 int retry = kNumRetries;
2000 while (retry > 0 && device_try_lock(adev) != 0) {
2001 sleep(kSleepTimeMS);
2002 retry--;
2003 }
2004
2005 if (retry > 0) {
2006 if (list_empty(&adev->output_stream_list)) {
2007 dprintf(fd, " No output streams.\n");
2008 } else {
2009 struct listnode* node;
2010 list_for_each(node, &adev->output_stream_list) {
2011 struct audio_stream* stream =
2012 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
2013 out_dump(stream, fd);
2014 }
2015 }
2016
2017 if (list_empty(&adev->input_stream_list)) {
2018 dprintf(fd, "\n No input streams.\n");
2019 } else {
2020 struct listnode* node;
2021 list_for_each(node, &adev->input_stream_list) {
2022 struct audio_stream* stream =
2023 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
2024 in_dump(stream, fd);
2025 }
2026 }
2027
2028 device_unlock(adev);
2029 } else {
2030 // Couldn't lock
2031 dprintf(fd, " Could not obtain device lock.\n");
2032 }
2033
2034 return 0;
2035 }
2036
adev_close(hw_device_t * device)2037 static int adev_close(hw_device_t *device)
2038 {
2039 free(device);
2040
2041 return 0;
2042 }
2043
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)2044 static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
2045 {
2046 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
2047 return -EINVAL;
2048
2049 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
2050 if (!adev)
2051 return -ENOMEM;
2052
2053 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2054
2055 list_init(&adev->output_stream_list);
2056 list_init(&adev->input_stream_list);
2057
2058 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
2059 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
2060 adev->hw_device.common.module = (struct hw_module_t *)module;
2061 adev->hw_device.common.close = adev_close;
2062
2063 adev->hw_device.init_check = adev_init_check;
2064 adev->hw_device.set_voice_volume = adev_set_voice_volume;
2065 adev->hw_device.set_master_volume = adev_set_master_volume;
2066 adev->hw_device.set_mode = adev_set_mode;
2067 adev->hw_device.set_mic_mute = adev_set_mic_mute;
2068 adev->hw_device.get_mic_mute = adev_get_mic_mute;
2069 adev->hw_device.set_parameters = adev_set_parameters;
2070 adev->hw_device.get_parameters = adev_get_parameters;
2071 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
2072 adev->hw_device.open_output_stream = adev_open_output_stream;
2073 adev->hw_device.close_output_stream = adev_close_output_stream;
2074 adev->hw_device.open_input_stream = adev_open_input_stream;
2075 adev->hw_device.close_input_stream = adev_close_input_stream;
2076 adev->hw_device.create_audio_patch = adev_create_audio_patch;
2077 adev->hw_device.release_audio_patch = adev_release_audio_patch;
2078 adev->hw_device.get_audio_port = adev_get_audio_port;
2079 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
2080 adev->hw_device.dump = adev_dump;
2081
2082 *device = &adev->hw_device.common;
2083
2084 return 0;
2085 }
2086
2087 static struct hw_module_methods_t hal_module_methods = {
2088 .open = adev_open,
2089 };
2090
2091 struct audio_module HAL_MODULE_INFO_SYM = {
2092 .common = {
2093 .tag = HARDWARE_MODULE_TAG,
2094 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2095 .hal_api_version = HARDWARE_HAL_API_VERSION,
2096 .id = AUDIO_HARDWARE_MODULE_ID,
2097 .name = "USB audio HW HAL",
2098 .author = "The Android Open Source Project",
2099 .methods = &hal_module_methods,
2100 },
2101 };
2102