blob: cdcf2542863bb9c2429387a9dde670ac20aeb5bf [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
161static const audio_channel_mask_t OUT_CHANNEL_MASKS_MAP[] = {
162 AUDIO_CHANNEL_NONE, /* 0 */
163 AUDIO_CHANNEL_OUT_MONO, /* 1 */
164 AUDIO_CHANNEL_OUT_STEREO, /* 2 */
165 AUDIO_CHANNEL_OUT_2POINT1, /* 3 */
166 AUDIO_CHANNEL_OUT_QUAD, /* 4 */
167 AUDIO_CHANNEL_OUT_PENTA, /* 5 */
168 AUDIO_CHANNEL_OUT_5POINT1, /* 6 */
169 AUDIO_CHANNEL_OUT_6POINT1, /* 7 */
170 AUDIO_CHANNEL_OUT_7POINT1 /* 8 */
171 /* channel counts greater than this are not considered */
172};
173static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP);
174
175// Map channel count to input channel mask
176static const audio_channel_mask_t IN_CHANNEL_MASKS_MAP[] = {
177 AUDIO_CHANNEL_NONE, /* 0 */
178 AUDIO_CHANNEL_IN_MONO, /* 1 */
179 AUDIO_CHANNEL_IN_STEREO, /* 2 */
180 /* channel counts greater than this are not considered */
181};
182static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP);
183
184// Map channel count to index mask
185static const audio_channel_mask_t CHANNEL_INDEX_MASKS_MAP[] = {
186 AUDIO_CHANNEL_NONE, /* 0 */
187 AUDIO_CHANNEL_INDEX_MASK_1, /* 1 */
188 AUDIO_CHANNEL_INDEX_MASK_2, /* 2 */
189 AUDIO_CHANNEL_INDEX_MASK_3, /* 3 */
190 AUDIO_CHANNEL_INDEX_MASK_4, /* 4 */
191 AUDIO_CHANNEL_INDEX_MASK_5, /* 5 */
192 AUDIO_CHANNEL_INDEX_MASK_6, /* 6 */
193 AUDIO_CHANNEL_INDEX_MASK_7, /* 7 */
194 AUDIO_CHANNEL_INDEX_MASK_8 /* 8 */
195 /* channel counts greater than this are not considered */
196};
197static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP);
198
199
200
Paul McLean994ac072016-06-02 15:33:24 -0600201/*
202 * Locking Helpers
203 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800204/*
Eric Laurent70318092015-06-19 17:49:17 -0700205 * NOTE: when multiple mutexes have to be acquired, always take the
206 * stream_in or stream_out mutex first, followed by the audio_device mutex.
207 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
208 * higher priority playback or capture thread.
209 */
210
Paul McLean994ac072016-06-02 15:33:24 -0600211static void stream_lock_init(struct stream_lock *lock) {
212 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
213 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
214}
215
216static void stream_lock(struct stream_lock *lock) {
jiabin400bbe02020-10-28 13:56:59 -0700217 if (lock == NULL) {
218 return;
219 }
Paul McLean994ac072016-06-02 15:33:24 -0600220 pthread_mutex_lock(&lock->pre_lock);
221 pthread_mutex_lock(&lock->lock);
222 pthread_mutex_unlock(&lock->pre_lock);
223}
224
225static void stream_unlock(struct stream_lock *lock) {
226 pthread_mutex_unlock(&lock->lock);
227}
228
229static void device_lock(struct audio_device *adev) {
230 pthread_mutex_lock(&adev->lock);
231}
232
233static int device_try_lock(struct audio_device *adev) {
234 return pthread_mutex_trylock(&adev->lock);
235}
236
237static void device_unlock(struct audio_device *adev) {
238 pthread_mutex_unlock(&adev->lock);
239}
240
241/*
242 * streams list management
243 */
244static void adev_add_stream_to_list(
245 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
246 device_lock(adev);
247
248 list_add_tail(list, stream_node);
249
250 device_unlock(adev);
251}
252
jiabin400bbe02020-10-28 13:56:59 -0700253static struct stream_out* adev_get_stream_out_by_io_handle_l(
254 struct audio_device* adev, audio_io_handle_t handle) {
255 struct listnode *node;
256 list_for_each (node, &adev->output_stream_list) {
257 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
258 if (out->handle == handle) {
259 return out;
260 }
261 }
262 return NULL;
263}
Paul McLean994ac072016-06-02 15:33:24 -0600264
jiabin400bbe02020-10-28 13:56:59 -0700265static struct stream_in* adev_get_stream_in_by_io_handle_l(
266 struct audio_device* adev, audio_io_handle_t handle) {
267 struct listnode *node;
268 list_for_each (node, &adev->input_stream_list) {
269 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
270 if (in->handle == handle) {
271 return in;
272 }
273 }
274 return NULL;
275}
Paul McLean994ac072016-06-02 15:33:24 -0600276
jiabin400bbe02020-10-28 13:56:59 -0700277static struct stream_out* adev_get_stream_out_by_patch_handle_l(
278 struct audio_device* adev, audio_patch_handle_t patch_handle) {
279 struct listnode *node;
280 list_for_each (node, &adev->output_stream_list) {
281 struct stream_out *out = node_to_item(node, struct stream_out, list_node);
282 if (out->patch_handle == patch_handle) {
283 return out;
284 }
285 }
286 return NULL;
287}
288
289static struct stream_in* adev_get_stream_in_by_patch_handle_l(
290 struct audio_device* adev, audio_patch_handle_t patch_handle) {
291 struct listnode *node;
292 list_for_each (node, &adev->input_stream_list) {
293 struct stream_in *in = node_to_item(node, struct stream_in, list_node);
294 if (in->patch_handle == patch_handle) {
295 return in;
296 }
297 }
298 return NULL;
Paul McLean994ac072016-06-02 15:33:24 -0600299}
300
Eric Laurent70318092015-06-19 17:49:17 -0700301/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700302 * Extract the card and device numbers from the supplied key/value pairs.
303 * kvpairs A null-terminated string containing the key/value pairs or card and device.
304 * i.e. "card=1;device=42"
305 * card A pointer to a variable to receive the parsed-out card number.
306 * device A pointer to a variable to receive the parsed-out device number.
307 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
308 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800309 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700310 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800311static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700312{
313 struct str_parms * parms = str_parms_create_str(kvpairs);
314 char value[32];
315 int param_val;
316
317 // initialize to "undefined" state.
318 *card = -1;
319 *device = -1;
320
321 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
322 if (param_val >= 0) {
323 *card = atoi(value);
324 }
325
326 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
327 if (param_val >= 0) {
328 *device = atoi(value);
329 }
330
331 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800332
333 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700334}
335
Andy Hung4e6a1c02017-10-27 20:31:46 -0700336static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800337{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700338 if (profile->card < 0 || profile->device < 0) {
339 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800340 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700341
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700342 struct str_parms *query = str_parms_create_str(keys);
343 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700344
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700345 /* These keys are from hardware/libhardware/include/audio.h */
346 /* supported sample rates */
347 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
348 char* rates_list = profile_get_sample_rate_strs(profile);
349 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
350 rates_list);
351 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700352 }
353
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700354 /* supported channel counts */
355 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
356 char* channels_list = profile_get_channel_count_strs(profile);
357 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
358 channels_list);
359 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700360 }
361
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700362 /* supported sample formats */
363 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
364 char * format_params = profile_get_format_strs(profile);
365 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
366 format_params);
367 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700368 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700369 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700370
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700371 char* result_str = str_parms_to_str(result);
372 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700373
Paul McLean65ec72b2015-02-18 09:13:24 -0800374 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700375
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700376 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800377}
378
jiabin400bbe02020-10-28 13:56:59 -0700379static audio_format_t audio_format_from(enum pcm_format format)
380{
381 switch (format) {
382 case PCM_FORMAT_S16_LE:
383 return AUDIO_FORMAT_PCM_16_BIT;
384 case PCM_FORMAT_S32_LE:
385 return AUDIO_FORMAT_PCM_32_BIT;
386 case PCM_FORMAT_S8:
387 return AUDIO_FORMAT_PCM_8_BIT;
388 case PCM_FORMAT_S24_LE:
389 return AUDIO_FORMAT_PCM_8_24_BIT;
390 case PCM_FORMAT_S24_3LE:
391 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
392 default:
393 return AUDIO_FORMAT_INVALID;
394 }
395}
396
Paul McLeaneedc92e2013-12-19 15:46:15 -0800397/*
398 * HAl Functions
399 */
Simon Wilson19957a32012-04-06 16:17:12 -0700400/**
401 * NOTE: when multiple mutexes have to be acquired, always respect the
402 * following order: hw device > out stream
403 */
404
jiabin8b265c92020-10-21 15:16:40 -0700405static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
406 if (list_empty(alsa_devices)) {
407 return NULL;
408 }
409 return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
410}
411
jiabin400bbe02020-10-28 13:56:59 -0700412/**
413 * Must be called with holding the stream's lock.
414 */
415static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
416{
417 if (!*standby) {
418 struct listnode *node;
419 list_for_each (node, alsa_devices) {
420 struct alsa_device_info *device_info =
421 node_to_item(node, struct alsa_device_info, list_node);
422 proxy_close(&device_info->proxy);
423 }
424 *standby = true;
425 }
426}
427
428static void stream_clear_devices(struct listnode *alsa_devices)
429{
430 struct listnode *node, *temp;
431 struct alsa_device_info *device_info = NULL;
432 list_for_each_safe (node, temp, alsa_devices) {
433 device_info = node_to_item(node, struct alsa_device_info, list_node);
434 if (device_info != NULL) {
435 list_remove(&device_info->list_node);
436 free(device_info);
437 }
438 }
439}
440
441static int stream_set_new_devices(struct pcm_config *config,
442 struct listnode *alsa_devices,
443 unsigned int num_devices,
444 const int cards[],
445 const int devices[],
446 int direction)
447{
448 int status = 0;
449 stream_clear_devices(alsa_devices);
450
451 for (unsigned int i = 0; i < num_devices; ++i) {
452 struct alsa_device_info *device_info =
453 (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
454 profile_init(&device_info->profile, direction);
455 device_info->profile.card = cards[i];
456 device_info->profile.device = devices[i];
457 status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
458 if (status != 0) {
459 ALOGE("%s failed to read device info card=%d;device=%d",
460 __func__, cards[i], devices[i]);
461 goto exit;
462 }
463 status = proxy_prepare(&device_info->proxy, &device_info->profile, config);
464 if (status != 0) {
465 ALOGE("%s failed to prepare device card=%d;device=%d",
466 __func__, cards[i], devices[i]);
467 goto exit;
468 }
469 list_add_tail(alsa_devices, &device_info->list_node);
470 }
471
472exit:
473 if (status != 0) {
474 stream_clear_devices(alsa_devices);
475 }
476 return status;
477}
478
jiabin8b265c92020-10-21 15:16:40 -0700479static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
480 struct listnode *node;
481 size_t i = 0;
482 list_for_each(node, alsa_devices) {
483 struct alsa_device_info *device_info =
484 node_to_item(node, struct alsa_device_info, list_node);
485 dprintf(fd, "Output Profile %zu:\n", i);
486 profile_dump(&device_info->profile, fd);
487
488 dprintf(fd, "Output Proxy %zu:\n", i);
489 proxy_dump(&device_info->proxy, fd);
490 }
491}
492
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700493/*
494 * OUT functions
495 */
Simon Wilson19957a32012-04-06 16:17:12 -0700496static uint32_t out_get_sample_rate(const struct audio_stream *stream)
497{
jiabin8b265c92020-10-21 15:16:40 -0700498 struct alsa_device_info *device_info = stream_get_first_alsa_device(
499 &((struct stream_out*)stream)->alsa_devices);
500 if (device_info == NULL) {
501 ALOGW("%s device info is null", __func__);
502 return 0;
503 }
504 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700505 ALOGV("out_get_sample_rate() = %d", rate);
506 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700507}
508
509static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
510{
511 return 0;
512}
513
514static size_t out_get_buffer_size(const struct audio_stream *stream)
515{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700516 const struct stream_out* out = (const struct stream_out*)stream;
jiabin8b265c92020-10-21 15:16:40 -0700517 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
518 if (device_info == NULL) {
519 ALOGW("%s device info is null", __func__);
520 return 0;
521 }
522 return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
Simon Wilson19957a32012-04-06 16:17:12 -0700523}
524
525static uint32_t out_get_channels(const struct audio_stream *stream)
526{
Andy Hung03576be2014-07-21 21:16:45 -0700527 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700528 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700529}
530
531static audio_format_t out_get_format(const struct audio_stream *stream)
532{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700533 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
534 * Relies on the framework to provide data in the specified format.
535 * This could change in the future.
536 */
jiabin8b265c92020-10-21 15:16:40 -0700537 struct alsa_device_info *device_info = stream_get_first_alsa_device(
538 &((struct stream_out*)stream)->alsa_devices);
539 if (device_info == NULL) {
540 ALOGW("%s device info is null", __func__);
541 return AUDIO_FORMAT_DEFAULT;
542 }
543 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700544 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700545}
546
547static int out_set_format(struct audio_stream *stream, audio_format_t format)
548{
549 return 0;
550}
551
552static int out_standby(struct audio_stream *stream)
553{
554 struct stream_out *out = (struct stream_out *)stream;
555
Paul McLean994ac072016-06-02 15:33:24 -0600556 stream_lock(&out->lock);
jiabin400bbe02020-10-28 13:56:59 -0700557 device_lock(out->adev);
558 stream_standby_l(&out->alsa_devices, &out->standby);
559 device_unlock(out->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600560 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700561 return 0;
562}
563
Paul McLean6a75e4e2016-05-25 14:09:02 -0600564static int out_dump(const struct audio_stream *stream, int fd) {
565 const struct stream_out* out_stream = (const struct stream_out*) stream;
566
567 if (out_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -0700568 stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600569 }
570
Simon Wilson19957a32012-04-06 16:17:12 -0700571 return 0;
572}
573
jiabin400bbe02020-10-28 13:56:59 -0700574static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -0700575{
Paul McLean65ec72b2015-02-18 09:13:24 -0800576 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800577
jiabin400bbe02020-10-28 13:56:59 -0700578 // The set parameters here only matters when the routing devices are changed.
579 // When the device version is not less than 3.0, the framework will use create
580 // audio patch API instead of set parameters to chanage audio routing.
581 return 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700582}
583
Paul McLeanf62d75e2014-07-11 15:14:19 -0700584static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
585{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700586 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600587 stream_lock(&out->lock);
jiabin8b265c92020-10-21 15:16:40 -0700588 struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
589 char *params_str = NULL;
590 if (device_info != NULL) {
591 params_str = device_get_parameters(&device_info->profile, keys);
592 }
Paul McLean994ac072016-06-02 15:33:24 -0600593 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700594 return params_str;
595}
596
Simon Wilson19957a32012-04-06 16:17:12 -0700597static uint32_t out_get_latency(const struct audio_stream_out *stream)
598{
jiabin8b265c92020-10-21 15:16:40 -0700599 struct alsa_device_info *device_info = stream_get_first_alsa_device(
600 &((struct stream_out*)stream)->alsa_devices);
601 if (device_info == NULL) {
602 ALOGW("%s device info is null", __func__);
603 return 0;
604 }
605 return proxy_get_latency(&device_info->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700606}
607
Paul McLean30f41852014-04-16 15:44:20 -0700608static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700609{
610 return -ENOSYS;
611}
612
Paul McLeaneedc92e2013-12-19 15:46:15 -0800613/* must be called with hw device and output stream mutexes locked */
614static int start_output_stream(struct stream_out *out)
615{
jiabin8b265c92020-10-21 15:16:40 -0700616 int status = 0;
617 struct listnode *node;
618 list_for_each(node, &out->alsa_devices) {
619 struct alsa_device_info *device_info =
620 node_to_item(node, struct alsa_device_info, list_node);
621 ALOGV("start_output_stream(card:%d device:%d)",
622 device_info->profile.card, device_info->profile.device);
623 status = proxy_open(&device_info->proxy);
624 if (status != 0) {
625 ALOGE("%s failed to open device(card: %d device: %d)",
626 __func__, device_info->profile.card, device_info->profile.device);
627 goto exit;
628 }
629 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800630
jiabin8b265c92020-10-21 15:16:40 -0700631exit:
632 if (status != 0) {
633 list_for_each(node, &out->alsa_devices) {
634 struct alsa_device_info *device_info =
635 node_to_item(node, struct alsa_device_info, list_node);
636 proxy_close(&device_info->proxy);
637 }
638
639 }
640 return status;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800641}
642
643static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700644{
645 int ret;
646 struct stream_out *out = (struct stream_out *)stream;
647
Paul McLean994ac072016-06-02 15:33:24 -0600648 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700649 if (out->standby) {
650 ret = start_output_stream(out);
651 if (ret != 0) {
652 goto err;
653 }
654 out->standby = false;
655 }
Eric Laurent05333d42014-08-04 20:29:17 -0700656
jiabin8b265c92020-10-21 15:16:40 -0700657 struct listnode* node;
658 list_for_each(node, &out->alsa_devices) {
659 struct alsa_device_info* device_info =
660 node_to_item(node, struct alsa_device_info, list_node);
661 alsa_device_proxy* proxy = &device_info->proxy;
662 const void * write_buff = buffer;
663 int num_write_buff_bytes = bytes;
664 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
665 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
666 if (num_device_channels != num_req_channels) {
667 /* allocate buffer */
668 const size_t required_conversion_buffer_size =
669 bytes * num_device_channels / num_req_channels;
670 if (required_conversion_buffer_size > out->conversion_buffer_size) {
671 out->conversion_buffer_size = required_conversion_buffer_size;
672 out->conversion_buffer = realloc(out->conversion_buffer,
673 out->conversion_buffer_size);
674 }
675 /* convert data */
676 const audio_format_t audio_format = out_get_format(&(out->stream.common));
677 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
678 num_write_buff_bytes =
679 adjust_channels(write_buff, num_req_channels,
680 out->conversion_buffer, num_device_channels,
681 sample_size_in_bytes, num_write_buff_bytes);
682 write_buff = out->conversion_buffer;
Andy Hung03576be2014-07-21 21:16:45 -0700683 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800684
jiabin8b265c92020-10-21 15:16:40 -0700685 if (write_buff != NULL && num_write_buff_bytes != 0) {
686 proxy_write(proxy, write_buff, num_write_buff_bytes);
687 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800688 }
Simon Wilson19957a32012-04-06 16:17:12 -0700689
Paul McLean994ac072016-06-02 15:33:24 -0600690 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700691
692 return bytes;
693
694err:
Paul McLean994ac072016-06-02 15:33:24 -0600695 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700696 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700697 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700698 out_get_sample_rate(&stream->common));
699 }
700
701 return bytes;
702}
703
Paul McLean30f41852014-04-16 15:44:20 -0700704static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700705{
706 return -EINVAL;
707}
708
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700709static int out_get_presentation_position(const struct audio_stream_out *stream,
710 uint64_t *frames, struct timespec *timestamp)
711{
Andy Hungc9515ce2015-08-04 15:05:19 -0700712 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600713 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700714
jiabin8b265c92020-10-21 15:16:40 -0700715 const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
716 const int ret = device_info == NULL ? -ENODEV :
717 proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
Paul McLean994ac072016-06-02 15:33:24 -0600718 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700719 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700720}
721
Simon Wilson19957a32012-04-06 16:17:12 -0700722static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
723{
724 return 0;
725}
726
727static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
728{
729 return 0;
730}
731
Paul McLean30f41852014-04-16 15:44:20 -0700732static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700733{
734 return -EINVAL;
735}
736
Paul McLean76dba682016-06-01 12:29:11 -0600737static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700738 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600739 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700740 audio_output_flags_t flags,
741 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700742 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700743 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700744{
Paul McLean76dba682016-06-01 12:29:11 -0600745 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
746 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800747
Simon Wilson19957a32012-04-06 16:17:12 -0700748 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700749
750 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600751 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700752 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600753 }
Simon Wilson19957a32012-04-06 16:17:12 -0700754
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700755 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700756 out->stream.common.get_sample_rate = out_get_sample_rate;
757 out->stream.common.set_sample_rate = out_set_sample_rate;
758 out->stream.common.get_buffer_size = out_get_buffer_size;
759 out->stream.common.get_channels = out_get_channels;
760 out->stream.common.get_format = out_get_format;
761 out->stream.common.set_format = out_set_format;
762 out->stream.common.standby = out_standby;
763 out->stream.common.dump = out_dump;
764 out->stream.common.set_parameters = out_set_parameters;
765 out->stream.common.get_parameters = out_get_parameters;
766 out->stream.common.add_audio_effect = out_add_audio_effect;
767 out->stream.common.remove_audio_effect = out_remove_audio_effect;
768 out->stream.get_latency = out_get_latency;
769 out->stream.set_volume = out_set_volume;
770 out->stream.write = out_write;
771 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700772 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700773 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
774
jiabin400bbe02020-10-28 13:56:59 -0700775 out->handle = handle;
776
Paul McLean994ac072016-06-02 15:33:24 -0600777 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700778
Paul McLean76dba682016-06-01 12:29:11 -0600779 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700780
jiabin8b265c92020-10-21 15:16:40 -0700781 list_init(&out->alsa_devices);
782 struct alsa_device_info *device_info =
783 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
784 profile_init(&device_info->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700785
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700786 // build this to hand to the alsa_device_proxy
jiabin8b265c92020-10-21 15:16:40 -0700787 struct pcm_config proxy_config = {};
Paul McLeaneedc92e2013-12-19 15:46:15 -0800788
Paul McLean0f1753e2014-12-02 12:36:45 -0700789 /* Pull out the card/device pair */
jiabin8b265c92020-10-21 15:16:40 -0700790 parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700791
jiabin8b265c92020-10-21 15:16:40 -0700792 profile_read_device_info(&device_info->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700793
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700794 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800795
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700796 /* Rate */
797 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -0700798 proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
799 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700800 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800801 } else {
jiabin8b265c92020-10-21 15:16:40 -0700802 proxy_config.rate = config->sample_rate =
803 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700804 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800805 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800806
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600807 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -0700808 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600809 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600810 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600811
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700812 /* Format */
813 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin8b265c92020-10-21 15:16:40 -0700814 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700815 config->format = audio_format_from_pcm_format(proxy_config.format);
816 } else {
817 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -0700818 if (profile_is_format_valid(&device_info->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700819 proxy_config.format = fmt;
820 } else {
jiabin8b265c92020-10-21 15:16:40 -0700821 proxy_config.format = profile_get_default_format(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700822 config->format = audio_format_from_pcm_format(proxy_config.format);
823 ret = -EINVAL;
824 }
825 }
826
827 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600828 bool calc_mask = false;
829 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
830 /* query case */
jiabin8b265c92020-10-21 15:16:40 -0700831 out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -0600832 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700833 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600834 /* explicit case */
835 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700836 }
Paul McLean698dbd72015-11-03 12:24:30 -0800837
Paul McLean64345f82016-06-06 13:26:05 -0600838 /* The Framework is currently limited to no more than this number of channels */
839 if (out->hal_channel_count > FCC_8) {
840 out->hal_channel_count = FCC_8;
841 calc_mask = true;
842 }
843
844 if (calc_mask) {
845 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -0700846 * 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 -0600847 config->channel_mask = out->hal_channel_count <= FCC_2
jiabin8b265c92020-10-21 15:16:40 -0700848 /* position mask for mono and stereo*/
849 ? audio_channel_out_mask_from_count(out->hal_channel_count)
850 /* otherwise indexed */
851 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
Paul McLean64345f82016-06-06 13:26:05 -0600852 }
853
Andy Hung182ddc72015-05-05 23:37:30 -0700854 out->hal_channel_mask = config->channel_mask;
855
Paul McLean64345f82016-06-06 13:26:05 -0600856 // Validate the "logical" channel count against support in the "actual" profile.
857 // if they differ, choose the "actual" number of channels *closest* to the "logical".
858 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -0700859 proxy_config.channels =
jiabin8b265c92020-10-21 15:16:40 -0700860 profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
861 proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config);
jiabin400bbe02020-10-28 13:56:59 -0700862 out->config = proxy_config;
jiabin8b265c92020-10-21 15:16:40 -0700863
864 list_add_tail(&out->alsa_devices, &device_info->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700865
Paul McLean96c4d302017-12-05 09:50:26 -0800866 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
867 * So clear any errors that may have occurred above.
868 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700869 ret = 0;
870
Paul McLeaneedc92e2013-12-19 15:46:15 -0800871 out->conversion_buffer = NULL;
872 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700873
874 out->standby = true;
875
Paul McLean6a75e4e2016-05-25 14:09:02 -0600876 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600877 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600878
Simon Wilson19957a32012-04-06 16:17:12 -0700879 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700880
881 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700882}
883
Paul McLean76dba682016-06-01 12:29:11 -0600884static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700885 struct audio_stream_out *stream)
886{
887 struct stream_out *out = (struct stream_out *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -0600888
jiabin400bbe02020-10-28 13:56:59 -0700889 stream_lock(&out->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700890 /* Close the pcm device */
jiabin400bbe02020-10-28 13:56:59 -0700891 stream_standby_l(&out->alsa_devices, &out->standby);
892 stream_clear_devices(&out->alsa_devices);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800893
894 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700895
Paul McLeaneedc92e2013-12-19 15:46:15 -0800896 out->conversion_buffer = NULL;
897 out->conversion_buffer_size = 0;
898
Paul McLean994ac072016-06-02 15:33:24 -0600899 device_lock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700900 list_remove(&out->list_node);
Paul McLean76dba682016-06-01 12:29:11 -0600901 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600902 device_unlock(out->adev);
jiabin400bbe02020-10-28 13:56:59 -0700903 stream_unlock(&out->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -0600904
Simon Wilson19957a32012-04-06 16:17:12 -0700905 free(stream);
906}
907
Paul McLean76dba682016-06-01 12:29:11 -0600908static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700909 const struct audio_config *config)
910{
911 /* TODO This needs to be calculated based on format/channels/rate */
912 return 320;
913}
914
915/*
916 * IN functions
917 */
918static uint32_t in_get_sample_rate(const struct audio_stream *stream)
919{
jiabin8b265c92020-10-21 15:16:40 -0700920 struct alsa_device_info *device_info = stream_get_first_alsa_device(
921 &((const struct stream_in *)stream)->alsa_devices);
922 if (device_info == NULL) {
923 ALOGW("%s device info is null", __func__);
924 return 0;
925 }
926 uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700927 ALOGV("in_get_sample_rate() = %d", rate);
928 return rate;
929}
930
931static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
932{
933 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
934 return -ENOSYS;
935}
936
937static size_t in_get_buffer_size(const struct audio_stream *stream)
938{
939 const struct stream_in * in = ((const struct stream_in*)stream);
jiabin8b265c92020-10-21 15:16:40 -0700940 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
941 if (device_info == NULL) {
942 ALOGW("%s device info is null", __func__);
943 return 0;
944 }
945 return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700946}
947
948static uint32_t in_get_channels(const struct audio_stream *stream)
949{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700950 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700951 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700952}
953
954static audio_format_t in_get_format(const struct audio_stream *stream)
955{
jiabin8b265c92020-10-21 15:16:40 -0700956 struct alsa_device_info *device_info = stream_get_first_alsa_device(
957 &((const struct stream_in *)stream)->alsa_devices);
958 if (device_info == NULL) {
959 ALOGW("%s device info is null", __func__);
960 return AUDIO_FORMAT_DEFAULT;
961 }
962 alsa_device_proxy *proxy = &device_info->proxy;
Andy Hung780f1f82015-04-22 22:14:39 -0700963 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
964 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700965}
966
967static int in_set_format(struct audio_stream *stream, audio_format_t format)
968{
969 ALOGV("in_set_format(%d) - NOPE", format);
970
971 return -ENOSYS;
972}
973
974static int in_standby(struct audio_stream *stream)
975{
976 struct stream_in *in = (struct stream_in *)stream;
977
Paul McLean994ac072016-06-02 15:33:24 -0600978 stream_lock(&in->lock);
jiabin400bbe02020-10-28 13:56:59 -0700979 device_lock(in->adev);
980 stream_standby_l(&in->alsa_devices, &in->standby);
981 device_unlock(in->adev);
Paul McLean994ac072016-06-02 15:33:24 -0600982 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700983 return 0;
984}
985
986static int in_dump(const struct audio_stream *stream, int fd)
987{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600988 const struct stream_in* in_stream = (const struct stream_in*)stream;
989 if (in_stream != NULL) {
jiabin8b265c92020-10-21 15:16:40 -0700990 stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600991 }
992
993 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700994}
995
996static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
997{
Paul McLean65ec72b2015-02-18 09:13:24 -0800998 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700999
jiabin400bbe02020-10-28 13:56:59 -07001000 // The set parameters here only matters when the routing devices are changed.
1001 // When the device version higher than 3.0, the framework will use create_audio_patch
1002 // API instead of set_parameters to change audio routing.
1003 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001004}
1005
1006static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1007{
1008 struct stream_in *in = (struct stream_in *)stream;
1009
Paul McLean994ac072016-06-02 15:33:24 -06001010 stream_lock(&in->lock);
jiabin8b265c92020-10-21 15:16:40 -07001011 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1012 char *params_str = NULL;
1013 if (device_info != NULL) {
1014 params_str = device_get_parameters(&device_info->profile, keys);
1015 }
Paul McLean994ac072016-06-02 15:33:24 -06001016 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001017
1018 return params_str;
1019}
1020
1021static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1022{
1023 return 0;
1024}
1025
1026static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1027{
1028 return 0;
1029}
1030
1031static int in_set_gain(struct audio_stream_in *stream, float gain)
1032{
1033 return 0;
1034}
1035
1036/* must be called with hw device and output stream mutexes locked */
1037static int start_input_stream(struct stream_in *in)
1038{
jiabin8b265c92020-10-21 15:16:40 -07001039 // Only care about the first device as only one input device is allowed.
1040 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1041 if (device_info == NULL) {
1042 return -ENODEV;
1043 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001044
jiabin8b265c92020-10-21 15:16:40 -07001045 ALOGV("start_input_stream(card:%d device:%d)",
1046 device_info->profile.card, device_info->profile.device);
1047 return proxy_open(&device_info->proxy);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001048}
1049
1050/* TODO mutex stuff here (see out_write) */
1051static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1052{
1053 size_t num_read_buff_bytes = 0;
1054 void * read_buff = buffer;
1055 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001056 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001057
1058 struct stream_in * in = (struct stream_in *)stream;
1059
Paul McLean994ac072016-06-02 15:33:24 -06001060 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001061 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -07001062 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -07001063 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001064 goto err;
1065 }
1066 in->standby = false;
1067 }
1068
jiabin8b265c92020-10-21 15:16:40 -07001069 // Only care about the first device as only one input device is allowed.
1070 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1071 if (device_info == NULL) {
1072 return 0;
1073 }
1074
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001075 /*
1076 * OK, we need to figure out how much data to read to be able to output the requested
1077 * number of bytes in the HAL format (16-bit, stereo).
1078 */
1079 num_read_buff_bytes = bytes;
jiabin8b265c92020-10-21 15:16:40 -07001080 int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
Andy Hung780f1f82015-04-22 22:14:39 -07001081 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001082
1083 if (num_device_channels != num_req_channels) {
1084 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1085 }
1086
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001087 /* Setup/Realloc the conversion buffer (if necessary). */
1088 if (num_read_buff_bytes != bytes) {
1089 if (num_read_buff_bytes > in->conversion_buffer_size) {
1090 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1091 (and do these conversions themselves) */
1092 in->conversion_buffer_size = num_read_buff_bytes;
1093 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1094 }
1095 read_buff = in->conversion_buffer;
1096 }
1097
jiabin8b265c92020-10-21 15:16:40 -07001098 ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
Pavan Chikkala83b47a62015-01-09 12:21:13 -08001099 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001100 if (num_device_channels != num_req_channels) {
1101 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1102
1103 out_buff = buffer;
1104 /* Num Channels conversion */
1105 if (num_device_channels != num_req_channels) {
1106 audio_format_t audio_format = in_get_format(&(in->stream.common));
1107 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1108
1109 num_read_buff_bytes =
1110 adjust_channels(read_buff, num_device_channels,
1111 out_buff, num_req_channels,
1112 sample_size_in_bytes, num_read_buff_bytes);
1113 }
1114 }
Eric Laurent253def92014-09-14 12:18:18 -07001115
Paul McLean76dba682016-06-01 12:29:11 -06001116 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1117 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -07001118 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +05301119 } else {
Eric Laurente6499422015-01-09 17:03:27 -08001120 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001121 }
1122
1123err:
Paul McLean994ac072016-06-02 15:33:24 -06001124 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001125 return num_read_buff_bytes;
1126}
1127
1128static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1129{
1130 return 0;
1131}
1132
Andy Hungfa6b4a62018-06-04 19:14:22 -07001133static int in_get_capture_position(const struct audio_stream_in *stream,
1134 int64_t *frames, int64_t *time)
1135{
1136 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1137 stream_lock(&in->lock);
1138
jiabin8b265c92020-10-21 15:16:40 -07001139 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1140
1141 const int ret = device_info == NULL ? -ENODEV
1142 : proxy_get_capture_position(&device_info->proxy, frames, time);
Andy Hungfa6b4a62018-06-04 19:14:22 -07001143
1144 stream_unlock(&in->lock);
1145 return ret;
1146}
1147
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001148static int in_get_active_microphones(const struct audio_stream_in *stream,
1149 struct audio_microphone_characteristic_t *mic_array,
1150 size_t *mic_count) {
1151 (void)stream;
1152 (void)mic_array;
1153 (void)mic_count;
1154
1155 return -ENOSYS;
1156}
1157
1158static int in_set_microphone_direction(const struct audio_stream_in *stream,
1159 audio_microphone_direction_t dir) {
1160 (void)stream;
1161 (void)dir;
1162 ALOGV("---- in_set_microphone_direction()");
1163 return -ENOSYS;
1164}
1165
1166static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1167 (void)zoom;
1168 ALOGV("---- in_set_microphone_field_dimension()");
1169 return -ENOSYS;
1170}
1171
Paul McLean76dba682016-06-01 12:29:11 -06001172static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001173 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -06001174 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001175 struct audio_config *config,
1176 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -07001177 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -07001178 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -07001179 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001180{
Paul McLean1d585cc2016-05-24 11:28:10 -06001181 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001182 config->sample_rate, config->channel_mask, config->format);
1183
Andy Hungb10ce6d2017-10-27 19:37:58 -07001184 /* Pull out the card/device pair */
1185 int32_t card, device;
1186 if (!parse_card_device_params(address, &card, &device)) {
1187 ALOGW("%s fail - invalid address %s", __func__, address);
1188 *stream_in = NULL;
1189 return -EINVAL;
1190 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001191
Andy Hungb10ce6d2017-10-27 19:37:58 -07001192 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -06001193 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001194 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001195 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -06001196 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001197
1198 /* setup function pointers */
1199 in->stream.common.get_sample_rate = in_get_sample_rate;
1200 in->stream.common.set_sample_rate = in_set_sample_rate;
1201 in->stream.common.get_buffer_size = in_get_buffer_size;
1202 in->stream.common.get_channels = in_get_channels;
1203 in->stream.common.get_format = in_get_format;
1204 in->stream.common.set_format = in_set_format;
1205 in->stream.common.standby = in_standby;
1206 in->stream.common.dump = in_dump;
1207 in->stream.common.set_parameters = in_set_parameters;
1208 in->stream.common.get_parameters = in_get_parameters;
1209 in->stream.common.add_audio_effect = in_add_audio_effect;
1210 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1211
1212 in->stream.set_gain = in_set_gain;
1213 in->stream.read = in_read;
1214 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -07001215 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001216
Paul McLeanfa3ae3e2018-12-12 09:57:02 -08001217 in->stream.get_active_microphones = in_get_active_microphones;
1218 in->stream.set_microphone_direction = in_set_microphone_direction;
1219 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1220
jiabin400bbe02020-10-28 13:56:59 -07001221 in->handle = handle;
1222
Paul McLean994ac072016-06-02 15:33:24 -06001223 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -07001224
Paul McLean76dba682016-06-01 12:29:11 -06001225 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001226
jiabin8b265c92020-10-21 15:16:40 -07001227 list_init(&in->alsa_devices);
1228 struct alsa_device_info *device_info =
1229 (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1230 profile_init(&device_info->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001231
jiabin400bbe02020-10-28 13:56:59 -07001232 memset(&in->config, 0, sizeof(in->config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001233
Andy Hungb10ce6d2017-10-27 19:37:58 -07001234 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001235 device_lock(in->adev);
1236 int num_open_inputs = in->adev->inputs_open;
1237 device_unlock(in->adev);
1238
Andy Hungb10ce6d2017-10-27 19:37:58 -07001239 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001240 if (num_open_inputs > 0) {
jiabin8b265c92020-10-21 15:16:40 -07001241 if (!profile_is_cached_for(&device_info->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001242 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1243 __func__, card, device);
1244 ret = -EINVAL;
1245 }
1246 } else {
1247 /* Read input profile only if necessary */
jiabin8b265c92020-10-21 15:16:40 -07001248 device_info->profile.card = card;
1249 device_info->profile.device = device;
1250 if (!profile_read_device_info(&device_info->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001251 ALOGW("%s fail - cannot read profile", __func__);
1252 ret = -EINVAL;
1253 }
1254 }
1255 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001256 free(in);
1257 *stream_in = NULL;
1258 return ret;
1259 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001260
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001261 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001262 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001263 if (config->sample_rate == 0) {
jiabin8b265c92020-10-21 15:16:40 -07001264 config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001265 }
1266
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001267 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001268 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001269 if (config->sample_rate != in->adev->device_sample_rate) {
jiabin8b265c92020-10-21 15:16:40 -07001270 unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001271 if (highest_rate == 0) {
1272 ret = -EINVAL; /* error with device */
1273 } else {
jiabin400bbe02020-10-28 13:56:59 -07001274 in->config.rate = config->sample_rate =
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001275 min(highest_rate, in->adev->device_sample_rate);
jiabin400bbe02020-10-28 13:56:59 -07001276 if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001277 /* Changing the requested rate */
1278 ret = -EINVAL;
1279 } else {
1280 /* Everything AOK! */
1281 ret = 0;
1282 }
1283 }
1284 }
jiabin8b265c92020-10-21 15:16:40 -07001285 } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
jiabin400bbe02020-10-28 13:56:59 -07001286 in->config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001287 } else {
jiabin400bbe02020-10-28 13:56:59 -07001288 in->config.rate = config->sample_rate =
jiabin8b265c92020-10-21 15:16:40 -07001289 profile_get_default_sample_rate(&device_info->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001290 ret = -EINVAL;
1291 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001292
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001293 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001294 if (config->format == AUDIO_FORMAT_DEFAULT) {
jiabin400bbe02020-10-28 13:56:59 -07001295 in->config.format = profile_get_default_format(&device_info->profile);
1296 config->format = audio_format_from_pcm_format(in->config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001297 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001298 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
jiabin8b265c92020-10-21 15:16:40 -07001299 if (profile_is_format_valid(&device_info->profile, fmt)) {
jiabin400bbe02020-10-28 13:56:59 -07001300 in->config.format = fmt;
Andy Hung780f1f82015-04-22 22:14:39 -07001301 } else {
jiabin400bbe02020-10-28 13:56:59 -07001302 in->config.format = profile_get_default_format(&device_info->profile);
1303 config->format = audio_format_from_pcm_format(in->config.format);
Andy Hung780f1f82015-04-22 22:14:39 -07001304 ret = -EINVAL;
1305 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001306 }
1307
Paul McLean2cfd81b2014-09-15 12:32:23 -07001308 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001309 bool calc_mask = false;
1310 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1311 /* query case */
jiabin8b265c92020-10-21 15:16:40 -07001312 in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001313 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001314 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001315 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001316 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1317 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001318
Paul McLean64345f82016-06-06 13:26:05 -06001319 /* The Framework is currently limited to no more than this number of channels */
1320 if (in->hal_channel_count > FCC_8) {
1321 in->hal_channel_count = FCC_8;
1322 calc_mask = true;
1323 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001324
Paul McLean64345f82016-06-06 13:26:05 -06001325 if (calc_mask) {
1326 /* need to calculate the mask from channel count either because this is the query case
jiabin8b265c92020-10-21 15:16:40 -07001327 * 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 -06001328 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1329 /* position mask for mono & stereo */
1330 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1331 /* otherwise indexed */
1332 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001333
Paul McLean64345f82016-06-06 13:26:05 -06001334 // if we change the mask...
1335 if (in->hal_channel_mask != config->channel_mask &&
1336 config->channel_mask != AUDIO_CHANNEL_NONE) {
1337 config->channel_mask = in->hal_channel_mask;
1338 ret = -EINVAL;
1339 }
1340 } else {
1341 in->hal_channel_mask = config->channel_mask;
1342 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001343
Paul McLean64345f82016-06-06 13:26:05 -06001344 if (ret == 0) {
1345 // Validate the "logical" channel count against support in the "actual" profile.
1346 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1347 // and store THAT in proxy_config.channels
jiabin400bbe02020-10-28 13:56:59 -07001348 in->config.channels =
jiabin8b265c92020-10-21 15:16:40 -07001349 profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
jiabin400bbe02020-10-28 13:56:59 -07001350 ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config);
Eric Laurent981f7742017-05-03 12:44:26 -07001351 if (ret == 0) {
1352 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001353
Eric Laurent981f7742017-05-03 12:44:26 -07001354 in->conversion_buffer = NULL;
1355 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001356
Eric Laurent981f7742017-05-03 12:44:26 -07001357 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001358
Eric Laurent981f7742017-05-03 12:44:26 -07001359 /* Save this for adev_dump() */
1360 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1361 } else {
1362 ALOGW("proxy_prepare error %d", ret);
jiabin8b265c92020-10-21 15:16:40 -07001363 unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001364 config->channel_mask = channel_count <= FCC_2
1365 ? audio_channel_in_mask_from_count(channel_count)
1366 : audio_channel_mask_for_index_assignment_from_count(channel_count);
jiabin8b265c92020-10-21 15:16:40 -07001367 config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1368 config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
Eric Laurent981f7742017-05-03 12:44:26 -07001369 }
1370 }
Paul McLean64345f82016-06-06 13:26:05 -06001371
Eric Laurent981f7742017-05-03 12:44:26 -07001372 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001373 // Deallocate this stream on error, because AudioFlinger won't call
1374 // adev_close_input_stream() in this case.
1375 *stream_in = NULL;
1376 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001377 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001378 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001379
jiabin8b265c92020-10-21 15:16:40 -07001380 list_add_tail(&in->alsa_devices, &device_info->list_node);
1381
Andy Hungb10ce6d2017-10-27 19:37:58 -07001382 device_lock(in->adev);
1383 ++in->adev->inputs_open;
1384 device_unlock(in->adev);
1385
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001386 return ret;
1387}
1388
Paul McLean76dba682016-06-01 12:29:11 -06001389static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1390 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001391{
1392 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001393
jiabin400bbe02020-10-28 13:56:59 -07001394 stream_lock(&in->lock);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001395 device_lock(in->adev);
jiabin400bbe02020-10-28 13:56:59 -07001396 list_remove(&in->list_node);
Andy Hungb10ce6d2017-10-27 19:37:58 -07001397 --in->adev->inputs_open;
jiabin8b265c92020-10-21 15:16:40 -07001398 struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1399 if (device_info != NULL) {
1400 ALOGV("adev_close_input_stream(c:%d d:%d)",
1401 device_info->profile.card, device_info->profile.device);
1402 }
Andy Hungb10ce6d2017-10-27 19:37:58 -07001403 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1404 "invalid inputs_open: %d", in->adev->inputs_open);
jiabin400bbe02020-10-28 13:56:59 -07001405
1406 stream_standby_l(&in->alsa_devices, &in->standby);
1407
Andy Hungb10ce6d2017-10-27 19:37:58 -07001408 device_unlock(in->adev);
1409
jiabin400bbe02020-10-28 13:56:59 -07001410 stream_clear_devices(&in->alsa_devices);
1411 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001412
1413 free(in->conversion_buffer);
1414
1415 free(stream);
1416}
1417
1418/*
1419 * ADEV Functions
1420 */
Paul McLean76dba682016-06-01 12:29:11 -06001421static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001422{
1423 return 0;
1424}
1425
Paul McLean76dba682016-06-01 12:29:11 -06001426static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001427{
1428 return strdup("");
1429}
1430
Paul McLean76dba682016-06-01 12:29:11 -06001431static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001432{
1433 return 0;
1434}
1435
Paul McLean76dba682016-06-01 12:29:11 -06001436static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001437{
1438 return -ENOSYS;
1439}
1440
Paul McLean76dba682016-06-01 12:29:11 -06001441static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001442{
1443 return -ENOSYS;
1444}
1445
Paul McLean76dba682016-06-01 12:29:11 -06001446static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001447{
1448 return 0;
1449}
1450
Paul McLean76dba682016-06-01 12:29:11 -06001451static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001452{
Paul McLean76dba682016-06-01 12:29:11 -06001453 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001454 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001455 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001456 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001457 return -ENOSYS;
1458}
1459
Paul McLean76dba682016-06-01 12:29:11 -06001460static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001461{
1462 return -ENOSYS;
1463}
1464
jiabin400bbe02020-10-28 13:56:59 -07001465static int adev_create_audio_patch(struct audio_hw_device *dev,
1466 unsigned int num_sources,
1467 const struct audio_port_config *sources,
1468 unsigned int num_sinks,
1469 const struct audio_port_config *sinks,
1470 audio_patch_handle_t *handle) {
1471 if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1472 // Only accept mix->device and device->mix cases. In that case, the number of sources
1473 // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1474 return -EINVAL;
1475 }
1476
1477 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1478 // If source is a device, the number of sinks should be 1.
1479 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1480 return -EINVAL;
1481 }
1482 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1483 // If source is a mix, all sinks should be device.
1484 for (unsigned int i = 0; i < num_sinks; i++) {
1485 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1486 ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1487 return -EINVAL;
1488 }
1489 }
1490 } else {
1491 // All other cases are invalid.
1492 return -EINVAL;
1493 }
1494
1495 struct audio_device* adev = (struct audio_device*) dev;
1496 bool generatedPatchHandle = false;
1497 device_lock(adev);
1498 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1499 *handle = ++adev->next_patch_handle;
1500 generatedPatchHandle = true;
1501 }
1502
1503 int cards[AUDIO_PATCH_PORTS_MAX];
1504 int devices[AUDIO_PATCH_PORTS_MAX];
1505 const struct audio_port_config *port_configs =
1506 sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1507 int num_configs = 0;
1508 audio_io_handle_t io_handle = 0;
1509 bool wasStandby = true;
1510 int direction = PCM_OUT;
1511 audio_patch_handle_t *patch_handle = NULL;
1512 struct listnode *alsa_devices = NULL;
1513 struct stream_lock *lock = NULL;
1514 struct pcm_config *config = NULL;
1515 struct stream_in *in = NULL;
1516 struct stream_out *out = NULL;
1517
1518 unsigned int num_saved_devices = 0;
1519 int saved_cards[AUDIO_PATCH_PORTS_MAX];
1520 int saved_devices[AUDIO_PATCH_PORTS_MAX];
1521
1522 struct listnode *node;
1523
1524 // Only handle patches for mix->devices and device->mix case.
1525 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1526 in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1527 if (in == NULL) {
1528 ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1529 device_unlock(adev);
1530 return -EINVAL;
1531 }
1532
1533 direction = PCM_IN;
1534 wasStandby = in->standby;
1535 io_handle = in->handle;
1536 num_configs = num_sources;
1537 patch_handle = &in->patch_handle;
1538 alsa_devices = &in->alsa_devices;
1539 lock = &in->lock;
1540 config = &in->config;
1541 } else {
1542 out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1543 if (out == NULL) {
1544 ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1545 device_unlock(adev);
1546 return -EINVAL;
1547 }
1548
1549 direction = PCM_OUT;
1550 wasStandby = out->standby;
1551 io_handle = out->handle;
1552 num_configs = num_sinks;
1553 patch_handle = &out->patch_handle;
1554 alsa_devices = &out->alsa_devices;
1555 lock = &out->lock;
1556 config = &out->config;
1557 }
1558
1559 // Check if the patch handle match the recorded one if a valid patch handle is passed.
1560 if (!generatedPatchHandle && *patch_handle != *handle) {
1561 ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1562 "with handle(%d) when creating audio patch",
1563 __func__, *handle, *patch_handle, io_handle);
1564 device_unlock(adev);
1565 return -EINVAL;
1566 }
1567 device_unlock(adev);
1568
1569 for (unsigned int i = 0; i < num_configs; ++i) {
1570 if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1571 ALOGE("%s, failed to parse card and device %s",
1572 __func__, port_configs[i].ext.device.address);
1573 return -EINVAL;
1574 }
1575 }
1576
1577 stream_lock(lock);
1578 list_for_each (node, alsa_devices) {
1579 struct alsa_device_info *device_info =
1580 node_to_item(node, struct alsa_device_info, list_node);
1581 saved_cards[num_saved_devices] = device_info->profile.card;
1582 saved_devices[num_saved_devices++] = device_info->profile.device;
1583 }
1584
1585 device_lock(adev);
1586 stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1587 device_unlock(adev);
1588
1589 int ret = stream_set_new_devices(config, alsa_devices, num_configs, cards, devices, direction);
1590
1591 if (ret != 0) {
1592 *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1593 stream_set_new_devices(
1594 config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction);
1595 } else {
1596 *patch_handle = *handle;
1597 }
1598 if (!wasStandby) {
1599 device_lock(adev);
1600 if (in != NULL) {
1601 start_input_stream(in);
1602 }
1603 if (out != NULL) {
1604 start_output_stream(out);
1605 }
1606 device_unlock(adev);
1607 }
1608 stream_unlock(lock);
1609 return ret;
1610}
1611
1612static int adev_release_audio_patch(struct audio_hw_device *dev,
1613 audio_patch_handle_t patch_handle)
1614{
1615 struct audio_device* adev = (struct audio_device*) dev;
1616
1617 device_lock(adev);
1618 struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1619 device_unlock(adev);
1620 if (out != NULL) {
1621 stream_lock(&out->lock);
1622 device_lock(adev);
1623 stream_standby_l(&out->alsa_devices, &out->standby);
1624 device_unlock(adev);
1625 out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1626 stream_unlock(&out->lock);
1627 return 0;
1628 }
1629
1630 device_lock(adev);
1631 struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1632 device_unlock(adev);
1633 if (in != NULL) {
1634 stream_lock(&in->lock);
1635 device_lock(adev);
1636 stream_standby_l(&in->alsa_devices, &in->standby);
1637 device_unlock(adev);
1638 in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1639 stream_unlock(&in->lock);
1640 return 0;
1641 }
1642
1643 ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1644 return -EINVAL;
1645}
1646
1647static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1648{
1649 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1650 return -EINVAL;
1651 }
1652
1653 alsa_device_profile profile;
1654 const bool is_output = audio_is_output_device(port->ext.device.type);
1655 profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1656 if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1657 return -EINVAL;
1658 }
1659
1660 if (!profile_read_device_info(&profile)) {
1661 return -ENOENT;
1662 }
1663
1664 port->num_formats = 0;;
1665 for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1666 profile.formats[i] != 0; ++i) {
1667 audio_format_t format = audio_format_from(profile.formats[i]);
1668 if (format != AUDIO_FORMAT_INVALID) {
1669 port->formats[port->num_formats++] = format;
1670 }
1671 }
1672
1673 port->num_sample_rates = min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES);
1674 memcpy(port->sample_rates, profile.sample_rates, port->num_sample_rates * sizeof(unsigned int));
1675
1676 port->num_channel_masks = 0;
1677 const audio_channel_mask_t* channel_masks_map =
1678 is_output ? OUT_CHANNEL_MASKS_MAP : IN_CHANNEL_MASKS_MAP;
1679 const int channel_masks_size = is_output ? OUT_CHANNEL_MASKS_SIZE : IN_CHANNEL_MASKS_SIZE;
1680 unsigned int channel_count = 0;
1681 for (size_t i = 0; i < min(channel_masks_size, AUDIO_PORT_MAX_CHANNEL_MASKS) &&
1682 (channel_count = profile.channel_counts[i]) != 0 &&
1683 port->num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS; ++i) {
1684 if (channel_count < channel_masks_size &&
1685 channel_masks_map[channel_count] != AUDIO_CHANNEL_NONE) {
1686 port->channel_masks[port->num_channel_masks++] = channel_masks_map[channel_count];
1687 if (port->num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
1688 break;
1689 }
1690 }
1691 if (channel_count < CHANNEL_INDEX_MASKS_SIZE &&
1692 CHANNEL_INDEX_MASKS_MAP[channel_count] != AUDIO_CHANNEL_NONE) {
1693 port->channel_masks[port->num_channel_masks++] = CHANNEL_INDEX_MASKS_MAP[channel_count];
1694 }
1695 }
1696 return 0;
1697}
1698
Paul McLean76dba682016-06-01 12:29:11 -06001699static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001700{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001701 dprintf(fd, "\nUSB audio module:\n");
1702
1703 struct audio_device* adev = (struct audio_device*)device;
1704 const int kNumRetries = 3;
1705 const int kSleepTimeMS = 500;
1706
Paul McLean994ac072016-06-02 15:33:24 -06001707 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001708 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001709 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001710 sleep(kSleepTimeMS);
1711 retry--;
1712 }
1713
1714 if (retry > 0) {
1715 if (list_empty(&adev->output_stream_list)) {
1716 dprintf(fd, " No output streams.\n");
1717 } else {
1718 struct listnode* node;
1719 list_for_each(node, &adev->output_stream_list) {
1720 struct audio_stream* stream =
1721 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1722 out_dump(stream, fd);
1723 }
1724 }
1725
1726 if (list_empty(&adev->input_stream_list)) {
1727 dprintf(fd, "\n No input streams.\n");
1728 } else {
1729 struct listnode* node;
1730 list_for_each(node, &adev->input_stream_list) {
1731 struct audio_stream* stream =
1732 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1733 in_dump(stream, fd);
1734 }
1735 }
1736
Paul McLean994ac072016-06-02 15:33:24 -06001737 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001738 } else {
1739 // Couldn't lock
1740 dprintf(fd, " Could not obtain device lock.\n");
1741 }
1742
Simon Wilson19957a32012-04-06 16:17:12 -07001743 return 0;
1744}
1745
1746static int adev_close(hw_device_t *device)
1747{
Simon Wilson19957a32012-04-06 16:17:12 -07001748 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001749
Simon Wilson19957a32012-04-06 16:17:12 -07001750 return 0;
1751}
1752
Paul McLean30f41852014-04-16 15:44:20 -07001753static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001754{
Simon Wilson19957a32012-04-06 16:17:12 -07001755 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1756 return -EINVAL;
1757
Paul McLeaneedc92e2013-12-19 15:46:15 -08001758 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001759 if (!adev)
1760 return -ENOMEM;
1761
Paul McLeancf5310a2018-08-22 14:33:12 -07001762 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001763
Paul McLean6a75e4e2016-05-25 14:09:02 -06001764 list_init(&adev->output_stream_list);
1765 list_init(&adev->input_stream_list);
1766
Simon Wilson19957a32012-04-06 16:17:12 -07001767 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
jiabin400bbe02020-10-28 13:56:59 -07001768 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001769 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001770 adev->hw_device.common.close = adev_close;
1771
Simon Wilson19957a32012-04-06 16:17:12 -07001772 adev->hw_device.init_check = adev_init_check;
1773 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1774 adev->hw_device.set_master_volume = adev_set_master_volume;
1775 adev->hw_device.set_mode = adev_set_mode;
1776 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1777 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1778 adev->hw_device.set_parameters = adev_set_parameters;
1779 adev->hw_device.get_parameters = adev_get_parameters;
1780 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1781 adev->hw_device.open_output_stream = adev_open_output_stream;
1782 adev->hw_device.close_output_stream = adev_close_output_stream;
1783 adev->hw_device.open_input_stream = adev_open_input_stream;
1784 adev->hw_device.close_input_stream = adev_close_input_stream;
jiabin400bbe02020-10-28 13:56:59 -07001785 adev->hw_device.create_audio_patch = adev_create_audio_patch;
1786 adev->hw_device.release_audio_patch = adev_release_audio_patch;
1787 adev->hw_device.get_audio_port = adev_get_audio_port;
Simon Wilson19957a32012-04-06 16:17:12 -07001788 adev->hw_device.dump = adev_dump;
1789
1790 *device = &adev->hw_device.common;
1791
1792 return 0;
1793}
1794
1795static struct hw_module_methods_t hal_module_methods = {
1796 .open = adev_open,
1797};
1798
1799struct audio_module HAL_MODULE_INFO_SYM = {
1800 .common = {
1801 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001802 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1803 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001804 .id = AUDIO_HARDWARE_MODULE_ID,
1805 .name = "USB audio HW HAL",
1806 .author = "The Android Open Source Project",
1807 .methods = &hal_module_methods,
1808 },
1809};