blob: 57f523b99f7363249ed40510c9e9c20184479d7c [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;
weizd5ad6d322023-04-20 08:38:12 +0800812 } else {
813 out->standby = false;
jiabin8b265c92020-10-21 15:16:40 -0700814 }
815 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800816
jiabin8b265c92020-10-21 15:16:40 -0700817exit:
818 if (status != 0) {
819 list_for_each(node, &out->alsa_devices) {
820 struct alsa_device_info *device_info =
821 node_to_item(node, struct alsa_device_info, list_node);
822 proxy_close(&device_info->proxy);
823 }
824
825 }
826 return status;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800827}
828
829static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700830{
831 int ret;
832 struct stream_out *out = (struct stream_out *)stream;
833
Paul McLean994ac072016-06-02 15:33:24 -0600834 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700835 if (out->standby) {
836 ret = start_output_stream(out);
837 if (ret != 0) {
838 goto err;
839 }
Simon Wilson19957a32012-04-06 16:17:12 -0700840 }
Eric Laurent05333d42014-08-04 20:29:17 -0700841
jiabin8b265c92020-10-21 15:16:40 -0700842 struct listnode* node;
843 list_for_each(node, &out->alsa_devices) {
844 struct alsa_device_info* device_info =
845 node_to_item(node, struct alsa_device_info, list_node);
846 alsa_device_proxy* proxy = &device_info->proxy;
847 const void * write_buff = buffer;
848 int num_write_buff_bytes = bytes;
849 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
850 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
851 if (num_device_channels != num_req_channels) {
852 /* allocate buffer */
853 const size_t required_conversion_buffer_size =
854 bytes * num_device_channels / num_req_channels;
855 if (required_conversion_buffer_size > out->conversion_buffer_size) {
856 out->conversion_buffer_size = required_conversion_buffer_size;
857 out->conversion_buffer = realloc(out->conversion_buffer,
858 out->conversion_buffer_size);
859 }
860 /* convert data */
861 const audio_format_t audio_format = out_get_format(&(out->stream.common));
862 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
863 num_write_buff_bytes =
864 adjust_channels(write_buff, num_req_channels,
865 out->conversion_buffer, num_device_channels,
866 sample_size_in_bytes, num_write_buff_bytes);
867 write_buff = out->conversion_buffer;
Andy Hung03576be2014-07-21 21:16:45 -0700868 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800869
jiabin8b265c92020-10-21 15:16:40 -0700870 if (write_buff != NULL && num_write_buff_bytes != 0) {
871 proxy_write(proxy, write_buff, num_write_buff_bytes);
872 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800873 }
Simon Wilson19957a32012-04-06 16:17:12 -0700874
Paul McLean994ac072016-06-02 15:33:24 -0600875 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700876
877 return bytes;
878
879err:
Paul McLean994ac072016-06-02 15:33:24 -0600880 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700881 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700882 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700883 out_get_sample_rate(&stream->common));
884 }
885
886 return bytes;
887}
888
Paul McLean30f41852014-04-16 15:44:20 -0700889static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700890{
891 return -EINVAL;
892}
893
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700894static int out_get_presentation_position(const struct audio_stream_out *stream,
895 uint64_t *frames, struct timespec *timestamp)
896{
Andy Hungc9515ce2015-08-04 15:05:19 -0700897 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600898 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700899
jiabin8b265c92020-10-21 15:16:40 -0700900 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
901 const int ret = device_info == NULL ? -ENODEV :
902 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
Paul McLean994ac072016-06-02 15:33:24 -0600903 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700904 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700905}
906
Simon Wilson19957a32012-04-06 16:17:12 -0700907static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
908{
909 return 0;
910}
911
912static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
913{
914 return 0;
915}
916
Paul McLean30f41852014-04-16 15:44:20 -0700917static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700918{
919 return -EINVAL;
920}
921
Paul McLean76dba682016-06-01 12:29:11 -0600922static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700923 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600924 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700925 audio_output_flags_t flags,
926 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700927 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700928 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700929{
Paul McLean76dba682016-06-01 12:29:11 -0600930 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
931 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800932
jiabinfea9e1b2022-12-19 19:31:36 +0000933 const bool is_bit_perfect = ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE);
934 if (is_bit_perfect && (config->format == AUDIO_FORMAT_DEFAULT ||
935 config->sample_rate == 0 ||
936 config->channel_mask == AUDIO_CHANNEL_NONE)) {
937 ALOGE("%s request bit perfect playback, config(format=%#x, sample_rate=%u, "
938 "channel_mask=%#x) must be specified", __func__, config->format,
939 config->sample_rate, config->channel_mask);
940 return -EINVAL;
941 }
942
Simon Wilson19957a32012-04-06 16:17:12 -0700943 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700944
945 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600946 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700947 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600948 }
Simon Wilson19957a32012-04-06 16:17:12 -0700949
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700950 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700951 out->stream.common.get_sample_rate = out_get_sample_rate;
952 out->stream.common.set_sample_rate = out_set_sample_rate;
953 out->stream.common.get_buffer_size = out_get_buffer_size;
954 out->stream.common.get_channels = out_get_channels;
955 out->stream.common.get_format = out_get_format;
956 out->stream.common.set_format = out_set_format;
957 out->stream.common.standby = out_standby;
958 out->stream.common.dump = out_dump;
959 out->stream.common.set_parameters = out_set_parameters;
960 out->stream.common.get_parameters = out_get_parameters;
961 out->stream.common.add_audio_effect = out_add_audio_effect;
962 out->stream.common.remove_audio_effect = out_remove_audio_effect;
963 out->stream.get_latency = out_get_latency;
964 out->stream.set_volume = out_set_volume;
965 out->stream.write = out_write;
966 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700967 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700968 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
969
jiabin400bbe02020-10-28 13:56:59 -0700970 out->handle = handle;
971
Paul McLean994ac072016-06-02 15:33:24 -0600972 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700973
Paul McLean76dba682016-06-01 12:29:11 -0600974 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700975
jiabin8b265c92020-10-21 15:16:40 -0700976 list_init(&out->alsa_devices);
977 struct alsa_device_info *device_info =
978 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
979 profile_init(&device_info->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700980
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700981 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700982 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800983
Paul McLean0f1753e2014-12-02 12:36:45 -0700984 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700985 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700986
jiabin8b265c92020-10-21 15:16:40 -0700987 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700988
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700989 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800990
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700991 /* Rate */
992 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700993 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
994 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700995 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800996 } else {
jiabinfea9e1b2022-12-19 19:31:36 +0000997 ret = -EINVAL;
998 if (is_bit_perfect) {
999 ALOGE("%s requesting bit-perfect but the sample rate(%u) is not valid",
1000 __func__, config->sample_rate);
1001 return ret;
1002 }
jiabin8b265c92020-10-21 15:16:40 -07001003 proxy_config.rate = config->sample_rate =
1004 profile_get_default_sample_rate(&device_info->profile);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001005 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001006
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001007 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -07001008 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -06001009 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -06001010 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -06001011
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001012 /* Format */
1013 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -07001014 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001015 config->format = audio_format_from_pcm_format(proxy_config.format);
1016 } else {
1017 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001018 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001019 proxy_config.format = fmt;
1020 } else {
jiabinfea9e1b2022-12-19 19:31:36 +00001021 ret = -EINVAL;
1022 if (is_bit_perfect) {
1023 ALOGE("%s request bit-perfect but the format(%#x) is not valid",
1024 __func__, config->format);
1025 return ret;
1026 }
jiabin8b265c92020-10-21 15:16:40 -07001027 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001028 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001029 }
1030 }
1031
1032 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001033 bool calc_mask = false;
1034 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1035 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001036 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001037 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -07001038 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001039 /* explicit case */
1040 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -07001041 }
Paul McLean698dbd72015-11-03 12:24:30 -08001042
Paul McLean64345f82016-06-06 13:26:05 -06001043 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001044 if (out->hal_channel_count > FCC_LIMIT) {
1045 out->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001046 calc_mask = true;
1047 }
1048
1049 if (calc_mask) {
1050 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001051 * 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 -06001052 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -07001053 /* position mask for mono and stereo*/
1054 ? audio_channel_out_mask_from_count(out->hal_channel_count)
1055 /* otherwise indexed */
1056 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
Paul McLean64345f82016-06-06 13:26:05 -06001057 }
1058
Andy Hung182ddc72015-05-05 23:37:30 -07001059 out->hal_channel_mask = config->channel_mask;
1060
Paul McLean64345f82016-06-06 13:26:05 -06001061 // Validate the "logical" channel count against support in the "actual" profile.
1062 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1063 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -07001064 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001065 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
jiabinfea9e1b2022-12-19 19:31:36 +00001066 if (is_bit_perfect && proxy_config.channels != out->hal_channel_count) {
1067 ALOGE("%s request bit-perfect, but channel mask(%#x) cannot find exact match",
1068 __func__, config->channel_mask);
1069 return -EINVAL;
1070 }
1071
1072 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config, is_bit_perfect);
1073 if (is_bit_perfect && ret != 0) {
1074 ALOGE("%s failed to prepare proxy for bit-perfect playback, err=%d", __func__, ret);
1075 return ret;
1076 }
jiabin400bbe02020-10-28 13:56:59 -07001077 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -07001078
1079 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001080
jiabin016fa212022-12-15 21:54:59 +00001081 if ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE) {
1082 out_stream_find_mixer_volume_control(out, device_info->profile.card);
1083 }
1084
Paul McLean96c4d302017-12-05 09:50:26 -08001085 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
1086 * So clear any errors that may have occurred above.
1087 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001088 ret = 0;
1089
Paul McLeaneedc92e2013-12-19 15:46:15 -08001090 out->conversion_buffer = NULL;
1091 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001092
1093 out->standby = true;
1094
Paul McLean6a75e4e2016-05-25 14:09:02 -06001095 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -06001096 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001097
Simon Wilson19957a32012-04-06 16:17:12 -07001098 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001099
1100 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001101}
1102
Paul McLean76dba682016-06-01 12:29:11 -06001103static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -07001104 struct audio_stream_out *stream)
1105{
1106 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001107
jiabin400bbe02020-10-28 13:56:59 -07001108 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001109 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -07001110 stream_standby_l(&out->alsa_devices, &out->standby);
1111 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001112
1113 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001114
Paul McLeaneedc92e2013-12-19 15:46:15 -08001115 out->conversion_buffer = NULL;
1116 out->conversion_buffer_size = 0;
1117
jiabin016fa212022-12-15 21:54:59 +00001118 if (out->volume_ctl != NULL) {
1119 for (int i = 0; i < out->volume_ctl_num_values; ++i) {
1120 mixer_ctl_set_value(out->volume_ctl, i, out->max_volume_level);
1121 }
1122 out->volume_ctl = NULL;
1123 }
1124 if (out->mixer != NULL) {
1125 mixer_close(out->mixer);
1126 out->mixer = NULL;
1127 }
1128
Paul McLean994ac072016-06-02 15:33:24 -06001129 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001130 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -06001131 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -06001132 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001133 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -06001134
Simon Wilson19957a32012-04-06 16:17:12 -07001135 free(stream);
1136}
1137
Paul McLean76dba682016-06-01 12:29:11 -06001138static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001139 const struct audio_config *config)
1140{
1141 /* TODO This needs to be calculated based on format/channels/rate */
1142 return 320;
1143}
1144
1145/*
1146 * IN functions
1147 */
1148static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1149{
jiabin8b265c92020-10-21 15:16:40 -07001150 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1151 &((const struct stream_in *)stream)->alsa_devices);
1152 if (device_info == NULL) {
1153 ALOGW("%s device info is null", __func__);
1154 return 0;
1155 }
1156 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001157 ALOGV("in_get_sample_rate() = %d", rate);
1158 return rate;
1159}
1160
1161static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1162{
1163 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
1164 return -ENOSYS;
1165}
1166
1167static size_t in_get_buffer_size(const struct audio_stream *stream)
1168{
1169 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -07001170 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1171 if (device_info == NULL) {
1172 ALOGW("%s device info is null", __func__);
1173 return 0;
1174 }
1175 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001176}
1177
1178static uint32_t in_get_channels(const struct audio_stream *stream)
1179{
Paul McLean2cfd81b2014-09-15 12:32:23 -07001180 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -07001181 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001182}
1183
1184static audio_format_t in_get_format(const struct audio_stream *stream)
1185{
jiabin8b265c92020-10-21 15:16:40 -07001186 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1187 &((const struct stream_in *)stream)->alsa_devices);
1188 if (device_info == NULL) {
1189 ALOGW("%s device info is null", __func__);
1190 return AUDIO_FORMAT_DEFAULT;
1191 }
1192 alsa_device_proxy *proxy = &device_info->proxy;
Andy Hung780f1f82015-04-22 22:14:39 -07001193 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1194 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001195}
1196
1197static int in_set_format(struct audio_stream *stream, audio_format_t format)
1198{
1199 ALOGV("in_set_format(%d) - NOPE", format);
1200
1201 return -ENOSYS;
1202}
1203
1204static int in_standby(struct audio_stream *stream)
1205{
1206 struct stream_in *in = (struct stream_in *)stream;
1207
Paul McLean994ac072016-06-02 15:33:24 -06001208 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -07001209 device_lock(in->adev);
1210 stream_standby_l(&in->alsa_devices, &in->standby);
1211 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -06001212 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001213 return 0;
1214}
1215
1216static int in_dump(const struct audio_stream *stream, int fd)
1217{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001218 const struct stream_in* in_stream = (const struct stream_in*)stream;
1219 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -07001220 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001221 }
1222
1223 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001224}
1225
1226static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1227{
Paul McLean65ec72b2015-02-18 09:13:24 -08001228 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001229
jiabin400bbe02020-10-28 13:56:59 -07001230 // The set parameters here only matters when the routing devices are changed.
1231 // When the device version higher than 3.0, the framework will use create_audio_patch
1232 // API instead of set_parameters to change audio routing.
1233 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001234}
1235
1236static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1237{
1238 struct stream_in *in = (struct stream_in *)stream;
1239
Paul McLean994ac072016-06-02 15:33:24 -06001240 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001241 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1242 char *params_str = NULL;
1243 if (device_info != NULL) {
1244 params_str = device_get_parameters(&device_info->profile, keys);
1245 }
Paul McLean994ac072016-06-02 15:33:24 -06001246 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001247
1248 return params_str;
1249}
1250
1251static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1252{
1253 return 0;
1254}
1255
1256static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1257{
1258 return 0;
1259}
1260
1261static int in_set_gain(struct audio_stream_in *stream, float gain)
1262{
1263 return 0;
1264}
1265
weizd5ad6d322023-04-20 08:38:12 +08001266/* must be called with hw device and input stream mutexes locked */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001267static int start_input_stream(struct stream_in *in)
1268{
jiabin8b265c92020-10-21 15:16:40 -07001269 // Only care about the first device as only one input device is allowed.
1270 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1271 if (device_info == NULL) {
1272 return -ENODEV;
1273 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001274
jiabin8b265c92020-10-21 15:16:40 -07001275 ALOGV("start_input_stream(card:%d device:%d)",
1276 device_info->profile.card, device_info->profile.device);
weizd5ad6d322023-04-20 08:38:12 +08001277 int ret = proxy_open(&device_info->proxy);
1278 if (ret == 0) {
1279 in->standby = false;
1280 }
1281 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001282}
1283
1284/* TODO mutex stuff here (see out_write) */
1285static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1286{
1287 size_t num_read_buff_bytes = 0;
1288 void * read_buff = buffer;
1289 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001290 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001291
1292 struct stream_in * in = (struct stream_in *)stream;
1293
Paul McLean994ac072016-06-02 15:33:24 -06001294 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001295 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001296 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001297 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001298 goto err;
1299 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001300 }
1301
jiabin8b265c92020-10-21 15:16:40 -07001302 // Only care about the first device as only one input device is allowed.
1303 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1304 if (device_info == NULL) {
1305 return 0;
1306 }
1307
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001308 /*
1309 * OK, we need to figure out how much data to read to be able to output the requested
1310 * number of bytes in the HAL format (16-bit, stereo).
1311 */
1312 num_read_buff_bytes = bytes;
jiabin8b265c92020-10-21 15:16:40 -07001313 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001314 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001315
1316 if (num_device_channels != num_req_channels) {
1317 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1318 }
1319
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001320 /* Setup/Realloc the conversion buffer (if necessary). */
1321 if (num_read_buff_bytes != bytes) {
1322 if (num_read_buff_bytes > in->conversion_buffer_size) {
1323 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1324 (and do these conversions themselves) */
1325 in->conversion_buffer_size = num_read_buff_bytes;
1326 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1327 }
1328 read_buff = in->conversion_buffer;
1329 }
1330
jiabin8b265c92020-10-21 15:16:40 -07001331 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001332 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001333 if (num_device_channels != num_req_channels) {
1334 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1335
1336 out_buff = buffer;
1337 /* Num Channels conversion */
1338 if (num_device_channels != num_req_channels) {
1339 audio_format_t audio_format = in_get_format(&(in->stream.common));
1340 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1341
1342 num_read_buff_bytes =
1343 adjust_channels(read_buff, num_device_channels,
1344 out_buff, num_req_channels,
1345 sample_size_in_bytes, num_read_buff_bytes);
1346 }
1347 }
Eric Laurent253def92014-09-14 12:18:18 -07001348
Paul McLean76dba682016-06-01 12:29:11 -06001349 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1350 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -07001351 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301352 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001353 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001354 }
1355
1356err:
Paul McLean994ac072016-06-02 15:33:24 -06001357 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001358 return num_read_buff_bytes;
1359}
1360
1361static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1362{
1363 return 0;
1364}
1365
Andy Hungfa6b4a62018-06-04 19:14:22 -07001366static int in_get_capture_position(const struct audio_stream_in *stream,
1367 int64_t *frames, int64_t *time)
1368{
1369 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1370 stream_lock(&in->lock);
1371
jiabin8b265c92020-10-21 15:16:40 -07001372 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1373
1374 const int ret = device_info == NULL ? -ENODEV
1375 : proxy_get_capture_position(&device_info->proxy, frames, time);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001376
1377 stream_unlock(&in->lock);
1378 return ret;
1379}
1380
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001381static int in_get_active_microphones(const struct audio_stream_in *stream,
1382 struct audio_microphone_characteristic_t *mic_array,
1383 size_t *mic_count) {
1384 (void)stream;
1385 (void)mic_array;
1386 (void)mic_count;
1387
1388 return -ENOSYS;
1389}
1390
1391static int in_set_microphone_direction(const struct audio_stream_in *stream,
1392 audio_microphone_direction_t dir) {
1393 (void)stream;
1394 (void)dir;
1395 ALOGV("---- in_set_microphone_direction()");
1396 return -ENOSYS;
1397}
1398
1399static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1400 (void)zoom;
1401 ALOGV("---- in_set_microphone_field_dimension()");
1402 return -ENOSYS;
1403}
1404
Paul McLean76dba682016-06-01 12:29:11 -06001405static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001406 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001407 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001408 struct audio_config *config,
1409 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001410 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001411 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001412 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001413{
Paul McLean1d585cc2016-05-24 11:28:10 -06001414 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001415 config->sample_rate, config->channel_mask, config->format);
1416
Andy Hungb10ce6d2017-10-27 19:37:58 -07001417 /* Pull out the card/device pair */
1418 int32_t card, device;
1419 if (!parse_card_device_params(address, &card, &device)) {
1420 ALOGW("%s fail - invalid address %s", __func__, address);
1421 *stream_in = NULL;
1422 return -EINVAL;
1423 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001424
Andy Hungb10ce6d2017-10-27 19:37:58 -07001425 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001426 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001427 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001428 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001429 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001430
1431 /* setup function pointers */
1432 in->stream.common.get_sample_rate = in_get_sample_rate;
1433 in->stream.common.set_sample_rate = in_set_sample_rate;
1434 in->stream.common.get_buffer_size = in_get_buffer_size;
1435 in->stream.common.get_channels = in_get_channels;
1436 in->stream.common.get_format = in_get_format;
1437 in->stream.common.set_format = in_set_format;
1438 in->stream.common.standby = in_standby;
1439 in->stream.common.dump = in_dump;
1440 in->stream.common.set_parameters = in_set_parameters;
1441 in->stream.common.get_parameters = in_get_parameters;
1442 in->stream.common.add_audio_effect = in_add_audio_effect;
1443 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1444
1445 in->stream.set_gain = in_set_gain;
1446 in->stream.read = in_read;
1447 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001448 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001449
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001450 in->stream.get_active_microphones = in_get_active_microphones;
1451 in->stream.set_microphone_direction = in_set_microphone_direction;
1452 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1453
jiabin400bbe02020-10-28 13:56:59 -07001454 in->handle = handle;
1455
Paul McLean994ac072016-06-02 15:33:24 -06001456 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001457
Paul McLean76dba682016-06-01 12:29:11 -06001458 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001459
jiabin8b265c92020-10-21 15:16:40 -07001460 list_init(&in->alsa_devices);
1461 struct alsa_device_info *device_info =
1462 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1463 profile_init(&device_info->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001464
jiabin400bbe02020-10-28 13:56:59 -07001465 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001466
Andy Hungb10ce6d2017-10-27 19:37:58 -07001467 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001468 device_lock(in->adev);
1469 int num_open_inputs = in->adev->inputs_open;
1470 device_unlock(in->adev);
1471
Andy Hungb10ce6d2017-10-27 19:37:58 -07001472 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001473 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001474 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001475 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1476 __func__, card, device);
1477 ret = -EINVAL;
1478 }
1479 } else {
1480 /* Read input profile only if necessary */
jiabin8b265c92020-10-21 15:16:40 -07001481 device_info->profile.card = card;
1482 device_info->profile.device = device;
1483 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001484 ALOGW("%s fail - cannot read profile", __func__);
1485 ret = -EINVAL;
1486 }
1487 }
1488 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001489 free(in);
1490 *stream_in = NULL;
1491 return ret;
1492 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001493
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001494 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001495 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001496 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001497 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001498 }
1499
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001500 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001501 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001502 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001503 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001504 if (highest_rate == 0) {
1505 ret = -EINVAL; /* error with device */
1506 } else {
jiabin400bbe02020-10-28 13:56:59 -07001507 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001508 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001509 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001510 /* Changing the requested rate */
1511 ret = -EINVAL;
1512 } else {
1513 /* Everything AOK! */
1514 ret = 0;
1515 }
1516 }
jiabinabf24d32023-05-16 23:20:17 +00001517 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
1518 in->config.rate = config->sample_rate;
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001519 }
jiabin8b265c92020-10-21 15:16:40 -07001520 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001521 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001522 } else {
jiabin400bbe02020-10-28 13:56:59 -07001523 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001524 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001525 ret = -EINVAL;
1526 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001527
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001528 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001529 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001530 in->config.format = profile_get_default_format(&device_info->profile);
1531 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001532 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001533 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001534 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001535 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001536 } else {
jiabin400bbe02020-10-28 13:56:59 -07001537 in->config.format = profile_get_default_format(&device_info->profile);
1538 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001539 ret = -EINVAL;
1540 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001541 }
1542
Paul McLean2cfd81b2014-09-15 12:32:23 -07001543 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001544 bool calc_mask = false;
1545 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1546 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001547 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001548 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001549 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001550 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001551 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1552 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001553
Paul McLean64345f82016-06-06 13:26:05 -06001554 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001555 if (in->hal_channel_count > FCC_LIMIT) {
1556 in->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001557 calc_mask = true;
1558 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001559
Paul McLean64345f82016-06-06 13:26:05 -06001560 if (calc_mask) {
1561 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001562 * 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 -06001563 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1564 /* position mask for mono & stereo */
1565 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1566 /* otherwise indexed */
1567 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001568
Paul McLean64345f82016-06-06 13:26:05 -06001569 // if we change the mask...
1570 if (in->hal_channel_mask != config->channel_mask &&
1571 config->channel_mask != AUDIO_CHANNEL_NONE) {
1572 config->channel_mask = in->hal_channel_mask;
1573 ret = -EINVAL;
1574 }
1575 } else {
1576 in->hal_channel_mask = config->channel_mask;
1577 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001578
Paul McLean64345f82016-06-06 13:26:05 -06001579 if (ret == 0) {
1580 // Validate the "logical" channel count against support in the "actual" profile.
1581 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1582 // and store THAT in proxy_config.channels
jiabin400bbe02020-10-28 13:56:59 -07001583 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001584 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabinfea9e1b2022-12-19 19:31:36 +00001585 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config,
1586 false /*require_exact_match*/);
Eric Laurent981f7742017-05-03 12:44:26 -07001587 if (ret == 0) {
1588 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001589
Eric Laurent981f7742017-05-03 12:44:26 -07001590 in->conversion_buffer = NULL;
1591 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001592
Eric Laurent981f7742017-05-03 12:44:26 -07001593 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001594
Eric Laurent981f7742017-05-03 12:44:26 -07001595 /* Save this for adev_dump() */
1596 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1597 } else {
1598 ALOGW("proxy_prepare error %d", ret);
jiabin8b265c92020-10-21 15:16:40 -07001599 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001600 config->channel_mask = channel_count <= FCC_2
1601 ? audio_channel_in_mask_from_count(channel_count)
1602 : audio_channel_mask_for_index_assignment_from_count(channel_count);
jiabin8b265c92020-10-21 15:16:40 -07001603 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1604 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001605 }
1606 }
Paul McLean64345f82016-06-06 13:26:05 -06001607
Eric Laurent981f7742017-05-03 12:44:26 -07001608 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001609 // Deallocate this stream on error, because AudioFlinger won't call
1610 // adev_close_input_stream() in this case.
1611 *stream_in = NULL;
1612 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001613 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001614 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001615
jiabin8b265c92020-10-21 15:16:40 -07001616 list_add_tail(&in->alsa_devices, &device_info->list_node);
1617
Andy Hungb10ce6d2017-10-27 19:37:58 -07001618 device_lock(in->adev);
1619 ++in->adev->inputs_open;
1620 device_unlock(in->adev);
1621
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001622 return ret;
1623}
1624
Paul McLean76dba682016-06-01 12:29:11 -06001625static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1626 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001627{
1628 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001629
jiabin400bbe02020-10-28 13:56:59 -07001630 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001631 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001632 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001633 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001634 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1635 if (device_info != NULL) {
1636 ALOGV("adev_close_input_stream(c:%d d:%d)",
1637 device_info->profile.card, device_info->profile.device);
1638 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001639 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1640 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001641
1642 stream_standby_l(&in->alsa_devices, &in->standby);
1643
Andy Hungb10ce6d2017-10-27 19:37:58 -07001644 device_unlock(in->adev);
1645
jiabin400bbe02020-10-28 13:56:59 -07001646 stream_clear_devices(&in->alsa_devices);
1647 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001648
1649 free(in->conversion_buffer);
1650
1651 free(stream);
1652}
1653
1654/*
1655 * ADEV Functions
1656 */
Paul McLean76dba682016-06-01 12:29:11 -06001657static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001658{
1659 return 0;
1660}
1661
Paul McLean76dba682016-06-01 12:29:11 -06001662static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001663{
1664 return strdup("");
1665}
1666
Paul McLean76dba682016-06-01 12:29:11 -06001667static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001668{
1669 return 0;
1670}
1671
Paul McLean76dba682016-06-01 12:29:11 -06001672static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001673{
1674 return -ENOSYS;
1675}
1676
Paul McLean76dba682016-06-01 12:29:11 -06001677static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001678{
1679 return -ENOSYS;
1680}
1681
Paul McLean76dba682016-06-01 12:29:11 -06001682static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001683{
1684 return 0;
1685}
1686
Paul McLean76dba682016-06-01 12:29:11 -06001687static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001688{
Paul McLean76dba682016-06-01 12:29:11 -06001689 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001690 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001691 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001692 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001693 return -ENOSYS;
1694}
1695
Paul McLean76dba682016-06-01 12:29:11 -06001696static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001697{
1698 return -ENOSYS;
1699}
1700
jiabin400bbe02020-10-28 13:56:59 -07001701static int adev_create_audio_patch(struct audio_hw_device *dev,
1702 unsigned int num_sources,
1703 const struct audio_port_config *sources,
1704 unsigned int num_sinks,
1705 const struct audio_port_config *sinks,
1706 audio_patch_handle_t *handle) {
1707 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1708 // Only accept mix->device and device->mix cases. In that case, the number of sources
1709 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1710 return -EINVAL;
1711 }
1712
1713 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1714 // If source is a device, the number of sinks should be 1.
1715 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1716 return -EINVAL;
1717 }
1718 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1719 // If source is a mix, all sinks should be device.
1720 for (unsigned int i = 0; i < num_sinks; i++) {
1721 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1722 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1723 return -EINVAL;
1724 }
1725 }
1726 } else {
1727 // All other cases are invalid.
1728 return -EINVAL;
1729 }
1730
1731 struct audio_device* adev = (struct audio_device*) dev;
1732 bool generatedPatchHandle = false;
1733 device_lock(adev);
1734 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1735 *handle = ++adev->next_patch_handle;
1736 generatedPatchHandle = true;
1737 }
1738
1739 int cards[AUDIO_PATCH_PORTS_MAX];
1740 int devices[AUDIO_PATCH_PORTS_MAX];
1741 const struct audio_port_config *port_configs =
1742 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1743 int num_configs = 0;
1744 audio_io_handle_t io_handle = 0;
1745 bool wasStandby = true;
1746 int direction = PCM_OUT;
1747 audio_patch_handle_t *patch_handle = NULL;
1748 struct listnode *alsa_devices = NULL;
1749 struct stream_lock *lock = NULL;
1750 struct pcm_config *config = NULL;
1751 struct stream_in *in = NULL;
1752 struct stream_out *out = NULL;
jiabinfea9e1b2022-12-19 19:31:36 +00001753 bool is_bit_perfect = false;
jiabin400bbe02020-10-28 13:56:59 -07001754
1755 unsigned int num_saved_devices = 0;
1756 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1757 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1758
1759 struct listnode *node;
1760
1761 // Only handle patches for mix->devices and device->mix case.
1762 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1763 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1764 if (in == NULL) {
1765 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1766 device_unlock(adev);
1767 return -EINVAL;
1768 }
1769
1770 direction = PCM_IN;
1771 wasStandby = in->standby;
1772 io_handle = in->handle;
1773 num_configs = num_sources;
1774 patch_handle = &in->patch_handle;
1775 alsa_devices = &in->alsa_devices;
1776 lock = &in->lock;
1777 config = &in->config;
1778 } else {
1779 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1780 if (out == NULL) {
1781 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1782 device_unlock(adev);
1783 return -EINVAL;
1784 }
1785
1786 direction = PCM_OUT;
1787 wasStandby = out->standby;
1788 io_handle = out->handle;
1789 num_configs = num_sinks;
1790 patch_handle = &out->patch_handle;
1791 alsa_devices = &out->alsa_devices;
1792 lock = &out->lock;
1793 config = &out->config;
jiabinfea9e1b2022-12-19 19:31:36 +00001794 is_bit_perfect = out->is_bit_perfect;
jiabin400bbe02020-10-28 13:56:59 -07001795 }
1796
1797 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1798 if (!generatedPatchHandle && *patch_handle != *handle) {
1799 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1800 "with handle(%d) when creating audio patch",
1801 __func__, *handle, *patch_handle, io_handle);
1802 device_unlock(adev);
1803 return -EINVAL;
1804 }
1805 device_unlock(adev);
1806
1807 for (unsigned int i = 0; i < num_configs; ++i) {
1808 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1809 ALOGE("%s, failed to parse card and device %s",
1810 __func__, port_configs[i].ext.device.address);
1811 return -EINVAL;
1812 }
1813 }
1814
1815 stream_lock(lock);
1816 list_for_each (node, alsa_devices) {
1817 struct alsa_device_info *device_info =
1818 node_to_item(node, struct alsa_device_info, list_node);
1819 saved_cards[num_saved_devices] = device_info->profile.card;
1820 saved_devices[num_saved_devices++] = device_info->profile.device;
1821 }
1822
jiabin4bc5f252021-06-02 18:31:13 +00001823 if (are_devices_the_same(
1824 num_configs, cards, devices, num_saved_devices, saved_cards, saved_devices)) {
1825 // The new devices are the same as original ones. No need to update.
1826 stream_unlock(lock);
1827 return 0;
1828 }
1829
jiabin400bbe02020-10-28 13:56:59 -07001830 device_lock(adev);
1831 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1832 device_unlock(adev);
1833
Andy Hung61583422021-07-02 16:13:11 -07001834 // Timestamps:
1835 // Audio timestamps assume continuous PCM frame counts which are maintained
1836 // with the device proxy.transferred variable. Technically it would be better
1837 // associated with in or out stream, not the device; here we save and restore
1838 // using the first alsa device as a simplification.
1839 uint64_t saved_transferred_frames = 0;
1840 struct alsa_device_info *device_info = stream_get_first_alsa_device(alsa_devices);
1841 if (device_info != NULL) saved_transferred_frames = device_info->proxy.transferred;
1842
jiabinfea9e1b2022-12-19 19:31:36 +00001843 int ret = stream_set_new_devices(
1844 config, alsa_devices, num_configs, cards, devices, direction, is_bit_perfect);
jiabin400bbe02020-10-28 13:56:59 -07001845
1846 if (ret != 0) {
1847 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1848 stream_set_new_devices(
jiabinfea9e1b2022-12-19 19:31:36 +00001849 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction,
1850 is_bit_perfect);
jiabin400bbe02020-10-28 13:56:59 -07001851 } else {
1852 *patch_handle = *handle;
1853 }
Andy Hung61583422021-07-02 16:13:11 -07001854
1855 // Timestamps: Restore transferred frames.
1856 if (saved_transferred_frames != 0) {
1857 device_info = stream_get_first_alsa_device(alsa_devices);
1858 if (device_info != NULL) device_info->proxy.transferred = saved_transferred_frames;
1859 }
1860
jiabin400bbe02020-10-28 13:56:59 -07001861 if (!wasStandby) {
1862 device_lock(adev);
1863 if (in != NULL) {
jiabin041a7d92022-01-05 19:06:30 +00001864 ret = start_input_stream(in);
jiabin400bbe02020-10-28 13:56:59 -07001865 }
1866 if (out != NULL) {
jiabin041a7d92022-01-05 19:06:30 +00001867 ret = start_output_stream(out);
jiabin400bbe02020-10-28 13:56:59 -07001868 }
1869 device_unlock(adev);
1870 }
1871 stream_unlock(lock);
1872 return ret;
1873}
1874
1875static int adev_release_audio_patch(struct audio_hw_device *dev,
1876 audio_patch_handle_t patch_handle)
1877{
1878 struct audio_device* adev = (struct audio_device*) dev;
1879
1880 device_lock(adev);
1881 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1882 device_unlock(adev);
1883 if (out != NULL) {
1884 stream_lock(&out->lock);
1885 device_lock(adev);
1886 stream_standby_l(&out->alsa_devices, &out->standby);
1887 device_unlock(adev);
1888 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1889 stream_unlock(&out->lock);
1890 return 0;
1891 }
1892
1893 device_lock(adev);
1894 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1895 device_unlock(adev);
1896 if (in != NULL) {
1897 stream_lock(&in->lock);
1898 device_lock(adev);
1899 stream_standby_l(&in->alsa_devices, &in->standby);
1900 device_unlock(adev);
1901 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1902 stream_unlock(&in->lock);
1903 return 0;
1904 }
1905
1906 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1907 return -EINVAL;
1908}
1909
1910static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1911{
1912 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1913 return -EINVAL;
1914 }
1915
1916 alsa_device_profile profile;
1917 const bool is_output = audio_is_output_device(port->ext.device.type);
1918 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1919 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1920 return -EINVAL;
1921 }
1922
1923 if (!profile_read_device_info(&profile)) {
1924 return -ENOENT;
1925 }
1926
1927 port->num_formats = 0;;
1928 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1929 profile.formats[i] != 0; ++i) {
1930 audio_format_t format = audio_format_from(profile.formats[i]);
1931 if (format != AUDIO_FORMAT_INVALID) {
1932 port->formats[port->num_formats++] = format;
1933 }
1934 }
1935
jiabina635fcf2021-01-11 11:34:50 -08001936 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1937 port->num_channel_masks = populate_channel_mask_from_profile(
1938 &profile, is_output, port->channel_masks);
jiabin400bbe02020-10-28 13:56:59 -07001939
jiabina635fcf2021-01-11 11:34:50 -08001940 return 0;
1941}
1942
1943static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1944{
1945 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1946 return -EINVAL;
jiabin400bbe02020-10-28 13:56:59 -07001947 }
jiabina635fcf2021-01-11 11:34:50 -08001948
1949 alsa_device_profile profile;
1950 const bool is_output = audio_is_output_device(port->ext.device.type);
1951 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1952 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1953 return -EINVAL;
1954 }
1955
1956 if (!profile_read_device_info(&profile)) {
1957 return -ENOENT;
1958 }
1959
1960 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1961 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1962 &profile, is_output, channel_masks);
1963 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1964 const unsigned int num_sample_rates =
1965 populate_sample_rates_from_profile(&profile, sample_rates);
1966 port->num_audio_profiles = 0;;
1967 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1968 profile.formats[i] != 0; ++i) {
1969 audio_format_t format = audio_format_from(profile.formats[i]);
1970 if (format == AUDIO_FORMAT_INVALID) {
1971 continue;
1972 }
1973 const unsigned int j = port->num_audio_profiles++;
1974 port->audio_profiles[j].format = format;
1975 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1976 memcpy(port->audio_profiles[j].sample_rates,
1977 sample_rates,
1978 num_sample_rates * sizeof(unsigned int));
1979 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1980 memcpy(port->audio_profiles[j].channel_masks,
1981 channel_masks,
1982 num_channel_masks* sizeof(audio_channel_mask_t));
1983 }
1984
jiabin400bbe02020-10-28 13:56:59 -07001985 return 0;
1986}
1987
Paul McLean76dba682016-06-01 12:29:11 -06001988static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001989{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001990 dprintf(fd, "\nUSB audio module:\n");
1991
1992 struct audio_device* adev = (struct audio_device*)device;
1993 const int kNumRetries = 3;
1994 const int kSleepTimeMS = 500;
1995
Paul McLean994ac072016-06-02 15:33:24 -06001996 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001997 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001998 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001999 sleep(kSleepTimeMS);
2000 retry--;
2001 }
2002
2003 if (retry > 0) {
2004 if (list_empty(&adev->output_stream_list)) {
2005 dprintf(fd, " No output streams.\n");
2006 } else {
2007 struct listnode* node;
2008 list_for_each(node, &adev->output_stream_list) {
2009 struct audio_stream* stream =
2010 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
2011 out_dump(stream, fd);
2012 }
2013 }
2014
2015 if (list_empty(&adev->input_stream_list)) {
2016 dprintf(fd, "\n No input streams.\n");
2017 } else {
2018 struct listnode* node;
2019 list_for_each(node, &adev->input_stream_list) {
2020 struct audio_stream* stream =
2021 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
2022 in_dump(stream, fd);
2023 }
2024 }
2025
Paul McLean994ac072016-06-02 15:33:24 -06002026 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06002027 } else {
2028 // Couldn't lock
2029 dprintf(fd, " Could not obtain device lock.\n");
2030 }
2031
Simon Wilson19957a32012-04-06 16:17:12 -07002032 return 0;
2033}
2034
2035static int adev_close(hw_device_t *device)
2036{
Simon Wilson19957a32012-04-06 16:17:12 -07002037 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08002038
Simon Wilson19957a32012-04-06 16:17:12 -07002039 return 0;
2040}
2041
Paul McLean30f41852014-04-16 15:44:20 -07002042static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07002043{
Simon Wilson19957a32012-04-06 16:17:12 -07002044 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
2045 return -EINVAL;
2046
Paul McLeaneedc92e2013-12-19 15:46:15 -08002047 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07002048 if (!adev)
2049 return -ENOMEM;
2050
Paul McLeancf5310a2018-08-22 14:33:12 -07002051 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07002052
Paul McLean6a75e4e2016-05-25 14:09:02 -06002053 list_init(&adev->output_stream_list);
2054 list_init(&adev->input_stream_list);
2055
Simon Wilson19957a32012-04-06 16:17:12 -07002056 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabina635fcf2021-01-11 11:34:50 -08002057 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07002058 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07002059 adev->hw_device.common.close = adev_close;
2060
Simon Wilson19957a32012-04-06 16:17:12 -07002061 adev->hw_device.init_check = adev_init_check;
2062 adev->hw_device.set_voice_volume = adev_set_voice_volume;
2063 adev->hw_device.set_master_volume = adev_set_master_volume;
2064 adev->hw_device.set_mode = adev_set_mode;
2065 adev->hw_device.set_mic_mute = adev_set_mic_mute;
2066 adev->hw_device.get_mic_mute = adev_get_mic_mute;
2067 adev->hw_device.set_parameters = adev_set_parameters;
2068 adev->hw_device.get_parameters = adev_get_parameters;
2069 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
2070 adev->hw_device.open_output_stream = adev_open_output_stream;
2071 adev->hw_device.close_output_stream = adev_close_output_stream;
2072 adev->hw_device.open_input_stream = adev_open_input_stream;
2073 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07002074 adev->hw_device.create_audio_patch = adev_create_audio_patch;
2075 adev->hw_device.release_audio_patch = adev_release_audio_patch;
2076 adev->hw_device.get_audio_port = adev_get_audio_port;
jiabina635fcf2021-01-11 11:34:50 -08002077 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
Simon Wilson19957a32012-04-06 16:17:12 -07002078 adev->hw_device.dump = adev_dump;
2079
2080 *device = &adev->hw_device.common;
2081
2082 return 0;
2083}
2084
2085static struct hw_module_methods_t hal_module_methods = {
2086 .open = adev_open,
2087};
2088
2089struct audio_module HAL_MODULE_INFO_SYM = {
2090 .common = {
2091 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07002092 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2093 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07002094 .id = AUDIO_HARDWARE_MODULE_ID,
2095 .name = "USB audio HW HAL",
2096 .author = "The Android Open Source Project",
2097 .methods = &hal_module_methods,
2098 },
2099};