blob: a19a0ae3993203e8d7adeb0a2339fd98274f8b2e [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>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070025#include <sys/time.h>
Tri Vo00a86732017-06-27 11:33:36 -070026#include <unistd.h>
Simon Wilson19957a32012-04-06 16:17:12 -070027
Mark Salyzyn88e458a2014-04-28 12:30:44 -070028#include <log/log.h>
Paul McLean6a75e4e2016-05-25 14:09:02 -060029#include <cutils/list.h>
Simon Wilson19957a32012-04-06 16:17:12 -070030#include <cutils/str_parms.h>
31#include <cutils/properties.h>
32
Simon Wilson19957a32012-04-06 16:17:12 -070033#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070034#include <hardware/audio_alsaops.h>
35#include <hardware/hardware.h>
36
37#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070038
39#include <tinyalsa/asoundlib.h>
40
Paul McLeanc2201152014-07-16 13:46:07 -070041#include <audio_utils/channels.h>
42
Paul McLeanc88e6ae2014-07-16 09:48:34 -070043#include "alsa_device_profile.h"
44#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080045#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070046
Paul McLean1d585cc2016-05-24 11:28:10 -060047/* Lock play & record samples rates at or above this threshold */
48#define RATELOCK_THRESHOLD 96000
49
Paul McLeancfdbd6b2018-07-27 11:16:02 -060050#define max(a, b) ((a) > (b) ? (a) : (b))
51#define min(a, b) ((a) < (b) ? (a) : (b))
52
Simon Wilson19957a32012-04-06 16:17:12 -070053struct audio_device {
54 struct audio_hw_device hw_device;
55
56 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080057
58 /* output */
Paul McLean6a75e4e2016-05-25 14:09:02 -060059 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080060
61 /* input */
Paul McLean6a75e4e2016-05-25 14:09:02 -060062 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080063
Paul McLean1d585cc2016-05-24 11:28:10 -060064 /* lock input & output sample rates */
65 /*FIXME - How do we address multiple output streams? */
Paul McLeancfdbd6b2018-07-27 11:16:02 -060066 uint32_t device_sample_rate; // this should be a rate that is common to both input & output
Paul McLean1d585cc2016-05-24 11:28:10 -060067
Eric Laurent253def92014-09-14 12:18:18 -070068 bool mic_muted;
69
Andy Hungb10ce6d2017-10-27 19:37:58 -070070 int32_t inputs_open; /* number of input streams currently open. */
Simon Wilson19957a32012-04-06 16:17:12 -070071};
72
Paul McLean994ac072016-06-02 15:33:24 -060073struct stream_lock {
74 pthread_mutex_t lock; /* see note below on mutex acquisition order */
75 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
76};
77
Simon Wilson19957a32012-04-06 16:17:12 -070078struct stream_out {
79 struct audio_stream_out stream;
80
Paul McLeancf5310a2018-08-22 14:33:12 -070081 struct stream_lock lock;
Paul McLean994ac072016-06-02 15:33:24 -060082
Paul McLeaneedc92e2013-12-19 15:46:15 -080083 bool standby;
84
Paul McLean76dba682016-06-01 12:29:11 -060085 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070086
Paul McLeancf5310a2018-08-22 14:33:12 -070087 alsa_device_profile profile; /* The profile of the ALSA device connected to the stream.
Andy Hung4e6a1c02017-10-27 20:31:46 -070088 */
89
Paul McLeanc88e6ae2014-07-16 09:48:34 -070090 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080091
Andy Hung03576be2014-07-21 21:16:45 -070092 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
93 * This may differ from the device channel count when
94 * the device is not compatible with AudioFlinger
95 * capabilities, e.g. exposes too many channels or
96 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -060097 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
98 * so the proxy doesn't have a channel_mask, but
99 * audio HALs need to talk about channel masks
100 * so expose the one calculated by
101 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700102
Paul McLean6a75e4e2016-05-25 14:09:02 -0600103 struct listnode list_node;
104
Paul McLeaneedc92e2013-12-19 15:46:15 -0800105 void * conversion_buffer; /* any conversions are put into here
106 * they could come from here too if
107 * there was a previous conversion */
108 size_t conversion_buffer_size; /* in bytes */
109};
110
Paul McLeaneedc92e2013-12-19 15:46:15 -0800111struct stream_in {
112 struct audio_stream_in stream;
113
Paul McLean994ac072016-06-02 15:33:24 -0600114 struct stream_lock lock;
115
Simon Wilson19957a32012-04-06 16:17:12 -0700116 bool standby;
117
Paul McLean76dba682016-06-01 12:29:11 -0600118 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800119
Paul McLeancf5310a2018-08-22 14:33:12 -0700120 alsa_device_profile profile; /* The profile of the ALSA device connected to the stream.
Andy Hung4e6a1c02017-10-27 20:31:46 -0700121 */
122
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700123 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700124
Paul McLean2cfd81b2014-09-15 12:32:23 -0700125 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
126 * This may differ from the device channel count when
127 * the device is not compatible with AudioFlinger
128 * capabilities, e.g. exposes too many channels or
129 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600130 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
131 * so the proxy doesn't have a channel_mask, but
132 * audio HALs need to talk about channel masks
133 * so expose the one calculated by
134 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700135
Paul McLean6a75e4e2016-05-25 14:09:02 -0600136 struct listnode list_node;
137
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700138 /* 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 -0700139 void * conversion_buffer; /* any conversions are put into here
140 * they could come from here too if
141 * there was a previous conversion */
142 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700143};
144
Paul McLean994ac072016-06-02 15:33:24 -0600145/*
146 * Locking Helpers
147 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800148/*
Eric Laurent70318092015-06-19 17:49:17 -0700149 * NOTE: when multiple mutexes have to be acquired, always take the
150 * stream_in or stream_out mutex first, followed by the audio_device mutex.
151 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
152 * higher priority playback or capture thread.
153 */
154
Paul McLean994ac072016-06-02 15:33:24 -0600155static void stream_lock_init(struct stream_lock *lock) {
156 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
157 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
158}
159
160static void stream_lock(struct stream_lock *lock) {
161 pthread_mutex_lock(&lock->pre_lock);
162 pthread_mutex_lock(&lock->lock);
163 pthread_mutex_unlock(&lock->pre_lock);
164}
165
166static void stream_unlock(struct stream_lock *lock) {
167 pthread_mutex_unlock(&lock->lock);
168}
169
170static void device_lock(struct audio_device *adev) {
171 pthread_mutex_lock(&adev->lock);
172}
173
174static int device_try_lock(struct audio_device *adev) {
175 return pthread_mutex_trylock(&adev->lock);
176}
177
178static void device_unlock(struct audio_device *adev) {
179 pthread_mutex_unlock(&adev->lock);
180}
181
182/*
183 * streams list management
184 */
185static void adev_add_stream_to_list(
186 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
187 device_lock(adev);
188
189 list_add_tail(list, stream_node);
190
191 device_unlock(adev);
192}
193
194static void adev_remove_stream_from_list(
195 struct audio_device* adev, struct listnode* stream_node) {
196 device_lock(adev);
197
198 list_remove(stream_node);
199
200 device_unlock(adev);
201}
202
Eric Laurent70318092015-06-19 17:49:17 -0700203/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700204 * Extract the card and device numbers from the supplied key/value pairs.
205 * kvpairs A null-terminated string containing the key/value pairs or card and device.
206 * i.e. "card=1;device=42"
207 * card A pointer to a variable to receive the parsed-out card number.
208 * device A pointer to a variable to receive the parsed-out device number.
209 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
210 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800211 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700212 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800213static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700214{
215 struct str_parms * parms = str_parms_create_str(kvpairs);
216 char value[32];
217 int param_val;
218
219 // initialize to "undefined" state.
220 *card = -1;
221 *device = -1;
222
223 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
224 if (param_val >= 0) {
225 *card = atoi(value);
226 }
227
228 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
229 if (param_val >= 0) {
230 *device = atoi(value);
231 }
232
233 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800234
235 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700236}
237
Andy Hung4e6a1c02017-10-27 20:31:46 -0700238static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800239{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700240 if (profile->card < 0 || profile->device < 0) {
241 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800242 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700243
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700244 struct str_parms *query = str_parms_create_str(keys);
245 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700246
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700247 /* These keys are from hardware/libhardware/include/audio.h */
248 /* supported sample rates */
249 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
250 char* rates_list = profile_get_sample_rate_strs(profile);
251 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
252 rates_list);
253 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700254 }
255
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700256 /* supported channel counts */
257 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
258 char* channels_list = profile_get_channel_count_strs(profile);
259 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
260 channels_list);
261 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700262 }
263
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700264 /* supported sample formats */
265 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
266 char * format_params = profile_get_format_strs(profile);
267 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
268 format_params);
269 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700270 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700271 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700272
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700273 char* result_str = str_parms_to_str(result);
274 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700275
Paul McLean65ec72b2015-02-18 09:13:24 -0800276 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700277
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700278 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800279}
280
281/*
282 * HAl Functions
283 */
Simon Wilson19957a32012-04-06 16:17:12 -0700284/**
285 * NOTE: when multiple mutexes have to be acquired, always respect the
286 * following order: hw device > out stream
287 */
288
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700289/*
290 * OUT functions
291 */
Simon Wilson19957a32012-04-06 16:17:12 -0700292static uint32_t out_get_sample_rate(const struct audio_stream *stream)
293{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700294 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
295 ALOGV("out_get_sample_rate() = %d", rate);
296 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700297}
298
299static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
300{
301 return 0;
302}
303
304static size_t out_get_buffer_size(const struct audio_stream *stream)
305{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700306 const struct stream_out* out = (const struct stream_out*)stream;
307 size_t buffer_size =
308 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700309 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700310}
311
312static uint32_t out_get_channels(const struct audio_stream *stream)
313{
Andy Hung03576be2014-07-21 21:16:45 -0700314 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700315 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700316}
317
318static audio_format_t out_get_format(const struct audio_stream *stream)
319{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700320 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
321 * Relies on the framework to provide data in the specified format.
322 * This could change in the future.
323 */
324 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
325 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700326 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700327}
328
329static int out_set_format(struct audio_stream *stream, audio_format_t format)
330{
331 return 0;
332}
333
334static int out_standby(struct audio_stream *stream)
335{
336 struct stream_out *out = (struct stream_out *)stream;
337
Paul McLean994ac072016-06-02 15:33:24 -0600338 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700339 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700340 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700341 out->standby = true;
342 }
Paul McLean994ac072016-06-02 15:33:24 -0600343 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700344 return 0;
345}
346
Paul McLean6a75e4e2016-05-25 14:09:02 -0600347static int out_dump(const struct audio_stream *stream, int fd) {
348 const struct stream_out* out_stream = (const struct stream_out*) stream;
349
350 if (out_stream != NULL) {
351 dprintf(fd, "Output Profile:\n");
Paul McLeancf5310a2018-08-22 14:33:12 -0700352 profile_dump(&out_stream->profile, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600353
354 dprintf(fd, "Output Proxy:\n");
355 proxy_dump(&out_stream->proxy, fd);
356 }
357
Simon Wilson19957a32012-04-06 16:17:12 -0700358 return 0;
359}
360
361static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
362{
Paul McLean65ec72b2015-02-18 09:13:24 -0800363 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800364
Simon Wilson19957a32012-04-06 16:17:12 -0700365 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700366
Paul McLeaneedc92e2013-12-19 15:46:15 -0800367 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700368 int card = -1;
369 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700370
Paul McLean65ec72b2015-02-18 09:13:24 -0800371 if (!parse_card_device_params(kvpairs, &card, &device)) {
372 // nothing to do
373 return ret_value;
374 }
375
Paul McLean994ac072016-06-02 15:33:24 -0600376 stream_lock(&out->lock);
Paul McLeancf5310a2018-08-22 14:33:12 -0700377 if (!profile_is_cached_for(&out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700378 /* cannot read pcm device info if playback is active */
379 if (!out->standby)
380 ret_value = -ENOSYS;
381 else {
Paul McLeancf5310a2018-08-22 14:33:12 -0700382 int saved_card = out->profile.card;
383 int saved_device = out->profile.device;
384 out->profile.card = card;
385 out->profile.device = device;
386 ret_value = profile_read_device_info(&out->profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700387 if (ret_value != 0) {
Paul McLeancf5310a2018-08-22 14:33:12 -0700388 out->profile.card = saved_card;
389 out->profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700390 }
391 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800392 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700393
Paul McLean994ac072016-06-02 15:33:24 -0600394 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700395
Paul McLeaneedc92e2013-12-19 15:46:15 -0800396 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700397}
398
Paul McLeanf62d75e2014-07-11 15:14:19 -0700399static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
400{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700401 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600402 stream_lock(&out->lock);
Paul McLeancf5310a2018-08-22 14:33:12 -0700403 char * params_str = device_get_parameters(&out->profile, keys);
Paul McLean994ac072016-06-02 15:33:24 -0600404 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700405 return params_str;
406}
407
Simon Wilson19957a32012-04-06 16:17:12 -0700408static uint32_t out_get_latency(const struct audio_stream_out *stream)
409{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700410 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
411 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700412}
413
Paul McLean30f41852014-04-16 15:44:20 -0700414static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700415{
416 return -ENOSYS;
417}
418
Paul McLeaneedc92e2013-12-19 15:46:15 -0800419/* must be called with hw device and output stream mutexes locked */
420static int start_output_stream(struct stream_out *out)
421{
Paul McLeancf5310a2018-08-22 14:33:12 -0700422 ALOGV("start_output_stream(card:%d device:%d)", out->profile.card, out->profile.device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800423
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700424 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800425}
426
427static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700428{
429 int ret;
430 struct stream_out *out = (struct stream_out *)stream;
431
Paul McLean994ac072016-06-02 15:33:24 -0600432 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700433 if (out->standby) {
434 ret = start_output_stream(out);
435 if (ret != 0) {
436 goto err;
437 }
438 out->standby = false;
439 }
Eric Laurent05333d42014-08-04 20:29:17 -0700440
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700441 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700442 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800443 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700444 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
445 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700446 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700447 /* allocate buffer */
448 const size_t required_conversion_buffer_size =
449 bytes * num_device_channels / num_req_channels;
450 if (required_conversion_buffer_size > out->conversion_buffer_size) {
451 out->conversion_buffer_size = required_conversion_buffer_size;
452 out->conversion_buffer = realloc(out->conversion_buffer,
453 out->conversion_buffer_size);
454 }
455 /* convert data */
456 const audio_format_t audio_format = out_get_format(&(out->stream.common));
457 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800458 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700459 adjust_channels(write_buff, num_req_channels,
460 out->conversion_buffer, num_device_channels,
461 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800462 write_buff = out->conversion_buffer;
463 }
464
Paul McLeaneedc92e2013-12-19 15:46:15 -0800465 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700466 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800467 }
Simon Wilson19957a32012-04-06 16:17:12 -0700468
Paul McLean994ac072016-06-02 15:33:24 -0600469 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700470
471 return bytes;
472
473err:
Paul McLean994ac072016-06-02 15:33:24 -0600474 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700475 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700476 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700477 out_get_sample_rate(&stream->common));
478 }
479
480 return bytes;
481}
482
Paul McLean30f41852014-04-16 15:44:20 -0700483static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700484{
485 return -EINVAL;
486}
487
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700488static int out_get_presentation_position(const struct audio_stream_out *stream,
489 uint64_t *frames, struct timespec *timestamp)
490{
Andy Hungc9515ce2015-08-04 15:05:19 -0700491 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600492 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700493
494 const alsa_device_proxy *proxy = &out->proxy;
495 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
496
Paul McLean994ac072016-06-02 15:33:24 -0600497 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700498 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700499}
500
Simon Wilson19957a32012-04-06 16:17:12 -0700501static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
502{
503 return 0;
504}
505
506static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
507{
508 return 0;
509}
510
Paul McLean30f41852014-04-16 15:44:20 -0700511static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700512{
513 return -EINVAL;
514}
515
Paul McLean76dba682016-06-01 12:29:11 -0600516static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700517 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600518 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700519 audio_output_flags_t flags,
520 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700521 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700522 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700523{
Paul McLean76dba682016-06-01 12:29:11 -0600524 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
525 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800526
Simon Wilson19957a32012-04-06 16:17:12 -0700527 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700528
529 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600530 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700531 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600532 }
Simon Wilson19957a32012-04-06 16:17:12 -0700533
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700534 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700535 out->stream.common.get_sample_rate = out_get_sample_rate;
536 out->stream.common.set_sample_rate = out_set_sample_rate;
537 out->stream.common.get_buffer_size = out_get_buffer_size;
538 out->stream.common.get_channels = out_get_channels;
539 out->stream.common.get_format = out_get_format;
540 out->stream.common.set_format = out_set_format;
541 out->stream.common.standby = out_standby;
542 out->stream.common.dump = out_dump;
543 out->stream.common.set_parameters = out_set_parameters;
544 out->stream.common.get_parameters = out_get_parameters;
545 out->stream.common.add_audio_effect = out_add_audio_effect;
546 out->stream.common.remove_audio_effect = out_remove_audio_effect;
547 out->stream.get_latency = out_get_latency;
548 out->stream.set_volume = out_set_volume;
549 out->stream.write = out_write;
550 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700551 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700552 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
553
Paul McLean994ac072016-06-02 15:33:24 -0600554 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700555
Paul McLean76dba682016-06-01 12:29:11 -0600556 out->adev = (struct audio_device *)hw_dev;
Paul McLeancf5310a2018-08-22 14:33:12 -0700557
558 profile_init(&out->profile, PCM_OUT);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700559
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700560 // build this to hand to the alsa_device_proxy
561 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700562 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800563
Paul McLean0f1753e2014-12-02 12:36:45 -0700564 /* Pull out the card/device pair */
Paul McLeancf5310a2018-08-22 14:33:12 -0700565 parse_card_device_params(address, &out->profile.card, &out->profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700566
Paul McLeancf5310a2018-08-22 14:33:12 -0700567 profile_read_device_info(&out->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700568
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700569 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800570
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700571 /* Rate */
572 if (config->sample_rate == 0) {
Paul McLeancf5310a2018-08-22 14:33:12 -0700573 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&out->profile);
574 } else if (profile_is_sample_rate_valid(&out->profile, config->sample_rate)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700575 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800576 } else {
Paul McLeancf5310a2018-08-22 14:33:12 -0700577 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&out->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700578 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800579 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800580
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600581 /* TODO: This is a problem if the input does not support this rate */
Paul McLeancf5310a2018-08-22 14:33:12 -0700582 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600583 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600584 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600585
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700586 /* Format */
587 if (config->format == AUDIO_FORMAT_DEFAULT) {
Paul McLeancf5310a2018-08-22 14:33:12 -0700588 proxy_config.format = profile_get_default_format(&out->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700589 config->format = audio_format_from_pcm_format(proxy_config.format);
590 } else {
591 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
Paul McLeancf5310a2018-08-22 14:33:12 -0700592 if (profile_is_format_valid(&out->profile, fmt)) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700593 proxy_config.format = fmt;
594 } else {
Paul McLeancf5310a2018-08-22 14:33:12 -0700595 proxy_config.format = profile_get_default_format(&out->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700596 config->format = audio_format_from_pcm_format(proxy_config.format);
597 ret = -EINVAL;
598 }
599 }
600
601 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600602 bool calc_mask = false;
603 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
604 /* query case */
Paul McLeancf5310a2018-08-22 14:33:12 -0700605 out->hal_channel_count = profile_get_default_channel_count(&out->profile);
Paul McLean64345f82016-06-06 13:26:05 -0600606 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700607 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600608 /* explicit case */
609 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700610 }
Paul McLean698dbd72015-11-03 12:24:30 -0800611
Paul McLean64345f82016-06-06 13:26:05 -0600612 /* The Framework is currently limited to no more than this number of channels */
613 if (out->hal_channel_count > FCC_8) {
614 out->hal_channel_count = FCC_8;
615 calc_mask = true;
616 }
617
618 if (calc_mask) {
619 /* need to calculate the mask from channel count either because this is the query case
620 * or the specified mask isn't valid for this device, or is more then the FW can handle */
621 config->channel_mask = out->hal_channel_count <= FCC_2
622 /* position mask for mono and stereo*/
623 ? audio_channel_out_mask_from_count(out->hal_channel_count)
624 /* otherwise indexed */
625 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
626 }
627
Andy Hung182ddc72015-05-05 23:37:30 -0700628 out->hal_channel_mask = config->channel_mask;
629
Paul McLean64345f82016-06-06 13:26:05 -0600630 // Validate the "logical" channel count against support in the "actual" profile.
631 // if they differ, choose the "actual" number of channels *closest* to the "logical".
632 // and store THAT in proxy_config.channels
Paul McLeancf5310a2018-08-22 14:33:12 -0700633 proxy_config.channels =
634 profile_get_closest_channel_count(&out->profile, out->hal_channel_count);
635 proxy_prepare(&out->proxy, &out->profile, &proxy_config);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700636
Paul McLean96c4d302017-12-05 09:50:26 -0800637 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
638 * So clear any errors that may have occurred above.
639 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700640 ret = 0;
641
Paul McLeaneedc92e2013-12-19 15:46:15 -0800642 out->conversion_buffer = NULL;
643 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700644
645 out->standby = true;
646
Paul McLean6a75e4e2016-05-25 14:09:02 -0600647 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600648 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600649
Simon Wilson19957a32012-04-06 16:17:12 -0700650 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700651
652 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700653}
654
Paul McLean76dba682016-06-01 12:29:11 -0600655static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700656 struct audio_stream_out *stream)
657{
658 struct stream_out *out = (struct stream_out *)stream;
Paul McLeancf5310a2018-08-22 14:33:12 -0700659 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile.card, out->profile.device);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600660
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700661 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700662 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800663
664 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700665
Paul McLeaneedc92e2013-12-19 15:46:15 -0800666 out->conversion_buffer = NULL;
667 out->conversion_buffer_size = 0;
668
Paul McLeancf5310a2018-08-22 14:33:12 -0700669 adev_remove_stream_from_list(out->adev, &out->list_node);
670
Paul McLean994ac072016-06-02 15:33:24 -0600671 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600672 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600673 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600674
Simon Wilson19957a32012-04-06 16:17:12 -0700675 free(stream);
676}
677
Paul McLean76dba682016-06-01 12:29:11 -0600678static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700679 const struct audio_config *config)
680{
681 /* TODO This needs to be calculated based on format/channels/rate */
682 return 320;
683}
684
685/*
686 * IN functions
687 */
688static uint32_t in_get_sample_rate(const struct audio_stream *stream)
689{
690 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
691 ALOGV("in_get_sample_rate() = %d", rate);
692 return rate;
693}
694
695static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
696{
697 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
698 return -ENOSYS;
699}
700
701static size_t in_get_buffer_size(const struct audio_stream *stream)
702{
703 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700704 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700705}
706
707static uint32_t in_get_channels(const struct audio_stream *stream)
708{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700709 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700710 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700711}
712
713static audio_format_t in_get_format(const struct audio_stream *stream)
714{
Andy Hung780f1f82015-04-22 22:14:39 -0700715 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
716 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
717 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700718}
719
720static int in_set_format(struct audio_stream *stream, audio_format_t format)
721{
722 ALOGV("in_set_format(%d) - NOPE", format);
723
724 return -ENOSYS;
725}
726
727static int in_standby(struct audio_stream *stream)
728{
729 struct stream_in *in = (struct stream_in *)stream;
730
Paul McLean994ac072016-06-02 15:33:24 -0600731 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700732 if (!in->standby) {
733 proxy_close(&in->proxy);
734 in->standby = true;
735 }
Paul McLean994ac072016-06-02 15:33:24 -0600736 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700737
738 return 0;
739}
740
741static int in_dump(const struct audio_stream *stream, int fd)
742{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600743 const struct stream_in* in_stream = (const struct stream_in*)stream;
744 if (in_stream != NULL) {
745 dprintf(fd, "Input Profile:\n");
Paul McLeancf5310a2018-08-22 14:33:12 -0700746 profile_dump(&in_stream->profile, fd);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600747
748 dprintf(fd, "Input Proxy:\n");
749 proxy_dump(&in_stream->proxy, fd);
750 }
751
752 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700753}
754
755static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
756{
Paul McLean65ec72b2015-02-18 09:13:24 -0800757 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700758
759 struct stream_in *in = (struct stream_in *)stream;
760
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700761 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700762 int card = -1;
763 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700764
Paul McLean65ec72b2015-02-18 09:13:24 -0800765 if (!parse_card_device_params(kvpairs, &card, &device)) {
766 // nothing to do
767 return ret_value;
768 }
769
Paul McLean994ac072016-06-02 15:33:24 -0600770 stream_lock(&in->lock);
771 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700772
Paul McLeancf5310a2018-08-22 14:33:12 -0700773 if (card >= 0 && device >= 0 && !profile_is_cached_for(&in->profile, card, device)) {
774 /* cannot read pcm device info if capture is active, or more than one open stream */
Andy Hungb10ce6d2017-10-27 19:37:58 -0700775 if (!in->standby || in->adev->inputs_open > 1)
Eric Laurent05333d42014-08-04 20:29:17 -0700776 ret_value = -ENOSYS;
777 else {
Paul McLeancf5310a2018-08-22 14:33:12 -0700778 int saved_card = in->profile.card;
779 int saved_device = in->profile.device;
780 in->profile.card = card;
781 in->profile.device = device;
782 ret_value = profile_read_device_info(&in->profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700783 if (ret_value != 0) {
Paul McLeancf5310a2018-08-22 14:33:12 -0700784 ALOGE("Can't read device profile. card:%d, device:%d", card, device);
785 in->profile.card = saved_card;
786 in->profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700787 }
788 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700789 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700790
Paul McLean994ac072016-06-02 15:33:24 -0600791 device_unlock(in->adev);
792 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700793
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700794 return ret_value;
795}
796
797static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
798{
799 struct stream_in *in = (struct stream_in *)stream;
800
Paul McLean994ac072016-06-02 15:33:24 -0600801 stream_lock(&in->lock);
Paul McLeancf5310a2018-08-22 14:33:12 -0700802 char * params_str = device_get_parameters(&in->profile, keys);
Paul McLean994ac072016-06-02 15:33:24 -0600803 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700804
805 return params_str;
806}
807
808static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
809{
810 return 0;
811}
812
813static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
814{
815 return 0;
816}
817
818static int in_set_gain(struct audio_stream_in *stream, float gain)
819{
820 return 0;
821}
822
823/* must be called with hw device and output stream mutexes locked */
824static int start_input_stream(struct stream_in *in)
825{
Paul McLeancf5310a2018-08-22 14:33:12 -0700826 ALOGV("start_input_stream(card:%d device:%d)", in->profile.card, in->profile.device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700827
828 return proxy_open(&in->proxy);
829}
830
831/* TODO mutex stuff here (see out_write) */
832static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
833{
834 size_t num_read_buff_bytes = 0;
835 void * read_buff = buffer;
836 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800837 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700838
839 struct stream_in * in = (struct stream_in *)stream;
840
Paul McLean994ac072016-06-02 15:33:24 -0600841 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700842 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700843 ret = start_input_stream(in);
Eric Laurent70318092015-06-19 17:49:17 -0700844 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700845 goto err;
846 }
847 in->standby = false;
848 }
849
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700850 /*
851 * OK, we need to figure out how much data to read to be able to output the requested
852 * number of bytes in the HAL format (16-bit, stereo).
853 */
854 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700855 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
856 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700857
858 if (num_device_channels != num_req_channels) {
859 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
860 }
861
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700862 /* Setup/Realloc the conversion buffer (if necessary). */
863 if (num_read_buff_bytes != bytes) {
864 if (num_read_buff_bytes > in->conversion_buffer_size) {
865 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
866 (and do these conversions themselves) */
867 in->conversion_buffer_size = num_read_buff_bytes;
868 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
869 }
870 read_buff = in->conversion_buffer;
871 }
872
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800873 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
874 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700875 if (num_device_channels != num_req_channels) {
876 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
877
878 out_buff = buffer;
879 /* Num Channels conversion */
880 if (num_device_channels != num_req_channels) {
881 audio_format_t audio_format = in_get_format(&(in->stream.common));
882 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
883
884 num_read_buff_bytes =
885 adjust_channels(read_buff, num_device_channels,
886 out_buff, num_req_channels,
887 sample_size_in_bytes, num_read_buff_bytes);
888 }
889 }
Eric Laurent253def92014-09-14 12:18:18 -0700890
Paul McLean76dba682016-06-01 12:29:11 -0600891 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
892 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700893 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530894 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800895 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700896 }
897
898err:
Paul McLean994ac072016-06-02 15:33:24 -0600899 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700900 return num_read_buff_bytes;
901}
902
903static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
904{
905 return 0;
906}
907
Andy Hungfa6b4a62018-06-04 19:14:22 -0700908static int in_get_capture_position(const struct audio_stream_in *stream,
909 int64_t *frames, int64_t *time)
910{
911 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
912 stream_lock(&in->lock);
913
914 const alsa_device_proxy *proxy = &in->proxy;
915 const int ret = proxy_get_capture_position(proxy, frames, time);
916
917 stream_unlock(&in->lock);
918 return ret;
919}
920
Paul McLeanfa3ae3e2018-12-12 09:57:02 -0800921static int in_get_active_microphones(const struct audio_stream_in *stream,
922 struct audio_microphone_characteristic_t *mic_array,
923 size_t *mic_count) {
924 (void)stream;
925 (void)mic_array;
926 (void)mic_count;
927
928 return -ENOSYS;
929}
930
931static int in_set_microphone_direction(const struct audio_stream_in *stream,
932 audio_microphone_direction_t dir) {
933 (void)stream;
934 (void)dir;
935 ALOGV("---- in_set_microphone_direction()");
936 return -ENOSYS;
937}
938
939static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
940 (void)zoom;
941 ALOGV("---- in_set_microphone_field_dimension()");
942 return -ENOSYS;
943}
944
Paul McLean76dba682016-06-01 12:29:11 -0600945static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700946 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600947 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700948 struct audio_config *config,
949 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700950 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -0700951 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -0700952 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700953{
Paul McLean1d585cc2016-05-24 11:28:10 -0600954 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700955 config->sample_rate, config->channel_mask, config->format);
956
Andy Hungb10ce6d2017-10-27 19:37:58 -0700957 /* Pull out the card/device pair */
958 int32_t card, device;
959 if (!parse_card_device_params(address, &card, &device)) {
960 ALOGW("%s fail - invalid address %s", __func__, address);
961 *stream_in = NULL;
962 return -EINVAL;
963 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700964
Andy Hungb10ce6d2017-10-27 19:37:58 -0700965 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -0600966 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700967 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700968 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600969 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700970
971 /* setup function pointers */
972 in->stream.common.get_sample_rate = in_get_sample_rate;
973 in->stream.common.set_sample_rate = in_set_sample_rate;
974 in->stream.common.get_buffer_size = in_get_buffer_size;
975 in->stream.common.get_channels = in_get_channels;
976 in->stream.common.get_format = in_get_format;
977 in->stream.common.set_format = in_set_format;
978 in->stream.common.standby = in_standby;
979 in->stream.common.dump = in_dump;
980 in->stream.common.set_parameters = in_set_parameters;
981 in->stream.common.get_parameters = in_get_parameters;
982 in->stream.common.add_audio_effect = in_add_audio_effect;
983 in->stream.common.remove_audio_effect = in_remove_audio_effect;
984
985 in->stream.set_gain = in_set_gain;
986 in->stream.read = in_read;
987 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -0700988 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700989
Paul McLeanfa3ae3e2018-12-12 09:57:02 -0800990 in->stream.get_active_microphones = in_get_active_microphones;
991 in->stream.set_microphone_direction = in_set_microphone_direction;
992 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
993
Paul McLean994ac072016-06-02 15:33:24 -0600994 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700995
Paul McLean76dba682016-06-01 12:29:11 -0600996 in->adev = (struct audio_device *)hw_dev;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700997
Paul McLeancf5310a2018-08-22 14:33:12 -0700998 profile_init(&in->profile, PCM_IN);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700999
1000 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -07001001 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001002
Andy Hungb10ce6d2017-10-27 19:37:58 -07001003 int ret = 0;
Paul McLeancf5310a2018-08-22 14:33:12 -07001004 device_lock(in->adev);
1005 int num_open_inputs = in->adev->inputs_open;
1006 device_unlock(in->adev);
1007
Andy Hungb10ce6d2017-10-27 19:37:58 -07001008 /* Check if an input stream is already open */
Paul McLeancf5310a2018-08-22 14:33:12 -07001009 if (num_open_inputs > 0) {
1010 if (!profile_is_cached_for(&in->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001011 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1012 __func__, card, device);
1013 ret = -EINVAL;
1014 }
1015 } else {
1016 /* Read input profile only if necessary */
Paul McLeancf5310a2018-08-22 14:33:12 -07001017 in->profile.card = card;
1018 in->profile.device = device;
1019 if (!profile_read_device_info(&in->profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001020 ALOGW("%s fail - cannot read profile", __func__);
1021 ret = -EINVAL;
1022 }
1023 }
1024 if (ret != 0) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001025 free(in);
1026 *stream_in = NULL;
1027 return ret;
1028 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001029
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001030 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001031 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001032 if (config->sample_rate == 0) {
Paul McLeancf5310a2018-08-22 14:33:12 -07001033 config->sample_rate = profile_get_default_sample_rate(&in->profile);
Paul McLean9a1c3052016-05-25 13:30:54 -06001034 }
1035
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001036 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001037 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001038 if (config->sample_rate != in->adev->device_sample_rate) {
Paul McLeancf5310a2018-08-22 14:33:12 -07001039 unsigned highest_rate = profile_get_highest_sample_rate(&in->profile);
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001040 if (highest_rate == 0) {
1041 ret = -EINVAL; /* error with device */
1042 } else {
1043 proxy_config.rate = config->sample_rate =
1044 min(highest_rate, in->adev->device_sample_rate);
1045 if (request_config_rate != 0 && proxy_config.rate != config->sample_rate) {
1046 /* Changing the requested rate */
1047 ret = -EINVAL;
1048 } else {
1049 /* Everything AOK! */
1050 ret = 0;
1051 }
1052 }
1053 }
Paul McLeancf5310a2018-08-22 14:33:12 -07001054 } else if (profile_is_sample_rate_valid(&in->profile, config->sample_rate)) {
Eric Laurent981f7742017-05-03 12:44:26 -07001055 proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001056 } else {
Paul McLeancf5310a2018-08-22 14:33:12 -07001057 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001058 ret = -EINVAL;
1059 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001060
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001061 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001062 if (config->format == AUDIO_FORMAT_DEFAULT) {
Paul McLeancf5310a2018-08-22 14:33:12 -07001063 proxy_config.format = profile_get_default_format(&in->profile);
Andy Hung780f1f82015-04-22 22:14:39 -07001064 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001065 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001066 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
Paul McLeancf5310a2018-08-22 14:33:12 -07001067 if (profile_is_format_valid(&in->profile, fmt)) {
Andy Hung780f1f82015-04-22 22:14:39 -07001068 proxy_config.format = fmt;
1069 } else {
Paul McLeancf5310a2018-08-22 14:33:12 -07001070 proxy_config.format = profile_get_default_format(&in->profile);
Andy Hung780f1f82015-04-22 22:14:39 -07001071 config->format = audio_format_from_pcm_format(proxy_config.format);
1072 ret = -EINVAL;
1073 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001074 }
1075
Paul McLean2cfd81b2014-09-15 12:32:23 -07001076 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001077 bool calc_mask = false;
1078 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1079 /* query case */
Paul McLeancf5310a2018-08-22 14:33:12 -07001080 in->hal_channel_count = profile_get_default_channel_count(&in->profile);
Paul McLean64345f82016-06-06 13:26:05 -06001081 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001082 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001083 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001084 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1085 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001086
Paul McLean64345f82016-06-06 13:26:05 -06001087 /* The Framework is currently limited to no more than this number of channels */
1088 if (in->hal_channel_count > FCC_8) {
1089 in->hal_channel_count = FCC_8;
1090 calc_mask = true;
1091 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001092
Paul McLean64345f82016-06-06 13:26:05 -06001093 if (calc_mask) {
1094 /* need to calculate the mask from channel count either because this is the query case
1095 * or the specified mask isn't valid for this device, or is more then the FW can handle */
1096 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1097 /* position mask for mono & stereo */
1098 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1099 /* otherwise indexed */
1100 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001101
Paul McLean64345f82016-06-06 13:26:05 -06001102 // if we change the mask...
1103 if (in->hal_channel_mask != config->channel_mask &&
1104 config->channel_mask != AUDIO_CHANNEL_NONE) {
1105 config->channel_mask = in->hal_channel_mask;
1106 ret = -EINVAL;
1107 }
1108 } else {
1109 in->hal_channel_mask = config->channel_mask;
1110 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001111
Paul McLean64345f82016-06-06 13:26:05 -06001112 if (ret == 0) {
1113 // Validate the "logical" channel count against support in the "actual" profile.
1114 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1115 // and store THAT in proxy_config.channels
1116 proxy_config.channels =
Paul McLeancf5310a2018-08-22 14:33:12 -07001117 profile_get_closest_channel_count(&in->profile, in->hal_channel_count);
1118 ret = proxy_prepare(&in->proxy, &in->profile, &proxy_config);
Eric Laurent981f7742017-05-03 12:44:26 -07001119 if (ret == 0) {
1120 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001121
Eric Laurent981f7742017-05-03 12:44:26 -07001122 in->conversion_buffer = NULL;
1123 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001124
Eric Laurent981f7742017-05-03 12:44:26 -07001125 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001126
Eric Laurent981f7742017-05-03 12:44:26 -07001127 /* Save this for adev_dump() */
1128 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1129 } else {
1130 ALOGW("proxy_prepare error %d", ret);
1131 unsigned channel_count = proxy_get_channel_count(&in->proxy);
1132 config->channel_mask = channel_count <= FCC_2
1133 ? audio_channel_in_mask_from_count(channel_count)
1134 : audio_channel_mask_for_index_assignment_from_count(channel_count);
1135 config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy));
1136 config->sample_rate = proxy_get_sample_rate(&in->proxy);
1137 }
1138 }
Paul McLean64345f82016-06-06 13:26:05 -06001139
Eric Laurent981f7742017-05-03 12:44:26 -07001140 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001141 // Deallocate this stream on error, because AudioFlinger won't call
1142 // adev_close_input_stream() in this case.
1143 *stream_in = NULL;
1144 free(in);
Mikhail Naganov1d08a562020-03-26 13:18:12 -07001145 return ret;
Paul McLean64345f82016-06-06 13:26:05 -06001146 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001147
Andy Hungb10ce6d2017-10-27 19:37:58 -07001148 device_lock(in->adev);
1149 ++in->adev->inputs_open;
1150 device_unlock(in->adev);
1151
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001152 return ret;
1153}
1154
Paul McLean76dba682016-06-01 12:29:11 -06001155static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1156 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001157{
1158 struct stream_in *in = (struct stream_in *)stream;
Paul McLeancf5310a2018-08-22 14:33:12 -07001159 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile.card, in->profile.device);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001160
Paul McLean76dba682016-06-01 12:29:11 -06001161 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001162
Andy Hungb10ce6d2017-10-27 19:37:58 -07001163 device_lock(in->adev);
1164 --in->adev->inputs_open;
1165 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1166 "invalid inputs_open: %d", in->adev->inputs_open);
1167 device_unlock(in->adev);
1168
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001169 /* Close the pcm device */
1170 in_standby(&stream->common);
1171
1172 free(in->conversion_buffer);
1173
1174 free(stream);
1175}
1176
1177/*
1178 * ADEV Functions
1179 */
Paul McLean76dba682016-06-01 12:29:11 -06001180static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001181{
1182 return 0;
1183}
1184
Paul McLean76dba682016-06-01 12:29:11 -06001185static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001186{
1187 return strdup("");
1188}
1189
Paul McLean76dba682016-06-01 12:29:11 -06001190static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001191{
1192 return 0;
1193}
1194
Paul McLean76dba682016-06-01 12:29:11 -06001195static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001196{
1197 return -ENOSYS;
1198}
1199
Paul McLean76dba682016-06-01 12:29:11 -06001200static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001201{
1202 return -ENOSYS;
1203}
1204
Paul McLean76dba682016-06-01 12:29:11 -06001205static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001206{
1207 return 0;
1208}
1209
Paul McLean76dba682016-06-01 12:29:11 -06001210static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001211{
Paul McLean76dba682016-06-01 12:29:11 -06001212 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001213 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001214 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001215 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001216 return -ENOSYS;
1217}
1218
Paul McLean76dba682016-06-01 12:29:11 -06001219static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001220{
1221 return -ENOSYS;
1222}
1223
Paul McLean76dba682016-06-01 12:29:11 -06001224static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001225{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001226 dprintf(fd, "\nUSB audio module:\n");
1227
1228 struct audio_device* adev = (struct audio_device*)device;
1229 const int kNumRetries = 3;
1230 const int kSleepTimeMS = 500;
1231
Paul McLean994ac072016-06-02 15:33:24 -06001232 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001233 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001234 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001235 sleep(kSleepTimeMS);
1236 retry--;
1237 }
1238
1239 if (retry > 0) {
1240 if (list_empty(&adev->output_stream_list)) {
1241 dprintf(fd, " No output streams.\n");
1242 } else {
1243 struct listnode* node;
1244 list_for_each(node, &adev->output_stream_list) {
1245 struct audio_stream* stream =
1246 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1247 out_dump(stream, fd);
1248 }
1249 }
1250
1251 if (list_empty(&adev->input_stream_list)) {
1252 dprintf(fd, "\n No input streams.\n");
1253 } else {
1254 struct listnode* node;
1255 list_for_each(node, &adev->input_stream_list) {
1256 struct audio_stream* stream =
1257 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1258 in_dump(stream, fd);
1259 }
1260 }
1261
Paul McLean994ac072016-06-02 15:33:24 -06001262 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001263 } else {
1264 // Couldn't lock
1265 dprintf(fd, " Could not obtain device lock.\n");
1266 }
1267
Simon Wilson19957a32012-04-06 16:17:12 -07001268 return 0;
1269}
1270
1271static int adev_close(hw_device_t *device)
1272{
Simon Wilson19957a32012-04-06 16:17:12 -07001273 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001274
Simon Wilson19957a32012-04-06 16:17:12 -07001275 return 0;
1276}
1277
Paul McLean30f41852014-04-16 15:44:20 -07001278static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001279{
Simon Wilson19957a32012-04-06 16:17:12 -07001280 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1281 return -EINVAL;
1282
Paul McLeaneedc92e2013-12-19 15:46:15 -08001283 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001284 if (!adev)
1285 return -ENOMEM;
1286
Paul McLeancf5310a2018-08-22 14:33:12 -07001287 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001288
Paul McLean6a75e4e2016-05-25 14:09:02 -06001289 list_init(&adev->output_stream_list);
1290 list_init(&adev->input_stream_list);
1291
Simon Wilson19957a32012-04-06 16:17:12 -07001292 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001293 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001294 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001295 adev->hw_device.common.close = adev_close;
1296
Simon Wilson19957a32012-04-06 16:17:12 -07001297 adev->hw_device.init_check = adev_init_check;
1298 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1299 adev->hw_device.set_master_volume = adev_set_master_volume;
1300 adev->hw_device.set_mode = adev_set_mode;
1301 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1302 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1303 adev->hw_device.set_parameters = adev_set_parameters;
1304 adev->hw_device.get_parameters = adev_get_parameters;
1305 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1306 adev->hw_device.open_output_stream = adev_open_output_stream;
1307 adev->hw_device.close_output_stream = adev_close_output_stream;
1308 adev->hw_device.open_input_stream = adev_open_input_stream;
1309 adev->hw_device.close_input_stream = adev_close_input_stream;
1310 adev->hw_device.dump = adev_dump;
1311
1312 *device = &adev->hw_device.common;
1313
1314 return 0;
1315}
1316
1317static struct hw_module_methods_t hal_module_methods = {
1318 .open = adev_open,
1319};
1320
1321struct audio_module HAL_MODULE_INFO_SYM = {
1322 .common = {
1323 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001324 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1325 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001326 .id = AUDIO_HARDWARE_MODULE_ID,
1327 .name = "USB audio HW HAL",
1328 .author = "The Android Open Source Project",
1329 .methods = &hal_module_methods,
1330 },
1331};