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