blob: aa4167d7c8812649d2152c9770206741414ca6b0 [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
123 // Mixer information used for volume handling
124 struct mixer* mixer;
125 struct mixer_ctl* volume_ctl;
126 int volume_ctl_num_values;
127 int max_volume_level;
128 int min_volume_level;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800129};
130
Paul McLeaneedc92e2013-12-19 15:46:15 -0800131struct stream_in {
132 struct audio_stream_in stream;
133
Paul McLean994ac072016-06-02 15:33:24 -0600134 struct stream_lock lock;
135
Simon Wilson19957a32012-04-06 16:17:12 -0700136 bool standby;
137
Paul McLean76dba682016-06-01 12:29:11 -0600138 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800139
jiabin8b265c92020-10-21 15:16:40 -0700140 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700141
Paul McLean2cfd81b2014-09-15 12:32:23 -0700142 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
143 * This may differ from the device channel count when
144 * the device is not compatible with AudioFlinger
145 * capabilities, e.g. exposes too many channels or
146 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600147 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
148 * so the proxy doesn't have a channel_mask, but
149 * audio HALs need to talk about channel masks
150 * so expose the one calculated by
151 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700152
Paul McLean6a75e4e2016-05-25 14:09:02 -0600153 struct listnode list_node;
154
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700155 /* 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 -0700156 void * conversion_buffer; /* any conversions are put into here
157 * they could come from here too if
158 * there was a previous conversion */
159 size_t conversion_buffer_size; /* in bytes */
jiabin400bbe02020-10-28 13:56:59 -0700160
161 struct pcm_config config;
162
163 audio_io_handle_t handle; // Unique identifier for a stream
164
165 audio_patch_handle_t patch_handle; // Patch handle for this stream
Simon Wilson19957a32012-04-06 16:17:12 -0700166};
167
jiabin400bbe02020-10-28 13:56:59 -0700168// Map channel count to output channel mask
Andy Hungdf511202021-06-07 16:45:03 -0700169static const audio_channel_mask_t OUT_CHANNEL_MASKS_MAP[FCC_24 + 1] = {
170 [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted)
171 // != AUDIO_CHANNEL_INVALID == 0xC0000000u
172
173 [1] = AUDIO_CHANNEL_OUT_MONO,
174 [2] = AUDIO_CHANNEL_OUT_STEREO,
175 [3] = AUDIO_CHANNEL_OUT_2POINT1,
176 [4] = AUDIO_CHANNEL_OUT_QUAD,
177 [5] = AUDIO_CHANNEL_OUT_PENTA,
178 [6] = AUDIO_CHANNEL_OUT_5POINT1,
179 [7] = AUDIO_CHANNEL_OUT_6POINT1,
180 [8] = AUDIO_CHANNEL_OUT_7POINT1,
181
182 [9 ... 11] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
183
184 [12] = AUDIO_CHANNEL_OUT_7POINT1POINT4,
185
186 [13 ... 23] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
187
188 [24] = AUDIO_CHANNEL_OUT_22POINT2,
jiabin400bbe02020-10-28 13:56:59 -0700189};
190static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP);
191
192// Map channel count to input channel mask
193static const audio_channel_mask_t IN_CHANNEL_MASKS_MAP[] = {
194 AUDIO_CHANNEL_NONE, /* 0 */
195 AUDIO_CHANNEL_IN_MONO, /* 1 */
196 AUDIO_CHANNEL_IN_STEREO, /* 2 */
197 /* channel counts greater than this are not considered */
198};
199static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP);
200
201// Map channel count to index mask
Andy Hungdf511202021-06-07 16:45:03 -0700202static const audio_channel_mask_t CHANNEL_INDEX_MASKS_MAP[FCC_24 + 1] = {
203 [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
204
205 [1] = AUDIO_CHANNEL_INDEX_MASK_1,
206 [2] = AUDIO_CHANNEL_INDEX_MASK_2,
207 [3] = AUDIO_CHANNEL_INDEX_MASK_3,
208 [4] = AUDIO_CHANNEL_INDEX_MASK_4,
209 [5] = AUDIO_CHANNEL_INDEX_MASK_5,
210 [6] = AUDIO_CHANNEL_INDEX_MASK_6,
211 [7] = AUDIO_CHANNEL_INDEX_MASK_7,
212 [8] = AUDIO_CHANNEL_INDEX_MASK_8,
213
214 [9] = AUDIO_CHANNEL_INDEX_MASK_9,
215 [10] = AUDIO_CHANNEL_INDEX_MASK_10,
216 [11] = AUDIO_CHANNEL_INDEX_MASK_11,
217 [12] = AUDIO_CHANNEL_INDEX_MASK_12,
218 [13] = AUDIO_CHANNEL_INDEX_MASK_13,
219 [14] = AUDIO_CHANNEL_INDEX_MASK_14,
220 [15] = AUDIO_CHANNEL_INDEX_MASK_15,
221 [16] = AUDIO_CHANNEL_INDEX_MASK_16,
222
223 [17] = AUDIO_CHANNEL_INDEX_MASK_17,
224 [18] = AUDIO_CHANNEL_INDEX_MASK_18,
225 [19] = AUDIO_CHANNEL_INDEX_MASK_19,
226 [20] = AUDIO_CHANNEL_INDEX_MASK_20,
227 [21] = AUDIO_CHANNEL_INDEX_MASK_21,
228 [22] = AUDIO_CHANNEL_INDEX_MASK_22,
229 [23] = AUDIO_CHANNEL_INDEX_MASK_23,
230 [24] = AUDIO_CHANNEL_INDEX_MASK_24,
jiabin400bbe02020-10-28 13:56:59 -0700231};
232static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP);
233
jiabin016fa212022-12-15 21:54:59 +0000234static const char* ALL_VOLUME_CONTROL_NAMES[] = {
235 "PCM Playback Volume",
236 "Headset Playback Volume",
237 "Headphone Playback Volume",
238 "Master Playback Volume",
239};
240static const int VOLUME_CONTROL_NAMES_NUM = AUDIO_ARRAY_SIZE(ALL_VOLUME_CONTROL_NAMES);
241
Paul McLean994ac072016-06-02 15:33:24 -0600242/*
243 * Locking Helpers
244 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800245/*
Eric Laurent70318092015-06-19 17:49:17 -0700246 * NOTE: when multiple mutexes have to be acquired, always take the
247 * stream_in or stream_out mutex first, followed by the audio_device mutex.
248 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
249 * higher priority playback or capture thread.
250 */
251
Paul McLean994ac072016-06-02 15:33:24 -0600252static void stream_lock_init(struct stream_lock *lock) {
253 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
254 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
255}
256
257static void stream_lock(struct stream_lock *lock) {
jiabin400bbe02020-10-28 13:56:59 -0700258 if (lock == NULL) {
259 return;
260 }
Paul McLean994ac072016-06-02 15:33:24 -0600261 pthread_mutex_lock(&lock->pre_lock);
262 pthread_mutex_lock(&lock->lock);
263 pthread_mutex_unlock(&lock->pre_lock);
264}
265
266static void stream_unlock(struct stream_lock *lock) {
267 pthread_mutex_unlock(&lock->lock);
268}
269
270static void device_lock(struct audio_device *adev) {
271 pthread_mutex_lock(&adev->lock);
272}
273
274static int device_try_lock(struct audio_device *adev) {
275 return pthread_mutex_trylock(&adev->lock);
276}
277
278static void device_unlock(struct audio_device *adev) {
279 pthread_mutex_unlock(&adev->lock);
280}
281
282/*
283 * streams list management
284 */
285static void adev_add_stream_to_list(
286 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
287 device_lock(adev);
288
289 list_add_tail(list, stream_node);
290
291 device_unlock(adev);
292}
293
jiabin400bbe02020-10-28 13:56:59 -0700294static struct stream_out* adev_get_stream_out_by_io_handle_l(
295 struct audio_device* adev, audio_io_handle_t handle) {
296 struct listnode *node;
297 list_for_each (node, &adev->output_stream_list) {
298 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
299 if (out->handle == handle) {
300 return out;
301 }
302 }
303 return NULL;
304}
Paul McLean994ac072016-06-02 15:33:24 -0600305
jiabin400bbe02020-10-28 13:56:59 -0700306static struct stream_in* adev_get_stream_in_by_io_handle_l(
307 struct audio_device* adev, audio_io_handle_t handle) {
308 struct listnode *node;
309 list_for_each (node, &adev->input_stream_list) {
310 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
311 if (in->handle == handle) {
312 return in;
313 }
314 }
315 return NULL;
316}
Paul McLean994ac072016-06-02 15:33:24 -0600317
jiabin400bbe02020-10-28 13:56:59 -0700318static struct stream_out* adev_get_stream_out_by_patch_handle_l(
319 struct audio_device* adev, audio_patch_handle_t patch_handle) {
320 struct listnode *node;
321 list_for_each (node, &adev->output_stream_list) {
322 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
323 if (out->patch_handle == patch_handle) {
324 return out;
325 }
326 }
327 return NULL;
328}
329
330static struct stream_in* adev_get_stream_in_by_patch_handle_l(
331 struct audio_device* adev, audio_patch_handle_t patch_handle) {
332 struct listnode *node;
333 list_for_each (node, &adev->input_stream_list) {
334 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
335 if (in->patch_handle == patch_handle) {
336 return in;
337 }
338 }
339 return NULL;
Paul McLean994ac072016-06-02 15:33:24 -0600340}
341
Eric Laurent70318092015-06-19 17:49:17 -0700342/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700343 * Extract the card and device numbers from the supplied key/value pairs.
344 * kvpairs A null-terminated string containing the key/value pairs or card and device.
345 * i.e. "card=1;device=42"
346 * card A pointer to a variable to receive the parsed-out card number.
347 * device A pointer to a variable to receive the parsed-out device number.
348 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
349 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800350 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700351 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800352static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700353{
354 struct str_parms * parms = str_parms_create_str(kvpairs);
355 char value[32];
356 int param_val;
357
358 // initialize to "undefined" state.
359 *card = -1;
360 *device = -1;
361
362 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
363 if (param_val >= 0) {
364 *card = atoi(value);
365 }
366
367 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
368 if (param_val >= 0) {
369 *device = atoi(value);
370 }
371
372 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800373
374 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700375}
376
Andy Hung4e6a1c02017-10-27 20:31:46 -0700377static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800378{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700379 if (profile->card < 0 || profile->device < 0) {
380 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800381 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700382
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700383 struct str_parms *query = str_parms_create_str(keys);
384 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700385
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700386 /* These keys are from hardware/libhardware/include/audio.h */
387 /* supported sample rates */
388 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
389 char* rates_list = profile_get_sample_rate_strs(profile);
390 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
391 rates_list);
392 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700393 }
394
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700395 /* supported channel counts */
396 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
397 char* channels_list = profile_get_channel_count_strs(profile);
398 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
399 channels_list);
400 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700401 }
402
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700403 /* supported sample formats */
404 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
405 char * format_params = profile_get_format_strs(profile);
406 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
407 format_params);
408 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700409 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700410 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700411
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700412 char* result_str = str_parms_to_str(result);
413 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700414
Paul McLean65ec72b2015-02-18 09:13:24 -0800415 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700416
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700417 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800418}
419
jiabin400bbe02020-10-28 13:56:59 -0700420static audio_format_t audio_format_from(enum pcm_format format)
421{
422 switch (format) {
423 case PCM_FORMAT_S16_LE:
424 return AUDIO_FORMAT_PCM_16_BIT;
425 case PCM_FORMAT_S32_LE:
426 return AUDIO_FORMAT_PCM_32_BIT;
427 case PCM_FORMAT_S8:
428 return AUDIO_FORMAT_PCM_8_BIT;
429 case PCM_FORMAT_S24_LE:
430 return AUDIO_FORMAT_PCM_8_24_BIT;
431 case PCM_FORMAT_S24_3LE:
432 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
433 default:
434 return AUDIO_FORMAT_INVALID;
435 }
436}
437
jiabina635fcf2021-01-11 11:34:50 -0800438static unsigned int populate_channel_mask_from_profile(const alsa_device_profile* profile,
439 bool is_output,
440 audio_channel_mask_t channel_masks[])
441{
442 unsigned int num_channel_masks = 0;
443 const audio_channel_mask_t* channel_masks_map =
444 is_output ? OUT_CHANNEL_MASKS_MAP : IN_CHANNEL_MASKS_MAP;
Andy Hungdf511202021-06-07 16:45:03 -0700445 int channel_masks_size = is_output ? OUT_CHANNEL_MASKS_SIZE : IN_CHANNEL_MASKS_SIZE;
446 if (channel_masks_size > FCC_LIMIT + 1) {
447 channel_masks_size = FCC_LIMIT + 1;
448 }
jiabina635fcf2021-01-11 11:34:50 -0800449 unsigned int channel_count = 0;
450 for (size_t i = 0; i < min(channel_masks_size, AUDIO_PORT_MAX_CHANNEL_MASKS) &&
451 (channel_count = profile->channel_counts[i]) != 0 &&
452 num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS; ++i) {
453 if (channel_count < channel_masks_size &&
454 channel_masks_map[channel_count] != AUDIO_CHANNEL_NONE) {
455 channel_masks[num_channel_masks++] = channel_masks_map[channel_count];
456 if (num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
457 break;
458 }
459 }
460 if (channel_count < CHANNEL_INDEX_MASKS_SIZE &&
461 CHANNEL_INDEX_MASKS_MAP[channel_count] != AUDIO_CHANNEL_NONE) {
462 channel_masks[num_channel_masks++] = CHANNEL_INDEX_MASKS_MAP[channel_count];
463 }
464 }
465 return num_channel_masks;
466}
467
468static unsigned int populate_sample_rates_from_profile(const alsa_device_profile* profile,
469 unsigned int sample_rates[])
470{
471 unsigned int num_sample_rates = 0;
472 for (;num_sample_rates < min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
473 profile->sample_rates[num_sample_rates] != 0; num_sample_rates++) {
474 sample_rates[num_sample_rates] = profile->sample_rates[num_sample_rates];
475 }
476 return num_sample_rates;
477}
478
jiabin4bc5f252021-06-02 18:31:13 +0000479static bool are_all_devices_found(unsigned int num_devices_to_find,
480 const int cards_to_find[],
481 const int devices_to_find[],
482 unsigned int num_devices,
483 const int cards[],
484 const int devices[]) {
485 for (unsigned int i = 0; i < num_devices_to_find; ++i) {
486 unsigned int j = 0;
487 for (; j < num_devices; ++j) {
488 if (cards_to_find[i] == cards[j] && devices_to_find[i] == devices[j]) {
489 break;
490 }
491 }
492 if (j >= num_devices) {
493 return false;
494 }
495 }
496 return true;
497}
498
499static bool are_devices_the_same(unsigned int left_num_devices,
500 const int left_cards[],
501 const int left_devices[],
502 unsigned int right_num_devices,
503 const int right_cards[],
504 const int right_devices[]) {
505 if (left_num_devices != right_num_devices) {
506 return false;
507 }
508 return are_all_devices_found(left_num_devices, left_cards, left_devices,
509 right_num_devices, right_cards, right_devices) &&
510 are_all_devices_found(right_num_devices, right_cards, right_devices,
511 left_num_devices, left_cards, left_devices);
512}
513
jiabin016fa212022-12-15 21:54:59 +0000514static void out_stream_find_mixer_volume_control(struct stream_out* out, int card) {
515 out->mixer = mixer_open(card);
516 if (out->mixer == NULL) {
517 ALOGI("%s, no mixer found for card=%d", __func__, card);
518 return;
519 }
520 unsigned int num_ctls = mixer_get_num_ctls(out->mixer);
521 for (int i = 0; i < VOLUME_CONTROL_NAMES_NUM; ++i) {
522 for (unsigned int j = 0; j < num_ctls; ++j) {
523 struct mixer_ctl *ctl = mixer_get_ctl(out->mixer, j);
524 enum mixer_ctl_type ctl_type = mixer_ctl_get_type(ctl);
525 if (strcasestr(mixer_ctl_get_name(ctl), ALL_VOLUME_CONTROL_NAMES[i]) == NULL ||
526 ctl_type != MIXER_CTL_TYPE_INT) {
527 continue;
528 }
529 ALOGD("%s, mixer volume control(%s) found", __func__, ALL_VOLUME_CONTROL_NAMES[i]);
530 out->volume_ctl_num_values = mixer_ctl_get_num_values(ctl);
531 if (out->volume_ctl_num_values <= 0) {
532 ALOGE("%s the num(%d) of volume ctl values is wrong",
533 __func__, out->volume_ctl_num_values);
534 out->volume_ctl_num_values = 0;
535 continue;
536 }
537 out->max_volume_level = mixer_ctl_get_range_max(ctl);
538 out->min_volume_level = mixer_ctl_get_range_min(ctl);
539 if (out->max_volume_level < out->min_volume_level) {
540 ALOGE("%s the max volume level(%d) is less than min volume level(%d)",
541 __func__, out->max_volume_level, out->min_volume_level);
542 out->max_volume_level = 0;
543 out->min_volume_level = 0;
544 continue;
545 }
546 out->volume_ctl = ctl;
547 return;
548 }
549 }
550 ALOGI("%s, no volume control found", __func__);
551}
552
Paul McLeaneedc92e2013-12-19 15:46:15 -0800553/*
554 * HAl Functions
555 */
Simon Wilson19957a32012-04-06 16:17:12 -0700556/**
557 * NOTE: when multiple mutexes have to be acquired, always respect the
558 * following order: hw device > out stream
559 */
560
jiabin8b265c92020-10-21 15:16:40 -0700561static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
562 if (list_empty(alsa_devices)) {
563 return NULL;
564 }
565 return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
566}
567
jiabin400bbe02020-10-28 13:56:59 -0700568/**
569 * Must be called with holding the stream's lock.
570 */
571static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
572{
573 if (!*standby) {
574 struct listnode *node;
575 list_for_each (node, alsa_devices) {
576 struct alsa_device_info *device_info =
577 node_to_item(node, struct alsa_device_info, list_node);
578 proxy_close(&device_info->proxy);
579 }
580 *standby = true;
581 }
582}
583
584static void stream_clear_devices(struct listnode *alsa_devices)
585{
586 struct listnode *node, *temp;
587 struct alsa_device_info *device_info = NULL;
588 list_for_each_safe (node, temp, alsa_devices) {
589 device_info = node_to_item(node, struct alsa_device_info, list_node);
590 if (device_info != NULL) {
591 list_remove(&device_info->list_node);
592 free(device_info);
593 }
594 }
595}
596
597static int stream_set_new_devices(struct pcm_config *config,
598 struct listnode *alsa_devices,
599 unsigned int num_devices,
600 const int cards[],
601 const int devices[],
602 int direction)
603{
604 int status = 0;
605 stream_clear_devices(alsa_devices);
606
607 for (unsigned int i = 0; i < num_devices; ++i) {
608 struct alsa_device_info *device_info =
609 (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
610 profile_init(&device_info->profile, direction);
611 device_info->profile.card = cards[i];
612 device_info->profile.device = devices[i];
613 status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
614 if (status != 0) {
615 ALOGE("%s failed to read device info card=%d;device=%d",
616 __func__, cards[i], devices[i]);
617 goto exit;
618 }
619 status = proxy_prepare(&device_info->proxy, &device_info->profile, config);
620 if (status != 0) {
621 ALOGE("%s failed to prepare device card=%d;device=%d",
622 __func__, cards[i], devices[i]);
623 goto exit;
624 }
625 list_add_tail(alsa_devices, &device_info->list_node);
626 }
627
628exit:
629 if (status != 0) {
630 stream_clear_devices(alsa_devices);
631 }
632 return status;
633}
634
jiabin8b265c92020-10-21 15:16:40 -0700635static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
636 struct listnode *node;
637 size_t i = 0;
638 list_for_each(node, alsa_devices) {
639 struct alsa_device_info *device_info =
640 node_to_item(node, struct alsa_device_info, list_node);
jiabin0269a9d2021-06-02 20:27:38 +0000641 const char* direction = device_info->profile.direction == PCM_OUT ? "Output" : "Input";
642 dprintf(fd, "%s Profile %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700643 profile_dump(&device_info->profile, fd);
644
jiabin0269a9d2021-06-02 20:27:38 +0000645 dprintf(fd, "%s Proxy %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700646 proxy_dump(&device_info->proxy, fd);
647 }
648}
649
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700650/*
651 * OUT functions
652 */
Simon Wilson19957a32012-04-06 16:17:12 -0700653static uint32_t out_get_sample_rate(const struct audio_stream *stream)
654{
jiabin8b265c92020-10-21 15:16:40 -0700655 struct alsa_device_info *device_info = stream_get_first_alsa_device(
656 &((struct stream_out*)stream)->alsa_devices);
657 if (device_info == NULL) {
658 ALOGW("%s device info is null", __func__);
659 return 0;
660 }
661 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700662 ALOGV("out_get_sample_rate() = %d", rate);
663 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700664}
665
666static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
667{
668 return 0;
669}
670
671static size_t out_get_buffer_size(const struct audio_stream *stream)
672{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700673 const struct stream_out* out = (const struct stream_out*)stream;
jiabin8b265c92020-10-21 15:16:40 -0700674 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
675 if (device_info == NULL) {
676 ALOGW("%s device info is null", __func__);
677 return 0;
678 }
679 return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
Simon Wilson19957a32012-04-06 16:17:12 -0700680}
681
682static uint32_t out_get_channels(const struct audio_stream *stream)
683{
Andy Hung03576be2014-07-21 21:16:45 -0700684 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700685 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700686}
687
688static audio_format_t out_get_format(const struct audio_stream *stream)
689{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700690 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
691 * Relies on the framework to provide data in the specified format.
692 * This could change in the future.
693 */
jiabin8b265c92020-10-21 15:16:40 -0700694 struct alsa_device_info *device_info = stream_get_first_alsa_device(
695 &((struct stream_out*)stream)->alsa_devices);
696 if (device_info == NULL) {
697 ALOGW("%s device info is null", __func__);
698 return AUDIO_FORMAT_DEFAULT;
699 }
700 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700701 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700702}
703
704static int out_set_format(struct audio_stream *stream, audio_format_t format)
705{
706 return 0;
707}
708
709static int out_standby(struct audio_stream *stream)
710{
711 struct stream_out *out = (struct stream_out *)stream;
712
Paul McLean994ac072016-06-02 15:33:24 -0600713 stream_lock(&out->lock);
jiabin400bbe02020-10-28 13:56:59 -0700714 device_lock(out->adev);
715 stream_standby_l(&out->alsa_devices, &out->standby);
716 device_unlock(out->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600717 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700718 return 0;
719}
720
Paul McLean6a75e4e2016-05-25 14:09:02 -0600721static int out_dump(const struct audio_stream *stream, int fd) {
722 const struct stream_out* out_stream = (const struct stream_out*) stream;
723
724 if (out_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -0700725 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600726 }
727
Simon Wilson19957a32012-04-06 16:17:12 -0700728 return 0;
729}
730
jiabin400bbe02020-10-28 13:56:59 -0700731static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -0700732{
Paul McLean65ec72b2015-02-18 09:13:24 -0800733 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800734
jiabin400bbe02020-10-28 13:56:59 -0700735 // The set parameters here only matters when the routing devices are changed.
736 // When the device version is not less than 3.0, the framework will use create
737 // audio patch API instead of set parameters to chanage audio routing.
738 return 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700739}
740
Paul McLeanf62d75e2014-07-11 15:14:19 -0700741static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
742{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700743 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600744 stream_lock(&out->lock);
jiabin8b265c92020-10-21 15:16:40 -0700745 struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
746 char *params_str = NULL;
747 if (device_info != NULL) {
748 params_str = device_get_parameters(&device_info->profile, keys);
749 }
Paul McLean994ac072016-06-02 15:33:24 -0600750 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700751 return params_str;
752}
753
Simon Wilson19957a32012-04-06 16:17:12 -0700754static uint32_t out_get_latency(const struct audio_stream_out *stream)
755{
jiabin8b265c92020-10-21 15:16:40 -0700756 struct alsa_device_info *device_info = stream_get_first_alsa_device(
757 &((struct stream_out*)stream)->alsa_devices);
758 if (device_info == NULL) {
759 ALOGW("%s device info is null", __func__);
760 return 0;
761 }
762 return proxy_get_latency(&device_info->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700763}
764
Paul McLean30f41852014-04-16 15:44:20 -0700765static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700766{
jiabin016fa212022-12-15 21:54:59 +0000767 struct stream_out *out = (struct stream_out *)stream;
768 int result = -ENOSYS;
769 stream_lock(&out->lock);
770 if (out->volume_ctl != NULL) {
771 int left_volume =
772 out->min_volume_level + ceil((out->max_volume_level - out->min_volume_level) * left);
773 int right_volume =
774 out->min_volume_level + ceil((out->max_volume_level - out->min_volume_level) * right);
775 int volumes[out->volume_ctl_num_values];
776 if (out->volume_ctl_num_values == 1) {
777 volumes[0] = left_volume;
778 } else {
779 volumes[0] = left_volume;
780 volumes[1] = right_volume;
781 for (int i = 2; i < out->volume_ctl_num_values; ++i) {
782 volumes[i] = left_volume;
783 }
784 }
785 result = mixer_ctl_set_array(out->volume_ctl, volumes, out->volume_ctl_num_values);
786 if (result != 0) {
787 ALOGE("%s error=%d left=%f right=%f", __func__, result, left, right);
788 }
789 }
790 stream_unlock(&out->lock);
791 return result;
Simon Wilson19957a32012-04-06 16:17:12 -0700792}
793
Paul McLeaneedc92e2013-12-19 15:46:15 -0800794/* must be called with hw device and output stream mutexes locked */
795static int start_output_stream(struct stream_out *out)
796{
jiabin8b265c92020-10-21 15:16:40 -0700797 int status = 0;
798 struct listnode *node;
799 list_for_each(node, &out->alsa_devices) {
800 struct alsa_device_info *device_info =
801 node_to_item(node, struct alsa_device_info, list_node);
802 ALOGV("start_output_stream(card:%d device:%d)",
803 device_info->profile.card, device_info->profile.device);
804 status = proxy_open(&device_info->proxy);
805 if (status != 0) {
806 ALOGE("%s failed to open device(card: %d device: %d)",
807 __func__, device_info->profile.card, device_info->profile.device);
808 goto exit;
809 }
810 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800811
jiabin8b265c92020-10-21 15:16:40 -0700812exit:
813 if (status != 0) {
814 list_for_each(node, &out->alsa_devices) {
815 struct alsa_device_info *device_info =
816 node_to_item(node, struct alsa_device_info, list_node);
817 proxy_close(&device_info->proxy);
818 }
819
820 }
821 return status;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800822}
823
824static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700825{
826 int ret;
827 struct stream_out *out = (struct stream_out *)stream;
828
Paul McLean994ac072016-06-02 15:33:24 -0600829 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700830 if (out->standby) {
831 ret = start_output_stream(out);
832 if (ret != 0) {
833 goto err;
834 }
835 out->standby = false;
836 }
Eric Laurent05333d42014-08-04 20:29:17 -0700837
jiabin8b265c92020-10-21 15:16:40 -0700838 struct listnode* node;
839 list_for_each(node, &out->alsa_devices) {
840 struct alsa_device_info* device_info =
841 node_to_item(node, struct alsa_device_info, list_node);
842 alsa_device_proxy* proxy = &device_info->proxy;
843 const void * write_buff = buffer;
844 int num_write_buff_bytes = bytes;
845 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
846 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
847 if (num_device_channels != num_req_channels) {
848 /* allocate buffer */
849 const size_t required_conversion_buffer_size =
850 bytes * num_device_channels / num_req_channels;
851 if (required_conversion_buffer_size > out->conversion_buffer_size) {
852 out->conversion_buffer_size = required_conversion_buffer_size;
853 out->conversion_buffer = realloc(out->conversion_buffer,
854 out->conversion_buffer_size);
855 }
856 /* convert data */
857 const audio_format_t audio_format = out_get_format(&(out->stream.common));
858 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
859 num_write_buff_bytes =
860 adjust_channels(write_buff, num_req_channels,
861 out->conversion_buffer, num_device_channels,
862 sample_size_in_bytes, num_write_buff_bytes);
863 write_buff = out->conversion_buffer;
Andy Hung03576be2014-07-21 21:16:45 -0700864 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800865
jiabin8b265c92020-10-21 15:16:40 -0700866 if (write_buff != NULL && num_write_buff_bytes != 0) {
867 proxy_write(proxy, write_buff, num_write_buff_bytes);
868 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800869 }
Simon Wilson19957a32012-04-06 16:17:12 -0700870
Paul McLean994ac072016-06-02 15:33:24 -0600871 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700872
873 return bytes;
874
875err:
Paul McLean994ac072016-06-02 15:33:24 -0600876 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700877 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700878 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700879 out_get_sample_rate(&stream->common));
880 }
881
882 return bytes;
883}
884
Paul McLean30f41852014-04-16 15:44:20 -0700885static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700886{
887 return -EINVAL;
888}
889
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700890static int out_get_presentation_position(const struct audio_stream_out *stream,
891 uint64_t *frames, struct timespec *timestamp)
892{
Andy Hungc9515ce2015-08-04 15:05:19 -0700893 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600894 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700895
jiabin8b265c92020-10-21 15:16:40 -0700896 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
897 const int ret = device_info == NULL ? -ENODEV :
898 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
Paul McLean994ac072016-06-02 15:33:24 -0600899 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700900 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700901}
902
Simon Wilson19957a32012-04-06 16:17:12 -0700903static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
904{
905 return 0;
906}
907
908static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
909{
910 return 0;
911}
912
Paul McLean30f41852014-04-16 15:44:20 -0700913static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700914{
915 return -EINVAL;
916}
917
Paul McLean76dba682016-06-01 12:29:11 -0600918static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700919 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600920 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700921 audio_output_flags_t flags,
922 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700923 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700924 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700925{
Paul McLean76dba682016-06-01 12:29:11 -0600926 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
927 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800928
Simon Wilson19957a32012-04-06 16:17:12 -0700929 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700930
931 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600932 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700933 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600934 }
Simon Wilson19957a32012-04-06 16:17:12 -0700935
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700936 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700937 out->stream.common.get_sample_rate = out_get_sample_rate;
938 out->stream.common.set_sample_rate = out_set_sample_rate;
939 out->stream.common.get_buffer_size = out_get_buffer_size;
940 out->stream.common.get_channels = out_get_channels;
941 out->stream.common.get_format = out_get_format;
942 out->stream.common.set_format = out_set_format;
943 out->stream.common.standby = out_standby;
944 out->stream.common.dump = out_dump;
945 out->stream.common.set_parameters = out_set_parameters;
946 out->stream.common.get_parameters = out_get_parameters;
947 out->stream.common.add_audio_effect = out_add_audio_effect;
948 out->stream.common.remove_audio_effect = out_remove_audio_effect;
949 out->stream.get_latency = out_get_latency;
950 out->stream.set_volume = out_set_volume;
951 out->stream.write = out_write;
952 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700953 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700954 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
955
jiabin400bbe02020-10-28 13:56:59 -0700956 out->handle = handle;
957
Paul McLean994ac072016-06-02 15:33:24 -0600958 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700959
Paul McLean76dba682016-06-01 12:29:11 -0600960 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700961
jiabin8b265c92020-10-21 15:16:40 -0700962 list_init(&out->alsa_devices);
963 struct alsa_device_info *device_info =
964 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
965 profile_init(&device_info->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700966
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700967 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700968 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800969
Paul McLean0f1753e2014-12-02 12:36:45 -0700970 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700971 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700972
jiabin8b265c92020-10-21 15:16:40 -0700973 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700974
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700975 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800976
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700977 /* Rate */
978 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700979 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
980 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700981 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800982 } else {
jiabin8b265c92020-10-21 15:16:40 -0700983 proxy_config.rate = config->sample_rate =
984 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700985 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800986 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800987
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600988 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -0700989 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600990 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600991 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600992
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700993 /* Format */
994 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -0700995 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700996 config->format = audio_format_from_pcm_format(proxy_config.format);
997 } else {
998 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -0700999 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001000 proxy_config.format = fmt;
1001 } else {
jiabin8b265c92020-10-21 15:16:40 -07001002 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001003 config->format = audio_format_from_pcm_format(proxy_config.format);
1004 ret = -EINVAL;
1005 }
1006 }
1007
1008 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001009 bool calc_mask = false;
1010 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1011 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001012 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001013 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -07001014 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001015 /* explicit case */
1016 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -07001017 }
Paul McLean698dbd72015-11-03 12:24:30 -08001018
Paul McLean64345f82016-06-06 13:26:05 -06001019 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001020 if (out->hal_channel_count > FCC_LIMIT) {
1021 out->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001022 calc_mask = true;
1023 }
1024
1025 if (calc_mask) {
1026 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001027 * 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 -06001028 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -07001029 /* position mask for mono and stereo*/
1030 ? audio_channel_out_mask_from_count(out->hal_channel_count)
1031 /* otherwise indexed */
1032 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
Paul McLean64345f82016-06-06 13:26:05 -06001033 }
1034
Andy Hung182ddc72015-05-05 23:37:30 -07001035 out->hal_channel_mask = config->channel_mask;
1036
Paul McLean64345f82016-06-06 13:26:05 -06001037 // Validate the "logical" channel count against support in the "actual" profile.
1038 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1039 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -07001040 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001041 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
1042 proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config);
jiabin400bbe02020-10-28 13:56:59 -07001043 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -07001044
1045 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001046
jiabin016fa212022-12-15 21:54:59 +00001047 if ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE) {
1048 out_stream_find_mixer_volume_control(out, device_info->profile.card);
1049 }
1050
Paul McLean96c4d302017-12-05 09:50:26 -08001051 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
1052 * So clear any errors that may have occurred above.
1053 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001054 ret = 0;
1055
Paul McLeaneedc92e2013-12-19 15:46:15 -08001056 out->conversion_buffer = NULL;
1057 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001058
1059 out->standby = true;
1060
Paul McLean6a75e4e2016-05-25 14:09:02 -06001061 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -06001062 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001063
Simon Wilson19957a32012-04-06 16:17:12 -07001064 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001065
1066 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001067}
1068
Paul McLean76dba682016-06-01 12:29:11 -06001069static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -07001070 struct audio_stream_out *stream)
1071{
1072 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001073
jiabin400bbe02020-10-28 13:56:59 -07001074 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001075 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -07001076 stream_standby_l(&out->alsa_devices, &out->standby);
1077 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001078
1079 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001080
Paul McLeaneedc92e2013-12-19 15:46:15 -08001081 out->conversion_buffer = NULL;
1082 out->conversion_buffer_size = 0;
1083
jiabin016fa212022-12-15 21:54:59 +00001084 if (out->volume_ctl != NULL) {
1085 for (int i = 0; i < out->volume_ctl_num_values; ++i) {
1086 mixer_ctl_set_value(out->volume_ctl, i, out->max_volume_level);
1087 }
1088 out->volume_ctl = NULL;
1089 }
1090 if (out->mixer != NULL) {
1091 mixer_close(out->mixer);
1092 out->mixer = NULL;
1093 }
1094
Paul McLean994ac072016-06-02 15:33:24 -06001095 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001096 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -06001097 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -06001098 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001099 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -06001100
Simon Wilson19957a32012-04-06 16:17:12 -07001101 free(stream);
1102}
1103
Paul McLean76dba682016-06-01 12:29:11 -06001104static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001105 const struct audio_config *config)
1106{
1107 /* TODO This needs to be calculated based on format/channels/rate */
1108 return 320;
1109}
1110
1111/*
1112 * IN functions
1113 */
1114static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1115{
jiabin8b265c92020-10-21 15:16:40 -07001116 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1117 &((const struct stream_in *)stream)->alsa_devices);
1118 if (device_info == NULL) {
1119 ALOGW("%s device info is null", __func__);
1120 return 0;
1121 }
1122 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001123 ALOGV("in_get_sample_rate() = %d", rate);
1124 return rate;
1125}
1126
1127static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1128{
1129 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
1130 return -ENOSYS;
1131}
1132
1133static size_t in_get_buffer_size(const struct audio_stream *stream)
1134{
1135 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -07001136 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1137 if (device_info == NULL) {
1138 ALOGW("%s device info is null", __func__);
1139 return 0;
1140 }
1141 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001142}
1143
1144static uint32_t in_get_channels(const struct audio_stream *stream)
1145{
Paul McLean2cfd81b2014-09-15 12:32:23 -07001146 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -07001147 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001148}
1149
1150static audio_format_t in_get_format(const struct audio_stream *stream)
1151{
jiabin8b265c92020-10-21 15:16:40 -07001152 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1153 &((const struct stream_in *)stream)->alsa_devices);
1154 if (device_info == NULL) {
1155 ALOGW("%s device info is null", __func__);
1156 return AUDIO_FORMAT_DEFAULT;
1157 }
1158 alsa_device_proxy *proxy = &device_info->proxy;
Andy Hung780f1f82015-04-22 22:14:39 -07001159 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1160 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001161}
1162
1163static int in_set_format(struct audio_stream *stream, audio_format_t format)
1164{
1165 ALOGV("in_set_format(%d) - NOPE", format);
1166
1167 return -ENOSYS;
1168}
1169
1170static int in_standby(struct audio_stream *stream)
1171{
1172 struct stream_in *in = (struct stream_in *)stream;
1173
Paul McLean994ac072016-06-02 15:33:24 -06001174 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -07001175 device_lock(in->adev);
1176 stream_standby_l(&in->alsa_devices, &in->standby);
1177 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -06001178 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001179 return 0;
1180}
1181
1182static int in_dump(const struct audio_stream *stream, int fd)
1183{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001184 const struct stream_in* in_stream = (const struct stream_in*)stream;
1185 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -07001186 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001187 }
1188
1189 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001190}
1191
1192static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1193{
Paul McLean65ec72b2015-02-18 09:13:24 -08001194 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001195
jiabin400bbe02020-10-28 13:56:59 -07001196 // The set parameters here only matters when the routing devices are changed.
1197 // When the device version higher than 3.0, the framework will use create_audio_patch
1198 // API instead of set_parameters to change audio routing.
1199 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001200}
1201
1202static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1203{
1204 struct stream_in *in = (struct stream_in *)stream;
1205
Paul McLean994ac072016-06-02 15:33:24 -06001206 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001207 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1208 char *params_str = NULL;
1209 if (device_info != NULL) {
1210 params_str = device_get_parameters(&device_info->profile, keys);
1211 }
Paul McLean994ac072016-06-02 15:33:24 -06001212 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001213
1214 return params_str;
1215}
1216
1217static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1218{
1219 return 0;
1220}
1221
1222static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1223{
1224 return 0;
1225}
1226
1227static int in_set_gain(struct audio_stream_in *stream, float gain)
1228{
1229 return 0;
1230}
1231
1232/* must be called with hw device and output stream mutexes locked */
1233static int start_input_stream(struct stream_in *in)
1234{
jiabin8b265c92020-10-21 15:16:40 -07001235 // Only care about the first device as only one input device is allowed.
1236 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1237 if (device_info == NULL) {
1238 return -ENODEV;
1239 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001240
jiabin8b265c92020-10-21 15:16:40 -07001241 ALOGV("start_input_stream(card:%d device:%d)",
1242 device_info->profile.card, device_info->profile.device);
1243 return proxy_open(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001244}
1245
1246/* TODO mutex stuff here (see out_write) */
1247static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1248{
1249 size_t num_read_buff_bytes = 0;
1250 void * read_buff = buffer;
1251 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001252 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001253
1254 struct stream_in * in = (struct stream_in *)stream;
1255
Paul McLean994ac072016-06-02 15:33:24 -06001256 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001257 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001258 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001259 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001260 goto err;
1261 }
1262 in->standby = false;
1263 }
1264
jiabin8b265c92020-10-21 15:16:40 -07001265 // Only care about the first device as only one input device is allowed.
1266 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1267 if (device_info == NULL) {
1268 return 0;
1269 }
1270
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001271 /*
1272 * OK, we need to figure out how much data to read to be able to output the requested
1273 * number of bytes in the HAL format (16-bit, stereo).
1274 */
1275 num_read_buff_bytes = bytes;
jiabin8b265c92020-10-21 15:16:40 -07001276 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001277 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001278
1279 if (num_device_channels != num_req_channels) {
1280 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1281 }
1282
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001283 /* Setup/Realloc the conversion buffer (if necessary). */
1284 if (num_read_buff_bytes != bytes) {
1285 if (num_read_buff_bytes > in->conversion_buffer_size) {
1286 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1287 (and do these conversions themselves) */
1288 in->conversion_buffer_size = num_read_buff_bytes;
1289 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1290 }
1291 read_buff = in->conversion_buffer;
1292 }
1293
jiabin8b265c92020-10-21 15:16:40 -07001294 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001295 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001296 if (num_device_channels != num_req_channels) {
1297 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1298
1299 out_buff = buffer;
1300 /* Num Channels conversion */
1301 if (num_device_channels != num_req_channels) {
1302 audio_format_t audio_format = in_get_format(&(in->stream.common));
1303 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1304
1305 num_read_buff_bytes =
1306 adjust_channels(read_buff, num_device_channels,
1307 out_buff, num_req_channels,
1308 sample_size_in_bytes, num_read_buff_bytes);
1309 }
1310 }
Eric Laurent253def92014-09-14 12:18:18 -07001311
Paul McLean76dba682016-06-01 12:29:11 -06001312 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1313 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -07001314 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301315 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001316 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001317 }
1318
1319err:
Paul McLean994ac072016-06-02 15:33:24 -06001320 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001321 return num_read_buff_bytes;
1322}
1323
1324static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1325{
1326 return 0;
1327}
1328
Andy Hungfa6b4a62018-06-04 19:14:22 -07001329static int in_get_capture_position(const struct audio_stream_in *stream,
1330 int64_t *frames, int64_t *time)
1331{
1332 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1333 stream_lock(&in->lock);
1334
jiabin8b265c92020-10-21 15:16:40 -07001335 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1336
1337 const int ret = device_info == NULL ? -ENODEV
1338 : proxy_get_capture_position(&device_info->proxy, frames, time);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001339
1340 stream_unlock(&in->lock);
1341 return ret;
1342}
1343
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001344static int in_get_active_microphones(const struct audio_stream_in *stream,
1345 struct audio_microphone_characteristic_t *mic_array,
1346 size_t *mic_count) {
1347 (void)stream;
1348 (void)mic_array;
1349 (void)mic_count;
1350
1351 return -ENOSYS;
1352}
1353
1354static int in_set_microphone_direction(const struct audio_stream_in *stream,
1355 audio_microphone_direction_t dir) {
1356 (void)stream;
1357 (void)dir;
1358 ALOGV("---- in_set_microphone_direction()");
1359 return -ENOSYS;
1360}
1361
1362static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1363 (void)zoom;
1364 ALOGV("---- in_set_microphone_field_dimension()");
1365 return -ENOSYS;
1366}
1367
Paul McLean76dba682016-06-01 12:29:11 -06001368static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001369 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001370 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001371 struct audio_config *config,
1372 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001373 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001374 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001375 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001376{
Paul McLean1d585cc2016-05-24 11:28:10 -06001377 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001378 config->sample_rate, config->channel_mask, config->format);
1379
Andy Hungb10ce6d2017-10-27 19:37:58 -07001380 /* Pull out the card/device pair */
1381 int32_t card, device;
1382 if (!parse_card_device_params(address, &card, &device)) {
1383 ALOGW("%s fail - invalid address %s", __func__, address);
1384 *stream_in = NULL;
1385 return -EINVAL;
1386 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001387
Andy Hungb10ce6d2017-10-27 19:37:58 -07001388 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001389 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001390 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001391 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001392 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001393
1394 /* setup function pointers */
1395 in->stream.common.get_sample_rate = in_get_sample_rate;
1396 in->stream.common.set_sample_rate = in_set_sample_rate;
1397 in->stream.common.get_buffer_size = in_get_buffer_size;
1398 in->stream.common.get_channels = in_get_channels;
1399 in->stream.common.get_format = in_get_format;
1400 in->stream.common.set_format = in_set_format;
1401 in->stream.common.standby = in_standby;
1402 in->stream.common.dump = in_dump;
1403 in->stream.common.set_parameters = in_set_parameters;
1404 in->stream.common.get_parameters = in_get_parameters;
1405 in->stream.common.add_audio_effect = in_add_audio_effect;
1406 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1407
1408 in->stream.set_gain = in_set_gain;
1409 in->stream.read = in_read;
1410 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001411 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001412
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001413 in->stream.get_active_microphones = in_get_active_microphones;
1414 in->stream.set_microphone_direction = in_set_microphone_direction;
1415 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1416
jiabin400bbe02020-10-28 13:56:59 -07001417 in->handle = handle;
1418
Paul McLean994ac072016-06-02 15:33:24 -06001419 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001420
Paul McLean76dba682016-06-01 12:29:11 -06001421 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001422
jiabin8b265c92020-10-21 15:16:40 -07001423 list_init(&in->alsa_devices);
1424 struct alsa_device_info *device_info =
1425 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1426 profile_init(&device_info->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001427
jiabin400bbe02020-10-28 13:56:59 -07001428 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001429
Andy Hungb10ce6d2017-10-27 19:37:58 -07001430 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001431 device_lock(in->adev);
1432 int num_open_inputs = in->adev->inputs_open;
1433 device_unlock(in->adev);
1434
Andy Hungb10ce6d2017-10-27 19:37:58 -07001435 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001436 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001437 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001438 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1439 __func__, card, device);
1440 ret = -EINVAL;
1441 }
1442 } else {
1443 /* Read input profile only if necessary */
jiabin8b265c92020-10-21 15:16:40 -07001444 device_info->profile.card = card;
1445 device_info->profile.device = device;
1446 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001447 ALOGW("%s fail - cannot read profile", __func__);
1448 ret = -EINVAL;
1449 }
1450 }
1451 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001452 free(in);
1453 *stream_in = NULL;
1454 return ret;
1455 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001456
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001457 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001458 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001459 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001460 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001461 }
1462
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001463 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001464 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001465 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001466 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001467 if (highest_rate == 0) {
1468 ret = -EINVAL; /* error with device */
1469 } else {
jiabin400bbe02020-10-28 13:56:59 -07001470 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001471 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001472 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001473 /* Changing the requested rate */
1474 ret = -EINVAL;
1475 } else {
1476 /* Everything AOK! */
1477 ret = 0;
1478 }
1479 }
1480 }
jiabin8b265c92020-10-21 15:16:40 -07001481 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001482 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001483 } else {
jiabin400bbe02020-10-28 13:56:59 -07001484 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001485 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001486 ret = -EINVAL;
1487 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001488
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001489 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001490 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001491 in->config.format = profile_get_default_format(&device_info->profile);
1492 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001493 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001494 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001495 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001496 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001497 } else {
jiabin400bbe02020-10-28 13:56:59 -07001498 in->config.format = profile_get_default_format(&device_info->profile);
1499 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001500 ret = -EINVAL;
1501 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001502 }
1503
Paul McLean2cfd81b2014-09-15 12:32:23 -07001504 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001505 bool calc_mask = false;
1506 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1507 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001508 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001509 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001510 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001511 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001512 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1513 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001514
Paul McLean64345f82016-06-06 13:26:05 -06001515 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001516 if (in->hal_channel_count > FCC_LIMIT) {
1517 in->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001518 calc_mask = true;
1519 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001520
Paul McLean64345f82016-06-06 13:26:05 -06001521 if (calc_mask) {
1522 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001523 * 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 -06001524 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1525 /* position mask for mono & stereo */
1526 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1527 /* otherwise indexed */
1528 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001529
Paul McLean64345f82016-06-06 13:26:05 -06001530 // if we change the mask...
1531 if (in->hal_channel_mask != config->channel_mask &&
1532 config->channel_mask != AUDIO_CHANNEL_NONE) {
1533 config->channel_mask = in->hal_channel_mask;
1534 ret = -EINVAL;
1535 }
1536 } else {
1537 in->hal_channel_mask = config->channel_mask;
1538 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001539
Paul McLean64345f82016-06-06 13:26:05 -06001540 if (ret == 0) {
1541 // Validate the "logical" channel count against support in the "actual" profile.
1542 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1543 // and store THAT in proxy_config.channels
jiabin400bbe02020-10-28 13:56:59 -07001544 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001545 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabin400bbe02020-10-28 13:56:59 -07001546 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config);
Eric Laurent981f7742017-05-03 12:44:26 -07001547 if (ret == 0) {
1548 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001549
Eric Laurent981f7742017-05-03 12:44:26 -07001550 in->conversion_buffer = NULL;
1551 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001552
Eric Laurent981f7742017-05-03 12:44:26 -07001553 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001554
Eric Laurent981f7742017-05-03 12:44:26 -07001555 /* Save this for adev_dump() */
1556 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1557 } else {
1558 ALOGW("proxy_prepare error %d", ret);
jiabin8b265c92020-10-21 15:16:40 -07001559 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001560 config->channel_mask = channel_count <= FCC_2
1561 ? audio_channel_in_mask_from_count(channel_count)
1562 : audio_channel_mask_for_index_assignment_from_count(channel_count);
jiabin8b265c92020-10-21 15:16:40 -07001563 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1564 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001565 }
1566 }
Paul McLean64345f82016-06-06 13:26:05 -06001567
Eric Laurent981f7742017-05-03 12:44:26 -07001568 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001569 // Deallocate this stream on error, because AudioFlinger won't call
1570 // adev_close_input_stream() in this case.
1571 *stream_in = NULL;
1572 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001573 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001574 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001575
jiabin8b265c92020-10-21 15:16:40 -07001576 list_add_tail(&in->alsa_devices, &device_info->list_node);
1577
Andy Hungb10ce6d2017-10-27 19:37:58 -07001578 device_lock(in->adev);
1579 ++in->adev->inputs_open;
1580 device_unlock(in->adev);
1581
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001582 return ret;
1583}
1584
Paul McLean76dba682016-06-01 12:29:11 -06001585static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1586 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001587{
1588 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001589
jiabin400bbe02020-10-28 13:56:59 -07001590 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001591 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001592 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001593 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001594 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1595 if (device_info != NULL) {
1596 ALOGV("adev_close_input_stream(c:%d d:%d)",
1597 device_info->profile.card, device_info->profile.device);
1598 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001599 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1600 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001601
1602 stream_standby_l(&in->alsa_devices, &in->standby);
1603
Andy Hungb10ce6d2017-10-27 19:37:58 -07001604 device_unlock(in->adev);
1605
jiabin400bbe02020-10-28 13:56:59 -07001606 stream_clear_devices(&in->alsa_devices);
1607 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001608
1609 free(in->conversion_buffer);
1610
1611 free(stream);
1612}
1613
1614/*
1615 * ADEV Functions
1616 */
Paul McLean76dba682016-06-01 12:29:11 -06001617static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001618{
1619 return 0;
1620}
1621
Paul McLean76dba682016-06-01 12:29:11 -06001622static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001623{
1624 return strdup("");
1625}
1626
Paul McLean76dba682016-06-01 12:29:11 -06001627static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001628{
1629 return 0;
1630}
1631
Paul McLean76dba682016-06-01 12:29:11 -06001632static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001633{
1634 return -ENOSYS;
1635}
1636
Paul McLean76dba682016-06-01 12:29:11 -06001637static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001638{
1639 return -ENOSYS;
1640}
1641
Paul McLean76dba682016-06-01 12:29:11 -06001642static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001643{
1644 return 0;
1645}
1646
Paul McLean76dba682016-06-01 12:29:11 -06001647static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001648{
Paul McLean76dba682016-06-01 12:29:11 -06001649 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001650 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001651 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001652 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001653 return -ENOSYS;
1654}
1655
Paul McLean76dba682016-06-01 12:29:11 -06001656static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001657{
1658 return -ENOSYS;
1659}
1660
jiabin400bbe02020-10-28 13:56:59 -07001661static int adev_create_audio_patch(struct audio_hw_device *dev,
1662 unsigned int num_sources,
1663 const struct audio_port_config *sources,
1664 unsigned int num_sinks,
1665 const struct audio_port_config *sinks,
1666 audio_patch_handle_t *handle) {
1667 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1668 // Only accept mix->device and device->mix cases. In that case, the number of sources
1669 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1670 return -EINVAL;
1671 }
1672
1673 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1674 // If source is a device, the number of sinks should be 1.
1675 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1676 return -EINVAL;
1677 }
1678 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1679 // If source is a mix, all sinks should be device.
1680 for (unsigned int i = 0; i < num_sinks; i++) {
1681 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1682 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1683 return -EINVAL;
1684 }
1685 }
1686 } else {
1687 // All other cases are invalid.
1688 return -EINVAL;
1689 }
1690
1691 struct audio_device* adev = (struct audio_device*) dev;
1692 bool generatedPatchHandle = false;
1693 device_lock(adev);
1694 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1695 *handle = ++adev->next_patch_handle;
1696 generatedPatchHandle = true;
1697 }
1698
1699 int cards[AUDIO_PATCH_PORTS_MAX];
1700 int devices[AUDIO_PATCH_PORTS_MAX];
1701 const struct audio_port_config *port_configs =
1702 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1703 int num_configs = 0;
1704 audio_io_handle_t io_handle = 0;
1705 bool wasStandby = true;
1706 int direction = PCM_OUT;
1707 audio_patch_handle_t *patch_handle = NULL;
1708 struct listnode *alsa_devices = NULL;
1709 struct stream_lock *lock = NULL;
1710 struct pcm_config *config = NULL;
1711 struct stream_in *in = NULL;
1712 struct stream_out *out = NULL;
1713
1714 unsigned int num_saved_devices = 0;
1715 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1716 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1717
1718 struct listnode *node;
1719
1720 // Only handle patches for mix->devices and device->mix case.
1721 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1722 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1723 if (in == NULL) {
1724 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1725 device_unlock(adev);
1726 return -EINVAL;
1727 }
1728
1729 direction = PCM_IN;
1730 wasStandby = in->standby;
1731 io_handle = in->handle;
1732 num_configs = num_sources;
1733 patch_handle = &in->patch_handle;
1734 alsa_devices = &in->alsa_devices;
1735 lock = &in->lock;
1736 config = &in->config;
1737 } else {
1738 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1739 if (out == NULL) {
1740 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1741 device_unlock(adev);
1742 return -EINVAL;
1743 }
1744
1745 direction = PCM_OUT;
1746 wasStandby = out->standby;
1747 io_handle = out->handle;
1748 num_configs = num_sinks;
1749 patch_handle = &out->patch_handle;
1750 alsa_devices = &out->alsa_devices;
1751 lock = &out->lock;
1752 config = &out->config;
1753 }
1754
1755 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1756 if (!generatedPatchHandle && *patch_handle != *handle) {
1757 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1758 "with handle(%d) when creating audio patch",
1759 __func__, *handle, *patch_handle, io_handle);
1760 device_unlock(adev);
1761 return -EINVAL;
1762 }
1763 device_unlock(adev);
1764
1765 for (unsigned int i = 0; i < num_configs; ++i) {
1766 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1767 ALOGE("%s, failed to parse card and device %s",
1768 __func__, port_configs[i].ext.device.address);
1769 return -EINVAL;
1770 }
1771 }
1772
1773 stream_lock(lock);
1774 list_for_each (node, alsa_devices) {
1775 struct alsa_device_info *device_info =
1776 node_to_item(node, struct alsa_device_info, list_node);
1777 saved_cards[num_saved_devices] = device_info->profile.card;
1778 saved_devices[num_saved_devices++] = device_info->profile.device;
1779 }
1780
jiabin4bc5f252021-06-02 18:31:13 +00001781 if (are_devices_the_same(
1782 num_configs, cards, devices, num_saved_devices, saved_cards, saved_devices)) {
1783 // The new devices are the same as original ones. No need to update.
1784 stream_unlock(lock);
1785 return 0;
1786 }
1787
jiabin400bbe02020-10-28 13:56:59 -07001788 device_lock(adev);
1789 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1790 device_unlock(adev);
1791
Andy Hung61583422021-07-02 16:13:11 -07001792 // Timestamps:
1793 // Audio timestamps assume continuous PCM frame counts which are maintained
1794 // with the device proxy.transferred variable. Technically it would be better
1795 // associated with in or out stream, not the device; here we save and restore
1796 // using the first alsa device as a simplification.
1797 uint64_t saved_transferred_frames = 0;
1798 struct alsa_device_info *device_info = stream_get_first_alsa_device(alsa_devices);
1799 if (device_info != NULL) saved_transferred_frames = device_info->proxy.transferred;
1800
jiabin400bbe02020-10-28 13:56:59 -07001801 int ret = stream_set_new_devices(config, alsa_devices, num_configs, cards, devices, direction);
1802
1803 if (ret != 0) {
1804 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1805 stream_set_new_devices(
1806 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction);
1807 } else {
1808 *patch_handle = *handle;
1809 }
Andy Hung61583422021-07-02 16:13:11 -07001810
1811 // Timestamps: Restore transferred frames.
1812 if (saved_transferred_frames != 0) {
1813 device_info = stream_get_first_alsa_device(alsa_devices);
1814 if (device_info != NULL) device_info->proxy.transferred = saved_transferred_frames;
1815 }
1816
jiabin400bbe02020-10-28 13:56:59 -07001817 if (!wasStandby) {
1818 device_lock(adev);
1819 if (in != NULL) {
1820 start_input_stream(in);
1821 }
1822 if (out != NULL) {
1823 start_output_stream(out);
1824 }
1825 device_unlock(adev);
1826 }
1827 stream_unlock(lock);
1828 return ret;
1829}
1830
1831static int adev_release_audio_patch(struct audio_hw_device *dev,
1832 audio_patch_handle_t patch_handle)
1833{
1834 struct audio_device* adev = (struct audio_device*) dev;
1835
1836 device_lock(adev);
1837 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1838 device_unlock(adev);
1839 if (out != NULL) {
1840 stream_lock(&out->lock);
1841 device_lock(adev);
1842 stream_standby_l(&out->alsa_devices, &out->standby);
1843 device_unlock(adev);
1844 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1845 stream_unlock(&out->lock);
1846 return 0;
1847 }
1848
1849 device_lock(adev);
1850 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1851 device_unlock(adev);
1852 if (in != NULL) {
1853 stream_lock(&in->lock);
1854 device_lock(adev);
1855 stream_standby_l(&in->alsa_devices, &in->standby);
1856 device_unlock(adev);
1857 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1858 stream_unlock(&in->lock);
1859 return 0;
1860 }
1861
1862 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1863 return -EINVAL;
1864}
1865
1866static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1867{
1868 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1869 return -EINVAL;
1870 }
1871
1872 alsa_device_profile profile;
1873 const bool is_output = audio_is_output_device(port->ext.device.type);
1874 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1875 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1876 return -EINVAL;
1877 }
1878
1879 if (!profile_read_device_info(&profile)) {
1880 return -ENOENT;
1881 }
1882
1883 port->num_formats = 0;;
1884 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1885 profile.formats[i] != 0; ++i) {
1886 audio_format_t format = audio_format_from(profile.formats[i]);
1887 if (format != AUDIO_FORMAT_INVALID) {
1888 port->formats[port->num_formats++] = format;
1889 }
1890 }
1891
jiabina635fcf2021-01-11 11:34:50 -08001892 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1893 port->num_channel_masks = populate_channel_mask_from_profile(
1894 &profile, is_output, port->channel_masks);
jiabin400bbe02020-10-28 13:56:59 -07001895
jiabina635fcf2021-01-11 11:34:50 -08001896 return 0;
1897}
1898
1899static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1900{
1901 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1902 return -EINVAL;
jiabin400bbe02020-10-28 13:56:59 -07001903 }
jiabina635fcf2021-01-11 11:34:50 -08001904
1905 alsa_device_profile profile;
1906 const bool is_output = audio_is_output_device(port->ext.device.type);
1907 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1908 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1909 return -EINVAL;
1910 }
1911
1912 if (!profile_read_device_info(&profile)) {
1913 return -ENOENT;
1914 }
1915
1916 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1917 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1918 &profile, is_output, channel_masks);
1919 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1920 const unsigned int num_sample_rates =
1921 populate_sample_rates_from_profile(&profile, sample_rates);
1922 port->num_audio_profiles = 0;;
1923 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1924 profile.formats[i] != 0; ++i) {
1925 audio_format_t format = audio_format_from(profile.formats[i]);
1926 if (format == AUDIO_FORMAT_INVALID) {
1927 continue;
1928 }
1929 const unsigned int j = port->num_audio_profiles++;
1930 port->audio_profiles[j].format = format;
1931 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1932 memcpy(port->audio_profiles[j].sample_rates,
1933 sample_rates,
1934 num_sample_rates * sizeof(unsigned int));
1935 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1936 memcpy(port->audio_profiles[j].channel_masks,
1937 channel_masks,
1938 num_channel_masks* sizeof(audio_channel_mask_t));
1939 }
1940
jiabin400bbe02020-10-28 13:56:59 -07001941 return 0;
1942}
1943
Paul McLean76dba682016-06-01 12:29:11 -06001944static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001945{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001946 dprintf(fd, "\nUSB audio module:\n");
1947
1948 struct audio_device* adev = (struct audio_device*)device;
1949 const int kNumRetries = 3;
1950 const int kSleepTimeMS = 500;
1951
Paul McLean994ac072016-06-02 15:33:24 -06001952 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001953 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001954 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001955 sleep(kSleepTimeMS);
1956 retry--;
1957 }
1958
1959 if (retry > 0) {
1960 if (list_empty(&adev->output_stream_list)) {
1961 dprintf(fd, " No output streams.\n");
1962 } else {
1963 struct listnode* node;
1964 list_for_each(node, &adev->output_stream_list) {
1965 struct audio_stream* stream =
1966 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1967 out_dump(stream, fd);
1968 }
1969 }
1970
1971 if (list_empty(&adev->input_stream_list)) {
1972 dprintf(fd, "\n No input streams.\n");
1973 } else {
1974 struct listnode* node;
1975 list_for_each(node, &adev->input_stream_list) {
1976 struct audio_stream* stream =
1977 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1978 in_dump(stream, fd);
1979 }
1980 }
1981
Paul McLean994ac072016-06-02 15:33:24 -06001982 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001983 } else {
1984 // Couldn't lock
1985 dprintf(fd, " Could not obtain device lock.\n");
1986 }
1987
Simon Wilson19957a32012-04-06 16:17:12 -07001988 return 0;
1989}
1990
1991static int adev_close(hw_device_t *device)
1992{
Simon Wilson19957a32012-04-06 16:17:12 -07001993 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001994
Simon Wilson19957a32012-04-06 16:17:12 -07001995 return 0;
1996}
1997
Paul McLean30f41852014-04-16 15:44:20 -07001998static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001999{
Simon Wilson19957a32012-04-06 16:17:12 -07002000 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
2001 return -EINVAL;
2002
Paul McLeaneedc92e2013-12-19 15:46:15 -08002003 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07002004 if (!adev)
2005 return -ENOMEM;
2006
Paul McLeancf5310a2018-08-22 14:33:12 -07002007 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07002008
Paul McLean6a75e4e2016-05-25 14:09:02 -06002009 list_init(&adev->output_stream_list);
2010 list_init(&adev->input_stream_list);
2011
Simon Wilson19957a32012-04-06 16:17:12 -07002012 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabina635fcf2021-01-11 11:34:50 -08002013 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07002014 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07002015 adev->hw_device.common.close = adev_close;
2016
Simon Wilson19957a32012-04-06 16:17:12 -07002017 adev->hw_device.init_check = adev_init_check;
2018 adev->hw_device.set_voice_volume = adev_set_voice_volume;
2019 adev->hw_device.set_master_volume = adev_set_master_volume;
2020 adev->hw_device.set_mode = adev_set_mode;
2021 adev->hw_device.set_mic_mute = adev_set_mic_mute;
2022 adev->hw_device.get_mic_mute = adev_get_mic_mute;
2023 adev->hw_device.set_parameters = adev_set_parameters;
2024 adev->hw_device.get_parameters = adev_get_parameters;
2025 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
2026 adev->hw_device.open_output_stream = adev_open_output_stream;
2027 adev->hw_device.close_output_stream = adev_close_output_stream;
2028 adev->hw_device.open_input_stream = adev_open_input_stream;
2029 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07002030 adev->hw_device.create_audio_patch = adev_create_audio_patch;
2031 adev->hw_device.release_audio_patch = adev_release_audio_patch;
2032 adev->hw_device.get_audio_port = adev_get_audio_port;
jiabina635fcf2021-01-11 11:34:50 -08002033 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
Simon Wilson19957a32012-04-06 16:17:12 -07002034 adev->hw_device.dump = adev_dump;
2035
2036 *device = &adev->hw_device.common;
2037
2038 return 0;
2039}
2040
2041static struct hw_module_methods_t hal_module_methods = {
2042 .open = adev_open,
2043};
2044
2045struct audio_module HAL_MODULE_INFO_SYM = {
2046 .common = {
2047 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07002048 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2049 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07002050 .id = AUDIO_HARDWARE_MODULE_ID,
2051 .name = "USB audio HW HAL",
2052 .author = "The Android Open Source Project",
2053 .methods = &hal_module_methods,
2054 },
2055};