blob: 3a6c5c9075e3f03e273ee686f71a3b4b476289eb [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
17#define LOG_TAG "usb_audio_hw"
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
31#include <hardware/hardware.h>
32#include <system/audio.h>
33#include <hardware/audio.h>
34
35#include <tinyalsa/asoundlib.h>
36
Paul McLeaneedc92e2013-12-19 15:46:15 -080037/* This is the default configuration to hand to The Framework on the initial
38 * adev_open_output_stream(). Actual device attributes will be used on the subsequent
39 * adev_open_output_stream() after the card and device number have been set in out_set_parameters()
40 */
41#define OUT_PERIOD_SIZE 1024
42#define OUT_PERIOD_COUNT 4
43#define OUT_SAMPLING_RATE 44100
44
45struct pcm_config default_alsa_out_config = {
Simon Wilson19957a32012-04-06 16:17:12 -070046 .channels = 2,
Paul McLeaneedc92e2013-12-19 15:46:15 -080047 .rate = OUT_SAMPLING_RATE,
48 .period_size = OUT_PERIOD_SIZE,
49 .period_count = OUT_PERIOD_COUNT,
Simon Wilson19957a32012-04-06 16:17:12 -070050 .format = PCM_FORMAT_S16_LE,
51};
52
Paul McLeaneedc92e2013-12-19 15:46:15 -080053/*
54 * Input defaults. See comment above.
55 */
56#define IN_PERIOD_SIZE 1024
57#define IN_PERIOD_COUNT 4
58#define IN_SAMPLING_RATE 44100
59
60struct pcm_config default_alsa_in_config = {
61 .channels = 2,
62 .rate = IN_SAMPLING_RATE,
63 .period_size = IN_PERIOD_SIZE,
64 .period_count = IN_PERIOD_COUNT,
65 .format = PCM_FORMAT_S16_LE,
66 .start_threshold = 1,
67 .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT),
68};
69
Simon Wilson19957a32012-04-06 16:17:12 -070070struct audio_device {
71 struct audio_hw_device hw_device;
72
73 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080074
75 /* output */
76 int out_card;
77 int out_device;
78
79 /* input */
80 int in_card;
81 int in_device;
82
Simon Wilson19957a32012-04-06 16:17:12 -070083 bool standby;
84};
85
86struct stream_out {
87 struct audio_stream_out stream;
88
Paul McLeaneedc92e2013-12-19 15:46:15 -080089 pthread_mutex_t lock; /* see note below on mutex acquisition order */
90 struct pcm *pcm; /* state of the stream */
91 bool standby;
92
93 struct audio_device *dev; /* hardware information */
94
95 void * conversion_buffer; /* any conversions are put into here
96 * they could come from here too if
97 * there was a previous conversion */
98 size_t conversion_buffer_size; /* in bytes */
99};
100
101/*
102 * Output Configuration Cache
Paul McLean30f41852014-04-16 15:44:20 -0700103 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
Paul McLeaneedc92e2013-12-19 15:46:15 -0800104 * but that will involve changes in The Framework.
105 */
106static struct pcm_config cached_output_hardware_config;
107static bool output_hardware_config_is_cached = false;
108
109struct stream_in {
110 struct audio_stream_in stream;
111
Simon Wilson19957a32012-04-06 16:17:12 -0700112 pthread_mutex_t lock; /* see note below on mutex acquisition order */
113 struct pcm *pcm;
114 bool standby;
115
116 struct audio_device *dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800117
118 struct audio_config hal_pcm_config;
119
Paul McLeaneedc92e2013-12-19 15:46:15 -0800120// struct resampler_itfe *resampler;
121// struct resampler_buffer_provider buf_provider;
Paul McLean30f41852014-04-16 15:44:20 -0700122
Paul McLeaneedc92e2013-12-19 15:46:15 -0800123 int read_status;
Paul McLean30f41852014-04-16 15:44:20 -0700124
125 // We may need to read more data from the device in order to data reduce to 16bit, 4chan */
126 void * conversion_buffer; /* any conversions are put into here
127 * they could come from here too if
128 * there was a previous conversion */
129 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700130};
131
Paul McLeaneedc92e2013-12-19 15:46:15 -0800132/*
Paul McLean30f41852014-04-16 15:44:20 -0700133 * Input Configuration Cache
134 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
135 * but that will involve changes in The Framework.
136 */
137static struct pcm_config cached_input_hardware_config;
138static bool input_hardware_config_is_cached = false;
139
140/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800141 * Utility
142 */
143/*
144 * Translates from ALSA format ID to ANDROID_AUDIO_CORE format ID
145 * (see master/system/core/include/core/audio.h)
146 * TODO(pmclean) Replace with audio_format_from_pcm_format() (in hardware/audio_alsaops.h).
147 * post-integration.
148 */
149static audio_format_t alsa_to_fw_format_id(int alsa_fmt_id)
150{
151 switch (alsa_fmt_id) {
152 case PCM_FORMAT_S8:
153 return AUDIO_FORMAT_PCM_8_BIT;
154
155 case PCM_FORMAT_S24_3LE:
156 //TODO(pmclean) make sure this is the 'right' sort of 24-bit
157 return AUDIO_FORMAT_PCM_8_24_BIT;
158
159 case PCM_FORMAT_S32_LE:
160 case PCM_FORMAT_S24_LE:
161 return AUDIO_FORMAT_PCM_32_BIT;
162 }
163
164 return AUDIO_FORMAT_PCM_16_BIT;
165}
166
167/*
168 * Data Conversions
169 */
170/*
171 * Convert a buffer of PCM16LE samples to packed (3-byte) PCM24LE samples.
172 * in_buff points to the buffer of PCM16 samples
173 * num_in_samples size of input buffer in SAMPLES
174 * out_buff points to the buffer to receive converted PCM24 LE samples.
Paul McLean30f41852014-04-16 15:44:20 -0700175 * returns
176 * the number of BYTES of output data.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800177 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
178 * support PCM24_3LE (24-bit, packed).
Paul McLean30f41852014-04-16 15:44:20 -0700179 * NOTE:
180 * We're just filling the low-order byte of the PCM24LE samples with 0.
181 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeaneedc92e2013-12-19 15:46:15 -0800182 * TODO(pmclean, hung) Move this to a utilities module.
183 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700184static size_t convert_16_to_24_3(const short * in_buff, size_t num_in_samples, unsigned char * out_buff) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800185 /*
186 * Move from back to front so that the conversion can be done in-place
187 * i.e. in_buff == out_buff
188 */
189 int in_buff_size_in_bytes = num_in_samples * 2;
190 /* we need 3 bytes in the output for every 2 bytes in the input */
191 int out_buff_size_in_bytes = ((3 * in_buff_size_in_bytes) / 2);
192 unsigned char* dst_ptr = out_buff + out_buff_size_in_bytes - 1;
Paul McLean30f41852014-04-16 15:44:20 -0700193 size_t src_smpl_index;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700194 const unsigned char* src_ptr = ((const unsigned char *)in_buff) + in_buff_size_in_bytes - 1;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800195 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
196 *dst_ptr-- = *src_ptr--; /* hi-byte */
197 *dst_ptr-- = *src_ptr--; /* low-byte */
Paul McLean30f41852014-04-16 15:44:20 -0700198 /*TODO(pmclean) - we might want to consider dithering the lowest byte. */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800199 *dst_ptr-- = 0; /* zero-byte */
200 }
201
202 /* return number of *bytes* generated */
203 return out_buff_size_in_bytes;
204}
205
206/*
Paul McLean30f41852014-04-16 15:44:20 -0700207 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
208 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800209 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700210 * out_buff points to the buffer to receive converted PCM16LE LE samples.
211 * returns
212 * the number of BYTES of output data.
213 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
214 * support PCM24_3LE (24-bit, packed).
215 * NOTE:
216 * We're just filling the low-order byte of the PCM24LE samples with 0.
217 * This conversion is safe to do in-place (in_buff == out_buff).
218 * TODO(pmclean, hung) Move this to a utilities module.
219 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700220static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples, short * out_buff) {
Paul McLean30f41852014-04-16 15:44:20 -0700221 /*
222 * Move from front to back so that the conversion can be done in-place
223 * i.e. in_buff == out_buff
224 */
225 /* we need 2 bytes in the output for every 3 bytes in the input */
226 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700227 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700228 size_t src_smpl_index;
229 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
230 src_ptr++; /* lowest-(skip)-byte */
231 *dst_ptr++ = *src_ptr++; /* low-byte */
232 *dst_ptr++ = *src_ptr++; /* high-byte */
233 }
234
235 /* return number of *bytes* generated: */
236 return num_in_samples * 2;
237}
238
239/*
240 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
241 * (where N < M).
242 * in_buff points to the buffer of PCM16 samples
243 * in_buff_channels Specifies the number of channels in the input buffer.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800244 * out_buff points to the buffer to receive converted PCM16 samples.
Paul McLean30f41852014-04-16 15:44:20 -0700245 * out_buff_channels Specifies the number of channels in the output buffer.
246 * num_in_samples size of input buffer in SAMPLES
247 * returns
248 * the number of BYTES of output data.
249 * NOTE
250 * channels > N are filled with silence.
251 * This conversion is safe to do in-place (in_buff == out_buff)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800252 * We are doing this since we *always* present to The Framework as STEREO device, but need to
253 * support 4-channel devices.
254 * TODO(pmclean, hung) Move this to a utilities module.
255 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700256static size_t expand_channels_16(const short* in_buff, int in_buff_chans,
Paul McLean30f41852014-04-16 15:44:20 -0700257 short* out_buff, int out_buff_chans,
258 size_t num_in_samples) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800259 /*
260 * Move from back to front so that the conversion can be done in-place
261 * i.e. in_buff == out_buff
Paul McLean30f41852014-04-16 15:44:20 -0700262 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
Paul McLeaneedc92e2013-12-19 15:46:15 -0800263 */
Paul McLean30f41852014-04-16 15:44:20 -0700264 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
265
266 short* dst_ptr = out_buff + num_out_samples - 1;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700267 size_t src_index;
268 const short* src_ptr = in_buff + num_in_samples - 1;
Paul McLean30f41852014-04-16 15:44:20 -0700269 int num_zero_chans = out_buff_chans - in_buff_chans;
270 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
271 int dst_offset;
272 for(dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) {
273 *dst_ptr-- = 0;
274 }
275 for(; dst_offset < out_buff_chans; dst_offset++) {
276 *dst_ptr-- = *src_ptr--;
277 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800278 }
279
280 /* return number of *bytes* generated */
Paul McLean30f41852014-04-16 15:44:20 -0700281 return num_out_samples * sizeof(short);
282}
283
284/*
285 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
286 * (where N > M).
287 * in_buff points to the buffer of PCM16 samples
288 * in_buff_channels Specifies the number of channels in the input buffer.
289 * out_buff points to the buffer to receive converted PCM16 samples.
290 * out_buff_channels Specifies the number of channels in the output buffer.
291 * num_in_samples size of input buffer in SAMPLES
292 * returns
293 * the number of BYTES of output data.
294 * NOTE
295 * channels > N are thrown away.
296 * This conversion is safe to do in-place (in_buff == out_buff)
297 * We are doing this since we *always* present to The Framework as STEREO device, but need to
298 * support 4-channel devices.
299 * TODO(pmclean, hung) Move this to a utilities module.
300 */
301static size_t contract_channels_16(short* in_buff, int in_buff_chans,
302 short* out_buff, int out_buff_chans,
303 size_t num_in_samples) {
304 /*
305 * Move from front to back so that the conversion can be done in-place
306 * i.e. in_buff == out_buff
307 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
308 */
309 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
310
311 int num_skip_samples = in_buff_chans - out_buff_chans;
312
313 short* dst_ptr = out_buff;
314 short* src_ptr = in_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700315 size_t src_index;
Paul McLean30f41852014-04-16 15:44:20 -0700316 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
317 int dst_offset;
318 for(dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) {
319 *dst_ptr++ = *src_ptr++;
320 }
321 src_ptr += num_skip_samples;
322 }
323
324 /* return number of *bytes* generated */
325 return num_out_samples * sizeof(short);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800326}
327
328/*
329 * ALSA Utilities
330 */
331/*
332 * gets the ALSA bit-format flag from a bits-per-sample value.
333 * TODO(pmclean, hung) Move this to a utilities module.
334 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700335static int bits_to_alsa_format(unsigned int bits_per_sample, int default_format)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800336{
337 enum pcm_format format;
338 for (format = PCM_FORMAT_S16_LE; format < PCM_FORMAT_MAX; format++) {
339 if (pcm_format_to_bits(format) == bits_per_sample) {
Paul McLean30f41852014-04-16 15:44:20 -0700340 return format;
341 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800342 }
343 return default_format;
344}
345
Paul McLeancf611912014-04-28 13:03:18 -0700346static void log_pcm_params(struct pcm_params * alsa_hw_params) {
347 ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u",
348 pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS),
349 pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS));
350 ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u",
351 pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS),
352 pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS));
353 ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u",
354 pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
355 pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
356 ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u",
357 pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
358 pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
359 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u",
360 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME),
361 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME));
362 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u",
363 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE),
364 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE));
365 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u",
366 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES),
367 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES));
368 ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u",
369 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS),
370 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS));
371 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u",
372 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME),
373 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME));
374 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u",
375 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE),
376 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE));
377 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u",
378 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES),
379 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES));
380 ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u",
381 pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME),
382 pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME));
383}
384
Paul McLeaneedc92e2013-12-19 15:46:15 -0800385/*
Paul McLean33a6b172014-06-19 12:35:28 -0700386 * Returns the supplied value rounded up to the next even multiple of 16
387 */
388static unsigned int round_to_16_mult(unsigned int size) {
389 return (size + 15) & 0xFFFFFFF0;
390}
391
392/*TODO(pmclean) - Evaluate if this value should/can be retrieved from a device-specific property */
393#define MIN_BUFF_TIME 5 /* milliseconds */
394
395/*
396 * Returns the system defined minimum period size based on the supplied sample rate
397 */
398static unsigned int calc_min_period_size(unsigned int sample_rate) {
399 unsigned int period_size = (sample_rate * MIN_BUFF_TIME) / 1000;
400 return round_to_16_mult(period_size);
401}
402
403/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800404 * Reads and decodes configuration info from the specified ALSA card/device
405 */
406static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config)
407{
Paul McLean30f41852014-04-16 15:44:20 -0700408 ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)",card, device, io_type);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800409
410 if (card < 0 || device < 0) {
411 return -EINVAL;
412 }
413
414 struct pcm_params * alsa_hw_params = pcm_params_get(card, device, io_type);
415 if (alsa_hw_params == NULL) {
416 return -EINVAL;
417 }
418
419 /*
420 * This Logging will be useful when testing new USB devices.
421 */
Paul McLeancf611912014-04-28 13:03:18 -0700422 /* log_pcm_params(alsa_hw_params); */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800423
424 config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
425 config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
Paul McLean33a6b172014-06-19 12:35:28 -0700426 config->period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE);
427 /* round this up to a multiple of 16 */
428 config->period_size = round_to_16_mult(config->period_size);
429 /* make sure it is above a minimum value to minimize jitter */
430 unsigned int min_period_size = calc_min_period_size(config->rate);
431 if (config->period_size < min_period_size) {
432 config->period_size = min_period_size;
433 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800434 config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
435
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700436 unsigned int bits_per_sample = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800437 config->format = bits_to_alsa_format(bits_per_sample, PCM_FORMAT_S16_LE);
438
439 return 0;
440}
441
442/*
443 * HAl Functions
444 */
Simon Wilson19957a32012-04-06 16:17:12 -0700445/**
446 * NOTE: when multiple mutexes have to be acquired, always respect the
447 * following order: hw device > out stream
448 */
449
450/* Helper functions */
Simon Wilson19957a32012-04-06 16:17:12 -0700451static uint32_t out_get_sample_rate(const struct audio_stream *stream)
452{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800453 return cached_output_hardware_config.rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700454}
455
456static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
457{
458 return 0;
459}
460
461static size_t out_get_buffer_size(const struct audio_stream *stream)
462{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800463 return cached_output_hardware_config.period_size * audio_stream_frame_size(stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700464}
465
466static uint32_t out_get_channels(const struct audio_stream *stream)
467{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800468 // Always Stero for now. We will do *some* conversions in this HAL.
469 // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary channels
470 // rewrite this to return the ACTUAL channel format
Simon Wilson19957a32012-04-06 16:17:12 -0700471 return AUDIO_CHANNEL_OUT_STEREO;
472}
473
474static audio_format_t out_get_format(const struct audio_stream *stream)
475{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800476 // Always return 16-bit PCM. We will do *some* conversions in this HAL.
477 // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary PCM formats
478 // rewrite this to return the ACTUAL data format
Simon Wilson19957a32012-04-06 16:17:12 -0700479 return AUDIO_FORMAT_PCM_16_BIT;
480}
481
482static int out_set_format(struct audio_stream *stream, audio_format_t format)
483{
484 return 0;
485}
486
487static int out_standby(struct audio_stream *stream)
488{
489 struct stream_out *out = (struct stream_out *)stream;
490
491 pthread_mutex_lock(&out->dev->lock);
492 pthread_mutex_lock(&out->lock);
493
494 if (!out->standby) {
495 pcm_close(out->pcm);
496 out->pcm = NULL;
497 out->standby = true;
498 }
499
500 pthread_mutex_unlock(&out->lock);
501 pthread_mutex_unlock(&out->dev->lock);
502
503 return 0;
504}
505
506static int out_dump(const struct audio_stream *stream, int fd)
507{
508 return 0;
509}
510
511static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
512{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800513 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
514
Simon Wilson19957a32012-04-06 16:17:12 -0700515 struct stream_out *out = (struct stream_out *)stream;
516 struct audio_device *adev = out->dev;
517 struct str_parms *parms;
518 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800519 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700520 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800521 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700522
523 parms = str_parms_create_str(kvpairs);
524 pthread_mutex_lock(&adev->lock);
525
Paul McLeaneedc92e2013-12-19 15:46:15 -0800526 bool recache_device_params = false;
527 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
528 if (param_val >= 0) {
529 adev->out_card = atoi(value);
530 recache_device_params = true;
531 }
Simon Wilson19957a32012-04-06 16:17:12 -0700532
Paul McLeaneedc92e2013-12-19 15:46:15 -0800533 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
534 if (param_val >= 0) {
535 adev->out_device = atoi(value);
536 recache_device_params = true;
537 }
538
539 if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) {
540 ret_value = read_alsa_device_config(adev->out_card, adev->out_device, PCM_OUT,
Paul McLean30f41852014-04-16 15:44:20 -0700541 &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800542 output_hardware_config_is_cached = (ret_value == 0);
543 }
Simon Wilson19957a32012-04-06 16:17:12 -0700544
545 pthread_mutex_unlock(&adev->lock);
546 str_parms_destroy(parms);
547
Paul McLeaneedc92e2013-12-19 15:46:15 -0800548 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700549}
550
Paul McLeaneedc92e2013-12-19 15:46:15 -0800551//TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters()
552// could be written in terms of a get_device_parameters(io_type)
553
Paul McLean30f41852014-04-16 15:44:20 -0700554static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
555{
556 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
557
Paul McLeaneedc92e2013-12-19 15:46:15 -0800558 struct stream_out *out = (struct stream_out *) stream;
559 struct audio_device *adev = out->dev;
560
Paul McLean30f41852014-04-16 15:44:20 -0700561 if (adev->out_card < 0 || adev->out_device < 0)
562 return strdup("");
563
Paul McLeaneedc92e2013-12-19 15:46:15 -0800564 unsigned min, max;
565
566 struct str_parms *query = str_parms_create_str(keys);
567 struct str_parms *result = str_parms_create();
568
569 int num_written = 0;
570 char buffer[256];
571 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
572 char* result_str = NULL;
573
574 struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT);
575
576 // These keys are from hardware/libhardware/include/audio.h
577 // supported sample rates
578 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
579 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
580 // if they are different, return a list containing those two values, otherwise just the one.
581 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
582 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700583 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800584 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700585 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800586 }
587 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
588 buffer);
589 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
590
591 // supported channel counts
592 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
593 // Similarly for output channels count
594 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
595 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700596 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800597 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700598 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800599 }
600 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer);
601 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
602
603 // supported sample formats
604 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
605 // Similarly for output channels count
606 //TODO(pmclean): this is wrong.
607 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
608 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700609 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800610 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700611 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800612 }
613 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
614 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
615
616 result_str = str_parms_to_str(result);
617
618 // done with these...
619 str_parms_destroy(query);
620 str_parms_destroy(result);
621
622 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700623}
624
625static uint32_t out_get_latency(const struct audio_stream_out *stream)
626{
Paul McLean30f41852014-04-16 15:44:20 -0700627 struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800628
629 //TODO(pmclean): Do we need a term here for the USB latency
630 // (as reported in the USB descriptors)?
Paul McLean30f41852014-04-16 15:44:20 -0700631 uint32_t latency = (cached_output_hardware_config.period_size
632 * cached_output_hardware_config.period_count * 1000) / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800633 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700634}
635
Paul McLean30f41852014-04-16 15:44:20 -0700636static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700637{
638 return -ENOSYS;
639}
640
Paul McLeaneedc92e2013-12-19 15:46:15 -0800641/* must be called with hw device and output stream mutexes locked */
642static int start_output_stream(struct stream_out *out)
643{
644 struct audio_device *adev = out->dev;
645 int return_val = 0;
646
Paul McLean30f41852014-04-16 15:44:20 -0700647 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
648 adev->out_card, adev->out_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800649
650 out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config);
651 if (out->pcm == NULL) {
652 return -ENOMEM;
653 }
654
655 if (out->pcm && !pcm_is_ready(out->pcm)) {
656 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
657 pcm_close(out->pcm);
658 return -ENOMEM;
659 }
660
Paul McLeaneedc92e2013-12-19 15:46:15 -0800661 return 0;
662}
663
664static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700665{
666 int ret;
667 struct stream_out *out = (struct stream_out *)stream;
668
669 pthread_mutex_lock(&out->dev->lock);
670 pthread_mutex_lock(&out->lock);
671 if (out->standby) {
672 ret = start_output_stream(out);
673 if (ret != 0) {
674 goto err;
675 }
676 out->standby = false;
677 }
678
Paul McLean30f41852014-04-16 15:44:20 -0700679 // Setup conversion buffer
680 // compute maximum potential buffer size.
681 // * 2 for stereo -> quad conversion
682 // * 3/2 for 16bit -> 24 bit conversion
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700683 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700684 if (required_conversion_buffer_size > out->conversion_buffer_size) {
685 //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
686 // (and do these conversions themselves)
687 out->conversion_buffer_size = required_conversion_buffer_size;
688 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
689 }
690
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700691 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800692 int num_write_buff_bytes = bytes;
693
694 /*
695 * Num Channels conversion
696 */
697 int num_device_channels = cached_output_hardware_config.channels;
698 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700699 if (num_device_channels != num_req_channels) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800700 num_write_buff_bytes =
Paul McLean30f41852014-04-16 15:44:20 -0700701 expand_channels_16(write_buff, num_req_channels,
702 out->conversion_buffer, num_device_channels,
703 num_write_buff_bytes / sizeof(short));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800704 write_buff = out->conversion_buffer;
705 }
706
707 /*
708 * 16 vs 24-bit logic here
709 */
710 switch (cached_output_hardware_config.format) {
711 case PCM_FORMAT_S16_LE:
712 // the output format is the same as the input format, so just write it out
713 break;
714
715 case PCM_FORMAT_S24_3LE:
716 // 16-bit LE2 - 24-bit LE3
Paul McLean30f41852014-04-16 15:44:20 -0700717 num_write_buff_bytes = convert_16_to_24_3(write_buff,
718 num_write_buff_bytes / sizeof(short),
719 out->conversion_buffer);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800720 write_buff = out->conversion_buffer;
721 break;
722
723 default:
724 // hmmmmm.....
725 ALOGV("usb:Unknown Format!!!");
726 break;
727 }
728
729 if (write_buff != NULL && num_write_buff_bytes != 0) {
730 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
731 }
Simon Wilson19957a32012-04-06 16:17:12 -0700732
733 pthread_mutex_unlock(&out->lock);
734 pthread_mutex_unlock(&out->dev->lock);
735
736 return bytes;
737
738err:
739 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800740 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700741 if (ret != 0) {
742 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
743 out_get_sample_rate(&stream->common));
744 }
745
746 return bytes;
747}
748
Paul McLean30f41852014-04-16 15:44:20 -0700749static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700750{
751 return -EINVAL;
752}
753
754static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
755{
756 return 0;
757}
758
759static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
760{
761 return 0;
762}
763
Paul McLean30f41852014-04-16 15:44:20 -0700764static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700765{
766 return -EINVAL;
767}
768
769static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700770 audio_io_handle_t handle,
771 audio_devices_t devices,
772 audio_output_flags_t flags,
773 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -0700774 struct audio_stream_out **stream_out)
775{
Paul McLean30f41852014-04-16 15:44:20 -0700776 ALOGV("usb:audio_hw::out adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X",
Paul McLeaneedc92e2013-12-19 15:46:15 -0800777 handle, devices, flags);
778
Simon Wilson19957a32012-04-06 16:17:12 -0700779 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800780
Simon Wilson19957a32012-04-06 16:17:12 -0700781 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700782
783 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
784 if (!out)
785 return -ENOMEM;
786
Paul McLeaneedc92e2013-12-19 15:46:15 -0800787 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -0700788 out->stream.common.get_sample_rate = out_get_sample_rate;
789 out->stream.common.set_sample_rate = out_set_sample_rate;
790 out->stream.common.get_buffer_size = out_get_buffer_size;
791 out->stream.common.get_channels = out_get_channels;
792 out->stream.common.get_format = out_get_format;
793 out->stream.common.set_format = out_set_format;
794 out->stream.common.standby = out_standby;
795 out->stream.common.dump = out_dump;
796 out->stream.common.set_parameters = out_set_parameters;
797 out->stream.common.get_parameters = out_get_parameters;
798 out->stream.common.add_audio_effect = out_add_audio_effect;
799 out->stream.common.remove_audio_effect = out_remove_audio_effect;
800 out->stream.get_latency = out_get_latency;
801 out->stream.set_volume = out_set_volume;
802 out->stream.write = out_write;
803 out->stream.get_render_position = out_get_render_position;
804 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
805
806 out->dev = adev;
807
Paul McLeaneedc92e2013-12-19 15:46:15 -0800808 if (output_hardware_config_is_cached) {
809 config->sample_rate = cached_output_hardware_config.rate;
810
811 config->format = alsa_to_fw_format_id(cached_output_hardware_config.format);
812 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
813 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
814 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
815 //TODO(pmclean) remove this when the above restriction is removed.
816 config->format = AUDIO_FORMAT_PCM_16_BIT;
817 }
818
819 config->channel_mask =
820 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
821 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
822 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
823 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
824 //TODO(pmclean) remove this when the above restriction is removed.
825 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
826 }
827 } else {
828 cached_output_hardware_config = default_alsa_out_config;
829
830 config->format = out_get_format(&out->stream.common);
831 config->channel_mask = out_get_channels(&out->stream.common);
832 config->sample_rate = out_get_sample_rate(&out->stream.common);
833 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800834
835 out->conversion_buffer = NULL;
836 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700837
838 out->standby = true;
839
840 *stream_out = &out->stream;
841 return 0;
842
843err_open:
844 free(out);
845 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800846 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700847}
848
849static void adev_close_output_stream(struct audio_hw_device *dev,
850 struct audio_stream_out *stream)
851{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800852 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -0700853 struct stream_out *out = (struct stream_out *)stream;
854
Paul McLeaneedc92e2013-12-19 15:46:15 -0800855 //TODO(pmclean) why are we doing this when stream get's freed at the end
856 // because it closes the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -0700857 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800858
859 free(out->conversion_buffer);
860 out->conversion_buffer = NULL;
861 out->conversion_buffer_size = 0;
862
Simon Wilson19957a32012-04-06 16:17:12 -0700863 free(stream);
864}
865
866static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
867{
868 return 0;
869}
870
Paul McLean30f41852014-04-16 15:44:20 -0700871static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -0700872{
873 return strdup("");
874}
875
876static int adev_init_check(const struct audio_hw_device *dev)
877{
878 return 0;
879}
880
881static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
882{
883 return -ENOSYS;
884}
885
886static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
887{
888 return -ENOSYS;
889}
890
891static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
892{
893 return 0;
894}
895
896static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
897{
898 return -ENOSYS;
899}
900
901static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
902{
903 return -ENOSYS;
904}
905
906static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700907 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -0700908{
909 return 0;
910}
911
Paul McLeaneedc92e2013-12-19 15:46:15 -0800912/* Helper functions */
913static uint32_t in_get_sample_rate(const struct audio_stream *stream)
914{
Paul McLean30f41852014-04-16 15:44:20 -0700915 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800916}
917
918static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
919{
920 return -ENOSYS;
921}
922
923static size_t in_get_buffer_size(const struct audio_stream *stream)
924{
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700925 ALOGV("usb: in_get_buffer_size() = %zu",
Paul McLean30f41852014-04-16 15:44:20 -0700926 cached_input_hardware_config.period_size * audio_stream_frame_size(stream));
927 return cached_input_hardware_config.period_size * audio_stream_frame_size(stream);
928
Paul McLeaneedc92e2013-12-19 15:46:15 -0800929}
930
931static uint32_t in_get_channels(const struct audio_stream *stream)
932{
Paul McLean30f41852014-04-16 15:44:20 -0700933 // just report stereo for now
934 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800935}
936
937static audio_format_t in_get_format(const struct audio_stream *stream)
938{
939 // just report 16-bit, pcm for now.
940 return AUDIO_FORMAT_PCM_16_BIT;
941}
942
943static int in_set_format(struct audio_stream *stream, audio_format_t format)
944{
945 return -ENOSYS;
946}
947
948static int in_standby(struct audio_stream *stream)
949{
Paul McLean30f41852014-04-16 15:44:20 -0700950 struct stream_in *in = (struct stream_in *) stream;
951
952 pthread_mutex_lock(&in->dev->lock);
953 pthread_mutex_lock(&in->lock);
954
955 if (!in->standby) {
956 pcm_close(in->pcm);
957 in->pcm = NULL;
958 in->standby = true;
959 }
960
961 pthread_mutex_unlock(&in->lock);
962 pthread_mutex_unlock(&in->dev->lock);
963
Paul McLeaneedc92e2013-12-19 15:46:15 -0800964 return 0;
965}
966
967static int in_dump(const struct audio_stream *stream, int fd)
968{
969 return 0;
970}
971
972static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
973{
Paul McLean30f41852014-04-16 15:44:20 -0700974 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800975
976 struct stream_in *in = (struct stream_in *)stream;
977 struct audio_device *adev = in->dev;
978 struct str_parms *parms;
979 char value[32];
980 int param_val;
981 int routing = 0;
982 int ret_value = 0;
983
984 parms = str_parms_create_str(kvpairs);
985 pthread_mutex_lock(&adev->lock);
986
Paul McLean30f41852014-04-16 15:44:20 -0700987 bool recache_device_params = false;
988
Paul McLeaneedc92e2013-12-19 15:46:15 -0800989 // Card/Device
990 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
991 if (param_val >= 0) {
992 adev->in_card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -0700993 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800994 }
995
996 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
997 if (param_val >= 0) {
998 adev->in_device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -0700999 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001000 }
1001
Paul McLean30f41852014-04-16 15:44:20 -07001002 if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) {
1003 ret_value = read_alsa_device_config(adev->in_card, adev->in_device,
1004 PCM_IN, &(cached_input_hardware_config));
1005 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001006 }
1007
1008 pthread_mutex_unlock(&adev->lock);
1009 str_parms_destroy(parms);
1010
1011 return ret_value;
1012}
1013
1014//TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters()
1015// could be written in terms of a get_device_parameters(io_type)
1016
Paul McLean30f41852014-04-16 15:44:20 -07001017static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
1018 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001019
Paul McLean30f41852014-04-16 15:44:20 -07001020 struct stream_in *in = (struct stream_in *)stream;
1021 struct audio_device *adev = in->dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001022
Paul McLean30f41852014-04-16 15:44:20 -07001023 if (adev->in_card < 0 || adev->in_device < 0)
1024 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001025
Paul McLean30f41852014-04-16 15:44:20 -07001026 struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN);
1027 if (alsa_hw_params == NULL)
1028 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001029
Paul McLean30f41852014-04-16 15:44:20 -07001030 struct str_parms *query = str_parms_create_str(keys);
1031 struct str_parms *result = str_parms_create();
Paul McLeaneedc92e2013-12-19 15:46:15 -08001032
Paul McLean30f41852014-04-16 15:44:20 -07001033 int num_written = 0;
1034 char buffer[256];
1035 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
1036 char* result_str = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001037
Paul McLean30f41852014-04-16 15:44:20 -07001038 unsigned min, max;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001039
Paul McLean30f41852014-04-16 15:44:20 -07001040 // These keys are from hardware/libhardware/include/audio.h
1041 // supported sample rates
1042 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
1043 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
1044 // if they are different, return a list containing those two values, otherwise just the one.
1045 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
1046 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001047 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001048 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001049 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001050 }
1051 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer);
1052 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
Paul McLeaneedc92e2013-12-19 15:46:15 -08001053
Paul McLean30f41852014-04-16 15:44:20 -07001054 // supported channel counts
1055 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
1056 // Similarly for output channels count
1057 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
1058 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001059 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001060 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001061 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001062 }
1063 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer);
1064 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001065
Paul McLean30f41852014-04-16 15:44:20 -07001066 // supported sample formats
1067 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
1068 //TODO(pmclean): this is wrong.
1069 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
1070 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001071 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001072 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001073 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001074 }
1075 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
1076 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001077
Paul McLean30f41852014-04-16 15:44:20 -07001078 result_str = str_parms_to_str(result);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001079
Paul McLean30f41852014-04-16 15:44:20 -07001080 // done with these...
1081 str_parms_destroy(query);
1082 str_parms_destroy(result);
1083
1084 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001085}
1086
1087static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1088{
1089 return 0;
1090}
1091
1092static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1093{
1094 return 0;
1095}
1096
Paul McLean30f41852014-04-16 15:44:20 -07001097static int in_set_gain(struct audio_stream_in *stream, float gain)
1098{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001099 return 0;
1100}
1101
Paul McLean30f41852014-04-16 15:44:20 -07001102/* must be called with hw device and output stream mutexes locked */
1103static int start_input_stream(struct stream_in *in) {
1104 struct audio_device *adev = in->dev;
1105 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001106
Paul McLean30f41852014-04-16 15:44:20 -07001107 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
1108 adev->in_card, adev->in_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001109
Paul McLean30f41852014-04-16 15:44:20 -07001110 in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config);
1111 if (in->pcm == NULL) {
1112 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1113 return -ENOMEM;
1114 }
1115
1116 if (in->pcm && !pcm_is_ready(in->pcm)) {
1117 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1118 pcm_close(in->pcm);
1119 return -ENOMEM;
1120 }
1121
1122 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001123}
1124
Paul McLean30f41852014-04-16 15:44:20 -07001125//TODO(pmclean) mutex stuff here (see out_write)
1126static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1127{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001128 size_t num_read_buff_bytes = 0;
Paul McLean30f41852014-04-16 15:44:20 -07001129 void * read_buff = buffer;
1130 void * out_buff = buffer;
1131
1132 struct stream_in * in = (struct stream_in *) stream;
1133
Paul McLeancf611912014-04-28 13:03:18 -07001134 ALOGV("usb: in_read(%d)", bytes);
1135
Paul McLean30f41852014-04-16 15:44:20 -07001136 pthread_mutex_lock(&in->dev->lock);
1137 pthread_mutex_lock(&in->lock);
1138
1139 if (in->standby) {
1140 if (start_input_stream(in) != 0) {
1141 goto err;
1142 }
1143 in->standby = false;
1144 }
1145
1146 // OK, we need to figure out how much data to read to be able to output the requested
1147 // number of bytes in the HAL format (16-bit, stereo).
1148 num_read_buff_bytes = bytes;
1149 int num_device_channels = cached_input_hardware_config.channels;
1150 int num_req_channels = 2; /* always, for now */
1151
1152 if (num_device_channels != num_req_channels) {
Paul McLeancf611912014-04-28 13:03:18 -07001153 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
Paul McLean30f41852014-04-16 15:44:20 -07001154 }
1155
Eric Laurent7661a482014-06-11 12:00:16 -07001156 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001157 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
1158 }
1159
1160 // Setup/Realloc the conversion buffer (if necessary).
1161 if (num_read_buff_bytes != bytes) {
1162 if (num_read_buff_bytes > in->conversion_buffer_size) {
1163 //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1164 // (and do these conversions themselves)
1165 in->conversion_buffer_size = num_read_buff_bytes;
1166 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1167 }
1168 read_buff = in->conversion_buffer;
1169 }
1170
1171 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1172 /*
1173 * Do any conversions necessary to send the data in the format specified to/by the HAL
1174 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1175 */
Eric Laurent7661a482014-06-11 12:00:16 -07001176 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001177 if (num_device_channels != num_req_channels) {
1178 out_buff = read_buff;
1179 }
1180
1181 /* Bit Format Conversion */
1182 num_read_buff_bytes =
1183 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1184 }
1185
1186 if (num_device_channels != num_req_channels) {
1187 out_buff = buffer;
1188 /* Num Channels conversion */
Paul McLeancf611912014-04-28 13:03:18 -07001189 if (num_device_channels < num_req_channels) {
1190 num_read_buff_bytes =
Paul McLeancf611912014-04-28 13:03:18 -07001191 expand_channels_16(read_buff, num_device_channels,
1192 out_buff, num_req_channels,
1193 num_read_buff_bytes / sizeof(short));
Eric Laurentfbc02dc2014-06-27 18:39:21 -07001194 } else {
1195 num_read_buff_bytes =
1196 contract_channels_16(read_buff, num_device_channels,
1197 out_buff, num_req_channels,
1198 num_read_buff_bytes / sizeof(short));
Paul McLeancf611912014-04-28 13:03:18 -07001199 }
Paul McLean30f41852014-04-16 15:44:20 -07001200 }
1201 }
1202
1203err:
1204 pthread_mutex_unlock(&in->lock);
1205 pthread_mutex_unlock(&in->dev->lock);
1206
1207 return num_read_buff_bytes;
1208}
1209
1210static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1211{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001212 return 0;
1213}
1214
Mike Lockwood46a98092012-04-24 16:41:18 -07001215static int adev_open_input_stream(struct audio_hw_device *dev,
1216 audio_io_handle_t handle,
1217 audio_devices_t devices,
Paul McLean30f41852014-04-16 15:44:20 -07001218 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001219 struct audio_stream_in **stream_in)
1220{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001221 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLean30f41852014-04-16 15:44:20 -07001222 config->sample_rate, config->channel_mask, config->format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001223
1224 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Eric Laurent7661a482014-06-11 12:00:16 -07001225 int ret = 0;
1226
Paul McLeaneedc92e2013-12-19 15:46:15 -08001227 if (in == NULL)
1228 return -ENOMEM;
1229
1230 // setup function pointers
1231 in->stream.common.get_sample_rate = in_get_sample_rate;
1232 in->stream.common.set_sample_rate = in_set_sample_rate;
1233 in->stream.common.get_buffer_size = in_get_buffer_size;
1234 in->stream.common.get_channels = in_get_channels;
1235 in->stream.common.get_format = in_get_format;
1236 in->stream.common.set_format = in_set_format;
1237 in->stream.common.standby = in_standby;
1238 in->stream.common.dump = in_dump;
1239 in->stream.common.set_parameters = in_set_parameters;
1240 in->stream.common.get_parameters = in_get_parameters;
1241 in->stream.common.add_audio_effect = in_add_audio_effect;
1242 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1243
1244 in->stream.set_gain = in_set_gain;
1245 in->stream.read = in_read;
1246 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1247
Paul McLean30f41852014-04-16 15:44:20 -07001248 in->dev = (struct audio_device *)dev;
1249
Eric Laurent7661a482014-06-11 12:00:16 -07001250 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO)
1251 ret = -EINVAL;
Paul McLean30f41852014-04-16 15:44:20 -07001252
Eric Laurent7661a482014-06-11 12:00:16 -07001253 if (input_hardware_config_is_cached) {
1254 config->sample_rate = cached_input_hardware_config.rate;
1255
1256 config->format = alsa_to_fw_format_id(cached_input_hardware_config.format);
Paul McLean30f41852014-04-16 15:44:20 -07001257 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
1258 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1259 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
1260 //TODO(pmclean) remove this when the above restriction is removed.
1261 config->format = AUDIO_FORMAT_PCM_16_BIT;
1262 }
1263
Eric Laurent7661a482014-06-11 12:00:16 -07001264 config->channel_mask = audio_channel_in_mask_from_count(
1265 cached_input_hardware_config.channels);
1266 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
Paul McLean30f41852014-04-16 15:44:20 -07001267 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1268 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
1269 //TODO(pmclean) remove this when the above restriction is removed.
Eric Laurent7661a482014-06-11 12:00:16 -07001270 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
Paul McLean30f41852014-04-16 15:44:20 -07001271 }
1272 } else {
1273 cached_input_hardware_config = default_alsa_in_config;
1274
Eric Laurent7661a482014-06-11 12:00:16 -07001275 config->format = in_get_format(&in->stream.common);
1276 config->channel_mask = in_get_channels(&in->stream.common);
1277 config->sample_rate = in_get_sample_rate(&in->stream.common);
Paul McLean30f41852014-04-16 15:44:20 -07001278 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001279
1280 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001281
Paul McLean30f41852014-04-16 15:44:20 -07001282 in->conversion_buffer = NULL;
1283 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001284
1285 *stream_in = &in->stream;
1286
Eric Laurent7661a482014-06-11 12:00:16 -07001287 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001288}
1289
Paul McLean30f41852014-04-16 15:44:20 -07001290static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001291{
Paul McLean30f41852014-04-16 15:44:20 -07001292 struct stream_in *in = (struct stream_in *)stream;
1293
1294 //TODO(pmclean) why are we doing this when stream get's freed at the end
1295 // because it closes the pcm device
1296 in_standby(&stream->common);
1297
1298 free(in->conversion_buffer);
1299
1300 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001301}
1302
1303static int adev_dump(const audio_hw_device_t *device, int fd)
1304{
1305 return 0;
1306}
1307
1308static int adev_close(hw_device_t *device)
1309{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001310 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001311 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001312
1313 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001314 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001315
Simon Wilson19957a32012-04-06 16:17:12 -07001316 return 0;
1317}
1318
Paul McLean30f41852014-04-16 15:44:20 -07001319static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001320{
Simon Wilson19957a32012-04-06 16:17:12 -07001321 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1322 return -EINVAL;
1323
Paul McLeaneedc92e2013-12-19 15:46:15 -08001324 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001325 if (!adev)
1326 return -ENOMEM;
1327
1328 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001329 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001330 adev->hw_device.common.module = (struct hw_module_t *) module;
1331 adev->hw_device.common.close = adev_close;
1332
Simon Wilson19957a32012-04-06 16:17:12 -07001333 adev->hw_device.init_check = adev_init_check;
1334 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1335 adev->hw_device.set_master_volume = adev_set_master_volume;
1336 adev->hw_device.set_mode = adev_set_mode;
1337 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1338 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1339 adev->hw_device.set_parameters = adev_set_parameters;
1340 adev->hw_device.get_parameters = adev_get_parameters;
1341 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1342 adev->hw_device.open_output_stream = adev_open_output_stream;
1343 adev->hw_device.close_output_stream = adev_close_output_stream;
1344 adev->hw_device.open_input_stream = adev_open_input_stream;
1345 adev->hw_device.close_input_stream = adev_close_input_stream;
1346 adev->hw_device.dump = adev_dump;
1347
1348 *device = &adev->hw_device.common;
1349
1350 return 0;
1351}
1352
1353static struct hw_module_methods_t hal_module_methods = {
1354 .open = adev_open,
1355};
1356
1357struct audio_module HAL_MODULE_INFO_SYM = {
1358 .common = {
1359 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001360 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1361 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001362 .id = AUDIO_HARDWARE_MODULE_ID,
1363 .name = "USB audio HW HAL",
1364 .author = "The Android Open Source Project",
1365 .methods = &hal_module_methods,
1366 },
1367};