blob: 7b6501434cb1fbda506c03af86a52f82859eb457 [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 McLean30f41852014-04-16 15:44:20 -0700105 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
Paul McLeaneedc92e2013-12-19 15:46:15 -0800106 * but that will involve changes in The Framework.
107 */
108static struct pcm_config cached_output_hardware_config;
109static bool output_hardware_config_is_cached = false;
110
111struct stream_in {
112 struct audio_stream_in stream;
113
Simon Wilson19957a32012-04-06 16:17:12 -0700114 pthread_mutex_t lock; /* see note below on mutex acquisition order */
115 struct pcm *pcm;
116 bool standby;
117
118 struct audio_device *dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800119
120 struct audio_config hal_pcm_config;
121
Paul McLeaneedc92e2013-12-19 15:46:15 -0800122// struct resampler_itfe *resampler;
123// struct resampler_buffer_provider buf_provider;
Paul McLean30f41852014-04-16 15:44:20 -0700124
Paul McLeaneedc92e2013-12-19 15:46:15 -0800125 int read_status;
Paul McLean30f41852014-04-16 15:44:20 -0700126
127 // We may need to read more data from the device in order to data reduce to 16bit, 4chan */
128 void * conversion_buffer; /* any conversions are put into here
129 * they could come from here too if
130 * there was a previous conversion */
131 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700132};
133
Paul McLeaneedc92e2013-12-19 15:46:15 -0800134/*
Paul McLean30f41852014-04-16 15:44:20 -0700135 * Input Configuration Cache
136 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
137 * but that will involve changes in The Framework.
138 */
139static struct pcm_config cached_input_hardware_config;
140static bool input_hardware_config_is_cached = false;
141
142/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800143 * Utility
144 */
145/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800146 * Data Conversions
147 */
148/*
Paul McLean30f41852014-04-16 15:44:20 -0700149 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
150 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800151 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700152 * out_buff points to the buffer to receive converted PCM16LE LE samples.
153 * returns
154 * the number of BYTES of output data.
155 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
156 * support PCM24_3LE (24-bit, packed).
157 * NOTE:
158 * We're just filling the low-order byte of the PCM24LE samples with 0.
159 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700160 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700161 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700162static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
163 short * out_buff)
164{
Paul McLean30f41852014-04-16 15:44:20 -0700165 /*
166 * Move from front to back so that the conversion can be done in-place
167 * i.e. in_buff == out_buff
168 */
169 /* we need 2 bytes in the output for every 3 bytes in the input */
170 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700171 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700172 size_t src_smpl_index;
173 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
174 src_ptr++; /* lowest-(skip)-byte */
175 *dst_ptr++ = *src_ptr++; /* low-byte */
176 *dst_ptr++ = *src_ptr++; /* high-byte */
177 }
178
179 /* return number of *bytes* generated: */
180 return num_in_samples * 2;
181}
182
183/*
184 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
185 * (where N < M).
186 * in_buff points to the buffer of PCM16 samples
187 * in_buff_channels Specifies the number of channels in the input buffer.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800188 * out_buff points to the buffer to receive converted PCM16 samples.
Paul McLean30f41852014-04-16 15:44:20 -0700189 * out_buff_channels Specifies the number of channels in the output buffer.
190 * num_in_samples size of input buffer in SAMPLES
191 * returns
192 * the number of BYTES of output data.
193 * NOTE
194 * channels > N are filled with silence.
195 * This conversion is safe to do in-place (in_buff == out_buff)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800196 * We are doing this since we *always* present to The Framework as STEREO device, but need to
197 * support 4-channel devices.
Paul McLeane32cbc12014-06-25 10:42:07 -0700198 * TODO Move this to a utilities module.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800199 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700200static size_t expand_channels_16(const short* in_buff, int in_buff_chans,
Paul McLean30f41852014-04-16 15:44:20 -0700201 short* out_buff, int out_buff_chans,
Paul McLeane32cbc12014-06-25 10:42:07 -0700202 size_t num_in_samples)
203{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800204 /*
205 * Move from back to front so that the conversion can be done in-place
206 * i.e. in_buff == out_buff
Paul McLean30f41852014-04-16 15:44:20 -0700207 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
Paul McLeaneedc92e2013-12-19 15:46:15 -0800208 */
Paul McLean30f41852014-04-16 15:44:20 -0700209 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
210
211 short* dst_ptr = out_buff + num_out_samples - 1;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700212 size_t src_index;
213 const short* src_ptr = in_buff + num_in_samples - 1;
Paul McLean30f41852014-04-16 15:44:20 -0700214 int num_zero_chans = out_buff_chans - in_buff_chans;
215 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
216 int dst_offset;
Paul McLeane32cbc12014-06-25 10:42:07 -0700217 for (dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) {
Paul McLean30f41852014-04-16 15:44:20 -0700218 *dst_ptr-- = 0;
219 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700220 for (; dst_offset < out_buff_chans; dst_offset++) {
Paul McLean30f41852014-04-16 15:44:20 -0700221 *dst_ptr-- = *src_ptr--;
222 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800223 }
224
225 /* return number of *bytes* generated */
Paul McLean30f41852014-04-16 15:44:20 -0700226 return num_out_samples * sizeof(short);
227}
228
229/*
230 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
231 * (where N > M).
232 * in_buff points to the buffer of PCM16 samples
233 * in_buff_channels Specifies the number of channels in the input buffer.
234 * out_buff points to the buffer to receive converted PCM16 samples.
235 * out_buff_channels Specifies the number of channels in the output buffer.
236 * num_in_samples size of input buffer in SAMPLES
237 * returns
238 * the number of BYTES of output data.
239 * NOTE
240 * channels > N are thrown away.
241 * This conversion is safe to do in-place (in_buff == out_buff)
242 * We are doing this since we *always* present to The Framework as STEREO device, but need to
243 * support 4-channel devices.
Paul McLeane32cbc12014-06-25 10:42:07 -0700244 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700245 */
246static size_t contract_channels_16(short* in_buff, int in_buff_chans,
247 short* out_buff, int out_buff_chans,
Paul McLeane32cbc12014-06-25 10:42:07 -0700248 size_t num_in_samples)
249{
Paul McLean30f41852014-04-16 15:44:20 -0700250 /*
251 * Move from front to back so that the conversion can be done in-place
252 * i.e. in_buff == out_buff
253 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
254 */
255 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
256
257 int num_skip_samples = in_buff_chans - out_buff_chans;
258
259 short* dst_ptr = out_buff;
260 short* src_ptr = in_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700261 size_t src_index;
Paul McLean30f41852014-04-16 15:44:20 -0700262 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
263 int dst_offset;
Paul McLeane32cbc12014-06-25 10:42:07 -0700264 for (dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) {
Paul McLean30f41852014-04-16 15:44:20 -0700265 *dst_ptr++ = *src_ptr++;
266 }
267 src_ptr += num_skip_samples;
268 }
269
270 /* return number of *bytes* generated */
271 return num_out_samples * sizeof(short);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800272}
273
274/*
275 * ALSA Utilities
276 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700277/*TODO This table and the function that uses it should be moved to a utilities module (probably) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800278/*
Paul McLeane32cbc12014-06-25 10:42:07 -0700279 * Maps bit-positions in a pcm_mask to the corresponding AUDIO_ format string.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800280 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700281static const char * const format_string_map[] = {
282 "AUDIO_FORMAT_PCM_8_BIT", /* 00 - SNDRV_PCM_FORMAT_S8 */
283 "AUDIO_FORMAT_PCM_8_BIT", /* 01 - SNDRV_PCM_FORMAT_U8 */
284 "AUDIO_FORMAT_PCM_16_BIT", /* 02 - SNDRV_PCM_FORMAT_S16_LE */
285 NULL, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
286 NULL, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
287 NULL, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
288 "AUDIO_FORMAT_PCM_24_BIT_PACKED", /* 06 - SNDRV_PCM_FORMAT_S24_LE */
289 NULL, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
290 NULL, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
291 NULL, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
292 "AUDIO_FORMAT_PCM_32_BIT", /* 10 - SNDRV_PCM_FORMAT_S32_LE */
293 NULL, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
294 NULL, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
295 NULL, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
296 "AUDIO_FORMAT_PCM_FLOAT", /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
297 NULL, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
298 NULL, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
299 NULL, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
300 NULL, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
301 NULL, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
302 NULL, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
303 NULL, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
304 NULL, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
305 NULL, /* 23 - SNDRV_PCM_FORMAT_MPEG */
306 NULL, /* 24 - SNDRV_PCM_FORMAT_GSM */
307 NULL, NULL, NULL, NULL, NULL, NULL, /* 25 -> 30 (not assigned) */
308 NULL, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
309 "AUDIO_FORMAT_PCM_24_BIT_PACKED", /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
310 NULL, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
311 NULL, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
312 NULL, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
313 NULL, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
314 NULL, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
315 NULL, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
316 NULL, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
317 NULL, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
318 NULL, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
319 NULL, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
320 NULL, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
321 NULL, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
322 NULL, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
323 NULL, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
324 NULL, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
325 NULL, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
326 NULL /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
327};
328
329/*
330 * Generate string containing a bar ("|") delimited list of AUDIO_ formats specified in
331 * the mask parameter.
332 *
333 */
334static char* get_format_str_for_mask(struct pcm_mask* mask)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800335{
Paul McLeane32cbc12014-06-25 10:42:07 -0700336 char buffer[256];
337 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
338 buffer[0] = '\0';
339
340 int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]);
341 int bits_per_slot = sizeof(mask->bits[0]) * 8;
342
343 const char* format_str = NULL;
344 int table_size = sizeof(format_string_map)/sizeof(format_string_map[0]);
345
346 int slot_index, bit_index, table_index;
347 table_index = 0;
348 int num_written = 0;
349 for (slot_index = 0; slot_index < num_slots; slot_index++) {
350 unsigned bit_mask = 1;
351 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
352 if ((mask->bits[slot_index] & bit_mask) != 0) {
353 format_str = table_index < table_size
354 ? format_string_map[table_index]
355 : NULL;
356 if (format_str != NULL) {
357 if (num_written != 0) {
358 num_written += snprintf(buffer + num_written,
359 buffer_size - num_written, "|");
360 }
361 num_written += snprintf(buffer + num_written, buffer_size - num_written,
362 "%s", format_str);
363 }
364 }
365 bit_mask <<= 1;
366 table_index++;
Paul McLean30f41852014-04-16 15:44:20 -0700367 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800368 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700369
370 return strdup(buffer);
371}
372
373/*
374 * Maps from bit position in pcm_mask to AUDIO_ format constants.
375 */
376static int const format_value_map[] = {
377 AUDIO_FORMAT_PCM_8_BIT, /* 00 - SNDRV_PCM_FORMAT_S8 */
378 AUDIO_FORMAT_PCM_8_BIT, /* 01 - SNDRV_PCM_FORMAT_U8 */
379 AUDIO_FORMAT_PCM_16_BIT, /* 02 - SNDRV_PCM_FORMAT_S16_LE */
380 AUDIO_FORMAT_INVALID, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
381 AUDIO_FORMAT_INVALID, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
382 AUDIO_FORMAT_INVALID, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
383 AUDIO_FORMAT_PCM_24_BIT_PACKED, /* 06 - SNDRV_PCM_FORMAT_S24_LE */
384 AUDIO_FORMAT_INVALID, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
385 AUDIO_FORMAT_INVALID, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
386 AUDIO_FORMAT_INVALID, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
387 AUDIO_FORMAT_PCM_32_BIT, /* 10 - SNDRV_PCM_FORMAT_S32_LE */
388 AUDIO_FORMAT_INVALID, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
389 AUDIO_FORMAT_INVALID, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
390 AUDIO_FORMAT_INVALID, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
391 AUDIO_FORMAT_PCM_FLOAT, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
392 AUDIO_FORMAT_INVALID, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
393 AUDIO_FORMAT_INVALID, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
394 AUDIO_FORMAT_INVALID, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
395 AUDIO_FORMAT_INVALID, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
396 AUDIO_FORMAT_INVALID, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
397 AUDIO_FORMAT_INVALID, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
398 AUDIO_FORMAT_INVALID, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
399 AUDIO_FORMAT_INVALID, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
400 AUDIO_FORMAT_INVALID, /* 23 - SNDRV_PCM_FORMAT_MPEG */
401 AUDIO_FORMAT_INVALID, /* 24 - SNDRV_PCM_FORMAT_GSM */
402 AUDIO_FORMAT_INVALID, /* 25 -> 30 (not assigned) */
403 AUDIO_FORMAT_INVALID,
404 AUDIO_FORMAT_INVALID,
405 AUDIO_FORMAT_INVALID,
406 AUDIO_FORMAT_INVALID,
407 AUDIO_FORMAT_INVALID,
408 AUDIO_FORMAT_INVALID, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
409 AUDIO_FORMAT_PCM_24_BIT_PACKED, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
410 AUDIO_FORMAT_INVALID, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
411 AUDIO_FORMAT_INVALID, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
412 AUDIO_FORMAT_INVALID, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
413 AUDIO_FORMAT_INVALID, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
414 AUDIO_FORMAT_INVALID, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
415 AUDIO_FORMAT_INVALID, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
416 AUDIO_FORMAT_INVALID, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
417 AUDIO_FORMAT_INVALID, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
418 AUDIO_FORMAT_INVALID, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
419 AUDIO_FORMAT_INVALID, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
420 AUDIO_FORMAT_INVALID, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
421 AUDIO_FORMAT_INVALID, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
422 AUDIO_FORMAT_INVALID, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
423 AUDIO_FORMAT_INVALID, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
424 AUDIO_FORMAT_INVALID, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
425 AUDIO_FORMAT_INVALID, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
426 AUDIO_FORMAT_INVALID /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
427};
428
429static int get_format_for_mask(struct pcm_mask* mask)
430{
431 int num_slots = sizeof(mask->bits)/ sizeof(mask->bits[0]);
432 int bits_per_slot = sizeof(mask->bits[0]) * 8;
433
434 int table_size = sizeof(format_value_map) / sizeof(format_value_map[0]);
435
436 int slot_index, bit_index, table_index;
437 table_index = 0;
438 int num_written = 0;
439 for (slot_index = 0; slot_index < num_slots; slot_index++) {
440 unsigned bit_mask = 1;
441 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
442 if ((mask->bits[slot_index] & bit_mask) != 0) {
443 /* just return the first one */
444 return table_index < table_size
445 ? format_value_map[table_index]
446 : AUDIO_FORMAT_INVALID;
447 }
448 bit_mask <<= 1;
449 table_index++;
450 }
451 }
452
453 return AUDIO_FORMAT_INVALID;
454}
455
456/*
457 * Maps from bit position in pcm_mask to AUDIO_ format constants.
458 */
459static int const pcm_format_value_map[] = {
460 PCM_FORMAT_S8, /* 00 - SNDRV_PCM_FORMAT_S8 */
461 0, /* 01 - SNDRV_PCM_FORMAT_U8 */
462 PCM_FORMAT_S16_LE, /* 02 - SNDRV_PCM_FORMAT_S16_LE */
463 0, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
464 0, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
465 0, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
466 PCM_FORMAT_S24_3LE, /* 06 - SNDRV_PCM_FORMAT_S24_LE */
467 0, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
468 0, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
469 0, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
470 PCM_FORMAT_S32_LE, /* 10 - SNDRV_PCM_FORMAT_S32_LE */
471 0, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
472 0, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
473 0, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
474 0, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
475 0, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
476 0, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
477 0, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
478 0, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
479 0, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
480 0, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
481 0, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
482 0, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
483 0, /* 23 - SNDRV_PCM_FORMAT_MPEG */
484 0, /* 24 - SNDRV_PCM_FORMAT_GSM */
485 0, /* 25 -> 30 (not assigned) */
486 0,
487 0,
488 0,
489 0,
490 0,
491 0, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
492 PCM_FORMAT_S24_3LE, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
493 0, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
494 0, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
495 0, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
496 0, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
497 0, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
498 0, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
499 0, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
500 0, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
501 0, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
502 0, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
503 0, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
504 0, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
505 0, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
506 0, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
507 0, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
508 0, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
509 0 /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
510};
511
512static int get_pcm_format_for_mask(struct pcm_mask* mask) {
513 int num_slots = sizeof(mask->bits)/ sizeof(mask->bits[0]);
514 int bits_per_slot = sizeof(mask->bits[0]) * 8;
515
516 int table_size = sizeof(pcm_format_value_map) / sizeof(pcm_format_value_map[0]);
517
518 int slot_index, bit_index, table_index;
519 table_index = 0;
520 int num_written = 0;
521 for (slot_index = 0; slot_index < num_slots; slot_index++) {
522 unsigned bit_mask = 1;
523 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
524 if ((mask->bits[slot_index] & bit_mask) != 0) {
525 /* just return the first one */
526 return table_index < table_size
527 ? pcm_format_value_map[table_index]
528 : AUDIO_FORMAT_INVALID;
529 }
530 bit_mask <<= 1;
531 table_index++;
532 }
533 }
534
535 return 0; // is this right?
536}
537
538static void log_pcm_mask(const char* mask_name, struct pcm_mask* mask) {
539 char buff[512];
540 char bit_buff[32];
541 int buffSize = sizeof(buff)/sizeof(buff[0]);
542
543 buff[0] = '\0';
544
545 int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]);
546 int bits_per_slot = sizeof(mask->bits[0]) * 8;
547
548 int slot_index, bit_index;
549 strcat(buff, "[");
550 for (slot_index = 0; slot_index < num_slots; slot_index++) {
551 unsigned bit_mask = 1;
552 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
553 strcat(buff, (mask->bits[slot_index] & bit_mask) != 0 ? "1" : "0");
554 bit_mask <<= 1;
555 }
556 if (slot_index < num_slots - 1) {
557 strcat(buff, ",");
558 }
559 }
560 strcat(buff, "]");
561
562 ALOGV("usb:audio_hw - %s mask:%s", mask_name, buff);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800563}
564
Paul McLeancf611912014-04-28 13:03:18 -0700565static void log_pcm_params(struct pcm_params * alsa_hw_params) {
566 ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u",
567 pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS),
568 pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS));
569 ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u",
570 pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS),
571 pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS));
Paul McLeane32cbc12014-06-25 10:42:07 -0700572 log_pcm_mask("PCM_PARAM_FORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
573 log_pcm_mask("PCM_PARAM_SUBFORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_SUBFORMAT));
Paul McLeancf611912014-04-28 13:03:18 -0700574 ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u",
575 pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
576 pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
577 ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u",
578 pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
579 pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
580 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u",
581 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME),
582 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME));
583 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u",
584 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE),
585 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE));
586 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u",
587 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES),
588 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES));
589 ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u",
590 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS),
591 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS));
592 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u",
593 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME),
594 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME));
595 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u",
596 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE),
597 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE));
598 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u",
599 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES),
600 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES));
601 ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u",
602 pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME),
603 pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME));
604}
605
Paul McLeaneedc92e2013-12-19 15:46:15 -0800606/*
Paul McLean33a6b172014-06-19 12:35:28 -0700607 * Returns the supplied value rounded up to the next even multiple of 16
608 */
609static unsigned int round_to_16_mult(unsigned int size) {
610 return (size + 15) & 0xFFFFFFF0;
611}
612
Paul McLeane32cbc12014-06-25 10:42:07 -0700613/*TODO - Evaluate if this value should/can be retrieved from a device-specific property */
Paul McLean33a6b172014-06-19 12:35:28 -0700614#define MIN_BUFF_TIME 5 /* milliseconds */
615
616/*
617 * Returns the system defined minimum period size based on the supplied sample rate
618 */
619static unsigned int calc_min_period_size(unsigned int sample_rate) {
620 unsigned int period_size = (sample_rate * MIN_BUFF_TIME) / 1000;
621 return round_to_16_mult(period_size);
622}
623
624/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800625 * Reads and decodes configuration info from the specified ALSA card/device
626 */
627static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config)
628{
Paul McLean30f41852014-04-16 15:44:20 -0700629 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 -0800630
631 if (card < 0 || device < 0) {
632 return -EINVAL;
633 }
634
635 struct pcm_params * alsa_hw_params = pcm_params_get(card, device, io_type);
636 if (alsa_hw_params == NULL) {
637 return -EINVAL;
638 }
639
640 /*
641 * This Logging will be useful when testing new USB devices.
642 */
Paul McLeancf611912014-04-28 13:03:18 -0700643 /* log_pcm_params(alsa_hw_params); */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800644
645 config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
646 config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
Paul McLean33a6b172014-06-19 12:35:28 -0700647 config->period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE);
648 /* round this up to a multiple of 16 */
649 config->period_size = round_to_16_mult(config->period_size);
650 /* make sure it is above a minimum value to minimize jitter */
651 unsigned int min_period_size = calc_min_period_size(config->rate);
652 if (config->period_size < min_period_size) {
653 config->period_size = min_period_size;
654 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800655 config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
656
Paul McLeane32cbc12014-06-25 10:42:07 -0700657 config->format = get_pcm_format_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800658 return 0;
659}
660
661/*
662 * HAl Functions
663 */
Simon Wilson19957a32012-04-06 16:17:12 -0700664/**
665 * NOTE: when multiple mutexes have to be acquired, always respect the
666 * following order: hw device > out stream
667 */
668
669/* Helper functions */
Simon Wilson19957a32012-04-06 16:17:12 -0700670static uint32_t out_get_sample_rate(const struct audio_stream *stream)
671{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800672 return cached_output_hardware_config.rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700673}
674
675static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
676{
677 return 0;
678}
679
680static size_t out_get_buffer_size(const struct audio_stream *stream)
681{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800682 return cached_output_hardware_config.period_size * audio_stream_frame_size(stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700683}
684
685static uint32_t out_get_channels(const struct audio_stream *stream)
686{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800687 // Always Stero for now. We will do *some* conversions in this HAL.
Paul McLeane32cbc12014-06-25 10:42:07 -0700688 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
689 rewrite this to return the ACTUAL channel format */
Simon Wilson19957a32012-04-06 16:17:12 -0700690 return AUDIO_CHANNEL_OUT_STEREO;
691}
692
693static audio_format_t out_get_format(const struct audio_stream *stream)
694{
Paul McLeane32cbc12014-06-25 10:42:07 -0700695 return audio_format_from_pcm_format(cached_output_hardware_config.format);
Simon Wilson19957a32012-04-06 16:17:12 -0700696}
697
698static int out_set_format(struct audio_stream *stream, audio_format_t format)
699{
Paul McLeane32cbc12014-06-25 10:42:07 -0700700 cached_output_hardware_config.format = pcm_format_from_audio_format(format);
Simon Wilson19957a32012-04-06 16:17:12 -0700701 return 0;
702}
703
704static int out_standby(struct audio_stream *stream)
705{
706 struct stream_out *out = (struct stream_out *)stream;
707
708 pthread_mutex_lock(&out->dev->lock);
709 pthread_mutex_lock(&out->lock);
710
711 if (!out->standby) {
712 pcm_close(out->pcm);
713 out->pcm = NULL;
714 out->standby = true;
715 }
716
717 pthread_mutex_unlock(&out->lock);
718 pthread_mutex_unlock(&out->dev->lock);
719
720 return 0;
721}
722
723static int out_dump(const struct audio_stream *stream, int fd)
724{
725 return 0;
726}
727
728static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
729{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800730 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
731
Simon Wilson19957a32012-04-06 16:17:12 -0700732 struct stream_out *out = (struct stream_out *)stream;
733 struct audio_device *adev = out->dev;
734 struct str_parms *parms;
735 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800736 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700737 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800738 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700739
740 parms = str_parms_create_str(kvpairs);
741 pthread_mutex_lock(&adev->lock);
742
Paul McLeaneedc92e2013-12-19 15:46:15 -0800743 bool recache_device_params = false;
744 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
745 if (param_val >= 0) {
746 adev->out_card = atoi(value);
747 recache_device_params = true;
748 }
Simon Wilson19957a32012-04-06 16:17:12 -0700749
Paul McLeaneedc92e2013-12-19 15:46:15 -0800750 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
751 if (param_val >= 0) {
752 adev->out_device = atoi(value);
753 recache_device_params = true;
754 }
755
756 if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) {
757 ret_value = read_alsa_device_config(adev->out_card, adev->out_device, PCM_OUT,
Paul McLean30f41852014-04-16 15:44:20 -0700758 &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800759 output_hardware_config_is_cached = (ret_value == 0);
760 }
Simon Wilson19957a32012-04-06 16:17:12 -0700761
762 pthread_mutex_unlock(&adev->lock);
763 str_parms_destroy(parms);
764
Paul McLeaneedc92e2013-12-19 15:46:15 -0800765 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700766}
767
Paul McLeane32cbc12014-06-25 10:42:07 -0700768/*TODO it seems like both out_get_parameters() and in_get_parameters()
769 could be written in terms of a get_device_parameters(io_type) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800770
Paul McLean30f41852014-04-16 15:44:20 -0700771static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
772{
773 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
774
Paul McLeaneedc92e2013-12-19 15:46:15 -0800775 struct stream_out *out = (struct stream_out *) stream;
776 struct audio_device *adev = out->dev;
777
Paul McLean30f41852014-04-16 15:44:20 -0700778 if (adev->out_card < 0 || adev->out_device < 0)
779 return strdup("");
780
Paul McLeaneedc92e2013-12-19 15:46:15 -0800781 unsigned min, max;
782
783 struct str_parms *query = str_parms_create_str(keys);
784 struct str_parms *result = str_parms_create();
785
786 int num_written = 0;
787 char buffer[256];
788 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
789 char* result_str = NULL;
790
791 struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT);
792
793 // These keys are from hardware/libhardware/include/audio.h
794 // supported sample rates
795 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
796 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
797 // if they are different, return a list containing those two values, otherwise just the one.
798 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
799 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700800 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800801 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700802 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800803 }
804 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
805 buffer);
806 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
807
808 // supported channel counts
809 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
810 // Similarly for output channels count
Paul McLeane32cbc12014-06-25 10:42:07 -0700811 /* TODO - This is wrong, we need format strings, not numbers (another CL) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800812 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
813 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700814 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800815 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700816 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800817 }
818 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer);
819 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
820
821 // supported sample formats
822 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700823 char * format_params =
824 get_format_str_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
825 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, format_params);
826 free(format_params);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800827 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
828
829 result_str = str_parms_to_str(result);
830
831 // done with these...
832 str_parms_destroy(query);
833 str_parms_destroy(result);
834
Paul McLeane32cbc12014-06-25 10:42:07 -0700835 ALOGV("usb:audio_hw::out out_get_parameters() = %s", result_str);
836
Paul McLeaneedc92e2013-12-19 15:46:15 -0800837 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700838}
839
840static uint32_t out_get_latency(const struct audio_stream_out *stream)
841{
Paul McLean30f41852014-04-16 15:44:20 -0700842 struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800843
Paul McLeane32cbc12014-06-25 10:42:07 -0700844 /*TODO Do we need a term here for the USB latency (as reported in the USB descriptors)? */
Paul McLean30f41852014-04-16 15:44:20 -0700845 uint32_t latency = (cached_output_hardware_config.period_size
Paul McLeane32cbc12014-06-25 10:42:07 -0700846 * cached_output_hardware_config.period_count * 1000)
847 / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800848 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700849}
850
Paul McLean30f41852014-04-16 15:44:20 -0700851static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700852{
853 return -ENOSYS;
854}
855
Paul McLeaneedc92e2013-12-19 15:46:15 -0800856/* must be called with hw device and output stream mutexes locked */
857static int start_output_stream(struct stream_out *out)
858{
859 struct audio_device *adev = out->dev;
860 int return_val = 0;
861
Paul McLean30f41852014-04-16 15:44:20 -0700862 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
863 adev->out_card, adev->out_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800864
865 out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config);
Paul McLeane32cbc12014-06-25 10:42:07 -0700866
Paul McLeaneedc92e2013-12-19 15:46:15 -0800867 if (out->pcm == NULL) {
868 return -ENOMEM;
869 }
870
871 if (out->pcm && !pcm_is_ready(out->pcm)) {
872 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
873 pcm_close(out->pcm);
874 return -ENOMEM;
875 }
876
Paul McLeaneedc92e2013-12-19 15:46:15 -0800877 return 0;
878}
879
880static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700881{
882 int ret;
883 struct stream_out *out = (struct stream_out *)stream;
884
885 pthread_mutex_lock(&out->dev->lock);
886 pthread_mutex_lock(&out->lock);
887 if (out->standby) {
888 ret = start_output_stream(out);
889 if (ret != 0) {
890 goto err;
891 }
892 out->standby = false;
893 }
894
Paul McLean30f41852014-04-16 15:44:20 -0700895 // Setup conversion buffer
896 // compute maximum potential buffer size.
897 // * 2 for stereo -> quad conversion
898 // * 3/2 for 16bit -> 24 bit conversion
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700899 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700900 if (required_conversion_buffer_size > out->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700901 /* TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
902 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -0700903 out->conversion_buffer_size = required_conversion_buffer_size;
904 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
905 }
906
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700907 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800908 int num_write_buff_bytes = bytes;
909
910 /*
911 * Num Channels conversion
912 */
913 int num_device_channels = cached_output_hardware_config.channels;
914 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700915 if (num_device_channels != num_req_channels) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800916 num_write_buff_bytes =
Paul McLean30f41852014-04-16 15:44:20 -0700917 expand_channels_16(write_buff, num_req_channels,
918 out->conversion_buffer, num_device_channels,
919 num_write_buff_bytes / sizeof(short));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800920 write_buff = out->conversion_buffer;
921 }
922
Paul McLeaneedc92e2013-12-19 15:46:15 -0800923 if (write_buff != NULL && num_write_buff_bytes != 0) {
924 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
925 }
Simon Wilson19957a32012-04-06 16:17:12 -0700926
927 pthread_mutex_unlock(&out->lock);
928 pthread_mutex_unlock(&out->dev->lock);
929
930 return bytes;
931
932err:
933 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800934 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700935 if (ret != 0) {
936 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
937 out_get_sample_rate(&stream->common));
938 }
939
940 return bytes;
941}
942
Paul McLean30f41852014-04-16 15:44:20 -0700943static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700944{
945 return -EINVAL;
946}
947
948static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
949{
950 return 0;
951}
952
953static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
954{
955 return 0;
956}
957
Paul McLean30f41852014-04-16 15:44:20 -0700958static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700959{
960 return -EINVAL;
961}
962
963static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700964 audio_io_handle_t handle,
965 audio_devices_t devices,
966 audio_output_flags_t flags,
967 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -0700968 struct audio_stream_out **stream_out)
969{
Paul McLean30f41852014-04-16 15:44:20 -0700970 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 -0800971 handle, devices, flags);
972
Simon Wilson19957a32012-04-06 16:17:12 -0700973 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800974
Simon Wilson19957a32012-04-06 16:17:12 -0700975 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700976
977 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
978 if (!out)
979 return -ENOMEM;
980
Paul McLeaneedc92e2013-12-19 15:46:15 -0800981 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -0700982 out->stream.common.get_sample_rate = out_get_sample_rate;
983 out->stream.common.set_sample_rate = out_set_sample_rate;
984 out->stream.common.get_buffer_size = out_get_buffer_size;
985 out->stream.common.get_channels = out_get_channels;
986 out->stream.common.get_format = out_get_format;
987 out->stream.common.set_format = out_set_format;
988 out->stream.common.standby = out_standby;
989 out->stream.common.dump = out_dump;
990 out->stream.common.set_parameters = out_set_parameters;
991 out->stream.common.get_parameters = out_get_parameters;
992 out->stream.common.add_audio_effect = out_add_audio_effect;
993 out->stream.common.remove_audio_effect = out_remove_audio_effect;
994 out->stream.get_latency = out_get_latency;
995 out->stream.set_volume = out_set_volume;
996 out->stream.write = out_write;
997 out->stream.get_render_position = out_get_render_position;
998 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
999
1000 out->dev = adev;
1001
Paul McLeaneedc92e2013-12-19 15:46:15 -08001002 if (output_hardware_config_is_cached) {
1003 config->sample_rate = cached_output_hardware_config.rate;
1004
Paul McLeane32cbc12014-06-25 10:42:07 -07001005 config->format = audio_format_from_pcm_format(cached_output_hardware_config.format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001006
1007 config->channel_mask =
1008 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
1009 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
1010 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1011 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001012 /*TODO remove this when the above restriction is removed. */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001013 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1014 }
1015 } else {
1016 cached_output_hardware_config = default_alsa_out_config;
1017
1018 config->format = out_get_format(&out->stream.common);
1019 config->channel_mask = out_get_channels(&out->stream.common);
1020 config->sample_rate = out_get_sample_rate(&out->stream.common);
1021 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001022
1023 out->conversion_buffer = NULL;
1024 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001025
1026 out->standby = true;
1027
1028 *stream_out = &out->stream;
1029 return 0;
1030
1031err_open:
1032 free(out);
1033 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001034 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -07001035}
1036
1037static void adev_close_output_stream(struct audio_hw_device *dev,
1038 struct audio_stream_out *stream)
1039{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001040 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -07001041 struct stream_out *out = (struct stream_out *)stream;
1042
Paul McLeane32cbc12014-06-25 10:42:07 -07001043 // Close the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -07001044 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001045
1046 free(out->conversion_buffer);
1047 out->conversion_buffer = NULL;
1048 out->conversion_buffer_size = 0;
1049
Simon Wilson19957a32012-04-06 16:17:12 -07001050 free(stream);
1051}
1052
1053static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1054{
1055 return 0;
1056}
1057
Paul McLean30f41852014-04-16 15:44:20 -07001058static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001059{
1060 return strdup("");
1061}
1062
1063static int adev_init_check(const struct audio_hw_device *dev)
1064{
1065 return 0;
1066}
1067
1068static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1069{
1070 return -ENOSYS;
1071}
1072
1073static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1074{
1075 return -ENOSYS;
1076}
1077
1078static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1079{
1080 return 0;
1081}
1082
1083static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1084{
1085 return -ENOSYS;
1086}
1087
1088static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1089{
1090 return -ENOSYS;
1091}
1092
1093static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001094 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -07001095{
1096 return 0;
1097}
1098
Paul McLeaneedc92e2013-12-19 15:46:15 -08001099/* Helper functions */
1100static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1101{
Paul McLean30f41852014-04-16 15:44:20 -07001102 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001103}
1104
1105static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1106{
1107 return -ENOSYS;
1108}
1109
1110static size_t in_get_buffer_size(const struct audio_stream *stream)
1111{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001112 ALOGV("usb: in_get_buffer_size() = %zu",
Paul McLean30f41852014-04-16 15:44:20 -07001113 cached_input_hardware_config.period_size * audio_stream_frame_size(stream));
1114 return cached_input_hardware_config.period_size * audio_stream_frame_size(stream);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001115}
1116
1117static uint32_t in_get_channels(const struct audio_stream *stream)
1118{
Paul McLean30f41852014-04-16 15:44:20 -07001119 // just report stereo for now
1120 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001121}
1122
1123static audio_format_t in_get_format(const struct audio_stream *stream)
1124{
Paul McLeane32cbc12014-06-25 10:42:07 -07001125 return audio_format_from_pcm_format(cached_input_hardware_config.format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001126}
1127
1128static int in_set_format(struct audio_stream *stream, audio_format_t format)
1129{
1130 return -ENOSYS;
1131}
1132
1133static int in_standby(struct audio_stream *stream)
1134{
Paul McLean30f41852014-04-16 15:44:20 -07001135 struct stream_in *in = (struct stream_in *) stream;
1136
1137 pthread_mutex_lock(&in->dev->lock);
1138 pthread_mutex_lock(&in->lock);
1139
1140 if (!in->standby) {
1141 pcm_close(in->pcm);
1142 in->pcm = NULL;
1143 in->standby = true;
1144 }
1145
1146 pthread_mutex_unlock(&in->lock);
1147 pthread_mutex_unlock(&in->dev->lock);
1148
Paul McLeaneedc92e2013-12-19 15:46:15 -08001149 return 0;
1150}
1151
1152static int in_dump(const struct audio_stream *stream, int fd)
1153{
1154 return 0;
1155}
1156
1157static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1158{
Paul McLean30f41852014-04-16 15:44:20 -07001159 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001160
1161 struct stream_in *in = (struct stream_in *)stream;
1162 struct audio_device *adev = in->dev;
1163 struct str_parms *parms;
1164 char value[32];
1165 int param_val;
1166 int routing = 0;
1167 int ret_value = 0;
1168
1169 parms = str_parms_create_str(kvpairs);
1170 pthread_mutex_lock(&adev->lock);
1171
Paul McLean30f41852014-04-16 15:44:20 -07001172 bool recache_device_params = false;
1173
Paul McLeaneedc92e2013-12-19 15:46:15 -08001174 // Card/Device
1175 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1176 if (param_val >= 0) {
1177 adev->in_card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001178 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001179 }
1180
1181 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1182 if (param_val >= 0) {
1183 adev->in_device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001184 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001185 }
1186
Paul McLean30f41852014-04-16 15:44:20 -07001187 if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) {
1188 ret_value = read_alsa_device_config(adev->in_card, adev->in_device,
1189 PCM_IN, &(cached_input_hardware_config));
1190 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001191 }
1192
1193 pthread_mutex_unlock(&adev->lock);
1194 str_parms_destroy(parms);
1195
1196 return ret_value;
1197}
1198
Paul McLeane32cbc12014-06-25 10:42:07 -07001199/*TODO it seems like both out_get_parameters() and in_get_parameters()
1200 could be written in terms of a get_device_parameters(io_type) */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001201
Paul McLean30f41852014-04-16 15:44:20 -07001202static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
1203 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001204
Paul McLean30f41852014-04-16 15:44:20 -07001205 struct stream_in *in = (struct stream_in *)stream;
1206 struct audio_device *adev = in->dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001207
Paul McLean30f41852014-04-16 15:44:20 -07001208 if (adev->in_card < 0 || adev->in_device < 0)
1209 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001210
Paul McLean30f41852014-04-16 15:44:20 -07001211 struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN);
1212 if (alsa_hw_params == NULL)
1213 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001214
Paul McLean30f41852014-04-16 15:44:20 -07001215 struct str_parms *query = str_parms_create_str(keys);
1216 struct str_parms *result = str_parms_create();
Paul McLeaneedc92e2013-12-19 15:46:15 -08001217
Paul McLean30f41852014-04-16 15:44:20 -07001218 int num_written = 0;
1219 char buffer[256];
1220 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
1221 char* result_str = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001222
Paul McLean30f41852014-04-16 15:44:20 -07001223 unsigned min, max;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001224
Paul McLean30f41852014-04-16 15:44:20 -07001225 // These keys are from hardware/libhardware/include/audio.h
1226 // supported sample rates
1227 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
1228 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
1229 // if they are different, return a list containing those two values, otherwise just the one.
1230 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
1231 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001232 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001233 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001234 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001235 }
1236 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer);
1237 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
Paul McLeaneedc92e2013-12-19 15:46:15 -08001238
Paul McLean30f41852014-04-16 15:44:20 -07001239 // supported channel counts
1240 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
1241 // Similarly for output channels count
Paul McLeane32cbc12014-06-25 10:42:07 -07001242 // TODO This is wrong, we need format strings, not numbers (another CL)
Paul McLean30f41852014-04-16 15:44:20 -07001243 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
1244 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001245 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001246 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001247 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001248 }
1249 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer);
1250 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001251
Paul McLean30f41852014-04-16 15:44:20 -07001252 // supported sample formats
1253 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLeane32cbc12014-06-25 10:42:07 -07001254 /*TODO This is wrong. It needs to return AUDIO_ format constants (as strings)
1255 as in audio_policy.conf */
Paul McLean30f41852014-04-16 15:44:20 -07001256 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
1257 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001258 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001259 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001260 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001261 }
1262 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
1263 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001264
Paul McLean30f41852014-04-16 15:44:20 -07001265 result_str = str_parms_to_str(result);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001266
Paul McLean30f41852014-04-16 15:44:20 -07001267 // done with these...
1268 str_parms_destroy(query);
1269 str_parms_destroy(result);
1270
1271 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001272}
1273
1274static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1275{
1276 return 0;
1277}
1278
1279static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1280{
1281 return 0;
1282}
1283
Paul McLean30f41852014-04-16 15:44:20 -07001284static int in_set_gain(struct audio_stream_in *stream, float gain)
1285{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001286 return 0;
1287}
1288
Paul McLean30f41852014-04-16 15:44:20 -07001289/* must be called with hw device and output stream mutexes locked */
1290static int start_input_stream(struct stream_in *in) {
1291 struct audio_device *adev = in->dev;
1292 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001293
Paul McLean30f41852014-04-16 15:44:20 -07001294 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
1295 adev->in_card, adev->in_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001296
Paul McLean30f41852014-04-16 15:44:20 -07001297 in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config);
1298 if (in->pcm == NULL) {
1299 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1300 return -ENOMEM;
1301 }
1302
1303 if (in->pcm && !pcm_is_ready(in->pcm)) {
1304 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1305 pcm_close(in->pcm);
1306 return -ENOMEM;
1307 }
1308
1309 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001310}
1311
Paul McLeane32cbc12014-06-25 10:42:07 -07001312/* TODO mutex stuff here (see out_write) */
Paul McLean30f41852014-04-16 15:44:20 -07001313static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1314{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001315 size_t num_read_buff_bytes = 0;
Paul McLean30f41852014-04-16 15:44:20 -07001316 void * read_buff = buffer;
1317 void * out_buff = buffer;
1318
1319 struct stream_in * in = (struct stream_in *) stream;
1320
1321 pthread_mutex_lock(&in->dev->lock);
1322 pthread_mutex_lock(&in->lock);
1323
1324 if (in->standby) {
1325 if (start_input_stream(in) != 0) {
1326 goto err;
1327 }
1328 in->standby = false;
1329 }
1330
1331 // OK, we need to figure out how much data to read to be able to output the requested
1332 // number of bytes in the HAL format (16-bit, stereo).
1333 num_read_buff_bytes = bytes;
1334 int num_device_channels = cached_input_hardware_config.channels;
1335 int num_req_channels = 2; /* always, for now */
1336
1337 if (num_device_channels != num_req_channels) {
Paul McLeancf611912014-04-28 13:03:18 -07001338 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
Paul McLean30f41852014-04-16 15:44:20 -07001339 }
1340
Eric Laurent7661a482014-06-11 12:00:16 -07001341 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001342 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
1343 }
1344
1345 // Setup/Realloc the conversion buffer (if necessary).
1346 if (num_read_buff_bytes != bytes) {
1347 if (num_read_buff_bytes > in->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -07001348 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1349 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -07001350 in->conversion_buffer_size = num_read_buff_bytes;
1351 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1352 }
1353 read_buff = in->conversion_buffer;
1354 }
1355
1356 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1357 /*
1358 * Do any conversions necessary to send the data in the format specified to/by the HAL
1359 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1360 */
Eric Laurent7661a482014-06-11 12:00:16 -07001361 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001362 if (num_device_channels != num_req_channels) {
1363 out_buff = read_buff;
1364 }
1365
1366 /* Bit Format Conversion */
1367 num_read_buff_bytes =
1368 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1369 }
1370
1371 if (num_device_channels != num_req_channels) {
1372 out_buff = buffer;
1373 /* Num Channels conversion */
Paul McLeancf611912014-04-28 13:03:18 -07001374 if (num_device_channels < num_req_channels) {
1375 num_read_buff_bytes =
Paul McLeancf611912014-04-28 13:03:18 -07001376 expand_channels_16(read_buff, num_device_channels,
1377 out_buff, num_req_channels,
1378 num_read_buff_bytes / sizeof(short));
Eric Laurentfbc02dc2014-06-27 18:39:21 -07001379 } else {
1380 num_read_buff_bytes =
1381 contract_channels_16(read_buff, num_device_channels,
1382 out_buff, num_req_channels,
1383 num_read_buff_bytes / sizeof(short));
Paul McLeancf611912014-04-28 13:03:18 -07001384 }
Paul McLean30f41852014-04-16 15:44:20 -07001385 }
1386 }
1387
1388err:
1389 pthread_mutex_unlock(&in->lock);
1390 pthread_mutex_unlock(&in->dev->lock);
1391
1392 return num_read_buff_bytes;
1393}
1394
1395static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1396{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001397 return 0;
1398}
1399
Mike Lockwood46a98092012-04-24 16:41:18 -07001400static int adev_open_input_stream(struct audio_hw_device *dev,
1401 audio_io_handle_t handle,
1402 audio_devices_t devices,
Paul McLean30f41852014-04-16 15:44:20 -07001403 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001404 struct audio_stream_in **stream_in)
1405{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001406 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLean30f41852014-04-16 15:44:20 -07001407 config->sample_rate, config->channel_mask, config->format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001408
1409 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Eric Laurent7661a482014-06-11 12:00:16 -07001410 int ret = 0;
1411
Paul McLeaneedc92e2013-12-19 15:46:15 -08001412 if (in == NULL)
1413 return -ENOMEM;
1414
1415 // setup function pointers
1416 in->stream.common.get_sample_rate = in_get_sample_rate;
1417 in->stream.common.set_sample_rate = in_set_sample_rate;
1418 in->stream.common.get_buffer_size = in_get_buffer_size;
1419 in->stream.common.get_channels = in_get_channels;
1420 in->stream.common.get_format = in_get_format;
1421 in->stream.common.set_format = in_set_format;
1422 in->stream.common.standby = in_standby;
1423 in->stream.common.dump = in_dump;
1424 in->stream.common.set_parameters = in_set_parameters;
1425 in->stream.common.get_parameters = in_get_parameters;
1426 in->stream.common.add_audio_effect = in_add_audio_effect;
1427 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1428
1429 in->stream.set_gain = in_set_gain;
1430 in->stream.read = in_read;
1431 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1432
Paul McLean30f41852014-04-16 15:44:20 -07001433 in->dev = (struct audio_device *)dev;
1434
Eric Laurent7661a482014-06-11 12:00:16 -07001435 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO)
1436 ret = -EINVAL;
Paul McLean30f41852014-04-16 15:44:20 -07001437
Eric Laurent7661a482014-06-11 12:00:16 -07001438 if (input_hardware_config_is_cached) {
1439 config->sample_rate = cached_input_hardware_config.rate;
1440
Paul McLeane32cbc12014-06-25 10:42:07 -07001441 config->format = audio_format_from_pcm_format(cached_input_hardware_config.format);
Paul McLean30f41852014-04-16 15:44:20 -07001442 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
1443 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1444 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001445 /* TODO Remove this when the above restriction is removed. */
Paul McLean30f41852014-04-16 15:44:20 -07001446 config->format = AUDIO_FORMAT_PCM_16_BIT;
1447 }
1448
Eric Laurent7661a482014-06-11 12:00:16 -07001449 config->channel_mask = audio_channel_in_mask_from_count(
1450 cached_input_hardware_config.channels);
1451 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
Paul McLean30f41852014-04-16 15:44:20 -07001452 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1453 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001454 /* TODO Remove this when the above restriction is removed. */
Eric Laurent7661a482014-06-11 12:00:16 -07001455 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
Paul McLean30f41852014-04-16 15:44:20 -07001456 }
1457 } else {
1458 cached_input_hardware_config = default_alsa_in_config;
1459
Eric Laurent7661a482014-06-11 12:00:16 -07001460 config->format = in_get_format(&in->stream.common);
1461 config->channel_mask = in_get_channels(&in->stream.common);
1462 config->sample_rate = in_get_sample_rate(&in->stream.common);
Paul McLean30f41852014-04-16 15:44:20 -07001463 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001464
1465 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001466
Paul McLean30f41852014-04-16 15:44:20 -07001467 in->conversion_buffer = NULL;
1468 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001469
1470 *stream_in = &in->stream;
1471
Eric Laurent7661a482014-06-11 12:00:16 -07001472 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001473}
1474
Paul McLean30f41852014-04-16 15:44:20 -07001475static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001476{
Paul McLean30f41852014-04-16 15:44:20 -07001477 struct stream_in *in = (struct stream_in *)stream;
1478
Paul McLeane32cbc12014-06-25 10:42:07 -07001479 // Close the pcm device
Paul McLean30f41852014-04-16 15:44:20 -07001480 in_standby(&stream->common);
1481
1482 free(in->conversion_buffer);
1483
1484 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001485}
1486
1487static int adev_dump(const audio_hw_device_t *device, int fd)
1488{
1489 return 0;
1490}
1491
1492static int adev_close(hw_device_t *device)
1493{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001494 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001495 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001496
1497 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001498 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001499
Simon Wilson19957a32012-04-06 16:17:12 -07001500 return 0;
1501}
1502
Paul McLean30f41852014-04-16 15:44:20 -07001503static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001504{
Simon Wilson19957a32012-04-06 16:17:12 -07001505 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1506 return -EINVAL;
1507
Paul McLeaneedc92e2013-12-19 15:46:15 -08001508 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001509 if (!adev)
1510 return -ENOMEM;
1511
1512 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001513 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001514 adev->hw_device.common.module = (struct hw_module_t *) module;
1515 adev->hw_device.common.close = adev_close;
1516
Simon Wilson19957a32012-04-06 16:17:12 -07001517 adev->hw_device.init_check = adev_init_check;
1518 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1519 adev->hw_device.set_master_volume = adev_set_master_volume;
1520 adev->hw_device.set_mode = adev_set_mode;
1521 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1522 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1523 adev->hw_device.set_parameters = adev_set_parameters;
1524 adev->hw_device.get_parameters = adev_get_parameters;
1525 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1526 adev->hw_device.open_output_stream = adev_open_output_stream;
1527 adev->hw_device.close_output_stream = adev_close_output_stream;
1528 adev->hw_device.open_input_stream = adev_open_input_stream;
1529 adev->hw_device.close_input_stream = adev_close_input_stream;
1530 adev->hw_device.dump = adev_dump;
1531
1532 *device = &adev->hw_device.common;
1533
1534 return 0;
1535}
1536
1537static struct hw_module_methods_t hal_module_methods = {
1538 .open = adev_open,
1539};
1540
1541struct audio_module HAL_MODULE_INFO_SYM = {
1542 .common = {
1543 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001544 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1545 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001546 .id = AUDIO_HARDWARE_MODULE_ID,
1547 .name = "USB audio HW HAL",
1548 .author = "The Android Open Source Project",
1549 .methods = &hal_module_methods,
1550 },
1551};