blob: e93396ffa48ab34088bd6bf78e2f4ba90d9d3bb6 [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 McLeanc88e6ae2014-07-16 09:48:34 -070047#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070048
Paul McLean1d585cc2016-05-24 11:28:10 -060049/* Lock play & record samples rates at or above this threshold */
50#define RATELOCK_THRESHOLD 96000
51
Simon Wilson19957a32012-04-06 16:17:12 -070052struct audio_device {
53 struct audio_hw_device hw_device;
54
55 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080056
57 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070058 alsa_device_profile out_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060059 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080060
61 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070062 alsa_device_profile in_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060063 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080064
Paul McLean1d585cc2016-05-24 11:28:10 -060065 /* lock input & output sample rates */
66 /*FIXME - How do we address multiple output streams? */
67 uint32_t device_sample_rate;
68
Eric Laurent253def92014-09-14 12:18:18 -070069 bool mic_muted;
70
Simon Wilson19957a32012-04-06 16:17:12 -070071 bool standby;
72};
73
Paul McLean994ac072016-06-02 15:33:24 -060074struct stream_lock {
75 pthread_mutex_t lock; /* see note below on mutex acquisition order */
76 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
77};
78
Simon Wilson19957a32012-04-06 16:17:12 -070079struct stream_out {
80 struct audio_stream_out stream;
81
Paul McLean994ac072016-06-02 15:33:24 -060082 struct stream_lock lock;
83
Paul McLeaneedc92e2013-12-19 15:46:15 -080084 bool standby;
85
Paul McLean76dba682016-06-01 12:29:11 -060086 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070087
Paul McLean65ec72b2015-02-18 09:13:24 -080088 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070089 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080090
Andy Hung03576be2014-07-21 21:16:45 -070091 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
92 * This may differ from the device channel count when
93 * the device is not compatible with AudioFlinger
94 * capabilities, e.g. exposes too many channels or
95 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -060096 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
97 * so the proxy doesn't have a channel_mask, but
98 * audio HALs need to talk about channel masks
99 * so expose the one calculated by
100 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700101
Paul McLean6a75e4e2016-05-25 14:09:02 -0600102 struct listnode list_node;
103
Paul McLeaneedc92e2013-12-19 15:46:15 -0800104 void * conversion_buffer; /* any conversions are put into here
105 * they could come from here too if
106 * there was a previous conversion */
107 size_t conversion_buffer_size; /* in bytes */
108};
109
Paul McLeaneedc92e2013-12-19 15:46:15 -0800110struct stream_in {
111 struct audio_stream_in stream;
112
Paul McLean994ac072016-06-02 15:33:24 -0600113 struct stream_lock lock;
114
Simon Wilson19957a32012-04-06 16:17:12 -0700115 bool standby;
116
Paul McLean76dba682016-06-01 12:29:11 -0600117 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800118
Paul McLean65ec72b2015-02-18 09:13:24 -0800119 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700120 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700121
Paul McLean2cfd81b2014-09-15 12:32:23 -0700122 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
123 * This may differ from the device channel count when
124 * the device is not compatible with AudioFlinger
125 * capabilities, e.g. exposes too many channels or
126 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600127 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
128 * so the proxy doesn't have a channel_mask, but
129 * audio HALs need to talk about channel masks
130 * so expose the one calculated by
131 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700132
Paul McLean6a75e4e2016-05-25 14:09:02 -0600133 struct listnode list_node;
134
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700135 /* 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 -0700136 void * conversion_buffer; /* any conversions are put into here
137 * they could come from here too if
138 * there was a previous conversion */
139 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700140};
141
Paul McLean994ac072016-06-02 15:33:24 -0600142/*
143 * Locking Helpers
144 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800145/*
Eric Laurent70318092015-06-19 17:49:17 -0700146 * NOTE: when multiple mutexes have to be acquired, always take the
147 * stream_in or stream_out mutex first, followed by the audio_device mutex.
148 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
149 * higher priority playback or capture thread.
150 */
151
Paul McLean994ac072016-06-02 15:33:24 -0600152static void stream_lock_init(struct stream_lock *lock) {
153 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
154 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
155}
156
157static void stream_lock(struct stream_lock *lock) {
158 pthread_mutex_lock(&lock->pre_lock);
159 pthread_mutex_lock(&lock->lock);
160 pthread_mutex_unlock(&lock->pre_lock);
161}
162
163static void stream_unlock(struct stream_lock *lock) {
164 pthread_mutex_unlock(&lock->lock);
165}
166
167static void device_lock(struct audio_device *adev) {
168 pthread_mutex_lock(&adev->lock);
169}
170
171static int device_try_lock(struct audio_device *adev) {
172 return pthread_mutex_trylock(&adev->lock);
173}
174
175static void device_unlock(struct audio_device *adev) {
176 pthread_mutex_unlock(&adev->lock);
177}
178
179/*
180 * streams list management
181 */
182static void adev_add_stream_to_list(
183 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
184 device_lock(adev);
185
186 list_add_tail(list, stream_node);
187
188 device_unlock(adev);
189}
190
191static void adev_remove_stream_from_list(
192 struct audio_device* adev, struct listnode* stream_node) {
193 device_lock(adev);
194
195 list_remove(stream_node);
196
197 device_unlock(adev);
198}
199
Eric Laurent70318092015-06-19 17:49:17 -0700200/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700201 * Extract the card and device numbers from the supplied key/value pairs.
202 * kvpairs A null-terminated string containing the key/value pairs or card and device.
203 * i.e. "card=1;device=42"
204 * card A pointer to a variable to receive the parsed-out card number.
205 * device A pointer to a variable to receive the parsed-out device number.
206 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
207 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800208 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700209 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800210static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700211{
212 struct str_parms * parms = str_parms_create_str(kvpairs);
213 char value[32];
214 int param_val;
215
216 // initialize to "undefined" state.
217 *card = -1;
218 *device = -1;
219
220 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
221 if (param_val >= 0) {
222 *card = atoi(value);
223 }
224
225 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
226 if (param_val >= 0) {
227 *device = atoi(value);
228 }
229
230 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800231
232 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700233}
234
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700235static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800236{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700237 if (profile->card < 0 || profile->device < 0) {
238 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800239 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700240
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700241 struct str_parms *query = str_parms_create_str(keys);
242 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700243
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700244 /* These keys are from hardware/libhardware/include/audio.h */
245 /* supported sample rates */
246 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
247 char* rates_list = profile_get_sample_rate_strs(profile);
248 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
249 rates_list);
250 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700251 }
252
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700253 /* supported channel counts */
254 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
255 char* channels_list = profile_get_channel_count_strs(profile);
256 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
257 channels_list);
258 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700259 }
260
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700261 /* supported sample formats */
262 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
263 char * format_params = profile_get_format_strs(profile);
264 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
265 format_params);
266 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700267 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700268 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700269
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700270 char* result_str = str_parms_to_str(result);
271 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700272
Paul McLean65ec72b2015-02-18 09:13:24 -0800273 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700274
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700275 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800276}
277
278/*
279 * HAl Functions
280 */
Simon Wilson19957a32012-04-06 16:17:12 -0700281/**
282 * NOTE: when multiple mutexes have to be acquired, always respect the
283 * following order: hw device > out stream
284 */
285
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700286/*
287 * OUT functions
288 */
Simon Wilson19957a32012-04-06 16:17:12 -0700289static uint32_t out_get_sample_rate(const struct audio_stream *stream)
290{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700291 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
292 ALOGV("out_get_sample_rate() = %d", rate);
293 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700294}
295
296static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
297{
298 return 0;
299}
300
301static size_t out_get_buffer_size(const struct audio_stream *stream)
302{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700303 const struct stream_out* out = (const struct stream_out*)stream;
304 size_t buffer_size =
305 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700306 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700307}
308
309static uint32_t out_get_channels(const struct audio_stream *stream)
310{
Andy Hung03576be2014-07-21 21:16:45 -0700311 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700312 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700313}
314
315static audio_format_t out_get_format(const struct audio_stream *stream)
316{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700317 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
318 * Relies on the framework to provide data in the specified format.
319 * This could change in the future.
320 */
321 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
322 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700323 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700324}
325
326static int out_set_format(struct audio_stream *stream, audio_format_t format)
327{
328 return 0;
329}
330
331static int out_standby(struct audio_stream *stream)
332{
333 struct stream_out *out = (struct stream_out *)stream;
334
Paul McLean994ac072016-06-02 15:33:24 -0600335 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700336 if (!out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600337 device_lock(out->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700338 proxy_close(&out->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600339 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700340 out->standby = true;
341 }
Paul McLean994ac072016-06-02 15:33:24 -0600342 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700343 return 0;
344}
345
Paul McLean6a75e4e2016-05-25 14:09:02 -0600346static int out_dump(const struct audio_stream *stream, int fd) {
347 const struct stream_out* out_stream = (const struct stream_out*) stream;
348
349 if (out_stream != NULL) {
350 dprintf(fd, "Output Profile:\n");
351 profile_dump(out_stream->profile, fd);
352
353 dprintf(fd, "Output Proxy:\n");
354 proxy_dump(&out_stream->proxy, fd);
355 }
356
Simon Wilson19957a32012-04-06 16:17:12 -0700357 return 0;
358}
359
360static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
361{
Paul McLean65ec72b2015-02-18 09:13:24 -0800362 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800363
Simon Wilson19957a32012-04-06 16:17:12 -0700364 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700365
Simon Wilson19957a32012-04-06 16:17:12 -0700366 int routing = 0;
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 McLean65ec72b2015-02-18 09:13:24 -0800377 /* Lock the device because that is where the profile lives */
Paul McLean994ac072016-06-02 15:33:24 -0600378 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700379
Paul McLean65ec72b2015-02-18 09:13:24 -0800380 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700381 /* cannot read pcm device info if playback is active */
382 if (!out->standby)
383 ret_value = -ENOSYS;
384 else {
385 int saved_card = out->profile->card;
386 int saved_device = out->profile->device;
387 out->profile->card = card;
388 out->profile->device = device;
389 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
390 if (ret_value != 0) {
391 out->profile->card = saved_card;
392 out->profile->device = saved_device;
393 }
394 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800395 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700396
Paul McLean994ac072016-06-02 15:33:24 -0600397 device_unlock(out->adev);
398 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700399
Paul McLeaneedc92e2013-12-19 15:46:15 -0800400 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700401}
402
Paul McLeanf62d75e2014-07-11 15:14:19 -0700403static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
404{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700405 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600406 stream_lock(&out->lock);
407 device_lock(out->adev);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700408
409 char * params_str = device_get_parameters(out->profile, keys);
410
Paul McLean994ac072016-06-02 15:33:24 -0600411 device_unlock(out->adev);
412 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700413 return params_str;
414}
415
Simon Wilson19957a32012-04-06 16:17:12 -0700416static uint32_t out_get_latency(const struct audio_stream_out *stream)
417{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700418 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
419 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700420}
421
Paul McLean30f41852014-04-16 15:44:20 -0700422static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700423{
424 return -ENOSYS;
425}
426
Paul McLeaneedc92e2013-12-19 15:46:15 -0800427/* must be called with hw device and output stream mutexes locked */
428static int start_output_stream(struct stream_out *out)
429{
Paul McLean65ec72b2015-02-18 09:13:24 -0800430 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800431
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700432 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800433}
434
435static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700436{
437 int ret;
438 struct stream_out *out = (struct stream_out *)stream;
439
Paul McLean994ac072016-06-02 15:33:24 -0600440 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700441 if (out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600442 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700443 ret = start_output_stream(out);
Paul McLean994ac072016-06-02 15:33:24 -0600444 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700445 if (ret != 0) {
446 goto err;
447 }
448 out->standby = false;
449 }
Eric Laurent05333d42014-08-04 20:29:17 -0700450
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700451 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700452 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800453 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700454 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
455 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700456 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700457 /* allocate buffer */
458 const size_t required_conversion_buffer_size =
459 bytes * num_device_channels / num_req_channels;
460 if (required_conversion_buffer_size > out->conversion_buffer_size) {
461 out->conversion_buffer_size = required_conversion_buffer_size;
462 out->conversion_buffer = realloc(out->conversion_buffer,
463 out->conversion_buffer_size);
464 }
465 /* convert data */
466 const audio_format_t audio_format = out_get_format(&(out->stream.common));
467 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800468 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700469 adjust_channels(write_buff, num_req_channels,
470 out->conversion_buffer, num_device_channels,
471 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800472 write_buff = out->conversion_buffer;
473 }
474
Paul McLeaneedc92e2013-12-19 15:46:15 -0800475 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700476 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800477 }
Simon Wilson19957a32012-04-06 16:17:12 -0700478
Paul McLean994ac072016-06-02 15:33:24 -0600479 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700480
481 return bytes;
482
483err:
Paul McLean994ac072016-06-02 15:33:24 -0600484 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700485 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700486 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700487 out_get_sample_rate(&stream->common));
488 }
489
490 return bytes;
491}
492
Paul McLean30f41852014-04-16 15:44:20 -0700493static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700494{
495 return -EINVAL;
496}
497
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700498static int out_get_presentation_position(const struct audio_stream_out *stream,
499 uint64_t *frames, struct timespec *timestamp)
500{
Andy Hungc9515ce2015-08-04 15:05:19 -0700501 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600502 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700503
504 const alsa_device_proxy *proxy = &out->proxy;
505 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
506
Paul McLean994ac072016-06-02 15:33:24 -0600507 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700508 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700509}
510
Simon Wilson19957a32012-04-06 16:17:12 -0700511static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
512{
513 return 0;
514}
515
516static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
517{
518 return 0;
519}
520
Paul McLean30f41852014-04-16 15:44:20 -0700521static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700522{
523 return -EINVAL;
524}
525
Paul McLean76dba682016-06-01 12:29:11 -0600526static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700527 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600528 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700529 audio_output_flags_t flags,
530 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700531 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700532 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700533{
Paul McLean76dba682016-06-01 12:29:11 -0600534 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
535 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800536
Simon Wilson19957a32012-04-06 16:17:12 -0700537 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700538
539 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600540 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700541 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600542 }
Simon Wilson19957a32012-04-06 16:17:12 -0700543
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700544 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700545 out->stream.common.get_sample_rate = out_get_sample_rate;
546 out->stream.common.set_sample_rate = out_set_sample_rate;
547 out->stream.common.get_buffer_size = out_get_buffer_size;
548 out->stream.common.get_channels = out_get_channels;
549 out->stream.common.get_format = out_get_format;
550 out->stream.common.set_format = out_set_format;
551 out->stream.common.standby = out_standby;
552 out->stream.common.dump = out_dump;
553 out->stream.common.set_parameters = out_set_parameters;
554 out->stream.common.get_parameters = out_get_parameters;
555 out->stream.common.add_audio_effect = out_add_audio_effect;
556 out->stream.common.remove_audio_effect = out_remove_audio_effect;
557 out->stream.get_latency = out_get_latency;
558 out->stream.set_volume = out_set_volume;
559 out->stream.write = out_write;
560 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700561 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700562 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
563
Paul McLean994ac072016-06-02 15:33:24 -0600564 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700565
Paul McLean76dba682016-06-01 12:29:11 -0600566 out->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600567 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600568 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700569
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700570 // build this to hand to the alsa_device_proxy
571 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700572 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800573
Paul McLean0f1753e2014-12-02 12:36:45 -0700574 /* Pull out the card/device pair */
575 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
576
577 profile_read_device_info(out->profile);
578
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700579 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800580
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700581 /* Rate */
582 if (config->sample_rate == 0) {
583 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
584 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
585 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800586 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700587 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
588 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800589 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800590
Paul McLean76dba682016-06-01 12:29:11 -0600591 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600592 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600593
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700594 /* Format */
595 if (config->format == AUDIO_FORMAT_DEFAULT) {
596 proxy_config.format = profile_get_default_format(out->profile);
597 config->format = audio_format_from_pcm_format(proxy_config.format);
598 } else {
599 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
600 if (profile_is_format_valid(out->profile, fmt)) {
601 proxy_config.format = fmt;
602 } else {
603 proxy_config.format = profile_get_default_format(out->profile);
604 config->format = audio_format_from_pcm_format(proxy_config.format);
605 ret = -EINVAL;
606 }
607 }
608
609 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600610 bool calc_mask = false;
611 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
612 /* query case */
613 out->hal_channel_count = profile_get_default_channel_count(out->profile);
614 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700615 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600616 /* explicit case */
617 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700618 }
Paul McLean698dbd72015-11-03 12:24:30 -0800619
Paul McLean64345f82016-06-06 13:26:05 -0600620 /* The Framework is currently limited to no more than this number of channels */
621 if (out->hal_channel_count > FCC_8) {
622 out->hal_channel_count = FCC_8;
623 calc_mask = true;
624 }
625
626 if (calc_mask) {
627 /* need to calculate the mask from channel count either because this is the query case
628 * or the specified mask isn't valid for this device, or is more then the FW can handle */
629 config->channel_mask = out->hal_channel_count <= FCC_2
630 /* position mask for mono and stereo*/
631 ? audio_channel_out_mask_from_count(out->hal_channel_count)
632 /* otherwise indexed */
633 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
634 }
635
Andy Hung182ddc72015-05-05 23:37:30 -0700636 out->hal_channel_mask = config->channel_mask;
637
Paul McLean64345f82016-06-06 13:26:05 -0600638 // Validate the "logical" channel count against support in the "actual" profile.
639 // if they differ, choose the "actual" number of channels *closest* to the "logical".
640 // and store THAT in proxy_config.channels
641 proxy_config.channels = profile_get_closest_channel_count(out->profile, out->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700642 proxy_prepare(&out->proxy, out->profile, &proxy_config);
643
644 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
645 ret = 0;
646
Paul McLeaneedc92e2013-12-19 15:46:15 -0800647 out->conversion_buffer = NULL;
648 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700649
650 out->standby = true;
651
Paul McLean6a75e4e2016-05-25 14:09:02 -0600652 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600653 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600654
Simon Wilson19957a32012-04-06 16:17:12 -0700655 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700656
657 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700658
659err_open:
660 free(out);
661 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800662 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700663}
664
Paul McLean76dba682016-06-01 12:29:11 -0600665static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700666 struct audio_stream_out *stream)
667{
668 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800669 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700670
Paul McLean76dba682016-06-01 12:29:11 -0600671 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600672
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700673 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700674 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800675
676 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700677
Paul McLeaneedc92e2013-12-19 15:46:15 -0800678 out->conversion_buffer = NULL;
679 out->conversion_buffer_size = 0;
680
Paul McLean994ac072016-06-02 15:33:24 -0600681 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600682 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600683 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600684
Simon Wilson19957a32012-04-06 16:17:12 -0700685 free(stream);
686}
687
Paul McLean76dba682016-06-01 12:29:11 -0600688static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700689 const struct audio_config *config)
690{
691 /* TODO This needs to be calculated based on format/channels/rate */
692 return 320;
693}
694
695/*
696 * IN functions
697 */
698static uint32_t in_get_sample_rate(const struct audio_stream *stream)
699{
700 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
701 ALOGV("in_get_sample_rate() = %d", rate);
702 return rate;
703}
704
705static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
706{
707 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
708 return -ENOSYS;
709}
710
711static size_t in_get_buffer_size(const struct audio_stream *stream)
712{
713 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700714 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700715}
716
717static uint32_t in_get_channels(const struct audio_stream *stream)
718{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700719 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700720 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700721}
722
723static audio_format_t in_get_format(const struct audio_stream *stream)
724{
Andy Hung780f1f82015-04-22 22:14:39 -0700725 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
726 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
727 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700728}
729
730static int in_set_format(struct audio_stream *stream, audio_format_t format)
731{
732 ALOGV("in_set_format(%d) - NOPE", format);
733
734 return -ENOSYS;
735}
736
737static int in_standby(struct audio_stream *stream)
738{
739 struct stream_in *in = (struct stream_in *)stream;
740
Paul McLean994ac072016-06-02 15:33:24 -0600741 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700742 if (!in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600743 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700744 proxy_close(&in->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600745 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700746 in->standby = true;
747 }
748
Paul McLean994ac072016-06-02 15:33:24 -0600749 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700750
751 return 0;
752}
753
754static int in_dump(const struct audio_stream *stream, int fd)
755{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600756 const struct stream_in* in_stream = (const struct stream_in*)stream;
757 if (in_stream != NULL) {
758 dprintf(fd, "Input Profile:\n");
759 profile_dump(in_stream->profile, fd);
760
761 dprintf(fd, "Input Proxy:\n");
762 proxy_dump(&in_stream->proxy, fd);
763 }
764
765 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700766}
767
768static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
769{
Paul McLean65ec72b2015-02-18 09:13:24 -0800770 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700771
772 struct stream_in *in = (struct stream_in *)stream;
773
774 char value[32];
775 int param_val;
776 int routing = 0;
777 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700778 int card = -1;
779 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700780
Paul McLean65ec72b2015-02-18 09:13:24 -0800781 if (!parse_card_device_params(kvpairs, &card, &device)) {
782 // nothing to do
783 return ret_value;
784 }
785
Paul McLean994ac072016-06-02 15:33:24 -0600786 stream_lock(&in->lock);
787 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700788
Paul McLean2c6196f2014-08-20 16:50:25 -0700789 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700790 /* cannot read pcm device info if playback is active */
791 if (!in->standby)
792 ret_value = -ENOSYS;
793 else {
794 int saved_card = in->profile->card;
795 int saved_device = in->profile->device;
796 in->profile->card = card;
797 in->profile->device = device;
798 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
799 if (ret_value != 0) {
800 in->profile->card = saved_card;
801 in->profile->device = saved_device;
802 }
803 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700804 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700805
Paul McLean994ac072016-06-02 15:33:24 -0600806 device_unlock(in->adev);
807 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700808
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700809 return ret_value;
810}
811
812static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
813{
814 struct stream_in *in = (struct stream_in *)stream;
815
Paul McLean994ac072016-06-02 15:33:24 -0600816 stream_lock(&in->lock);
817 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700818
819 char * params_str = device_get_parameters(in->profile, keys);
820
Paul McLean994ac072016-06-02 15:33:24 -0600821 device_unlock(in->adev);
822 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700823
824 return params_str;
825}
826
827static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
828{
829 return 0;
830}
831
832static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
833{
834 return 0;
835}
836
837static int in_set_gain(struct audio_stream_in *stream, float gain)
838{
839 return 0;
840}
841
842/* must be called with hw device and output stream mutexes locked */
843static int start_input_stream(struct stream_in *in)
844{
Paul McLean1d585cc2016-05-24 11:28:10 -0600845 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700846
847 return proxy_open(&in->proxy);
848}
849
850/* TODO mutex stuff here (see out_write) */
851static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
852{
853 size_t num_read_buff_bytes = 0;
854 void * read_buff = buffer;
855 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800856 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700857
858 struct stream_in * in = (struct stream_in *)stream;
859
Paul McLean994ac072016-06-02 15:33:24 -0600860 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700861 if (in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600862 device_lock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700863 ret = start_input_stream(in);
Paul McLean994ac072016-06-02 15:33:24 -0600864 device_unlock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700865 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700866 goto err;
867 }
868 in->standby = false;
869 }
870
871 alsa_device_profile * profile = in->profile;
872
873 /*
874 * OK, we need to figure out how much data to read to be able to output the requested
875 * number of bytes in the HAL format (16-bit, stereo).
876 */
877 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700878 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
879 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700880
881 if (num_device_channels != num_req_channels) {
882 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
883 }
884
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700885 /* Setup/Realloc the conversion buffer (if necessary). */
886 if (num_read_buff_bytes != bytes) {
887 if (num_read_buff_bytes > in->conversion_buffer_size) {
888 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
889 (and do these conversions themselves) */
890 in->conversion_buffer_size = num_read_buff_bytes;
891 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
892 }
893 read_buff = in->conversion_buffer;
894 }
895
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800896 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
897 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700898 if (num_device_channels != num_req_channels) {
899 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
900
901 out_buff = buffer;
902 /* Num Channels conversion */
903 if (num_device_channels != num_req_channels) {
904 audio_format_t audio_format = in_get_format(&(in->stream.common));
905 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
906
907 num_read_buff_bytes =
908 adjust_channels(read_buff, num_device_channels,
909 out_buff, num_req_channels,
910 sample_size_in_bytes, num_read_buff_bytes);
911 }
912 }
Eric Laurent253def92014-09-14 12:18:18 -0700913
Paul McLean76dba682016-06-01 12:29:11 -0600914 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
915 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700916 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530917 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800918 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700919 }
920
921err:
Paul McLean994ac072016-06-02 15:33:24 -0600922 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700923 return num_read_buff_bytes;
924}
925
926static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
927{
928 return 0;
929}
930
Paul McLean76dba682016-06-01 12:29:11 -0600931static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700932 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600933 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700934 struct audio_config *config,
935 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700936 audio_input_flags_t flags __unused,
Eric Laurent981f7742017-05-03 12:44:26 -0700937 const char *address,
Eric Laurentf5e24692014-07-27 16:14:57 -0700938 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700939{
Paul McLean1d585cc2016-05-24 11:28:10 -0600940 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700941 config->sample_rate, config->channel_mask, config->format);
942
943 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
944 int ret = 0;
945
Paul McLean76dba682016-06-01 12:29:11 -0600946 if (in == NULL) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700947 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600948 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700949
950 /* setup function pointers */
951 in->stream.common.get_sample_rate = in_get_sample_rate;
952 in->stream.common.set_sample_rate = in_set_sample_rate;
953 in->stream.common.get_buffer_size = in_get_buffer_size;
954 in->stream.common.get_channels = in_get_channels;
955 in->stream.common.get_format = in_get_format;
956 in->stream.common.set_format = in_set_format;
957 in->stream.common.standby = in_standby;
958 in->stream.common.dump = in_dump;
959 in->stream.common.set_parameters = in_set_parameters;
960 in->stream.common.get_parameters = in_get_parameters;
961 in->stream.common.add_audio_effect = in_add_audio_effect;
962 in->stream.common.remove_audio_effect = in_remove_audio_effect;
963
964 in->stream.set_gain = in_set_gain;
965 in->stream.read = in_read;
966 in->stream.get_input_frames_lost = in_get_input_frames_lost;
967
Paul McLean994ac072016-06-02 15:33:24 -0600968 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700969
Paul McLean76dba682016-06-01 12:29:11 -0600970 in->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600971 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700972
Paul McLean76dba682016-06-01 12:29:11 -0600973 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700974
975 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700976 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700977
Paul McLean0f1753e2014-12-02 12:36:45 -0700978 /* Pull out the card/device pair */
979 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
980
981 profile_read_device_info(in->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700982
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700983 /* Rate */
984 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -0600985 config->sample_rate = profile_get_default_sample_rate(in->profile);
986 }
987
Paul McLean76dba682016-06-01 12:29:11 -0600988 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */
989 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
990 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0;
991 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700992 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Eric Laurent981f7742017-05-03 12:44:26 -0700993 proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700994 } else {
995 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
996 ret = -EINVAL;
997 }
Paul McLean994ac072016-06-02 15:33:24 -0600998 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700999
1000 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001001 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -07001002 proxy_config.format = profile_get_default_format(in->profile);
1003 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001004 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001005 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1006 if (profile_is_format_valid(in->profile, fmt)) {
1007 proxy_config.format = fmt;
1008 } else {
1009 proxy_config.format = profile_get_default_format(in->profile);
1010 config->format = audio_format_from_pcm_format(proxy_config.format);
1011 ret = -EINVAL;
1012 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001013 }
1014
Paul McLean2cfd81b2014-09-15 12:32:23 -07001015 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001016 bool calc_mask = false;
1017 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1018 /* query case */
1019 in->hal_channel_count = profile_get_default_channel_count(in->profile);
1020 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001021 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001022 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001023 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1024 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001025
Paul McLean64345f82016-06-06 13:26:05 -06001026 /* The Framework is currently limited to no more than this number of channels */
1027 if (in->hal_channel_count > FCC_8) {
1028 in->hal_channel_count = FCC_8;
1029 calc_mask = true;
1030 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001031
Paul McLean64345f82016-06-06 13:26:05 -06001032 if (calc_mask) {
1033 /* need to calculate the mask from channel count either because this is the query case
1034 * or the specified mask isn't valid for this device, or is more then the FW can handle */
1035 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1036 /* position mask for mono & stereo */
1037 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1038 /* otherwise indexed */
1039 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001040
Paul McLean64345f82016-06-06 13:26:05 -06001041 // if we change the mask...
1042 if (in->hal_channel_mask != config->channel_mask &&
1043 config->channel_mask != AUDIO_CHANNEL_NONE) {
1044 config->channel_mask = in->hal_channel_mask;
1045 ret = -EINVAL;
1046 }
1047 } else {
1048 in->hal_channel_mask = config->channel_mask;
1049 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001050
Paul McLean64345f82016-06-06 13:26:05 -06001051 if (ret == 0) {
1052 // Validate the "logical" channel count against support in the "actual" profile.
1053 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1054 // and store THAT in proxy_config.channels
1055 proxy_config.channels =
1056 profile_get_closest_channel_count(in->profile, in->hal_channel_count);
Eric Laurent981f7742017-05-03 12:44:26 -07001057 ret = proxy_prepare(&in->proxy, in->profile, &proxy_config);
1058 if (ret == 0) {
1059 in->standby = true;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001060
Eric Laurent981f7742017-05-03 12:44:26 -07001061 in->conversion_buffer = NULL;
1062 in->conversion_buffer_size = 0;
Paul McLean64345f82016-06-06 13:26:05 -06001063
Eric Laurent981f7742017-05-03 12:44:26 -07001064 *stream_in = &in->stream;
Paul McLean64345f82016-06-06 13:26:05 -06001065
Eric Laurent981f7742017-05-03 12:44:26 -07001066 /* Save this for adev_dump() */
1067 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1068 } else {
1069 ALOGW("proxy_prepare error %d", ret);
1070 unsigned channel_count = proxy_get_channel_count(&in->proxy);
1071 config->channel_mask = channel_count <= FCC_2
1072 ? audio_channel_in_mask_from_count(channel_count)
1073 : audio_channel_mask_for_index_assignment_from_count(channel_count);
1074 config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy));
1075 config->sample_rate = proxy_get_sample_rate(&in->proxy);
1076 }
1077 }
Paul McLean64345f82016-06-06 13:26:05 -06001078
Eric Laurent981f7742017-05-03 12:44:26 -07001079 if (ret != 0) {
Paul McLean64345f82016-06-06 13:26:05 -06001080 // Deallocate this stream on error, because AudioFlinger won't call
1081 // adev_close_input_stream() in this case.
1082 *stream_in = NULL;
1083 free(in);
1084 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001085
1086 return ret;
1087}
1088
Paul McLean76dba682016-06-01 12:29:11 -06001089static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1090 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001091{
1092 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001093 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1094
Paul McLean76dba682016-06-01 12:29:11 -06001095 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001096
1097 /* Close the pcm device */
1098 in_standby(&stream->common);
1099
1100 free(in->conversion_buffer);
1101
1102 free(stream);
1103}
1104
1105/*
1106 * ADEV Functions
1107 */
Paul McLean76dba682016-06-01 12:29:11 -06001108static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001109{
1110 return 0;
1111}
1112
Paul McLean76dba682016-06-01 12:29:11 -06001113static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001114{
1115 return strdup("");
1116}
1117
Paul McLean76dba682016-06-01 12:29:11 -06001118static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001119{
1120 return 0;
1121}
1122
Paul McLean76dba682016-06-01 12:29:11 -06001123static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001124{
1125 return -ENOSYS;
1126}
1127
Paul McLean76dba682016-06-01 12:29:11 -06001128static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001129{
1130 return -ENOSYS;
1131}
1132
Paul McLean76dba682016-06-01 12:29:11 -06001133static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001134{
1135 return 0;
1136}
1137
Paul McLean76dba682016-06-01 12:29:11 -06001138static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001139{
Paul McLean76dba682016-06-01 12:29:11 -06001140 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001141 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001142 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001143 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001144 return -ENOSYS;
1145}
1146
Paul McLean76dba682016-06-01 12:29:11 -06001147static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001148{
1149 return -ENOSYS;
1150}
1151
Paul McLean76dba682016-06-01 12:29:11 -06001152static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001153{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001154 dprintf(fd, "\nUSB audio module:\n");
1155
1156 struct audio_device* adev = (struct audio_device*)device;
1157 const int kNumRetries = 3;
1158 const int kSleepTimeMS = 500;
1159
Paul McLean994ac072016-06-02 15:33:24 -06001160 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001161 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001162 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001163 sleep(kSleepTimeMS);
1164 retry--;
1165 }
1166
1167 if (retry > 0) {
1168 if (list_empty(&adev->output_stream_list)) {
1169 dprintf(fd, " No output streams.\n");
1170 } else {
1171 struct listnode* node;
1172 list_for_each(node, &adev->output_stream_list) {
1173 struct audio_stream* stream =
1174 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1175 out_dump(stream, fd);
1176 }
1177 }
1178
1179 if (list_empty(&adev->input_stream_list)) {
1180 dprintf(fd, "\n No input streams.\n");
1181 } else {
1182 struct listnode* node;
1183 list_for_each(node, &adev->input_stream_list) {
1184 struct audio_stream* stream =
1185 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1186 in_dump(stream, fd);
1187 }
1188 }
1189
Paul McLean994ac072016-06-02 15:33:24 -06001190 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001191 } else {
1192 // Couldn't lock
1193 dprintf(fd, " Could not obtain device lock.\n");
1194 }
1195
Simon Wilson19957a32012-04-06 16:17:12 -07001196 return 0;
1197}
1198
1199static int adev_close(hw_device_t *device)
1200{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001201 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001202 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001203
Simon Wilson19957a32012-04-06 16:17:12 -07001204 return 0;
1205}
1206
Paul McLean30f41852014-04-16 15:44:20 -07001207static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001208{
Simon Wilson19957a32012-04-06 16:17:12 -07001209 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1210 return -EINVAL;
1211
Paul McLeaneedc92e2013-12-19 15:46:15 -08001212 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001213 if (!adev)
1214 return -ENOMEM;
1215
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001216 profile_init(&adev->out_profile, PCM_OUT);
1217 profile_init(&adev->in_profile, PCM_IN);
1218
Paul McLean6a75e4e2016-05-25 14:09:02 -06001219 list_init(&adev->output_stream_list);
1220 list_init(&adev->input_stream_list);
1221
Simon Wilson19957a32012-04-06 16:17:12 -07001222 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001223 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001224 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001225 adev->hw_device.common.close = adev_close;
1226
Simon Wilson19957a32012-04-06 16:17:12 -07001227 adev->hw_device.init_check = adev_init_check;
1228 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1229 adev->hw_device.set_master_volume = adev_set_master_volume;
1230 adev->hw_device.set_mode = adev_set_mode;
1231 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1232 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1233 adev->hw_device.set_parameters = adev_set_parameters;
1234 adev->hw_device.get_parameters = adev_get_parameters;
1235 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1236 adev->hw_device.open_output_stream = adev_open_output_stream;
1237 adev->hw_device.close_output_stream = adev_close_output_stream;
1238 adev->hw_device.open_input_stream = adev_open_input_stream;
1239 adev->hw_device.close_input_stream = adev_close_input_stream;
1240 adev->hw_device.dump = adev_dump;
1241
1242 *device = &adev->hw_device.common;
1243
1244 return 0;
1245}
1246
1247static struct hw_module_methods_t hal_module_methods = {
1248 .open = adev_open,
1249};
1250
1251struct audio_module HAL_MODULE_INFO_SYM = {
1252 .common = {
1253 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001254 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1255 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001256 .id = AUDIO_HARDWARE_MODULE_ID,
1257 .name = "USB audio HW HAL",
1258 .author = "The Android Open Source Project",
1259 .methods = &hal_module_methods,
1260 },
1261};