blob: 7d82b37038f07d2913da8947d613435d0c5d21d7 [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>
Simon Wilson19957a32012-04-06 16:17:12 -070028#include <cutils/str_parms.h>
29#include <cutils/properties.h>
30
Simon Wilson19957a32012-04-06 16:17:12 -070031#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070032#include <hardware/audio_alsaops.h>
33#include <hardware/hardware.h>
34
35#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070036
37#include <tinyalsa/asoundlib.h>
38
Paul McLeanc2201152014-07-16 13:46:07 -070039#include <audio_utils/channels.h>
40
Andy Hung03576be2014-07-21 21:16:45 -070041/* FOR TESTING:
42 * Set k_force_channels to force the number of channels to present to AudioFlinger.
43 * 0 disables (this is default: present the device channels to AudioFlinger).
44 * 2 forces to legacy stereo mode.
45 *
46 * Others values can be tried (up to 8).
47 * TODO: AudioFlinger cannot support more than 8 active output channels
48 * at this time, so limiting logic needs to be put here or communicated from above.
49 */
50static const unsigned k_force_channels = 0;
51
Paul McLeanc88e6ae2014-07-16 09:48:34 -070052#include "alsa_device_profile.h"
53#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080054#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070055
Paul McLeanc88e6ae2014-07-16 09:48:34 -070056#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070057
Simon Wilson19957a32012-04-06 16:17:12 -070058struct audio_device {
59 struct audio_hw_device hw_device;
60
61 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080062
63 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070064 alsa_device_profile out_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080065
66 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070067 alsa_device_profile in_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080068
Eric Laurent253def92014-09-14 12:18:18 -070069 bool mic_muted;
70
Simon Wilson19957a32012-04-06 16:17:12 -070071 bool standby;
72};
73
74struct stream_out {
75 struct audio_stream_out stream;
76
Paul McLeaneedc92e2013-12-19 15:46:15 -080077 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080078 bool standby;
79
Paul McLeanc88e6ae2014-07-16 09:48:34 -070080 struct audio_device *dev; /* hardware information - only using this for the lock */
81
Paul McLean65ec72b2015-02-18 09:13:24 -080082 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070083 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080084
Andy Hung03576be2014-07-21 21:16:45 -070085 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
86 * This may differ from the device channel count when
87 * the device is not compatible with AudioFlinger
88 * capabilities, e.g. exposes too many channels or
89 * too few channels. */
Paul McLeaneedc92e2013-12-19 15:46:15 -080090 void * conversion_buffer; /* any conversions are put into here
91 * they could come from here too if
92 * there was a previous conversion */
93 size_t conversion_buffer_size; /* in bytes */
94};
95
Paul McLeaneedc92e2013-12-19 15:46:15 -080096struct stream_in {
97 struct audio_stream_in stream;
98
Simon Wilson19957a32012-04-06 16:17:12 -070099 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Simon Wilson19957a32012-04-06 16:17:12 -0700100 bool standby;
101
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700102 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800103
Paul McLean65ec72b2015-02-18 09:13:24 -0800104 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700105 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700106
Paul McLean2cfd81b2014-09-15 12:32:23 -0700107 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
108 * This may differ from the device channel count when
109 * the device is not compatible with AudioFlinger
110 * capabilities, e.g. exposes too many channels or
111 * too few channels. */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700112 /* 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 -0700113 void * conversion_buffer; /* any conversions are put into here
114 * they could come from here too if
115 * there was a previous conversion */
116 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700117};
118
Paul McLeaneedc92e2013-12-19 15:46:15 -0800119/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800120 * Data Conversions
121 */
122/*
Paul McLean30f41852014-04-16 15:44:20 -0700123 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
124 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800125 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700126 * out_buff points to the buffer to receive converted PCM16LE LE samples.
127 * returns
128 * the number of BYTES of output data.
129 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
130 * support PCM24_3LE (24-bit, packed).
131 * NOTE:
Paul McLean30f41852014-04-16 15:44:20 -0700132 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700133 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700134 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700135static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
136 short * out_buff)
137{
Paul McLean30f41852014-04-16 15:44:20 -0700138 /*
139 * Move from front to back so that the conversion can be done in-place
140 * i.e. in_buff == out_buff
141 */
142 /* we need 2 bytes in the output for every 3 bytes in the input */
143 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700144 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700145 size_t src_smpl_index;
146 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
147 src_ptr++; /* lowest-(skip)-byte */
148 *dst_ptr++ = *src_ptr++; /* low-byte */
149 *dst_ptr++ = *src_ptr++; /* high-byte */
150 }
151
152 /* return number of *bytes* generated: */
153 return num_in_samples * 2;
154}
155
156/*
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700157 * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
158 * in_buff points to the buffer of PCM32 samples
159 * num_in_samples size of input buffer in SAMPLES
160 * out_buff points to the buffer to receive converted PCM16LE LE samples.
161 * returns
162 * the number of BYTES of output data.
163 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
164 * support PCM_FORMAT_S32_LE (32-bit).
165 * NOTE:
166 * This conversion is safe to do in-place (in_buff == out_buff).
167 * TODO Move this to a utilities module.
168 */
169static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
170{
171 /*
172 * Move from front to back so that the conversion can be done in-place
173 * i.e. in_buff == out_buff
174 */
175
176 short * dst_ptr = out_buff;
177 const int32_t* src_ptr = in_buff;
178 size_t src_smpl_index;
179 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
180 *dst_ptr++ = *src_ptr++ >> 16;
181 }
182
183 /* return number of *bytes* generated: */
184 return num_in_samples * 2;
185}
186
Paul McLean0f1753e2014-12-02 12:36:45 -0700187/*
188 * Extract the card and device numbers from the supplied key/value pairs.
189 * kvpairs A null-terminated string containing the key/value pairs or card and device.
190 * i.e. "card=1;device=42"
191 * card A pointer to a variable to receive the parsed-out card number.
192 * device A pointer to a variable to receive the parsed-out device number.
193 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
194 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800195 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700196 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800197static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700198{
199 struct str_parms * parms = str_parms_create_str(kvpairs);
200 char value[32];
201 int param_val;
202
203 // initialize to "undefined" state.
204 *card = -1;
205 *device = -1;
206
207 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
208 if (param_val >= 0) {
209 *card = atoi(value);
210 }
211
212 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
213 if (param_val >= 0) {
214 *device = atoi(value);
215 }
216
217 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800218
219 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700220}
221
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700222static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800223{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700224 if (profile->card < 0 || profile->device < 0) {
225 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800226 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700227
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700228 struct str_parms *query = str_parms_create_str(keys);
229 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700230
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700231 /* These keys are from hardware/libhardware/include/audio.h */
232 /* supported sample rates */
233 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
234 char* rates_list = profile_get_sample_rate_strs(profile);
235 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
236 rates_list);
237 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700238 }
239
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700240 /* supported channel counts */
241 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
242 char* channels_list = profile_get_channel_count_strs(profile);
243 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
244 channels_list);
245 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700246 }
247
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700248 /* supported sample formats */
249 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
250 char * format_params = profile_get_format_strs(profile);
251 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
252 format_params);
253 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700254 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700255 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700256
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700257 char* result_str = str_parms_to_str(result);
258 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700259
Paul McLean65ec72b2015-02-18 09:13:24 -0800260 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700261
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700262 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800263}
264
265/*
266 * HAl Functions
267 */
Simon Wilson19957a32012-04-06 16:17:12 -0700268/**
269 * NOTE: when multiple mutexes have to be acquired, always respect the
270 * following order: hw device > out stream
271 */
272
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700273/*
274 * OUT functions
275 */
Simon Wilson19957a32012-04-06 16:17:12 -0700276static uint32_t out_get_sample_rate(const struct audio_stream *stream)
277{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700278 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
279 ALOGV("out_get_sample_rate() = %d", rate);
280 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700281}
282
283static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
284{
285 return 0;
286}
287
288static size_t out_get_buffer_size(const struct audio_stream *stream)
289{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700290 const struct stream_out* out = (const struct stream_out*)stream;
291 size_t buffer_size =
292 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700293 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700294}
295
296static uint32_t out_get_channels(const struct audio_stream *stream)
297{
Andy Hung03576be2014-07-21 21:16:45 -0700298 const struct stream_out *out = (const struct stream_out*)stream;
299 return audio_channel_out_mask_from_count(out->hal_channel_count);
Simon Wilson19957a32012-04-06 16:17:12 -0700300}
301
302static audio_format_t out_get_format(const struct audio_stream *stream)
303{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700304 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
305 * Relies on the framework to provide data in the specified format.
306 * This could change in the future.
307 */
308 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
309 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700310 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700311}
312
313static int out_set_format(struct audio_stream *stream, audio_format_t format)
314{
315 return 0;
316}
317
318static int out_standby(struct audio_stream *stream)
319{
320 struct stream_out *out = (struct stream_out *)stream;
321
322 pthread_mutex_lock(&out->dev->lock);
323 pthread_mutex_lock(&out->lock);
324
325 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700326 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700327 out->standby = true;
328 }
329
330 pthread_mutex_unlock(&out->lock);
331 pthread_mutex_unlock(&out->dev->lock);
332
333 return 0;
334}
335
336static int out_dump(const struct audio_stream *stream, int fd)
337{
338 return 0;
339}
340
341static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
342{
Paul McLean65ec72b2015-02-18 09:13:24 -0800343 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800344
Simon Wilson19957a32012-04-06 16:17:12 -0700345 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700346
Simon Wilson19957a32012-04-06 16:17:12 -0700347 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800348 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700349 int card = -1;
350 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700351
Paul McLean65ec72b2015-02-18 09:13:24 -0800352 if (!parse_card_device_params(kvpairs, &card, &device)) {
353 // nothing to do
354 return ret_value;
355 }
356
357 /* Lock the device because that is where the profile lives */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700358 pthread_mutex_lock(&out->dev->lock);
359 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700360
Paul McLean65ec72b2015-02-18 09:13:24 -0800361 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700362 /* cannot read pcm device info if playback is active */
363 if (!out->standby)
364 ret_value = -ENOSYS;
365 else {
366 int saved_card = out->profile->card;
367 int saved_device = out->profile->device;
368 out->profile->card = card;
369 out->profile->device = device;
370 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
371 if (ret_value != 0) {
372 out->profile->card = saved_card;
373 out->profile->device = saved_device;
374 }
375 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800376 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700377
Paul McLeanf62d75e2014-07-11 15:14:19 -0700378 pthread_mutex_unlock(&out->lock);
379 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700380
Paul McLeaneedc92e2013-12-19 15:46:15 -0800381 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700382}
383
Paul McLeanf62d75e2014-07-11 15:14:19 -0700384static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
385{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700386 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700387 pthread_mutex_lock(&out->dev->lock);
388 pthread_mutex_lock(&out->lock);
389
390 char * params_str = device_get_parameters(out->profile, keys);
391
392 pthread_mutex_unlock(&out->lock);
393 pthread_mutex_unlock(&out->dev->lock);
394
395 return params_str;
396}
397
Simon Wilson19957a32012-04-06 16:17:12 -0700398static uint32_t out_get_latency(const struct audio_stream_out *stream)
399{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700400 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
401 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700402}
403
Paul McLean30f41852014-04-16 15:44:20 -0700404static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700405{
406 return -ENOSYS;
407}
408
Paul McLeaneedc92e2013-12-19 15:46:15 -0800409/* must be called with hw device and output stream mutexes locked */
410static int start_output_stream(struct stream_out *out)
411{
Paul McLean65ec72b2015-02-18 09:13:24 -0800412 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800413
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700414 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800415}
416
417static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700418{
419 int ret;
420 struct stream_out *out = (struct stream_out *)stream;
421
422 pthread_mutex_lock(&out->dev->lock);
423 pthread_mutex_lock(&out->lock);
424 if (out->standby) {
425 ret = start_output_stream(out);
426 if (ret != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700427 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700428 goto err;
429 }
430 out->standby = false;
431 }
Eric Laurent05333d42014-08-04 20:29:17 -0700432 pthread_mutex_unlock(&out->dev->lock);
433
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700434 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700435 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800436 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700437 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
438 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700439 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700440 /* allocate buffer */
441 const size_t required_conversion_buffer_size =
442 bytes * num_device_channels / num_req_channels;
443 if (required_conversion_buffer_size > out->conversion_buffer_size) {
444 out->conversion_buffer_size = required_conversion_buffer_size;
445 out->conversion_buffer = realloc(out->conversion_buffer,
446 out->conversion_buffer_size);
447 }
448 /* convert data */
449 const audio_format_t audio_format = out_get_format(&(out->stream.common));
450 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800451 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700452 adjust_channels(write_buff, num_req_channels,
453 out->conversion_buffer, num_device_channels,
454 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800455 write_buff = out->conversion_buffer;
456 }
457
Paul McLeaneedc92e2013-12-19 15:46:15 -0800458 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700459 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800460 }
Simon Wilson19957a32012-04-06 16:17:12 -0700461
462 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700463
464 return bytes;
465
466err:
467 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700468 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700469 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700470 out_get_sample_rate(&stream->common));
471 }
472
473 return bytes;
474}
475
Paul McLean30f41852014-04-16 15:44:20 -0700476static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700477{
478 return -EINVAL;
479}
480
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700481static int out_get_presentation_position(const struct audio_stream_out *stream,
482 uint64_t *frames, struct timespec *timestamp)
483{
484 /* FIXME - This needs to be implemented */
485 return -EINVAL;
486}
487
Simon Wilson19957a32012-04-06 16:17:12 -0700488static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
489{
490 return 0;
491}
492
493static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
494{
495 return 0;
496}
497
Paul McLean30f41852014-04-16 15:44:20 -0700498static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700499{
500 return -EINVAL;
501}
502
503static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700504 audio_io_handle_t handle,
505 audio_devices_t devices,
506 audio_output_flags_t flags,
507 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700508 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700509 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700510{
Paul McLean65ec72b2015-02-18 09:13:24 -0800511 ALOGV("adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X, addr:%s",
Paul McLean0f1753e2014-12-02 12:36:45 -0700512 handle, devices, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800513
Simon Wilson19957a32012-04-06 16:17:12 -0700514 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800515
Simon Wilson19957a32012-04-06 16:17:12 -0700516 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700517
518 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
519 if (!out)
520 return -ENOMEM;
521
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700522 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700523 out->stream.common.get_sample_rate = out_get_sample_rate;
524 out->stream.common.set_sample_rate = out_set_sample_rate;
525 out->stream.common.get_buffer_size = out_get_buffer_size;
526 out->stream.common.get_channels = out_get_channels;
527 out->stream.common.get_format = out_get_format;
528 out->stream.common.set_format = out_set_format;
529 out->stream.common.standby = out_standby;
530 out->stream.common.dump = out_dump;
531 out->stream.common.set_parameters = out_set_parameters;
532 out->stream.common.get_parameters = out_get_parameters;
533 out->stream.common.add_audio_effect = out_add_audio_effect;
534 out->stream.common.remove_audio_effect = out_remove_audio_effect;
535 out->stream.get_latency = out_get_latency;
536 out->stream.set_volume = out_set_volume;
537 out->stream.write = out_write;
538 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700539 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700540 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
541
542 out->dev = adev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700543 pthread_mutex_lock(&adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700544 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700545
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700546 // build this to hand to the alsa_device_proxy
547 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700548 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800549
Paul McLean0f1753e2014-12-02 12:36:45 -0700550 /* Pull out the card/device pair */
551 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
552
553 profile_read_device_info(out->profile);
554
555 pthread_mutex_unlock(&adev->lock);
556
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700557 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800558
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700559 /* Rate */
560 if (config->sample_rate == 0) {
561 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
562 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
563 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800564 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700565 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
566 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800567 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800568
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700569 /* Format */
570 if (config->format == AUDIO_FORMAT_DEFAULT) {
571 proxy_config.format = profile_get_default_format(out->profile);
572 config->format = audio_format_from_pcm_format(proxy_config.format);
573 } else {
574 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
575 if (profile_is_format_valid(out->profile, fmt)) {
576 proxy_config.format = fmt;
577 } else {
578 proxy_config.format = profile_get_default_format(out->profile);
579 config->format = audio_format_from_pcm_format(proxy_config.format);
580 ret = -EINVAL;
581 }
582 }
583
584 /* Channels */
Andy Hung03576be2014-07-21 21:16:45 -0700585 unsigned proposed_channel_count = profile_get_default_channel_count(out->profile);
586 if (k_force_channels) {
587 proposed_channel_count = k_force_channels;
588 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
589 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700590 }
Andy Hung03576be2014-07-21 21:16:45 -0700591 /* we can expose any channel count mask, and emulate internally. */
592 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
593 out->hal_channel_count = proposed_channel_count;
594 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
595 * and we emulate any channel count discrepancies in out_write(). */
596 proxy_config.channels = proposed_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700597
598 proxy_prepare(&out->proxy, out->profile, &proxy_config);
599
600 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
601 ret = 0;
602
Paul McLeaneedc92e2013-12-19 15:46:15 -0800603 out->conversion_buffer = NULL;
604 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700605
606 out->standby = true;
607
608 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700609
610 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700611
612err_open:
613 free(out);
614 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800615 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700616}
617
618static void adev_close_output_stream(struct audio_hw_device *dev,
619 struct audio_stream_out *stream)
620{
621 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800622 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700623
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700624 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700625 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800626
627 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700628
Paul McLeaneedc92e2013-12-19 15:46:15 -0800629 out->conversion_buffer = NULL;
630 out->conversion_buffer_size = 0;
631
Simon Wilson19957a32012-04-06 16:17:12 -0700632 free(stream);
633}
634
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700635static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
636 const struct audio_config *config)
637{
638 /* TODO This needs to be calculated based on format/channels/rate */
639 return 320;
640}
641
642/*
643 * IN functions
644 */
645static uint32_t in_get_sample_rate(const struct audio_stream *stream)
646{
647 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
648 ALOGV("in_get_sample_rate() = %d", rate);
649 return rate;
650}
651
652static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
653{
654 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
655 return -ENOSYS;
656}
657
658static size_t in_get_buffer_size(const struct audio_stream *stream)
659{
660 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700661 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700662}
663
664static uint32_t in_get_channels(const struct audio_stream *stream)
665{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700666 const struct stream_in *in = (const struct stream_in*)stream;
667 return audio_channel_in_mask_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700668}
669
670static audio_format_t in_get_format(const struct audio_stream *stream)
671{
672 /* TODO Here is the code we need when we support arbitrary input formats
673 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
674 * audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
675 * ALOGV("in_get_format() = %d", format);
676 * return format;
677 */
678 /* Input only supports PCM16 */
679 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary input formats
680 rewrite this to return the ACTUAL channel format (above) */
681 return AUDIO_FORMAT_PCM_16_BIT;
682}
683
684static int in_set_format(struct audio_stream *stream, audio_format_t format)
685{
686 ALOGV("in_set_format(%d) - NOPE", format);
687
688 return -ENOSYS;
689}
690
691static int in_standby(struct audio_stream *stream)
692{
693 struct stream_in *in = (struct stream_in *)stream;
694
695 pthread_mutex_lock(&in->dev->lock);
696 pthread_mutex_lock(&in->lock);
697
698 if (!in->standby) {
699 proxy_close(&in->proxy);
700 in->standby = true;
701 }
702
703 pthread_mutex_unlock(&in->lock);
704 pthread_mutex_unlock(&in->dev->lock);
705
706 return 0;
707}
708
709static int in_dump(const struct audio_stream *stream, int fd)
710{
711 return 0;
712}
713
714static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
715{
Paul McLean65ec72b2015-02-18 09:13:24 -0800716 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700717
718 struct stream_in *in = (struct stream_in *)stream;
719
720 char value[32];
721 int param_val;
722 int routing = 0;
723 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700724 int card = -1;
725 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700726
Paul McLean65ec72b2015-02-18 09:13:24 -0800727 if (!parse_card_device_params(kvpairs, &card, &device)) {
728 // nothing to do
729 return ret_value;
730 }
731
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700732 pthread_mutex_lock(&in->dev->lock);
733 pthread_mutex_lock(&in->lock);
734
Paul McLean2c6196f2014-08-20 16:50:25 -0700735 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700736 /* cannot read pcm device info if playback is active */
737 if (!in->standby)
738 ret_value = -ENOSYS;
739 else {
740 int saved_card = in->profile->card;
741 int saved_device = in->profile->device;
742 in->profile->card = card;
743 in->profile->device = device;
744 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
745 if (ret_value != 0) {
746 in->profile->card = saved_card;
747 in->profile->device = saved_device;
748 }
749 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700750 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700751
752 pthread_mutex_unlock(&in->lock);
753 pthread_mutex_unlock(&in->dev->lock);
754
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700755 return ret_value;
756}
757
758static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
759{
760 struct stream_in *in = (struct stream_in *)stream;
761
762 pthread_mutex_lock(&in->dev->lock);
763 pthread_mutex_lock(&in->lock);
764
765 char * params_str = device_get_parameters(in->profile, keys);
766
767 pthread_mutex_unlock(&in->lock);
768 pthread_mutex_unlock(&in->dev->lock);
769
770 return params_str;
771}
772
773static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
774{
775 return 0;
776}
777
778static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
779{
780 return 0;
781}
782
783static int in_set_gain(struct audio_stream_in *stream, float gain)
784{
785 return 0;
786}
787
788/* must be called with hw device and output stream mutexes locked */
789static int start_input_stream(struct stream_in *in)
790{
Paul McLean65ec72b2015-02-18 09:13:24 -0800791 ALOGV("ustart_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700792
793 return proxy_open(&in->proxy);
794}
795
796/* TODO mutex stuff here (see out_write) */
797static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
798{
799 size_t num_read_buff_bytes = 0;
800 void * read_buff = buffer;
801 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800802 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700803
804 struct stream_in * in = (struct stream_in *)stream;
805
806 pthread_mutex_lock(&in->dev->lock);
807 pthread_mutex_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700808 if (in->standby) {
809 if (start_input_stream(in) != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700810 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700811 goto err;
812 }
813 in->standby = false;
814 }
Eric Laurent05333d42014-08-04 20:29:17 -0700815 pthread_mutex_unlock(&in->dev->lock);
816
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700817
818 alsa_device_profile * profile = in->profile;
819
820 /*
821 * OK, we need to figure out how much data to read to be able to output the requested
822 * number of bytes in the HAL format (16-bit, stereo).
823 */
824 num_read_buff_bytes = bytes;
825 int num_device_channels = proxy_get_channel_count(&in->proxy);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700826 int num_req_channels = in->hal_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700827
828 if (num_device_channels != num_req_channels) {
829 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
830 }
831
832 enum pcm_format format = proxy_get_format(&in->proxy);
833 if (format == PCM_FORMAT_S24_3LE) {
834 /* 24-bit USB device */
835 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
836 } else if (format == PCM_FORMAT_S32_LE) {
837 /* 32-bit USB device */
838 num_read_buff_bytes = num_read_buff_bytes * 2;
839 }
840
841 /* Setup/Realloc the conversion buffer (if necessary). */
842 if (num_read_buff_bytes != bytes) {
843 if (num_read_buff_bytes > in->conversion_buffer_size) {
844 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
845 (and do these conversions themselves) */
846 in->conversion_buffer_size = num_read_buff_bytes;
847 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
848 }
849 read_buff = in->conversion_buffer;
850 }
851
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800852 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
853 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700854 /*
855 * Do any conversions necessary to send the data in the format specified to/by the HAL
856 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
857 */
858 if (format != PCM_FORMAT_S16_LE) {
859 /* we need to convert */
860 if (num_device_channels != num_req_channels) {
861 out_buff = read_buff;
862 }
863
864 if (format == PCM_FORMAT_S24_3LE) {
865 num_read_buff_bytes =
866 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
867 } else if (format == PCM_FORMAT_S32_LE) {
868 num_read_buff_bytes =
869 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
870 } else {
Viswanath L8c7e1112014-10-31 13:07:39 +0530871 LOG_ALWAYS_FATAL("Unsupported format");
872 num_read_buff_bytes = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700873 goto err;
874 }
875 }
876
877 if (num_device_channels != num_req_channels) {
878 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
879
880 out_buff = buffer;
881 /* Num Channels conversion */
882 if (num_device_channels != num_req_channels) {
883 audio_format_t audio_format = in_get_format(&(in->stream.common));
884 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
885
886 num_read_buff_bytes =
887 adjust_channels(read_buff, num_device_channels,
888 out_buff, num_req_channels,
889 sample_size_in_bytes, num_read_buff_bytes);
890 }
891 }
Eric Laurent253def92014-09-14 12:18:18 -0700892
893 /* no need to acquire in->dev->lock to read mic_muted here as we don't change its state */
894 if (num_read_buff_bytes > 0 && in->dev->mic_muted)
895 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530896 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800897 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700898 }
899
900err:
901 pthread_mutex_unlock(&in->lock);
902
903 return num_read_buff_bytes;
904}
905
906static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
907{
908 return 0;
909}
910
911static int adev_open_input_stream(struct audio_hw_device *dev,
912 audio_io_handle_t handle,
913 audio_devices_t devices,
914 struct audio_config *config,
915 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700916 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700917 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700918 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700919{
Paul McLean65ec72b2015-02-18 09:13:24 -0800920 ALOGV("in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700921 config->sample_rate, config->channel_mask, config->format);
922
923 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
924 int ret = 0;
925
926 if (in == NULL)
927 return -ENOMEM;
928
929 /* setup function pointers */
930 in->stream.common.get_sample_rate = in_get_sample_rate;
931 in->stream.common.set_sample_rate = in_set_sample_rate;
932 in->stream.common.get_buffer_size = in_get_buffer_size;
933 in->stream.common.get_channels = in_get_channels;
934 in->stream.common.get_format = in_get_format;
935 in->stream.common.set_format = in_set_format;
936 in->stream.common.standby = in_standby;
937 in->stream.common.dump = in_dump;
938 in->stream.common.set_parameters = in_set_parameters;
939 in->stream.common.get_parameters = in_get_parameters;
940 in->stream.common.add_audio_effect = in_add_audio_effect;
941 in->stream.common.remove_audio_effect = in_remove_audio_effect;
942
943 in->stream.set_gain = in_set_gain;
944 in->stream.read = in_read;
945 in->stream.get_input_frames_lost = in_get_input_frames_lost;
946
947 in->dev = (struct audio_device *)dev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700948 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700949
950 in->profile = &in->dev->in_profile;
951
952 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700953 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700954
Paul McLean0f1753e2014-12-02 12:36:45 -0700955 /* Pull out the card/device pair */
956 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
957
958 profile_read_device_info(in->profile);
959 pthread_mutex_unlock(&in->dev->lock);
960
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700961 /* Rate */
962 if (config->sample_rate == 0) {
963 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
964 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
965 proxy_config.rate = config->sample_rate;
966 } else {
967 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
968 ret = -EINVAL;
969 }
970
971 /* Format */
972 /* until the framework supports format conversion, just take what it asks for
973 * i.e. AUDIO_FORMAT_PCM_16_BIT */
974 if (config->format == AUDIO_FORMAT_DEFAULT) {
975 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
976 * formats */
977 config->format = AUDIO_FORMAT_PCM_16_BIT;
978 proxy_config.format = PCM_FORMAT_S16_LE;
979 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
980 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
981 * formats */
982 proxy_config.format = PCM_FORMAT_S16_LE;
983 } else {
984 /* When the framework support other formats, validate here */
985 config->format = AUDIO_FORMAT_PCM_16_BIT;
986 proxy_config.format = PCM_FORMAT_S16_LE;
987 ret = -EINVAL;
988 }
989
Paul McLean2cfd81b2014-09-15 12:32:23 -0700990 /* Channels */
991 unsigned proposed_channel_count = profile_get_default_channel_count(in->profile);
992 if (k_force_channels) {
993 proposed_channel_count = k_force_channels;
994 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
995 proposed_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700996 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700997
Paul McLean2cfd81b2014-09-15 12:32:23 -0700998 /* we can expose any channel count mask, and emulate internally. */
999 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
1000 in->hal_channel_count = proposed_channel_count;
1001 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001002 proxy_prepare(&in->proxy, in->profile, &proxy_config);
1003
1004 in->standby = true;
1005
1006 in->conversion_buffer = NULL;
1007 in->conversion_buffer_size = 0;
1008
1009 *stream_in = &in->stream;
1010
1011 return ret;
1012}
1013
1014static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
1015{
1016 struct stream_in *in = (struct stream_in *)stream;
1017
1018 /* Close the pcm device */
1019 in_standby(&stream->common);
1020
1021 free(in->conversion_buffer);
1022
1023 free(stream);
1024}
1025
1026/*
1027 * ADEV Functions
1028 */
Simon Wilson19957a32012-04-06 16:17:12 -07001029static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1030{
Paul McLean65ec72b2015-02-18 09:13:24 -08001031 ALOGV("adev_set_parameters(%s)", kvpairs);
Paul McLean2c6196f2014-08-20 16:50:25 -07001032
1033 struct audio_device * adev = (struct audio_device *)dev;
1034
1035 char value[32];
1036 int param_val;
1037
1038 struct str_parms * parms = str_parms_create_str(kvpairs);
1039
1040 /* Check for the "disconnect" message */
1041 param_val = str_parms_get_str(parms, "disconnect", value, sizeof(value));
1042 if (param_val >= 0) {
1043 audio_devices_t device = (audio_devices_t)atoi(value);
1044
1045 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1046 int alsa_card = param_val >= 0 ? atoi(value) : -1;
1047
1048 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1049 int alsa_device = param_val >= 0 ? atoi(value) : -1;
1050
1051 if (alsa_card >= 0 && alsa_device >= 0) {
1052 /* "decache" the profile */
1053 pthread_mutex_lock(&adev->lock);
1054 if (device == AUDIO_DEVICE_OUT_USB_DEVICE &&
1055 profile_is_cached_for(&adev->out_profile, alsa_card, alsa_device)) {
1056 profile_decache(&adev->out_profile);
1057 }
1058 if (device == AUDIO_DEVICE_IN_USB_DEVICE &&
1059 profile_is_cached_for(&adev->in_profile, alsa_card, alsa_device)) {
1060 profile_decache(&adev->in_profile);
1061 }
1062 pthread_mutex_unlock(&adev->lock);
1063 }
1064 }
1065
soon1.choic3571572014-12-18 16:03:43 +09001066 str_parms_destroy(parms);
1067
Simon Wilson19957a32012-04-06 16:17:12 -07001068 return 0;
1069}
1070
Paul McLean30f41852014-04-16 15:44:20 -07001071static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001072{
1073 return strdup("");
1074}
1075
1076static int adev_init_check(const struct audio_hw_device *dev)
1077{
1078 return 0;
1079}
1080
1081static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1082{
1083 return -ENOSYS;
1084}
1085
1086static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1087{
1088 return -ENOSYS;
1089}
1090
1091static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1092{
1093 return 0;
1094}
1095
1096static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1097{
Eric Laurent253def92014-09-14 12:18:18 -07001098 struct audio_device * adev = (struct audio_device *)dev;
1099 pthread_mutex_lock(&adev->lock);
1100 adev->mic_muted = state;
1101 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001102 return -ENOSYS;
1103}
1104
1105static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1106{
1107 return -ENOSYS;
1108}
1109
Simon Wilson19957a32012-04-06 16:17:12 -07001110static int adev_dump(const audio_hw_device_t *device, int fd)
1111{
1112 return 0;
1113}
1114
1115static int adev_close(hw_device_t *device)
1116{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001117 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001118 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001119
Simon Wilson19957a32012-04-06 16:17:12 -07001120 return 0;
1121}
1122
Paul McLean30f41852014-04-16 15:44:20 -07001123static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001124{
Simon Wilson19957a32012-04-06 16:17:12 -07001125 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1126 return -EINVAL;
1127
Paul McLeaneedc92e2013-12-19 15:46:15 -08001128 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001129 if (!adev)
1130 return -ENOMEM;
1131
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001132 profile_init(&adev->out_profile, PCM_OUT);
1133 profile_init(&adev->in_profile, PCM_IN);
1134
Simon Wilson19957a32012-04-06 16:17:12 -07001135 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001136 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001137 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001138 adev->hw_device.common.close = adev_close;
1139
Simon Wilson19957a32012-04-06 16:17:12 -07001140 adev->hw_device.init_check = adev_init_check;
1141 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1142 adev->hw_device.set_master_volume = adev_set_master_volume;
1143 adev->hw_device.set_mode = adev_set_mode;
1144 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1145 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1146 adev->hw_device.set_parameters = adev_set_parameters;
1147 adev->hw_device.get_parameters = adev_get_parameters;
1148 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1149 adev->hw_device.open_output_stream = adev_open_output_stream;
1150 adev->hw_device.close_output_stream = adev_close_output_stream;
1151 adev->hw_device.open_input_stream = adev_open_input_stream;
1152 adev->hw_device.close_input_stream = adev_close_input_stream;
1153 adev->hw_device.dump = adev_dump;
1154
1155 *device = &adev->hw_device.common;
1156
1157 return 0;
1158}
1159
1160static struct hw_module_methods_t hal_module_methods = {
1161 .open = adev_open,
1162};
1163
1164struct audio_module HAL_MODULE_INFO_SYM = {
1165 .common = {
1166 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001167 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1168 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001169 .id = AUDIO_HARDWARE_MODULE_ID,
1170 .name = "USB audio HW HAL",
1171 .author = "The Android Open Source Project",
1172 .methods = &hal_module_methods,
1173 },
1174};