blob: 39c0fb5c16fb757bb0e8a51a0f2f7cd46c78188e [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>
Simon Wilson19957a32012-04-06 16:17:12 -070022#include <pthread.h>
23#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070024#include <stdlib.h>
jiabin400bbe02020-10-28 13:56:59 -070025#include <string.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070026#include <sys/time.h>
Tri Vo00a86732017-06-27 11:33:36 -070027#include <unistd.h>
Simon Wilson19957a32012-04-06 16:17:12 -070028
Mark Salyzyn88e458a2014-04-28 12:30:44 -070029#include <log/log.h>
Paul McLean6a75e4e2016-05-25 14:09:02 -060030#include <cutils/list.h>
Simon Wilson19957a32012-04-06 16:17:12 -070031#include <cutils/str_parms.h>
32#include <cutils/properties.h>
33
Simon Wilson19957a32012-04-06 16:17:12 -070034#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070035#include <hardware/audio_alsaops.h>
36#include <hardware/hardware.h>
37
38#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070039
40#include <tinyalsa/asoundlib.h>
41
Paul McLeanc2201152014-07-16 13:46:07 -070042#include <audio_utils/channels.h>
43
Paul McLeanc88e6ae2014-07-16 09:48:34 -070044#include "alsa_device_profile.h"
45#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080046#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070047
Paul McLean1d585cc2016-05-24 11:28:10 -060048/* Lock play & record samples rates at or above this threshold */
49#define RATELOCK_THRESHOLD 96000
50
Paul McLeancfdbd6b2018-07-27 11:16:02 -060051#define max(a, b) ((a) > (b) ? (a) : (b))
52#define min(a, b) ((a) < (b) ? (a) : (b))
53
Simon Wilson19957a32012-04-06 16:17:12 -070054struct audio_device {
55 struct audio_hw_device hw_device;
56
57 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080058
59 /* output */
Paul McLean6a75e4e2016-05-25 14:09:02 -060060 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080061
62 /* input */
Paul McLean6a75e4e2016-05-25 14:09:02 -060063 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080064
Paul McLean1d585cc2016-05-24 11:28:10 -060065 /* lock input & output sample rates */
66 /*FIXME - How do we address multiple output streams? */
Paul McLeancfdbd6b2018-07-27 11:16:02 -060067 uint32_t device_sample_rate; // this should be a rate that is common to both input & output
Paul McLean1d585cc2016-05-24 11:28:10 -060068
Eric Laurent253def92014-09-14 12:18:18 -070069 bool mic_muted;
70
Andy Hungb10ce6d2017-10-27 19:37:58 -070071 int32_t inputs_open; /* number of input streams currently open. */
jiabin400bbe02020-10-28 13:56:59 -070072
73 audio_patch_handle_t next_patch_handle; // Increase 1 when create audio patch
Simon Wilson19957a32012-04-06 16:17:12 -070074};
75
Paul McLean994ac072016-06-02 15:33:24 -060076struct stream_lock {
77 pthread_mutex_t lock; /* see note below on mutex acquisition order */
78 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
79};
80
jiabin8b265c92020-10-21 15:16:40 -070081struct alsa_device_info {
82 alsa_device_profile profile; /* The profile of the ALSA device */
83 alsa_device_proxy proxy; /* The state */
84 struct listnode list_node;
85};
86
Simon Wilson19957a32012-04-06 16:17:12 -070087struct stream_out {
88 struct audio_stream_out stream;
89
Paul McLeancf5310a2018-08-22 14:33:12 -070090 struct stream_lock lock;
Paul McLean994ac072016-06-02 15:33:24 -060091
Paul McLeaneedc92e2013-12-19 15:46:15 -080092 bool standby;
93
Paul McLean76dba682016-06-01 12:29:11 -060094 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070095
jiabin8b265c92020-10-21 15:16:40 -070096 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
Paul McLeaneedc92e2013-12-19 15:46:15 -080097
Andy Hung03576be2014-07-21 21:16:45 -070098 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
99 * This may differ from the device channel count when
100 * the device is not compatible with AudioFlinger
101 * capabilities, e.g. exposes too many channels or
102 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600103 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
104 * so the proxy doesn't have a channel_mask, but
105 * audio HALs need to talk about channel masks
106 * so expose the one calculated by
107 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700108
Paul McLean6a75e4e2016-05-25 14:09:02 -0600109 struct listnode list_node;
110
Paul McLeaneedc92e2013-12-19 15:46:15 -0800111 void * conversion_buffer; /* any conversions are put into here
112 * they could come from here too if
113 * there was a previous conversion */
114 size_t conversion_buffer_size; /* in bytes */
jiabin400bbe02020-10-28 13:56:59 -0700115
116 struct pcm_config config;
117
118 audio_io_handle_t handle; // Unique constant for a stream
119
120 audio_patch_handle_t patch_handle; // Patch handle for this stream
Paul McLeaneedc92e2013-12-19 15:46:15 -0800121};
122
Paul McLeaneedc92e2013-12-19 15:46:15 -0800123struct stream_in {
124 struct audio_stream_in stream;
125
Paul McLean994ac072016-06-02 15:33:24 -0600126 struct stream_lock lock;
127
Simon Wilson19957a32012-04-06 16:17:12 -0700128 bool standby;
129
Paul McLean76dba682016-06-01 12:29:11 -0600130 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800131
jiabin8b265c92020-10-21 15:16:40 -0700132 struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700133
Paul McLean2cfd81b2014-09-15 12:32:23 -0700134 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
135 * This may differ from the device channel count when
136 * the device is not compatible with AudioFlinger
137 * capabilities, e.g. exposes too many channels or
138 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600139 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
140 * so the proxy doesn't have a channel_mask, but
141 * audio HALs need to talk about channel masks
142 * so expose the one calculated by
143 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700144
Paul McLean6a75e4e2016-05-25 14:09:02 -0600145 struct listnode list_node;
146
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700147 /* 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 -0700148 void * conversion_buffer; /* any conversions are put into here
149 * they could come from here too if
150 * there was a previous conversion */
151 size_t conversion_buffer_size; /* in bytes */
jiabin400bbe02020-10-28 13:56:59 -0700152
153 struct pcm_config config;
154
155 audio_io_handle_t handle; // Unique identifier for a stream
156
157 audio_patch_handle_t patch_handle; // Patch handle for this stream
Simon Wilson19957a32012-04-06 16:17:12 -0700158};
159
jiabin400bbe02020-10-28 13:56:59 -0700160// Map channel count to output channel mask
Andy Hungdf511202021-06-07 16:45:03 -0700161static const audio_channel_mask_t OUT_CHANNEL_MASKS_MAP[FCC_24 + 1] = {
162 [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted)
163 // != AUDIO_CHANNEL_INVALID == 0xC0000000u
164
165 [1] = AUDIO_CHANNEL_OUT_MONO,
166 [2] = AUDIO_CHANNEL_OUT_STEREO,
167 [3] = AUDIO_CHANNEL_OUT_2POINT1,
168 [4] = AUDIO_CHANNEL_OUT_QUAD,
169 [5] = AUDIO_CHANNEL_OUT_PENTA,
170 [6] = AUDIO_CHANNEL_OUT_5POINT1,
171 [7] = AUDIO_CHANNEL_OUT_6POINT1,
172 [8] = AUDIO_CHANNEL_OUT_7POINT1,
173
174 [9 ... 11] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
175
176 [12] = AUDIO_CHANNEL_OUT_7POINT1POINT4,
177
178 [13 ... 23] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
179
180 [24] = AUDIO_CHANNEL_OUT_22POINT2,
jiabin400bbe02020-10-28 13:56:59 -0700181};
182static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP);
183
184// Map channel count to input channel mask
185static const audio_channel_mask_t IN_CHANNEL_MASKS_MAP[] = {
186 AUDIO_CHANNEL_NONE, /* 0 */
187 AUDIO_CHANNEL_IN_MONO, /* 1 */
188 AUDIO_CHANNEL_IN_STEREO, /* 2 */
189 /* channel counts greater than this are not considered */
190};
191static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP);
192
193// Map channel count to index mask
Andy Hungdf511202021-06-07 16:45:03 -0700194static const audio_channel_mask_t CHANNEL_INDEX_MASKS_MAP[FCC_24 + 1] = {
195 [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
196
197 [1] = AUDIO_CHANNEL_INDEX_MASK_1,
198 [2] = AUDIO_CHANNEL_INDEX_MASK_2,
199 [3] = AUDIO_CHANNEL_INDEX_MASK_3,
200 [4] = AUDIO_CHANNEL_INDEX_MASK_4,
201 [5] = AUDIO_CHANNEL_INDEX_MASK_5,
202 [6] = AUDIO_CHANNEL_INDEX_MASK_6,
203 [7] = AUDIO_CHANNEL_INDEX_MASK_7,
204 [8] = AUDIO_CHANNEL_INDEX_MASK_8,
205
206 [9] = AUDIO_CHANNEL_INDEX_MASK_9,
207 [10] = AUDIO_CHANNEL_INDEX_MASK_10,
208 [11] = AUDIO_CHANNEL_INDEX_MASK_11,
209 [12] = AUDIO_CHANNEL_INDEX_MASK_12,
210 [13] = AUDIO_CHANNEL_INDEX_MASK_13,
211 [14] = AUDIO_CHANNEL_INDEX_MASK_14,
212 [15] = AUDIO_CHANNEL_INDEX_MASK_15,
213 [16] = AUDIO_CHANNEL_INDEX_MASK_16,
214
215 [17] = AUDIO_CHANNEL_INDEX_MASK_17,
216 [18] = AUDIO_CHANNEL_INDEX_MASK_18,
217 [19] = AUDIO_CHANNEL_INDEX_MASK_19,
218 [20] = AUDIO_CHANNEL_INDEX_MASK_20,
219 [21] = AUDIO_CHANNEL_INDEX_MASK_21,
220 [22] = AUDIO_CHANNEL_INDEX_MASK_22,
221 [23] = AUDIO_CHANNEL_INDEX_MASK_23,
222 [24] = AUDIO_CHANNEL_INDEX_MASK_24,
jiabin400bbe02020-10-28 13:56:59 -0700223};
224static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP);
225
Paul McLean994ac072016-06-02 15:33:24 -0600226/*
227 * Locking Helpers
228 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800229/*
Eric Laurent70318092015-06-19 17:49:17 -0700230 * NOTE: when multiple mutexes have to be acquired, always take the
231 * stream_in or stream_out mutex first, followed by the audio_device mutex.
232 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
233 * higher priority playback or capture thread.
234 */
235
Paul McLean994ac072016-06-02 15:33:24 -0600236static void stream_lock_init(struct stream_lock *lock) {
237 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
238 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
239}
240
241static void stream_lock(struct stream_lock *lock) {
jiabin400bbe02020-10-28 13:56:59 -0700242 if (lock == NULL) {
243 return;
244 }
Paul McLean994ac072016-06-02 15:33:24 -0600245 pthread_mutex_lock(&lock->pre_lock);
246 pthread_mutex_lock(&lock->lock);
247 pthread_mutex_unlock(&lock->pre_lock);
248}
249
250static void stream_unlock(struct stream_lock *lock) {
251 pthread_mutex_unlock(&lock->lock);
252}
253
254static void device_lock(struct audio_device *adev) {
255 pthread_mutex_lock(&adev->lock);
256}
257
258static int device_try_lock(struct audio_device *adev) {
259 return pthread_mutex_trylock(&adev->lock);
260}
261
262static void device_unlock(struct audio_device *adev) {
263 pthread_mutex_unlock(&adev->lock);
264}
265
266/*
267 * streams list management
268 */
269static void adev_add_stream_to_list(
270 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
271 device_lock(adev);
272
273 list_add_tail(list, stream_node);
274
275 device_unlock(adev);
276}
277
jiabin400bbe02020-10-28 13:56:59 -0700278static struct stream_out* adev_get_stream_out_by_io_handle_l(
279 struct audio_device* adev, audio_io_handle_t handle) {
280 struct listnode *node;
281 list_for_each (node, &adev->output_stream_list) {
282 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
283 if (out->handle == handle) {
284 return out;
285 }
286 }
287 return NULL;
288}
Paul McLean994ac072016-06-02 15:33:24 -0600289
jiabin400bbe02020-10-28 13:56:59 -0700290static struct stream_in* adev_get_stream_in_by_io_handle_l(
291 struct audio_device* adev, audio_io_handle_t handle) {
292 struct listnode *node;
293 list_for_each (node, &adev->input_stream_list) {
294 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
295 if (in->handle == handle) {
296 return in;
297 }
298 }
299 return NULL;
300}
Paul McLean994ac072016-06-02 15:33:24 -0600301
jiabin400bbe02020-10-28 13:56:59 -0700302static struct stream_out* adev_get_stream_out_by_patch_handle_l(
303 struct audio_device* adev, audio_patch_handle_t patch_handle) {
304 struct listnode *node;
305 list_for_each (node, &adev->output_stream_list) {
306 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
307 if (out->patch_handle == patch_handle) {
308 return out;
309 }
310 }
311 return NULL;
312}
313
314static struct stream_in* adev_get_stream_in_by_patch_handle_l(
315 struct audio_device* adev, audio_patch_handle_t patch_handle) {
316 struct listnode *node;
317 list_for_each (node, &adev->input_stream_list) {
318 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
319 if (in->patch_handle == patch_handle) {
320 return in;
321 }
322 }
323 return NULL;
Paul McLean994ac072016-06-02 15:33:24 -0600324}
325
Eric Laurent70318092015-06-19 17:49:17 -0700326/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700327 * Extract the card and device numbers from the supplied key/value pairs.
328 * kvpairs A null-terminated string containing the key/value pairs or card and device.
329 * i.e. "card=1;device=42"
330 * card A pointer to a variable to receive the parsed-out card number.
331 * device A pointer to a variable to receive the parsed-out device number.
332 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
333 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800334 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700335 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800336static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700337{
338 struct str_parms * parms = str_parms_create_str(kvpairs);
339 char value[32];
340 int param_val;
341
342 // initialize to "undefined" state.
343 *card = -1;
344 *device = -1;
345
346 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
347 if (param_val >= 0) {
348 *card = atoi(value);
349 }
350
351 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
352 if (param_val >= 0) {
353 *device = atoi(value);
354 }
355
356 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800357
358 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700359}
360
Andy Hung4e6a1c02017-10-27 20:31:46 -0700361static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800362{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700363 if (profile->card < 0 || profile->device < 0) {
364 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800365 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700366
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700367 struct str_parms *query = str_parms_create_str(keys);
368 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700369
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700370 /* These keys are from hardware/libhardware/include/audio.h */
371 /* supported sample rates */
372 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
373 char* rates_list = profile_get_sample_rate_strs(profile);
374 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
375 rates_list);
376 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700377 }
378
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700379 /* supported channel counts */
380 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
381 char* channels_list = profile_get_channel_count_strs(profile);
382 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
383 channels_list);
384 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700385 }
386
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700387 /* supported sample formats */
388 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
389 char * format_params = profile_get_format_strs(profile);
390 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
391 format_params);
392 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700393 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700394 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700395
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700396 char* result_str = str_parms_to_str(result);
397 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700398
Paul McLean65ec72b2015-02-18 09:13:24 -0800399 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700400
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700401 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800402}
403
jiabin400bbe02020-10-28 13:56:59 -0700404static audio_format_t audio_format_from(enum pcm_format format)
405{
406 switch (format) {
407 case PCM_FORMAT_S16_LE:
408 return AUDIO_FORMAT_PCM_16_BIT;
409 case PCM_FORMAT_S32_LE:
410 return AUDIO_FORMAT_PCM_32_BIT;
411 case PCM_FORMAT_S8:
412 return AUDIO_FORMAT_PCM_8_BIT;
413 case PCM_FORMAT_S24_LE:
414 return AUDIO_FORMAT_PCM_8_24_BIT;
415 case PCM_FORMAT_S24_3LE:
416 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
417 default:
418 return AUDIO_FORMAT_INVALID;
419 }
420}
421
jiabina635fcf2021-01-11 11:34:50 -0800422static unsigned int populate_channel_mask_from_profile(const alsa_device_profile* profile,
423 bool is_output,
424 audio_channel_mask_t channel_masks[])
425{
426 unsigned int num_channel_masks = 0;
427 const audio_channel_mask_t* channel_masks_map =
428 is_output ? OUT_CHANNEL_MASKS_MAP : IN_CHANNEL_MASKS_MAP;
Andy Hungdf511202021-06-07 16:45:03 -0700429 int channel_masks_size = is_output ? OUT_CHANNEL_MASKS_SIZE : IN_CHANNEL_MASKS_SIZE;
430 if (channel_masks_size > FCC_LIMIT + 1) {
431 channel_masks_size = FCC_LIMIT + 1;
432 }
jiabina635fcf2021-01-11 11:34:50 -0800433 unsigned int channel_count = 0;
434 for (size_t i = 0; i < min(channel_masks_size, AUDIO_PORT_MAX_CHANNEL_MASKS) &&
435 (channel_count = profile->channel_counts[i]) != 0 &&
436 num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS; ++i) {
437 if (channel_count < channel_masks_size &&
438 channel_masks_map[channel_count] != AUDIO_CHANNEL_NONE) {
439 channel_masks[num_channel_masks++] = channel_masks_map[channel_count];
440 if (num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
441 break;
442 }
443 }
444 if (channel_count < CHANNEL_INDEX_MASKS_SIZE &&
445 CHANNEL_INDEX_MASKS_MAP[channel_count] != AUDIO_CHANNEL_NONE) {
446 channel_masks[num_channel_masks++] = CHANNEL_INDEX_MASKS_MAP[channel_count];
447 }
448 }
449 return num_channel_masks;
450}
451
452static unsigned int populate_sample_rates_from_profile(const alsa_device_profile* profile,
453 unsigned int sample_rates[])
454{
455 unsigned int num_sample_rates = 0;
456 for (;num_sample_rates < min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
457 profile->sample_rates[num_sample_rates] != 0; num_sample_rates++) {
458 sample_rates[num_sample_rates] = profile->sample_rates[num_sample_rates];
459 }
460 return num_sample_rates;
461}
462
Paul McLeaneedc92e2013-12-19 15:46:15 -0800463/*
464 * HAl Functions
465 */
Simon Wilson19957a32012-04-06 16:17:12 -0700466/**
467 * NOTE: when multiple mutexes have to be acquired, always respect the
468 * following order: hw device > out stream
469 */
470
jiabin8b265c92020-10-21 15:16:40 -0700471static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
472 if (list_empty(alsa_devices)) {
473 return NULL;
474 }
475 return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
476}
477
jiabin400bbe02020-10-28 13:56:59 -0700478/**
479 * Must be called with holding the stream's lock.
480 */
481static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
482{
483 if (!*standby) {
484 struct listnode *node;
485 list_for_each (node, alsa_devices) {
486 struct alsa_device_info *device_info =
487 node_to_item(node, struct alsa_device_info, list_node);
488 proxy_close(&device_info->proxy);
489 }
490 *standby = true;
491 }
492}
493
494static void stream_clear_devices(struct listnode *alsa_devices)
495{
496 struct listnode *node, *temp;
497 struct alsa_device_info *device_info = NULL;
498 list_for_each_safe (node, temp, alsa_devices) {
499 device_info = node_to_item(node, struct alsa_device_info, list_node);
500 if (device_info != NULL) {
501 list_remove(&device_info->list_node);
502 free(device_info);
503 }
504 }
505}
506
507static int stream_set_new_devices(struct pcm_config *config,
508 struct listnode *alsa_devices,
509 unsigned int num_devices,
510 const int cards[],
511 const int devices[],
512 int direction)
513{
514 int status = 0;
515 stream_clear_devices(alsa_devices);
516
517 for (unsigned int i = 0; i < num_devices; ++i) {
518 struct alsa_device_info *device_info =
519 (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
520 profile_init(&device_info->profile, direction);
521 device_info->profile.card = cards[i];
522 device_info->profile.device = devices[i];
523 status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
524 if (status != 0) {
525 ALOGE("%s failed to read device info card=%d;device=%d",
526 __func__, cards[i], devices[i]);
527 goto exit;
528 }
529 status = proxy_prepare(&device_info->proxy, &device_info->profile, config);
530 if (status != 0) {
531 ALOGE("%s failed to prepare device card=%d;device=%d",
532 __func__, cards[i], devices[i]);
533 goto exit;
534 }
535 list_add_tail(alsa_devices, &device_info->list_node);
536 }
537
538exit:
539 if (status != 0) {
540 stream_clear_devices(alsa_devices);
541 }
542 return status;
543}
544
jiabin8b265c92020-10-21 15:16:40 -0700545static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
546 struct listnode *node;
547 size_t i = 0;
548 list_for_each(node, alsa_devices) {
549 struct alsa_device_info *device_info =
550 node_to_item(node, struct alsa_device_info, list_node);
551 dprintf(fd, "Output Profile %zu:\n", i);
552 profile_dump(&device_info->profile, fd);
553
554 dprintf(fd, "Output Proxy %zu:\n", i);
555 proxy_dump(&device_info->proxy, fd);
556 }
557}
558
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700559/*
560 * OUT functions
561 */
Simon Wilson19957a32012-04-06 16:17:12 -0700562static uint32_t out_get_sample_rate(const struct audio_stream *stream)
563{
jiabin8b265c92020-10-21 15:16:40 -0700564 struct alsa_device_info *device_info = stream_get_first_alsa_device(
565 &((struct stream_out*)stream)->alsa_devices);
566 if (device_info == NULL) {
567 ALOGW("%s device info is null", __func__);
568 return 0;
569 }
570 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700571 ALOGV("out_get_sample_rate() = %d", rate);
572 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700573}
574
575static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
576{
577 return 0;
578}
579
580static size_t out_get_buffer_size(const struct audio_stream *stream)
581{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700582 const struct stream_out* out = (const struct stream_out*)stream;
jiabin8b265c92020-10-21 15:16:40 -0700583 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
584 if (device_info == NULL) {
585 ALOGW("%s device info is null", __func__);
586 return 0;
587 }
588 return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
Simon Wilson19957a32012-04-06 16:17:12 -0700589}
590
591static uint32_t out_get_channels(const struct audio_stream *stream)
592{
Andy Hung03576be2014-07-21 21:16:45 -0700593 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700594 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700595}
596
597static audio_format_t out_get_format(const struct audio_stream *stream)
598{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700599 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
600 * Relies on the framework to provide data in the specified format.
601 * This could change in the future.
602 */
jiabin8b265c92020-10-21 15:16:40 -0700603 struct alsa_device_info *device_info = stream_get_first_alsa_device(
604 &((struct stream_out*)stream)->alsa_devices);
605 if (device_info == NULL) {
606 ALOGW("%s device info is null", __func__);
607 return AUDIO_FORMAT_DEFAULT;
608 }
609 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700610 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700611}
612
613static int out_set_format(struct audio_stream *stream, audio_format_t format)
614{
615 return 0;
616}
617
618static int out_standby(struct audio_stream *stream)
619{
620 struct stream_out *out = (struct stream_out *)stream;
621
Paul McLean994ac072016-06-02 15:33:24 -0600622 stream_lock(&out->lock);
jiabin400bbe02020-10-28 13:56:59 -0700623 device_lock(out->adev);
624 stream_standby_l(&out->alsa_devices, &out->standby);
625 device_unlock(out->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600626 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700627 return 0;
628}
629
Paul McLean6a75e4e2016-05-25 14:09:02 -0600630static int out_dump(const struct audio_stream *stream, int fd) {
631 const struct stream_out* out_stream = (const struct stream_out*) stream;
632
633 if (out_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -0700634 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600635 }
636
Simon Wilson19957a32012-04-06 16:17:12 -0700637 return 0;
638}
639
jiabin400bbe02020-10-28 13:56:59 -0700640static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -0700641{
Paul McLean65ec72b2015-02-18 09:13:24 -0800642 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800643
jiabin400bbe02020-10-28 13:56:59 -0700644 // The set parameters here only matters when the routing devices are changed.
645 // When the device version is not less than 3.0, the framework will use create
646 // audio patch API instead of set parameters to chanage audio routing.
647 return 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700648}
649
Paul McLeanf62d75e2014-07-11 15:14:19 -0700650static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
651{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700652 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600653 stream_lock(&out->lock);
jiabin8b265c92020-10-21 15:16:40 -0700654 struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
655 char *params_str = NULL;
656 if (device_info != NULL) {
657 params_str = device_get_parameters(&device_info->profile, keys);
658 }
Paul McLean994ac072016-06-02 15:33:24 -0600659 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700660 return params_str;
661}
662
Simon Wilson19957a32012-04-06 16:17:12 -0700663static uint32_t out_get_latency(const struct audio_stream_out *stream)
664{
jiabin8b265c92020-10-21 15:16:40 -0700665 struct alsa_device_info *device_info = stream_get_first_alsa_device(
666 &((struct stream_out*)stream)->alsa_devices);
667 if (device_info == NULL) {
668 ALOGW("%s device info is null", __func__);
669 return 0;
670 }
671 return proxy_get_latency(&device_info->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700672}
673
Paul McLean30f41852014-04-16 15:44:20 -0700674static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700675{
676 return -ENOSYS;
677}
678
Paul McLeaneedc92e2013-12-19 15:46:15 -0800679/* must be called with hw device and output stream mutexes locked */
680static int start_output_stream(struct stream_out *out)
681{
jiabin8b265c92020-10-21 15:16:40 -0700682 int status = 0;
683 struct listnode *node;
684 list_for_each(node, &out->alsa_devices) {
685 struct alsa_device_info *device_info =
686 node_to_item(node, struct alsa_device_info, list_node);
687 ALOGV("start_output_stream(card:%d device:%d)",
688 device_info->profile.card, device_info->profile.device);
689 status = proxy_open(&device_info->proxy);
690 if (status != 0) {
691 ALOGE("%s failed to open device(card: %d device: %d)",
692 __func__, device_info->profile.card, device_info->profile.device);
693 goto exit;
694 }
695 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800696
jiabin8b265c92020-10-21 15:16:40 -0700697exit:
698 if (status != 0) {
699 list_for_each(node, &out->alsa_devices) {
700 struct alsa_device_info *device_info =
701 node_to_item(node, struct alsa_device_info, list_node);
702 proxy_close(&device_info->proxy);
703 }
704
705 }
706 return status;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800707}
708
709static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700710{
711 int ret;
712 struct stream_out *out = (struct stream_out *)stream;
713
Paul McLean994ac072016-06-02 15:33:24 -0600714 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700715 if (out->standby) {
716 ret = start_output_stream(out);
717 if (ret != 0) {
718 goto err;
719 }
720 out->standby = false;
721 }
Eric Laurent05333d42014-08-04 20:29:17 -0700722
jiabin8b265c92020-10-21 15:16:40 -0700723 struct listnode* node;
724 list_for_each(node, &out->alsa_devices) {
725 struct alsa_device_info* device_info =
726 node_to_item(node, struct alsa_device_info, list_node);
727 alsa_device_proxy* proxy = &device_info->proxy;
728 const void * write_buff = buffer;
729 int num_write_buff_bytes = bytes;
730 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
731 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
732 if (num_device_channels != num_req_channels) {
733 /* allocate buffer */
734 const size_t required_conversion_buffer_size =
735 bytes * num_device_channels / num_req_channels;
736 if (required_conversion_buffer_size > out->conversion_buffer_size) {
737 out->conversion_buffer_size = required_conversion_buffer_size;
738 out->conversion_buffer = realloc(out->conversion_buffer,
739 out->conversion_buffer_size);
740 }
741 /* convert data */
742 const audio_format_t audio_format = out_get_format(&(out->stream.common));
743 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
744 num_write_buff_bytes =
745 adjust_channels(write_buff, num_req_channels,
746 out->conversion_buffer, num_device_channels,
747 sample_size_in_bytes, num_write_buff_bytes);
748 write_buff = out->conversion_buffer;
Andy Hung03576be2014-07-21 21:16:45 -0700749 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800750
jiabin8b265c92020-10-21 15:16:40 -0700751 if (write_buff != NULL && num_write_buff_bytes != 0) {
752 proxy_write(proxy, write_buff, num_write_buff_bytes);
753 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800754 }
Simon Wilson19957a32012-04-06 16:17:12 -0700755
Paul McLean994ac072016-06-02 15:33:24 -0600756 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700757
758 return bytes;
759
760err:
Paul McLean994ac072016-06-02 15:33:24 -0600761 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700762 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700763 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700764 out_get_sample_rate(&stream->common));
765 }
766
767 return bytes;
768}
769
Paul McLean30f41852014-04-16 15:44:20 -0700770static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700771{
772 return -EINVAL;
773}
774
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700775static int out_get_presentation_position(const struct audio_stream_out *stream,
776 uint64_t *frames, struct timespec *timestamp)
777{
Andy Hungc9515ce2015-08-04 15:05:19 -0700778 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600779 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700780
jiabin8b265c92020-10-21 15:16:40 -0700781 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
782 const int ret = device_info == NULL ? -ENODEV :
783 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
Paul McLean994ac072016-06-02 15:33:24 -0600784 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700785 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700786}
787
Simon Wilson19957a32012-04-06 16:17:12 -0700788static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
789{
790 return 0;
791}
792
793static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
794{
795 return 0;
796}
797
Paul McLean30f41852014-04-16 15:44:20 -0700798static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700799{
800 return -EINVAL;
801}
802
Paul McLean76dba682016-06-01 12:29:11 -0600803static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700804 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600805 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700806 audio_output_flags_t flags,
807 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700808 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700809 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700810{
Paul McLean76dba682016-06-01 12:29:11 -0600811 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
812 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800813
Simon Wilson19957a32012-04-06 16:17:12 -0700814 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700815
816 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600817 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700818 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600819 }
Simon Wilson19957a32012-04-06 16:17:12 -0700820
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700821 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700822 out->stream.common.get_sample_rate = out_get_sample_rate;
823 out->stream.common.set_sample_rate = out_set_sample_rate;
824 out->stream.common.get_buffer_size = out_get_buffer_size;
825 out->stream.common.get_channels = out_get_channels;
826 out->stream.common.get_format = out_get_format;
827 out->stream.common.set_format = out_set_format;
828 out->stream.common.standby = out_standby;
829 out->stream.common.dump = out_dump;
830 out->stream.common.set_parameters = out_set_parameters;
831 out->stream.common.get_parameters = out_get_parameters;
832 out->stream.common.add_audio_effect = out_add_audio_effect;
833 out->stream.common.remove_audio_effect = out_remove_audio_effect;
834 out->stream.get_latency = out_get_latency;
835 out->stream.set_volume = out_set_volume;
836 out->stream.write = out_write;
837 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700838 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700839 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
840
jiabin400bbe02020-10-28 13:56:59 -0700841 out->handle = handle;
842
Paul McLean994ac072016-06-02 15:33:24 -0600843 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700844
Paul McLean76dba682016-06-01 12:29:11 -0600845 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700846
jiabin8b265c92020-10-21 15:16:40 -0700847 list_init(&out->alsa_devices);
848 struct alsa_device_info *device_info =
849 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
850 profile_init(&device_info->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700851
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700852 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700853 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800854
Paul McLean0f1753e2014-12-02 12:36:45 -0700855 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700856 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700857
jiabin8b265c92020-10-21 15:16:40 -0700858 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700859
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700860 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800861
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700862 /* Rate */
863 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700864 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
865 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700866 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800867 } else {
jiabin8b265c92020-10-21 15:16:40 -0700868 proxy_config.rate = config->sample_rate =
869 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700870 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800871 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800872
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600873 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -0700874 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600875 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600876 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600877
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700878 /* Format */
879 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -0700880 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700881 config->format = audio_format_from_pcm_format(proxy_config.format);
882 } else {
883 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -0700884 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700885 proxy_config.format = fmt;
886 } else {
jiabin8b265c92020-10-21 15:16:40 -0700887 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700888 config->format = audio_format_from_pcm_format(proxy_config.format);
889 ret = -EINVAL;
890 }
891 }
892
893 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600894 bool calc_mask = false;
895 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
896 /* query case */
jiabin8b265c92020-10-21 15:16:40 -0700897 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -0600898 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700899 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600900 /* explicit case */
901 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700902 }
Paul McLean698dbd72015-11-03 12:24:30 -0800903
Paul McLean64345f82016-06-06 13:26:05 -0600904 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -0700905 if (out->hal_channel_count > FCC_LIMIT) {
906 out->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -0600907 calc_mask = true;
908 }
909
910 if (calc_mask) {
911 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -0700912 * 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 -0600913 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -0700914 /* position mask for mono and stereo*/
915 ? audio_channel_out_mask_from_count(out->hal_channel_count)
916 /* otherwise indexed */
917 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
Paul McLean64345f82016-06-06 13:26:05 -0600918 }
919
Andy Hung182ddc72015-05-05 23:37:30 -0700920 out->hal_channel_mask = config->channel_mask;
921
Paul McLean64345f82016-06-06 13:26:05 -0600922 // Validate the "logical" channel count against support in the "actual" profile.
923 // if they differ, choose the "actual" number of channels *closest* to the "logical".
924 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -0700925 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -0700926 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
927 proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config);
jiabin400bbe02020-10-28 13:56:59 -0700928 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -0700929
930 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700931
Paul McLean96c4d302017-12-05 09:50:26 -0800932 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
933 * So clear any errors that may have occurred above.
934 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700935 ret = 0;
936
Paul McLeaneedc92e2013-12-19 15:46:15 -0800937 out->conversion_buffer = NULL;
938 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700939
940 out->standby = true;
941
Paul McLean6a75e4e2016-05-25 14:09:02 -0600942 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600943 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600944
Simon Wilson19957a32012-04-06 16:17:12 -0700945 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700946
947 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700948}
949
Paul McLean76dba682016-06-01 12:29:11 -0600950static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700951 struct audio_stream_out *stream)
952{
953 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -0600954
jiabin400bbe02020-10-28 13:56:59 -0700955 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700956 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -0700957 stream_standby_l(&out->alsa_devices, &out->standby);
958 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800959
960 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700961
Paul McLeaneedc92e2013-12-19 15:46:15 -0800962 out->conversion_buffer = NULL;
963 out->conversion_buffer_size = 0;
964
Paul McLean994ac072016-06-02 15:33:24 -0600965 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700966 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -0600967 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600968 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700969 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -0600970
Simon Wilson19957a32012-04-06 16:17:12 -0700971 free(stream);
972}
973
Paul McLean76dba682016-06-01 12:29:11 -0600974static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700975 const struct audio_config *config)
976{
977 /* TODO This needs to be calculated based on format/channels/rate */
978 return 320;
979}
980
981/*
982 * IN functions
983 */
984static uint32_t in_get_sample_rate(const struct audio_stream *stream)
985{
jiabin8b265c92020-10-21 15:16:40 -0700986 struct alsa_device_info *device_info = stream_get_first_alsa_device(
987 &((const struct stream_in *)stream)->alsa_devices);
988 if (device_info == NULL) {
989 ALOGW("%s device info is null", __func__);
990 return 0;
991 }
992 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700993 ALOGV("in_get_sample_rate() = %d", rate);
994 return rate;
995}
996
997static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
998{
999 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
1000 return -ENOSYS;
1001}
1002
1003static size_t in_get_buffer_size(const struct audio_stream *stream)
1004{
1005 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -07001006 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1007 if (device_info == NULL) {
1008 ALOGW("%s device info is null", __func__);
1009 return 0;
1010 }
1011 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001012}
1013
1014static uint32_t in_get_channels(const struct audio_stream *stream)
1015{
Paul McLean2cfd81b2014-09-15 12:32:23 -07001016 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -07001017 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001018}
1019
1020static audio_format_t in_get_format(const struct audio_stream *stream)
1021{
jiabin8b265c92020-10-21 15:16:40 -07001022 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1023 &((const struct stream_in *)stream)->alsa_devices);
1024 if (device_info == NULL) {
1025 ALOGW("%s device info is null", __func__);
1026 return AUDIO_FORMAT_DEFAULT;
1027 }
1028 alsa_device_proxy *proxy = &device_info->proxy;
Andy Hung780f1f82015-04-22 22:14:39 -07001029 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1030 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001031}
1032
1033static int in_set_format(struct audio_stream *stream, audio_format_t format)
1034{
1035 ALOGV("in_set_format(%d) - NOPE", format);
1036
1037 return -ENOSYS;
1038}
1039
1040static int in_standby(struct audio_stream *stream)
1041{
1042 struct stream_in *in = (struct stream_in *)stream;
1043
Paul McLean994ac072016-06-02 15:33:24 -06001044 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -07001045 device_lock(in->adev);
1046 stream_standby_l(&in->alsa_devices, &in->standby);
1047 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -06001048 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001049 return 0;
1050}
1051
1052static int in_dump(const struct audio_stream *stream, int fd)
1053{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001054 const struct stream_in* in_stream = (const struct stream_in*)stream;
1055 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -07001056 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001057 }
1058
1059 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001060}
1061
1062static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1063{
Paul McLean65ec72b2015-02-18 09:13:24 -08001064 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001065
jiabin400bbe02020-10-28 13:56:59 -07001066 // The set parameters here only matters when the routing devices are changed.
1067 // When the device version higher than 3.0, the framework will use create_audio_patch
1068 // API instead of set_parameters to change audio routing.
1069 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001070}
1071
1072static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1073{
1074 struct stream_in *in = (struct stream_in *)stream;
1075
Paul McLean994ac072016-06-02 15:33:24 -06001076 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001077 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1078 char *params_str = NULL;
1079 if (device_info != NULL) {
1080 params_str = device_get_parameters(&device_info->profile, keys);
1081 }
Paul McLean994ac072016-06-02 15:33:24 -06001082 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001083
1084 return params_str;
1085}
1086
1087static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1088{
1089 return 0;
1090}
1091
1092static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1093{
1094 return 0;
1095}
1096
1097static int in_set_gain(struct audio_stream_in *stream, float gain)
1098{
1099 return 0;
1100}
1101
1102/* must be called with hw device and output stream mutexes locked */
1103static int start_input_stream(struct stream_in *in)
1104{
jiabin8b265c92020-10-21 15:16:40 -07001105 // Only care about the first device as only one input device is allowed.
1106 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1107 if (device_info == NULL) {
1108 return -ENODEV;
1109 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001110
jiabin8b265c92020-10-21 15:16:40 -07001111 ALOGV("start_input_stream(card:%d device:%d)",
1112 device_info->profile.card, device_info->profile.device);
1113 return proxy_open(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001114}
1115
1116/* TODO mutex stuff here (see out_write) */
1117static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1118{
1119 size_t num_read_buff_bytes = 0;
1120 void * read_buff = buffer;
1121 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001122 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001123
1124 struct stream_in * in = (struct stream_in *)stream;
1125
Paul McLean994ac072016-06-02 15:33:24 -06001126 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001127 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001128 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001129 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001130 goto err;
1131 }
1132 in->standby = false;
1133 }
1134
jiabin8b265c92020-10-21 15:16:40 -07001135 // Only care about the first device as only one input device is allowed.
1136 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1137 if (device_info == NULL) {
1138 return 0;
1139 }
1140
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001141 /*
1142 * OK, we need to figure out how much data to read to be able to output the requested
1143 * number of bytes in the HAL format (16-bit, stereo).
1144 */
1145 num_read_buff_bytes = bytes;
jiabin8b265c92020-10-21 15:16:40 -07001146 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001147 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001148
1149 if (num_device_channels != num_req_channels) {
1150 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1151 }
1152
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001153 /* Setup/Realloc the conversion buffer (if necessary). */
1154 if (num_read_buff_bytes != bytes) {
1155 if (num_read_buff_bytes > in->conversion_buffer_size) {
1156 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1157 (and do these conversions themselves) */
1158 in->conversion_buffer_size = num_read_buff_bytes;
1159 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1160 }
1161 read_buff = in->conversion_buffer;
1162 }
1163
jiabin8b265c92020-10-21 15:16:40 -07001164 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001165 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001166 if (num_device_channels != num_req_channels) {
1167 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1168
1169 out_buff = buffer;
1170 /* Num Channels conversion */
1171 if (num_device_channels != num_req_channels) {
1172 audio_format_t audio_format = in_get_format(&(in->stream.common));
1173 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1174
1175 num_read_buff_bytes =
1176 adjust_channels(read_buff, num_device_channels,
1177 out_buff, num_req_channels,
1178 sample_size_in_bytes, num_read_buff_bytes);
1179 }
1180 }
Eric Laurent253def92014-09-14 12:18:18 -07001181
Paul McLean76dba682016-06-01 12:29:11 -06001182 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1183 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -07001184 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301185 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001186 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001187 }
1188
1189err:
Paul McLean994ac072016-06-02 15:33:24 -06001190 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001191 return num_read_buff_bytes;
1192}
1193
1194static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1195{
1196 return 0;
1197}
1198
Andy Hungfa6b4a62018-06-04 19:14:22 -07001199static int in_get_capture_position(const struct audio_stream_in *stream,
1200 int64_t *frames, int64_t *time)
1201{
1202 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1203 stream_lock(&in->lock);
1204
jiabin8b265c92020-10-21 15:16:40 -07001205 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1206
1207 const int ret = device_info == NULL ? -ENODEV
1208 : proxy_get_capture_position(&device_info->proxy, frames, time);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001209
1210 stream_unlock(&in->lock);
1211 return ret;
1212}
1213
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001214static int in_get_active_microphones(const struct audio_stream_in *stream,
1215 struct audio_microphone_characteristic_t *mic_array,
1216 size_t *mic_count) {
1217 (void)stream;
1218 (void)mic_array;
1219 (void)mic_count;
1220
1221 return -ENOSYS;
1222}
1223
1224static int in_set_microphone_direction(const struct audio_stream_in *stream,
1225 audio_microphone_direction_t dir) {
1226 (void)stream;
1227 (void)dir;
1228 ALOGV("---- in_set_microphone_direction()");
1229 return -ENOSYS;
1230}
1231
1232static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1233 (void)zoom;
1234 ALOGV("---- in_set_microphone_field_dimension()");
1235 return -ENOSYS;
1236}
1237
Paul McLean76dba682016-06-01 12:29:11 -06001238static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001239 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001240 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001241 struct audio_config *config,
1242 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001243 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001244 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001245 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001246{
Paul McLean1d585cc2016-05-24 11:28:10 -06001247 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001248 config->sample_rate, config->channel_mask, config->format);
1249
Andy Hungb10ce6d2017-10-27 19:37:58 -07001250 /* Pull out the card/device pair */
1251 int32_t card, device;
1252 if (!parse_card_device_params(address, &card, &device)) {
1253 ALOGW("%s fail - invalid address %s", __func__, address);
1254 *stream_in = NULL;
1255 return -EINVAL;
1256 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001257
Andy Hungb10ce6d2017-10-27 19:37:58 -07001258 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001259 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001260 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001261 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001262 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001263
1264 /* setup function pointers */
1265 in->stream.common.get_sample_rate = in_get_sample_rate;
1266 in->stream.common.set_sample_rate = in_set_sample_rate;
1267 in->stream.common.get_buffer_size = in_get_buffer_size;
1268 in->stream.common.get_channels = in_get_channels;
1269 in->stream.common.get_format = in_get_format;
1270 in->stream.common.set_format = in_set_format;
1271 in->stream.common.standby = in_standby;
1272 in->stream.common.dump = in_dump;
1273 in->stream.common.set_parameters = in_set_parameters;
1274 in->stream.common.get_parameters = in_get_parameters;
1275 in->stream.common.add_audio_effect = in_add_audio_effect;
1276 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1277
1278 in->stream.set_gain = in_set_gain;
1279 in->stream.read = in_read;
1280 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001281 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001282
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001283 in->stream.get_active_microphones = in_get_active_microphones;
1284 in->stream.set_microphone_direction = in_set_microphone_direction;
1285 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1286
jiabin400bbe02020-10-28 13:56:59 -07001287 in->handle = handle;
1288
Paul McLean994ac072016-06-02 15:33:24 -06001289 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001290
Paul McLean76dba682016-06-01 12:29:11 -06001291 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001292
jiabin8b265c92020-10-21 15:16:40 -07001293 list_init(&in->alsa_devices);
1294 struct alsa_device_info *device_info =
1295 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1296 profile_init(&device_info->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001297
jiabin400bbe02020-10-28 13:56:59 -07001298 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001299
Andy Hungb10ce6d2017-10-27 19:37:58 -07001300 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001301 device_lock(in->adev);
1302 int num_open_inputs = in->adev->inputs_open;
1303 device_unlock(in->adev);
1304
Andy Hungb10ce6d2017-10-27 19:37:58 -07001305 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001306 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001307 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001308 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1309 __func__, card, device);
1310 ret = -EINVAL;
1311 }
1312 } else {
1313 /* Read input profile only if necessary */
jiabin8b265c92020-10-21 15:16:40 -07001314 device_info->profile.card = card;
1315 device_info->profile.device = device;
1316 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001317 ALOGW("%s fail - cannot read profile", __func__);
1318 ret = -EINVAL;
1319 }
1320 }
1321 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001322 free(in);
1323 *stream_in = NULL;
1324 return ret;
1325 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001326
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001327 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001328 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001329 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001330 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001331 }
1332
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001333 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001334 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001335 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001336 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001337 if (highest_rate == 0) {
1338 ret = -EINVAL; /* error with device */
1339 } else {
jiabin400bbe02020-10-28 13:56:59 -07001340 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001341 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001342 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001343 /* Changing the requested rate */
1344 ret = -EINVAL;
1345 } else {
1346 /* Everything AOK! */
1347 ret = 0;
1348 }
1349 }
1350 }
jiabin8b265c92020-10-21 15:16:40 -07001351 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001352 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001353 } else {
jiabin400bbe02020-10-28 13:56:59 -07001354 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001355 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001356 ret = -EINVAL;
1357 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001358
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001359 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001360 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001361 in->config.format = profile_get_default_format(&device_info->profile);
1362 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001363 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001364 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001365 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001366 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001367 } else {
jiabin400bbe02020-10-28 13:56:59 -07001368 in->config.format = profile_get_default_format(&device_info->profile);
1369 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001370 ret = -EINVAL;
1371 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001372 }
1373
Paul McLean2cfd81b2014-09-15 12:32:23 -07001374 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001375 bool calc_mask = false;
1376 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1377 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001378 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001379 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001380 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001381 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001382 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1383 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001384
Paul McLean64345f82016-06-06 13:26:05 -06001385 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001386 if (in->hal_channel_count > FCC_LIMIT) {
1387 in->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001388 calc_mask = true;
1389 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001390
Paul McLean64345f82016-06-06 13:26:05 -06001391 if (calc_mask) {
1392 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001393 * 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 -06001394 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1395 /* position mask for mono & stereo */
1396 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1397 /* otherwise indexed */
1398 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001399
Paul McLean64345f82016-06-06 13:26:05 -06001400 // if we change the mask...
1401 if (in->hal_channel_mask != config->channel_mask &&
1402 config->channel_mask != AUDIO_CHANNEL_NONE) {
1403 config->channel_mask = in->hal_channel_mask;
1404 ret = -EINVAL;
1405 }
1406 } else {
1407 in->hal_channel_mask = config->channel_mask;
1408 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001409
Paul McLean64345f82016-06-06 13:26:05 -06001410 if (ret == 0) {
1411 // Validate the "logical" channel count against support in the "actual" profile.
1412 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1413 // and store THAT in proxy_config.channels
jiabin400bbe02020-10-28 13:56:59 -07001414 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001415 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabin400bbe02020-10-28 13:56:59 -07001416 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config);
Eric Laurent981f7742017-05-03 12:44:26 -07001417 if (ret == 0) {
1418 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001419
Eric Laurent981f7742017-05-03 12:44:26 -07001420 in->conversion_buffer = NULL;
1421 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001422
Eric Laurent981f7742017-05-03 12:44:26 -07001423 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001424
Eric Laurent981f7742017-05-03 12:44:26 -07001425 /* Save this for adev_dump() */
1426 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1427 } else {
1428 ALOGW("proxy_prepare error %d", ret);
jiabin8b265c92020-10-21 15:16:40 -07001429 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001430 config->channel_mask = channel_count <= FCC_2
1431 ? audio_channel_in_mask_from_count(channel_count)
1432 : audio_channel_mask_for_index_assignment_from_count(channel_count);
jiabin8b265c92020-10-21 15:16:40 -07001433 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1434 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001435 }
1436 }
Paul McLean64345f82016-06-06 13:26:05 -06001437
Eric Laurent981f7742017-05-03 12:44:26 -07001438 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001439 // Deallocate this stream on error, because AudioFlinger won't call
1440 // adev_close_input_stream() in this case.
1441 *stream_in = NULL;
1442 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001443 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001444 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001445
jiabin8b265c92020-10-21 15:16:40 -07001446 list_add_tail(&in->alsa_devices, &device_info->list_node);
1447
Andy Hungb10ce6d2017-10-27 19:37:58 -07001448 device_lock(in->adev);
1449 ++in->adev->inputs_open;
1450 device_unlock(in->adev);
1451
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001452 return ret;
1453}
1454
Paul McLean76dba682016-06-01 12:29:11 -06001455static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1456 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001457{
1458 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001459
jiabin400bbe02020-10-28 13:56:59 -07001460 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001461 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001462 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001463 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001464 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1465 if (device_info != NULL) {
1466 ALOGV("adev_close_input_stream(c:%d d:%d)",
1467 device_info->profile.card, device_info->profile.device);
1468 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001469 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1470 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001471
1472 stream_standby_l(&in->alsa_devices, &in->standby);
1473
Andy Hungb10ce6d2017-10-27 19:37:58 -07001474 device_unlock(in->adev);
1475
jiabin400bbe02020-10-28 13:56:59 -07001476 stream_clear_devices(&in->alsa_devices);
1477 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001478
1479 free(in->conversion_buffer);
1480
1481 free(stream);
1482}
1483
1484/*
1485 * ADEV Functions
1486 */
Paul McLean76dba682016-06-01 12:29:11 -06001487static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001488{
1489 return 0;
1490}
1491
Paul McLean76dba682016-06-01 12:29:11 -06001492static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001493{
1494 return strdup("");
1495}
1496
Paul McLean76dba682016-06-01 12:29:11 -06001497static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001498{
1499 return 0;
1500}
1501
Paul McLean76dba682016-06-01 12:29:11 -06001502static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001503{
1504 return -ENOSYS;
1505}
1506
Paul McLean76dba682016-06-01 12:29:11 -06001507static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001508{
1509 return -ENOSYS;
1510}
1511
Paul McLean76dba682016-06-01 12:29:11 -06001512static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001513{
1514 return 0;
1515}
1516
Paul McLean76dba682016-06-01 12:29:11 -06001517static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001518{
Paul McLean76dba682016-06-01 12:29:11 -06001519 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001520 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001521 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001522 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001523 return -ENOSYS;
1524}
1525
Paul McLean76dba682016-06-01 12:29:11 -06001526static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001527{
1528 return -ENOSYS;
1529}
1530
jiabin400bbe02020-10-28 13:56:59 -07001531static int adev_create_audio_patch(struct audio_hw_device *dev,
1532 unsigned int num_sources,
1533 const struct audio_port_config *sources,
1534 unsigned int num_sinks,
1535 const struct audio_port_config *sinks,
1536 audio_patch_handle_t *handle) {
1537 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1538 // Only accept mix->device and device->mix cases. In that case, the number of sources
1539 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1540 return -EINVAL;
1541 }
1542
1543 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1544 // If source is a device, the number of sinks should be 1.
1545 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1546 return -EINVAL;
1547 }
1548 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1549 // If source is a mix, all sinks should be device.
1550 for (unsigned int i = 0; i < num_sinks; i++) {
1551 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1552 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1553 return -EINVAL;
1554 }
1555 }
1556 } else {
1557 // All other cases are invalid.
1558 return -EINVAL;
1559 }
1560
1561 struct audio_device* adev = (struct audio_device*) dev;
1562 bool generatedPatchHandle = false;
1563 device_lock(adev);
1564 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1565 *handle = ++adev->next_patch_handle;
1566 generatedPatchHandle = true;
1567 }
1568
1569 int cards[AUDIO_PATCH_PORTS_MAX];
1570 int devices[AUDIO_PATCH_PORTS_MAX];
1571 const struct audio_port_config *port_configs =
1572 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1573 int num_configs = 0;
1574 audio_io_handle_t io_handle = 0;
1575 bool wasStandby = true;
1576 int direction = PCM_OUT;
1577 audio_patch_handle_t *patch_handle = NULL;
1578 struct listnode *alsa_devices = NULL;
1579 struct stream_lock *lock = NULL;
1580 struct pcm_config *config = NULL;
1581 struct stream_in *in = NULL;
1582 struct stream_out *out = NULL;
1583
1584 unsigned int num_saved_devices = 0;
1585 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1586 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1587
1588 struct listnode *node;
1589
1590 // Only handle patches for mix->devices and device->mix case.
1591 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1592 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1593 if (in == NULL) {
1594 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1595 device_unlock(adev);
1596 return -EINVAL;
1597 }
1598
1599 direction = PCM_IN;
1600 wasStandby = in->standby;
1601 io_handle = in->handle;
1602 num_configs = num_sources;
1603 patch_handle = &in->patch_handle;
1604 alsa_devices = &in->alsa_devices;
1605 lock = &in->lock;
1606 config = &in->config;
1607 } else {
1608 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1609 if (out == NULL) {
1610 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1611 device_unlock(adev);
1612 return -EINVAL;
1613 }
1614
1615 direction = PCM_OUT;
1616 wasStandby = out->standby;
1617 io_handle = out->handle;
1618 num_configs = num_sinks;
1619 patch_handle = &out->patch_handle;
1620 alsa_devices = &out->alsa_devices;
1621 lock = &out->lock;
1622 config = &out->config;
1623 }
1624
1625 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1626 if (!generatedPatchHandle && *patch_handle != *handle) {
1627 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1628 "with handle(%d) when creating audio patch",
1629 __func__, *handle, *patch_handle, io_handle);
1630 device_unlock(adev);
1631 return -EINVAL;
1632 }
1633 device_unlock(adev);
1634
1635 for (unsigned int i = 0; i < num_configs; ++i) {
1636 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1637 ALOGE("%s, failed to parse card and device %s",
1638 __func__, port_configs[i].ext.device.address);
1639 return -EINVAL;
1640 }
1641 }
1642
1643 stream_lock(lock);
1644 list_for_each (node, alsa_devices) {
1645 struct alsa_device_info *device_info =
1646 node_to_item(node, struct alsa_device_info, list_node);
1647 saved_cards[num_saved_devices] = device_info->profile.card;
1648 saved_devices[num_saved_devices++] = device_info->profile.device;
1649 }
1650
1651 device_lock(adev);
1652 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1653 device_unlock(adev);
1654
Andy Hung61583422021-07-02 16:13:11 -07001655 // Timestamps:
1656 // Audio timestamps assume continuous PCM frame counts which are maintained
1657 // with the device proxy.transferred variable. Technically it would be better
1658 // associated with in or out stream, not the device; here we save and restore
1659 // using the first alsa device as a simplification.
1660 uint64_t saved_transferred_frames = 0;
1661 struct alsa_device_info *device_info = stream_get_first_alsa_device(alsa_devices);
1662 if (device_info != NULL) saved_transferred_frames = device_info->proxy.transferred;
1663
jiabin400bbe02020-10-28 13:56:59 -07001664 int ret = stream_set_new_devices(config, alsa_devices, num_configs, cards, devices, direction);
1665
1666 if (ret != 0) {
1667 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1668 stream_set_new_devices(
1669 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction);
1670 } else {
1671 *patch_handle = *handle;
1672 }
Andy Hung61583422021-07-02 16:13:11 -07001673
1674 // Timestamps: Restore transferred frames.
1675 if (saved_transferred_frames != 0) {
1676 device_info = stream_get_first_alsa_device(alsa_devices);
1677 if (device_info != NULL) device_info->proxy.transferred = saved_transferred_frames;
1678 }
1679
jiabin400bbe02020-10-28 13:56:59 -07001680 if (!wasStandby) {
1681 device_lock(adev);
1682 if (in != NULL) {
1683 start_input_stream(in);
1684 }
1685 if (out != NULL) {
1686 start_output_stream(out);
1687 }
1688 device_unlock(adev);
1689 }
1690 stream_unlock(lock);
1691 return ret;
1692}
1693
1694static int adev_release_audio_patch(struct audio_hw_device *dev,
1695 audio_patch_handle_t patch_handle)
1696{
1697 struct audio_device* adev = (struct audio_device*) dev;
1698
1699 device_lock(adev);
1700 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1701 device_unlock(adev);
1702 if (out != NULL) {
1703 stream_lock(&out->lock);
1704 device_lock(adev);
1705 stream_standby_l(&out->alsa_devices, &out->standby);
1706 device_unlock(adev);
1707 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1708 stream_unlock(&out->lock);
1709 return 0;
1710 }
1711
1712 device_lock(adev);
1713 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1714 device_unlock(adev);
1715 if (in != NULL) {
1716 stream_lock(&in->lock);
1717 device_lock(adev);
1718 stream_standby_l(&in->alsa_devices, &in->standby);
1719 device_unlock(adev);
1720 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1721 stream_unlock(&in->lock);
1722 return 0;
1723 }
1724
1725 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1726 return -EINVAL;
1727}
1728
1729static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1730{
1731 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1732 return -EINVAL;
1733 }
1734
1735 alsa_device_profile profile;
1736 const bool is_output = audio_is_output_device(port->ext.device.type);
1737 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1738 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1739 return -EINVAL;
1740 }
1741
1742 if (!profile_read_device_info(&profile)) {
1743 return -ENOENT;
1744 }
1745
1746 port->num_formats = 0;;
1747 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1748 profile.formats[i] != 0; ++i) {
1749 audio_format_t format = audio_format_from(profile.formats[i]);
1750 if (format != AUDIO_FORMAT_INVALID) {
1751 port->formats[port->num_formats++] = format;
1752 }
1753 }
1754
jiabina635fcf2021-01-11 11:34:50 -08001755 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1756 port->num_channel_masks = populate_channel_mask_from_profile(
1757 &profile, is_output, port->channel_masks);
jiabin400bbe02020-10-28 13:56:59 -07001758
jiabina635fcf2021-01-11 11:34:50 -08001759 return 0;
1760}
1761
1762static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1763{
1764 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1765 return -EINVAL;
jiabin400bbe02020-10-28 13:56:59 -07001766 }
jiabina635fcf2021-01-11 11:34:50 -08001767
1768 alsa_device_profile profile;
1769 const bool is_output = audio_is_output_device(port->ext.device.type);
1770 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1771 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1772 return -EINVAL;
1773 }
1774
1775 if (!profile_read_device_info(&profile)) {
1776 return -ENOENT;
1777 }
1778
1779 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1780 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1781 &profile, is_output, channel_masks);
1782 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1783 const unsigned int num_sample_rates =
1784 populate_sample_rates_from_profile(&profile, sample_rates);
1785 port->num_audio_profiles = 0;;
1786 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1787 profile.formats[i] != 0; ++i) {
1788 audio_format_t format = audio_format_from(profile.formats[i]);
1789 if (format == AUDIO_FORMAT_INVALID) {
1790 continue;
1791 }
1792 const unsigned int j = port->num_audio_profiles++;
1793 port->audio_profiles[j].format = format;
1794 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1795 memcpy(port->audio_profiles[j].sample_rates,
1796 sample_rates,
1797 num_sample_rates * sizeof(unsigned int));
1798 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1799 memcpy(port->audio_profiles[j].channel_masks,
1800 channel_masks,
1801 num_channel_masks* sizeof(audio_channel_mask_t));
1802 }
1803
jiabin400bbe02020-10-28 13:56:59 -07001804 return 0;
1805}
1806
Paul McLean76dba682016-06-01 12:29:11 -06001807static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001808{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001809 dprintf(fd, "\nUSB audio module:\n");
1810
1811 struct audio_device* adev = (struct audio_device*)device;
1812 const int kNumRetries = 3;
1813 const int kSleepTimeMS = 500;
1814
Paul McLean994ac072016-06-02 15:33:24 -06001815 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001816 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001817 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001818 sleep(kSleepTimeMS);
1819 retry--;
1820 }
1821
1822 if (retry > 0) {
1823 if (list_empty(&adev->output_stream_list)) {
1824 dprintf(fd, " No output streams.\n");
1825 } else {
1826 struct listnode* node;
1827 list_for_each(node, &adev->output_stream_list) {
1828 struct audio_stream* stream =
1829 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1830 out_dump(stream, fd);
1831 }
1832 }
1833
1834 if (list_empty(&adev->input_stream_list)) {
1835 dprintf(fd, "\n No input streams.\n");
1836 } else {
1837 struct listnode* node;
1838 list_for_each(node, &adev->input_stream_list) {
1839 struct audio_stream* stream =
1840 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1841 in_dump(stream, fd);
1842 }
1843 }
1844
Paul McLean994ac072016-06-02 15:33:24 -06001845 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001846 } else {
1847 // Couldn't lock
1848 dprintf(fd, " Could not obtain device lock.\n");
1849 }
1850
Simon Wilson19957a32012-04-06 16:17:12 -07001851 return 0;
1852}
1853
1854static int adev_close(hw_device_t *device)
1855{
Simon Wilson19957a32012-04-06 16:17:12 -07001856 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001857
Simon Wilson19957a32012-04-06 16:17:12 -07001858 return 0;
1859}
1860
Paul McLean30f41852014-04-16 15:44:20 -07001861static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001862{
Simon Wilson19957a32012-04-06 16:17:12 -07001863 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1864 return -EINVAL;
1865
Paul McLeaneedc92e2013-12-19 15:46:15 -08001866 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001867 if (!adev)
1868 return -ENOMEM;
1869
Paul McLeancf5310a2018-08-22 14:33:12 -07001870 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001871
Paul McLean6a75e4e2016-05-25 14:09:02 -06001872 list_init(&adev->output_stream_list);
1873 list_init(&adev->input_stream_list);
1874
Simon Wilson19957a32012-04-06 16:17:12 -07001875 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabina635fcf2021-01-11 11:34:50 -08001876 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001877 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001878 adev->hw_device.common.close = adev_close;
1879
Simon Wilson19957a32012-04-06 16:17:12 -07001880 adev->hw_device.init_check = adev_init_check;
1881 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1882 adev->hw_device.set_master_volume = adev_set_master_volume;
1883 adev->hw_device.set_mode = adev_set_mode;
1884 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1885 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1886 adev->hw_device.set_parameters = adev_set_parameters;
1887 adev->hw_device.get_parameters = adev_get_parameters;
1888 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1889 adev->hw_device.open_output_stream = adev_open_output_stream;
1890 adev->hw_device.close_output_stream = adev_close_output_stream;
1891 adev->hw_device.open_input_stream = adev_open_input_stream;
1892 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07001893 adev->hw_device.create_audio_patch = adev_create_audio_patch;
1894 adev->hw_device.release_audio_patch = adev_release_audio_patch;
1895 adev->hw_device.get_audio_port = adev_get_audio_port;
jiabina635fcf2021-01-11 11:34:50 -08001896 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
Simon Wilson19957a32012-04-06 16:17:12 -07001897 adev->hw_device.dump = adev_dump;
1898
1899 *device = &adev->hw_device.common;
1900
1901 return 0;
1902}
1903
1904static struct hw_module_methods_t hal_module_methods = {
1905 .open = adev_open,
1906};
1907
1908struct audio_module HAL_MODULE_INFO_SYM = {
1909 .common = {
1910 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001911 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1912 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001913 .id = AUDIO_HARDWARE_MODULE_ID,
1914 .name = "USB audio HW HAL",
1915 .author = "The Android Open Source Project",
1916 .methods = &hal_module_methods,
1917 },
1918};