blob: 05f0d2b3b26ae6776e3b56a834ab706abd92ac2d [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/*
Paul McLeana02bc9d2014-07-16 08:40:00 -0700419 * Maps from bit position in pcm_mask to PCM_ format constants.
Paul McLeane32cbc12014-06-25 10:42:07 -0700420 */
Paul McLeana02bc9d2014-07-16 08:40:00 -0700421static int8_t const pcm_format_value_map[] = {
Paul McLeane32cbc12014-06-25 10:42:07 -0700422 PCM_FORMAT_S8, /* 00 - SNDRV_PCM_FORMAT_S8 */
Paul McLeana02bc9d2014-07-16 08:40:00 -0700423 PCM_FORMAT_INVALID, /* 01 - SNDRV_PCM_FORMAT_U8 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700424 PCM_FORMAT_S16_LE, /* 02 - SNDRV_PCM_FORMAT_S16_LE */
Paul McLeana02bc9d2014-07-16 08:40:00 -0700425 PCM_FORMAT_INVALID, /* 03 - SNDRV_PCM_FORMAT_S16_BE */
426 PCM_FORMAT_INVALID, /* 04 - SNDRV_PCM_FORMAT_U16_LE */
427 PCM_FORMAT_INVALID, /* 05 - SNDRV_PCM_FORMAT_U16_BE */
Paul McLeane32cbc12014-06-25 10:42:07 -0700428 PCM_FORMAT_S24_3LE, /* 06 - SNDRV_PCM_FORMAT_S24_LE */
Paul McLeana02bc9d2014-07-16 08:40:00 -0700429 PCM_FORMAT_INVALID, /* 07 - SNDRV_PCM_FORMAT_S24_BE */
430 PCM_FORMAT_INVALID, /* 08 - SNDRV_PCM_FORMAT_U24_LE */
431 PCM_FORMAT_INVALID, /* 09 - SNDRV_PCM_FORMAT_U24_BE */
Paul McLeane32cbc12014-06-25 10:42:07 -0700432 PCM_FORMAT_S32_LE, /* 10 - SNDRV_PCM_FORMAT_S32_LE */
Paul McLeana02bc9d2014-07-16 08:40:00 -0700433 PCM_FORMAT_INVALID, /* 11 - SNDRV_PCM_FORMAT_S32_BE */
434 PCM_FORMAT_INVALID, /* 12 - SNDRV_PCM_FORMAT_U32_LE */
435 PCM_FORMAT_INVALID, /* 13 - SNDRV_PCM_FORMAT_U32_BE */
436 PCM_FORMAT_INVALID, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */
437 PCM_FORMAT_INVALID, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */
438 PCM_FORMAT_INVALID, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */
439 PCM_FORMAT_INVALID, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */
440 PCM_FORMAT_INVALID, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */
441 PCM_FORMAT_INVALID, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */
442 PCM_FORMAT_INVALID, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */
443 PCM_FORMAT_INVALID, /* 21 - SNDRV_PCM_FORMAT_A_LAW */
444 PCM_FORMAT_INVALID, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */
445 PCM_FORMAT_INVALID, /* 23 - SNDRV_PCM_FORMAT_MPEG */
446 PCM_FORMAT_INVALID, /* 24 - SNDRV_PCM_FORMAT_GSM */
447 PCM_FORMAT_INVALID, /* 25 -> 30 (not assigned) */
448 PCM_FORMAT_INVALID,
449 PCM_FORMAT_INVALID,
450 PCM_FORMAT_INVALID,
451 PCM_FORMAT_INVALID,
452 PCM_FORMAT_INVALID,
453 PCM_FORMAT_INVALID, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */
Paul McLeane32cbc12014-06-25 10:42:07 -0700454 PCM_FORMAT_S24_3LE, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */
Paul McLeana02bc9d2014-07-16 08:40:00 -0700455 PCM_FORMAT_INVALID, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */
456 PCM_FORMAT_INVALID, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */
457 PCM_FORMAT_INVALID, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */
458 PCM_FORMAT_INVALID, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */
459 PCM_FORMAT_INVALID, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */
460 PCM_FORMAT_INVALID, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */
461 PCM_FORMAT_INVALID, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */
462 PCM_FORMAT_INVALID, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */
463 PCM_FORMAT_INVALID, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */
464 PCM_FORMAT_INVALID, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */
465 PCM_FORMAT_INVALID, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */
466 PCM_FORMAT_INVALID, /* 44 - SNDRV_PCM_FORMAT_G723_24 */
467 PCM_FORMAT_INVALID, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */
468 PCM_FORMAT_INVALID, /* 46 - SNDRV_PCM_FORMAT_G723_40 */
469 PCM_FORMAT_INVALID, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */
470 PCM_FORMAT_INVALID, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */
471 PCM_FORMAT_INVALID /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */
Paul McLeane32cbc12014-06-25 10:42:07 -0700472};
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
Paul McLeana02bc9d2014-07-16 08:40:00 -0700482 int table_size = ARRAY_SIZE(pcm_format_value_map);
Paul McLeane32cbc12014-06-25 10:42:07 -0700483
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 McLeana02bc9d2014-07-16 08:40:00 -0700491 /* TODO - we don't want a low-level function to be making this decision */
Paul McLeaneb192972014-07-11 16:29:41 -0700492 if (table_index != 0) { /* Don't pick 8-bit */
493 /* just return the first one */
Paul McLeana02bc9d2014-07-16 08:40:00 -0700494 return (int)pcm_format_value_map[table_index];
Paul McLeaneb192972014-07-11 16:29:41 -0700495 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700496 }
497 bit_mask <<= 1;
498 table_index++;
499 }
500 }
501
Paul McLeana02bc9d2014-07-16 08:40:00 -0700502 return PCM_FORMAT_INVALID;
Paul McLeane32cbc12014-06-25 10:42:07 -0700503}
504
Paul McLeanf62d75e2014-07-11 15:14:19 -0700505static bool test_out_sample_rate(struct audio_device_profile* dev_profile, unsigned rate) {
506 struct pcm_config local_config = cached_output_hardware_config;
507 local_config.rate = rate;
508
509 bool works = false; /* let's be pessimistic */
510 struct pcm * pcm =
511 pcm_open(dev_profile->card, dev_profile->device, dev_profile->direction, &local_config);
512
513 if (pcm != NULL) {
514 works = pcm_is_ready(pcm);
515 pcm_close(pcm);
516 }
517
518 return works;
519}
520
521/* sort these highest -> lowest */
522static const unsigned std_sample_rates[] =
523 {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};
524
525static char* enum_std_sample_rates(struct audio_device_profile* dev_profile,
526 unsigned min, unsigned max)
527{
528 char buffer[128];
529 buffer[0] = '\0';
530 int buffSize = ARRAY_SIZE(buffer);
531
532 char numBuffer[32];
533
534 int numEntries = 0;
535 unsigned index;
536 for(index = 0; index < ARRAY_SIZE(std_sample_rates); index++) {
537 if (std_sample_rates[index] >= min && std_sample_rates[index] <= max &&
538 test_out_sample_rate(dev_profile, std_sample_rates[index])) {
539 if (numEntries++ != 0) {
540 strncat(buffer, "|", buffSize);
541 }
542 snprintf(numBuffer, sizeof(numBuffer), "%u", std_sample_rates[index]);
543 strncat(buffer, numBuffer, buffSize);
544 }
545 }
546
547 return strdup(buffer);
548}
549
550/*
551 * Logging
552 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700553static void log_pcm_mask(const char* mask_name, struct pcm_mask* mask) {
554 char buff[512];
555 char bit_buff[32];
556 int buffSize = sizeof(buff)/sizeof(buff[0]);
557
558 buff[0] = '\0';
559
560 int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]);
561 int bits_per_slot = sizeof(mask->bits[0]) * 8;
562
563 int slot_index, bit_index;
564 strcat(buff, "[");
565 for (slot_index = 0; slot_index < num_slots; slot_index++) {
566 unsigned bit_mask = 1;
567 for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
568 strcat(buff, (mask->bits[slot_index] & bit_mask) != 0 ? "1" : "0");
569 bit_mask <<= 1;
570 }
571 if (slot_index < num_slots - 1) {
572 strcat(buff, ",");
573 }
574 }
575 strcat(buff, "]");
576
577 ALOGV("usb:audio_hw - %s mask:%s", mask_name, buff);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800578}
579
Paul McLeancf611912014-04-28 13:03:18 -0700580static void log_pcm_params(struct pcm_params * alsa_hw_params) {
581 ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u",
582 pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS),
583 pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS));
584 ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u",
585 pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS),
586 pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS));
Paul McLeane32cbc12014-06-25 10:42:07 -0700587 log_pcm_mask("PCM_PARAM_FORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
588 log_pcm_mask("PCM_PARAM_SUBFORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_SUBFORMAT));
Paul McLeancf611912014-04-28 13:03:18 -0700589 ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u",
590 pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
591 pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
592 ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u",
593 pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
594 pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
595 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u",
596 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME),
597 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME));
598 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u",
599 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE),
600 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE));
601 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u",
602 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES),
603 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES));
604 ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u",
605 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS),
606 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS));
607 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u",
608 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME),
609 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME));
610 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u",
611 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE),
612 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE));
613 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u",
614 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES),
615 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES));
616 ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u",
617 pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME),
618 pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME));
619}
620
Paul McLeaneedc92e2013-12-19 15:46:15 -0800621/*
Paul McLean33a6b172014-06-19 12:35:28 -0700622 * Returns the supplied value rounded up to the next even multiple of 16
623 */
624static unsigned int round_to_16_mult(unsigned int size) {
625 return (size + 15) & 0xFFFFFFF0;
626}
627
Paul McLeane32cbc12014-06-25 10:42:07 -0700628/*TODO - Evaluate if this value should/can be retrieved from a device-specific property */
Paul McLean33a6b172014-06-19 12:35:28 -0700629#define MIN_BUFF_TIME 5 /* milliseconds */
630
631/*
632 * Returns the system defined minimum period size based on the supplied sample rate
633 */
634static unsigned int calc_min_period_size(unsigned int sample_rate) {
635 unsigned int period_size = (sample_rate * MIN_BUFF_TIME) / 1000;
636 return round_to_16_mult(period_size);
637}
638
639/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800640 * Reads and decodes configuration info from the specified ALSA card/device
641 */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700642static int read_alsa_device_config(struct audio_device_profile * dev_profile,
643 struct pcm_config * config)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800644{
Paul McLeanf62d75e2014-07-11 15:14:19 -0700645 ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)",
646 dev_profile->card, dev_profile->device, dev_profile->direction);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800647
Paul McLeanf62d75e2014-07-11 15:14:19 -0700648 if (dev_profile->card < 0 || dev_profile->device < 0) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800649 return -EINVAL;
650 }
651
Paul McLeanf62d75e2014-07-11 15:14:19 -0700652 struct pcm_params * alsa_hw_params =
653 pcm_params_get(dev_profile->card, dev_profile->device, dev_profile->direction);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800654 if (alsa_hw_params == NULL) {
655 return -EINVAL;
656 }
657
Paul McLeaneb192972014-07-11 16:29:41 -0700658 int ret = 0;
659
Paul McLeaneedc92e2013-12-19 15:46:15 -0800660 /*
661 * This Logging will be useful when testing new USB devices.
662 */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700663#ifdef LOG_PCM_PARAMS
664 log_pcm_params(alsa_hw_params);
665#endif
Paul McLeaneedc92e2013-12-19 15:46:15 -0800666
667 config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
668 config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
Paul McLean33a6b172014-06-19 12:35:28 -0700669 config->period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE);
670 /* round this up to a multiple of 16 */
671 config->period_size = round_to_16_mult(config->period_size);
672 /* make sure it is above a minimum value to minimize jitter */
673 unsigned int min_period_size = calc_min_period_size(config->rate);
674 if (config->period_size < min_period_size) {
675 config->period_size = min_period_size;
676 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800677 config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
678
Paul McLeaneb192972014-07-11 16:29:41 -0700679 int format = get_pcm_format_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
Paul McLeana02bc9d2014-07-16 08:40:00 -0700680 if (format == PCM_FORMAT_INVALID) {
Paul McLeaneb192972014-07-11 16:29:41 -0700681 ret = -EINVAL;
682 } else {
683 config->format = format;
684 }
Paul McLeanf62d75e2014-07-11 15:14:19 -0700685
686 pcm_params_free(alsa_hw_params);
Paul McLeaneb192972014-07-11 16:29:41 -0700687 return ret;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800688}
689
690/*
691 * HAl Functions
692 */
Simon Wilson19957a32012-04-06 16:17:12 -0700693/**
694 * NOTE: when multiple mutexes have to be acquired, always respect the
695 * following order: hw device > out stream
696 */
697
698/* Helper functions */
Simon Wilson19957a32012-04-06 16:17:12 -0700699static uint32_t out_get_sample_rate(const struct audio_stream *stream)
700{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800701 return cached_output_hardware_config.rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700702}
703
704static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
705{
706 return 0;
707}
708
709static size_t out_get_buffer_size(const struct audio_stream *stream)
710{
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700711 return cached_output_hardware_config.period_size *
712 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700713}
714
715static uint32_t out_get_channels(const struct audio_stream *stream)
716{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800717 // Always Stero for now. We will do *some* conversions in this HAL.
Paul McLeane32cbc12014-06-25 10:42:07 -0700718 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
719 rewrite this to return the ACTUAL channel format */
Simon Wilson19957a32012-04-06 16:17:12 -0700720 return AUDIO_CHANNEL_OUT_STEREO;
721}
722
723static audio_format_t out_get_format(const struct audio_stream *stream)
724{
Paul McLeane32cbc12014-06-25 10:42:07 -0700725 return audio_format_from_pcm_format(cached_output_hardware_config.format);
Simon Wilson19957a32012-04-06 16:17:12 -0700726}
727
728static int out_set_format(struct audio_stream *stream, audio_format_t format)
729{
Paul McLeane32cbc12014-06-25 10:42:07 -0700730 cached_output_hardware_config.format = pcm_format_from_audio_format(format);
Simon Wilson19957a32012-04-06 16:17:12 -0700731 return 0;
732}
733
734static int out_standby(struct audio_stream *stream)
735{
736 struct stream_out *out = (struct stream_out *)stream;
737
738 pthread_mutex_lock(&out->dev->lock);
739 pthread_mutex_lock(&out->lock);
740
741 if (!out->standby) {
742 pcm_close(out->pcm);
743 out->pcm = NULL;
744 out->standby = true;
745 }
746
747 pthread_mutex_unlock(&out->lock);
748 pthread_mutex_unlock(&out->dev->lock);
749
750 return 0;
751}
752
753static int out_dump(const struct audio_stream *stream, int fd)
754{
755 return 0;
756}
757
758static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
759{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800760 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
761
Simon Wilson19957a32012-04-06 16:17:12 -0700762 struct stream_out *out = (struct stream_out *)stream;
Simon Wilson19957a32012-04-06 16:17:12 -0700763 struct str_parms *parms;
764 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800765 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700766 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800767 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700768
769 parms = str_parms_create_str(kvpairs);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700770 pthread_mutex_lock(&out->dev->lock);
771 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700772
Paul McLeaneedc92e2013-12-19 15:46:15 -0800773 bool recache_device_params = false;
774 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
775 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700776 out->profile->card = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800777 recache_device_params = true;
778 }
Simon Wilson19957a32012-04-06 16:17:12 -0700779
Paul McLeaneedc92e2013-12-19 15:46:15 -0800780 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
781 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700782 out->profile->device = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800783 recache_device_params = true;
784 }
785
Paul McLeanf62d75e2014-07-11 15:14:19 -0700786 if (recache_device_params && out->profile->card >= 0 && out->profile->device >= 0) {
787 ret_value = read_alsa_device_config(out->profile, &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800788 output_hardware_config_is_cached = (ret_value == 0);
789 }
Simon Wilson19957a32012-04-06 16:17:12 -0700790
Paul McLeanf62d75e2014-07-11 15:14:19 -0700791 pthread_mutex_unlock(&out->lock);
792 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700793 str_parms_destroy(parms);
794
Paul McLeaneedc92e2013-12-19 15:46:15 -0800795 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700796}
797
Paul McLeanf62d75e2014-07-11 15:14:19 -0700798static char * device_get_parameters(struct audio_device_profile * dev_profile, const char *keys)
Paul McLean30f41852014-04-16 15:44:20 -0700799{
Paul McLeanf62d75e2014-07-11 15:14:19 -0700800 ALOGV("usb:audio_hw::device_get_parameters() keys:%s", keys);
Paul McLean30f41852014-04-16 15:44:20 -0700801
Paul McLeanf62d75e2014-07-11 15:14:19 -0700802 if (dev_profile->card < 0 || dev_profile->device < 0) {
Paul McLean30f41852014-04-16 15:44:20 -0700803 return strdup("");
Paul McLeanf62d75e2014-07-11 15:14:19 -0700804 }
Paul McLean30f41852014-04-16 15:44:20 -0700805
Paul McLeaneedc92e2013-12-19 15:46:15 -0800806 unsigned min, max;
807
808 struct str_parms *query = str_parms_create_str(keys);
809 struct str_parms *result = str_parms_create();
810
811 int num_written = 0;
812 char buffer[256];
813 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
814 char* result_str = NULL;
815
Paul McLeanf62d75e2014-07-11 15:14:19 -0700816 struct pcm_params * alsa_hw_params =
817 pcm_params_get(dev_profile->card, dev_profile->device, dev_profile->direction);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800818
819 // These keys are from hardware/libhardware/include/audio.h
820 // supported sample rates
821 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800822 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
823 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700824
825 char* rates_list = enum_std_sample_rates(dev_profile, min, max);
826 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, rates_list);
827 free(rates_list);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800828 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
829
830 // supported channel counts
831 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700832 // TODO remove this hack when it is superceeded by proper multi-channel support
833 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
834 dev_profile->direction == PCM_OUT
835 ? "AUDIO_CHANNEL_OUT_STEREO"
836 : "AUDIO_CHANNEL_IN_STEREO");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800837 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
838
839 // supported sample formats
840 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
Paul McLeanf62d75e2014-07-11 15:14:19 -0700841 // TODO remove this hack when we have support for input in non PCM16 formats
842 if (dev_profile->direction == PCM_IN) {
843 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, "AUDIO_FORMAT_PCM_16_BIT");
844 } else {
845 struct pcm_mask * format_mask = pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT);
846 char * format_params = get_format_str_for_mask(format_mask);
847 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, format_params);
848 free(format_params);
849 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800850 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
851
Paul McLeanf62d75e2014-07-11 15:14:19 -0700852 pcm_params_free(alsa_hw_params);
853
Paul McLeaneedc92e2013-12-19 15:46:15 -0800854 result_str = str_parms_to_str(result);
855
856 // done with these...
857 str_parms_destroy(query);
858 str_parms_destroy(result);
859
Paul McLeanf62d75e2014-07-11 15:14:19 -0700860 ALOGV("usb:audio_hw::device_get_parameters = %s", result_str);
Paul McLeane32cbc12014-06-25 10:42:07 -0700861
Paul McLeaneedc92e2013-12-19 15:46:15 -0800862 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700863}
864
Paul McLeanf62d75e2014-07-11 15:14:19 -0700865static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
866{
867 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
868
869 struct stream_out *out = (struct stream_out *) stream;
870 pthread_mutex_lock(&out->dev->lock);
871 pthread_mutex_lock(&out->lock);
872
873 char * params_str = device_get_parameters(out->profile, keys);
874
875 pthread_mutex_unlock(&out->lock);
876 pthread_mutex_unlock(&out->dev->lock);
877
878 return params_str;
879}
880
Simon Wilson19957a32012-04-06 16:17:12 -0700881static uint32_t out_get_latency(const struct audio_stream_out *stream)
882{
Paul McLeanf62d75e2014-07-11 15:14:19 -0700883 // struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800884
Paul McLeane32cbc12014-06-25 10:42:07 -0700885 /*TODO Do we need a term here for the USB latency (as reported in the USB descriptors)? */
Paul McLean30f41852014-04-16 15:44:20 -0700886 uint32_t latency = (cached_output_hardware_config.period_size
Paul McLeane32cbc12014-06-25 10:42:07 -0700887 * cached_output_hardware_config.period_count * 1000)
888 / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800889 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700890}
891
Paul McLean30f41852014-04-16 15:44:20 -0700892static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700893{
894 return -ENOSYS;
895}
896
Paul McLeaneedc92e2013-12-19 15:46:15 -0800897/* must be called with hw device and output stream mutexes locked */
898static int start_output_stream(struct stream_out *out)
899{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800900 int return_val = 0;
901
Paul McLean30f41852014-04-16 15:44:20 -0700902 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -0700903 out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800904
Paul McLeanf62d75e2014-07-11 15:14:19 -0700905 out->pcm = pcm_open(out->profile->card, out->profile->device, PCM_OUT,
906 &cached_output_hardware_config);
Paul McLeane32cbc12014-06-25 10:42:07 -0700907
Paul McLeaneedc92e2013-12-19 15:46:15 -0800908 if (out->pcm == NULL) {
909 return -ENOMEM;
910 }
911
912 if (out->pcm && !pcm_is_ready(out->pcm)) {
913 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
914 pcm_close(out->pcm);
915 return -ENOMEM;
916 }
917
Paul McLeaneedc92e2013-12-19 15:46:15 -0800918 return 0;
919}
920
921static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700922{
923 int ret;
924 struct stream_out *out = (struct stream_out *)stream;
925
926 pthread_mutex_lock(&out->dev->lock);
927 pthread_mutex_lock(&out->lock);
928 if (out->standby) {
929 ret = start_output_stream(out);
930 if (ret != 0) {
931 goto err;
932 }
933 out->standby = false;
934 }
935
Paul McLean30f41852014-04-16 15:44:20 -0700936 // Setup conversion buffer
937 // compute maximum potential buffer size.
938 // * 2 for stereo -> quad conversion
939 // * 3/2 for 16bit -> 24 bit conversion
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700940 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700941 if (required_conversion_buffer_size > out->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -0700942 /* TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
943 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -0700944 out->conversion_buffer_size = required_conversion_buffer_size;
945 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
946 }
947
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700948 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800949 int num_write_buff_bytes = bytes;
950
951 /*
952 * Num Channels conversion
953 */
954 int num_device_channels = cached_output_hardware_config.channels;
955 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700956 if (num_device_channels != num_req_channels) {
Paul McLeaneb192972014-07-11 16:29:41 -0700957 audio_format_t audio_format = out_get_format(&(out->stream.common));
958 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800959 num_write_buff_bytes =
Paul McLeaneb192972014-07-11 16:29:41 -0700960 adjust_channels(write_buff, num_req_channels,
961 out->conversion_buffer, num_device_channels,
962 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800963 write_buff = out->conversion_buffer;
964 }
965
Paul McLeaneedc92e2013-12-19 15:46:15 -0800966 if (write_buff != NULL && num_write_buff_bytes != 0) {
967 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
968 }
Simon Wilson19957a32012-04-06 16:17:12 -0700969
970 pthread_mutex_unlock(&out->lock);
971 pthread_mutex_unlock(&out->dev->lock);
972
973 return bytes;
974
975err:
976 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800977 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700978 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700979 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700980 out_get_sample_rate(&stream->common));
981 }
982
983 return bytes;
984}
985
Paul McLean30f41852014-04-16 15:44:20 -0700986static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700987{
988 return -EINVAL;
989}
990
991static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
992{
993 return 0;
994}
995
996static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
997{
998 return 0;
999}
1000
Paul McLean30f41852014-04-16 15:44:20 -07001001static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -07001002{
1003 return -EINVAL;
1004}
1005
1006static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001007 audio_io_handle_t handle,
1008 audio_devices_t devices,
1009 audio_output_flags_t flags,
1010 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001011 struct audio_stream_out **stream_out)
1012{
Paul McLean30f41852014-04-16 15:44:20 -07001013 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 -08001014 handle, devices, flags);
1015
Simon Wilson19957a32012-04-06 16:17:12 -07001016 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001017
Simon Wilson19957a32012-04-06 16:17:12 -07001018 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -07001019
1020 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1021 if (!out)
1022 return -ENOMEM;
1023
Paul McLeaneedc92e2013-12-19 15:46:15 -08001024 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -07001025 out->stream.common.get_sample_rate = out_get_sample_rate;
1026 out->stream.common.set_sample_rate = out_set_sample_rate;
1027 out->stream.common.get_buffer_size = out_get_buffer_size;
1028 out->stream.common.get_channels = out_get_channels;
1029 out->stream.common.get_format = out_get_format;
1030 out->stream.common.set_format = out_set_format;
1031 out->stream.common.standby = out_standby;
1032 out->stream.common.dump = out_dump;
1033 out->stream.common.set_parameters = out_set_parameters;
1034 out->stream.common.get_parameters = out_get_parameters;
1035 out->stream.common.add_audio_effect = out_add_audio_effect;
1036 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1037 out->stream.get_latency = out_get_latency;
1038 out->stream.set_volume = out_set_volume;
1039 out->stream.write = out_write;
1040 out->stream.get_render_position = out_get_render_position;
1041 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1042
1043 out->dev = adev;
1044
Paul McLeanf62d75e2014-07-11 15:14:19 -07001045 out->profile = &(adev->out_profile);
1046 out->profile->direction = PCM_OUT;
1047
Paul McLeaneedc92e2013-12-19 15:46:15 -08001048 if (output_hardware_config_is_cached) {
1049 config->sample_rate = cached_output_hardware_config.rate;
1050
Paul McLeane32cbc12014-06-25 10:42:07 -07001051 config->format = audio_format_from_pcm_format(cached_output_hardware_config.format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001052
1053 config->channel_mask =
1054 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
1055 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
1056 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1057 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
Paul McLeane32cbc12014-06-25 10:42:07 -07001058 /*TODO remove this when the above restriction is removed. */
Paul McLeaneedc92e2013-12-19 15:46:15 -08001059 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1060 }
1061 } else {
1062 cached_output_hardware_config = default_alsa_out_config;
1063
1064 config->format = out_get_format(&out->stream.common);
1065 config->channel_mask = out_get_channels(&out->stream.common);
1066 config->sample_rate = out_get_sample_rate(&out->stream.common);
1067 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001068
1069 out->conversion_buffer = NULL;
1070 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001071
1072 out->standby = true;
1073
1074 *stream_out = &out->stream;
1075 return 0;
1076
1077err_open:
1078 free(out);
1079 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001080 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -07001081}
1082
1083static void adev_close_output_stream(struct audio_hw_device *dev,
1084 struct audio_stream_out *stream)
1085{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001086 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -07001087 struct stream_out *out = (struct stream_out *)stream;
1088
Paul McLeane32cbc12014-06-25 10:42:07 -07001089 // Close the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -07001090 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001091
1092 free(out->conversion_buffer);
1093 out->conversion_buffer = NULL;
1094 out->conversion_buffer_size = 0;
1095
Simon Wilson19957a32012-04-06 16:17:12 -07001096 free(stream);
1097}
1098
1099static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1100{
1101 return 0;
1102}
1103
Paul McLean30f41852014-04-16 15:44:20 -07001104static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001105{
1106 return strdup("");
1107}
1108
1109static int adev_init_check(const struct audio_hw_device *dev)
1110{
1111 return 0;
1112}
1113
1114static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1115{
1116 return -ENOSYS;
1117}
1118
1119static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1120{
1121 return -ENOSYS;
1122}
1123
1124static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1125{
1126 return 0;
1127}
1128
1129static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1130{
1131 return -ENOSYS;
1132}
1133
1134static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1135{
1136 return -ENOSYS;
1137}
1138
1139static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -07001140 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -07001141{
1142 return 0;
1143}
1144
Paul McLeaneedc92e2013-12-19 15:46:15 -08001145/* Helper functions */
1146static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1147{
Paul McLean30f41852014-04-16 15:44:20 -07001148 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001149}
1150
1151static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1152{
1153 return -ENOSYS;
1154}
1155
1156static size_t in_get_buffer_size(const struct audio_stream *stream)
1157{
Eric Laurentc5ae6a02014-07-02 13:45:32 -07001158 size_t buffer_size = cached_input_hardware_config.period_size *
1159 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
1160 ALOGV("usb: in_get_buffer_size() = %zu", buffer_size);
1161 return buffer_size;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001162}
1163
1164static uint32_t in_get_channels(const struct audio_stream *stream)
1165{
Paul McLean30f41852014-04-16 15:44:20 -07001166 // just report stereo for now
1167 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001168}
1169
1170static audio_format_t in_get_format(const struct audio_stream *stream)
1171{
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001172 const struct stream_in * in_stream = (const struct stream_in *)stream;
1173
1174 ALOGV("in_get_format() = %d -> %d", in_stream->input_framework_format,
1175 audio_format_from_pcm_format(in_stream->input_framework_format));
1176 /* return audio_format_from_pcm_format(cached_input_hardware_config.format); */
1177 return audio_format_from_pcm_format(in_stream->input_framework_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001178}
1179
1180static int in_set_format(struct audio_stream *stream, audio_format_t format)
1181{
1182 return -ENOSYS;
1183}
1184
1185static int in_standby(struct audio_stream *stream)
1186{
Paul McLean30f41852014-04-16 15:44:20 -07001187 struct stream_in *in = (struct stream_in *) stream;
1188
1189 pthread_mutex_lock(&in->dev->lock);
1190 pthread_mutex_lock(&in->lock);
1191
1192 if (!in->standby) {
1193 pcm_close(in->pcm);
1194 in->pcm = NULL;
1195 in->standby = true;
1196 }
1197
1198 pthread_mutex_unlock(&in->lock);
1199 pthread_mutex_unlock(&in->dev->lock);
1200
Paul McLeaneedc92e2013-12-19 15:46:15 -08001201 return 0;
1202}
1203
1204static int in_dump(const struct audio_stream *stream, int fd)
1205{
1206 return 0;
1207}
1208
1209static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1210{
Paul McLean30f41852014-04-16 15:44:20 -07001211 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001212
1213 struct stream_in *in = (struct stream_in *)stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001214 struct str_parms *parms;
1215 char value[32];
1216 int param_val;
1217 int routing = 0;
1218 int ret_value = 0;
1219
1220 parms = str_parms_create_str(kvpairs);
Paul McLeanf62d75e2014-07-11 15:14:19 -07001221 pthread_mutex_lock(&in->dev->lock);
1222 pthread_mutex_lock(&in->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001223
Paul McLean30f41852014-04-16 15:44:20 -07001224 bool recache_device_params = false;
1225
Paul McLeaneedc92e2013-12-19 15:46:15 -08001226 // Card/Device
1227 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1228 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -07001229 in->profile->card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001230 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001231 }
1232
1233 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1234 if (param_val >= 0) {
Paul McLeanf62d75e2014-07-11 15:14:19 -07001235 in->profile->device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -07001236 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001237 }
1238
Paul McLeanf62d75e2014-07-11 15:14:19 -07001239 if (recache_device_params && in->profile->card >= 0 && in->profile->device >= 0) {
1240 ret_value = read_alsa_device_config(in->profile, &cached_input_hardware_config);
Paul McLean30f41852014-04-16 15:44:20 -07001241 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001242 }
1243
Paul McLeanf62d75e2014-07-11 15:14:19 -07001244 pthread_mutex_unlock(&in->lock);
1245 pthread_mutex_unlock(&in->dev->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001246 str_parms_destroy(parms);
1247
1248 return ret_value;
1249}
1250
Paul McLean30f41852014-04-16 15:44:20 -07001251static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
1252 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001253
Paul McLean30f41852014-04-16 15:44:20 -07001254 struct stream_in *in = (struct stream_in *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -07001255 pthread_mutex_lock(&in->dev->lock);
1256 pthread_mutex_lock(&in->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001257
Paul McLeanf62d75e2014-07-11 15:14:19 -07001258 char * params_str = device_get_parameters(in->profile, keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001259
Paul McLeanf62d75e2014-07-11 15:14:19 -07001260 pthread_mutex_unlock(&in->lock);
1261 pthread_mutex_unlock(&in->dev->lock);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001262
Paul McLeanf62d75e2014-07-11 15:14:19 -07001263 return params_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001264}
1265
1266static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1267{
1268 return 0;
1269}
1270
1271static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1272{
1273 return 0;
1274}
1275
Paul McLean30f41852014-04-16 15:44:20 -07001276static int in_set_gain(struct audio_stream_in *stream, float gain)
1277{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001278 return 0;
1279}
1280
Paul McLean30f41852014-04-16 15:44:20 -07001281/* must be called with hw device and output stream mutexes locked */
1282static int start_input_stream(struct stream_in *in) {
Paul McLean30f41852014-04-16 15:44:20 -07001283 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001284
Paul McLean30f41852014-04-16 15:44:20 -07001285 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -07001286 in->profile->card, in->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001287
Paul McLeanf62d75e2014-07-11 15:14:19 -07001288 in->pcm = pcm_open(in->profile->card, in->profile->device, PCM_IN,
1289 &cached_input_hardware_config);
Paul McLean30f41852014-04-16 15:44:20 -07001290 if (in->pcm == NULL) {
1291 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1292 return -ENOMEM;
1293 }
1294
1295 if (in->pcm && !pcm_is_ready(in->pcm)) {
1296 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1297 pcm_close(in->pcm);
1298 return -ENOMEM;
1299 }
1300
1301 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001302}
1303
Paul McLeane32cbc12014-06-25 10:42:07 -07001304/* TODO mutex stuff here (see out_write) */
Paul McLean30f41852014-04-16 15:44:20 -07001305static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1306{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001307 size_t num_read_buff_bytes = 0;
Paul McLean30f41852014-04-16 15:44:20 -07001308 void * read_buff = buffer;
1309 void * out_buff = buffer;
1310
1311 struct stream_in * in = (struct stream_in *) stream;
1312
1313 pthread_mutex_lock(&in->dev->lock);
1314 pthread_mutex_lock(&in->lock);
1315
1316 if (in->standby) {
1317 if (start_input_stream(in) != 0) {
1318 goto err;
1319 }
1320 in->standby = false;
1321 }
1322
1323 // OK, we need to figure out how much data to read to be able to output the requested
1324 // number of bytes in the HAL format (16-bit, stereo).
1325 num_read_buff_bytes = bytes;
1326 int num_device_channels = cached_input_hardware_config.channels;
1327 int num_req_channels = 2; /* always, for now */
1328
1329 if (num_device_channels != num_req_channels) {
Paul McLeancf611912014-04-28 13:03:18 -07001330 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
Paul McLean30f41852014-04-16 15:44:20 -07001331 }
1332
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001333 /* Assume (for now) that in->input_framework_format == PCM_FORMAT_S16_LE */
Eric Laurent7661a482014-06-11 12:00:16 -07001334 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001335 /* 24-bit USB device */
Paul McLean30f41852014-04-16 15:44:20 -07001336 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001337 } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) {
1338 /* 32-bit USB device */
1339 num_read_buff_bytes = num_read_buff_bytes * 2;
Paul McLean30f41852014-04-16 15:44:20 -07001340 }
1341
1342 // Setup/Realloc the conversion buffer (if necessary).
1343 if (num_read_buff_bytes != bytes) {
1344 if (num_read_buff_bytes > in->conversion_buffer_size) {
Paul McLeane32cbc12014-06-25 10:42:07 -07001345 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1346 (and do these conversions themselves) */
Paul McLean30f41852014-04-16 15:44:20 -07001347 in->conversion_buffer_size = num_read_buff_bytes;
1348 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1349 }
1350 read_buff = in->conversion_buffer;
1351 }
1352
1353 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1354 /*
1355 * Do any conversions necessary to send the data in the format specified to/by the HAL
1356 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1357 */
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001358 if (cached_input_hardware_config.format != PCM_FORMAT_S16_LE) {
1359 // we need to convert
Paul McLean30f41852014-04-16 15:44:20 -07001360 if (num_device_channels != num_req_channels) {
1361 out_buff = read_buff;
1362 }
1363
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001364 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
1365 num_read_buff_bytes =
1366 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1367 } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) {
1368 num_read_buff_bytes =
1369 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
1370 }
1371 else {
1372 goto err;
1373 }
Paul McLean30f41852014-04-16 15:44:20 -07001374 }
1375
1376 if (num_device_channels != num_req_channels) {
1377 out_buff = buffer;
1378 /* Num Channels conversion */
Paul McLeaneb192972014-07-11 16:29:41 -07001379 if (num_device_channels != num_req_channels) {
1380 audio_format_t audio_format = in_get_format(&(in->stream.common));
1381 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1382
Paul McLeancf611912014-04-28 13:03:18 -07001383 num_read_buff_bytes =
Paul McLeaneb192972014-07-11 16:29:41 -07001384 adjust_channels(read_buff, num_device_channels,
1385 out_buff, num_req_channels,
1386 sample_size_in_bytes, num_read_buff_bytes);
Paul McLeancf611912014-04-28 13:03:18 -07001387 }
Paul McLean30f41852014-04-16 15:44:20 -07001388 }
1389 }
1390
1391err:
1392 pthread_mutex_unlock(&in->lock);
1393 pthread_mutex_unlock(&in->dev->lock);
1394
1395 return num_read_buff_bytes;
1396}
1397
1398static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1399{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001400 return 0;
1401}
1402
Mike Lockwood46a98092012-04-24 16:41:18 -07001403static int adev_open_input_stream(struct audio_hw_device *dev,
1404 audio_io_handle_t handle,
1405 audio_devices_t devices,
Paul McLean30f41852014-04-16 15:44:20 -07001406 struct audio_config *config,
Glenn Kasten7d973ad2014-07-15 11:10:38 -07001407 struct audio_stream_in **stream_in,
1408 audio_input_flags_t flags __unused)
Simon Wilson19957a32012-04-06 16:17:12 -07001409{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001410 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLean30f41852014-04-16 15:44:20 -07001411 config->sample_rate, config->channel_mask, config->format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001412
1413 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Eric Laurent7661a482014-06-11 12:00:16 -07001414 int ret = 0;
1415
Paul McLeaneedc92e2013-12-19 15:46:15 -08001416 if (in == NULL)
1417 return -ENOMEM;
1418
1419 // setup function pointers
1420 in->stream.common.get_sample_rate = in_get_sample_rate;
1421 in->stream.common.set_sample_rate = in_set_sample_rate;
1422 in->stream.common.get_buffer_size = in_get_buffer_size;
1423 in->stream.common.get_channels = in_get_channels;
1424 in->stream.common.get_format = in_get_format;
1425 in->stream.common.set_format = in_set_format;
1426 in->stream.common.standby = in_standby;
1427 in->stream.common.dump = in_dump;
1428 in->stream.common.set_parameters = in_set_parameters;
1429 in->stream.common.get_parameters = in_get_parameters;
1430 in->stream.common.add_audio_effect = in_add_audio_effect;
1431 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1432
1433 in->stream.set_gain = in_set_gain;
1434 in->stream.read = in_read;
1435 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1436
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001437 in->input_framework_format = PCM_FORMAT_S16_LE;
1438
Paul McLean30f41852014-04-16 15:44:20 -07001439 in->dev = (struct audio_device *)dev;
1440
Paul McLeanf62d75e2014-07-11 15:14:19 -07001441 in->profile = &(in->dev->in_profile);
1442 in->profile->direction = PCM_IN;
1443
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001444 if (!input_hardware_config_is_cached) {
1445 // just return defaults until we can actually query the device.
Paul McLean30f41852014-04-16 15:44:20 -07001446 cached_input_hardware_config = default_alsa_in_config;
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001447 }
Paul McLean30f41852014-04-16 15:44:20 -07001448
Paul McLean6b1c0fe2014-07-02 07:27:41 -07001449 /* Rate */
1450 /* TODO Check that the requested rate is valid for the connected device */
1451 if (config->sample_rate == 0) {
1452 config->sample_rate = cached_input_hardware_config.rate;
1453 } else {
1454 cached_input_hardware_config.rate = config->sample_rate;
1455 }
1456
1457 /* Format */
1458 /* until the framework supports format conversion, just take what it asks for
1459 * i.e. AUDIO_FORMAT_PCM_16_BIT */
1460 /* config->format = audio_format_from_pcm_format(cached_input_hardware_config.format); */
1461 if (config->format == AUDIO_FORMAT_DEFAULT) {
1462 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
1463 * formats */
1464 config->format = AUDIO_FORMAT_PCM_16_BIT;
1465 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
1466 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
1467 * formats */
1468 } else {
1469 /* When the framework support other formats, validate here */
1470 config->format = AUDIO_FORMAT_PCM_16_BIT;
1471 ret = -EINVAL;
1472 }
1473
1474 /* don't change the cached_input_hardware_config, we will open it as what it is and
1475 * convert as necessary */
1476 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1477 /* just return AUDIO_CHANNEL_IN_STEREO until the framework supports other input
1478 * formats */
1479 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
1480 } else if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
1481 /* allow only stereo capture for now */
1482 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
1483 ret = -EINVAL;
Paul McLean30f41852014-04-16 15:44:20 -07001484 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001485
1486 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001487
Paul McLean30f41852014-04-16 15:44:20 -07001488 in->conversion_buffer = NULL;
1489 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001490
1491 *stream_in = &in->stream;
1492
Eric Laurent7661a482014-06-11 12:00:16 -07001493 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001494}
1495
Paul McLean30f41852014-04-16 15:44:20 -07001496static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001497{
Paul McLean30f41852014-04-16 15:44:20 -07001498 struct stream_in *in = (struct stream_in *)stream;
1499
Paul McLeane32cbc12014-06-25 10:42:07 -07001500 // Close the pcm device
Paul McLean30f41852014-04-16 15:44:20 -07001501 in_standby(&stream->common);
1502
1503 free(in->conversion_buffer);
1504
1505 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001506}
1507
1508static int adev_dump(const audio_hw_device_t *device, int fd)
1509{
1510 return 0;
1511}
1512
1513static int adev_close(hw_device_t *device)
1514{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001515 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001516 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001517
1518 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001519 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001520
Simon Wilson19957a32012-04-06 16:17:12 -07001521 return 0;
1522}
1523
Paul McLean30f41852014-04-16 15:44:20 -07001524static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001525{
Simon Wilson19957a32012-04-06 16:17:12 -07001526 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1527 return -EINVAL;
1528
Paul McLeaneedc92e2013-12-19 15:46:15 -08001529 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001530 if (!adev)
1531 return -ENOMEM;
1532
1533 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001534 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001535 adev->hw_device.common.module = (struct hw_module_t *) module;
1536 adev->hw_device.common.close = adev_close;
1537
Simon Wilson19957a32012-04-06 16:17:12 -07001538 adev->hw_device.init_check = adev_init_check;
1539 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1540 adev->hw_device.set_master_volume = adev_set_master_volume;
1541 adev->hw_device.set_mode = adev_set_mode;
1542 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1543 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1544 adev->hw_device.set_parameters = adev_set_parameters;
1545 adev->hw_device.get_parameters = adev_get_parameters;
1546 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1547 adev->hw_device.open_output_stream = adev_open_output_stream;
1548 adev->hw_device.close_output_stream = adev_close_output_stream;
1549 adev->hw_device.open_input_stream = adev_open_input_stream;
1550 adev->hw_device.close_input_stream = adev_close_input_stream;
1551 adev->hw_device.dump = adev_dump;
1552
1553 *device = &adev->hw_device.common;
1554
1555 return 0;
1556}
1557
1558static struct hw_module_methods_t hal_module_methods = {
1559 .open = adev_open,
1560};
1561
1562struct audio_module HAL_MODULE_INFO_SYM = {
1563 .common = {
1564 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001565 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1566 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001567 .id = AUDIO_HARDWARE_MODULE_ID,
1568 .name = "USB audio HW HAL",
1569 .author = "The Android Open Source Project",
1570 .methods = &hal_module_methods,
1571 },
1572};