blob: fe921d6984097a040d55d045d726e05c88aba44d [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
jiabin4bc5f252021-06-02 18:31:13 +0000463static bool are_all_devices_found(unsigned int num_devices_to_find,
464 const int cards_to_find[],
465 const int devices_to_find[],
466 unsigned int num_devices,
467 const int cards[],
468 const int devices[]) {
469 for (unsigned int i = 0; i < num_devices_to_find; ++i) {
470 unsigned int j = 0;
471 for (; j < num_devices; ++j) {
472 if (cards_to_find[i] == cards[j] && devices_to_find[i] == devices[j]) {
473 break;
474 }
475 }
476 if (j >= num_devices) {
477 return false;
478 }
479 }
480 return true;
481}
482
483static bool are_devices_the_same(unsigned int left_num_devices,
484 const int left_cards[],
485 const int left_devices[],
486 unsigned int right_num_devices,
487 const int right_cards[],
488 const int right_devices[]) {
489 if (left_num_devices != right_num_devices) {
490 return false;
491 }
492 return are_all_devices_found(left_num_devices, left_cards, left_devices,
493 right_num_devices, right_cards, right_devices) &&
494 are_all_devices_found(right_num_devices, right_cards, right_devices,
495 left_num_devices, left_cards, left_devices);
496}
497
Paul McLeaneedc92e2013-12-19 15:46:15 -0800498/*
499 * HAl Functions
500 */
Simon Wilson19957a32012-04-06 16:17:12 -0700501/**
502 * NOTE: when multiple mutexes have to be acquired, always respect the
503 * following order: hw device > out stream
504 */
505
jiabin8b265c92020-10-21 15:16:40 -0700506static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
507 if (list_empty(alsa_devices)) {
508 return NULL;
509 }
510 return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
511}
512
jiabin400bbe02020-10-28 13:56:59 -0700513/**
514 * Must be called with holding the stream's lock.
515 */
516static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
517{
518 if (!*standby) {
519 struct listnode *node;
520 list_for_each (node, alsa_devices) {
521 struct alsa_device_info *device_info =
522 node_to_item(node, struct alsa_device_info, list_node);
523 proxy_close(&device_info->proxy);
524 }
525 *standby = true;
526 }
527}
528
529static void stream_clear_devices(struct listnode *alsa_devices)
530{
531 struct listnode *node, *temp;
532 struct alsa_device_info *device_info = NULL;
533 list_for_each_safe (node, temp, alsa_devices) {
534 device_info = node_to_item(node, struct alsa_device_info, list_node);
535 if (device_info != NULL) {
536 list_remove(&device_info->list_node);
537 free(device_info);
538 }
539 }
540}
541
542static int stream_set_new_devices(struct pcm_config *config,
543 struct listnode *alsa_devices,
544 unsigned int num_devices,
545 const int cards[],
546 const int devices[],
547 int direction)
548{
549 int status = 0;
550 stream_clear_devices(alsa_devices);
551
552 for (unsigned int i = 0; i < num_devices; ++i) {
553 struct alsa_device_info *device_info =
554 (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
555 profile_init(&device_info->profile, direction);
556 device_info->profile.card = cards[i];
557 device_info->profile.device = devices[i];
558 status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
559 if (status != 0) {
560 ALOGE("%s failed to read device info card=%d;device=%d",
561 __func__, cards[i], devices[i]);
562 goto exit;
563 }
564 status = proxy_prepare(&device_info->proxy, &device_info->profile, config);
565 if (status != 0) {
566 ALOGE("%s failed to prepare device card=%d;device=%d",
567 __func__, cards[i], devices[i]);
568 goto exit;
569 }
570 list_add_tail(alsa_devices, &device_info->list_node);
571 }
572
573exit:
574 if (status != 0) {
575 stream_clear_devices(alsa_devices);
576 }
577 return status;
578}
579
jiabin8b265c92020-10-21 15:16:40 -0700580static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
581 struct listnode *node;
582 size_t i = 0;
583 list_for_each(node, alsa_devices) {
584 struct alsa_device_info *device_info =
585 node_to_item(node, struct alsa_device_info, list_node);
jiabin0269a9d2021-06-02 20:27:38 +0000586 const char* direction = device_info->profile.direction == PCM_OUT ? "Output" : "Input";
587 dprintf(fd, "%s Profile %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700588 profile_dump(&device_info->profile, fd);
589
jiabin0269a9d2021-06-02 20:27:38 +0000590 dprintf(fd, "%s Proxy %zu:\n", direction, i);
jiabin8b265c92020-10-21 15:16:40 -0700591 proxy_dump(&device_info->proxy, fd);
592 }
593}
594
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700595/*
596 * OUT functions
597 */
Simon Wilson19957a32012-04-06 16:17:12 -0700598static uint32_t out_get_sample_rate(const struct audio_stream *stream)
599{
jiabin8b265c92020-10-21 15:16:40 -0700600 struct alsa_device_info *device_info = stream_get_first_alsa_device(
601 &((struct stream_out*)stream)->alsa_devices);
602 if (device_info == NULL) {
603 ALOGW("%s device info is null", __func__);
604 return 0;
605 }
606 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700607 ALOGV("out_get_sample_rate() = %d", rate);
608 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700609}
610
611static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
612{
613 return 0;
614}
615
616static size_t out_get_buffer_size(const struct audio_stream *stream)
617{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700618 const struct stream_out* out = (const struct stream_out*)stream;
jiabin8b265c92020-10-21 15:16:40 -0700619 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
620 if (device_info == NULL) {
621 ALOGW("%s device info is null", __func__);
622 return 0;
623 }
624 return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
Simon Wilson19957a32012-04-06 16:17:12 -0700625}
626
627static uint32_t out_get_channels(const struct audio_stream *stream)
628{
Andy Hung03576be2014-07-21 21:16:45 -0700629 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700630 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700631}
632
633static audio_format_t out_get_format(const struct audio_stream *stream)
634{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700635 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
636 * Relies on the framework to provide data in the specified format.
637 * This could change in the future.
638 */
jiabin8b265c92020-10-21 15:16:40 -0700639 struct alsa_device_info *device_info = stream_get_first_alsa_device(
640 &((struct stream_out*)stream)->alsa_devices);
641 if (device_info == NULL) {
642 ALOGW("%s device info is null", __func__);
643 return AUDIO_FORMAT_DEFAULT;
644 }
645 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700646 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700647}
648
649static int out_set_format(struct audio_stream *stream, audio_format_t format)
650{
651 return 0;
652}
653
654static int out_standby(struct audio_stream *stream)
655{
656 struct stream_out *out = (struct stream_out *)stream;
657
Paul McLean994ac072016-06-02 15:33:24 -0600658 stream_lock(&out->lock);
jiabin400bbe02020-10-28 13:56:59 -0700659 device_lock(out->adev);
660 stream_standby_l(&out->alsa_devices, &out->standby);
661 device_unlock(out->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600662 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700663 return 0;
664}
665
Paul McLean6a75e4e2016-05-25 14:09:02 -0600666static int out_dump(const struct audio_stream *stream, int fd) {
667 const struct stream_out* out_stream = (const struct stream_out*) stream;
668
669 if (out_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -0700670 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600671 }
672
Simon Wilson19957a32012-04-06 16:17:12 -0700673 return 0;
674}
675
jiabin400bbe02020-10-28 13:56:59 -0700676static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -0700677{
Paul McLean65ec72b2015-02-18 09:13:24 -0800678 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800679
jiabin400bbe02020-10-28 13:56:59 -0700680 // The set parameters here only matters when the routing devices are changed.
681 // When the device version is not less than 3.0, the framework will use create
682 // audio patch API instead of set parameters to chanage audio routing.
683 return 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700684}
685
Paul McLeanf62d75e2014-07-11 15:14:19 -0700686static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
687{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700688 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600689 stream_lock(&out->lock);
jiabin8b265c92020-10-21 15:16:40 -0700690 struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
691 char *params_str = NULL;
692 if (device_info != NULL) {
693 params_str = device_get_parameters(&device_info->profile, keys);
694 }
Paul McLean994ac072016-06-02 15:33:24 -0600695 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700696 return params_str;
697}
698
Simon Wilson19957a32012-04-06 16:17:12 -0700699static uint32_t out_get_latency(const struct audio_stream_out *stream)
700{
jiabin8b265c92020-10-21 15:16:40 -0700701 struct alsa_device_info *device_info = stream_get_first_alsa_device(
702 &((struct stream_out*)stream)->alsa_devices);
703 if (device_info == NULL) {
704 ALOGW("%s device info is null", __func__);
705 return 0;
706 }
707 return proxy_get_latency(&device_info->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700708}
709
Paul McLean30f41852014-04-16 15:44:20 -0700710static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700711{
712 return -ENOSYS;
713}
714
Paul McLeaneedc92e2013-12-19 15:46:15 -0800715/* must be called with hw device and output stream mutexes locked */
716static int start_output_stream(struct stream_out *out)
717{
jiabin8b265c92020-10-21 15:16:40 -0700718 int status = 0;
719 struct listnode *node;
720 list_for_each(node, &out->alsa_devices) {
721 struct alsa_device_info *device_info =
722 node_to_item(node, struct alsa_device_info, list_node);
723 ALOGV("start_output_stream(card:%d device:%d)",
724 device_info->profile.card, device_info->profile.device);
725 status = proxy_open(&device_info->proxy);
726 if (status != 0) {
727 ALOGE("%s failed to open device(card: %d device: %d)",
728 __func__, device_info->profile.card, device_info->profile.device);
729 goto exit;
730 }
731 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800732
jiabin8b265c92020-10-21 15:16:40 -0700733exit:
734 if (status != 0) {
735 list_for_each(node, &out->alsa_devices) {
736 struct alsa_device_info *device_info =
737 node_to_item(node, struct alsa_device_info, list_node);
738 proxy_close(&device_info->proxy);
739 }
740
741 }
742 return status;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800743}
744
745static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700746{
747 int ret;
748 struct stream_out *out = (struct stream_out *)stream;
749
Paul McLean994ac072016-06-02 15:33:24 -0600750 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700751 if (out->standby) {
752 ret = start_output_stream(out);
753 if (ret != 0) {
754 goto err;
755 }
756 out->standby = false;
757 }
Eric Laurent05333d42014-08-04 20:29:17 -0700758
jiabin8b265c92020-10-21 15:16:40 -0700759 struct listnode* node;
760 list_for_each(node, &out->alsa_devices) {
761 struct alsa_device_info* device_info =
762 node_to_item(node, struct alsa_device_info, list_node);
763 alsa_device_proxy* proxy = &device_info->proxy;
764 const void * write_buff = buffer;
765 int num_write_buff_bytes = bytes;
766 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
767 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
768 if (num_device_channels != num_req_channels) {
769 /* allocate buffer */
770 const size_t required_conversion_buffer_size =
771 bytes * num_device_channels / num_req_channels;
772 if (required_conversion_buffer_size > out->conversion_buffer_size) {
773 out->conversion_buffer_size = required_conversion_buffer_size;
774 out->conversion_buffer = realloc(out->conversion_buffer,
775 out->conversion_buffer_size);
776 }
777 /* convert data */
778 const audio_format_t audio_format = out_get_format(&(out->stream.common));
779 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
780 num_write_buff_bytes =
781 adjust_channels(write_buff, num_req_channels,
782 out->conversion_buffer, num_device_channels,
783 sample_size_in_bytes, num_write_buff_bytes);
784 write_buff = out->conversion_buffer;
Andy Hung03576be2014-07-21 21:16:45 -0700785 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800786
jiabin8b265c92020-10-21 15:16:40 -0700787 if (write_buff != NULL && num_write_buff_bytes != 0) {
788 proxy_write(proxy, write_buff, num_write_buff_bytes);
789 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800790 }
Simon Wilson19957a32012-04-06 16:17:12 -0700791
Paul McLean994ac072016-06-02 15:33:24 -0600792 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700793
794 return bytes;
795
796err:
Paul McLean994ac072016-06-02 15:33:24 -0600797 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700798 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700799 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700800 out_get_sample_rate(&stream->common));
801 }
802
803 return bytes;
804}
805
Paul McLean30f41852014-04-16 15:44:20 -0700806static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700807{
808 return -EINVAL;
809}
810
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700811static int out_get_presentation_position(const struct audio_stream_out *stream,
812 uint64_t *frames, struct timespec *timestamp)
813{
Andy Hungc9515ce2015-08-04 15:05:19 -0700814 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600815 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700816
jiabin8b265c92020-10-21 15:16:40 -0700817 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
818 const int ret = device_info == NULL ? -ENODEV :
819 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
Paul McLean994ac072016-06-02 15:33:24 -0600820 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700821 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700822}
823
Simon Wilson19957a32012-04-06 16:17:12 -0700824static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
825{
826 return 0;
827}
828
829static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
830{
831 return 0;
832}
833
Paul McLean30f41852014-04-16 15:44:20 -0700834static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700835{
836 return -EINVAL;
837}
838
Paul McLean76dba682016-06-01 12:29:11 -0600839static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700840 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600841 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700842 audio_output_flags_t flags,
843 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700844 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700845 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700846{
Paul McLean76dba682016-06-01 12:29:11 -0600847 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
848 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800849
Simon Wilson19957a32012-04-06 16:17:12 -0700850 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700851
852 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600853 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700854 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600855 }
Simon Wilson19957a32012-04-06 16:17:12 -0700856
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700857 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700858 out->stream.common.get_sample_rate = out_get_sample_rate;
859 out->stream.common.set_sample_rate = out_set_sample_rate;
860 out->stream.common.get_buffer_size = out_get_buffer_size;
861 out->stream.common.get_channels = out_get_channels;
862 out->stream.common.get_format = out_get_format;
863 out->stream.common.set_format = out_set_format;
864 out->stream.common.standby = out_standby;
865 out->stream.common.dump = out_dump;
866 out->stream.common.set_parameters = out_set_parameters;
867 out->stream.common.get_parameters = out_get_parameters;
868 out->stream.common.add_audio_effect = out_add_audio_effect;
869 out->stream.common.remove_audio_effect = out_remove_audio_effect;
870 out->stream.get_latency = out_get_latency;
871 out->stream.set_volume = out_set_volume;
872 out->stream.write = out_write;
873 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700874 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700875 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
876
jiabin400bbe02020-10-28 13:56:59 -0700877 out->handle = handle;
878
Paul McLean994ac072016-06-02 15:33:24 -0600879 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700880
Paul McLean76dba682016-06-01 12:29:11 -0600881 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700882
jiabin8b265c92020-10-21 15:16:40 -0700883 list_init(&out->alsa_devices);
884 struct alsa_device_info *device_info =
885 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
886 profile_init(&device_info->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700887
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700888 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700889 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800890
Paul McLean0f1753e2014-12-02 12:36:45 -0700891 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700892 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700893
jiabin8b265c92020-10-21 15:16:40 -0700894 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700895
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700896 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800897
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700898 /* Rate */
899 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700900 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
901 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700902 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800903 } else {
jiabin8b265c92020-10-21 15:16:40 -0700904 proxy_config.rate = config->sample_rate =
905 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700906 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800907 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800908
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600909 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -0700910 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600911 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600912 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600913
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700914 /* Format */
915 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -0700916 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700917 config->format = audio_format_from_pcm_format(proxy_config.format);
918 } else {
919 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -0700920 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700921 proxy_config.format = fmt;
922 } else {
jiabin8b265c92020-10-21 15:16:40 -0700923 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700924 config->format = audio_format_from_pcm_format(proxy_config.format);
925 ret = -EINVAL;
926 }
927 }
928
929 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600930 bool calc_mask = false;
931 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
932 /* query case */
jiabin8b265c92020-10-21 15:16:40 -0700933 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -0600934 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700935 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600936 /* explicit case */
937 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700938 }
Paul McLean698dbd72015-11-03 12:24:30 -0800939
Paul McLean64345f82016-06-06 13:26:05 -0600940 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -0700941 if (out->hal_channel_count > FCC_LIMIT) {
942 out->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -0600943 calc_mask = true;
944 }
945
946 if (calc_mask) {
947 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -0700948 * 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 -0600949 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -0700950 /* position mask for mono and stereo*/
951 ? audio_channel_out_mask_from_count(out->hal_channel_count)
952 /* otherwise indexed */
953 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
Paul McLean64345f82016-06-06 13:26:05 -0600954 }
955
Andy Hung182ddc72015-05-05 23:37:30 -0700956 out->hal_channel_mask = config->channel_mask;
957
Paul McLean64345f82016-06-06 13:26:05 -0600958 // Validate the "logical" channel count against support in the "actual" profile.
959 // if they differ, choose the "actual" number of channels *closest* to the "logical".
960 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -0700961 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -0700962 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
963 proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config);
jiabin400bbe02020-10-28 13:56:59 -0700964 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -0700965
966 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700967
Paul McLean96c4d302017-12-05 09:50:26 -0800968 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
969 * So clear any errors that may have occurred above.
970 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700971 ret = 0;
972
Paul McLeaneedc92e2013-12-19 15:46:15 -0800973 out->conversion_buffer = NULL;
974 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700975
976 out->standby = true;
977
Paul McLean6a75e4e2016-05-25 14:09:02 -0600978 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600979 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600980
Simon Wilson19957a32012-04-06 16:17:12 -0700981 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700982
983 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700984}
985
Paul McLean76dba682016-06-01 12:29:11 -0600986static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700987 struct audio_stream_out *stream)
988{
989 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -0600990
jiabin400bbe02020-10-28 13:56:59 -0700991 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700992 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -0700993 stream_standby_l(&out->alsa_devices, &out->standby);
994 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800995
996 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700997
Paul McLeaneedc92e2013-12-19 15:46:15 -0800998 out->conversion_buffer = NULL;
999 out->conversion_buffer_size = 0;
1000
Paul McLean994ac072016-06-02 15:33:24 -06001001 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001002 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -06001003 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -06001004 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -07001005 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -06001006
Simon Wilson19957a32012-04-06 16:17:12 -07001007 free(stream);
1008}
1009
Paul McLean76dba682016-06-01 12:29:11 -06001010static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001011 const struct audio_config *config)
1012{
1013 /* TODO This needs to be calculated based on format/channels/rate */
1014 return 320;
1015}
1016
1017/*
1018 * IN functions
1019 */
1020static uint32_t in_get_sample_rate(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 0;
1027 }
1028 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001029 ALOGV("in_get_sample_rate() = %d", rate);
1030 return rate;
1031}
1032
1033static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1034{
1035 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
1036 return -ENOSYS;
1037}
1038
1039static size_t in_get_buffer_size(const struct audio_stream *stream)
1040{
1041 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -07001042 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1043 if (device_info == NULL) {
1044 ALOGW("%s device info is null", __func__);
1045 return 0;
1046 }
1047 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001048}
1049
1050static uint32_t in_get_channels(const struct audio_stream *stream)
1051{
Paul McLean2cfd81b2014-09-15 12:32:23 -07001052 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -07001053 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001054}
1055
1056static audio_format_t in_get_format(const struct audio_stream *stream)
1057{
jiabin8b265c92020-10-21 15:16:40 -07001058 struct alsa_device_info *device_info = stream_get_first_alsa_device(
1059 &((const struct stream_in *)stream)->alsa_devices);
1060 if (device_info == NULL) {
1061 ALOGW("%s device info is null", __func__);
1062 return AUDIO_FORMAT_DEFAULT;
1063 }
1064 alsa_device_proxy *proxy = &device_info->proxy;
Andy Hung780f1f82015-04-22 22:14:39 -07001065 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1066 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001067}
1068
1069static int in_set_format(struct audio_stream *stream, audio_format_t format)
1070{
1071 ALOGV("in_set_format(%d) - NOPE", format);
1072
1073 return -ENOSYS;
1074}
1075
1076static int in_standby(struct audio_stream *stream)
1077{
1078 struct stream_in *in = (struct stream_in *)stream;
1079
Paul McLean994ac072016-06-02 15:33:24 -06001080 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -07001081 device_lock(in->adev);
1082 stream_standby_l(&in->alsa_devices, &in->standby);
1083 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -06001084 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001085 return 0;
1086}
1087
1088static int in_dump(const struct audio_stream *stream, int fd)
1089{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001090 const struct stream_in* in_stream = (const struct stream_in*)stream;
1091 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -07001092 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001093 }
1094
1095 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001096}
1097
1098static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1099{
Paul McLean65ec72b2015-02-18 09:13:24 -08001100 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001101
jiabin400bbe02020-10-28 13:56:59 -07001102 // The set parameters here only matters when the routing devices are changed.
1103 // When the device version higher than 3.0, the framework will use create_audio_patch
1104 // API instead of set_parameters to change audio routing.
1105 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001106}
1107
1108static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1109{
1110 struct stream_in *in = (struct stream_in *)stream;
1111
Paul McLean994ac072016-06-02 15:33:24 -06001112 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001113 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1114 char *params_str = NULL;
1115 if (device_info != NULL) {
1116 params_str = device_get_parameters(&device_info->profile, keys);
1117 }
Paul McLean994ac072016-06-02 15:33:24 -06001118 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001119
1120 return params_str;
1121}
1122
1123static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1124{
1125 return 0;
1126}
1127
1128static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1129{
1130 return 0;
1131}
1132
1133static int in_set_gain(struct audio_stream_in *stream, float gain)
1134{
1135 return 0;
1136}
1137
1138/* must be called with hw device and output stream mutexes locked */
1139static int start_input_stream(struct stream_in *in)
1140{
jiabin8b265c92020-10-21 15:16:40 -07001141 // Only care about the first device as only one input device is allowed.
1142 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1143 if (device_info == NULL) {
1144 return -ENODEV;
1145 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001146
jiabin8b265c92020-10-21 15:16:40 -07001147 ALOGV("start_input_stream(card:%d device:%d)",
1148 device_info->profile.card, device_info->profile.device);
1149 return proxy_open(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001150}
1151
1152/* TODO mutex stuff here (see out_write) */
1153static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1154{
1155 size_t num_read_buff_bytes = 0;
1156 void * read_buff = buffer;
1157 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001158 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001159
1160 struct stream_in * in = (struct stream_in *)stream;
1161
Paul McLean994ac072016-06-02 15:33:24 -06001162 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001163 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001164 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001165 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001166 goto err;
1167 }
1168 in->standby = false;
1169 }
1170
jiabin8b265c92020-10-21 15:16:40 -07001171 // Only care about the first device as only one input device is allowed.
1172 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1173 if (device_info == NULL) {
1174 return 0;
1175 }
1176
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001177 /*
1178 * OK, we need to figure out how much data to read to be able to output the requested
1179 * number of bytes in the HAL format (16-bit, stereo).
1180 */
1181 num_read_buff_bytes = bytes;
jiabin8b265c92020-10-21 15:16:40 -07001182 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001183 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001184
1185 if (num_device_channels != num_req_channels) {
1186 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1187 }
1188
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001189 /* Setup/Realloc the conversion buffer (if necessary). */
1190 if (num_read_buff_bytes != bytes) {
1191 if (num_read_buff_bytes > in->conversion_buffer_size) {
1192 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1193 (and do these conversions themselves) */
1194 in->conversion_buffer_size = num_read_buff_bytes;
1195 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1196 }
1197 read_buff = in->conversion_buffer;
1198 }
1199
jiabin8b265c92020-10-21 15:16:40 -07001200 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001201 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001202 if (num_device_channels != num_req_channels) {
1203 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1204
1205 out_buff = buffer;
1206 /* Num Channels conversion */
1207 if (num_device_channels != num_req_channels) {
1208 audio_format_t audio_format = in_get_format(&(in->stream.common));
1209 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1210
1211 num_read_buff_bytes =
1212 adjust_channels(read_buff, num_device_channels,
1213 out_buff, num_req_channels,
1214 sample_size_in_bytes, num_read_buff_bytes);
1215 }
1216 }
Eric Laurent253def92014-09-14 12:18:18 -07001217
Paul McLean76dba682016-06-01 12:29:11 -06001218 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1219 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -07001220 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301221 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001222 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001223 }
1224
1225err:
Paul McLean994ac072016-06-02 15:33:24 -06001226 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001227 return num_read_buff_bytes;
1228}
1229
1230static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1231{
1232 return 0;
1233}
1234
Andy Hungfa6b4a62018-06-04 19:14:22 -07001235static int in_get_capture_position(const struct audio_stream_in *stream,
1236 int64_t *frames, int64_t *time)
1237{
1238 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1239 stream_lock(&in->lock);
1240
jiabin8b265c92020-10-21 15:16:40 -07001241 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1242
1243 const int ret = device_info == NULL ? -ENODEV
1244 : proxy_get_capture_position(&device_info->proxy, frames, time);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001245
1246 stream_unlock(&in->lock);
1247 return ret;
1248}
1249
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001250static int in_get_active_microphones(const struct audio_stream_in *stream,
1251 struct audio_microphone_characteristic_t *mic_array,
1252 size_t *mic_count) {
1253 (void)stream;
1254 (void)mic_array;
1255 (void)mic_count;
1256
1257 return -ENOSYS;
1258}
1259
1260static int in_set_microphone_direction(const struct audio_stream_in *stream,
1261 audio_microphone_direction_t dir) {
1262 (void)stream;
1263 (void)dir;
1264 ALOGV("---- in_set_microphone_direction()");
1265 return -ENOSYS;
1266}
1267
1268static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1269 (void)zoom;
1270 ALOGV("---- in_set_microphone_field_dimension()");
1271 return -ENOSYS;
1272}
1273
Paul McLean76dba682016-06-01 12:29:11 -06001274static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001275 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001276 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001277 struct audio_config *config,
1278 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001279 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001280 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001281 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001282{
Paul McLean1d585cc2016-05-24 11:28:10 -06001283 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001284 config->sample_rate, config->channel_mask, config->format);
1285
Andy Hungb10ce6d2017-10-27 19:37:58 -07001286 /* Pull out the card/device pair */
1287 int32_t card, device;
1288 if (!parse_card_device_params(address, &card, &device)) {
1289 ALOGW("%s fail - invalid address %s", __func__, address);
1290 *stream_in = NULL;
1291 return -EINVAL;
1292 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001293
Andy Hungb10ce6d2017-10-27 19:37:58 -07001294 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001295 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001296 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001297 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001298 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001299
1300 /* setup function pointers */
1301 in->stream.common.get_sample_rate = in_get_sample_rate;
1302 in->stream.common.set_sample_rate = in_set_sample_rate;
1303 in->stream.common.get_buffer_size = in_get_buffer_size;
1304 in->stream.common.get_channels = in_get_channels;
1305 in->stream.common.get_format = in_get_format;
1306 in->stream.common.set_format = in_set_format;
1307 in->stream.common.standby = in_standby;
1308 in->stream.common.dump = in_dump;
1309 in->stream.common.set_parameters = in_set_parameters;
1310 in->stream.common.get_parameters = in_get_parameters;
1311 in->stream.common.add_audio_effect = in_add_audio_effect;
1312 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1313
1314 in->stream.set_gain = in_set_gain;
1315 in->stream.read = in_read;
1316 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001317 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001318
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001319 in->stream.get_active_microphones = in_get_active_microphones;
1320 in->stream.set_microphone_direction = in_set_microphone_direction;
1321 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1322
jiabin400bbe02020-10-28 13:56:59 -07001323 in->handle = handle;
1324
Paul McLean994ac072016-06-02 15:33:24 -06001325 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001326
Paul McLean76dba682016-06-01 12:29:11 -06001327 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001328
jiabin8b265c92020-10-21 15:16:40 -07001329 list_init(&in->alsa_devices);
1330 struct alsa_device_info *device_info =
1331 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1332 profile_init(&device_info->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001333
jiabin400bbe02020-10-28 13:56:59 -07001334 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001335
Andy Hungb10ce6d2017-10-27 19:37:58 -07001336 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001337 device_lock(in->adev);
1338 int num_open_inputs = in->adev->inputs_open;
1339 device_unlock(in->adev);
1340
Andy Hungb10ce6d2017-10-27 19:37:58 -07001341 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001342 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001343 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001344 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1345 __func__, card, device);
1346 ret = -EINVAL;
1347 }
1348 } else {
1349 /* Read input profile only if necessary */
jiabin8b265c92020-10-21 15:16:40 -07001350 device_info->profile.card = card;
1351 device_info->profile.device = device;
1352 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001353 ALOGW("%s fail - cannot read profile", __func__);
1354 ret = -EINVAL;
1355 }
1356 }
1357 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001358 free(in);
1359 *stream_in = NULL;
1360 return ret;
1361 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001362
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001363 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001364 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001365 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001366 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001367 }
1368
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001369 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001370 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001371 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001372 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001373 if (highest_rate == 0) {
1374 ret = -EINVAL; /* error with device */
1375 } else {
jiabin400bbe02020-10-28 13:56:59 -07001376 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001377 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001378 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001379 /* Changing the requested rate */
1380 ret = -EINVAL;
1381 } else {
1382 /* Everything AOK! */
1383 ret = 0;
1384 }
1385 }
1386 }
jiabin8b265c92020-10-21 15:16:40 -07001387 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001388 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001389 } else {
jiabin400bbe02020-10-28 13:56:59 -07001390 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001391 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001392 ret = -EINVAL;
1393 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001394
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001395 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001396 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001397 in->config.format = profile_get_default_format(&device_info->profile);
1398 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001399 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001400 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001401 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001402 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001403 } else {
jiabin400bbe02020-10-28 13:56:59 -07001404 in->config.format = profile_get_default_format(&device_info->profile);
1405 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001406 ret = -EINVAL;
1407 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001408 }
1409
Paul McLean2cfd81b2014-09-15 12:32:23 -07001410 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001411 bool calc_mask = false;
1412 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1413 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001414 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001415 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001416 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001417 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001418 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1419 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001420
Paul McLean64345f82016-06-06 13:26:05 -06001421 /* The Framework is currently limited to no more than this number of channels */
Andy Hungdf511202021-06-07 16:45:03 -07001422 if (in->hal_channel_count > FCC_LIMIT) {
1423 in->hal_channel_count = FCC_LIMIT;
Paul McLean64345f82016-06-06 13:26:05 -06001424 calc_mask = true;
1425 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001426
Paul McLean64345f82016-06-06 13:26:05 -06001427 if (calc_mask) {
1428 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001429 * 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 -06001430 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1431 /* position mask for mono & stereo */
1432 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1433 /* otherwise indexed */
1434 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001435
Paul McLean64345f82016-06-06 13:26:05 -06001436 // if we change the mask...
1437 if (in->hal_channel_mask != config->channel_mask &&
1438 config->channel_mask != AUDIO_CHANNEL_NONE) {
1439 config->channel_mask = in->hal_channel_mask;
1440 ret = -EINVAL;
1441 }
1442 } else {
1443 in->hal_channel_mask = config->channel_mask;
1444 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001445
Paul McLean64345f82016-06-06 13:26:05 -06001446 if (ret == 0) {
1447 // Validate the "logical" channel count against support in the "actual" profile.
1448 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1449 // and store THAT in proxy_config.channels
jiabin400bbe02020-10-28 13:56:59 -07001450 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001451 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabin400bbe02020-10-28 13:56:59 -07001452 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config);
Eric Laurent981f7742017-05-03 12:44:26 -07001453 if (ret == 0) {
1454 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001455
Eric Laurent981f7742017-05-03 12:44:26 -07001456 in->conversion_buffer = NULL;
1457 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001458
Eric Laurent981f7742017-05-03 12:44:26 -07001459 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001460
Eric Laurent981f7742017-05-03 12:44:26 -07001461 /* Save this for adev_dump() */
1462 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1463 } else {
1464 ALOGW("proxy_prepare error %d", ret);
jiabin8b265c92020-10-21 15:16:40 -07001465 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001466 config->channel_mask = channel_count <= FCC_2
1467 ? audio_channel_in_mask_from_count(channel_count)
1468 : audio_channel_mask_for_index_assignment_from_count(channel_count);
jiabin8b265c92020-10-21 15:16:40 -07001469 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1470 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001471 }
1472 }
Paul McLean64345f82016-06-06 13:26:05 -06001473
Eric Laurent981f7742017-05-03 12:44:26 -07001474 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001475 // Deallocate this stream on error, because AudioFlinger won't call
1476 // adev_close_input_stream() in this case.
1477 *stream_in = NULL;
1478 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001479 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001480 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001481
jiabin8b265c92020-10-21 15:16:40 -07001482 list_add_tail(&in->alsa_devices, &device_info->list_node);
1483
Andy Hungb10ce6d2017-10-27 19:37:58 -07001484 device_lock(in->adev);
1485 ++in->adev->inputs_open;
1486 device_unlock(in->adev);
1487
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001488 return ret;
1489}
1490
Paul McLean76dba682016-06-01 12:29:11 -06001491static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1492 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001493{
1494 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001495
jiabin400bbe02020-10-28 13:56:59 -07001496 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001497 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001498 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001499 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001500 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1501 if (device_info != NULL) {
1502 ALOGV("adev_close_input_stream(c:%d d:%d)",
1503 device_info->profile.card, device_info->profile.device);
1504 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001505 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1506 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001507
1508 stream_standby_l(&in->alsa_devices, &in->standby);
1509
Andy Hungb10ce6d2017-10-27 19:37:58 -07001510 device_unlock(in->adev);
1511
jiabin400bbe02020-10-28 13:56:59 -07001512 stream_clear_devices(&in->alsa_devices);
1513 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001514
1515 free(in->conversion_buffer);
1516
1517 free(stream);
1518}
1519
1520/*
1521 * ADEV Functions
1522 */
Paul McLean76dba682016-06-01 12:29:11 -06001523static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001524{
1525 return 0;
1526}
1527
Paul McLean76dba682016-06-01 12:29:11 -06001528static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001529{
1530 return strdup("");
1531}
1532
Paul McLean76dba682016-06-01 12:29:11 -06001533static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001534{
1535 return 0;
1536}
1537
Paul McLean76dba682016-06-01 12:29:11 -06001538static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001539{
1540 return -ENOSYS;
1541}
1542
Paul McLean76dba682016-06-01 12:29:11 -06001543static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001544{
1545 return -ENOSYS;
1546}
1547
Paul McLean76dba682016-06-01 12:29:11 -06001548static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001549{
1550 return 0;
1551}
1552
Paul McLean76dba682016-06-01 12:29:11 -06001553static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001554{
Paul McLean76dba682016-06-01 12:29:11 -06001555 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001556 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001557 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001558 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001559 return -ENOSYS;
1560}
1561
Paul McLean76dba682016-06-01 12:29:11 -06001562static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001563{
1564 return -ENOSYS;
1565}
1566
jiabin400bbe02020-10-28 13:56:59 -07001567static int adev_create_audio_patch(struct audio_hw_device *dev,
1568 unsigned int num_sources,
1569 const struct audio_port_config *sources,
1570 unsigned int num_sinks,
1571 const struct audio_port_config *sinks,
1572 audio_patch_handle_t *handle) {
1573 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1574 // Only accept mix->device and device->mix cases. In that case, the number of sources
1575 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1576 return -EINVAL;
1577 }
1578
1579 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1580 // If source is a device, the number of sinks should be 1.
1581 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1582 return -EINVAL;
1583 }
1584 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1585 // If source is a mix, all sinks should be device.
1586 for (unsigned int i = 0; i < num_sinks; i++) {
1587 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1588 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1589 return -EINVAL;
1590 }
1591 }
1592 } else {
1593 // All other cases are invalid.
1594 return -EINVAL;
1595 }
1596
1597 struct audio_device* adev = (struct audio_device*) dev;
1598 bool generatedPatchHandle = false;
1599 device_lock(adev);
1600 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1601 *handle = ++adev->next_patch_handle;
1602 generatedPatchHandle = true;
1603 }
1604
1605 int cards[AUDIO_PATCH_PORTS_MAX];
1606 int devices[AUDIO_PATCH_PORTS_MAX];
1607 const struct audio_port_config *port_configs =
1608 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1609 int num_configs = 0;
1610 audio_io_handle_t io_handle = 0;
1611 bool wasStandby = true;
1612 int direction = PCM_OUT;
1613 audio_patch_handle_t *patch_handle = NULL;
1614 struct listnode *alsa_devices = NULL;
1615 struct stream_lock *lock = NULL;
1616 struct pcm_config *config = NULL;
1617 struct stream_in *in = NULL;
1618 struct stream_out *out = NULL;
1619
1620 unsigned int num_saved_devices = 0;
1621 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1622 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1623
1624 struct listnode *node;
1625
1626 // Only handle patches for mix->devices and device->mix case.
1627 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1628 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1629 if (in == NULL) {
1630 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1631 device_unlock(adev);
1632 return -EINVAL;
1633 }
1634
1635 direction = PCM_IN;
1636 wasStandby = in->standby;
1637 io_handle = in->handle;
1638 num_configs = num_sources;
1639 patch_handle = &in->patch_handle;
1640 alsa_devices = &in->alsa_devices;
1641 lock = &in->lock;
1642 config = &in->config;
1643 } else {
1644 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1645 if (out == NULL) {
1646 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1647 device_unlock(adev);
1648 return -EINVAL;
1649 }
1650
1651 direction = PCM_OUT;
1652 wasStandby = out->standby;
1653 io_handle = out->handle;
1654 num_configs = num_sinks;
1655 patch_handle = &out->patch_handle;
1656 alsa_devices = &out->alsa_devices;
1657 lock = &out->lock;
1658 config = &out->config;
1659 }
1660
1661 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1662 if (!generatedPatchHandle && *patch_handle != *handle) {
1663 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1664 "with handle(%d) when creating audio patch",
1665 __func__, *handle, *patch_handle, io_handle);
1666 device_unlock(adev);
1667 return -EINVAL;
1668 }
1669 device_unlock(adev);
1670
1671 for (unsigned int i = 0; i < num_configs; ++i) {
1672 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1673 ALOGE("%s, failed to parse card and device %s",
1674 __func__, port_configs[i].ext.device.address);
1675 return -EINVAL;
1676 }
1677 }
1678
1679 stream_lock(lock);
1680 list_for_each (node, alsa_devices) {
1681 struct alsa_device_info *device_info =
1682 node_to_item(node, struct alsa_device_info, list_node);
1683 saved_cards[num_saved_devices] = device_info->profile.card;
1684 saved_devices[num_saved_devices++] = device_info->profile.device;
1685 }
1686
jiabin4bc5f252021-06-02 18:31:13 +00001687 if (are_devices_the_same(
1688 num_configs, cards, devices, num_saved_devices, saved_cards, saved_devices)) {
1689 // The new devices are the same as original ones. No need to update.
1690 stream_unlock(lock);
1691 return 0;
1692 }
1693
jiabin400bbe02020-10-28 13:56:59 -07001694 device_lock(adev);
1695 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1696 device_unlock(adev);
1697
Andy Hung61583422021-07-02 16:13:11 -07001698 // Timestamps:
1699 // Audio timestamps assume continuous PCM frame counts which are maintained
1700 // with the device proxy.transferred variable. Technically it would be better
1701 // associated with in or out stream, not the device; here we save and restore
1702 // using the first alsa device as a simplification.
1703 uint64_t saved_transferred_frames = 0;
1704 struct alsa_device_info *device_info = stream_get_first_alsa_device(alsa_devices);
1705 if (device_info != NULL) saved_transferred_frames = device_info->proxy.transferred;
1706
jiabin400bbe02020-10-28 13:56:59 -07001707 int ret = stream_set_new_devices(config, alsa_devices, num_configs, cards, devices, direction);
1708
1709 if (ret != 0) {
1710 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1711 stream_set_new_devices(
1712 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction);
1713 } else {
1714 *patch_handle = *handle;
1715 }
Andy Hung61583422021-07-02 16:13:11 -07001716
1717 // Timestamps: Restore transferred frames.
1718 if (saved_transferred_frames != 0) {
1719 device_info = stream_get_first_alsa_device(alsa_devices);
1720 if (device_info != NULL) device_info->proxy.transferred = saved_transferred_frames;
1721 }
1722
jiabin400bbe02020-10-28 13:56:59 -07001723 if (!wasStandby) {
1724 device_lock(adev);
1725 if (in != NULL) {
1726 start_input_stream(in);
1727 }
1728 if (out != NULL) {
1729 start_output_stream(out);
1730 }
1731 device_unlock(adev);
1732 }
1733 stream_unlock(lock);
1734 return ret;
1735}
1736
1737static int adev_release_audio_patch(struct audio_hw_device *dev,
1738 audio_patch_handle_t patch_handle)
1739{
1740 struct audio_device* adev = (struct audio_device*) dev;
1741
1742 device_lock(adev);
1743 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1744 device_unlock(adev);
1745 if (out != NULL) {
1746 stream_lock(&out->lock);
1747 device_lock(adev);
1748 stream_standby_l(&out->alsa_devices, &out->standby);
1749 device_unlock(adev);
1750 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1751 stream_unlock(&out->lock);
1752 return 0;
1753 }
1754
1755 device_lock(adev);
1756 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1757 device_unlock(adev);
1758 if (in != NULL) {
1759 stream_lock(&in->lock);
1760 device_lock(adev);
1761 stream_standby_l(&in->alsa_devices, &in->standby);
1762 device_unlock(adev);
1763 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1764 stream_unlock(&in->lock);
1765 return 0;
1766 }
1767
1768 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1769 return -EINVAL;
1770}
1771
1772static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1773{
1774 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1775 return -EINVAL;
1776 }
1777
1778 alsa_device_profile profile;
1779 const bool is_output = audio_is_output_device(port->ext.device.type);
1780 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1781 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1782 return -EINVAL;
1783 }
1784
1785 if (!profile_read_device_info(&profile)) {
1786 return -ENOENT;
1787 }
1788
1789 port->num_formats = 0;;
1790 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1791 profile.formats[i] != 0; ++i) {
1792 audio_format_t format = audio_format_from(profile.formats[i]);
1793 if (format != AUDIO_FORMAT_INVALID) {
1794 port->formats[port->num_formats++] = format;
1795 }
1796 }
1797
jiabina635fcf2021-01-11 11:34:50 -08001798 port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1799 port->num_channel_masks = populate_channel_mask_from_profile(
1800 &profile, is_output, port->channel_masks);
jiabin400bbe02020-10-28 13:56:59 -07001801
jiabina635fcf2021-01-11 11:34:50 -08001802 return 0;
1803}
1804
1805static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1806{
1807 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1808 return -EINVAL;
jiabin400bbe02020-10-28 13:56:59 -07001809 }
jiabina635fcf2021-01-11 11:34:50 -08001810
1811 alsa_device_profile profile;
1812 const bool is_output = audio_is_output_device(port->ext.device.type);
1813 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1814 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1815 return -EINVAL;
1816 }
1817
1818 if (!profile_read_device_info(&profile)) {
1819 return -ENOENT;
1820 }
1821
1822 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1823 unsigned int num_channel_masks = populate_channel_mask_from_profile(
1824 &profile, is_output, channel_masks);
1825 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1826 const unsigned int num_sample_rates =
1827 populate_sample_rates_from_profile(&profile, sample_rates);
1828 port->num_audio_profiles = 0;;
1829 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1830 profile.formats[i] != 0; ++i) {
1831 audio_format_t format = audio_format_from(profile.formats[i]);
1832 if (format == AUDIO_FORMAT_INVALID) {
1833 continue;
1834 }
1835 const unsigned int j = port->num_audio_profiles++;
1836 port->audio_profiles[j].format = format;
1837 port->audio_profiles[j].num_sample_rates = num_sample_rates;
1838 memcpy(port->audio_profiles[j].sample_rates,
1839 sample_rates,
1840 num_sample_rates * sizeof(unsigned int));
1841 port->audio_profiles[j].num_channel_masks = num_channel_masks;
1842 memcpy(port->audio_profiles[j].channel_masks,
1843 channel_masks,
1844 num_channel_masks* sizeof(audio_channel_mask_t));
1845 }
1846
jiabin400bbe02020-10-28 13:56:59 -07001847 return 0;
1848}
1849
Paul McLean76dba682016-06-01 12:29:11 -06001850static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001851{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001852 dprintf(fd, "\nUSB audio module:\n");
1853
1854 struct audio_device* adev = (struct audio_device*)device;
1855 const int kNumRetries = 3;
1856 const int kSleepTimeMS = 500;
1857
Paul McLean994ac072016-06-02 15:33:24 -06001858 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001859 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001860 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001861 sleep(kSleepTimeMS);
1862 retry--;
1863 }
1864
1865 if (retry > 0) {
1866 if (list_empty(&adev->output_stream_list)) {
1867 dprintf(fd, " No output streams.\n");
1868 } else {
1869 struct listnode* node;
1870 list_for_each(node, &adev->output_stream_list) {
1871 struct audio_stream* stream =
1872 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1873 out_dump(stream, fd);
1874 }
1875 }
1876
1877 if (list_empty(&adev->input_stream_list)) {
1878 dprintf(fd, "\n No input streams.\n");
1879 } else {
1880 struct listnode* node;
1881 list_for_each(node, &adev->input_stream_list) {
1882 struct audio_stream* stream =
1883 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1884 in_dump(stream, fd);
1885 }
1886 }
1887
Paul McLean994ac072016-06-02 15:33:24 -06001888 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001889 } else {
1890 // Couldn't lock
1891 dprintf(fd, " Could not obtain device lock.\n");
1892 }
1893
Simon Wilson19957a32012-04-06 16:17:12 -07001894 return 0;
1895}
1896
1897static int adev_close(hw_device_t *device)
1898{
Simon Wilson19957a32012-04-06 16:17:12 -07001899 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001900
Simon Wilson19957a32012-04-06 16:17:12 -07001901 return 0;
1902}
1903
Paul McLean30f41852014-04-16 15:44:20 -07001904static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001905{
Simon Wilson19957a32012-04-06 16:17:12 -07001906 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1907 return -EINVAL;
1908
Paul McLeaneedc92e2013-12-19 15:46:15 -08001909 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001910 if (!adev)
1911 return -ENOMEM;
1912
Paul McLeancf5310a2018-08-22 14:33:12 -07001913 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001914
Paul McLean6a75e4e2016-05-25 14:09:02 -06001915 list_init(&adev->output_stream_list);
1916 list_init(&adev->input_stream_list);
1917
Simon Wilson19957a32012-04-06 16:17:12 -07001918 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabina635fcf2021-01-11 11:34:50 -08001919 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001920 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001921 adev->hw_device.common.close = adev_close;
1922
Simon Wilson19957a32012-04-06 16:17:12 -07001923 adev->hw_device.init_check = adev_init_check;
1924 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1925 adev->hw_device.set_master_volume = adev_set_master_volume;
1926 adev->hw_device.set_mode = adev_set_mode;
1927 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1928 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1929 adev->hw_device.set_parameters = adev_set_parameters;
1930 adev->hw_device.get_parameters = adev_get_parameters;
1931 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1932 adev->hw_device.open_output_stream = adev_open_output_stream;
1933 adev->hw_device.close_output_stream = adev_close_output_stream;
1934 adev->hw_device.open_input_stream = adev_open_input_stream;
1935 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07001936 adev->hw_device.create_audio_patch = adev_create_audio_patch;
1937 adev->hw_device.release_audio_patch = adev_release_audio_patch;
1938 adev->hw_device.get_audio_port = adev_get_audio_port;
jiabina635fcf2021-01-11 11:34:50 -08001939 adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
Simon Wilson19957a32012-04-06 16:17:12 -07001940 adev->hw_device.dump = adev_dump;
1941
1942 *device = &adev->hw_device.common;
1943
1944 return 0;
1945}
1946
1947static struct hw_module_methods_t hal_module_methods = {
1948 .open = adev_open,
1949};
1950
1951struct audio_module HAL_MODULE_INFO_SYM = {
1952 .common = {
1953 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001954 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1955 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001956 .id = AUDIO_HARDWARE_MODULE_ID,
1957 .name = "USB audio HW HAL",
1958 .author = "The Android Open Source Project",
1959 .methods = &hal_module_methods,
1960 },
1961};