blob: 4b5c25757b0e79fd70a7732c3d8f6db840f46ed0 [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{
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700682 return cached_output_hardware_config.period_size *
683 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700684}
685
686static uint32_t out_get_channels(const struct audio_stream *stream)
687{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800688 // Always Stero for now. We will do *some* conversions in this HAL.
Paul McLeane32cbc12014-06-25 10:42:07 -0700689 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
690 rewrite this to return the ACTUAL channel format */
Simon Wilson19957a32012-04-06 16:17:12 -0700691 return AUDIO_CHANNEL_OUT_STEREO;
692}
693
694static audio_format_t out_get_format(const struct audio_stream *stream)
695{
Paul McLeane32cbc12014-06-25 10:42:07 -0700696 return audio_format_from_pcm_format(cached_output_hardware_config.format);
Simon Wilson19957a32012-04-06 16:17:12 -0700697}
698
699static int out_set_format(struct audio_stream *stream, audio_format_t format)
700{
Paul McLeane32cbc12014-06-25 10:42:07 -0700701 cached_output_hardware_config.format = pcm_format_from_audio_format(format);
Simon Wilson19957a32012-04-06 16:17:12 -0700702 return 0;
703}
704
705static int out_standby(struct audio_stream *stream)
706{
707 struct stream_out *out = (struct stream_out *)stream;
708
709 pthread_mutex_lock(&out->dev->lock);
710 pthread_mutex_lock(&out->lock);
711
712 if (!out->standby) {
713 pcm_close(out->pcm);
714 out->pcm = NULL;
715 out->standby = true;
716 }
717
718 pthread_mutex_unlock(&out->lock);
719 pthread_mutex_unlock(&out->dev->lock);
720
721 return 0;
722}
723
724static int out_dump(const struct audio_stream *stream, int fd)
725{
726 return 0;
727}
728
729static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
730{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800731 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
732
Simon Wilson19957a32012-04-06 16:17:12 -0700733 struct stream_out *out = (struct stream_out *)stream;
734 struct audio_device *adev = out->dev;
735 struct str_parms *parms;
736 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800737 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700738 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800739 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700740
741 parms = str_parms_create_str(kvpairs);
742 pthread_mutex_lock(&adev->lock);
743
Paul McLeaneedc92e2013-12-19 15:46:15 -0800744 bool recache_device_params = false;
745 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
746 if (param_val >= 0) {
747 adev->out_card = atoi(value);
748 recache_device_params = true;
749 }
Simon Wilson19957a32012-04-06 16:17:12 -0700750
Paul McLeaneedc92e2013-12-19 15:46:15 -0800751 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
752 if (param_val >= 0) {
753 adev->out_device = atoi(value);
754 recache_device_params = true;
755 }
756
757 if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) {
758 ret_value = read_alsa_device_config(adev->out_card, adev->out_device, PCM_OUT,
Paul McLean30f41852014-04-16 15:44:20 -0700759 &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800760 output_hardware_config_is_cached = (ret_value == 0);
761 }
Simon Wilson19957a32012-04-06 16:17:12 -0700762
763 pthread_mutex_unlock(&adev->lock);
764 str_parms_destroy(parms);
765
Paul McLeaneedc92e2013-12-19 15:46:15 -0800766 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700767}
768
Paul McLeane32cbc12014-06-25 10:42:07 -0700769/*TODO it seems like both out_get_parameters() and in_get_parameters()
770 could be written in terms of a get_device_parameters(io_type) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800771
Paul McLean30f41852014-04-16 15:44:20 -0700772static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
773{
774 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
775
Paul McLeaneedc92e2013-12-19 15:46:15 -0800776 struct stream_out *out = (struct stream_out *) stream;
777 struct audio_device *adev = out->dev;
778
Paul McLean30f41852014-04-16 15:44:20 -0700779 if (adev->out_card < 0 || adev->out_device < 0)
780 return strdup("");
781
Paul McLeaneedc92e2013-12-19 15:46:15 -0800782 unsigned min, max;
783
784 struct str_parms *query = str_parms_create_str(keys);
785 struct str_parms *result = str_parms_create();
786
787 int num_written = 0;
788 char buffer[256];
789 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
790 char* result_str = NULL;
791
792 struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT);
793
794 // These keys are from hardware/libhardware/include/audio.h
795 // supported sample rates
796 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
797 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
798 // if they are different, return a list containing those two values, otherwise just the one.
799 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
800 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700801 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800802 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700803 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800804 }
805 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
806 buffer);
807 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
808
809 // supported channel counts
810 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
811 // Similarly for output channels count
Paul McLeane32cbc12014-06-25 10:42:07 -0700812 /* TODO - This is wrong, we need format strings, not numbers (another CL) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800813 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
814 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700815 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800816 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700817 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800818 }
819 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer);
820 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
821
822 // supported sample formats
823 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700824 char * format_params =
825 get_format_str_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
826 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, format_params);
827 free(format_params);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800828 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
829
830 result_str = str_parms_to_str(result);
831
832 // done with these...
833 str_parms_destroy(query);
834 str_parms_destroy(result);
835
Paul McLeane32cbc12014-06-25 10:42:07 -0700836 ALOGV("usb:audio_hw::out out_get_parameters() = %s", result_str);
837
Paul McLeaneedc92e2013-12-19 15:46:15 -0800838 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700839}
840
841static uint32_t out_get_latency(const struct audio_stream_out *stream)
842{
Paul McLean30f41852014-04-16 15:44:20 -0700843 struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800844
Paul McLeane32cbc12014-06-25 10:42:07 -0700845 /*TODO Do we need a term here for the USB latency (as reported in the USB descriptors)? */
Paul McLean30f41852014-04-16 15:44:20 -0700846 uint32_t latency = (cached_output_hardware_config.period_size
Paul McLeane32cbc12014-06-25 10:42:07 -0700847 * cached_output_hardware_config.period_count * 1000)
848 / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800849 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700850}
851
Paul McLean30f41852014-04-16 15:44:20 -0700852static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700853{
854 return -ENOSYS;
855}
856
Paul McLeaneedc92e2013-12-19 15:46:15 -0800857/* must be called with hw device and output stream mutexes locked */
858static int start_output_stream(struct stream_out *out)
859{
860 struct audio_device *adev = out->dev;
861 int return_val = 0;
862
Paul McLean30f41852014-04-16 15:44:20 -0700863 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
864 adev->out_card, adev->out_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800865
866 out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config);
Paul McLeane32cbc12014-06-25 10:42:07 -0700867
Paul McLeaneedc92e2013-12-19 15:46:15 -0800868 if (out->pcm == NULL) {
869 return -ENOMEM;
870 }
871
872 if (out->pcm && !pcm_is_ready(out->pcm)) {
873 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
874 pcm_close(out->pcm);
875 return -ENOMEM;
876 }
877
Paul McLeaneedc92e2013-12-19 15:46:15 -0800878 return 0;
879}
880
881static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700882{
883 int ret;
884 struct stream_out *out = (struct stream_out *)stream;
885
886 pthread_mutex_lock(&out->dev->lock);
887 pthread_mutex_lock(&out->lock);
888 if (out->standby) {
889 ret = start_output_stream(out);
890 if (ret != 0) {
891 goto err;
892 }
893 out->standby = false;
894 }
895
Paul McLean30f41852014-04-16 15:44:20 -0700896 // Setup conversion buffer
897 // compute maximum potential buffer size.
898 // * 2 for stereo -> quad conversion
899 // * 3/2 for 16bit -> 24 bit conversion
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700900 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700901 if (required_conversion_buffer_size > out->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700902 /* TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
903 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -0700904 out->conversion_buffer_size = required_conversion_buffer_size;
905 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
906 }
907
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700908 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800909 int num_write_buff_bytes = bytes;
910
911 /*
912 * Num Channels conversion
913 */
914 int num_device_channels = cached_output_hardware_config.channels;
915 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700916 if (num_device_channels != num_req_channels) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800917 num_write_buff_bytes =
Paul McLean30f41852014-04-16 15:44:20 -0700918 expand_channels_16(write_buff, num_req_channels,
919 out->conversion_buffer, num_device_channels,
920 num_write_buff_bytes / sizeof(short));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800921 write_buff = out->conversion_buffer;
922 }
923
Paul McLeaneedc92e2013-12-19 15:46:15 -0800924 if (write_buff != NULL && num_write_buff_bytes != 0) {
925 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
926 }
Simon Wilson19957a32012-04-06 16:17:12 -0700927
928 pthread_mutex_unlock(&out->lock);
929 pthread_mutex_unlock(&out->dev->lock);
930
931 return bytes;
932
933err:
934 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800935 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700936 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700937 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700938 out_get_sample_rate(&stream->common));
939 }
940
941 return bytes;
942}
943
Paul McLean30f41852014-04-16 15:44:20 -0700944static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700945{
946 return -EINVAL;
947}
948
949static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
950{
951 return 0;
952}
953
954static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
955{
956 return 0;
957}
958
Paul McLean30f41852014-04-16 15:44:20 -0700959static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700960{
961 return -EINVAL;
962}
963
964static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700965 audio_io_handle_t handle,
966 audio_devices_t devices,
967 audio_output_flags_t flags,
968 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -0700969 struct audio_stream_out **stream_out)
970{
Paul McLean30f41852014-04-16 15:44:20 -0700971 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 -0800972 handle, devices, flags);
973
Simon Wilson19957a32012-04-06 16:17:12 -0700974 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800975
Simon Wilson19957a32012-04-06 16:17:12 -0700976 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700977
978 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
979 if (!out)
980 return -ENOMEM;
981
Paul McLeaneedc92e2013-12-19 15:46:15 -0800982 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -0700983 out->stream.common.get_sample_rate = out_get_sample_rate;
984 out->stream.common.set_sample_rate = out_set_sample_rate;
985 out->stream.common.get_buffer_size = out_get_buffer_size;
986 out->stream.common.get_channels = out_get_channels;
987 out->stream.common.get_format = out_get_format;
988 out->stream.common.set_format = out_set_format;
989 out->stream.common.standby = out_standby;
990 out->stream.common.dump = out_dump;
991 out->stream.common.set_parameters = out_set_parameters;
992 out->stream.common.get_parameters = out_get_parameters;
993 out->stream.common.add_audio_effect = out_add_audio_effect;
994 out->stream.common.remove_audio_effect = out_remove_audio_effect;
995 out->stream.get_latency = out_get_latency;
996 out->stream.set_volume = out_set_volume;
997 out->stream.write = out_write;
998 out->stream.get_render_position = out_get_render_position;
999 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1000
1001 out->dev = adev;
1002
Paul McLeaneedc92e2013-12-19 15:46:15 -08001003 if (output_hardware_config_is_cached) {
1004 config->sample_rate = cached_output_hardware_config.rate;
1005
Paul McLeane32cbc12014-06-25 10:42:07 -07001006 config->format = audio_format_from_pcm_format(cached_output_hardware_config.format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001007
1008 config->channel_mask =
1009 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
1010 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
1011 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1012 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001013 /*TODO remove this when the above restriction is removed. */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001014 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1015 }
1016 } else {
1017 cached_output_hardware_config = default_alsa_out_config;
1018
1019 config->format = out_get_format(&out->stream.common);
1020 config->channel_mask = out_get_channels(&out->stream.common);
1021 config->sample_rate = out_get_sample_rate(&out->stream.common);
1022 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001023
1024 out->conversion_buffer = NULL;
1025 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001026
1027 out->standby = true;
1028
1029 *stream_out = &out->stream;
1030 return 0;
1031
1032err_open:
1033 free(out);
1034 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001035 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -07001036}
1037
1038static void adev_close_output_stream(struct audio_hw_device *dev,
1039 struct audio_stream_out *stream)
1040{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001041 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -07001042 struct stream_out *out = (struct stream_out *)stream;
1043
Paul McLeane32cbc12014-06-25 10:42:07 -07001044 // Close the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -07001045 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001046
1047 free(out->conversion_buffer);
1048 out->conversion_buffer = NULL;
1049 out->conversion_buffer_size = 0;
1050
Simon Wilson19957a32012-04-06 16:17:12 -07001051 free(stream);
1052}
1053
1054static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1055{
1056 return 0;
1057}
1058
Paul McLean30f41852014-04-16 15:44:20 -07001059static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001060{
1061 return strdup("");
1062}
1063
1064static int adev_init_check(const struct audio_hw_device *dev)
1065{
1066 return 0;
1067}
1068
1069static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1070{
1071 return -ENOSYS;
1072}
1073
1074static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1075{
1076 return -ENOSYS;
1077}
1078
1079static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1080{
1081 return 0;
1082}
1083
1084static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1085{
1086 return -ENOSYS;
1087}
1088
1089static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1090{
1091 return -ENOSYS;
1092}
1093
1094static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001095 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -07001096{
1097 return 0;
1098}
1099
Paul McLeaneedc92e2013-12-19 15:46:15 -08001100/* Helper functions */
1101static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1102{
Paul McLean30f41852014-04-16 15:44:20 -07001103 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001104}
1105
1106static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1107{
1108 return -ENOSYS;
1109}
1110
1111static size_t in_get_buffer_size(const struct audio_stream *stream)
1112{
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001113 size_t buffer_size = cached_input_hardware_config.period_size *
1114 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
1115 ALOGV("usb: in_get_buffer_size() = %zu", buffer_size);
1116 return buffer_size;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001117}
1118
1119static uint32_t in_get_channels(const struct audio_stream *stream)
1120{
Paul McLean30f41852014-04-16 15:44:20 -07001121 // just report stereo for now
1122 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001123}
1124
1125static audio_format_t in_get_format(const struct audio_stream *stream)
1126{
Paul McLeane32cbc12014-06-25 10:42:07 -07001127 return audio_format_from_pcm_format(cached_input_hardware_config.format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001128}
1129
1130static int in_set_format(struct audio_stream *stream, audio_format_t format)
1131{
1132 return -ENOSYS;
1133}
1134
1135static int in_standby(struct audio_stream *stream)
1136{
Paul McLean30f41852014-04-16 15:44:20 -07001137 struct stream_in *in = (struct stream_in *) stream;
1138
1139 pthread_mutex_lock(&in->dev->lock);
1140 pthread_mutex_lock(&in->lock);
1141
1142 if (!in->standby) {
1143 pcm_close(in->pcm);
1144 in->pcm = NULL;
1145 in->standby = true;
1146 }
1147
1148 pthread_mutex_unlock(&in->lock);
1149 pthread_mutex_unlock(&in->dev->lock);
1150
Paul McLeaneedc92e2013-12-19 15:46:15 -08001151 return 0;
1152}
1153
1154static int in_dump(const struct audio_stream *stream, int fd)
1155{
1156 return 0;
1157}
1158
1159static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1160{
Paul McLean30f41852014-04-16 15:44:20 -07001161 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001162
1163 struct stream_in *in = (struct stream_in *)stream;
1164 struct audio_device *adev = in->dev;
1165 struct str_parms *parms;
1166 char value[32];
1167 int param_val;
1168 int routing = 0;
1169 int ret_value = 0;
1170
1171 parms = str_parms_create_str(kvpairs);
1172 pthread_mutex_lock(&adev->lock);
1173
Paul McLean30f41852014-04-16 15:44:20 -07001174 bool recache_device_params = false;
1175
Paul McLeaneedc92e2013-12-19 15:46:15 -08001176 // Card/Device
1177 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1178 if (param_val >= 0) {
1179 adev->in_card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001180 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001181 }
1182
1183 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1184 if (param_val >= 0) {
1185 adev->in_device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001186 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001187 }
1188
Paul McLean30f41852014-04-16 15:44:20 -07001189 if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) {
1190 ret_value = read_alsa_device_config(adev->in_card, adev->in_device,
1191 PCM_IN, &(cached_input_hardware_config));
1192 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001193 }
1194
1195 pthread_mutex_unlock(&adev->lock);
1196 str_parms_destroy(parms);
1197
1198 return ret_value;
1199}
1200
Paul McLeane32cbc12014-06-25 10:42:07 -07001201/*TODO it seems like both out_get_parameters() and in_get_parameters()
1202 could be written in terms of a get_device_parameters(io_type) */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001203
Paul McLean30f41852014-04-16 15:44:20 -07001204static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
1205 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001206
Paul McLean30f41852014-04-16 15:44:20 -07001207 struct stream_in *in = (struct stream_in *)stream;
1208 struct audio_device *adev = in->dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001209
Paul McLean30f41852014-04-16 15:44:20 -07001210 if (adev->in_card < 0 || adev->in_device < 0)
1211 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001212
Paul McLean30f41852014-04-16 15:44:20 -07001213 struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN);
1214 if (alsa_hw_params == NULL)
1215 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001216
Paul McLean30f41852014-04-16 15:44:20 -07001217 struct str_parms *query = str_parms_create_str(keys);
1218 struct str_parms *result = str_parms_create();
Paul McLeaneedc92e2013-12-19 15:46:15 -08001219
Paul McLean30f41852014-04-16 15:44:20 -07001220 int num_written = 0;
1221 char buffer[256];
1222 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
1223 char* result_str = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001224
Paul McLean30f41852014-04-16 15:44:20 -07001225 unsigned min, max;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001226
Paul McLean30f41852014-04-16 15:44:20 -07001227 // These keys are from hardware/libhardware/include/audio.h
1228 // supported sample rates
1229 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
1230 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
1231 // if they are different, return a list containing those two values, otherwise just the one.
1232 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
1233 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001234 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001235 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001236 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001237 }
1238 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer);
1239 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
Paul McLeaneedc92e2013-12-19 15:46:15 -08001240
Paul McLean30f41852014-04-16 15:44:20 -07001241 // supported channel counts
1242 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
1243 // Similarly for output channels count
Paul McLeane32cbc12014-06-25 10:42:07 -07001244 // TODO This is wrong, we need format strings, not numbers (another CL)
Paul McLean30f41852014-04-16 15:44:20 -07001245 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
1246 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001247 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001248 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001249 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001250 }
1251 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer);
1252 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001253
Paul McLean30f41852014-04-16 15:44:20 -07001254 // supported sample formats
1255 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLeane32cbc12014-06-25 10:42:07 -07001256 /*TODO This is wrong. It needs to return AUDIO_ format constants (as strings)
1257 as in audio_policy.conf */
Paul McLean30f41852014-04-16 15:44:20 -07001258 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
1259 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001260 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001261 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001262 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001263 }
1264 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
1265 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001266
Paul McLean30f41852014-04-16 15:44:20 -07001267 result_str = str_parms_to_str(result);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001268
Paul McLean30f41852014-04-16 15:44:20 -07001269 // done with these...
1270 str_parms_destroy(query);
1271 str_parms_destroy(result);
1272
1273 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001274}
1275
1276static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1277{
1278 return 0;
1279}
1280
1281static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1282{
1283 return 0;
1284}
1285
Paul McLean30f41852014-04-16 15:44:20 -07001286static int in_set_gain(struct audio_stream_in *stream, float gain)
1287{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001288 return 0;
1289}
1290
Paul McLean30f41852014-04-16 15:44:20 -07001291/* must be called with hw device and output stream mutexes locked */
1292static int start_input_stream(struct stream_in *in) {
1293 struct audio_device *adev = in->dev;
1294 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001295
Paul McLean30f41852014-04-16 15:44:20 -07001296 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
1297 adev->in_card, adev->in_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001298
Paul McLean30f41852014-04-16 15:44:20 -07001299 in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config);
1300 if (in->pcm == NULL) {
1301 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1302 return -ENOMEM;
1303 }
1304
1305 if (in->pcm && !pcm_is_ready(in->pcm)) {
1306 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1307 pcm_close(in->pcm);
1308 return -ENOMEM;
1309 }
1310
1311 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001312}
1313
Paul McLeane32cbc12014-06-25 10:42:07 -07001314/* TODO mutex stuff here (see out_write) */
Paul McLean30f41852014-04-16 15:44:20 -07001315static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1316{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001317 size_t num_read_buff_bytes = 0;
Paul McLean30f41852014-04-16 15:44:20 -07001318 void * read_buff = buffer;
1319 void * out_buff = buffer;
1320
1321 struct stream_in * in = (struct stream_in *) stream;
1322
1323 pthread_mutex_lock(&in->dev->lock);
1324 pthread_mutex_lock(&in->lock);
1325
1326 if (in->standby) {
1327 if (start_input_stream(in) != 0) {
1328 goto err;
1329 }
1330 in->standby = false;
1331 }
1332
1333 // OK, we need to figure out how much data to read to be able to output the requested
1334 // number of bytes in the HAL format (16-bit, stereo).
1335 num_read_buff_bytes = bytes;
1336 int num_device_channels = cached_input_hardware_config.channels;
1337 int num_req_channels = 2; /* always, for now */
1338
1339 if (num_device_channels != num_req_channels) {
Paul McLeancf611912014-04-28 13:03:18 -07001340 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
Paul McLean30f41852014-04-16 15:44:20 -07001341 }
1342
Eric Laurent7661a482014-06-11 12:00:16 -07001343 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001344 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
1345 }
1346
1347 // Setup/Realloc the conversion buffer (if necessary).
1348 if (num_read_buff_bytes != bytes) {
1349 if (num_read_buff_bytes > in->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -07001350 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1351 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -07001352 in->conversion_buffer_size = num_read_buff_bytes;
1353 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1354 }
1355 read_buff = in->conversion_buffer;
1356 }
1357
1358 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1359 /*
1360 * Do any conversions necessary to send the data in the format specified to/by the HAL
1361 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1362 */
Eric Laurent7661a482014-06-11 12:00:16 -07001363 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001364 if (num_device_channels != num_req_channels) {
1365 out_buff = read_buff;
1366 }
1367
1368 /* Bit Format Conversion */
1369 num_read_buff_bytes =
1370 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1371 }
1372
1373 if (num_device_channels != num_req_channels) {
1374 out_buff = buffer;
1375 /* Num Channels conversion */
Paul McLeancf611912014-04-28 13:03:18 -07001376 if (num_device_channels < num_req_channels) {
1377 num_read_buff_bytes =
Paul McLeancf611912014-04-28 13:03:18 -07001378 expand_channels_16(read_buff, num_device_channels,
1379 out_buff, num_req_channels,
1380 num_read_buff_bytes / sizeof(short));
Eric Laurentfbc02dc2014-06-27 18:39:21 -07001381 } else {
1382 num_read_buff_bytes =
1383 contract_channels_16(read_buff, num_device_channels,
1384 out_buff, num_req_channels,
1385 num_read_buff_bytes / sizeof(short));
Paul McLeancf611912014-04-28 13:03:18 -07001386 }
Paul McLean30f41852014-04-16 15:44:20 -07001387 }
1388 }
1389
1390err:
1391 pthread_mutex_unlock(&in->lock);
1392 pthread_mutex_unlock(&in->dev->lock);
1393
1394 return num_read_buff_bytes;
1395}
1396
1397static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1398{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001399 return 0;
1400}
1401
Mike Lockwood46a98092012-04-24 16:41:18 -07001402static int adev_open_input_stream(struct audio_hw_device *dev,
1403 audio_io_handle_t handle,
1404 audio_devices_t devices,
Paul McLean30f41852014-04-16 15:44:20 -07001405 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001406 struct audio_stream_in **stream_in)
1407{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001408 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLean30f41852014-04-16 15:44:20 -07001409 config->sample_rate, config->channel_mask, config->format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001410
1411 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Eric Laurent7661a482014-06-11 12:00:16 -07001412 int ret = 0;
1413
Paul McLeaneedc92e2013-12-19 15:46:15 -08001414 if (in == NULL)
1415 return -ENOMEM;
1416
1417 // setup function pointers
1418 in->stream.common.get_sample_rate = in_get_sample_rate;
1419 in->stream.common.set_sample_rate = in_set_sample_rate;
1420 in->stream.common.get_buffer_size = in_get_buffer_size;
1421 in->stream.common.get_channels = in_get_channels;
1422 in->stream.common.get_format = in_get_format;
1423 in->stream.common.set_format = in_set_format;
1424 in->stream.common.standby = in_standby;
1425 in->stream.common.dump = in_dump;
1426 in->stream.common.set_parameters = in_set_parameters;
1427 in->stream.common.get_parameters = in_get_parameters;
1428 in->stream.common.add_audio_effect = in_add_audio_effect;
1429 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1430
1431 in->stream.set_gain = in_set_gain;
1432 in->stream.read = in_read;
1433 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1434
Paul McLean30f41852014-04-16 15:44:20 -07001435 in->dev = (struct audio_device *)dev;
1436
Eric Laurent7661a482014-06-11 12:00:16 -07001437 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO)
1438 ret = -EINVAL;
Paul McLean30f41852014-04-16 15:44:20 -07001439
Eric Laurent7661a482014-06-11 12:00:16 -07001440 if (input_hardware_config_is_cached) {
1441 config->sample_rate = cached_input_hardware_config.rate;
1442
Paul McLeane32cbc12014-06-25 10:42:07 -07001443 config->format = audio_format_from_pcm_format(cached_input_hardware_config.format);
Paul McLean30f41852014-04-16 15:44:20 -07001444 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
1445 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1446 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001447 /* TODO Remove this when the above restriction is removed. */
Paul McLean30f41852014-04-16 15:44:20 -07001448 config->format = AUDIO_FORMAT_PCM_16_BIT;
1449 }
1450
Eric Laurent7661a482014-06-11 12:00:16 -07001451 config->channel_mask = audio_channel_in_mask_from_count(
1452 cached_input_hardware_config.channels);
1453 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
Paul McLean30f41852014-04-16 15:44:20 -07001454 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1455 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001456 /* TODO Remove this when the above restriction is removed. */
Eric Laurent7661a482014-06-11 12:00:16 -07001457 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
Paul McLean30f41852014-04-16 15:44:20 -07001458 }
1459 } else {
1460 cached_input_hardware_config = default_alsa_in_config;
1461
Eric Laurent7661a482014-06-11 12:00:16 -07001462 config->format = in_get_format(&in->stream.common);
1463 config->channel_mask = in_get_channels(&in->stream.common);
1464 config->sample_rate = in_get_sample_rate(&in->stream.common);
Paul McLean30f41852014-04-16 15:44:20 -07001465 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001466
1467 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001468
Paul McLean30f41852014-04-16 15:44:20 -07001469 in->conversion_buffer = NULL;
1470 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001471
1472 *stream_in = &in->stream;
1473
Eric Laurent7661a482014-06-11 12:00:16 -07001474 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001475}
1476
Paul McLean30f41852014-04-16 15:44:20 -07001477static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001478{
Paul McLean30f41852014-04-16 15:44:20 -07001479 struct stream_in *in = (struct stream_in *)stream;
1480
Paul McLeane32cbc12014-06-25 10:42:07 -07001481 // Close the pcm device
Paul McLean30f41852014-04-16 15:44:20 -07001482 in_standby(&stream->common);
1483
1484 free(in->conversion_buffer);
1485
1486 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001487}
1488
1489static int adev_dump(const audio_hw_device_t *device, int fd)
1490{
1491 return 0;
1492}
1493
1494static int adev_close(hw_device_t *device)
1495{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001496 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001497 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001498
1499 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001500 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001501
Simon Wilson19957a32012-04-06 16:17:12 -07001502 return 0;
1503}
1504
Paul McLean30f41852014-04-16 15:44:20 -07001505static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001506{
Simon Wilson19957a32012-04-06 16:17:12 -07001507 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1508 return -EINVAL;
1509
Paul McLeaneedc92e2013-12-19 15:46:15 -08001510 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001511 if (!adev)
1512 return -ENOMEM;
1513
1514 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001515 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001516 adev->hw_device.common.module = (struct hw_module_t *) module;
1517 adev->hw_device.common.close = adev_close;
1518
Simon Wilson19957a32012-04-06 16:17:12 -07001519 adev->hw_device.init_check = adev_init_check;
1520 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1521 adev->hw_device.set_master_volume = adev_set_master_volume;
1522 adev->hw_device.set_mode = adev_set_mode;
1523 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1524 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1525 adev->hw_device.set_parameters = adev_set_parameters;
1526 adev->hw_device.get_parameters = adev_get_parameters;
1527 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1528 adev->hw_device.open_output_stream = adev_open_output_stream;
1529 adev->hw_device.close_output_stream = adev_close_output_stream;
1530 adev->hw_device.open_input_stream = adev_open_input_stream;
1531 adev->hw_device.close_input_stream = adev_close_input_stream;
1532 adev->hw_device.dump = adev_dump;
1533
1534 *device = &adev->hw_device.common;
1535
1536 return 0;
1537}
1538
1539static struct hw_module_methods_t hal_module_methods = {
1540 .open = adev_open,
1541};
1542
1543struct audio_module HAL_MODULE_INFO_SYM = {
1544 .common = {
1545 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001546 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1547 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001548 .id = AUDIO_HARDWARE_MODULE_ID,
1549 .name = "USB audio HW HAL",
1550 .author = "The Android Open Source Project",
1551 .methods = &hal_module_methods,
1552 },
1553};