blob: d797a21aab29c5bdc4ffdd95a2ca5c357b7143ab [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 McLeancf611912014-04-28 13:03:18 -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 McLeanc88e6ae2014-07-16 09:48:34 -070059 alsa_device_profile out_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060060 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080061
62 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070063 alsa_device_profile in_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060064 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080065
Paul McLean1d585cc2016-05-24 11:28:10 -060066 /* lock input & output sample rates */
67 /*FIXME - How do we address multiple output streams? */
Paul McLeancfdbd6b2018-07-27 11:16:02 -060068 uint32_t device_sample_rate; // this should be a rate that is common to both input & output
Paul McLean1d585cc2016-05-24 11:28:10 -060069
Eric Laurent253def92014-09-14 12:18:18 -070070 bool mic_muted;
71
Simon Wilson19957a32012-04-06 16:17:12 -070072 bool standby;
Andy Hungb10ce6d2017-10-27 19:37:58 -070073
74 int32_t inputs_open; /* number of input streams currently open. */
Simon Wilson19957a32012-04-06 16:17:12 -070075};
76
Paul McLean994ac072016-06-02 15:33:24 -060077struct stream_lock {
78 pthread_mutex_t lock; /* see note below on mutex acquisition order */
79 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
80};
81
Simon Wilson19957a32012-04-06 16:17:12 -070082struct stream_out {
83 struct audio_stream_out stream;
84
Paul McLean994ac072016-06-02 15:33:24 -060085 struct stream_lock lock;
86
Paul McLeaneedc92e2013-12-19 15:46:15 -080087 bool standby;
88
Paul McLean76dba682016-06-01 12:29:11 -060089 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070090
Andy Hung4e6a1c02017-10-27 20:31:46 -070091 const alsa_device_profile *profile; /* Points to the alsa_device_profile in the audio_device.
92 * Const, so modifications go through adev->out_profile
93 * and thus should have the hardware lock and ensure
94 * stream is not active and no other open output streams.
95 */
96
Paul McLeanc88e6ae2014-07-16 09:48:34 -070097 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080098
Andy Hung03576be2014-07-21 21:16:45 -070099 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
100 * This may differ from the device channel count when
101 * the device is not compatible with AudioFlinger
102 * capabilities, e.g. exposes too many channels or
103 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600104 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
105 * so the proxy doesn't have a channel_mask, but
106 * audio HALs need to talk about channel masks
107 * so expose the one calculated by
108 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700109
Paul McLean6a75e4e2016-05-25 14:09:02 -0600110 struct listnode list_node;
111
Paul McLeaneedc92e2013-12-19 15:46:15 -0800112 void * conversion_buffer; /* any conversions are put into here
113 * they could come from here too if
114 * there was a previous conversion */
115 size_t conversion_buffer_size; /* in bytes */
116};
117
Paul McLeaneedc92e2013-12-19 15:46:15 -0800118struct stream_in {
119 struct audio_stream_in stream;
120
Paul McLean994ac072016-06-02 15:33:24 -0600121 struct stream_lock lock;
122
Simon Wilson19957a32012-04-06 16:17:12 -0700123 bool standby;
124
Paul McLean76dba682016-06-01 12:29:11 -0600125 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800126
Andy Hung4e6a1c02017-10-27 20:31:46 -0700127 const alsa_device_profile *profile; /* Points to the alsa_device_profile in the audio_device.
128 * Const, so modifications go through adev->out_profile
129 * and thus should have the hardware lock and ensure
130 * stream is not active and no other open input streams.
131 */
132
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700133 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700134
Paul McLean2cfd81b2014-09-15 12:32:23 -0700135 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
136 * This may differ from the device channel count when
137 * the device is not compatible with AudioFlinger
138 * capabilities, e.g. exposes too many channels or
139 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600140 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
141 * so the proxy doesn't have a channel_mask, but
142 * audio HALs need to talk about channel masks
143 * so expose the one calculated by
144 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700145
Paul McLean6a75e4e2016-05-25 14:09:02 -0600146 struct listnode list_node;
147
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700148 /* 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 -0700149 void * conversion_buffer; /* any conversions are put into here
150 * they could come from here too if
151 * there was a previous conversion */
152 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700153};
154
Paul McLean994ac072016-06-02 15:33:24 -0600155/*
156 * Locking Helpers
157 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800158/*
Eric Laurent70318092015-06-19 17:49:17 -0700159 * NOTE: when multiple mutexes have to be acquired, always take the
160 * stream_in or stream_out mutex first, followed by the audio_device mutex.
161 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
162 * higher priority playback or capture thread.
163 */
164
Paul McLean994ac072016-06-02 15:33:24 -0600165static void stream_lock_init(struct stream_lock *lock) {
166 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
167 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
168}
169
170static void stream_lock(struct stream_lock *lock) {
171 pthread_mutex_lock(&lock->pre_lock);
172 pthread_mutex_lock(&lock->lock);
173 pthread_mutex_unlock(&lock->pre_lock);
174}
175
176static void stream_unlock(struct stream_lock *lock) {
177 pthread_mutex_unlock(&lock->lock);
178}
179
180static void device_lock(struct audio_device *adev) {
181 pthread_mutex_lock(&adev->lock);
182}
183
184static int device_try_lock(struct audio_device *adev) {
185 return pthread_mutex_trylock(&adev->lock);
186}
187
188static void device_unlock(struct audio_device *adev) {
189 pthread_mutex_unlock(&adev->lock);
190}
191
192/*
193 * streams list management
194 */
195static void adev_add_stream_to_list(
196 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
197 device_lock(adev);
198
199 list_add_tail(list, stream_node);
200
201 device_unlock(adev);
202}
203
204static void adev_remove_stream_from_list(
205 struct audio_device* adev, struct listnode* stream_node) {
206 device_lock(adev);
207
208 list_remove(stream_node);
209
210 device_unlock(adev);
211}
212
Eric Laurent70318092015-06-19 17:49:17 -0700213/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700214 * Extract the card and device numbers from the supplied key/value pairs.
215 * kvpairs A null-terminated string containing the key/value pairs or card and device.
216 * i.e. "card=1;device=42"
217 * card A pointer to a variable to receive the parsed-out card number.
218 * device A pointer to a variable to receive the parsed-out device number.
219 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
220 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800221 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700222 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800223static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700224{
225 struct str_parms * parms = str_parms_create_str(kvpairs);
226 char value[32];
227 int param_val;
228
229 // initialize to "undefined" state.
230 *card = -1;
231 *device = -1;
232
233 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
234 if (param_val >= 0) {
235 *card = atoi(value);
236 }
237
238 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
239 if (param_val >= 0) {
240 *device = atoi(value);
241 }
242
243 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800244
245 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700246}
247
Andy Hung4e6a1c02017-10-27 20:31:46 -0700248static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800249{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700250 if (profile->card < 0 || profile->device < 0) {
251 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800252 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700253
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700254 struct str_parms *query = str_parms_create_str(keys);
255 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700256
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700257 /* These keys are from hardware/libhardware/include/audio.h */
258 /* supported sample rates */
259 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
260 char* rates_list = profile_get_sample_rate_strs(profile);
261 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
262 rates_list);
263 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700264 }
265
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700266 /* supported channel counts */
267 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
268 char* channels_list = profile_get_channel_count_strs(profile);
269 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
270 channels_list);
271 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700272 }
273
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700274 /* supported sample formats */
275 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
276 char * format_params = profile_get_format_strs(profile);
277 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
278 format_params);
279 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700280 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700281 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700282
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700283 char* result_str = str_parms_to_str(result);
284 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700285
Paul McLean65ec72b2015-02-18 09:13:24 -0800286 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700287
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700288 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800289}
290
291/*
292 * HAl Functions
293 */
Simon Wilson19957a32012-04-06 16:17:12 -0700294/**
295 * NOTE: when multiple mutexes have to be acquired, always respect the
296 * following order: hw device > out stream
297 */
298
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700299/*
300 * OUT functions
301 */
Simon Wilson19957a32012-04-06 16:17:12 -0700302static uint32_t out_get_sample_rate(const struct audio_stream *stream)
303{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700304 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
305 ALOGV("out_get_sample_rate() = %d", rate);
306 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700307}
308
309static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
310{
311 return 0;
312}
313
314static size_t out_get_buffer_size(const struct audio_stream *stream)
315{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700316 const struct stream_out* out = (const struct stream_out*)stream;
317 size_t buffer_size =
318 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700319 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700320}
321
322static uint32_t out_get_channels(const struct audio_stream *stream)
323{
Andy Hung03576be2014-07-21 21:16:45 -0700324 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700325 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700326}
327
328static audio_format_t out_get_format(const struct audio_stream *stream)
329{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700330 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
331 * Relies on the framework to provide data in the specified format.
332 * This could change in the future.
333 */
334 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
335 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700336 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700337}
338
339static int out_set_format(struct audio_stream *stream, audio_format_t format)
340{
341 return 0;
342}
343
344static int out_standby(struct audio_stream *stream)
345{
346 struct stream_out *out = (struct stream_out *)stream;
347
Paul McLean994ac072016-06-02 15:33:24 -0600348 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700349 if (!out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600350 device_lock(out->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700351 proxy_close(&out->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600352 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700353 out->standby = true;
354 }
Paul McLean994ac072016-06-02 15:33:24 -0600355 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700356 return 0;
357}
358
Paul McLean6a75e4e2016-05-25 14:09:02 -0600359static int out_dump(const struct audio_stream *stream, int fd) {
360 const struct stream_out* out_stream = (const struct stream_out*) stream;
361
362 if (out_stream != NULL) {
363 dprintf(fd, "Output Profile:\n");
364 profile_dump(out_stream->profile, fd);
365
366 dprintf(fd, "Output Proxy:\n");
367 proxy_dump(&out_stream->proxy, fd);
368 }
369
Simon Wilson19957a32012-04-06 16:17:12 -0700370 return 0;
371}
372
373static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
374{
Paul McLean65ec72b2015-02-18 09:13:24 -0800375 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800376
Simon Wilson19957a32012-04-06 16:17:12 -0700377 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700378
Paul McLeaneedc92e2013-12-19 15:46:15 -0800379 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700380 int card = -1;
381 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700382
Paul McLean65ec72b2015-02-18 09:13:24 -0800383 if (!parse_card_device_params(kvpairs, &card, &device)) {
384 // nothing to do
385 return ret_value;
386 }
387
Paul McLean994ac072016-06-02 15:33:24 -0600388 stream_lock(&out->lock);
Paul McLean65ec72b2015-02-18 09:13:24 -0800389 /* Lock the device because that is where the profile lives */
Paul McLean994ac072016-06-02 15:33:24 -0600390 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700391
Paul McLean65ec72b2015-02-18 09:13:24 -0800392 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700393 /* cannot read pcm device info if playback is active */
394 if (!out->standby)
395 ret_value = -ENOSYS;
396 else {
397 int saved_card = out->profile->card;
398 int saved_device = out->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700399 out->adev->out_profile.card = card;
400 out->adev->out_profile.device = device;
401 ret_value = profile_read_device_info(&out->adev->out_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700402 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700403 out->adev->out_profile.card = saved_card;
404 out->adev->out_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700405 }
406 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800407 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700408
Paul McLean994ac072016-06-02 15:33:24 -0600409 device_unlock(out->adev);
410 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700411
Paul McLeaneedc92e2013-12-19 15:46:15 -0800412 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700413}
414
Paul McLeanf62d75e2014-07-11 15:14:19 -0700415static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
416{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700417 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600418 stream_lock(&out->lock);
419 device_lock(out->adev);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700420
421 char * params_str = device_get_parameters(out->profile, keys);
422
Paul McLean994ac072016-06-02 15:33:24 -0600423 device_unlock(out->adev);
424 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700425 return params_str;
426}
427
Simon Wilson19957a32012-04-06 16:17:12 -0700428static uint32_t out_get_latency(const struct audio_stream_out *stream)
429{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700430 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
431 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700432}
433
Paul McLean30f41852014-04-16 15:44:20 -0700434static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700435{
436 return -ENOSYS;
437}
438
Paul McLeaneedc92e2013-12-19 15:46:15 -0800439/* must be called with hw device and output stream mutexes locked */
440static int start_output_stream(struct stream_out *out)
441{
Paul McLean65ec72b2015-02-18 09:13:24 -0800442 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800443
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700444 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800445}
446
447static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700448{
449 int ret;
450 struct stream_out *out = (struct stream_out *)stream;
451
Paul McLean994ac072016-06-02 15:33:24 -0600452 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700453 if (out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600454 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700455 ret = start_output_stream(out);
Paul McLean994ac072016-06-02 15:33:24 -0600456 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700457 if (ret != 0) {
458 goto err;
459 }
460 out->standby = false;
461 }
Eric Laurent05333d42014-08-04 20:29:17 -0700462
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700463 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700464 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800465 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700466 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
467 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700468 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700469 /* allocate buffer */
470 const size_t required_conversion_buffer_size =
471 bytes * num_device_channels / num_req_channels;
472 if (required_conversion_buffer_size > out->conversion_buffer_size) {
473 out->conversion_buffer_size = required_conversion_buffer_size;
474 out->conversion_buffer = realloc(out->conversion_buffer,
475 out->conversion_buffer_size);
476 }
477 /* convert data */
478 const audio_format_t audio_format = out_get_format(&(out->stream.common));
479 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800480 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700481 adjust_channels(write_buff, num_req_channels,
482 out->conversion_buffer, num_device_channels,
483 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800484 write_buff = out->conversion_buffer;
485 }
486
Paul McLeaneedc92e2013-12-19 15:46:15 -0800487 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700488 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800489 }
Simon Wilson19957a32012-04-06 16:17:12 -0700490
Paul McLean994ac072016-06-02 15:33:24 -0600491 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700492
493 return bytes;
494
495err:
Paul McLean994ac072016-06-02 15:33:24 -0600496 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700497 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700498 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700499 out_get_sample_rate(&stream->common));
500 }
501
502 return bytes;
503}
504
Paul McLean30f41852014-04-16 15:44:20 -0700505static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700506{
507 return -EINVAL;
508}
509
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700510static int out_get_presentation_position(const struct audio_stream_out *stream,
511 uint64_t *frames, struct timespec *timestamp)
512{
Andy Hungc9515ce2015-08-04 15:05:19 -0700513 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600514 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700515
516 const alsa_device_proxy *proxy = &out->proxy;
517 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
518
Paul McLean994ac072016-06-02 15:33:24 -0600519 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700520 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700521}
522
Simon Wilson19957a32012-04-06 16:17:12 -0700523static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
524{
525 return 0;
526}
527
528static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
529{
530 return 0;
531}
532
Paul McLean30f41852014-04-16 15:44:20 -0700533static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700534{
535 return -EINVAL;
536}
537
Paul McLean76dba682016-06-01 12:29:11 -0600538static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700539 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600540 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700541 audio_output_flags_t flags,
542 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700543 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700544 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700545{
Paul McLean76dba682016-06-01 12:29:11 -0600546 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
547 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800548
Simon Wilson19957a32012-04-06 16:17:12 -0700549 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700550
551 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600552 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700553 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600554 }
Simon Wilson19957a32012-04-06 16:17:12 -0700555
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700556 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700557 out->stream.common.get_sample_rate = out_get_sample_rate;
558 out->stream.common.set_sample_rate = out_set_sample_rate;
559 out->stream.common.get_buffer_size = out_get_buffer_size;
560 out->stream.common.get_channels = out_get_channels;
561 out->stream.common.get_format = out_get_format;
562 out->stream.common.set_format = out_set_format;
563 out->stream.common.standby = out_standby;
564 out->stream.common.dump = out_dump;
565 out->stream.common.set_parameters = out_set_parameters;
566 out->stream.common.get_parameters = out_get_parameters;
567 out->stream.common.add_audio_effect = out_add_audio_effect;
568 out->stream.common.remove_audio_effect = out_remove_audio_effect;
569 out->stream.get_latency = out_get_latency;
570 out->stream.set_volume = out_set_volume;
571 out->stream.write = out_write;
572 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700573 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700574 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
575
Paul McLean994ac072016-06-02 15:33:24 -0600576 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700577
Paul McLean76dba682016-06-01 12:29:11 -0600578 out->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600579 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600580 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700581
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700582 // build this to hand to the alsa_device_proxy
583 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700584 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800585
Paul McLean0f1753e2014-12-02 12:36:45 -0700586 /* Pull out the card/device pair */
Andy Hung4e6a1c02017-10-27 20:31:46 -0700587 parse_card_device_params(address, &out->adev->out_profile.card, &out->adev->out_profile.device);
Paul McLean0f1753e2014-12-02 12:36:45 -0700588
Andy Hung4e6a1c02017-10-27 20:31:46 -0700589 profile_read_device_info(&out->adev->out_profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700590
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700591 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800592
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700593 /* Rate */
594 if (config->sample_rate == 0) {
595 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
596 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
597 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800598 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700599 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
600 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800601 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800602
Paul McLeancfdbd6b2018-07-27 11:16:02 -0600603 /* TODO: This is a problem if the input does not support this rate */
Paul McLean76dba682016-06-01 12:29:11 -0600604 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600605 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600606
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700607 /* Format */
608 if (config->format == AUDIO_FORMAT_DEFAULT) {
609 proxy_config.format = profile_get_default_format(out->profile);
610 config->format = audio_format_from_pcm_format(proxy_config.format);
611 } else {
612 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
613 if (profile_is_format_valid(out->profile, fmt)) {
614 proxy_config.format = fmt;
615 } else {
616 proxy_config.format = profile_get_default_format(out->profile);
617 config->format = audio_format_from_pcm_format(proxy_config.format);
618 ret = -EINVAL;
619 }
620 }
621
622 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600623 bool calc_mask = false;
624 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
625 /* query case */
626 out->hal_channel_count = profile_get_default_channel_count(out->profile);
627 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700628 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600629 /* explicit case */
630 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700631 }
Paul McLean698dbd72015-11-03 12:24:30 -0800632
Paul McLean64345f82016-06-06 13:26:05 -0600633 /* The Framework is currently limited to no more than this number of channels */
634 if (out->hal_channel_count > FCC_8) {
635 out->hal_channel_count = FCC_8;
636 calc_mask = true;
637 }
638
639 if (calc_mask) {
640 /* need to calculate the mask from channel count either because this is the query case
641 * or the specified mask isn't valid for this device, or is more then the FW can handle */
642 config->channel_mask = out->hal_channel_count <= FCC_2
643 /* position mask for mono and stereo*/
644 ? audio_channel_out_mask_from_count(out->hal_channel_count)
645 /* otherwise indexed */
646 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
647 }
648
Andy Hung182ddc72015-05-05 23:37:30 -0700649 out->hal_channel_mask = config->channel_mask;
650
Paul McLean64345f82016-06-06 13:26:05 -0600651 // Validate the "logical" channel count against support in the "actual" profile.
652 // if they differ, choose the "actual" number of channels *closest* to the "logical".
653 // and store THAT in proxy_config.channels
654 proxy_config.channels = profile_get_closest_channel_count(out->profile, out->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700655 proxy_prepare(&out->proxy, out->profile, &proxy_config);
656
Paul McLean96c4d302017-12-05 09:50:26 -0800657 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
658 * So clear any errors that may have occurred above.
659 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700660 ret = 0;
661
Paul McLeaneedc92e2013-12-19 15:46:15 -0800662 out->conversion_buffer = NULL;
663 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700664
665 out->standby = true;
666
Paul McLean6a75e4e2016-05-25 14:09:02 -0600667 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600668 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600669
Simon Wilson19957a32012-04-06 16:17:12 -0700670 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700671
672 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700673}
674
Paul McLean76dba682016-06-01 12:29:11 -0600675static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700676 struct audio_stream_out *stream)
677{
678 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800679 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700680
Paul McLean76dba682016-06-01 12:29:11 -0600681 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600682
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700683 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700684 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800685
686 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700687
Paul McLeaneedc92e2013-12-19 15:46:15 -0800688 out->conversion_buffer = NULL;
689 out->conversion_buffer_size = 0;
690
Paul McLean994ac072016-06-02 15:33:24 -0600691 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600692 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600693 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600694
Simon Wilson19957a32012-04-06 16:17:12 -0700695 free(stream);
696}
697
Paul McLean76dba682016-06-01 12:29:11 -0600698static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700699 const struct audio_config *config)
700{
701 /* TODO This needs to be calculated based on format/channels/rate */
702 return 320;
703}
704
705/*
706 * IN functions
707 */
708static uint32_t in_get_sample_rate(const struct audio_stream *stream)
709{
710 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
711 ALOGV("in_get_sample_rate() = %d", rate);
712 return rate;
713}
714
715static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
716{
717 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
718 return -ENOSYS;
719}
720
721static size_t in_get_buffer_size(const struct audio_stream *stream)
722{
723 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700724 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700725}
726
727static uint32_t in_get_channels(const struct audio_stream *stream)
728{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700729 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700730 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700731}
732
733static audio_format_t in_get_format(const struct audio_stream *stream)
734{
Andy Hung780f1f82015-04-22 22:14:39 -0700735 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
736 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
737 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700738}
739
740static int in_set_format(struct audio_stream *stream, audio_format_t format)
741{
742 ALOGV("in_set_format(%d) - NOPE", format);
743
744 return -ENOSYS;
745}
746
747static int in_standby(struct audio_stream *stream)
748{
749 struct stream_in *in = (struct stream_in *)stream;
750
Paul McLean994ac072016-06-02 15:33:24 -0600751 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700752 if (!in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600753 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700754 proxy_close(&in->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600755 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700756 in->standby = true;
757 }
758
Paul McLean994ac072016-06-02 15:33:24 -0600759 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700760
761 return 0;
762}
763
764static int in_dump(const struct audio_stream *stream, int fd)
765{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600766 const struct stream_in* in_stream = (const struct stream_in*)stream;
767 if (in_stream != NULL) {
768 dprintf(fd, "Input Profile:\n");
769 profile_dump(in_stream->profile, fd);
770
771 dprintf(fd, "Input Proxy:\n");
772 proxy_dump(&in_stream->proxy, fd);
773 }
774
775 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700776}
777
778static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
779{
Paul McLean65ec72b2015-02-18 09:13:24 -0800780 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700781
782 struct stream_in *in = (struct stream_in *)stream;
783
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700784 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700785 int card = -1;
786 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700787
Paul McLean65ec72b2015-02-18 09:13:24 -0800788 if (!parse_card_device_params(kvpairs, &card, &device)) {
789 // nothing to do
790 return ret_value;
791 }
792
Paul McLean994ac072016-06-02 15:33:24 -0600793 stream_lock(&in->lock);
794 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700795
Paul McLean2c6196f2014-08-20 16:50:25 -0700796 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700797 /* cannot read pcm device info if playback is active, or more than one open stream */
798 if (!in->standby || in->adev->inputs_open > 1)
Eric Laurent05333d42014-08-04 20:29:17 -0700799 ret_value = -ENOSYS;
800 else {
801 int saved_card = in->profile->card;
802 int saved_device = in->profile->device;
Andy Hung4e6a1c02017-10-27 20:31:46 -0700803 in->adev->in_profile.card = card;
804 in->adev->in_profile.device = device;
805 ret_value = profile_read_device_info(&in->adev->in_profile) ? 0 : -EINVAL;
Eric Laurent05333d42014-08-04 20:29:17 -0700806 if (ret_value != 0) {
Andy Hung4e6a1c02017-10-27 20:31:46 -0700807 in->adev->in_profile.card = saved_card;
808 in->adev->in_profile.device = saved_device;
Eric Laurent05333d42014-08-04 20:29:17 -0700809 }
810 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700811 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700812
Paul McLean994ac072016-06-02 15:33:24 -0600813 device_unlock(in->adev);
814 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700815
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700816 return ret_value;
817}
818
819static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
820{
821 struct stream_in *in = (struct stream_in *)stream;
822
Paul McLean994ac072016-06-02 15:33:24 -0600823 stream_lock(&in->lock);
824 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700825
826 char * params_str = device_get_parameters(in->profile, keys);
827
Paul McLean994ac072016-06-02 15:33:24 -0600828 device_unlock(in->adev);
829 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700830
831 return params_str;
832}
833
834static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
835{
836 return 0;
837}
838
839static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
840{
841 return 0;
842}
843
844static int in_set_gain(struct audio_stream_in *stream, float gain)
845{
846 return 0;
847}
848
849/* must be called with hw device and output stream mutexes locked */
850static int start_input_stream(struct stream_in *in)
851{
Paul McLean1d585cc2016-05-24 11:28:10 -0600852 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700853
854 return proxy_open(&in->proxy);
855}
856
857/* TODO mutex stuff here (see out_write) */
858static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
859{
860 size_t num_read_buff_bytes = 0;
861 void * read_buff = buffer;
862 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800863 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700864
865 struct stream_in * in = (struct stream_in *)stream;
866
Paul McLean994ac072016-06-02 15:33:24 -0600867 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700868 if (in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600869 device_lock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700870 ret = start_input_stream(in);
Paul McLean994ac072016-06-02 15:33:24 -0600871 device_unlock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700872 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700873 goto err;
874 }
875 in->standby = false;
876 }
877
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700878 /*
879 * OK, we need to figure out how much data to read to be able to output the requested
880 * number of bytes in the HAL format (16-bit, stereo).
881 */
882 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700883 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
884 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700885
886 if (num_device_channels != num_req_channels) {
887 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
888 }
889
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700890 /* Setup/Realloc the conversion buffer (if necessary). */
891 if (num_read_buff_bytes != bytes) {
892 if (num_read_buff_bytes > in->conversion_buffer_size) {
893 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
894 (and do these conversions themselves) */
895 in->conversion_buffer_size = num_read_buff_bytes;
896 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
897 }
898 read_buff = in->conversion_buffer;
899 }
900
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800901 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
902 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700903 if (num_device_channels != num_req_channels) {
904 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
905
906 out_buff = buffer;
907 /* Num Channels conversion */
908 if (num_device_channels != num_req_channels) {
909 audio_format_t audio_format = in_get_format(&(in->stream.common));
910 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
911
912 num_read_buff_bytes =
913 adjust_channels(read_buff, num_device_channels,
914 out_buff, num_req_channels,
915 sample_size_in_bytes, num_read_buff_bytes);
916 }
917 }
Eric Laurent253def92014-09-14 12:18:18 -0700918
Paul McLean76dba682016-06-01 12:29:11 -0600919 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
920 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700921 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530922 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800923 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700924 }
925
926err:
Paul McLean994ac072016-06-02 15:33:24 -0600927 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700928 return num_read_buff_bytes;
929}
930
931static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
932{
933 return 0;
934}
935
Andy Hungfa6b4a62018-06-04 19:14:22 -0700936static int in_get_capture_position(const struct audio_stream_in *stream,
937 int64_t *frames, int64_t *time)
938{
939 struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
940 stream_lock(&in->lock);
941
942 const alsa_device_proxy *proxy = &in->proxy;
943 const int ret = proxy_get_capture_position(proxy, frames, time);
944
945 stream_unlock(&in->lock);
946 return ret;
947}
948
Paul McLean76dba682016-06-01 12:29:11 -0600949static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700950 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600951 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700952 struct audio_config *config,
953 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700954 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -0700955 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -0700956 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700957{
Paul McLean1d585cc2016-05-24 11:28:10 -0600958 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700959 config->sample_rate, config->channel_mask, config->format);
960
Andy Hungb10ce6d2017-10-27 19:37:58 -0700961 /* Pull out the card/device pair */
962 int32_t card, device;
963 if (!parse_card_device_params(address, &card, &device)) {
964 ALOGW("%s fail - invalid address %s", __func__, address);
965 *stream_in = NULL;
966 return -EINVAL;
967 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700968
Andy Hungb10ce6d2017-10-27 19:37:58 -0700969 struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Paul McLean76dba682016-06-01 12:29:11 -0600970 if (in == NULL) {
Andy Hungb10ce6d2017-10-27 19:37:58 -0700971 *stream_in = NULL;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700972 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600973 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700974
975 /* setup function pointers */
976 in->stream.common.get_sample_rate = in_get_sample_rate;
977 in->stream.common.set_sample_rate = in_set_sample_rate;
978 in->stream.common.get_buffer_size = in_get_buffer_size;
979 in->stream.common.get_channels = in_get_channels;
980 in->stream.common.get_format = in_get_format;
981 in->stream.common.set_format = in_set_format;
982 in->stream.common.standby = in_standby;
983 in->stream.common.dump = in_dump;
984 in->stream.common.set_parameters = in_set_parameters;
985 in->stream.common.get_parameters = in_get_parameters;
986 in->stream.common.add_audio_effect = in_add_audio_effect;
987 in->stream.common.remove_audio_effect = in_remove_audio_effect;
988
989 in->stream.set_gain = in_set_gain;
990 in->stream.read = in_read;
991 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hungfa6b4a62018-06-04 19:14:22 -0700992 in->stream.get_capture_position = in_get_capture_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700993
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 McLean994ac072016-06-02 15:33:24 -0600997 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700998
Paul McLean76dba682016-06-01 12:29:11 -0600999 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001000
1001 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -07001002 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001003
Andy Hungb10ce6d2017-10-27 19:37:58 -07001004 int ret = 0;
1005 /* Check if an input stream is already open */
1006 if (in->adev->inputs_open > 0) {
1007 if (!profile_is_cached_for(in->profile, card, device)) {
1008 ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1009 __func__, card, device);
1010 ret = -EINVAL;
1011 }
1012 } else {
1013 /* Read input profile only if necessary */
Andy Hung4e6a1c02017-10-27 20:31:46 -07001014 in->adev->in_profile.card = card;
1015 in->adev->in_profile.device = device;
1016 if (!profile_read_device_info(&in->adev->in_profile)) {
Andy Hungb10ce6d2017-10-27 19:37:58 -07001017 ALOGW("%s fail - cannot read profile", __func__);
1018 ret = -EINVAL;
1019 }
1020 }
1021 if (ret != 0) {
1022 device_unlock(in->adev);
1023 free(in);
1024 *stream_in = NULL;
1025 return ret;
1026 }
Paul McLean0f1753e2014-12-02 12:36:45 -07001027
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001028 /* Rate */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001029 int request_config_rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001030 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -06001031 config->sample_rate = profile_get_default_sample_rate(in->profile);
1032 }
1033
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001034 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
Paul McLean76dba682016-06-01 12:29:11 -06001035 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001036 if (config->sample_rate != in->adev->device_sample_rate) {
1037 unsigned highest_rate = profile_get_highest_sample_rate(in->profile);
1038 if (highest_rate == 0) {
1039 ret = -EINVAL; /* error with device */
1040 } else {
1041 proxy_config.rate = config->sample_rate =
1042 min(highest_rate, in->adev->device_sample_rate);
1043 if (request_config_rate != 0 && proxy_config.rate != config->sample_rate) {
1044 /* Changing the requested rate */
1045 ret = -EINVAL;
1046 } else {
1047 /* Everything AOK! */
1048 ret = 0;
1049 }
1050 }
1051 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001052 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Eric Laurent981f7742017-05-03 12:44:26 -07001053 proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001054 } else {
1055 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
1056 ret = -EINVAL;
1057 }
Paul McLeancfdbd6b2018-07-27 11:16:02 -06001058
Paul McLean994ac072016-06-02 15:33:24 -06001059 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001060
1061 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001062 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -07001063 proxy_config.format = profile_get_default_format(in->profile);
1064 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);
1067 if (profile_is_format_valid(in->profile, fmt)) {
1068 proxy_config.format = fmt;
1069 } else {
1070 proxy_config.format = profile_get_default_format(in->profile);
1071 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 */
1080 in->hal_channel_count = profile_get_default_channel_count(in->profile);
1081 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 =
1117 profile_get_closest_channel_count(in->profile, in->hal_channel_count);
Eric Laurent981f7742017-05-03 12:44:26 -07001118 ret = proxy_prepare(&in->proxy, in->profile, &proxy_config);
1119 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);
1145 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001146
Andy Hungb10ce6d2017-10-27 19:37:58 -07001147 device_lock(in->adev);
1148 ++in->adev->inputs_open;
1149 device_unlock(in->adev);
1150
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001151 return ret;
1152}
1153
Paul McLean76dba682016-06-01 12:29:11 -06001154static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1155 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001156{
1157 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001158 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1159
Paul McLean76dba682016-06-01 12:29:11 -06001160 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001161
Andy Hungb10ce6d2017-10-27 19:37:58 -07001162 device_lock(in->adev);
1163 --in->adev->inputs_open;
1164 LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1165 "invalid inputs_open: %d", in->adev->inputs_open);
1166 device_unlock(in->adev);
1167
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001168 /* Close the pcm device */
1169 in_standby(&stream->common);
1170
1171 free(in->conversion_buffer);
1172
1173 free(stream);
1174}
1175
1176/*
1177 * ADEV Functions
1178 */
Paul McLean76dba682016-06-01 12:29:11 -06001179static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001180{
1181 return 0;
1182}
1183
Paul McLean76dba682016-06-01 12:29:11 -06001184static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001185{
1186 return strdup("");
1187}
1188
Paul McLean76dba682016-06-01 12:29:11 -06001189static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001190{
1191 return 0;
1192}
1193
Paul McLean76dba682016-06-01 12:29:11 -06001194static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001195{
1196 return -ENOSYS;
1197}
1198
Paul McLean76dba682016-06-01 12:29:11 -06001199static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001200{
1201 return -ENOSYS;
1202}
1203
Paul McLean76dba682016-06-01 12:29:11 -06001204static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001205{
1206 return 0;
1207}
1208
Paul McLean76dba682016-06-01 12:29:11 -06001209static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001210{
Paul McLean76dba682016-06-01 12:29:11 -06001211 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001212 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001213 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001214 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001215 return -ENOSYS;
1216}
1217
Paul McLean76dba682016-06-01 12:29:11 -06001218static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001219{
1220 return -ENOSYS;
1221}
1222
Paul McLean76dba682016-06-01 12:29:11 -06001223static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001224{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001225 dprintf(fd, "\nUSB audio module:\n");
1226
1227 struct audio_device* adev = (struct audio_device*)device;
1228 const int kNumRetries = 3;
1229 const int kSleepTimeMS = 500;
1230
Paul McLean994ac072016-06-02 15:33:24 -06001231 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001232 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001233 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001234 sleep(kSleepTimeMS);
1235 retry--;
1236 }
1237
1238 if (retry > 0) {
1239 if (list_empty(&adev->output_stream_list)) {
1240 dprintf(fd, " No output streams.\n");
1241 } else {
1242 struct listnode* node;
1243 list_for_each(node, &adev->output_stream_list) {
1244 struct audio_stream* stream =
1245 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1246 out_dump(stream, fd);
1247 }
1248 }
1249
1250 if (list_empty(&adev->input_stream_list)) {
1251 dprintf(fd, "\n No input streams.\n");
1252 } else {
1253 struct listnode* node;
1254 list_for_each(node, &adev->input_stream_list) {
1255 struct audio_stream* stream =
1256 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1257 in_dump(stream, fd);
1258 }
1259 }
1260
Paul McLean994ac072016-06-02 15:33:24 -06001261 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001262 } else {
1263 // Couldn't lock
1264 dprintf(fd, " Could not obtain device lock.\n");
1265 }
1266
Simon Wilson19957a32012-04-06 16:17:12 -07001267 return 0;
1268}
1269
1270static int adev_close(hw_device_t *device)
1271{
Simon Wilson19957a32012-04-06 16:17:12 -07001272 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001273
Simon Wilson19957a32012-04-06 16:17:12 -07001274 return 0;
1275}
1276
Paul McLean30f41852014-04-16 15:44:20 -07001277static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001278{
Simon Wilson19957a32012-04-06 16:17:12 -07001279 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1280 return -EINVAL;
1281
Paul McLeaneedc92e2013-12-19 15:46:15 -08001282 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001283 if (!adev)
1284 return -ENOMEM;
1285
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001286 profile_init(&adev->out_profile, PCM_OUT);
1287 profile_init(&adev->in_profile, PCM_IN);
1288
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};