blob: 208526834e6a9b0dac276bba15b4a6d8ae5bfbc8 [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
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 McLeaneedc92e2013-12-19 15:46:15 -080039/* This is the default configuration to hand to The Framework on the initial
40 * adev_open_output_stream(). Actual device attributes will be used on the subsequent
41 * adev_open_output_stream() after the card and device number have been set in out_set_parameters()
42 */
43#define OUT_PERIOD_SIZE 1024
44#define OUT_PERIOD_COUNT 4
45#define OUT_SAMPLING_RATE 44100
46
47struct pcm_config default_alsa_out_config = {
Simon Wilson19957a32012-04-06 16:17:12 -070048 .channels = 2,
Paul McLeaneedc92e2013-12-19 15:46:15 -080049 .rate = OUT_SAMPLING_RATE,
50 .period_size = OUT_PERIOD_SIZE,
51 .period_count = OUT_PERIOD_COUNT,
Simon Wilson19957a32012-04-06 16:17:12 -070052 .format = PCM_FORMAT_S16_LE,
53};
54
Paul McLeaneedc92e2013-12-19 15:46:15 -080055/*
56 * Input defaults. See comment above.
57 */
58#define IN_PERIOD_SIZE 1024
59#define IN_PERIOD_COUNT 4
60#define IN_SAMPLING_RATE 44100
61
62struct pcm_config default_alsa_in_config = {
63 .channels = 2,
64 .rate = IN_SAMPLING_RATE,
65 .period_size = IN_PERIOD_SIZE,
66 .period_count = IN_PERIOD_COUNT,
67 .format = PCM_FORMAT_S16_LE,
68 .start_threshold = 1,
69 .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT),
70};
71
Simon Wilson19957a32012-04-06 16:17:12 -070072struct audio_device {
73 struct audio_hw_device hw_device;
74
75 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080076
77 /* output */
78 int out_card;
79 int out_device;
80
81 /* input */
82 int in_card;
83 int in_device;
84
Simon Wilson19957a32012-04-06 16:17:12 -070085 bool standby;
86};
87
88struct stream_out {
89 struct audio_stream_out stream;
90
Paul McLeaneedc92e2013-12-19 15:46:15 -080091 pthread_mutex_t lock; /* see note below on mutex acquisition order */
92 struct pcm *pcm; /* state of the stream */
93 bool standby;
94
95 struct audio_device *dev; /* hardware information */
96
97 void * conversion_buffer; /* any conversions are put into here
98 * they could come from here too if
99 * there was a previous conversion */
100 size_t conversion_buffer_size; /* in bytes */
101};
102
103/*
104 * Output Configuration Cache
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700105 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800106 */
107static struct pcm_config cached_output_hardware_config;
108static bool output_hardware_config_is_cached = false;
109
110struct stream_in {
111 struct audio_stream_in stream;
112
Simon Wilson19957a32012-04-06 16:17:12 -0700113 pthread_mutex_t lock; /* see note below on mutex acquisition order */
114 struct pcm *pcm;
115 bool standby;
116
117 struct audio_device *dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800118
119 struct audio_config hal_pcm_config;
120
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700121 /* this is the format the framework thinks it's using. We may need to convert from the actual
122 * (24-bit, 32-bit?) format to this theoretical (framework, probably 16-bit)
123 * format in in_read() */
124 enum pcm_format input_framework_format;
125
Paul McLeaneedc92e2013-12-19 15:46:15 -0800126// struct resampler_itfe *resampler;
127// struct resampler_buffer_provider buf_provider;
Paul McLean30f41852014-04-16 15:44:20 -0700128
Paul McLeaneedc92e2013-12-19 15:46:15 -0800129 int read_status;
Paul McLean30f41852014-04-16 15:44:20 -0700130
131 // We may need to read more data from the device in order to data reduce to 16bit, 4chan */
132 void * conversion_buffer; /* any conversions are put into here
133 * they could come from here too if
134 * there was a previous conversion */
135 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700136};
137
Paul McLeaneedc92e2013-12-19 15:46:15 -0800138/*
Paul McLean30f41852014-04-16 15:44:20 -0700139 * Input Configuration Cache
140 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
141 * but that will involve changes in The Framework.
142 */
143static struct pcm_config cached_input_hardware_config;
144static bool input_hardware_config_is_cached = false;
145
146/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800147 * Utility
148 */
149/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800150 * Data Conversions
151 */
152/*
Paul McLean30f41852014-04-16 15:44:20 -0700153 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
154 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800155 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700156 * out_buff points to the buffer to receive converted PCM16LE LE samples.
157 * returns
158 * the number of BYTES of output data.
159 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
160 * support PCM24_3LE (24-bit, packed).
161 * NOTE:
Paul McLean30f41852014-04-16 15:44:20 -0700162 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700163 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700164 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700165static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
166 short * out_buff)
167{
Paul McLean30f41852014-04-16 15:44:20 -0700168 /*
169 * Move from front to back so that the conversion can be done in-place
170 * i.e. in_buff == out_buff
171 */
172 /* we need 2 bytes in the output for every 3 bytes in the input */
173 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700174 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700175 size_t src_smpl_index;
176 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
177 src_ptr++; /* lowest-(skip)-byte */
178 *dst_ptr++ = *src_ptr++; /* low-byte */
179 *dst_ptr++ = *src_ptr++; /* high-byte */
180 }
181
182 /* return number of *bytes* generated: */
183 return num_in_samples * 2;
184}
185
186/*
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700187 * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
188 * in_buff points to the buffer of PCM32 samples
189 * num_in_samples size of input buffer in SAMPLES
190 * out_buff points to the buffer to receive converted PCM16LE LE samples.
191 * returns
192 * the number of BYTES of output data.
193 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
194 * support PCM_FORMAT_S32_LE (32-bit).
195 * NOTE:
196 * This conversion is safe to do in-place (in_buff == out_buff).
197 * TODO Move this to a utilities module.
198 */
199static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
200{
201 /*
202 * Move from front to back so that the conversion can be done in-place
203 * i.e. in_buff == out_buff
204 */
205
206 short * dst_ptr = out_buff;
207 const int32_t* src_ptr = in_buff;
208 size_t src_smpl_index;
209 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
210 *dst_ptr++ = *src_ptr++ >> 16;
211 }
212
213 /* return number of *bytes* generated: */
214 return num_in_samples * 2;
215}
216
217/*
Paul McLean30f41852014-04-16 15:44:20 -0700218 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
219 * (where N < M).
220 * in_buff points to the buffer of PCM16 samples
221 * in_buff_channels Specifies the number of channels in the input buffer.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800222 * out_buff points to the buffer to receive converted PCM16 samples.
Paul McLean30f41852014-04-16 15:44:20 -0700223 * out_buff_channels Specifies the number of channels in the output buffer.
224 * num_in_samples size of input buffer in SAMPLES
225 * returns
226 * the number of BYTES of output data.
227 * NOTE
228 * channels > N are filled with silence.
229 * This conversion is safe to do in-place (in_buff == out_buff)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800230 * We are doing this since we *always* present to The Framework as STEREO device, but need to
231 * support 4-channel devices.
Paul McLeane32cbc12014-06-25 10:42:07 -0700232 * TODO Move this to a utilities module.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800233 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700234static size_t expand_channels_16(const short* in_buff, int in_buff_chans,
Paul McLean30f41852014-04-16 15:44:20 -0700235 short* out_buff, int out_buff_chans,
Paul McLeane32cbc12014-06-25 10:42:07 -0700236 size_t num_in_samples)
237{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800238 /*
239 * Move from back to front so that the conversion can be done in-place
240 * i.e. in_buff == out_buff
Paul McLean30f41852014-04-16 15:44:20 -0700241 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
Paul McLeaneedc92e2013-12-19 15:46:15 -0800242 */
Paul McLean30f41852014-04-16 15:44:20 -0700243 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
244
245 short* dst_ptr = out_buff + num_out_samples - 1;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700246 size_t src_index;
247 const short* src_ptr = in_buff + num_in_samples - 1;
Paul McLean30f41852014-04-16 15:44:20 -0700248 int num_zero_chans = out_buff_chans - in_buff_chans;
249 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
250 int dst_offset;
Paul McLeane32cbc12014-06-25 10:42:07 -0700251 for (dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) {
Paul McLean30f41852014-04-16 15:44:20 -0700252 *dst_ptr-- = 0;
253 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700254 for (; dst_offset < out_buff_chans; dst_offset++) {
Paul McLean30f41852014-04-16 15:44:20 -0700255 *dst_ptr-- = *src_ptr--;
256 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800257 }
258
259 /* return number of *bytes* generated */
Paul McLean30f41852014-04-16 15:44:20 -0700260 return num_out_samples * sizeof(short);
261}
262
263/*
264 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
265 * (where N > M).
266 * in_buff points to the buffer of PCM16 samples
267 * in_buff_channels Specifies the number of channels in the input buffer.
268 * out_buff points to the buffer to receive converted PCM16 samples.
269 * out_buff_channels Specifies the number of channels in the output buffer.
270 * num_in_samples size of input buffer in SAMPLES
271 * returns
272 * the number of BYTES of output data.
273 * NOTE
274 * channels > N are thrown away.
275 * This conversion is safe to do in-place (in_buff == out_buff)
276 * We are doing this since we *always* present to The Framework as STEREO device, but need to
277 * support 4-channel devices.
Paul McLeane32cbc12014-06-25 10:42:07 -0700278 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700279 */
280static size_t contract_channels_16(short* in_buff, int in_buff_chans,
281 short* out_buff, int out_buff_chans,
Paul McLeane32cbc12014-06-25 10:42:07 -0700282 size_t num_in_samples)
283{
Paul McLean30f41852014-04-16 15:44:20 -0700284 /*
285 * Move from front to back so that the conversion can be done in-place
286 * i.e. in_buff == out_buff
287 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
288 */
289 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
290
291 int num_skip_samples = in_buff_chans - out_buff_chans;
292
293 short* dst_ptr = out_buff;
294 short* src_ptr = in_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700295 size_t src_index;
Paul McLean30f41852014-04-16 15:44:20 -0700296 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
297 int dst_offset;
Paul McLeane32cbc12014-06-25 10:42:07 -0700298 for (dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) {
Paul McLean30f41852014-04-16 15:44:20 -0700299 *dst_ptr++ = *src_ptr++;
300 }
301 src_ptr += num_skip_samples;
302 }
303
304 /* return number of *bytes* generated */
305 return num_out_samples * sizeof(short);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800306}
307
308/*
309 * ALSA Utilities
310 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700311/*TODO This table and the function that uses it should be moved to a utilities module (probably) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800312/*
Paul McLeane32cbc12014-06-25 10:42:07 -0700313 * Maps bit-positions in a pcm_mask to the corresponding AUDIO_ format string.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800314 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700315static const char * const format_string_map[] = {
316 "AUDIO_FORMAT_PCM_8_BIT", /* 00 - SNDRV_PCM_FORMAT_S8 */
317 "AUDIO_FORMAT_PCM_8_BIT", /* 01 - SNDRV_PCM_FORMAT_U8 */
318 "AUDIO_FORMAT_PCM_16_BIT", /* 02 - SNDRV_PCM_FORMAT_S16_LE */
319 NULL, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
320 NULL, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
321 NULL, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
322 "AUDIO_FORMAT_PCM_24_BIT_PACKED", /* 06 - SNDRV_PCM_FORMAT_S24_LE */
323 NULL, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
324 NULL, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
325 NULL, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
326 "AUDIO_FORMAT_PCM_32_BIT", /* 10 - SNDRV_PCM_FORMAT_S32_LE */
327 NULL, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
328 NULL, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
329 NULL, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
330 "AUDIO_FORMAT_PCM_FLOAT", /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
331 NULL, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
332 NULL, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
333 NULL, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
334 NULL, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
335 NULL, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
336 NULL, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
337 NULL, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
338 NULL, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
339 NULL, /* 23 - SNDRV_PCM_FORMAT_MPEG */
340 NULL, /* 24 - SNDRV_PCM_FORMAT_GSM */
341 NULL, NULL, NULL, NULL, NULL, NULL, /* 25 -> 30 (not assigned) */
342 NULL, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
343 "AUDIO_FORMAT_PCM_24_BIT_PACKED", /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
344 NULL, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
345 NULL, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
346 NULL, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
347 NULL, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
348 NULL, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
349 NULL, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
350 NULL, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
351 NULL, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
352 NULL, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
353 NULL, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
354 NULL, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
355 NULL, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
356 NULL, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
357 NULL, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
358 NULL, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
359 NULL, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
360 NULL /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
361};
362
363/*
364 * Generate string containing a bar ("|") delimited list of AUDIO_ formats specified in
365 * the mask parameter.
366 *
367 */
368static char* get_format_str_for_mask(struct pcm_mask* mask)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800369{
Paul McLeane32cbc12014-06-25 10:42:07 -0700370 char buffer[256];
371 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
372 buffer[0] = '\0';
373
374 int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]);
375 int bits_per_slot = sizeof(mask->bits[0]) * 8;
376
377 const char* format_str = NULL;
378 int table_size = sizeof(format_string_map)/sizeof(format_string_map[0]);
379
380 int slot_index, bit_index, table_index;
381 table_index = 0;
382 int num_written = 0;
383 for (slot_index = 0; slot_index < num_slots; slot_index++) {
384 unsigned bit_mask = 1;
385 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
386 if ((mask->bits[slot_index] & bit_mask) != 0) {
387 format_str = table_index < table_size
388 ? format_string_map[table_index]
389 : NULL;
390 if (format_str != NULL) {
391 if (num_written != 0) {
392 num_written += snprintf(buffer + num_written,
393 buffer_size - num_written, "|");
394 }
395 num_written += snprintf(buffer + num_written, buffer_size - num_written,
396 "%s", format_str);
397 }
398 }
399 bit_mask <<= 1;
400 table_index++;
Paul McLean30f41852014-04-16 15:44:20 -0700401 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800402 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700403
404 return strdup(buffer);
405}
406
407/*
408 * Maps from bit position in pcm_mask to AUDIO_ format constants.
409 */
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700410static audio_format_t const format_value_map[] = {
Paul McLeane32cbc12014-06-25 10:42:07 -0700411 AUDIO_FORMAT_PCM_8_BIT, /* 00 - SNDRV_PCM_FORMAT_S8 */
412 AUDIO_FORMAT_PCM_8_BIT, /* 01 - SNDRV_PCM_FORMAT_U8 */
413 AUDIO_FORMAT_PCM_16_BIT, /* 02 - SNDRV_PCM_FORMAT_S16_LE */
414 AUDIO_FORMAT_INVALID, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
415 AUDIO_FORMAT_INVALID, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
416 AUDIO_FORMAT_INVALID, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700417 AUDIO_FORMAT_INVALID, /* 06 - SNDRV_PCM_FORMAT_S24_LE */
Paul McLeane32cbc12014-06-25 10:42:07 -0700418 AUDIO_FORMAT_INVALID, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
419 AUDIO_FORMAT_INVALID, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
420 AUDIO_FORMAT_INVALID, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
421 AUDIO_FORMAT_PCM_32_BIT, /* 10 - SNDRV_PCM_FORMAT_S32_LE */
422 AUDIO_FORMAT_INVALID, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
423 AUDIO_FORMAT_INVALID, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
424 AUDIO_FORMAT_INVALID, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
425 AUDIO_FORMAT_PCM_FLOAT, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
426 AUDIO_FORMAT_INVALID, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
427 AUDIO_FORMAT_INVALID, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
428 AUDIO_FORMAT_INVALID, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
429 AUDIO_FORMAT_INVALID, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
430 AUDIO_FORMAT_INVALID, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
431 AUDIO_FORMAT_INVALID, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
432 AUDIO_FORMAT_INVALID, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
433 AUDIO_FORMAT_INVALID, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
434 AUDIO_FORMAT_INVALID, /* 23 - SNDRV_PCM_FORMAT_MPEG */
435 AUDIO_FORMAT_INVALID, /* 24 - SNDRV_PCM_FORMAT_GSM */
436 AUDIO_FORMAT_INVALID, /* 25 -> 30 (not assigned) */
437 AUDIO_FORMAT_INVALID,
438 AUDIO_FORMAT_INVALID,
439 AUDIO_FORMAT_INVALID,
440 AUDIO_FORMAT_INVALID,
441 AUDIO_FORMAT_INVALID,
442 AUDIO_FORMAT_INVALID, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700443 AUDIO_FORMAT_PCM_24_BIT_PACKED, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */
Paul McLeane32cbc12014-06-25 10:42:07 -0700444 AUDIO_FORMAT_INVALID, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
445 AUDIO_FORMAT_INVALID, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
446 AUDIO_FORMAT_INVALID, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
447 AUDIO_FORMAT_INVALID, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
448 AUDIO_FORMAT_INVALID, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
449 AUDIO_FORMAT_INVALID, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
450 AUDIO_FORMAT_INVALID, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
451 AUDIO_FORMAT_INVALID, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
452 AUDIO_FORMAT_INVALID, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
453 AUDIO_FORMAT_INVALID, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
454 AUDIO_FORMAT_INVALID, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
455 AUDIO_FORMAT_INVALID, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
456 AUDIO_FORMAT_INVALID, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
457 AUDIO_FORMAT_INVALID, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
458 AUDIO_FORMAT_INVALID, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
459 AUDIO_FORMAT_INVALID, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
460 AUDIO_FORMAT_INVALID /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
461};
462
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700463/*
464 * Returns true if mask indicates support for PCM_16.
465 */
466static bool mask_has_pcm_16(struct pcm_mask* mask) {
467 return (mask->bits[0] & 0x0004) != 0;
468}
469
Paul McLeane32cbc12014-06-25 10:42:07 -0700470static int get_format_for_mask(struct pcm_mask* mask)
471{
472 int num_slots = sizeof(mask->bits)/ sizeof(mask->bits[0]);
473 int bits_per_slot = sizeof(mask->bits[0]) * 8;
474
475 int table_size = sizeof(format_value_map) / sizeof(format_value_map[0]);
476
477 int slot_index, bit_index, table_index;
478 table_index = 0;
479 int num_written = 0;
480 for (slot_index = 0; slot_index < num_slots; slot_index++) {
481 unsigned bit_mask = 1;
482 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
483 if ((mask->bits[slot_index] & bit_mask) != 0) {
484 /* just return the first one */
485 return table_index < table_size
486 ? format_value_map[table_index]
487 : AUDIO_FORMAT_INVALID;
488 }
489 bit_mask <<= 1;
490 table_index++;
491 }
492 }
493
494 return AUDIO_FORMAT_INVALID;
495}
496
497/*
498 * Maps from bit position in pcm_mask to AUDIO_ format constants.
499 */
500static int const pcm_format_value_map[] = {
501 PCM_FORMAT_S8, /* 00 - SNDRV_PCM_FORMAT_S8 */
502 0, /* 01 - SNDRV_PCM_FORMAT_U8 */
503 PCM_FORMAT_S16_LE, /* 02 - SNDRV_PCM_FORMAT_S16_LE */
504 0, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
505 0, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
506 0, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
507 PCM_FORMAT_S24_3LE, /* 06 - SNDRV_PCM_FORMAT_S24_LE */
508 0, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
509 0, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
510 0, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
511 PCM_FORMAT_S32_LE, /* 10 - SNDRV_PCM_FORMAT_S32_LE */
512 0, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
513 0, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
514 0, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
515 0, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
516 0, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
517 0, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
518 0, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
519 0, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
520 0, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
521 0, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
522 0, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
523 0, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
524 0, /* 23 - SNDRV_PCM_FORMAT_MPEG */
525 0, /* 24 - SNDRV_PCM_FORMAT_GSM */
526 0, /* 25 -> 30 (not assigned) */
527 0,
528 0,
529 0,
530 0,
531 0,
532 0, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
533 PCM_FORMAT_S24_3LE, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
534 0, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
535 0, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
536 0, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
537 0, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
538 0, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
539 0, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
540 0, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
541 0, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
542 0, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
543 0, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
544 0, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
545 0, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
546 0, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
547 0, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
548 0, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
549 0, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
550 0 /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
551};
552
553static int get_pcm_format_for_mask(struct pcm_mask* mask) {
554 int num_slots = sizeof(mask->bits)/ sizeof(mask->bits[0]);
555 int bits_per_slot = sizeof(mask->bits[0]) * 8;
556
557 int table_size = sizeof(pcm_format_value_map) / sizeof(pcm_format_value_map[0]);
558
559 int slot_index, bit_index, table_index;
560 table_index = 0;
561 int num_written = 0;
562 for (slot_index = 0; slot_index < num_slots; slot_index++) {
563 unsigned bit_mask = 1;
564 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
565 if ((mask->bits[slot_index] & bit_mask) != 0) {
566 /* just return the first one */
567 return table_index < table_size
568 ? pcm_format_value_map[table_index]
569 : AUDIO_FORMAT_INVALID;
570 }
571 bit_mask <<= 1;
572 table_index++;
573 }
574 }
575
576 return 0; // is this right?
577}
578
579static void log_pcm_mask(const char* mask_name, struct pcm_mask* mask) {
580 char buff[512];
581 char bit_buff[32];
582 int buffSize = sizeof(buff)/sizeof(buff[0]);
583
584 buff[0] = '\0';
585
586 int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]);
587 int bits_per_slot = sizeof(mask->bits[0]) * 8;
588
589 int slot_index, bit_index;
590 strcat(buff, "[");
591 for (slot_index = 0; slot_index < num_slots; slot_index++) {
592 unsigned bit_mask = 1;
593 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
594 strcat(buff, (mask->bits[slot_index] & bit_mask) != 0 ? "1" : "0");
595 bit_mask <<= 1;
596 }
597 if (slot_index < num_slots - 1) {
598 strcat(buff, ",");
599 }
600 }
601 strcat(buff, "]");
602
603 ALOGV("usb:audio_hw - %s mask:%s", mask_name, buff);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800604}
605
Paul McLeancf611912014-04-28 13:03:18 -0700606static void log_pcm_params(struct pcm_params * alsa_hw_params) {
607 ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u",
608 pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS),
609 pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS));
610 ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u",
611 pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS),
612 pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS));
Paul McLeane32cbc12014-06-25 10:42:07 -0700613 log_pcm_mask("PCM_PARAM_FORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
614 log_pcm_mask("PCM_PARAM_SUBFORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_SUBFORMAT));
Paul McLeancf611912014-04-28 13:03:18 -0700615 ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u",
616 pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
617 pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
618 ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u",
619 pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
620 pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
621 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u",
622 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME),
623 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME));
624 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u",
625 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE),
626 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE));
627 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u",
628 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES),
629 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES));
630 ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u",
631 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS),
632 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS));
633 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u",
634 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME),
635 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME));
636 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u",
637 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE),
638 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE));
639 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u",
640 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES),
641 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES));
642 ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u",
643 pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME),
644 pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME));
645}
646
Paul McLeaneedc92e2013-12-19 15:46:15 -0800647/*
Paul McLean33a6b172014-06-19 12:35:28 -0700648 * Returns the supplied value rounded up to the next even multiple of 16
649 */
650static unsigned int round_to_16_mult(unsigned int size) {
651 return (size + 15) & 0xFFFFFFF0;
652}
653
Paul McLeane32cbc12014-06-25 10:42:07 -0700654/*TODO - Evaluate if this value should/can be retrieved from a device-specific property */
Paul McLean33a6b172014-06-19 12:35:28 -0700655#define MIN_BUFF_TIME 5 /* milliseconds */
656
657/*
658 * Returns the system defined minimum period size based on the supplied sample rate
659 */
660static unsigned int calc_min_period_size(unsigned int sample_rate) {
661 unsigned int period_size = (sample_rate * MIN_BUFF_TIME) / 1000;
662 return round_to_16_mult(period_size);
663}
664
665/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800666 * Reads and decodes configuration info from the specified ALSA card/device
667 */
668static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config)
669{
Paul McLean30f41852014-04-16 15:44:20 -0700670 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 -0800671
672 if (card < 0 || device < 0) {
673 return -EINVAL;
674 }
675
676 struct pcm_params * alsa_hw_params = pcm_params_get(card, device, io_type);
677 if (alsa_hw_params == NULL) {
678 return -EINVAL;
679 }
680
681 /*
682 * This Logging will be useful when testing new USB devices.
683 */
Paul McLeancf611912014-04-28 13:03:18 -0700684 /* log_pcm_params(alsa_hw_params); */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800685
686 config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
687 config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
Paul McLean33a6b172014-06-19 12:35:28 -0700688 config->period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE);
689 /* round this up to a multiple of 16 */
690 config->period_size = round_to_16_mult(config->period_size);
691 /* make sure it is above a minimum value to minimize jitter */
692 unsigned int min_period_size = calc_min_period_size(config->rate);
693 if (config->period_size < min_period_size) {
694 config->period_size = min_period_size;
695 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800696 config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
697
Paul McLeane32cbc12014-06-25 10:42:07 -0700698 config->format = get_pcm_format_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800699 return 0;
700}
701
702/*
703 * HAl Functions
704 */
Simon Wilson19957a32012-04-06 16:17:12 -0700705/**
706 * NOTE: when multiple mutexes have to be acquired, always respect the
707 * following order: hw device > out stream
708 */
709
710/* Helper functions */
Simon Wilson19957a32012-04-06 16:17:12 -0700711static uint32_t out_get_sample_rate(const struct audio_stream *stream)
712{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800713 return cached_output_hardware_config.rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700714}
715
716static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
717{
718 return 0;
719}
720
721static size_t out_get_buffer_size(const struct audio_stream *stream)
722{
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700723 return cached_output_hardware_config.period_size *
724 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700725}
726
727static uint32_t out_get_channels(const struct audio_stream *stream)
728{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800729 // Always Stero for now. We will do *some* conversions in this HAL.
Paul McLeane32cbc12014-06-25 10:42:07 -0700730 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
731 rewrite this to return the ACTUAL channel format */
Simon Wilson19957a32012-04-06 16:17:12 -0700732 return AUDIO_CHANNEL_OUT_STEREO;
733}
734
735static audio_format_t out_get_format(const struct audio_stream *stream)
736{
Paul McLeane32cbc12014-06-25 10:42:07 -0700737 return audio_format_from_pcm_format(cached_output_hardware_config.format);
Simon Wilson19957a32012-04-06 16:17:12 -0700738}
739
740static int out_set_format(struct audio_stream *stream, audio_format_t format)
741{
Paul McLeane32cbc12014-06-25 10:42:07 -0700742 cached_output_hardware_config.format = pcm_format_from_audio_format(format);
Simon Wilson19957a32012-04-06 16:17:12 -0700743 return 0;
744}
745
746static int out_standby(struct audio_stream *stream)
747{
748 struct stream_out *out = (struct stream_out *)stream;
749
750 pthread_mutex_lock(&out->dev->lock);
751 pthread_mutex_lock(&out->lock);
752
753 if (!out->standby) {
754 pcm_close(out->pcm);
755 out->pcm = NULL;
756 out->standby = true;
757 }
758
759 pthread_mutex_unlock(&out->lock);
760 pthread_mutex_unlock(&out->dev->lock);
761
762 return 0;
763}
764
765static int out_dump(const struct audio_stream *stream, int fd)
766{
767 return 0;
768}
769
770static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
771{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800772 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
773
Simon Wilson19957a32012-04-06 16:17:12 -0700774 struct stream_out *out = (struct stream_out *)stream;
775 struct audio_device *adev = out->dev;
776 struct str_parms *parms;
777 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800778 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700779 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800780 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700781
782 parms = str_parms_create_str(kvpairs);
783 pthread_mutex_lock(&adev->lock);
784
Paul McLeaneedc92e2013-12-19 15:46:15 -0800785 bool recache_device_params = false;
786 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
787 if (param_val >= 0) {
788 adev->out_card = atoi(value);
789 recache_device_params = true;
790 }
Simon Wilson19957a32012-04-06 16:17:12 -0700791
Paul McLeaneedc92e2013-12-19 15:46:15 -0800792 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
793 if (param_val >= 0) {
794 adev->out_device = atoi(value);
795 recache_device_params = true;
796 }
797
798 if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) {
799 ret_value = read_alsa_device_config(adev->out_card, adev->out_device, PCM_OUT,
Paul McLean30f41852014-04-16 15:44:20 -0700800 &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800801 output_hardware_config_is_cached = (ret_value == 0);
802 }
Simon Wilson19957a32012-04-06 16:17:12 -0700803
804 pthread_mutex_unlock(&adev->lock);
805 str_parms_destroy(parms);
806
Paul McLeaneedc92e2013-12-19 15:46:15 -0800807 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700808}
809
Paul McLeane32cbc12014-06-25 10:42:07 -0700810/*TODO it seems like both out_get_parameters() and in_get_parameters()
811 could be written in terms of a get_device_parameters(io_type) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800812
Paul McLean30f41852014-04-16 15:44:20 -0700813static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
814{
815 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
816
Paul McLeaneedc92e2013-12-19 15:46:15 -0800817 struct stream_out *out = (struct stream_out *) stream;
818 struct audio_device *adev = out->dev;
819
Paul McLean30f41852014-04-16 15:44:20 -0700820 if (adev->out_card < 0 || adev->out_device < 0)
821 return strdup("");
822
Paul McLeaneedc92e2013-12-19 15:46:15 -0800823 unsigned min, max;
824
825 struct str_parms *query = str_parms_create_str(keys);
826 struct str_parms *result = str_parms_create();
827
828 int num_written = 0;
829 char buffer[256];
830 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
831 char* result_str = NULL;
832
833 struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT);
834
835 // These keys are from hardware/libhardware/include/audio.h
836 // supported sample rates
837 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
838 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
839 // if they are different, return a list containing those two values, otherwise just the one.
840 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
841 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700842 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800843 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700844 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800845 }
846 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
847 buffer);
848 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
849
850 // supported channel counts
851 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
852 // Similarly for output channels count
Paul McLeane32cbc12014-06-25 10:42:07 -0700853 /* TODO - This is wrong, we need format strings, not numbers (another CL) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800854 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
855 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700856 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800857 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700858 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800859 }
860 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer);
861 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
862
863 // supported sample formats
864 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700865 char * format_params =
866 get_format_str_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
867 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, format_params);
868 free(format_params);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800869 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
870
871 result_str = str_parms_to_str(result);
872
873 // done with these...
874 str_parms_destroy(query);
875 str_parms_destroy(result);
876
Paul McLeane32cbc12014-06-25 10:42:07 -0700877 ALOGV("usb:audio_hw::out out_get_parameters() = %s", result_str);
878
Paul McLeaneedc92e2013-12-19 15:46:15 -0800879 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700880}
881
882static uint32_t out_get_latency(const struct audio_stream_out *stream)
883{
Paul McLean30f41852014-04-16 15:44:20 -0700884 struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800885
Paul McLeane32cbc12014-06-25 10:42:07 -0700886 /*TODO Do we need a term here for the USB latency (as reported in the USB descriptors)? */
Paul McLean30f41852014-04-16 15:44:20 -0700887 uint32_t latency = (cached_output_hardware_config.period_size
Paul McLeane32cbc12014-06-25 10:42:07 -0700888 * cached_output_hardware_config.period_count * 1000)
889 / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800890 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700891}
892
Paul McLean30f41852014-04-16 15:44:20 -0700893static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700894{
895 return -ENOSYS;
896}
897
Paul McLeaneedc92e2013-12-19 15:46:15 -0800898/* must be called with hw device and output stream mutexes locked */
899static int start_output_stream(struct stream_out *out)
900{
901 struct audio_device *adev = out->dev;
902 int return_val = 0;
903
Paul McLean30f41852014-04-16 15:44:20 -0700904 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
905 adev->out_card, adev->out_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800906
907 out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config);
Paul McLeane32cbc12014-06-25 10:42:07 -0700908
Paul McLeaneedc92e2013-12-19 15:46:15 -0800909 if (out->pcm == NULL) {
910 return -ENOMEM;
911 }
912
913 if (out->pcm && !pcm_is_ready(out->pcm)) {
914 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
915 pcm_close(out->pcm);
916 return -ENOMEM;
917 }
918
Paul McLeaneedc92e2013-12-19 15:46:15 -0800919 return 0;
920}
921
922static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700923{
924 int ret;
925 struct stream_out *out = (struct stream_out *)stream;
926
927 pthread_mutex_lock(&out->dev->lock);
928 pthread_mutex_lock(&out->lock);
929 if (out->standby) {
930 ret = start_output_stream(out);
931 if (ret != 0) {
932 goto err;
933 }
934 out->standby = false;
935 }
936
Paul McLean30f41852014-04-16 15:44:20 -0700937 // Setup conversion buffer
938 // compute maximum potential buffer size.
939 // * 2 for stereo -> quad conversion
940 // * 3/2 for 16bit -> 24 bit conversion
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700941 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700942 if (required_conversion_buffer_size > out->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700943 /* TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
944 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -0700945 out->conversion_buffer_size = required_conversion_buffer_size;
946 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
947 }
948
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700949 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800950 int num_write_buff_bytes = bytes;
951
952 /*
953 * Num Channels conversion
954 */
955 int num_device_channels = cached_output_hardware_config.channels;
956 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700957 if (num_device_channels != num_req_channels) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800958 num_write_buff_bytes =
Paul McLean30f41852014-04-16 15:44:20 -0700959 expand_channels_16(write_buff, num_req_channels,
960 out->conversion_buffer, num_device_channels,
961 num_write_buff_bytes / sizeof(short));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800962 write_buff = out->conversion_buffer;
963 }
964
Paul McLeaneedc92e2013-12-19 15:46:15 -0800965 if (write_buff != NULL && num_write_buff_bytes != 0) {
966 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
967 }
Simon Wilson19957a32012-04-06 16:17:12 -0700968
969 pthread_mutex_unlock(&out->lock);
970 pthread_mutex_unlock(&out->dev->lock);
971
972 return bytes;
973
974err:
975 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800976 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700977 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700978 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700979 out_get_sample_rate(&stream->common));
980 }
981
982 return bytes;
983}
984
Paul McLean30f41852014-04-16 15:44:20 -0700985static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700986{
987 return -EINVAL;
988}
989
990static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
991{
992 return 0;
993}
994
995static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
996{
997 return 0;
998}
999
Paul McLean30f41852014-04-16 15:44:20 -07001000static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -07001001{
1002 return -EINVAL;
1003}
1004
1005static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001006 audio_io_handle_t handle,
1007 audio_devices_t devices,
1008 audio_output_flags_t flags,
1009 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001010 struct audio_stream_out **stream_out)
1011{
Paul McLean30f41852014-04-16 15:44:20 -07001012 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 -08001013 handle, devices, flags);
1014
Simon Wilson19957a32012-04-06 16:17:12 -07001015 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001016
Simon Wilson19957a32012-04-06 16:17:12 -07001017 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -07001018
1019 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1020 if (!out)
1021 return -ENOMEM;
1022
Paul McLeaneedc92e2013-12-19 15:46:15 -08001023 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -07001024 out->stream.common.get_sample_rate = out_get_sample_rate;
1025 out->stream.common.set_sample_rate = out_set_sample_rate;
1026 out->stream.common.get_buffer_size = out_get_buffer_size;
1027 out->stream.common.get_channels = out_get_channels;
1028 out->stream.common.get_format = out_get_format;
1029 out->stream.common.set_format = out_set_format;
1030 out->stream.common.standby = out_standby;
1031 out->stream.common.dump = out_dump;
1032 out->stream.common.set_parameters = out_set_parameters;
1033 out->stream.common.get_parameters = out_get_parameters;
1034 out->stream.common.add_audio_effect = out_add_audio_effect;
1035 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1036 out->stream.get_latency = out_get_latency;
1037 out->stream.set_volume = out_set_volume;
1038 out->stream.write = out_write;
1039 out->stream.get_render_position = out_get_render_position;
1040 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1041
1042 out->dev = adev;
1043
Paul McLeaneedc92e2013-12-19 15:46:15 -08001044 if (output_hardware_config_is_cached) {
1045 config->sample_rate = cached_output_hardware_config.rate;
1046
Paul McLeane32cbc12014-06-25 10:42:07 -07001047 config->format = audio_format_from_pcm_format(cached_output_hardware_config.format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001048
1049 config->channel_mask =
1050 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
1051 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
1052 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1053 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001054 /*TODO remove this when the above restriction is removed. */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001055 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1056 }
1057 } else {
1058 cached_output_hardware_config = default_alsa_out_config;
1059
1060 config->format = out_get_format(&out->stream.common);
1061 config->channel_mask = out_get_channels(&out->stream.common);
1062 config->sample_rate = out_get_sample_rate(&out->stream.common);
1063 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001064
1065 out->conversion_buffer = NULL;
1066 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001067
1068 out->standby = true;
1069
1070 *stream_out = &out->stream;
1071 return 0;
1072
1073err_open:
1074 free(out);
1075 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001076 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -07001077}
1078
1079static void adev_close_output_stream(struct audio_hw_device *dev,
1080 struct audio_stream_out *stream)
1081{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001082 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -07001083 struct stream_out *out = (struct stream_out *)stream;
1084
Paul McLeane32cbc12014-06-25 10:42:07 -07001085 // Close the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -07001086 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001087
1088 free(out->conversion_buffer);
1089 out->conversion_buffer = NULL;
1090 out->conversion_buffer_size = 0;
1091
Simon Wilson19957a32012-04-06 16:17:12 -07001092 free(stream);
1093}
1094
1095static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1096{
1097 return 0;
1098}
1099
Paul McLean30f41852014-04-16 15:44:20 -07001100static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001101{
1102 return strdup("");
1103}
1104
1105static int adev_init_check(const struct audio_hw_device *dev)
1106{
1107 return 0;
1108}
1109
1110static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1111{
1112 return -ENOSYS;
1113}
1114
1115static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1116{
1117 return -ENOSYS;
1118}
1119
1120static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1121{
1122 return 0;
1123}
1124
1125static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1126{
1127 return -ENOSYS;
1128}
1129
1130static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1131{
1132 return -ENOSYS;
1133}
1134
1135static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001136 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -07001137{
1138 return 0;
1139}
1140
Paul McLeaneedc92e2013-12-19 15:46:15 -08001141/* Helper functions */
1142static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1143{
Paul McLean30f41852014-04-16 15:44:20 -07001144 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001145}
1146
1147static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1148{
1149 return -ENOSYS;
1150}
1151
1152static size_t in_get_buffer_size(const struct audio_stream *stream)
1153{
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001154 size_t buffer_size = cached_input_hardware_config.period_size *
1155 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
1156 ALOGV("usb: in_get_buffer_size() = %zu", buffer_size);
1157 return buffer_size;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001158}
1159
1160static uint32_t in_get_channels(const struct audio_stream *stream)
1161{
Paul McLean30f41852014-04-16 15:44:20 -07001162 // just report stereo for now
1163 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001164}
1165
1166static audio_format_t in_get_format(const struct audio_stream *stream)
1167{
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001168 const struct stream_in * in_stream = (const struct stream_in *)stream;
1169
1170 ALOGV("in_get_format() = %d -> %d", in_stream->input_framework_format,
1171 audio_format_from_pcm_format(in_stream->input_framework_format));
1172 /* return audio_format_from_pcm_format(cached_input_hardware_config.format); */
1173 return audio_format_from_pcm_format(in_stream->input_framework_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001174}
1175
1176static int in_set_format(struct audio_stream *stream, audio_format_t format)
1177{
1178 return -ENOSYS;
1179}
1180
1181static int in_standby(struct audio_stream *stream)
1182{
Paul McLean30f41852014-04-16 15:44:20 -07001183 struct stream_in *in = (struct stream_in *) stream;
1184
1185 pthread_mutex_lock(&in->dev->lock);
1186 pthread_mutex_lock(&in->lock);
1187
1188 if (!in->standby) {
1189 pcm_close(in->pcm);
1190 in->pcm = NULL;
1191 in->standby = true;
1192 }
1193
1194 pthread_mutex_unlock(&in->lock);
1195 pthread_mutex_unlock(&in->dev->lock);
1196
Paul McLeaneedc92e2013-12-19 15:46:15 -08001197 return 0;
1198}
1199
1200static int in_dump(const struct audio_stream *stream, int fd)
1201{
1202 return 0;
1203}
1204
1205static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1206{
Paul McLean30f41852014-04-16 15:44:20 -07001207 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001208
1209 struct stream_in *in = (struct stream_in *)stream;
1210 struct audio_device *adev = in->dev;
1211 struct str_parms *parms;
1212 char value[32];
1213 int param_val;
1214 int routing = 0;
1215 int ret_value = 0;
1216
1217 parms = str_parms_create_str(kvpairs);
1218 pthread_mutex_lock(&adev->lock);
1219
Paul McLean30f41852014-04-16 15:44:20 -07001220 bool recache_device_params = false;
1221
Paul McLeaneedc92e2013-12-19 15:46:15 -08001222 // Card/Device
1223 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1224 if (param_val >= 0) {
1225 adev->in_card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001226 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001227 }
1228
1229 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1230 if (param_val >= 0) {
1231 adev->in_device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001232 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001233 }
1234
Paul McLean30f41852014-04-16 15:44:20 -07001235 if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) {
1236 ret_value = read_alsa_device_config(adev->in_card, adev->in_device,
1237 PCM_IN, &(cached_input_hardware_config));
1238 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001239 }
1240
1241 pthread_mutex_unlock(&adev->lock);
1242 str_parms_destroy(parms);
1243
1244 return ret_value;
1245}
1246
Paul McLeane32cbc12014-06-25 10:42:07 -07001247/*TODO it seems like both out_get_parameters() and in_get_parameters()
1248 could be written in terms of a get_device_parameters(io_type) */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001249
Paul McLean30f41852014-04-16 15:44:20 -07001250static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
1251 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001252
Paul McLean30f41852014-04-16 15:44:20 -07001253 struct stream_in *in = (struct stream_in *)stream;
1254 struct audio_device *adev = in->dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001255
Paul McLean30f41852014-04-16 15:44:20 -07001256 if (adev->in_card < 0 || adev->in_device < 0)
1257 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001258
Paul McLean30f41852014-04-16 15:44:20 -07001259 struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN);
1260 if (alsa_hw_params == NULL)
1261 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001262
Paul McLean30f41852014-04-16 15:44:20 -07001263 struct str_parms *query = str_parms_create_str(keys);
1264 struct str_parms *result = str_parms_create();
Paul McLeaneedc92e2013-12-19 15:46:15 -08001265
Paul McLean30f41852014-04-16 15:44:20 -07001266 int num_written = 0;
1267 char buffer[256];
1268 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
1269 char* result_str = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001270
Paul McLean30f41852014-04-16 15:44:20 -07001271 unsigned min, max;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001272
Paul McLean30f41852014-04-16 15:44:20 -07001273 // These keys are from hardware/libhardware/include/audio.h
1274 // supported sample rates
1275 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
1276 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
1277 // if they are different, return a list containing those two values, otherwise just the one.
1278 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
1279 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001280 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001281 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001282 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001283 }
1284 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer);
1285 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
Paul McLeaneedc92e2013-12-19 15:46:15 -08001286
Paul McLean30f41852014-04-16 15:44:20 -07001287 // supported channel counts
1288 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
1289 // Similarly for output channels count
Paul McLeane32cbc12014-06-25 10:42:07 -07001290 // TODO This is wrong, we need format strings, not numbers (another CL)
Paul McLean30f41852014-04-16 15:44:20 -07001291 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
1292 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001293 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001294 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001295 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001296 }
1297 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer);
1298 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001299
Paul McLean30f41852014-04-16 15:44:20 -07001300 // supported sample formats
1301 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001302 struct pcm_mask * format_mask = pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT);
1303 char * format_params = get_format_str_for_mask(format_mask);
1304 if (!mask_has_pcm_16(format_mask)) {
1305 /* For now, always support PCM_16 and convert locally if necessary */
1306 char buff[256];
1307 snprintf(buff, sizeof(buff), "AUDIO_FORMAT_PCM_16_BIT|%s", format_params);
1308 free(format_params);
1309 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buff);
1310 } else {
1311 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, format_params);
Paul McLean30f41852014-04-16 15:44:20 -07001312 }
Paul McLean30f41852014-04-16 15:44:20 -07001313 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001314
Paul McLean30f41852014-04-16 15:44:20 -07001315 result_str = str_parms_to_str(result);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001316
Paul McLean30f41852014-04-16 15:44:20 -07001317 // done with these...
1318 str_parms_destroy(query);
1319 str_parms_destroy(result);
1320
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001321 ALOGV("usb:audio_hw::in in_get_parameters() = %s", result_str);
1322
Paul McLean30f41852014-04-16 15:44:20 -07001323 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001324}
1325
1326static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1327{
1328 return 0;
1329}
1330
1331static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1332{
1333 return 0;
1334}
1335
Paul McLean30f41852014-04-16 15:44:20 -07001336static int in_set_gain(struct audio_stream_in *stream, float gain)
1337{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001338 return 0;
1339}
1340
Paul McLean30f41852014-04-16 15:44:20 -07001341/* must be called with hw device and output stream mutexes locked */
1342static int start_input_stream(struct stream_in *in) {
1343 struct audio_device *adev = in->dev;
1344 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001345
Paul McLean30f41852014-04-16 15:44:20 -07001346 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
1347 adev->in_card, adev->in_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001348
Paul McLean30f41852014-04-16 15:44:20 -07001349 in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config);
1350 if (in->pcm == NULL) {
1351 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1352 return -ENOMEM;
1353 }
1354
1355 if (in->pcm && !pcm_is_ready(in->pcm)) {
1356 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1357 pcm_close(in->pcm);
1358 return -ENOMEM;
1359 }
1360
1361 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001362}
1363
Paul McLeane32cbc12014-06-25 10:42:07 -07001364/* TODO mutex stuff here (see out_write) */
Paul McLean30f41852014-04-16 15:44:20 -07001365static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1366{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001367 size_t num_read_buff_bytes = 0;
Paul McLean30f41852014-04-16 15:44:20 -07001368 void * read_buff = buffer;
1369 void * out_buff = buffer;
1370
1371 struct stream_in * in = (struct stream_in *) stream;
1372
1373 pthread_mutex_lock(&in->dev->lock);
1374 pthread_mutex_lock(&in->lock);
1375
1376 if (in->standby) {
1377 if (start_input_stream(in) != 0) {
1378 goto err;
1379 }
1380 in->standby = false;
1381 }
1382
1383 // OK, we need to figure out how much data to read to be able to output the requested
1384 // number of bytes in the HAL format (16-bit, stereo).
1385 num_read_buff_bytes = bytes;
1386 int num_device_channels = cached_input_hardware_config.channels;
1387 int num_req_channels = 2; /* always, for now */
1388
1389 if (num_device_channels != num_req_channels) {
Paul McLeancf611912014-04-28 13:03:18 -07001390 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
Paul McLean30f41852014-04-16 15:44:20 -07001391 }
1392
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001393 /* Assume (for now) that in->input_framework_format == PCM_FORMAT_S16_LE */
Eric Laurent7661a482014-06-11 12:00:16 -07001394 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001395 /* 24-bit USB device */
Paul McLean30f41852014-04-16 15:44:20 -07001396 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001397 } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) {
1398 /* 32-bit USB device */
1399 num_read_buff_bytes = num_read_buff_bytes * 2;
Paul McLean30f41852014-04-16 15:44:20 -07001400 }
1401
1402 // Setup/Realloc the conversion buffer (if necessary).
1403 if (num_read_buff_bytes != bytes) {
1404 if (num_read_buff_bytes > in->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -07001405 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1406 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -07001407 in->conversion_buffer_size = num_read_buff_bytes;
1408 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1409 }
1410 read_buff = in->conversion_buffer;
1411 }
1412
1413 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1414 /*
1415 * Do any conversions necessary to send the data in the format specified to/by the HAL
1416 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1417 */
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001418 if (cached_input_hardware_config.format != PCM_FORMAT_S16_LE) {
1419 // we need to convert
Paul McLean30f41852014-04-16 15:44:20 -07001420 if (num_device_channels != num_req_channels) {
1421 out_buff = read_buff;
1422 }
1423
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001424 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
1425 num_read_buff_bytes =
1426 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1427 } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) {
1428 num_read_buff_bytes =
1429 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
1430 }
1431 else {
1432 goto err;
1433 }
Paul McLean30f41852014-04-16 15:44:20 -07001434 }
1435
1436 if (num_device_channels != num_req_channels) {
1437 out_buff = buffer;
1438 /* Num Channels conversion */
Paul McLeancf611912014-04-28 13:03:18 -07001439 if (num_device_channels < num_req_channels) {
1440 num_read_buff_bytes =
Paul McLeancf611912014-04-28 13:03:18 -07001441 expand_channels_16(read_buff, num_device_channels,
1442 out_buff, num_req_channels,
1443 num_read_buff_bytes / sizeof(short));
Eric Laurentfbc02dc2014-06-27 18:39:21 -07001444 } else {
1445 num_read_buff_bytes =
1446 contract_channels_16(read_buff, num_device_channels,
1447 out_buff, num_req_channels,
1448 num_read_buff_bytes / sizeof(short));
Paul McLeancf611912014-04-28 13:03:18 -07001449 }
Paul McLean30f41852014-04-16 15:44:20 -07001450 }
1451 }
1452
1453err:
1454 pthread_mutex_unlock(&in->lock);
1455 pthread_mutex_unlock(&in->dev->lock);
1456
1457 return num_read_buff_bytes;
1458}
1459
1460static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1461{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001462 return 0;
1463}
1464
Mike Lockwood46a98092012-04-24 16:41:18 -07001465static int adev_open_input_stream(struct audio_hw_device *dev,
1466 audio_io_handle_t handle,
1467 audio_devices_t devices,
Paul McLean30f41852014-04-16 15:44:20 -07001468 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001469 struct audio_stream_in **stream_in)
1470{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001471 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLean30f41852014-04-16 15:44:20 -07001472 config->sample_rate, config->channel_mask, config->format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001473
1474 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Eric Laurent7661a482014-06-11 12:00:16 -07001475 int ret = 0;
1476
Paul McLeaneedc92e2013-12-19 15:46:15 -08001477 if (in == NULL)
1478 return -ENOMEM;
1479
1480 // setup function pointers
1481 in->stream.common.get_sample_rate = in_get_sample_rate;
1482 in->stream.common.set_sample_rate = in_set_sample_rate;
1483 in->stream.common.get_buffer_size = in_get_buffer_size;
1484 in->stream.common.get_channels = in_get_channels;
1485 in->stream.common.get_format = in_get_format;
1486 in->stream.common.set_format = in_set_format;
1487 in->stream.common.standby = in_standby;
1488 in->stream.common.dump = in_dump;
1489 in->stream.common.set_parameters = in_set_parameters;
1490 in->stream.common.get_parameters = in_get_parameters;
1491 in->stream.common.add_audio_effect = in_add_audio_effect;
1492 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1493
1494 in->stream.set_gain = in_set_gain;
1495 in->stream.read = in_read;
1496 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1497
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001498 in->input_framework_format = PCM_FORMAT_S16_LE;
1499
Paul McLean30f41852014-04-16 15:44:20 -07001500 in->dev = (struct audio_device *)dev;
1501
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001502 if (!input_hardware_config_is_cached) {
1503 // just return defaults until we can actually query the device.
Paul McLean30f41852014-04-16 15:44:20 -07001504 cached_input_hardware_config = default_alsa_in_config;
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001505 }
Paul McLean30f41852014-04-16 15:44:20 -07001506
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001507 /* Rate */
1508 /* TODO Check that the requested rate is valid for the connected device */
1509 if (config->sample_rate == 0) {
1510 config->sample_rate = cached_input_hardware_config.rate;
1511 } else {
1512 cached_input_hardware_config.rate = config->sample_rate;
1513 }
1514
1515 /* Format */
1516 /* until the framework supports format conversion, just take what it asks for
1517 * i.e. AUDIO_FORMAT_PCM_16_BIT */
1518 /* config->format = audio_format_from_pcm_format(cached_input_hardware_config.format); */
1519 if (config->format == AUDIO_FORMAT_DEFAULT) {
1520 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
1521 * formats */
1522 config->format = AUDIO_FORMAT_PCM_16_BIT;
1523 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
1524 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
1525 * formats */
1526 } else {
1527 /* When the framework support other formats, validate here */
1528 config->format = AUDIO_FORMAT_PCM_16_BIT;
1529 ret = -EINVAL;
1530 }
1531
1532 /* don't change the cached_input_hardware_config, we will open it as what it is and
1533 * convert as necessary */
1534 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1535 /* just return AUDIO_CHANNEL_IN_STEREO until the framework supports other input
1536 * formats */
1537 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
1538 } else if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
1539 /* allow only stereo capture for now */
1540 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
1541 ret = -EINVAL;
Paul McLean30f41852014-04-16 15:44:20 -07001542 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001543
1544 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001545
Paul McLean30f41852014-04-16 15:44:20 -07001546 in->conversion_buffer = NULL;
1547 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001548
1549 *stream_in = &in->stream;
1550
Eric Laurent7661a482014-06-11 12:00:16 -07001551 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001552}
1553
Paul McLean30f41852014-04-16 15:44:20 -07001554static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001555{
Paul McLean30f41852014-04-16 15:44:20 -07001556 struct stream_in *in = (struct stream_in *)stream;
1557
Paul McLeane32cbc12014-06-25 10:42:07 -07001558 // Close the pcm device
Paul McLean30f41852014-04-16 15:44:20 -07001559 in_standby(&stream->common);
1560
1561 free(in->conversion_buffer);
1562
1563 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001564}
1565
1566static int adev_dump(const audio_hw_device_t *device, int fd)
1567{
1568 return 0;
1569}
1570
1571static int adev_close(hw_device_t *device)
1572{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001573 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001574 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001575
1576 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001577 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001578
Simon Wilson19957a32012-04-06 16:17:12 -07001579 return 0;
1580}
1581
Paul McLean30f41852014-04-16 15:44:20 -07001582static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001583{
Simon Wilson19957a32012-04-06 16:17:12 -07001584 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1585 return -EINVAL;
1586
Paul McLeaneedc92e2013-12-19 15:46:15 -08001587 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001588 if (!adev)
1589 return -ENOMEM;
1590
1591 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001592 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001593 adev->hw_device.common.module = (struct hw_module_t *) module;
1594 adev->hw_device.common.close = adev_close;
1595
Simon Wilson19957a32012-04-06 16:17:12 -07001596 adev->hw_device.init_check = adev_init_check;
1597 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1598 adev->hw_device.set_master_volume = adev_set_master_volume;
1599 adev->hw_device.set_mode = adev_set_mode;
1600 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1601 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1602 adev->hw_device.set_parameters = adev_set_parameters;
1603 adev->hw_device.get_parameters = adev_get_parameters;
1604 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1605 adev->hw_device.open_output_stream = adev_open_output_stream;
1606 adev->hw_device.close_output_stream = adev_close_output_stream;
1607 adev->hw_device.open_input_stream = adev_open_input_stream;
1608 adev->hw_device.close_input_stream = adev_close_input_stream;
1609 adev->hw_device.dump = adev_dump;
1610
1611 *device = &adev->hw_device.common;
1612
1613 return 0;
1614}
1615
1616static struct hw_module_methods_t hal_module_methods = {
1617 .open = adev_open,
1618};
1619
1620struct audio_module HAL_MODULE_INFO_SYM = {
1621 .common = {
1622 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001623 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1624 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001625 .id = AUDIO_HARDWARE_MODULE_ID,
1626 .name = "USB audio HW HAL",
1627 .author = "The Android Open Source Project",
1628 .methods = &hal_module_methods,
1629 },
1630};