blob: 10e0a755c4ef30827e5461b555f2cce02bb23ad5 [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>
Simon Wilson19957a32012-04-06 16:17:12 -070026
Mark Salyzyn88e458a2014-04-28 12:30:44 -070027#include <log/log.h>
Paul McLean6a75e4e2016-05-25 14:09:02 -060028#include <cutils/list.h>
Simon Wilson19957a32012-04-06 16:17:12 -070029#include <cutils/str_parms.h>
30#include <cutils/properties.h>
31
Simon Wilson19957a32012-04-06 16:17:12 -070032#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070033#include <hardware/audio_alsaops.h>
34#include <hardware/hardware.h>
35
36#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070037
38#include <tinyalsa/asoundlib.h>
39
Paul McLeanc2201152014-07-16 13:46:07 -070040#include <audio_utils/channels.h>
41
Andy Hung03576be2014-07-21 21:16:45 -070042/* FOR TESTING:
43 * Set k_force_channels to force the number of channels to present to AudioFlinger.
44 * 0 disables (this is default: present the device channels to AudioFlinger).
45 * 2 forces to legacy stereo mode.
46 *
47 * Others values can be tried (up to 8).
48 * TODO: AudioFlinger cannot support more than 8 active output channels
49 * at this time, so limiting logic needs to be put here or communicated from above.
50 */
51static const unsigned k_force_channels = 0;
52
Paul McLeanc88e6ae2014-07-16 09:48:34 -070053#include "alsa_device_profile.h"
54#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080055#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070056
Paul McLeanc88e6ae2014-07-16 09:48:34 -070057#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070058
Paul McLean1d585cc2016-05-24 11:28:10 -060059/* Lock play & record samples rates at or above this threshold */
60#define RATELOCK_THRESHOLD 96000
61
Simon Wilson19957a32012-04-06 16:17:12 -070062struct audio_device {
63 struct audio_hw_device hw_device;
64
65 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080066
67 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070068 alsa_device_profile out_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060069 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080070
71 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070072 alsa_device_profile in_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060073 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080074
Paul McLean1d585cc2016-05-24 11:28:10 -060075 /* lock input & output sample rates */
76 /*FIXME - How do we address multiple output streams? */
77 uint32_t device_sample_rate;
78
Eric Laurent253def92014-09-14 12:18:18 -070079 bool mic_muted;
80
Simon Wilson19957a32012-04-06 16:17:12 -070081 bool standby;
82};
83
Paul McLean994ac072016-06-02 15:33:24 -060084struct stream_lock {
85 pthread_mutex_t lock; /* see note below on mutex acquisition order */
86 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
87};
88
Simon Wilson19957a32012-04-06 16:17:12 -070089struct stream_out {
90 struct audio_stream_out stream;
91
Paul McLean994ac072016-06-02 15:33:24 -060092 struct stream_lock lock;
93
Paul McLeaneedc92e2013-12-19 15:46:15 -080094 bool standby;
95
Paul McLean76dba682016-06-01 12:29:11 -060096 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070097
Paul McLean65ec72b2015-02-18 09:13:24 -080098 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070099 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800100
Andy Hung03576be2014-07-21 21:16:45 -0700101 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
102 * This may differ from the device channel count when
103 * the device is not compatible with AudioFlinger
104 * capabilities, e.g. exposes too many channels or
105 * too few channels. */
Andy Hung182ddc72015-05-05 23:37:30 -0700106 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
107
Paul McLean6a75e4e2016-05-25 14:09:02 -0600108 struct listnode list_node;
109
Paul McLeaneedc92e2013-12-19 15:46:15 -0800110 void * conversion_buffer; /* any conversions are put into here
111 * they could come from here too if
112 * there was a previous conversion */
113 size_t conversion_buffer_size; /* in bytes */
114};
115
Paul McLeaneedc92e2013-12-19 15:46:15 -0800116struct stream_in {
117 struct audio_stream_in stream;
118
Paul McLean994ac072016-06-02 15:33:24 -0600119 struct stream_lock lock;
120
Simon Wilson19957a32012-04-06 16:17:12 -0700121 bool standby;
122
Paul McLean76dba682016-06-01 12:29:11 -0600123 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800124
Paul McLean65ec72b2015-02-18 09:13:24 -0800125 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700126 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700127
Paul McLean2cfd81b2014-09-15 12:32:23 -0700128 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
129 * This may differ from the device channel count when
130 * the device is not compatible with AudioFlinger
131 * capabilities, e.g. exposes too many channels or
132 * too few channels. */
Andy Hung780f1f82015-04-22 22:14:39 -0700133 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
134
Paul McLean6a75e4e2016-05-25 14:09:02 -0600135 struct listnode list_node;
136
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700137 /* 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 -0700138 void * conversion_buffer; /* any conversions are put into here
139 * they could come from here too if
140 * there was a previous conversion */
141 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700142};
143
Paul McLean994ac072016-06-02 15:33:24 -0600144/*
145 * Locking Helpers
146 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800147/*
Eric Laurent70318092015-06-19 17:49:17 -0700148 * NOTE: when multiple mutexes have to be acquired, always take the
149 * stream_in or stream_out mutex first, followed by the audio_device mutex.
150 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
151 * higher priority playback or capture thread.
152 */
153
Paul McLean994ac072016-06-02 15:33:24 -0600154static void stream_lock_init(struct stream_lock *lock) {
155 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
156 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
157}
158
159static void stream_lock(struct stream_lock *lock) {
160 pthread_mutex_lock(&lock->pre_lock);
161 pthread_mutex_lock(&lock->lock);
162 pthread_mutex_unlock(&lock->pre_lock);
163}
164
165static void stream_unlock(struct stream_lock *lock) {
166 pthread_mutex_unlock(&lock->lock);
167}
168
169static void device_lock(struct audio_device *adev) {
170 pthread_mutex_lock(&adev->lock);
171}
172
173static int device_try_lock(struct audio_device *adev) {
174 return pthread_mutex_trylock(&adev->lock);
175}
176
177static void device_unlock(struct audio_device *adev) {
178 pthread_mutex_unlock(&adev->lock);
179}
180
181/*
182 * streams list management
183 */
184static void adev_add_stream_to_list(
185 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
186 device_lock(adev);
187
188 list_add_tail(list, stream_node);
189
190 device_unlock(adev);
191}
192
193static void adev_remove_stream_from_list(
194 struct audio_device* adev, struct listnode* stream_node) {
195 device_lock(adev);
196
197 list_remove(stream_node);
198
199 device_unlock(adev);
200}
201
Eric Laurent70318092015-06-19 17:49:17 -0700202/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700203 * Extract the card and device numbers from the supplied key/value pairs.
204 * kvpairs A null-terminated string containing the key/value pairs or card and device.
205 * i.e. "card=1;device=42"
206 * card A pointer to a variable to receive the parsed-out card number.
207 * device A pointer to a variable to receive the parsed-out device number.
208 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
209 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800210 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700211 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800212static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700213{
214 struct str_parms * parms = str_parms_create_str(kvpairs);
215 char value[32];
216 int param_val;
217
218 // initialize to "undefined" state.
219 *card = -1;
220 *device = -1;
221
222 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
223 if (param_val >= 0) {
224 *card = atoi(value);
225 }
226
227 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
228 if (param_val >= 0) {
229 *device = atoi(value);
230 }
231
232 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800233
234 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700235}
236
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700237static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800238{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700239 if (profile->card < 0 || profile->device < 0) {
240 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800241 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700242
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700243 struct str_parms *query = str_parms_create_str(keys);
244 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700245
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700246 /* These keys are from hardware/libhardware/include/audio.h */
247 /* supported sample rates */
248 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
249 char* rates_list = profile_get_sample_rate_strs(profile);
250 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
251 rates_list);
252 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700253 }
254
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700255 /* supported channel counts */
256 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
257 char* channels_list = profile_get_channel_count_strs(profile);
258 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
259 channels_list);
260 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700261 }
262
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700263 /* supported sample formats */
264 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
265 char * format_params = profile_get_format_strs(profile);
266 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
267 format_params);
268 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700269 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700270 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700271
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700272 char* result_str = str_parms_to_str(result);
273 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700274
Paul McLean65ec72b2015-02-18 09:13:24 -0800275 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700276
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700277 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800278}
279
280/*
281 * HAl Functions
282 */
Simon Wilson19957a32012-04-06 16:17:12 -0700283/**
284 * NOTE: when multiple mutexes have to be acquired, always respect the
285 * following order: hw device > out stream
286 */
287
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700288/*
289 * OUT functions
290 */
Simon Wilson19957a32012-04-06 16:17:12 -0700291static uint32_t out_get_sample_rate(const struct audio_stream *stream)
292{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700293 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
294 ALOGV("out_get_sample_rate() = %d", rate);
295 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700296}
297
298static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
299{
300 return 0;
301}
302
303static size_t out_get_buffer_size(const struct audio_stream *stream)
304{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700305 const struct stream_out* out = (const struct stream_out*)stream;
306 size_t buffer_size =
307 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700308 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700309}
310
311static uint32_t out_get_channels(const struct audio_stream *stream)
312{
Andy Hung03576be2014-07-21 21:16:45 -0700313 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700314 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700315}
316
317static audio_format_t out_get_format(const struct audio_stream *stream)
318{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700319 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
320 * Relies on the framework to provide data in the specified format.
321 * This could change in the future.
322 */
323 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
324 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700325 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700326}
327
328static int out_set_format(struct audio_stream *stream, audio_format_t format)
329{
330 return 0;
331}
332
333static int out_standby(struct audio_stream *stream)
334{
335 struct stream_out *out = (struct stream_out *)stream;
336
Paul McLean994ac072016-06-02 15:33:24 -0600337 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700338 if (!out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600339 device_lock(out->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700340 proxy_close(&out->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600341 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700342 out->standby = true;
343 }
Paul McLean994ac072016-06-02 15:33:24 -0600344 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700345 return 0;
346}
347
Paul McLean6a75e4e2016-05-25 14:09:02 -0600348static int out_dump(const struct audio_stream *stream, int fd) {
349 const struct stream_out* out_stream = (const struct stream_out*) stream;
350
351 if (out_stream != NULL) {
352 dprintf(fd, "Output Profile:\n");
353 profile_dump(out_stream->profile, fd);
354
355 dprintf(fd, "Output Proxy:\n");
356 proxy_dump(&out_stream->proxy, fd);
357 }
358
Simon Wilson19957a32012-04-06 16:17:12 -0700359 return 0;
360}
361
362static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
363{
Paul McLean65ec72b2015-02-18 09:13:24 -0800364 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800365
Simon Wilson19957a32012-04-06 16:17:12 -0700366 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700367
Simon Wilson19957a32012-04-06 16:17:12 -0700368 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800369 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700370 int card = -1;
371 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700372
Paul McLean65ec72b2015-02-18 09:13:24 -0800373 if (!parse_card_device_params(kvpairs, &card, &device)) {
374 // nothing to do
375 return ret_value;
376 }
377
Paul McLean994ac072016-06-02 15:33:24 -0600378 stream_lock(&out->lock);
Paul McLean65ec72b2015-02-18 09:13:24 -0800379 /* Lock the device because that is where the profile lives */
Paul McLean994ac072016-06-02 15:33:24 -0600380 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700381
Paul McLean65ec72b2015-02-18 09:13:24 -0800382 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700383 /* cannot read pcm device info if playback is active */
384 if (!out->standby)
385 ret_value = -ENOSYS;
386 else {
387 int saved_card = out->profile->card;
388 int saved_device = out->profile->device;
389 out->profile->card = card;
390 out->profile->device = device;
391 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
392 if (ret_value != 0) {
393 out->profile->card = saved_card;
394 out->profile->device = saved_device;
395 }
396 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800397 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700398
Paul McLean994ac072016-06-02 15:33:24 -0600399 device_unlock(out->adev);
400 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700401
Paul McLeaneedc92e2013-12-19 15:46:15 -0800402 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700403}
404
Paul McLeanf62d75e2014-07-11 15:14:19 -0700405static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
406{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700407 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600408 stream_lock(&out->lock);
409 device_lock(out->adev);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700410
411 char * params_str = device_get_parameters(out->profile, keys);
412
Paul McLean994ac072016-06-02 15:33:24 -0600413 device_unlock(out->adev);
414 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700415 return params_str;
416}
417
Simon Wilson19957a32012-04-06 16:17:12 -0700418static uint32_t out_get_latency(const struct audio_stream_out *stream)
419{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700420 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
421 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700422}
423
Paul McLean30f41852014-04-16 15:44:20 -0700424static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700425{
426 return -ENOSYS;
427}
428
Paul McLeaneedc92e2013-12-19 15:46:15 -0800429/* must be called with hw device and output stream mutexes locked */
430static int start_output_stream(struct stream_out *out)
431{
Paul McLean65ec72b2015-02-18 09:13:24 -0800432 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800433
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700434 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800435}
436
437static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700438{
439 int ret;
440 struct stream_out *out = (struct stream_out *)stream;
441
Paul McLean994ac072016-06-02 15:33:24 -0600442 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700443 if (out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600444 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700445 ret = start_output_stream(out);
Paul McLean994ac072016-06-02 15:33:24 -0600446 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700447 if (ret != 0) {
448 goto err;
449 }
450 out->standby = false;
451 }
Eric Laurent05333d42014-08-04 20:29:17 -0700452
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700453 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700454 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800455 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700456 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
457 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700458 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700459 /* allocate buffer */
460 const size_t required_conversion_buffer_size =
461 bytes * num_device_channels / num_req_channels;
462 if (required_conversion_buffer_size > out->conversion_buffer_size) {
463 out->conversion_buffer_size = required_conversion_buffer_size;
464 out->conversion_buffer = realloc(out->conversion_buffer,
465 out->conversion_buffer_size);
466 }
467 /* convert data */
468 const audio_format_t audio_format = out_get_format(&(out->stream.common));
469 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800470 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700471 adjust_channels(write_buff, num_req_channels,
472 out->conversion_buffer, num_device_channels,
473 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800474 write_buff = out->conversion_buffer;
475 }
476
Paul McLeaneedc92e2013-12-19 15:46:15 -0800477 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700478 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800479 }
Simon Wilson19957a32012-04-06 16:17:12 -0700480
Paul McLean994ac072016-06-02 15:33:24 -0600481 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700482
483 return bytes;
484
485err:
Paul McLean994ac072016-06-02 15:33:24 -0600486 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700487 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700488 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700489 out_get_sample_rate(&stream->common));
490 }
491
492 return bytes;
493}
494
Paul McLean30f41852014-04-16 15:44:20 -0700495static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700496{
497 return -EINVAL;
498}
499
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700500static int out_get_presentation_position(const struct audio_stream_out *stream,
501 uint64_t *frames, struct timespec *timestamp)
502{
Andy Hungc9515ce2015-08-04 15:05:19 -0700503 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600504 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700505
506 const alsa_device_proxy *proxy = &out->proxy;
507 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
508
Paul McLean994ac072016-06-02 15:33:24 -0600509 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700510 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700511}
512
Simon Wilson19957a32012-04-06 16:17:12 -0700513static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
514{
515 return 0;
516}
517
518static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
519{
520 return 0;
521}
522
Paul McLean30f41852014-04-16 15:44:20 -0700523static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700524{
525 return -EINVAL;
526}
527
Paul McLean76dba682016-06-01 12:29:11 -0600528static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700529 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600530 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700531 audio_output_flags_t flags,
532 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700533 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700534 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700535{
Paul McLean76dba682016-06-01 12:29:11 -0600536 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
537 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800538
Simon Wilson19957a32012-04-06 16:17:12 -0700539 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700540
541 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600542 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700543 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600544 }
Simon Wilson19957a32012-04-06 16:17:12 -0700545
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700546 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700547 out->stream.common.get_sample_rate = out_get_sample_rate;
548 out->stream.common.set_sample_rate = out_set_sample_rate;
549 out->stream.common.get_buffer_size = out_get_buffer_size;
550 out->stream.common.get_channels = out_get_channels;
551 out->stream.common.get_format = out_get_format;
552 out->stream.common.set_format = out_set_format;
553 out->stream.common.standby = out_standby;
554 out->stream.common.dump = out_dump;
555 out->stream.common.set_parameters = out_set_parameters;
556 out->stream.common.get_parameters = out_get_parameters;
557 out->stream.common.add_audio_effect = out_add_audio_effect;
558 out->stream.common.remove_audio_effect = out_remove_audio_effect;
559 out->stream.get_latency = out_get_latency;
560 out->stream.set_volume = out_set_volume;
561 out->stream.write = out_write;
562 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700563 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700564 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
565
Paul McLean994ac072016-06-02 15:33:24 -0600566 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700567
Paul McLean76dba682016-06-01 12:29:11 -0600568 out->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600569 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600570 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700571
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700572 // build this to hand to the alsa_device_proxy
573 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700574 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800575
Paul McLean0f1753e2014-12-02 12:36:45 -0700576 /* Pull out the card/device pair */
577 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
578
579 profile_read_device_info(out->profile);
580
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700581 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800582
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700583 /* Rate */
584 if (config->sample_rate == 0) {
585 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
586 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
587 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800588 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700589 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
590 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800591 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800592
Paul McLean76dba682016-06-01 12:29:11 -0600593 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600594 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600595
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700596 /* Format */
597 if (config->format == AUDIO_FORMAT_DEFAULT) {
598 proxy_config.format = profile_get_default_format(out->profile);
599 config->format = audio_format_from_pcm_format(proxy_config.format);
600 } else {
601 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
602 if (profile_is_format_valid(out->profile, fmt)) {
603 proxy_config.format = fmt;
604 } else {
605 proxy_config.format = profile_get_default_format(out->profile);
606 config->format = audio_format_from_pcm_format(proxy_config.format);
607 ret = -EINVAL;
608 }
609 }
610
611 /* Channels */
Andy Hung182ddc72015-05-05 23:37:30 -0700612 unsigned proposed_channel_count = 0;
Andy Hung03576be2014-07-21 21:16:45 -0700613 if (k_force_channels) {
614 proposed_channel_count = k_force_channels;
Andy Hung182ddc72015-05-05 23:37:30 -0700615 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
616 proposed_channel_count = profile_get_default_channel_count(out->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700617 }
Paul McLean698dbd72015-11-03 12:24:30 -0800618
Andy Hung182ddc72015-05-05 23:37:30 -0700619 if (proposed_channel_count != 0) {
Andy Hung5e58a302015-06-10 15:17:00 -0700620 if (proposed_channel_count <= FCC_2) {
621 // use channel position mask for mono and stereo
622 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
623 } else {
624 // use channel index mask for multichannel
Andy Hung182ddc72015-05-05 23:37:30 -0700625 config->channel_mask =
626 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
Andy Hung5e58a302015-06-10 15:17:00 -0700627 }
Andy Hung182ddc72015-05-05 23:37:30 -0700628 } else {
Paul McLean698dbd72015-11-03 12:24:30 -0800629 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700630 }
Paul McLean698dbd72015-11-03 12:24:30 -0800631 out->hal_channel_count = proposed_channel_count;
632
Andy Hung182ddc72015-05-05 23:37:30 -0700633 /* we can expose any channel mask, and emulate internally based on channel count. */
634 out->hal_channel_mask = config->channel_mask;
635
Andy Hung03576be2014-07-21 21:16:45 -0700636 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
637 * and we emulate any channel count discrepancies in out_write(). */
Paul McLean698dbd72015-11-03 12:24:30 -0800638 proxy_config.channels = out->hal_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700639 proxy_prepare(&out->proxy, out->profile, &proxy_config);
640
641 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
642 ret = 0;
643
Paul McLeaneedc92e2013-12-19 15:46:15 -0800644 out->conversion_buffer = NULL;
645 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700646
647 out->standby = true;
648
Paul McLean6a75e4e2016-05-25 14:09:02 -0600649 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600650 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600651
Simon Wilson19957a32012-04-06 16:17:12 -0700652 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700653
654 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700655
656err_open:
657 free(out);
658 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800659 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700660}
661
Paul McLean76dba682016-06-01 12:29:11 -0600662static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700663 struct audio_stream_out *stream)
664{
665 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800666 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700667
Paul McLean76dba682016-06-01 12:29:11 -0600668 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600669
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700670 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700671 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800672
673 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700674
Paul McLeaneedc92e2013-12-19 15:46:15 -0800675 out->conversion_buffer = NULL;
676 out->conversion_buffer_size = 0;
677
Paul McLean994ac072016-06-02 15:33:24 -0600678 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600679 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600680 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600681
Simon Wilson19957a32012-04-06 16:17:12 -0700682 free(stream);
683}
684
Paul McLean76dba682016-06-01 12:29:11 -0600685static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700686 const struct audio_config *config)
687{
688 /* TODO This needs to be calculated based on format/channels/rate */
689 return 320;
690}
691
692/*
693 * IN functions
694 */
695static uint32_t in_get_sample_rate(const struct audio_stream *stream)
696{
697 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
698 ALOGV("in_get_sample_rate() = %d", rate);
699 return rate;
700}
701
702static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
703{
704 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
705 return -ENOSYS;
706}
707
708static size_t in_get_buffer_size(const struct audio_stream *stream)
709{
710 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700711 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700712}
713
714static uint32_t in_get_channels(const struct audio_stream *stream)
715{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700716 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700717 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700718}
719
720static audio_format_t in_get_format(const struct audio_stream *stream)
721{
Andy Hung780f1f82015-04-22 22:14:39 -0700722 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
723 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
724 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700725}
726
727static int in_set_format(struct audio_stream *stream, audio_format_t format)
728{
729 ALOGV("in_set_format(%d) - NOPE", format);
730
731 return -ENOSYS;
732}
733
734static int in_standby(struct audio_stream *stream)
735{
736 struct stream_in *in = (struct stream_in *)stream;
737
Paul McLean994ac072016-06-02 15:33:24 -0600738 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700739 if (!in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600740 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700741 proxy_close(&in->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600742 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700743 in->standby = true;
744 }
745
Paul McLean994ac072016-06-02 15:33:24 -0600746 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700747
748 return 0;
749}
750
751static int in_dump(const struct audio_stream *stream, int fd)
752{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600753 const struct stream_in* in_stream = (const struct stream_in*)stream;
754 if (in_stream != NULL) {
755 dprintf(fd, "Input Profile:\n");
756 profile_dump(in_stream->profile, fd);
757
758 dprintf(fd, "Input Proxy:\n");
759 proxy_dump(&in_stream->proxy, fd);
760 }
761
762 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700763}
764
765static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
766{
Paul McLean65ec72b2015-02-18 09:13:24 -0800767 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700768
769 struct stream_in *in = (struct stream_in *)stream;
770
771 char value[32];
772 int param_val;
773 int routing = 0;
774 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700775 int card = -1;
776 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700777
Paul McLean65ec72b2015-02-18 09:13:24 -0800778 if (!parse_card_device_params(kvpairs, &card, &device)) {
779 // nothing to do
780 return ret_value;
781 }
782
Paul McLean994ac072016-06-02 15:33:24 -0600783 stream_lock(&in->lock);
784 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700785
Paul McLean2c6196f2014-08-20 16:50:25 -0700786 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700787 /* cannot read pcm device info if playback is active */
788 if (!in->standby)
789 ret_value = -ENOSYS;
790 else {
791 int saved_card = in->profile->card;
792 int saved_device = in->profile->device;
793 in->profile->card = card;
794 in->profile->device = device;
795 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
796 if (ret_value != 0) {
797 in->profile->card = saved_card;
798 in->profile->device = saved_device;
799 }
800 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700801 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700802
Paul McLean994ac072016-06-02 15:33:24 -0600803 device_unlock(in->adev);
804 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700805
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700806 return ret_value;
807}
808
809static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
810{
811 struct stream_in *in = (struct stream_in *)stream;
812
Paul McLean994ac072016-06-02 15:33:24 -0600813 stream_lock(&in->lock);
814 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700815
816 char * params_str = device_get_parameters(in->profile, keys);
817
Paul McLean994ac072016-06-02 15:33:24 -0600818 device_unlock(in->adev);
819 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700820
821 return params_str;
822}
823
824static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
825{
826 return 0;
827}
828
829static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
830{
831 return 0;
832}
833
834static int in_set_gain(struct audio_stream_in *stream, float gain)
835{
836 return 0;
837}
838
839/* must be called with hw device and output stream mutexes locked */
840static int start_input_stream(struct stream_in *in)
841{
Paul McLean1d585cc2016-05-24 11:28:10 -0600842 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700843
844 return proxy_open(&in->proxy);
845}
846
847/* TODO mutex stuff here (see out_write) */
848static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
849{
850 size_t num_read_buff_bytes = 0;
851 void * read_buff = buffer;
852 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800853 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700854
855 struct stream_in * in = (struct stream_in *)stream;
856
Paul McLean994ac072016-06-02 15:33:24 -0600857 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700858 if (in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600859 device_lock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700860 ret = start_input_stream(in);
Paul McLean994ac072016-06-02 15:33:24 -0600861 device_unlock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700862 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700863 goto err;
864 }
865 in->standby = false;
866 }
867
868 alsa_device_profile * profile = in->profile;
869
870 /*
871 * OK, we need to figure out how much data to read to be able to output the requested
872 * number of bytes in the HAL format (16-bit, stereo).
873 */
874 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700875 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
876 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700877
878 if (num_device_channels != num_req_channels) {
879 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
880 }
881
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700882 /* Setup/Realloc the conversion buffer (if necessary). */
883 if (num_read_buff_bytes != bytes) {
884 if (num_read_buff_bytes > in->conversion_buffer_size) {
885 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
886 (and do these conversions themselves) */
887 in->conversion_buffer_size = num_read_buff_bytes;
888 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
889 }
890 read_buff = in->conversion_buffer;
891 }
892
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800893 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
894 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700895 if (num_device_channels != num_req_channels) {
896 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
897
898 out_buff = buffer;
899 /* Num Channels conversion */
900 if (num_device_channels != num_req_channels) {
901 audio_format_t audio_format = in_get_format(&(in->stream.common));
902 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
903
904 num_read_buff_bytes =
905 adjust_channels(read_buff, num_device_channels,
906 out_buff, num_req_channels,
907 sample_size_in_bytes, num_read_buff_bytes);
908 }
909 }
Eric Laurent253def92014-09-14 12:18:18 -0700910
Paul McLean76dba682016-06-01 12:29:11 -0600911 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
912 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700913 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530914 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800915 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700916 }
917
918err:
Paul McLean994ac072016-06-02 15:33:24 -0600919 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700920 return num_read_buff_bytes;
921}
922
923static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
924{
925 return 0;
926}
927
Paul McLean76dba682016-06-01 12:29:11 -0600928static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700929 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600930 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700931 struct audio_config *config,
932 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700933 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700934 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700935 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700936{
Paul McLean1d585cc2016-05-24 11:28:10 -0600937 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700938 config->sample_rate, config->channel_mask, config->format);
939
940 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
941 int ret = 0;
942
Paul McLean76dba682016-06-01 12:29:11 -0600943 if (in == NULL) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700944 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600945 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700946
947 /* setup function pointers */
948 in->stream.common.get_sample_rate = in_get_sample_rate;
949 in->stream.common.set_sample_rate = in_set_sample_rate;
950 in->stream.common.get_buffer_size = in_get_buffer_size;
951 in->stream.common.get_channels = in_get_channels;
952 in->stream.common.get_format = in_get_format;
953 in->stream.common.set_format = in_set_format;
954 in->stream.common.standby = in_standby;
955 in->stream.common.dump = in_dump;
956 in->stream.common.set_parameters = in_set_parameters;
957 in->stream.common.get_parameters = in_get_parameters;
958 in->stream.common.add_audio_effect = in_add_audio_effect;
959 in->stream.common.remove_audio_effect = in_remove_audio_effect;
960
961 in->stream.set_gain = in_set_gain;
962 in->stream.read = in_read;
963 in->stream.get_input_frames_lost = in_get_input_frames_lost;
964
Paul McLean994ac072016-06-02 15:33:24 -0600965 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700966
Paul McLean76dba682016-06-01 12:29:11 -0600967 in->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600968 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700969
Paul McLean76dba682016-06-01 12:29:11 -0600970 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700971
972 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700973 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700974
Paul McLean0f1753e2014-12-02 12:36:45 -0700975 /* Pull out the card/device pair */
976 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
977
978 profile_read_device_info(in->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700979
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700980 /* Rate */
981 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -0600982 config->sample_rate = profile_get_default_sample_rate(in->profile);
983 }
984
Paul McLean76dba682016-06-01 12:29:11 -0600985 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */
986 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
987 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0;
988 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700989 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Paul McLean76dba682016-06-01 12:29:11 -0600990 in->adev->device_sample_rate = proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700991 } else {
992 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
993 ret = -EINVAL;
994 }
Paul McLean994ac072016-06-02 15:33:24 -0600995 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700996
997 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700998 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -0700999 proxy_config.format = profile_get_default_format(in->profile);
1000 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001001 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001002 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1003 if (profile_is_format_valid(in->profile, fmt)) {
1004 proxy_config.format = fmt;
1005 } else {
1006 proxy_config.format = profile_get_default_format(in->profile);
1007 config->format = audio_format_from_pcm_format(proxy_config.format);
1008 ret = -EINVAL;
1009 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001010 }
1011
Paul McLean2cfd81b2014-09-15 12:32:23 -07001012 /* Channels */
Andy Hung780f1f82015-04-22 22:14:39 -07001013 unsigned proposed_channel_count = 0;
Paul McLean2cfd81b2014-09-15 12:32:23 -07001014 if (k_force_channels) {
1015 proposed_channel_count = k_force_channels;
Andy Hung780f1f82015-04-22 22:14:39 -07001016 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1017 proposed_channel_count = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001018 }
Andy Hung780f1f82015-04-22 22:14:39 -07001019 if (proposed_channel_count != 0) {
1020 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
1021 if (config->channel_mask == AUDIO_CHANNEL_INVALID)
1022 config->channel_mask =
1023 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
1024 in->hal_channel_count = proposed_channel_count;
1025 } else {
1026 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1027 }
1028 /* we can expose any channel mask, and emulate internally based on channel count. */
1029 in->hal_channel_mask = config->channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001030
Paul McLean2cfd81b2014-09-15 12:32:23 -07001031 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001032 proxy_prepare(&in->proxy, in->profile, &proxy_config);
1033
1034 in->standby = true;
1035
Paul McLean6a75e4e2016-05-25 14:09:02 -06001036 /* Save this for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -06001037 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001038
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001039 in->conversion_buffer = NULL;
1040 in->conversion_buffer_size = 0;
1041
1042 *stream_in = &in->stream;
1043
1044 return ret;
1045}
1046
Paul McLean76dba682016-06-01 12:29:11 -06001047static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1048 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001049{
1050 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001051 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1052
Paul McLean76dba682016-06-01 12:29:11 -06001053 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001054
1055 /* Close the pcm device */
1056 in_standby(&stream->common);
1057
1058 free(in->conversion_buffer);
1059
1060 free(stream);
1061}
1062
1063/*
1064 * ADEV Functions
1065 */
Paul McLean76dba682016-06-01 12:29:11 -06001066static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001067{
1068 return 0;
1069}
1070
Paul McLean76dba682016-06-01 12:29:11 -06001071static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001072{
1073 return strdup("");
1074}
1075
Paul McLean76dba682016-06-01 12:29:11 -06001076static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001077{
1078 return 0;
1079}
1080
Paul McLean76dba682016-06-01 12:29:11 -06001081static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001082{
1083 return -ENOSYS;
1084}
1085
Paul McLean76dba682016-06-01 12:29:11 -06001086static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001087{
1088 return -ENOSYS;
1089}
1090
Paul McLean76dba682016-06-01 12:29:11 -06001091static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001092{
1093 return 0;
1094}
1095
Paul McLean76dba682016-06-01 12:29:11 -06001096static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001097{
Paul McLean76dba682016-06-01 12:29:11 -06001098 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001099 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001100 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001101 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001102 return -ENOSYS;
1103}
1104
Paul McLean76dba682016-06-01 12:29:11 -06001105static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001106{
1107 return -ENOSYS;
1108}
1109
Paul McLean76dba682016-06-01 12:29:11 -06001110static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001111{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001112 dprintf(fd, "\nUSB audio module:\n");
1113
1114 struct audio_device* adev = (struct audio_device*)device;
1115 const int kNumRetries = 3;
1116 const int kSleepTimeMS = 500;
1117
Paul McLean994ac072016-06-02 15:33:24 -06001118 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001119 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001120 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001121 sleep(kSleepTimeMS);
1122 retry--;
1123 }
1124
1125 if (retry > 0) {
1126 if (list_empty(&adev->output_stream_list)) {
1127 dprintf(fd, " No output streams.\n");
1128 } else {
1129 struct listnode* node;
1130 list_for_each(node, &adev->output_stream_list) {
1131 struct audio_stream* stream =
1132 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1133 out_dump(stream, fd);
1134 }
1135 }
1136
1137 if (list_empty(&adev->input_stream_list)) {
1138 dprintf(fd, "\n No input streams.\n");
1139 } else {
1140 struct listnode* node;
1141 list_for_each(node, &adev->input_stream_list) {
1142 struct audio_stream* stream =
1143 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1144 in_dump(stream, fd);
1145 }
1146 }
1147
Paul McLean994ac072016-06-02 15:33:24 -06001148 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001149 } else {
1150 // Couldn't lock
1151 dprintf(fd, " Could not obtain device lock.\n");
1152 }
1153
Simon Wilson19957a32012-04-06 16:17:12 -07001154 return 0;
1155}
1156
1157static int adev_close(hw_device_t *device)
1158{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001159 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001160 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001161
Simon Wilson19957a32012-04-06 16:17:12 -07001162 return 0;
1163}
1164
Paul McLean30f41852014-04-16 15:44:20 -07001165static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001166{
Simon Wilson19957a32012-04-06 16:17:12 -07001167 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1168 return -EINVAL;
1169
Paul McLeaneedc92e2013-12-19 15:46:15 -08001170 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001171 if (!adev)
1172 return -ENOMEM;
1173
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001174 profile_init(&adev->out_profile, PCM_OUT);
1175 profile_init(&adev->in_profile, PCM_IN);
1176
Paul McLean6a75e4e2016-05-25 14:09:02 -06001177 list_init(&adev->output_stream_list);
1178 list_init(&adev->input_stream_list);
1179
Simon Wilson19957a32012-04-06 16:17:12 -07001180 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001181 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001182 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001183 adev->hw_device.common.close = adev_close;
1184
Simon Wilson19957a32012-04-06 16:17:12 -07001185 adev->hw_device.init_check = adev_init_check;
1186 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1187 adev->hw_device.set_master_volume = adev_set_master_volume;
1188 adev->hw_device.set_mode = adev_set_mode;
1189 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1190 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1191 adev->hw_device.set_parameters = adev_set_parameters;
1192 adev->hw_device.get_parameters = adev_get_parameters;
1193 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1194 adev->hw_device.open_output_stream = adev_open_output_stream;
1195 adev->hw_device.close_output_stream = adev_close_output_stream;
1196 adev->hw_device.open_input_stream = adev_open_input_stream;
1197 adev->hw_device.close_input_stream = adev_close_input_stream;
1198 adev->hw_device.dump = adev_dump;
1199
1200 *device = &adev->hw_device.common;
1201
1202 return 0;
1203}
1204
1205static struct hw_module_methods_t hal_module_methods = {
1206 .open = adev_open,
1207};
1208
1209struct audio_module HAL_MODULE_INFO_SYM = {
1210 .common = {
1211 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001212 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1213 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001214 .id = AUDIO_HARDWARE_MODULE_ID,
1215 .name = "USB audio HW HAL",
1216 .author = "The Android Open Source Project",
1217 .methods = &hal_module_methods,
1218 },
1219};