blob: f21dcd7e6ab5bb2ba7e6e7723426b2cc975bb326 [file] [log] [blame]
Simon Wilson19957a32012-04-06 16:17:12 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Paul McLean9ab869a2015-01-13 09:37:06 -080017#define LOG_TAG "modules.usbaudio.audio_hal"
Paul McLeancf5310a2018-08-22 14:33:12 -070018/* #define LOG_NDEBUG 0 */
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070021#include <inttypes.h>
jiabin016fa212022-12-15 21:54:59 +000022#include <math.h>
Simon Wilson19957a32012-04-06 16:17:12 -070023#include <pthread.h>
24#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070025#include <stdlib.h>
jiabin400bbe02020-10-28 13:56:59 -070026#include <string.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070027#include <sys/time.h>
Tri Vo00a86732017-06-27 11:33:36 -070028#include <unistd.h>
Simon Wilson19957a32012-04-06 16:17:12 -070029
Mark Salyzyn88e458a2014-04-28 12:30:44 -070030#include <log/log.h>
Paul McLean6a75e4e2016-05-25 14:09:02 -060031#include <cutils/list.h>
Simon Wilson19957a32012-04-06 16:17:12 -070032#include <cutils/str_parms.h>
33#include <cutils/properties.h>
34
Simon Wilson19957a32012-04-06 16:17:12 -070035#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070036#include <hardware/audio_alsaops.h>
37#include <hardware/hardware.h>
38
39#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070040
41#include <tinyalsa/asoundlib.h>
42
Paul McLeanc2201152014-07-16 13:46:07 -070043#include <audio_utils/channels.h>
44
Paul McLeanc88e6ae2014-07-16 09:48:34 -070045#include "alsa_device_profile.h"
46#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080047#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070048
Paul McLean1d585cc2016-05-24 11:28:10 -060049/* Lock play & record samples rates at or above this threshold */
50#define RATELOCK_THRESHOLD 96000
51
Paul McLeancfdbd6b2018-07-27 11:16:02 -060052#define max(a, b) ((a) > (b) ? (a) : (b))
53#define min(a, b) ((a) < (b) ? (a) : (b))
54
Simon Wilson19957a32012-04-06 16:17:12 -070055struct audio_device {
56 struct audio_hw_device hw_device;
57
58 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080059
60 /* output */
Paul McLean6a75e4e2016-05-25 14:09:02 -060061 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080062
63 /* input */
Paul McLean6a75e4e2016-05-25 14:09:02 -060064 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080065
Paul McLean1d585cc2016-05-24 11:28:10 -060066 /* lock input & output sample rates */
67 /*FIXME - How do we address multiple output streams? */
Paul McLeancfdbd6b2018-07-27 11:16:02 -060068 uint32_t device_sample_rate; // this should be a rate that is common to both input & output
Paul McLean1d585cc2016-05-24 11:28:10 -060069
Eric Laurent253def92014-09-14 12:18:18 -070070 bool mic_muted;
71
Andy Hungb10ce6d2017-10-27 19:37:58 -070072 int32_t inputs_open; /* number of input streams currently open. */
jiabin400bbe02020-10-28 13:56:59 -070073
74 audio_patch_handle_t next_patch_handle; // Increase 1 when create audio patch
Simon Wilson19957a32012-04-06 16:17:12 -070075};
76
Paul McLean994ac072016-06-02 15:33:24 -060077struct 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
jiabin8b265c92020-10-21 15:16:40 -070082struct 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
Simon Wilson19957a32012-04-06 16:17:12 -070088struct stream_out {
89 struct audio_stream_out stream;
90
Paul McLeancf5310a2018-08-22 14:33:12 -070091 struct stream_lock lock;
Paul McLean994ac072016-06-02 15:33:24 -060092
Paul McLeaneedc92e2013-12-19 15:46:15 -080093 bool standby;
94
Paul McLean76dba682016-06-01 12:29:11 -060095 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070096
jiabin8b265c92020-10-21 15:16:40 -070097 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
Paul McLeaneedc92e2013-12-19 15:46:15 -080098
Andy Hung03576be2014-07-21 21:16:45 -070099 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. */
Paul McLean64345f82016-06-06 13:26:05 -0600104 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 */
Andy Hung182ddc72015-05-05 23:37:30 -0700109
Paul McLean6a75e4e2016-05-25 14:09:02 -0600110 struct listnode list_node;
111
Paul McLeaneedc92e2013-12-19 15:46:15 -0800112 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 */
jiabin400bbe02020-10-28 13:56:59 -0700116
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
jiabin016fa212022-12-15 21:54:59 +0000122
jiabinfea9e1b2022-12-19 19:31:36 +0000123 bool is_bit_perfect; // True if the stream is open with bit-perfect output flag
124
jiabin016fa212022-12-15 21:54:59 +0000125 // 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;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800131};
132
Paul McLeaneedc92e2013-12-19 15:46:15 -0800133struct stream_in {
134 struct audio_stream_in stream;
135
Paul McLean994ac072016-06-02 15:33:24 -0600136 struct stream_lock lock;
137
Simon Wilson19957a32012-04-06 16:17:12 -0700138 bool standby;
139
Paul McLean76dba682016-06-01 12:29:11 -0600140 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800141
jiabin8b265c92020-10-21 15:16:40 -0700142 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700143
Paul McLean2cfd81b2014-09-15 12:32:23 -0700144 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. */
Paul McLean64345f82016-06-06 13:26:05 -0600149 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 */
Andy Hung780f1f82015-04-22 22:14:39 -0700154
Paul McLean6a75e4e2016-05-25 14:09:02 -0600155 struct listnode list_node;
156
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700157 /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
Paul McLean30f41852014-04-16 15:44:20 -0700158 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 */
jiabin400bbe02020-10-28 13:56:59 -0700162
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
Simon Wilson19957a32012-04-06 16:17:12 -0700168};
169
jiabin400bbe02020-10-28 13:56:59 -0700170// Map channel count to output channel mask
Andy Hungdf511202021-06-07 16:45:03 -0700171static 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,
jiabin400bbe02020-10-28 13:56:59 -0700191};
192static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP);
193
194// Map channel count to input channel mask
195static 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};
201static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP);
202
203// Map channel count to index mask
Andy Hungdf511202021-06-07 16:45:03 -0700204static 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,
jiabin400bbe02020-10-28 13:56:59 -0700233};
234static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP);
235
jiabin016fa212022-12-15 21:54:59 +0000236static const char* ALL_VOLUME_CONTROL_NAMES[] = {
237 "PCM Playback Volume",
238 "Headset Playback Volume",
239 "Headphone Playback Volume",
240 "Master Playback Volume",
241};
242static const int VOLUME_CONTROL_NAMES_NUM = AUDIO_ARRAY_SIZE(ALL_VOLUME_CONTROL_NAMES);
243
Paul McLean994ac072016-06-02 15:33:24 -0600244/*
245 * Locking Helpers
246 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800247/*
Eric Laurent70318092015-06-19 17:49:17 -0700248 * 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
Paul McLean994ac072016-06-02 15:33:24 -0600254static 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
259static void stream_lock(struct stream_lock *lock) {
jiabin400bbe02020-10-28 13:56:59 -0700260 if (lock == NULL) {
261 return;
262 }
Paul McLean994ac072016-06-02 15:33:24 -0600263 pthread_mutex_lock(&lock->pre_lock);
264 pthread_mutex_lock(&lock->lock);
265 pthread_mutex_unlock(&lock->pre_lock);
266}
267
268static void stream_unlock(struct stream_lock *lock) {
269 pthread_mutex_unlock(&lock->lock);
270}
271
272static void device_lock(struct audio_device *adev) {
273 pthread_mutex_lock(&adev->lock);
274}
275
276static int device_try_lock(struct audio_device *adev) {
277 return pthread_mutex_trylock(&adev->lock);
278}
279
280static void device_unlock(struct audio_device *adev) {
281 pthread_mutex_unlock(&adev->lock);
282}
283
284/*
285 * streams list management
286 */
287static 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
jiabin400bbe02020-10-28 13:56:59 -0700296static 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}
Paul McLean994ac072016-06-02 15:33:24 -0600307
jiabin400bbe02020-10-28 13:56:59 -0700308static 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}
Paul McLean994ac072016-06-02 15:33:24 -0600319
jiabin400bbe02020-10-28 13:56:59 -0700320static 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
332static 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;
Paul McLean994ac072016-06-02 15:33:24 -0600342}
343
Eric Laurent70318092015-06-19 17:49:17 -0700344/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700345 * 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.
Paul McLean65ec72b2015-02-18 09:13:24 -0800352 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700353 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800354static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700355{
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);
Paul McLean65ec72b2015-02-18 09:13:24 -0800375
376 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700377}
378
Andy Hung4e6a1c02017-10-27 20:31:46 -0700379static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800380{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700381 if (profile->card < 0 || profile->device < 0) {
382 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800383 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700384
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700385 struct str_parms *query = str_parms_create_str(keys);
386 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700387
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700388 /* 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);
Paul McLeane32cbc12014-06-25 10:42:07 -0700395 }
396
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700397 /* 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);
Paul McLeane32cbc12014-06-25 10:42:07 -0700403 }
404
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700405 /* 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);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700411 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700412 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700413
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700414 char* result_str = str_parms_to_str(result);
415 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700416
Paul McLean65ec72b2015-02-18 09:13:24 -0800417 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700418
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700419 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800420}
421
jiabin400bbe02020-10-28 13:56:59 -0700422static 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
jiabina635fcf2021-01-11 11:34:50 -0800440static 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;
Andy Hungdf511202021-06-07 16:45:03 -0700447 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 }
jiabina635fcf2021-01-11 11:34:50 -0800451 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
470static 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
jiabin4bc5f252021-06-02 18:31:13 +0000481static 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
501static 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
jiabin016fa212022-12-15 21:54:59 +0000516static 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
Paul McLeaneedc92e2013-12-19 15:46:15 -0800555/*
556 * HAl Functions
557 */
Simon Wilson19957a32012-04-06 16:17:12 -0700558/**
559 * NOTE: when multiple mutexes have to be acquired, always respect the
560 * following order: hw device > out stream
561 */
562
jiabin8b265c92020-10-21 15:16:40 -0700563static 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
jiabin400bbe02020-10-28 13:56:59 -0700570/**
571 * Must be called with holding the stream's lock.
572 */
573static 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
586static 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
599static 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[],
jiabinfea9e1b2022-12-19 19:31:36 +0000604 int direction,
605 bool is_bit_perfect)
jiabin400bbe02020-10-28 13:56:59 -0700606{
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 }
jiabinfea9e1b2022-12-19 19:31:36 +0000622 status = proxy_prepare(&device_info->proxy, &device_info->profile, config, is_bit_perfect);
jiabin400bbe02020-10-28 13:56:59 -0700623 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
631exit:
632 if (status != 0) {
633 stream_clear_devices(alsa_devices);
634 }
635 return status;
636}
637
jiabin8b265c92020-10-21 15:16:40 -0700638static 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);
jiabin0269a9d2021-06-02 20:27:38 +0000644 const char* direction = device_info->profile.direction == PCM_OUT ? "Output" : "Input";
645 dprintf(fd, "%s Profile %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700646 profile_dump(&device_info->profile, fd);
647
jiabin0269a9d2021-06-02 20:27:38 +0000648 dprintf(fd, "%s Proxy %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700649 proxy_dump(&device_info->proxy, fd);
650 }
651}
652
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700653/*
654 * OUT functions
655 */
Simon Wilson19957a32012-04-06 16:17:12 -0700656static uint32_t out_get_sample_rate(const struct audio_stream *stream)
657{
jiabin8b265c92020-10-21 15:16:40 -0700658 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);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700665 ALOGV("out_get_sample_rate() = %d", rate);
666 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700667}
668
669static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
670{
671 return 0;
672}
673
674static size_t out_get_buffer_size(const struct audio_stream *stream)
675{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700676 const struct stream_out* out = (const struct stream_out*)stream;
jiabin8b265c92020-10-21 15:16:40 -0700677 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));
Simon Wilson19957a32012-04-06 16:17:12 -0700683}
684
685static uint32_t out_get_channels(const struct audio_stream *stream)
686{
Andy Hung03576be2014-07-21 21:16:45 -0700687 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700688 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700689}
690
691static audio_format_t out_get_format(const struct audio_stream *stream)
692{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700693 /* 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 */
jiabin8b265c92020-10-21 15:16:40 -0700697 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));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700704 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700705}
706
707static int out_set_format(struct audio_stream *stream, audio_format_t format)
708{
709 return 0;
710}
711
712static int out_standby(struct audio_stream *stream)
713{
714 struct stream_out *out = (struct stream_out *)stream;
715
Paul McLean994ac072016-06-02 15:33:24 -0600716 stream_lock(&out->lock);
jiabin400bbe02020-10-28 13:56:59 -0700717 device_lock(out->adev);
718 stream_standby_l(&out->alsa_devices, &out->standby);
719 device_unlock(out->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600720 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700721 return 0;
722}
723
Paul McLean6a75e4e2016-05-25 14:09:02 -0600724static 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) {
jiabin8b265c92020-10-21 15:16:40 -0700728 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600729 }
730
Simon Wilson19957a32012-04-06 16:17:12 -0700731 return 0;
732}
733
jiabin400bbe02020-10-28 13:56:59 -0700734static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -0700735{
Paul McLean65ec72b2015-02-18 09:13:24 -0800736 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800737
jiabin400bbe02020-10-28 13:56:59 -0700738 // 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;
Simon Wilson19957a32012-04-06 16:17:12 -0700742}
743
Paul McLeanf62d75e2014-07-11 15:14:19 -0700744static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
745{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700746 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600747 stream_lock(&out->lock);
jiabin8b265c92020-10-21 15:16:40 -0700748 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 }
Paul McLean994ac072016-06-02 15:33:24 -0600753 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700754 return params_str;
755}
756
Simon Wilson19957a32012-04-06 16:17:12 -0700757static uint32_t out_get_latency(const struct audio_stream_out *stream)
758{
jiabin8b265c92020-10-21 15:16:40 -0700759 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);
Simon Wilson19957a32012-04-06 16:17:12 -0700766}
767
Paul McLean30f41852014-04-16 15:44:20 -0700768static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700769{
jiabin016fa212022-12-15 21:54:59 +0000770 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;
Simon Wilson19957a32012-04-06 16:17:12 -0700795}
796
Paul McLeaneedc92e2013-12-19 15:46:15 -0800797/* must be called with hw device and output stream mutexes locked */
798static int start_output_stream(struct stream_out *out)
799{
jiabin8b265c92020-10-21 15:16:40 -0700800 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 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800814
jiabin8b265c92020-10-21 15:16:40 -0700815exit:
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;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800825}
826
827static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700828{
829 int ret;
830 struct stream_out *out = (struct stream_out *)stream;
831
Paul McLean994ac072016-06-02 15:33:24 -0600832 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700833 if (out->standby) {
834 ret = start_output_stream(out);
835 if (ret != 0) {
836 goto err;
837 }
838 out->standby = false;
839 }
Eric Laurent05333d42014-08-04 20:29:17 -0700840
jiabin8b265c92020-10-21 15:16:40 -0700841 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;
Andy Hung03576be2014-07-21 21:16:45 -0700867 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800868
jiabin8b265c92020-10-21 15:16:40 -0700869 if (write_buff != NULL && num_write_buff_bytes != 0) {
870 proxy_write(proxy, write_buff, num_write_buff_bytes);
871 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800872 }
Simon Wilson19957a32012-04-06 16:17:12 -0700873
Paul McLean994ac072016-06-02 15:33:24 -0600874 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700875
876 return bytes;
877
878err:
Paul McLean994ac072016-06-02 15:33:24 -0600879 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700880 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700881 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700882 out_get_sample_rate(&stream->common));
883 }
884
885 return bytes;
886}
887
Paul McLean30f41852014-04-16 15:44:20 -0700888static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700889{
890 return -EINVAL;
891}
892
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700893static int out_get_presentation_position(const struct audio_stream_out *stream,
894 uint64_t *frames, struct timespec *timestamp)
895{
Andy Hungc9515ce2015-08-04 15:05:19 -0700896 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600897 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700898
jiabin8b265c92020-10-21 15:16:40 -0700899 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);
Paul McLean994ac072016-06-02 15:33:24 -0600902 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700903 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700904}
905
Simon Wilson19957a32012-04-06 16:17:12 -0700906static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
907{
908 return 0;
909}
910
911static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
912{
913 return 0;
914}
915
Paul McLean30f41852014-04-16 15:44:20 -0700916static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700917{
918 return -EINVAL;
919}
920
Paul McLean76dba682016-06-01 12:29:11 -0600921static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700922 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600923 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700924 audio_output_flags_t flags,
925 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700926 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700927 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700928{
Paul McLean76dba682016-06-01 12:29:11 -0600929 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
930 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800931
jiabinfea9e1b2022-12-19 19:31:36 +0000932 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
Simon Wilson19957a32012-04-06 16:17:12 -0700942 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700943
944 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600945 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700946 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600947 }
Simon Wilson19957a32012-04-06 16:17:12 -0700948
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700949 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700950 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;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700966 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700967 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
968
jiabin400bbe02020-10-28 13:56:59 -0700969 out->handle = handle;
970
Paul McLean994ac072016-06-02 15:33:24 -0600971 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700972
Paul McLean76dba682016-06-01 12:29:11 -0600973 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700974
jiabin8b265c92020-10-21 15:16:40 -0700975 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);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700979
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700980 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700981 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800982
Paul McLean0f1753e2014-12-02 12:36:45 -0700983 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700984 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700985
jiabin8b265c92020-10-21 15:16:40 -0700986 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700987
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700988 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800989
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700990 /* Rate */
991 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700992 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)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700994 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800995 } else {
jiabinfea9e1b2022-12-19 19:31:36 +0000996 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 }
jiabin8b265c92020-10-21 15:16:40 -07001002 proxy_config.rate = config->sample_rate =
1003 profile_get_default_sample_rate(&device_info->profile);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001004 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001005
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001006 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -07001007 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -06001008 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -06001009 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -06001010
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001011 /* Format */
1012 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -07001013 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001014 config->format = audio_format_from_pcm_format(proxy_config.format);
1015 } else {
1016 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001017 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001018 proxy_config.format = fmt;
1019 } else {
jiabinfea9e1b2022-12-19 19:31:36 +00001020 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 }
jiabin8b265c92020-10-21 15:16:40 -07001026 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001027 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001028 }
1029 }
1030
1031 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001032 bool calc_mask = false;
1033 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1034 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001035 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001036 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -07001037 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001038 /* explicit case */
1039 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -07001040 }
Paul McLean698dbd72015-11-03 12:24:30 -08001041
Paul McLean64345f82016-06-06 13:26:05 -06001042 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001043 if (out->hal_channel_count > FCC_LIMIT) {
1044 out->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001045 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
jiabin8b265c92020-10-21 15:16:40 -07001050 * or the specified mask isn't valid for this device, or is more than the FW can handle */
Paul McLean64345f82016-06-06 13:26:05 -06001051 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -07001052 /* 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);
Paul McLean64345f82016-06-06 13:26:05 -06001056 }
1057
Andy Hung182ddc72015-05-05 23:37:30 -07001058 out->hal_channel_mask = config->channel_mask;
1059
Paul McLean64345f82016-06-06 13:26:05 -06001060 // 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
Paul McLeancf5310a2018-08-22 14:33:12 -07001063 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001064 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
jiabinfea9e1b2022-12-19 19:31:36 +00001065 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 }
jiabin400bbe02020-10-28 13:56:59 -07001076 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -07001077
1078 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001079
jiabin016fa212022-12-15 21:54:59 +00001080 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
Paul McLean96c4d302017-12-05 09:50:26 -08001084 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
1085 * So clear any errors that may have occurred above.
1086 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001087 ret = 0;
1088
Paul McLeaneedc92e2013-12-19 15:46:15 -08001089 out->conversion_buffer = NULL;
1090 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001091
1092 out->standby = true;
1093
Paul McLean6a75e4e2016-05-25 14:09:02 -06001094 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -06001095 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001096
Simon Wilson19957a32012-04-06 16:17:12 -07001097 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001098
1099 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001100}
1101
Paul McLean76dba682016-06-01 12:29:11 -06001102static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -07001103 struct audio_stream_out *stream)
1104{
1105 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001106
jiabin400bbe02020-10-28 13:56:59 -07001107 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001108 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -07001109 stream_standby_l(&out->alsa_devices, &out->standby);
1110 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001111
1112 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001113
Paul McLeaneedc92e2013-12-19 15:46:15 -08001114 out->conversion_buffer = NULL;
1115 out->conversion_buffer_size = 0;
1116
jiabin016fa212022-12-15 21:54:59 +00001117 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
Paul McLean994ac072016-06-02 15:33:24 -06001128 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001129 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -06001130 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -06001131 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001132 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -06001133
Simon Wilson19957a32012-04-06 16:17:12 -07001134 free(stream);
1135}
1136
Paul McLean76dba682016-06-01 12:29:11 -06001137static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001138 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 */
1147static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1148{
jiabin8b265c92020-10-21 15:16:40 -07001149 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);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001156 ALOGV("in_get_sample_rate() = %d", rate);
1157 return rate;
1158}
1159
1160static 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
1166static size_t in_get_buffer_size(const struct audio_stream *stream)
1167{
1168 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -07001169 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));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001175}
1176
1177static uint32_t in_get_channels(const struct audio_stream *stream)
1178{
Paul McLean2cfd81b2014-09-15 12:32:23 -07001179 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -07001180 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001181}
1182
1183static audio_format_t in_get_format(const struct audio_stream *stream)
1184{
jiabin8b265c92020-10-21 15:16:40 -07001185 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;
Andy Hung780f1f82015-04-22 22:14:39 -07001192 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1193 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001194}
1195
1196static 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
1203static int in_standby(struct audio_stream *stream)
1204{
1205 struct stream_in *in = (struct stream_in *)stream;
1206
Paul McLean994ac072016-06-02 15:33:24 -06001207 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -07001208 device_lock(in->adev);
1209 stream_standby_l(&in->alsa_devices, &in->standby);
1210 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -06001211 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001212 return 0;
1213}
1214
1215static int in_dump(const struct audio_stream *stream, int fd)
1216{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001217 const struct stream_in* in_stream = (const struct stream_in*)stream;
1218 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -07001219 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001220 }
1221
1222 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001223}
1224
1225static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1226{
Paul McLean65ec72b2015-02-18 09:13:24 -08001227 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001228
jiabin400bbe02020-10-28 13:56:59 -07001229 // 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;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001233}
1234
1235static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1236{
1237 struct stream_in *in = (struct stream_in *)stream;
1238
Paul McLean994ac072016-06-02 15:33:24 -06001239 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001240 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 }
Paul McLean994ac072016-06-02 15:33:24 -06001245 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001246
1247 return params_str;
1248}
1249
1250static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1251{
1252 return 0;
1253}
1254
1255static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1256{
1257 return 0;
1258}
1259
1260static 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 */
1266static int start_input_stream(struct stream_in *in)
1267{
jiabin8b265c92020-10-21 15:16:40 -07001268 // 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 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001273
jiabin8b265c92020-10-21 15:16:40 -07001274 ALOGV("start_input_stream(card:%d device:%d)",
1275 device_info->profile.card, device_info->profile.device);
1276 return proxy_open(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001277}
1278
1279/* TODO mutex stuff here (see out_write) */
1280static 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;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001285 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001286
1287 struct stream_in * in = (struct stream_in *)stream;
1288
Paul McLean994ac072016-06-02 15:33:24 -06001289 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001290 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001291 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001292 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001293 goto err;
1294 }
1295 in->standby = false;
1296 }
1297
jiabin8b265c92020-10-21 15:16:40 -07001298 // 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
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001304 /*
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;
jiabin8b265c92020-10-21 15:16:40 -07001309 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001310 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001311
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
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001316 /* 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
jiabin8b265c92020-10-21 15:16:40 -07001327 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001328 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001329 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 }
Eric Laurent253def92014-09-14 12:18:18 -07001344
Paul McLean76dba682016-06-01 12:29:11 -06001345 /* 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)
Eric Laurent253def92014-09-14 12:18:18 -07001347 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301348 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001349 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001350 }
1351
1352err:
Paul McLean994ac072016-06-02 15:33:24 -06001353 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001354 return num_read_buff_bytes;
1355}
1356
1357static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1358{
1359 return 0;
1360}
1361
Andy Hungfa6b4a62018-06-04 19:14:22 -07001362static 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
jiabin8b265c92020-10-21 15:16:40 -07001368 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);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001372
1373 stream_unlock(&in->lock);
1374 return ret;
1375}
1376
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001377static 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
1387static 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
1395static 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
Paul McLean76dba682016-06-01 12:29:11 -06001401static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001402 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001403 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001404 struct audio_config *config,
1405 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001406 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001407 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001408 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001409{
Paul McLean1d585cc2016-05-24 11:28:10 -06001410 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001411 config->sample_rate, config->channel_mask, config->format);
1412
Andy Hungb10ce6d2017-10-27 19:37:58 -07001413 /* 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 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001420
Andy Hungb10ce6d2017-10-27 19:37:58 -07001421 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001422 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001423 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001424 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001425 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001426
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;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001444 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001445
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001446 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
jiabin400bbe02020-10-28 13:56:59 -07001450 in->handle = handle;
1451
Paul McLean994ac072016-06-02 15:33:24 -06001452 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001453
Paul McLean76dba682016-06-01 12:29:11 -06001454 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001455
jiabin8b265c92020-10-21 15:16:40 -07001456 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);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001460
jiabin400bbe02020-10-28 13:56:59 -07001461 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001462
Andy Hungb10ce6d2017-10-27 19:37:58 -07001463 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001464 device_lock(in->adev);
1465 int num_open_inputs = in->adev->inputs_open;
1466 device_unlock(in->adev);
1467
Andy Hungb10ce6d2017-10-27 19:37:58 -07001468 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001469 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001470 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001471 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 */
jiabin8b265c92020-10-21 15:16:40 -07001477 device_info->profile.card = card;
1478 device_info->profile.device = device;
1479 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001480 ALOGW("%s fail - cannot read profile", __func__);
1481 ret = -EINVAL;
1482 }
1483 }
1484 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001485 free(in);
1486 *stream_in = NULL;
1487 return ret;
1488 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001489
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001490 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001491 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001492 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001493 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001494 }
1495
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001496 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001497 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001498 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001499 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001500 if (highest_rate == 0) {
1501 ret = -EINVAL; /* error with device */
1502 } else {
jiabin400bbe02020-10-28 13:56:59 -07001503 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001504 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001505 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001506 /* Changing the requested rate */
1507 ret = -EINVAL;
1508 } else {
1509 /* Everything AOK! */
1510 ret = 0;
1511 }
1512 }
jiabinabf24d32023-05-16 23:20:17 +00001513 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
1514 in->config.rate = config->sample_rate;
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001515 }
jiabin8b265c92020-10-21 15:16:40 -07001516 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001517 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001518 } else {
jiabin400bbe02020-10-28 13:56:59 -07001519 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001520 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001521 ret = -EINVAL;
1522 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001523
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001524 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001525 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001526 in->config.format = profile_get_default_format(&device_info->profile);
1527 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001528 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001529 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001530 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001531 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001532 } else {
jiabin400bbe02020-10-28 13:56:59 -07001533 in->config.format = profile_get_default_format(&device_info->profile);
1534 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001535 ret = -EINVAL;
1536 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001537 }
1538
Paul McLean2cfd81b2014-09-15 12:32:23 -07001539 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001540 bool calc_mask = false;
1541 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1542 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001543 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001544 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001545 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001546 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001547 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1548 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001549
Paul McLean64345f82016-06-06 13:26:05 -06001550 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001551 if (in->hal_channel_count > FCC_LIMIT) {
1552 in->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001553 calc_mask = true;
1554 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001555
Paul McLean64345f82016-06-06 13:26:05 -06001556 if (calc_mask) {
1557 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001558 * or the specified mask isn't valid for this device, or is more than the FW can handle */
Paul McLean64345f82016-06-06 13:26:05 -06001559 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);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001564
Paul McLean64345f82016-06-06 13:26:05 -06001565 // 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 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001574
Paul McLean64345f82016-06-06 13:26:05 -06001575 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
jiabin400bbe02020-10-28 13:56:59 -07001579 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001580 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabinfea9e1b2022-12-19 19:31:36 +00001581 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config,
1582 false /*require_exact_match*/);
Eric Laurent981f7742017-05-03 12:44:26 -07001583 if (ret == 0) {
1584 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001585
Eric Laurent981f7742017-05-03 12:44:26 -07001586 in->conversion_buffer = NULL;
1587 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001588
Eric Laurent981f7742017-05-03 12:44:26 -07001589 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001590
Eric Laurent981f7742017-05-03 12:44:26 -07001591 /* 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);
jiabin8b265c92020-10-21 15:16:40 -07001595 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001596 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);
jiabin8b265c92020-10-21 15:16:40 -07001599 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1600 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001601 }
1602 }
Paul McLean64345f82016-06-06 13:26:05 -06001603
Eric Laurent981f7742017-05-03 12:44:26 -07001604 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001605 // 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);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001609 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001610 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001611
jiabin8b265c92020-10-21 15:16:40 -07001612 list_add_tail(&in->alsa_devices, &device_info->list_node);
1613
Andy Hungb10ce6d2017-10-27 19:37:58 -07001614 device_lock(in->adev);
1615 ++in->adev->inputs_open;
1616 device_unlock(in->adev);
1617
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001618 return ret;
1619}
1620
Paul McLean76dba682016-06-01 12:29:11 -06001621static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1622 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001623{
1624 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001625
jiabin400bbe02020-10-28 13:56:59 -07001626 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001627 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001628 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001629 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001630 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 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001635 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1636 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001637
1638 stream_standby_l(&in->alsa_devices, &in->standby);
1639
Andy Hungb10ce6d2017-10-27 19:37:58 -07001640 device_unlock(in->adev);
1641
jiabin400bbe02020-10-28 13:56:59 -07001642 stream_clear_devices(&in->alsa_devices);
1643 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001644
1645 free(in->conversion_buffer);
1646
1647 free(stream);
1648}
1649
1650/*
1651 * ADEV Functions
1652 */
Paul McLean76dba682016-06-01 12:29:11 -06001653static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001654{
1655 return 0;
1656}
1657
Paul McLean76dba682016-06-01 12:29:11 -06001658static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001659{
1660 return strdup("");
1661}
1662
Paul McLean76dba682016-06-01 12:29:11 -06001663static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001664{
1665 return 0;
1666}
1667
Paul McLean76dba682016-06-01 12:29:11 -06001668static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001669{
1670 return -ENOSYS;
1671}
1672
Paul McLean76dba682016-06-01 12:29:11 -06001673static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001674{
1675 return -ENOSYS;
1676}
1677
Paul McLean76dba682016-06-01 12:29:11 -06001678static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001679{
1680 return 0;
1681}
1682
Paul McLean76dba682016-06-01 12:29:11 -06001683static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001684{
Paul McLean76dba682016-06-01 12:29:11 -06001685 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001686 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001687 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001688 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001689 return -ENOSYS;
1690}
1691
Paul McLean76dba682016-06-01 12:29:11 -06001692static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001693{
1694 return -ENOSYS;
1695}
1696
jiabin400bbe02020-10-28 13:56:59 -07001697static 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;
jiabinfea9e1b2022-12-19 19:31:36 +00001749 bool is_bit_perfect = false;
jiabin400bbe02020-10-28 13:56:59 -07001750
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;
jiabinfea9e1b2022-12-19 19:31:36 +00001790 is_bit_perfect = out->is_bit_perfect;
jiabin400bbe02020-10-28 13:56:59 -07001791 }
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
jiabin4bc5f252021-06-02 18:31:13 +00001819 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
jiabin400bbe02020-10-28 13:56:59 -07001826 device_lock(adev);
1827 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1828 device_unlock(adev);
1829
Andy Hung61583422021-07-02 16:13:11 -07001830 // 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
jiabinfea9e1b2022-12-19 19:31:36 +00001839 int ret = stream_set_new_devices(
1840 config, alsa_devices, num_configs, cards, devices, direction, is_bit_perfect);
jiabin400bbe02020-10-28 13:56:59 -07001841
1842 if (ret != 0) {
1843 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1844 stream_set_new_devices(
jiabinfea9e1b2022-12-19 19:31:36 +00001845 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction,
1846 is_bit_perfect);
jiabin400bbe02020-10-28 13:56:59 -07001847 } else {
1848 *patch_handle = *handle;
1849 }
Andy Hung61583422021-07-02 16:13:11 -07001850
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
jiabin400bbe02020-10-28 13:56:59 -07001857 if (!wasStandby) {
1858 device_lock(adev);
1859 if (in != NULL) {
1860 start_input_stream(in);
1861 }
1862 if (out != NULL) {
1863 start_output_stream(out);
1864 }
1865 device_unlock(adev);
1866 }
1867 stream_unlock(lock);
1868 return ret;
1869}
1870
1871static int adev_release_audio_patch(struct audio_hw_device *dev,
1872 audio_patch_handle_t patch_handle)
1873{
1874 struct audio_device* adev = (struct audio_device*) dev;
1875
1876 device_lock(adev);
1877 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1878 device_unlock(adev);
1879 if (out != NULL) {
1880 stream_lock(&out->lock);
1881 device_lock(adev);
1882 stream_standby_l(&out->alsa_devices, &out->standby);
1883 device_unlock(adev);
1884 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1885 stream_unlock(&out->lock);
1886 return 0;
1887 }
1888
1889 device_lock(adev);
1890 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1891 device_unlock(adev);
1892 if (in != NULL) {
1893 stream_lock(&in->lock);
1894 device_lock(adev);
1895 stream_standby_l(&in->alsa_devices, &in->standby);
1896 device_unlock(adev);
1897 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1898 stream_unlock(&in->lock);
1899 return 0;
1900 }
1901
1902 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1903 return -EINVAL;
1904}
1905
1906static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1907{
1908 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1909 return -EINVAL;
1910 }
1911
1912 alsa_device_profile profile;
1913 const bool is_output = audio_is_output_device(port->ext.device.type);
1914 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1915 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1916 return -EINVAL;
1917 }
1918
1919 if (!profile_read_device_info(&profile)) {
1920 return -ENOENT;
1921 }
1922
1923 port->num_formats = 0;;
1924 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1925 profile.formats[i] != 0; ++i) {
1926 audio_format_t format = audio_format_from(profile.formats[i]);
1927 if (format != AUDIO_FORMAT_INVALID) {
1928 port->formats[port->num_formats++] = format;
1929 }
1930 }
1931
jiabina635fcf2021-01-11 11:34:50 -08001932 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1933 port->num_channel_masks = populate_channel_mask_from_profile(
1934 &profile, is_output, port->channel_masks);
jiabin400bbe02020-10-28 13:56:59 -07001935
jiabina635fcf2021-01-11 11:34:50 -08001936 return 0;
1937}
1938
1939static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1940{
1941 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1942 return -EINVAL;
jiabin400bbe02020-10-28 13:56:59 -07001943 }
jiabina635fcf2021-01-11 11:34:50 -08001944
1945 alsa_device_profile profile;
1946 const bool is_output = audio_is_output_device(port->ext.device.type);
1947 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1948 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1949 return -EINVAL;
1950 }
1951
1952 if (!profile_read_device_info(&profile)) {
1953 return -ENOENT;
1954 }
1955
1956 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1957 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1958 &profile, is_output, channel_masks);
1959 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1960 const unsigned int num_sample_rates =
1961 populate_sample_rates_from_profile(&profile, sample_rates);
1962 port->num_audio_profiles = 0;;
1963 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1964 profile.formats[i] != 0; ++i) {
1965 audio_format_t format = audio_format_from(profile.formats[i]);
1966 if (format == AUDIO_FORMAT_INVALID) {
1967 continue;
1968 }
1969 const unsigned int j = port->num_audio_profiles++;
1970 port->audio_profiles[j].format = format;
1971 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1972 memcpy(port->audio_profiles[j].sample_rates,
1973 sample_rates,
1974 num_sample_rates * sizeof(unsigned int));
1975 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1976 memcpy(port->audio_profiles[j].channel_masks,
1977 channel_masks,
1978 num_channel_masks* sizeof(audio_channel_mask_t));
1979 }
1980
jiabin400bbe02020-10-28 13:56:59 -07001981 return 0;
1982}
1983
Paul McLean76dba682016-06-01 12:29:11 -06001984static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001985{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001986 dprintf(fd, "\nUSB audio module:\n");
1987
1988 struct audio_device* adev = (struct audio_device*)device;
1989 const int kNumRetries = 3;
1990 const int kSleepTimeMS = 500;
1991
Paul McLean994ac072016-06-02 15:33:24 -06001992 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001993 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001994 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001995 sleep(kSleepTimeMS);
1996 retry--;
1997 }
1998
1999 if (retry > 0) {
2000 if (list_empty(&adev->output_stream_list)) {
2001 dprintf(fd, " No output streams.\n");
2002 } else {
2003 struct listnode* node;
2004 list_for_each(node, &adev->output_stream_list) {
2005 struct audio_stream* stream =
2006 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
2007 out_dump(stream, fd);
2008 }
2009 }
2010
2011 if (list_empty(&adev->input_stream_list)) {
2012 dprintf(fd, "\n No input streams.\n");
2013 } else {
2014 struct listnode* node;
2015 list_for_each(node, &adev->input_stream_list) {
2016 struct audio_stream* stream =
2017 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
2018 in_dump(stream, fd);
2019 }
2020 }
2021
Paul McLean994ac072016-06-02 15:33:24 -06002022 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06002023 } else {
2024 // Couldn't lock
2025 dprintf(fd, " Could not obtain device lock.\n");
2026 }
2027
Simon Wilson19957a32012-04-06 16:17:12 -07002028 return 0;
2029}
2030
2031static int adev_close(hw_device_t *device)
2032{
Simon Wilson19957a32012-04-06 16:17:12 -07002033 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08002034
Simon Wilson19957a32012-04-06 16:17:12 -07002035 return 0;
2036}
2037
Paul McLean30f41852014-04-16 15:44:20 -07002038static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07002039{
Simon Wilson19957a32012-04-06 16:17:12 -07002040 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
2041 return -EINVAL;
2042
Paul McLeaneedc92e2013-12-19 15:46:15 -08002043 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07002044 if (!adev)
2045 return -ENOMEM;
2046
Paul McLeancf5310a2018-08-22 14:33:12 -07002047 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07002048
Paul McLean6a75e4e2016-05-25 14:09:02 -06002049 list_init(&adev->output_stream_list);
2050 list_init(&adev->input_stream_list);
2051
Simon Wilson19957a32012-04-06 16:17:12 -07002052 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabina635fcf2021-01-11 11:34:50 -08002053 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07002054 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07002055 adev->hw_device.common.close = adev_close;
2056
Simon Wilson19957a32012-04-06 16:17:12 -07002057 adev->hw_device.init_check = adev_init_check;
2058 adev->hw_device.set_voice_volume = adev_set_voice_volume;
2059 adev->hw_device.set_master_volume = adev_set_master_volume;
2060 adev->hw_device.set_mode = adev_set_mode;
2061 adev->hw_device.set_mic_mute = adev_set_mic_mute;
2062 adev->hw_device.get_mic_mute = adev_get_mic_mute;
2063 adev->hw_device.set_parameters = adev_set_parameters;
2064 adev->hw_device.get_parameters = adev_get_parameters;
2065 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
2066 adev->hw_device.open_output_stream = adev_open_output_stream;
2067 adev->hw_device.close_output_stream = adev_close_output_stream;
2068 adev->hw_device.open_input_stream = adev_open_input_stream;
2069 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07002070 adev->hw_device.create_audio_patch = adev_create_audio_patch;
2071 adev->hw_device.release_audio_patch = adev_release_audio_patch;
2072 adev->hw_device.get_audio_port = adev_get_audio_port;
jiabina635fcf2021-01-11 11:34:50 -08002073 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
Simon Wilson19957a32012-04-06 16:17:12 -07002074 adev->hw_device.dump = adev_dump;
2075
2076 *device = &adev->hw_device.common;
2077
2078 return 0;
2079}
2080
2081static struct hw_module_methods_t hal_module_methods = {
2082 .open = adev_open,
2083};
2084
2085struct audio_module HAL_MODULE_INFO_SYM = {
2086 .common = {
2087 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07002088 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2089 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07002090 .id = AUDIO_HARDWARE_MODULE_ID,
2091 .name = "USB audio HW HAL",
2092 .author = "The Android Open Source Project",
2093 .methods = &hal_module_methods,
2094 },
2095};