blob: edbbc1f36a25aea8c9fcd52f9af69f06e3e7d15f [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*/
Paul McLeanf62d75e2014-07-11 15:14:19 -070019/*#define LOG_PCM_PARAMS 0*/
Simon Wilson19957a32012-04-06 16:17:12 -070020
21#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070022#include <inttypes.h>
Simon Wilson19957a32012-04-06 16:17:12 -070023#include <pthread.h>
24#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070025#include <stdlib.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070026#include <sys/time.h>
Simon Wilson19957a32012-04-06 16:17:12 -070027
Mark Salyzyn88e458a2014-04-28 12:30:44 -070028#include <log/log.h>
Simon Wilson19957a32012-04-06 16:17:12 -070029#include <cutils/str_parms.h>
30#include <cutils/properties.h>
31
Simon Wilson19957a32012-04-06 16:17:12 -070032#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070033#include <hardware/audio_alsaops.h>
34#include <hardware/hardware.h>
35
36#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070037
38#include <tinyalsa/asoundlib.h>
39
Paul McLeanc2201152014-07-16 13:46:07 -070040#include <audio_utils/channels.h>
41
Paul McLeanf62d75e2014-07-11 15:14:19 -070042#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
43
Paul McLeaneedc92e2013-12-19 15:46:15 -080044/* This is the default configuration to hand to The Framework on the initial
45 * adev_open_output_stream(). Actual device attributes will be used on the subsequent
46 * adev_open_output_stream() after the card and device number have been set in out_set_parameters()
47 */
48#define OUT_PERIOD_SIZE 1024
49#define OUT_PERIOD_COUNT 4
50#define OUT_SAMPLING_RATE 44100
51
52struct pcm_config default_alsa_out_config = {
Simon Wilson19957a32012-04-06 16:17:12 -070053 .channels = 2,
Paul McLeaneedc92e2013-12-19 15:46:15 -080054 .rate = OUT_SAMPLING_RATE,
55 .period_size = OUT_PERIOD_SIZE,
56 .period_count = OUT_PERIOD_COUNT,
Simon Wilson19957a32012-04-06 16:17:12 -070057 .format = PCM_FORMAT_S16_LE,
58};
59
Paul McLeaneedc92e2013-12-19 15:46:15 -080060/*
61 * Input defaults. See comment above.
62 */
63#define IN_PERIOD_SIZE 1024
64#define IN_PERIOD_COUNT 4
65#define IN_SAMPLING_RATE 44100
66
67struct pcm_config default_alsa_in_config = {
68 .channels = 2,
69 .rate = IN_SAMPLING_RATE,
70 .period_size = IN_PERIOD_SIZE,
71 .period_count = IN_PERIOD_COUNT,
72 .format = PCM_FORMAT_S16_LE,
73 .start_threshold = 1,
74 .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT),
75};
76
Paul McLeanf62d75e2014-07-11 15:14:19 -070077struct audio_device_profile {
78 int card;
79 int device;
80 int direction; /* PCM_OUT or PCM_IN */
81};
82
Simon Wilson19957a32012-04-06 16:17:12 -070083struct audio_device {
84 struct audio_hw_device hw_device;
85
86 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080087
88 /* output */
Paul McLeanf62d75e2014-07-11 15:14:19 -070089 struct audio_device_profile out_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080090
91 /* input */
Paul McLeanf62d75e2014-07-11 15:14:19 -070092 struct audio_device_profile in_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080093
Simon Wilson19957a32012-04-06 16:17:12 -070094 bool standby;
95};
96
97struct stream_out {
98 struct audio_stream_out stream;
99
Paul McLeaneedc92e2013-12-19 15:46:15 -0800100 pthread_mutex_t lock; /* see note below on mutex acquisition order */
101 struct pcm *pcm; /* state of the stream */
102 bool standby;
103
104 struct audio_device *dev; /* hardware information */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700105 struct audio_device_profile * profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800106
107 void * conversion_buffer; /* any conversions are put into here
108 * they could come from here too if
109 * there was a previous conversion */
110 size_t conversion_buffer_size; /* in bytes */
111};
112
113/*
114 * Output Configuration Cache
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700115 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800116 */
117static struct pcm_config cached_output_hardware_config;
118static bool output_hardware_config_is_cached = false;
119
120struct stream_in {
121 struct audio_stream_in stream;
122
Simon Wilson19957a32012-04-06 16:17:12 -0700123 pthread_mutex_t lock; /* see note below on mutex acquisition order */
124 struct pcm *pcm;
125 bool standby;
126
127 struct audio_device *dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800128
Paul McLeanf62d75e2014-07-11 15:14:19 -0700129 struct audio_device_profile * profile;
130
Paul McLeaneedc92e2013-12-19 15:46:15 -0800131 struct audio_config hal_pcm_config;
132
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700133 /* this is the format the framework thinks it's using. We may need to convert from the actual
134 * (24-bit, 32-bit?) format to this theoretical (framework, probably 16-bit)
135 * format in in_read() */
136 enum pcm_format input_framework_format;
137
Paul McLeaneedc92e2013-12-19 15:46:15 -0800138// struct resampler_itfe *resampler;
139// struct resampler_buffer_provider buf_provider;
Paul McLean30f41852014-04-16 15:44:20 -0700140
Paul McLeaneedc92e2013-12-19 15:46:15 -0800141 int read_status;
Paul McLean30f41852014-04-16 15:44:20 -0700142
143 // We may need to read more data from the device in order to data reduce to 16bit, 4chan */
144 void * conversion_buffer; /* any conversions are put into here
145 * they could come from here too if
146 * there was a previous conversion */
147 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700148};
149
Paul McLeaneedc92e2013-12-19 15:46:15 -0800150/*
Paul McLean30f41852014-04-16 15:44:20 -0700151 * Input Configuration Cache
152 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
153 * but that will involve changes in The Framework.
154 */
155static struct pcm_config cached_input_hardware_config;
156static bool input_hardware_config_is_cached = false;
157
158/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800159 * Utility
160 */
161/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800162 * Data Conversions
163 */
164/*
Paul McLean30f41852014-04-16 15:44:20 -0700165 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
166 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800167 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700168 * out_buff points to the buffer to receive converted PCM16LE LE samples.
169 * returns
170 * the number of BYTES of output data.
171 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
172 * support PCM24_3LE (24-bit, packed).
173 * NOTE:
Paul McLean30f41852014-04-16 15:44:20 -0700174 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700175 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700176 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700177static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
178 short * out_buff)
179{
Paul McLean30f41852014-04-16 15:44:20 -0700180 /*
181 * Move from front to back so that the conversion can be done in-place
182 * i.e. in_buff == out_buff
183 */
184 /* we need 2 bytes in the output for every 3 bytes in the input */
185 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700186 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700187 size_t src_smpl_index;
188 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
189 src_ptr++; /* lowest-(skip)-byte */
190 *dst_ptr++ = *src_ptr++; /* low-byte */
191 *dst_ptr++ = *src_ptr++; /* high-byte */
192 }
193
194 /* return number of *bytes* generated: */
195 return num_in_samples * 2;
196}
197
198/*
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700199 * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
200 * in_buff points to the buffer of PCM32 samples
201 * num_in_samples size of input buffer in SAMPLES
202 * out_buff points to the buffer to receive converted PCM16LE LE samples.
203 * returns
204 * the number of BYTES of output data.
205 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
206 * support PCM_FORMAT_S32_LE (32-bit).
207 * NOTE:
208 * This conversion is safe to do in-place (in_buff == out_buff).
209 * TODO Move this to a utilities module.
210 */
211static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
212{
213 /*
214 * Move from front to back so that the conversion can be done in-place
215 * i.e. in_buff == out_buff
216 */
217
218 short * dst_ptr = out_buff;
219 const int32_t* src_ptr = in_buff;
220 size_t src_smpl_index;
221 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
222 *dst_ptr++ = *src_ptr++ >> 16;
223 }
224
225 /* return number of *bytes* generated: */
226 return num_in_samples * 2;
227}
228
229/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800230 * ALSA Utilities
231 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700232/*TODO This table and the function that uses it should be moved to a utilities module (probably) */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800233/*
Paul McLeane32cbc12014-06-25 10:42:07 -0700234 * Maps bit-positions in a pcm_mask to the corresponding AUDIO_ format string.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800235 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700236static const char * const format_string_map[] = {
237 "AUDIO_FORMAT_PCM_8_BIT", /* 00 - SNDRV_PCM_FORMAT_S8 */
238 "AUDIO_FORMAT_PCM_8_BIT", /* 01 - SNDRV_PCM_FORMAT_U8 */
239 "AUDIO_FORMAT_PCM_16_BIT", /* 02 - SNDRV_PCM_FORMAT_S16_LE */
240 NULL, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
241 NULL, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
242 NULL, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
243 "AUDIO_FORMAT_PCM_24_BIT_PACKED", /* 06 - SNDRV_PCM_FORMAT_S24_LE */
244 NULL, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
245 NULL, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
246 NULL, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
247 "AUDIO_FORMAT_PCM_32_BIT", /* 10 - SNDRV_PCM_FORMAT_S32_LE */
248 NULL, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
249 NULL, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
250 NULL, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
251 "AUDIO_FORMAT_PCM_FLOAT", /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
252 NULL, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
253 NULL, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
254 NULL, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
255 NULL, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
256 NULL, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
257 NULL, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
258 NULL, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
259 NULL, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
260 NULL, /* 23 - SNDRV_PCM_FORMAT_MPEG */
261 NULL, /* 24 - SNDRV_PCM_FORMAT_GSM */
262 NULL, NULL, NULL, NULL, NULL, NULL, /* 25 -> 30 (not assigned) */
263 NULL, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
264 "AUDIO_FORMAT_PCM_24_BIT_PACKED", /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
265 NULL, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
266 NULL, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
267 NULL, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
268 NULL, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
269 NULL, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
270 NULL, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
271 NULL, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
272 NULL, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
273 NULL, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
274 NULL, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
275 NULL, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
276 NULL, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
277 NULL, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
278 NULL, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
279 NULL, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
280 NULL, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
281 NULL /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
282};
283
284/*
285 * Generate string containing a bar ("|") delimited list of AUDIO_ formats specified in
286 * the mask parameter.
287 *
288 */
289static char* get_format_str_for_mask(struct pcm_mask* mask)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800290{
Paul McLeane32cbc12014-06-25 10:42:07 -0700291 char buffer[256];
292 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
293 buffer[0] = '\0';
294
295 int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]);
296 int bits_per_slot = sizeof(mask->bits[0]) * 8;
297
298 const char* format_str = NULL;
299 int table_size = sizeof(format_string_map)/sizeof(format_string_map[0]);
300
301 int slot_index, bit_index, table_index;
302 table_index = 0;
303 int num_written = 0;
304 for (slot_index = 0; slot_index < num_slots; slot_index++) {
305 unsigned bit_mask = 1;
306 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
307 if ((mask->bits[slot_index] & bit_mask) != 0) {
308 format_str = table_index < table_size
309 ? format_string_map[table_index]
310 : NULL;
311 if (format_str != NULL) {
312 if (num_written != 0) {
313 num_written += snprintf(buffer + num_written,
314 buffer_size - num_written, "|");
315 }
316 num_written += snprintf(buffer + num_written, buffer_size - num_written,
317 "%s", format_str);
318 }
319 }
320 bit_mask <<= 1;
321 table_index++;
Paul McLean30f41852014-04-16 15:44:20 -0700322 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800323 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700324
325 return strdup(buffer);
326}
327
328/*
329 * Maps from bit position in pcm_mask to AUDIO_ format constants.
330 */
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700331static audio_format_t const format_value_map[] = {
Paul McLeane32cbc12014-06-25 10:42:07 -0700332 AUDIO_FORMAT_PCM_8_BIT, /* 00 - SNDRV_PCM_FORMAT_S8 */
333 AUDIO_FORMAT_PCM_8_BIT, /* 01 - SNDRV_PCM_FORMAT_U8 */
334 AUDIO_FORMAT_PCM_16_BIT, /* 02 - SNDRV_PCM_FORMAT_S16_LE */
335 AUDIO_FORMAT_INVALID, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
336 AUDIO_FORMAT_INVALID, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
337 AUDIO_FORMAT_INVALID, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700338 AUDIO_FORMAT_INVALID, /* 06 - SNDRV_PCM_FORMAT_S24_LE */
Paul McLeane32cbc12014-06-25 10:42:07 -0700339 AUDIO_FORMAT_INVALID, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
340 AUDIO_FORMAT_INVALID, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
341 AUDIO_FORMAT_INVALID, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
342 AUDIO_FORMAT_PCM_32_BIT, /* 10 - SNDRV_PCM_FORMAT_S32_LE */
343 AUDIO_FORMAT_INVALID, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
344 AUDIO_FORMAT_INVALID, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
345 AUDIO_FORMAT_INVALID, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
346 AUDIO_FORMAT_PCM_FLOAT, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
347 AUDIO_FORMAT_INVALID, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
348 AUDIO_FORMAT_INVALID, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
349 AUDIO_FORMAT_INVALID, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
350 AUDIO_FORMAT_INVALID, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
351 AUDIO_FORMAT_INVALID, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
352 AUDIO_FORMAT_INVALID, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
353 AUDIO_FORMAT_INVALID, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
354 AUDIO_FORMAT_INVALID, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
355 AUDIO_FORMAT_INVALID, /* 23 - SNDRV_PCM_FORMAT_MPEG */
356 AUDIO_FORMAT_INVALID, /* 24 - SNDRV_PCM_FORMAT_GSM */
357 AUDIO_FORMAT_INVALID, /* 25 -> 30 (not assigned) */
358 AUDIO_FORMAT_INVALID,
359 AUDIO_FORMAT_INVALID,
360 AUDIO_FORMAT_INVALID,
361 AUDIO_FORMAT_INVALID,
362 AUDIO_FORMAT_INVALID,
363 AUDIO_FORMAT_INVALID, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700364 AUDIO_FORMAT_PCM_24_BIT_PACKED, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */
Paul McLeane32cbc12014-06-25 10:42:07 -0700365 AUDIO_FORMAT_INVALID, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
366 AUDIO_FORMAT_INVALID, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
367 AUDIO_FORMAT_INVALID, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
368 AUDIO_FORMAT_INVALID, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
369 AUDIO_FORMAT_INVALID, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
370 AUDIO_FORMAT_INVALID, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
371 AUDIO_FORMAT_INVALID, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
372 AUDIO_FORMAT_INVALID, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
373 AUDIO_FORMAT_INVALID, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
374 AUDIO_FORMAT_INVALID, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
375 AUDIO_FORMAT_INVALID, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
376 AUDIO_FORMAT_INVALID, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
377 AUDIO_FORMAT_INVALID, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
378 AUDIO_FORMAT_INVALID, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
379 AUDIO_FORMAT_INVALID, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
380 AUDIO_FORMAT_INVALID, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
381 AUDIO_FORMAT_INVALID /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
382};
383
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700384/*
385 * Returns true if mask indicates support for PCM_16.
386 */
387static bool mask_has_pcm_16(struct pcm_mask* mask) {
388 return (mask->bits[0] & 0x0004) != 0;
389}
390
Paul McLeaneb192972014-07-11 16:29:41 -0700391static audio_format_t get_format_for_mask(struct pcm_mask* mask)
Paul McLeane32cbc12014-06-25 10:42:07 -0700392{
393 int num_slots = sizeof(mask->bits)/ sizeof(mask->bits[0]);
394 int bits_per_slot = sizeof(mask->bits[0]) * 8;
395
396 int table_size = sizeof(format_value_map) / sizeof(format_value_map[0]);
397
398 int slot_index, bit_index, table_index;
399 table_index = 0;
400 int num_written = 0;
401 for (slot_index = 0; slot_index < num_slots; slot_index++) {
402 unsigned bit_mask = 1;
403 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
404 if ((mask->bits[slot_index] & bit_mask) != 0) {
405 /* just return the first one */
406 return table_index < table_size
407 ? format_value_map[table_index]
408 : AUDIO_FORMAT_INVALID;
409 }
410 bit_mask <<= 1;
411 table_index++;
412 }
413 }
414
415 return AUDIO_FORMAT_INVALID;
416}
417
418/*
419 * Maps from bit position in pcm_mask to AUDIO_ format constants.
420 */
421static int const pcm_format_value_map[] = {
422 PCM_FORMAT_S8, /* 00 - SNDRV_PCM_FORMAT_S8 */
423 0, /* 01 - SNDRV_PCM_FORMAT_U8 */
424 PCM_FORMAT_S16_LE, /* 02 - SNDRV_PCM_FORMAT_S16_LE */
425 0, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
426 0, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
427 0, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
428 PCM_FORMAT_S24_3LE, /* 06 - SNDRV_PCM_FORMAT_S24_LE */
429 0, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
430 0, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
431 0, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
432 PCM_FORMAT_S32_LE, /* 10 - SNDRV_PCM_FORMAT_S32_LE */
433 0, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
434 0, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
435 0, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
436 0, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
437 0, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
438 0, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
439 0, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
440 0, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
441 0, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
442 0, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
443 0, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
444 0, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
445 0, /* 23 - SNDRV_PCM_FORMAT_MPEG */
446 0, /* 24 - SNDRV_PCM_FORMAT_GSM */
447 0, /* 25 -> 30 (not assigned) */
448 0,
449 0,
450 0,
451 0,
452 0,
453 0, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
454 PCM_FORMAT_S24_3LE, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
455 0, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
456 0, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
457 0, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
458 0, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
459 0, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
460 0, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
461 0, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
462 0, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
463 0, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
464 0, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
465 0, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
466 0, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
467 0, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
468 0, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
469 0, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
470 0, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
471 0 /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
472};
473
Paul McLeaneb192972014-07-11 16:29:41 -0700474/*
475 * Scans the provided format mask and returns the first non-8 bit sample
476 * format supported by the devices.
477 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700478static int get_pcm_format_for_mask(struct pcm_mask* mask) {
479 int num_slots = sizeof(mask->bits)/ sizeof(mask->bits[0]);
480 int bits_per_slot = sizeof(mask->bits[0]) * 8;
481
482 int table_size = sizeof(pcm_format_value_map) / sizeof(pcm_format_value_map[0]);
483
484 int slot_index, bit_index, table_index;
485 table_index = 0;
486 int num_written = 0;
Paul McLeaneb192972014-07-11 16:29:41 -0700487 for (slot_index = 0; slot_index < num_slots && table_index < table_size; slot_index++) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700488 unsigned bit_mask = 1;
Paul McLeaneb192972014-07-11 16:29:41 -0700489 for (bit_index = 0; bit_index < bits_per_slot && table_index < table_size; bit_index++) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700490 if ((mask->bits[slot_index] & bit_mask) != 0) {
Paul McLeaneb192972014-07-11 16:29:41 -0700491 if (table_index != 0) { /* Don't pick 8-bit */
492 /* just return the first one */
493 return pcm_format_value_map[table_index];
494 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700495 }
496 bit_mask <<= 1;
497 table_index++;
498 }
499 }
500
Paul McLeaneb192972014-07-11 16:29:41 -0700501 return -1; /* error */
Paul McLeane32cbc12014-06-25 10:42:07 -0700502}
503
Paul McLeanf62d75e2014-07-11 15:14:19 -0700504static bool test_out_sample_rate(struct audio_device_profile* dev_profile, unsigned rate) {
505 struct pcm_config local_config = cached_output_hardware_config;
506 local_config.rate = rate;
507
508 bool works = false; /* let's be pessimistic */
509 struct pcm * pcm =
510 pcm_open(dev_profile->card, dev_profile->device, dev_profile->direction, &local_config);
511
512 if (pcm != NULL) {
513 works = pcm_is_ready(pcm);
514 pcm_close(pcm);
515 }
516
517 return works;
518}
519
520/* sort these highest -> lowest */
521static const unsigned std_sample_rates[] =
522 {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};
523
524static char* enum_std_sample_rates(struct audio_device_profile* dev_profile,
525 unsigned min, unsigned max)
526{
527 char buffer[128];
528 buffer[0] = '\0';
529 int buffSize = ARRAY_SIZE(buffer);
530
531 char numBuffer[32];
532
533 int numEntries = 0;
534 unsigned index;
535 for(index = 0; index < ARRAY_SIZE(std_sample_rates); index++) {
536 if (std_sample_rates[index] >= min && std_sample_rates[index] <= max &&
537 test_out_sample_rate(dev_profile, std_sample_rates[index])) {
538 if (numEntries++ != 0) {
539 strncat(buffer, "|", buffSize);
540 }
541 snprintf(numBuffer, sizeof(numBuffer), "%u", std_sample_rates[index]);
542 strncat(buffer, numBuffer, buffSize);
543 }
544 }
545
546 return strdup(buffer);
547}
548
549/*
550 * Logging
551 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700552static void log_pcm_mask(const char* mask_name, struct pcm_mask* mask) {
553 char buff[512];
554 char bit_buff[32];
555 int buffSize = sizeof(buff)/sizeof(buff[0]);
556
557 buff[0] = '\0';
558
559 int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]);
560 int bits_per_slot = sizeof(mask->bits[0]) * 8;
561
562 int slot_index, bit_index;
563 strcat(buff, "[");
564 for (slot_index = 0; slot_index < num_slots; slot_index++) {
565 unsigned bit_mask = 1;
566 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
567 strcat(buff, (mask->bits[slot_index] & bit_mask) != 0 ? "1" : "0");
568 bit_mask <<= 1;
569 }
570 if (slot_index < num_slots - 1) {
571 strcat(buff, ",");
572 }
573 }
574 strcat(buff, "]");
575
576 ALOGV("usb:audio_hw - %s mask:%s", mask_name, buff);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800577}
578
Paul McLeancf611912014-04-28 13:03:18 -0700579static void log_pcm_params(struct pcm_params * alsa_hw_params) {
580 ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u",
581 pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS),
582 pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS));
583 ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u",
584 pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS),
585 pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS));
Paul McLeane32cbc12014-06-25 10:42:07 -0700586 log_pcm_mask("PCM_PARAM_FORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
587 log_pcm_mask("PCM_PARAM_SUBFORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_SUBFORMAT));
Paul McLeancf611912014-04-28 13:03:18 -0700588 ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u",
589 pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
590 pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
591 ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u",
592 pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
593 pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
594 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u",
595 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME),
596 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME));
597 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u",
598 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE),
599 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE));
600 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u",
601 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES),
602 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES));
603 ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u",
604 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS),
605 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS));
606 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u",
607 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME),
608 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME));
609 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u",
610 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE),
611 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE));
612 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u",
613 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES),
614 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES));
615 ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u",
616 pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME),
617 pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME));
618}
619
Paul McLeaneedc92e2013-12-19 15:46:15 -0800620/*
Paul McLean33a6b172014-06-19 12:35:28 -0700621 * Returns the supplied value rounded up to the next even multiple of 16
622 */
623static unsigned int round_to_16_mult(unsigned int size) {
624 return (size + 15) & 0xFFFFFFF0;
625}
626
Paul McLeane32cbc12014-06-25 10:42:07 -0700627/*TODO - Evaluate if this value should/can be retrieved from a device-specific property */
Paul McLean33a6b172014-06-19 12:35:28 -0700628#define MIN_BUFF_TIME 5 /* milliseconds */
629
630/*
631 * Returns the system defined minimum period size based on the supplied sample rate
632 */
633static unsigned int calc_min_period_size(unsigned int sample_rate) {
634 unsigned int period_size = (sample_rate * MIN_BUFF_TIME) / 1000;
635 return round_to_16_mult(period_size);
636}
637
638/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800639 * Reads and decodes configuration info from the specified ALSA card/device
640 */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700641static int read_alsa_device_config(struct audio_device_profile * dev_profile,
642 struct pcm_config * config)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800643{
Paul McLeanf62d75e2014-07-11 15:14:19 -0700644 ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)",
645 dev_profile->card, dev_profile->device, dev_profile->direction);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800646
Paul McLeanf62d75e2014-07-11 15:14:19 -0700647 if (dev_profile->card < 0 || dev_profile->device < 0) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800648 return -EINVAL;
649 }
650
Paul McLeanf62d75e2014-07-11 15:14:19 -0700651 struct pcm_params * alsa_hw_params =
652 pcm_params_get(dev_profile->card, dev_profile->device, dev_profile->direction);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800653 if (alsa_hw_params == NULL) {
654 return -EINVAL;
655 }
656
Paul McLeaneb192972014-07-11 16:29:41 -0700657 int ret = 0;
658
Paul McLeaneedc92e2013-12-19 15:46:15 -0800659 /*
660 * This Logging will be useful when testing new USB devices.
661 */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700662#ifdef LOG_PCM_PARAMS
663 log_pcm_params(alsa_hw_params);
664#endif
Paul McLeaneedc92e2013-12-19 15:46:15 -0800665
666 config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
667 config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
Paul McLean33a6b172014-06-19 12:35:28 -0700668 config->period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE);
669 /* round this up to a multiple of 16 */
670 config->period_size = round_to_16_mult(config->period_size);
671 /* make sure it is above a minimum value to minimize jitter */
672 unsigned int min_period_size = calc_min_period_size(config->rate);
673 if (config->period_size < min_period_size) {
674 config->period_size = min_period_size;
675 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800676 config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
677
Paul McLeaneb192972014-07-11 16:29:41 -0700678 int format = get_pcm_format_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
679 if (format == -1) {
680 ret = -EINVAL;
681 } else {
682 config->format = format;
683 }
Paul McLeanf62d75e2014-07-11 15:14:19 -0700684
685 pcm_params_free(alsa_hw_params);
Paul McLeaneb192972014-07-11 16:29:41 -0700686 return ret;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800687}
688
689/*
690 * HAl Functions
691 */
Simon Wilson19957a32012-04-06 16:17:12 -0700692/**
693 * NOTE: when multiple mutexes have to be acquired, always respect the
694 * following order: hw device > out stream
695 */
696
697/* Helper functions */
Simon Wilson19957a32012-04-06 16:17:12 -0700698static uint32_t out_get_sample_rate(const struct audio_stream *stream)
699{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800700 return cached_output_hardware_config.rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700701}
702
703static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
704{
705 return 0;
706}
707
708static size_t out_get_buffer_size(const struct audio_stream *stream)
709{
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700710 return cached_output_hardware_config.period_size *
711 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700712}
713
714static uint32_t out_get_channels(const struct audio_stream *stream)
715{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800716 // Always Stero for now. We will do *some* conversions in this HAL.
Paul McLeane32cbc12014-06-25 10:42:07 -0700717 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
718 rewrite this to return the ACTUAL channel format */
Simon Wilson19957a32012-04-06 16:17:12 -0700719 return AUDIO_CHANNEL_OUT_STEREO;
720}
721
722static audio_format_t out_get_format(const struct audio_stream *stream)
723{
Paul McLeane32cbc12014-06-25 10:42:07 -0700724 return audio_format_from_pcm_format(cached_output_hardware_config.format);
Simon Wilson19957a32012-04-06 16:17:12 -0700725}
726
727static int out_set_format(struct audio_stream *stream, audio_format_t format)
728{
Paul McLeane32cbc12014-06-25 10:42:07 -0700729 cached_output_hardware_config.format = pcm_format_from_audio_format(format);
Simon Wilson19957a32012-04-06 16:17:12 -0700730 return 0;
731}
732
733static int out_standby(struct audio_stream *stream)
734{
735 struct stream_out *out = (struct stream_out *)stream;
736
737 pthread_mutex_lock(&out->dev->lock);
738 pthread_mutex_lock(&out->lock);
739
740 if (!out->standby) {
741 pcm_close(out->pcm);
742 out->pcm = NULL;
743 out->standby = true;
744 }
745
746 pthread_mutex_unlock(&out->lock);
747 pthread_mutex_unlock(&out->dev->lock);
748
749 return 0;
750}
751
752static int out_dump(const struct audio_stream *stream, int fd)
753{
754 return 0;
755}
756
757static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
758{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800759 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
760
Simon Wilson19957a32012-04-06 16:17:12 -0700761 struct stream_out *out = (struct stream_out *)stream;
Simon Wilson19957a32012-04-06 16:17:12 -0700762 struct str_parms *parms;
763 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800764 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700765 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800766 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700767
768 parms = str_parms_create_str(kvpairs);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700769 pthread_mutex_lock(&out->dev->lock);
770 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700771
Paul McLeaneedc92e2013-12-19 15:46:15 -0800772 bool recache_device_params = false;
773 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
774 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700775 out->profile->card = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800776 recache_device_params = true;
777 }
Simon Wilson19957a32012-04-06 16:17:12 -0700778
Paul McLeaneedc92e2013-12-19 15:46:15 -0800779 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
780 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700781 out->profile->device = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800782 recache_device_params = true;
783 }
784
Paul McLeanf62d75e2014-07-11 15:14:19 -0700785 if (recache_device_params && out->profile->card >= 0 && out->profile->device >= 0) {
786 ret_value = read_alsa_device_config(out->profile, &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800787 output_hardware_config_is_cached = (ret_value == 0);
788 }
Simon Wilson19957a32012-04-06 16:17:12 -0700789
Paul McLeanf62d75e2014-07-11 15:14:19 -0700790 pthread_mutex_unlock(&out->lock);
791 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700792 str_parms_destroy(parms);
793
Paul McLeaneedc92e2013-12-19 15:46:15 -0800794 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700795}
796
Paul McLeanf62d75e2014-07-11 15:14:19 -0700797static char * device_get_parameters(struct audio_device_profile * dev_profile, const char *keys)
Paul McLean30f41852014-04-16 15:44:20 -0700798{
Paul McLeanf62d75e2014-07-11 15:14:19 -0700799 ALOGV("usb:audio_hw::device_get_parameters() keys:%s", keys);
Paul McLean30f41852014-04-16 15:44:20 -0700800
Paul McLeanf62d75e2014-07-11 15:14:19 -0700801 if (dev_profile->card < 0 || dev_profile->device < 0) {
Paul McLean30f41852014-04-16 15:44:20 -0700802 return strdup("");
Paul McLeanf62d75e2014-07-11 15:14:19 -0700803 }
Paul McLean30f41852014-04-16 15:44:20 -0700804
Paul McLeaneedc92e2013-12-19 15:46:15 -0800805 unsigned min, max;
806
807 struct str_parms *query = str_parms_create_str(keys);
808 struct str_parms *result = str_parms_create();
809
810 int num_written = 0;
811 char buffer[256];
812 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
813 char* result_str = NULL;
814
Paul McLeanf62d75e2014-07-11 15:14:19 -0700815 struct pcm_params * alsa_hw_params =
816 pcm_params_get(dev_profile->card, dev_profile->device, dev_profile->direction);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800817
818 // These keys are from hardware/libhardware/include/audio.h
819 // supported sample rates
820 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800821 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
822 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700823
824 char* rates_list = enum_std_sample_rates(dev_profile, min, max);
825 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, rates_list);
826 free(rates_list);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800827 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
828
829 // supported channel counts
830 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700831 // TODO remove this hack when it is superceeded by proper multi-channel support
832 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
833 dev_profile->direction == PCM_OUT
834 ? "AUDIO_CHANNEL_OUT_STEREO"
835 : "AUDIO_CHANNEL_IN_STEREO");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800836 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
837
838 // supported sample formats
839 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700840 // TODO remove this hack when we have support for input in non PCM16 formats
841 if (dev_profile->direction == PCM_IN) {
842 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, "AUDIO_FORMAT_PCM_16_BIT");
843 } else {
844 struct pcm_mask * format_mask = pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT);
845 char * format_params = get_format_str_for_mask(format_mask);
846 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, format_params);
847 free(format_params);
848 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800849 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
850
Paul McLeanf62d75e2014-07-11 15:14:19 -0700851 pcm_params_free(alsa_hw_params);
852
Paul McLeaneedc92e2013-12-19 15:46:15 -0800853 result_str = str_parms_to_str(result);
854
855 // done with these...
856 str_parms_destroy(query);
857 str_parms_destroy(result);
858
Paul McLeanf62d75e2014-07-11 15:14:19 -0700859 ALOGV("usb:audio_hw::device_get_parameters = %s", result_str);
Paul McLeane32cbc12014-06-25 10:42:07 -0700860
Paul McLeaneedc92e2013-12-19 15:46:15 -0800861 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700862}
863
Paul McLeanf62d75e2014-07-11 15:14:19 -0700864static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
865{
866 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
867
868 struct stream_out *out = (struct stream_out *) stream;
869 pthread_mutex_lock(&out->dev->lock);
870 pthread_mutex_lock(&out->lock);
871
872 char * params_str = device_get_parameters(out->profile, keys);
873
874 pthread_mutex_unlock(&out->lock);
875 pthread_mutex_unlock(&out->dev->lock);
876
877 return params_str;
878}
879
Simon Wilson19957a32012-04-06 16:17:12 -0700880static uint32_t out_get_latency(const struct audio_stream_out *stream)
881{
Paul McLeanf62d75e2014-07-11 15:14:19 -0700882 // struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800883
Paul McLeane32cbc12014-06-25 10:42:07 -0700884 /*TODO Do we need a term here for the USB latency (as reported in the USB descriptors)? */
Paul McLean30f41852014-04-16 15:44:20 -0700885 uint32_t latency = (cached_output_hardware_config.period_size
Paul McLeane32cbc12014-06-25 10:42:07 -0700886 * cached_output_hardware_config.period_count * 1000)
887 / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800888 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700889}
890
Paul McLean30f41852014-04-16 15:44:20 -0700891static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700892{
893 return -ENOSYS;
894}
895
Paul McLeaneedc92e2013-12-19 15:46:15 -0800896/* must be called with hw device and output stream mutexes locked */
897static int start_output_stream(struct stream_out *out)
898{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800899 int return_val = 0;
900
Paul McLean30f41852014-04-16 15:44:20 -0700901 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -0700902 out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800903
Paul McLeanf62d75e2014-07-11 15:14:19 -0700904 out->pcm = pcm_open(out->profile->card, out->profile->device, PCM_OUT,
905 &cached_output_hardware_config);
Paul McLeane32cbc12014-06-25 10:42:07 -0700906
Paul McLeaneedc92e2013-12-19 15:46:15 -0800907 if (out->pcm == NULL) {
908 return -ENOMEM;
909 }
910
911 if (out->pcm && !pcm_is_ready(out->pcm)) {
912 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
913 pcm_close(out->pcm);
914 return -ENOMEM;
915 }
916
Paul McLeaneedc92e2013-12-19 15:46:15 -0800917 return 0;
918}
919
920static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700921{
922 int ret;
923 struct stream_out *out = (struct stream_out *)stream;
924
925 pthread_mutex_lock(&out->dev->lock);
926 pthread_mutex_lock(&out->lock);
927 if (out->standby) {
928 ret = start_output_stream(out);
929 if (ret != 0) {
930 goto err;
931 }
932 out->standby = false;
933 }
934
Paul McLean30f41852014-04-16 15:44:20 -0700935 // Setup conversion buffer
936 // compute maximum potential buffer size.
937 // * 2 for stereo -> quad conversion
938 // * 3/2 for 16bit -> 24 bit conversion
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700939 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700940 if (required_conversion_buffer_size > out->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700941 /* TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
942 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -0700943 out->conversion_buffer_size = required_conversion_buffer_size;
944 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
945 }
946
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700947 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800948 int num_write_buff_bytes = bytes;
949
950 /*
951 * Num Channels conversion
952 */
953 int num_device_channels = cached_output_hardware_config.channels;
954 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700955 if (num_device_channels != num_req_channels) {
Paul McLeaneb192972014-07-11 16:29:41 -0700956 audio_format_t audio_format = out_get_format(&(out->stream.common));
957 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800958 num_write_buff_bytes =
Paul McLeaneb192972014-07-11 16:29:41 -0700959 adjust_channels(write_buff, num_req_channels,
960 out->conversion_buffer, num_device_channels,
961 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800962 write_buff = out->conversion_buffer;
963 }
964
Paul McLeaneedc92e2013-12-19 15:46:15 -0800965 if (write_buff != NULL && num_write_buff_bytes != 0) {
966 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
967 }
Simon Wilson19957a32012-04-06 16:17:12 -0700968
969 pthread_mutex_unlock(&out->lock);
970 pthread_mutex_unlock(&out->dev->lock);
971
972 return bytes;
973
974err:
975 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800976 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700977 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700978 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700979 out_get_sample_rate(&stream->common));
980 }
981
982 return bytes;
983}
984
Paul McLean30f41852014-04-16 15:44:20 -0700985static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700986{
987 return -EINVAL;
988}
989
990static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
991{
992 return 0;
993}
994
995static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
996{
997 return 0;
998}
999
Paul McLean30f41852014-04-16 15:44:20 -07001000static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -07001001{
1002 return -EINVAL;
1003}
1004
1005static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001006 audio_io_handle_t handle,
1007 audio_devices_t devices,
1008 audio_output_flags_t flags,
1009 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001010 struct audio_stream_out **stream_out)
1011{
Paul McLean30f41852014-04-16 15:44:20 -07001012 ALOGV("usb:audio_hw::out adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X",
Paul McLeaneedc92e2013-12-19 15:46:15 -08001013 handle, devices, flags);
1014
Simon Wilson19957a32012-04-06 16:17:12 -07001015 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001016
Simon Wilson19957a32012-04-06 16:17:12 -07001017 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -07001018
1019 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1020 if (!out)
1021 return -ENOMEM;
1022
Paul McLeaneedc92e2013-12-19 15:46:15 -08001023 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -07001024 out->stream.common.get_sample_rate = out_get_sample_rate;
1025 out->stream.common.set_sample_rate = out_set_sample_rate;
1026 out->stream.common.get_buffer_size = out_get_buffer_size;
1027 out->stream.common.get_channels = out_get_channels;
1028 out->stream.common.get_format = out_get_format;
1029 out->stream.common.set_format = out_set_format;
1030 out->stream.common.standby = out_standby;
1031 out->stream.common.dump = out_dump;
1032 out->stream.common.set_parameters = out_set_parameters;
1033 out->stream.common.get_parameters = out_get_parameters;
1034 out->stream.common.add_audio_effect = out_add_audio_effect;
1035 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1036 out->stream.get_latency = out_get_latency;
1037 out->stream.set_volume = out_set_volume;
1038 out->stream.write = out_write;
1039 out->stream.get_render_position = out_get_render_position;
1040 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1041
1042 out->dev = adev;
1043
Paul McLeanf62d75e2014-07-11 15:14:19 -07001044 out->profile = &(adev->out_profile);
1045 out->profile->direction = PCM_OUT;
1046
Paul McLeaneedc92e2013-12-19 15:46:15 -08001047 if (output_hardware_config_is_cached) {
1048 config->sample_rate = cached_output_hardware_config.rate;
1049
Paul McLeane32cbc12014-06-25 10:42:07 -07001050 config->format = audio_format_from_pcm_format(cached_output_hardware_config.format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001051
1052 config->channel_mask =
1053 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
1054 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
1055 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1056 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001057 /*TODO remove this when the above restriction is removed. */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001058 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1059 }
1060 } else {
1061 cached_output_hardware_config = default_alsa_out_config;
1062
1063 config->format = out_get_format(&out->stream.common);
1064 config->channel_mask = out_get_channels(&out->stream.common);
1065 config->sample_rate = out_get_sample_rate(&out->stream.common);
1066 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001067
1068 out->conversion_buffer = NULL;
1069 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001070
1071 out->standby = true;
1072
1073 *stream_out = &out->stream;
1074 return 0;
1075
1076err_open:
1077 free(out);
1078 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001079 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -07001080}
1081
1082static void adev_close_output_stream(struct audio_hw_device *dev,
1083 struct audio_stream_out *stream)
1084{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001085 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -07001086 struct stream_out *out = (struct stream_out *)stream;
1087
Paul McLeane32cbc12014-06-25 10:42:07 -07001088 // Close the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -07001089 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001090
1091 free(out->conversion_buffer);
1092 out->conversion_buffer = NULL;
1093 out->conversion_buffer_size = 0;
1094
Simon Wilson19957a32012-04-06 16:17:12 -07001095 free(stream);
1096}
1097
1098static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1099{
1100 return 0;
1101}
1102
Paul McLean30f41852014-04-16 15:44:20 -07001103static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001104{
1105 return strdup("");
1106}
1107
1108static int adev_init_check(const struct audio_hw_device *dev)
1109{
1110 return 0;
1111}
1112
1113static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1114{
1115 return -ENOSYS;
1116}
1117
1118static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1119{
1120 return -ENOSYS;
1121}
1122
1123static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1124{
1125 return 0;
1126}
1127
1128static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1129{
1130 return -ENOSYS;
1131}
1132
1133static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1134{
1135 return -ENOSYS;
1136}
1137
1138static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001139 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -07001140{
1141 return 0;
1142}
1143
Paul McLeaneedc92e2013-12-19 15:46:15 -08001144/* Helper functions */
1145static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1146{
Paul McLean30f41852014-04-16 15:44:20 -07001147 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001148}
1149
1150static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1151{
1152 return -ENOSYS;
1153}
1154
1155static size_t in_get_buffer_size(const struct audio_stream *stream)
1156{
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001157 size_t buffer_size = cached_input_hardware_config.period_size *
1158 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
1159 ALOGV("usb: in_get_buffer_size() = %zu", buffer_size);
1160 return buffer_size;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001161}
1162
1163static uint32_t in_get_channels(const struct audio_stream *stream)
1164{
Paul McLean30f41852014-04-16 15:44:20 -07001165 // just report stereo for now
1166 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001167}
1168
1169static audio_format_t in_get_format(const struct audio_stream *stream)
1170{
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001171 const struct stream_in * in_stream = (const struct stream_in *)stream;
1172
1173 ALOGV("in_get_format() = %d -> %d", in_stream->input_framework_format,
1174 audio_format_from_pcm_format(in_stream->input_framework_format));
1175 /* return audio_format_from_pcm_format(cached_input_hardware_config.format); */
1176 return audio_format_from_pcm_format(in_stream->input_framework_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001177}
1178
1179static int in_set_format(struct audio_stream *stream, audio_format_t format)
1180{
1181 return -ENOSYS;
1182}
1183
1184static int in_standby(struct audio_stream *stream)
1185{
Paul McLean30f41852014-04-16 15:44:20 -07001186 struct stream_in *in = (struct stream_in *) stream;
1187
1188 pthread_mutex_lock(&in->dev->lock);
1189 pthread_mutex_lock(&in->lock);
1190
1191 if (!in->standby) {
1192 pcm_close(in->pcm);
1193 in->pcm = NULL;
1194 in->standby = true;
1195 }
1196
1197 pthread_mutex_unlock(&in->lock);
1198 pthread_mutex_unlock(&in->dev->lock);
1199
Paul McLeaneedc92e2013-12-19 15:46:15 -08001200 return 0;
1201}
1202
1203static int in_dump(const struct audio_stream *stream, int fd)
1204{
1205 return 0;
1206}
1207
1208static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1209{
Paul McLean30f41852014-04-16 15:44:20 -07001210 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001211
1212 struct stream_in *in = (struct stream_in *)stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001213 struct str_parms *parms;
1214 char value[32];
1215 int param_val;
1216 int routing = 0;
1217 int ret_value = 0;
1218
1219 parms = str_parms_create_str(kvpairs);
Paul McLeanf62d75e2014-07-11 15:14:19 -07001220 pthread_mutex_lock(&in->dev->lock);
1221 pthread_mutex_lock(&in->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001222
Paul McLean30f41852014-04-16 15:44:20 -07001223 bool recache_device_params = false;
1224
Paul McLeaneedc92e2013-12-19 15:46:15 -08001225 // Card/Device
1226 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1227 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -07001228 in->profile->card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001229 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001230 }
1231
1232 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1233 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -07001234 in->profile->device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001235 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001236 }
1237
Paul McLeanf62d75e2014-07-11 15:14:19 -07001238 if (recache_device_params && in->profile->card >= 0 && in->profile->device >= 0) {
1239 ret_value = read_alsa_device_config(in->profile, &cached_input_hardware_config);
Paul McLean30f41852014-04-16 15:44:20 -07001240 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001241 }
1242
Paul McLeanf62d75e2014-07-11 15:14:19 -07001243 pthread_mutex_unlock(&in->lock);
1244 pthread_mutex_unlock(&in->dev->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001245 str_parms_destroy(parms);
1246
1247 return ret_value;
1248}
1249
Paul McLean30f41852014-04-16 15:44:20 -07001250static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
1251 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001252
Paul McLean30f41852014-04-16 15:44:20 -07001253 struct stream_in *in = (struct stream_in *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -07001254 pthread_mutex_lock(&in->dev->lock);
1255 pthread_mutex_lock(&in->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001256
Paul McLeanf62d75e2014-07-11 15:14:19 -07001257 char * params_str = device_get_parameters(in->profile, keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001258
Paul McLeanf62d75e2014-07-11 15:14:19 -07001259 pthread_mutex_unlock(&in->lock);
1260 pthread_mutex_unlock(&in->dev->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001261
Paul McLeanf62d75e2014-07-11 15:14:19 -07001262 return params_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001263}
1264
1265static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1266{
1267 return 0;
1268}
1269
1270static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1271{
1272 return 0;
1273}
1274
Paul McLean30f41852014-04-16 15:44:20 -07001275static int in_set_gain(struct audio_stream_in *stream, float gain)
1276{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001277 return 0;
1278}
1279
Paul McLean30f41852014-04-16 15:44:20 -07001280/* must be called with hw device and output stream mutexes locked */
1281static int start_input_stream(struct stream_in *in) {
Paul McLean30f41852014-04-16 15:44:20 -07001282 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001283
Paul McLean30f41852014-04-16 15:44:20 -07001284 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -07001285 in->profile->card, in->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001286
Paul McLeanf62d75e2014-07-11 15:14:19 -07001287 in->pcm = pcm_open(in->profile->card, in->profile->device, PCM_IN,
1288 &cached_input_hardware_config);
Paul McLean30f41852014-04-16 15:44:20 -07001289 if (in->pcm == NULL) {
1290 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1291 return -ENOMEM;
1292 }
1293
1294 if (in->pcm && !pcm_is_ready(in->pcm)) {
1295 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1296 pcm_close(in->pcm);
1297 return -ENOMEM;
1298 }
1299
1300 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001301}
1302
Paul McLeane32cbc12014-06-25 10:42:07 -07001303/* TODO mutex stuff here (see out_write) */
Paul McLean30f41852014-04-16 15:44:20 -07001304static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1305{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001306 size_t num_read_buff_bytes = 0;
Paul McLean30f41852014-04-16 15:44:20 -07001307 void * read_buff = buffer;
1308 void * out_buff = buffer;
1309
1310 struct stream_in * in = (struct stream_in *) stream;
1311
1312 pthread_mutex_lock(&in->dev->lock);
1313 pthread_mutex_lock(&in->lock);
1314
1315 if (in->standby) {
1316 if (start_input_stream(in) != 0) {
1317 goto err;
1318 }
1319 in->standby = false;
1320 }
1321
1322 // OK, we need to figure out how much data to read to be able to output the requested
1323 // number of bytes in the HAL format (16-bit, stereo).
1324 num_read_buff_bytes = bytes;
1325 int num_device_channels = cached_input_hardware_config.channels;
1326 int num_req_channels = 2; /* always, for now */
1327
1328 if (num_device_channels != num_req_channels) {
Paul McLeancf611912014-04-28 13:03:18 -07001329 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
Paul McLean30f41852014-04-16 15:44:20 -07001330 }
1331
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001332 /* Assume (for now) that in->input_framework_format == PCM_FORMAT_S16_LE */
Eric Laurent7661a482014-06-11 12:00:16 -07001333 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001334 /* 24-bit USB device */
Paul McLean30f41852014-04-16 15:44:20 -07001335 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001336 } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) {
1337 /* 32-bit USB device */
1338 num_read_buff_bytes = num_read_buff_bytes * 2;
Paul McLean30f41852014-04-16 15:44:20 -07001339 }
1340
1341 // Setup/Realloc the conversion buffer (if necessary).
1342 if (num_read_buff_bytes != bytes) {
1343 if (num_read_buff_bytes > in->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -07001344 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1345 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -07001346 in->conversion_buffer_size = num_read_buff_bytes;
1347 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1348 }
1349 read_buff = in->conversion_buffer;
1350 }
1351
1352 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1353 /*
1354 * Do any conversions necessary to send the data in the format specified to/by the HAL
1355 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1356 */
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001357 if (cached_input_hardware_config.format != PCM_FORMAT_S16_LE) {
1358 // we need to convert
Paul McLean30f41852014-04-16 15:44:20 -07001359 if (num_device_channels != num_req_channels) {
1360 out_buff = read_buff;
1361 }
1362
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001363 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
1364 num_read_buff_bytes =
1365 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1366 } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) {
1367 num_read_buff_bytes =
1368 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
1369 }
1370 else {
1371 goto err;
1372 }
Paul McLean30f41852014-04-16 15:44:20 -07001373 }
1374
1375 if (num_device_channels != num_req_channels) {
1376 out_buff = buffer;
1377 /* Num Channels conversion */
Paul McLeaneb192972014-07-11 16:29:41 -07001378 if (num_device_channels != num_req_channels) {
1379 audio_format_t audio_format = in_get_format(&(in->stream.common));
1380 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1381
Paul McLeancf611912014-04-28 13:03:18 -07001382 num_read_buff_bytes =
Paul McLeaneb192972014-07-11 16:29:41 -07001383 adjust_channels(read_buff, num_device_channels,
1384 out_buff, num_req_channels,
1385 sample_size_in_bytes, num_read_buff_bytes);
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 McLean6b1c0fe2014-07-02 07:27:41 -07001435 in->input_framework_format = PCM_FORMAT_S16_LE;
1436
Paul McLean30f41852014-04-16 15:44:20 -07001437 in->dev = (struct audio_device *)dev;
1438
Paul McLeanf62d75e2014-07-11 15:14:19 -07001439 in->profile = &(in->dev->in_profile);
1440 in->profile->direction = PCM_IN;
1441
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001442 if (!input_hardware_config_is_cached) {
1443 // just return defaults until we can actually query the device.
Paul McLean30f41852014-04-16 15:44:20 -07001444 cached_input_hardware_config = default_alsa_in_config;
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001445 }
Paul McLean30f41852014-04-16 15:44:20 -07001446
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001447 /* Rate */
1448 /* TODO Check that the requested rate is valid for the connected device */
1449 if (config->sample_rate == 0) {
1450 config->sample_rate = cached_input_hardware_config.rate;
1451 } else {
1452 cached_input_hardware_config.rate = config->sample_rate;
1453 }
1454
1455 /* Format */
1456 /* until the framework supports format conversion, just take what it asks for
1457 * i.e. AUDIO_FORMAT_PCM_16_BIT */
1458 /* config->format = audio_format_from_pcm_format(cached_input_hardware_config.format); */
1459 if (config->format == AUDIO_FORMAT_DEFAULT) {
1460 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
1461 * formats */
1462 config->format = AUDIO_FORMAT_PCM_16_BIT;
1463 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
1464 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
1465 * formats */
1466 } else {
1467 /* When the framework support other formats, validate here */
1468 config->format = AUDIO_FORMAT_PCM_16_BIT;
1469 ret = -EINVAL;
1470 }
1471
1472 /* don't change the cached_input_hardware_config, we will open it as what it is and
1473 * convert as necessary */
1474 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1475 /* just return AUDIO_CHANNEL_IN_STEREO until the framework supports other input
1476 * formats */
1477 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
1478 } else if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
1479 /* allow only stereo capture for now */
1480 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
1481 ret = -EINVAL;
Paul McLean30f41852014-04-16 15:44:20 -07001482 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001483
1484 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001485
Paul McLean30f41852014-04-16 15:44:20 -07001486 in->conversion_buffer = NULL;
1487 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001488
1489 *stream_in = &in->stream;
1490
Eric Laurent7661a482014-06-11 12:00:16 -07001491 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001492}
1493
Paul McLean30f41852014-04-16 15:44:20 -07001494static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001495{
Paul McLean30f41852014-04-16 15:44:20 -07001496 struct stream_in *in = (struct stream_in *)stream;
1497
Paul McLeane32cbc12014-06-25 10:42:07 -07001498 // Close the pcm device
Paul McLean30f41852014-04-16 15:44:20 -07001499 in_standby(&stream->common);
1500
1501 free(in->conversion_buffer);
1502
1503 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001504}
1505
1506static int adev_dump(const audio_hw_device_t *device, int fd)
1507{
1508 return 0;
1509}
1510
1511static int adev_close(hw_device_t *device)
1512{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001513 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001514 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001515
1516 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001517 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001518
Simon Wilson19957a32012-04-06 16:17:12 -07001519 return 0;
1520}
1521
Paul McLean30f41852014-04-16 15:44:20 -07001522static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001523{
Simon Wilson19957a32012-04-06 16:17:12 -07001524 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1525 return -EINVAL;
1526
Paul McLeaneedc92e2013-12-19 15:46:15 -08001527 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001528 if (!adev)
1529 return -ENOMEM;
1530
1531 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001532 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001533 adev->hw_device.common.module = (struct hw_module_t *) module;
1534 adev->hw_device.common.close = adev_close;
1535
Simon Wilson19957a32012-04-06 16:17:12 -07001536 adev->hw_device.init_check = adev_init_check;
1537 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1538 adev->hw_device.set_master_volume = adev_set_master_volume;
1539 adev->hw_device.set_mode = adev_set_mode;
1540 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1541 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1542 adev->hw_device.set_parameters = adev_set_parameters;
1543 adev->hw_device.get_parameters = adev_get_parameters;
1544 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1545 adev->hw_device.open_output_stream = adev_open_output_stream;
1546 adev->hw_device.close_output_stream = adev_close_output_stream;
1547 adev->hw_device.open_input_stream = adev_open_input_stream;
1548 adev->hw_device.close_input_stream = adev_close_input_stream;
1549 adev->hw_device.dump = adev_dump;
1550
1551 *device = &adev->hw_device.common;
1552
1553 return 0;
1554}
1555
1556static struct hw_module_methods_t hal_module_methods = {
1557 .open = adev_open,
1558};
1559
1560struct audio_module HAL_MODULE_INFO_SYM = {
1561 .common = {
1562 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001563 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1564 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001565 .id = AUDIO_HARDWARE_MODULE_ID,
1566 .name = "USB audio HW HAL",
1567 .author = "The Android Open Source Project",
1568 .methods = &hal_module_methods,
1569 },
1570};