blob: 4cb0071facf2fb49c7db0da3f6a5bd210ed34cee [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 McLean30f41852014-04-16 15:44:20 -070018/* #define LOG_NDEBUG 0 */
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
21#include <pthread.h>
22#include <stdint.h>
23#include <sys/time.h>
24#include <stdlib.h>
25
26#include <cutils/log.h>
27#include <cutils/str_parms.h>
28#include <cutils/properties.h>
29
30#include <hardware/hardware.h>
31#include <system/audio.h>
32#include <hardware/audio.h>
33
34#include <tinyalsa/asoundlib.h>
35
Paul McLeaneedc92e2013-12-19 15:46:15 -080036/* This is the default configuration to hand to The Framework on the initial
37 * adev_open_output_stream(). Actual device attributes will be used on the subsequent
38 * adev_open_output_stream() after the card and device number have been set in out_set_parameters()
39 */
40#define OUT_PERIOD_SIZE 1024
41#define OUT_PERIOD_COUNT 4
42#define OUT_SAMPLING_RATE 44100
43
44struct pcm_config default_alsa_out_config = {
Simon Wilson19957a32012-04-06 16:17:12 -070045 .channels = 2,
Paul McLeaneedc92e2013-12-19 15:46:15 -080046 .rate = OUT_SAMPLING_RATE,
47 .period_size = OUT_PERIOD_SIZE,
48 .period_count = OUT_PERIOD_COUNT,
Simon Wilson19957a32012-04-06 16:17:12 -070049 .format = PCM_FORMAT_S16_LE,
50};
51
Paul McLeaneedc92e2013-12-19 15:46:15 -080052/*
53 * Input defaults. See comment above.
54 */
55#define IN_PERIOD_SIZE 1024
56#define IN_PERIOD_COUNT 4
57#define IN_SAMPLING_RATE 44100
58
59struct pcm_config default_alsa_in_config = {
60 .channels = 2,
61 .rate = IN_SAMPLING_RATE,
62 .period_size = IN_PERIOD_SIZE,
63 .period_count = IN_PERIOD_COUNT,
64 .format = PCM_FORMAT_S16_LE,
65 .start_threshold = 1,
66 .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT),
67};
68
Simon Wilson19957a32012-04-06 16:17:12 -070069struct audio_device {
70 struct audio_hw_device hw_device;
71
72 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080073
74 /* output */
75 int out_card;
76 int out_device;
77
78 /* input */
79 int in_card;
80 int in_device;
81
Simon Wilson19957a32012-04-06 16:17:12 -070082 bool standby;
83};
84
85struct stream_out {
86 struct audio_stream_out stream;
87
Paul McLeaneedc92e2013-12-19 15:46:15 -080088 pthread_mutex_t lock; /* see note below on mutex acquisition order */
89 struct pcm *pcm; /* state of the stream */
90 bool standby;
91
92 struct audio_device *dev; /* hardware information */
93
94 void * conversion_buffer; /* any conversions are put into here
95 * they could come from here too if
96 * there was a previous conversion */
97 size_t conversion_buffer_size; /* in bytes */
98};
99
100/*
101 * Output Configuration Cache
Paul McLean30f41852014-04-16 15:44:20 -0700102 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
Paul McLeaneedc92e2013-12-19 15:46:15 -0800103 * but that will involve changes in The Framework.
104 */
105static struct pcm_config cached_output_hardware_config;
106static bool output_hardware_config_is_cached = false;
107
108struct stream_in {
109 struct audio_stream_in stream;
110
Simon Wilson19957a32012-04-06 16:17:12 -0700111 pthread_mutex_t lock; /* see note below on mutex acquisition order */
112 struct pcm *pcm;
113 bool standby;
114
115 struct audio_device *dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800116
117 struct audio_config hal_pcm_config;
118
Paul McLeaneedc92e2013-12-19 15:46:15 -0800119// struct resampler_itfe *resampler;
120// struct resampler_buffer_provider buf_provider;
Paul McLean30f41852014-04-16 15:44:20 -0700121
Paul McLeaneedc92e2013-12-19 15:46:15 -0800122 int read_status;
Paul McLean30f41852014-04-16 15:44:20 -0700123
124 // We may need to read more data from the device in order to data reduce to 16bit, 4chan */
125 void * conversion_buffer; /* any conversions are put into here
126 * they could come from here too if
127 * there was a previous conversion */
128 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700129};
130
Paul McLeaneedc92e2013-12-19 15:46:15 -0800131/*
Paul McLean30f41852014-04-16 15:44:20 -0700132 * Input Configuration Cache
133 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
134 * but that will involve changes in The Framework.
135 */
136static struct pcm_config cached_input_hardware_config;
137static bool input_hardware_config_is_cached = false;
138
139/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800140 * Utility
141 */
142/*
143 * Translates from ALSA format ID to ANDROID_AUDIO_CORE format ID
144 * (see master/system/core/include/core/audio.h)
145 * TODO(pmclean) Replace with audio_format_from_pcm_format() (in hardware/audio_alsaops.h).
146 * post-integration.
147 */
148static audio_format_t alsa_to_fw_format_id(int alsa_fmt_id)
149{
150 switch (alsa_fmt_id) {
151 case PCM_FORMAT_S8:
152 return AUDIO_FORMAT_PCM_8_BIT;
153
154 case PCM_FORMAT_S24_3LE:
155 //TODO(pmclean) make sure this is the 'right' sort of 24-bit
156 return AUDIO_FORMAT_PCM_8_24_BIT;
157
158 case PCM_FORMAT_S32_LE:
159 case PCM_FORMAT_S24_LE:
160 return AUDIO_FORMAT_PCM_32_BIT;
161 }
162
163 return AUDIO_FORMAT_PCM_16_BIT;
164}
165
166/*
167 * Data Conversions
168 */
169/*
170 * Convert a buffer of PCM16LE samples to packed (3-byte) PCM24LE samples.
171 * in_buff points to the buffer of PCM16 samples
172 * num_in_samples size of input buffer in SAMPLES
173 * out_buff points to the buffer to receive converted PCM24 LE samples.
Paul McLean30f41852014-04-16 15:44:20 -0700174 * returns
175 * the number of BYTES of output data.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800176 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
177 * support PCM24_3LE (24-bit, packed).
Paul McLean30f41852014-04-16 15:44:20 -0700178 * NOTE:
179 * We're just filling the low-order byte of the PCM24LE samples with 0.
180 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeaneedc92e2013-12-19 15:46:15 -0800181 * TODO(pmclean, hung) Move this to a utilities module.
182 */
Paul McLean30f41852014-04-16 15:44:20 -0700183static size_t convert_16_to_24_3(short * in_buff, size_t num_in_samples, unsigned char * out_buff) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800184 /*
185 * Move from back to front so that the conversion can be done in-place
186 * i.e. in_buff == out_buff
187 */
188 int in_buff_size_in_bytes = num_in_samples * 2;
189 /* we need 3 bytes in the output for every 2 bytes in the input */
190 int out_buff_size_in_bytes = ((3 * in_buff_size_in_bytes) / 2);
191 unsigned char* dst_ptr = out_buff + out_buff_size_in_bytes - 1;
Paul McLean30f41852014-04-16 15:44:20 -0700192 size_t src_smpl_index;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800193 unsigned char* src_ptr = ((unsigned char *)in_buff) + in_buff_size_in_bytes - 1;
194 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
195 *dst_ptr-- = *src_ptr--; /* hi-byte */
196 *dst_ptr-- = *src_ptr--; /* low-byte */
Paul McLean30f41852014-04-16 15:44:20 -0700197 /*TODO(pmclean) - we might want to consider dithering the lowest byte. */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800198 *dst_ptr-- = 0; /* zero-byte */
199 }
200
201 /* return number of *bytes* generated */
202 return out_buff_size_in_bytes;
203}
204
205/*
Paul McLean30f41852014-04-16 15:44:20 -0700206 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
207 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800208 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700209 * out_buff points to the buffer to receive converted PCM16LE LE samples.
210 * returns
211 * the number of BYTES of output data.
212 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
213 * support PCM24_3LE (24-bit, packed).
214 * NOTE:
215 * We're just filling the low-order byte of the PCM24LE samples with 0.
216 * This conversion is safe to do in-place (in_buff == out_buff).
217 * TODO(pmclean, hung) Move this to a utilities module.
218 */
219static size_t convert_24_3_to_16(unsigned char * in_buff, size_t num_in_samples, short * out_buff) {
220 /*
221 * Move from front to back so that the conversion can be done in-place
222 * i.e. in_buff == out_buff
223 */
224 /* we need 2 bytes in the output for every 3 bytes in the input */
225 unsigned char* dst_ptr = (unsigned char*)out_buff;
226 unsigned char* src_ptr = in_buff;
227 size_t src_smpl_index;
228 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
229 src_ptr++; /* lowest-(skip)-byte */
230 *dst_ptr++ = *src_ptr++; /* low-byte */
231 *dst_ptr++ = *src_ptr++; /* high-byte */
232 }
233
234 /* return number of *bytes* generated: */
235 return num_in_samples * 2;
236}
237
238/*
239 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
240 * (where N < M).
241 * in_buff points to the buffer of PCM16 samples
242 * in_buff_channels Specifies the number of channels in the input buffer.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800243 * out_buff points to the buffer to receive converted PCM16 samples.
Paul McLean30f41852014-04-16 15:44:20 -0700244 * out_buff_channels Specifies the number of channels in the output buffer.
245 * num_in_samples size of input buffer in SAMPLES
246 * returns
247 * the number of BYTES of output data.
248 * NOTE
249 * channels > N are filled with silence.
250 * This conversion is safe to do in-place (in_buff == out_buff)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800251 * We are doing this since we *always* present to The Framework as STEREO device, but need to
252 * support 4-channel devices.
253 * TODO(pmclean, hung) Move this to a utilities module.
254 */
Paul McLean30f41852014-04-16 15:44:20 -0700255static size_t expand_channels_16(short* in_buff, int in_buff_chans,
256 short* out_buff, int out_buff_chans,
257 size_t num_in_samples) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800258 /*
259 * Move from back to front so that the conversion can be done in-place
260 * i.e. in_buff == out_buff
Paul McLean30f41852014-04-16 15:44:20 -0700261 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
Paul McLeaneedc92e2013-12-19 15:46:15 -0800262 */
Paul McLean30f41852014-04-16 15:44:20 -0700263 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
264
265 short* dst_ptr = out_buff + num_out_samples - 1;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800266 int src_index;
Paul McLean30f41852014-04-16 15:44:20 -0700267 short* src_ptr = in_buff + num_in_samples - 1;
268 int num_zero_chans = out_buff_chans - in_buff_chans;
269 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
270 int dst_offset;
271 for(dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) {
272 *dst_ptr-- = 0;
273 }
274 for(; dst_offset < out_buff_chans; dst_offset++) {
275 *dst_ptr-- = *src_ptr--;
276 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800277 }
278
279 /* return number of *bytes* generated */
Paul McLean30f41852014-04-16 15:44:20 -0700280 return num_out_samples * sizeof(short);
281}
282
283/*
284 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
285 * (where N > M).
286 * in_buff points to the buffer of PCM16 samples
287 * in_buff_channels Specifies the number of channels in the input buffer.
288 * out_buff points to the buffer to receive converted PCM16 samples.
289 * out_buff_channels Specifies the number of channels in the output buffer.
290 * num_in_samples size of input buffer in SAMPLES
291 * returns
292 * the number of BYTES of output data.
293 * NOTE
294 * channels > N are thrown away.
295 * This conversion is safe to do in-place (in_buff == out_buff)
296 * We are doing this since we *always* present to The Framework as STEREO device, but need to
297 * support 4-channel devices.
298 * TODO(pmclean, hung) Move this to a utilities module.
299 */
300static size_t contract_channels_16(short* in_buff, int in_buff_chans,
301 short* out_buff, int out_buff_chans,
302 size_t num_in_samples) {
303 /*
304 * Move from front to back so that the conversion can be done in-place
305 * i.e. in_buff == out_buff
306 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
307 */
308 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
309
310 int num_skip_samples = in_buff_chans - out_buff_chans;
311
312 short* dst_ptr = out_buff;
313 short* src_ptr = in_buff;
314 int src_index;
315 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
316 int dst_offset;
317 for(dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) {
318 *dst_ptr++ = *src_ptr++;
319 }
320 src_ptr += num_skip_samples;
321 }
322
323 /* return number of *bytes* generated */
324 return num_out_samples * sizeof(short);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800325}
326
327/*
328 * ALSA Utilities
329 */
330/*
331 * gets the ALSA bit-format flag from a bits-per-sample value.
332 * TODO(pmclean, hung) Move this to a utilities module.
333 */
334static int bits_to_alsa_format(int bits_per_sample, int default_format)
335{
336 enum pcm_format format;
337 for (format = PCM_FORMAT_S16_LE; format < PCM_FORMAT_MAX; format++) {
338 if (pcm_format_to_bits(format) == bits_per_sample) {
Paul McLean30f41852014-04-16 15:44:20 -0700339 return format;
340 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800341 }
342 return default_format;
343}
344
345/*
346 * Reads and decodes configuration info from the specified ALSA card/device
347 */
348static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config)
349{
Paul McLean30f41852014-04-16 15:44:20 -0700350 ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)",card, device, io_type);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800351
352 if (card < 0 || device < 0) {
353 return -EINVAL;
354 }
355
356 struct pcm_params * alsa_hw_params = pcm_params_get(card, device, io_type);
357 if (alsa_hw_params == NULL) {
358 return -EINVAL;
359 }
360
361 /*
362 * This Logging will be useful when testing new USB devices.
363 */
364 /* ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS), pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS)); */
365 /* ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS), pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS)); */
366 /* ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS), pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS)); */
367 /* ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE), pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE)); */
368 /* ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME), pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME)); */
369 /* ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE), pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE)); */
370 /* ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES), pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES)); */
371 /* ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS), pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS)); */
372 /* ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME), pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME)); */
373 /* ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE), pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE)); */
374 /* ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES), pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES)); */
375 /* ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%d, max:%d", pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME), pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME)); */
376
377 config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
378 config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
379 config->period_size = pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS);
380 config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
381
382 int bits_per_sample = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
383 config->format = bits_to_alsa_format(bits_per_sample, PCM_FORMAT_S16_LE);
384
385 return 0;
386}
387
388/*
389 * HAl Functions
390 */
Simon Wilson19957a32012-04-06 16:17:12 -0700391/**
392 * NOTE: when multiple mutexes have to be acquired, always respect the
393 * following order: hw device > out stream
394 */
395
396/* Helper functions */
Simon Wilson19957a32012-04-06 16:17:12 -0700397static uint32_t out_get_sample_rate(const struct audio_stream *stream)
398{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800399 return cached_output_hardware_config.rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700400}
401
402static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
403{
404 return 0;
405}
406
407static size_t out_get_buffer_size(const struct audio_stream *stream)
408{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800409 return cached_output_hardware_config.period_size * audio_stream_frame_size(stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700410}
411
412static uint32_t out_get_channels(const struct audio_stream *stream)
413{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800414 // Always Stero for now. We will do *some* conversions in this HAL.
415 // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary channels
416 // rewrite this to return the ACTUAL channel format
Simon Wilson19957a32012-04-06 16:17:12 -0700417 return AUDIO_CHANNEL_OUT_STEREO;
418}
419
420static audio_format_t out_get_format(const struct audio_stream *stream)
421{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800422 // Always return 16-bit PCM. We will do *some* conversions in this HAL.
423 // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary PCM formats
424 // rewrite this to return the ACTUAL data format
Simon Wilson19957a32012-04-06 16:17:12 -0700425 return AUDIO_FORMAT_PCM_16_BIT;
426}
427
428static int out_set_format(struct audio_stream *stream, audio_format_t format)
429{
430 return 0;
431}
432
433static int out_standby(struct audio_stream *stream)
434{
435 struct stream_out *out = (struct stream_out *)stream;
436
437 pthread_mutex_lock(&out->dev->lock);
438 pthread_mutex_lock(&out->lock);
439
440 if (!out->standby) {
441 pcm_close(out->pcm);
442 out->pcm = NULL;
443 out->standby = true;
444 }
445
446 pthread_mutex_unlock(&out->lock);
447 pthread_mutex_unlock(&out->dev->lock);
448
449 return 0;
450}
451
452static int out_dump(const struct audio_stream *stream, int fd)
453{
454 return 0;
455}
456
457static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
458{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800459 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
460
Simon Wilson19957a32012-04-06 16:17:12 -0700461 struct stream_out *out = (struct stream_out *)stream;
462 struct audio_device *adev = out->dev;
463 struct str_parms *parms;
464 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800465 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700466 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800467 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700468
469 parms = str_parms_create_str(kvpairs);
470 pthread_mutex_lock(&adev->lock);
471
Paul McLeaneedc92e2013-12-19 15:46:15 -0800472 bool recache_device_params = false;
473 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
474 if (param_val >= 0) {
475 adev->out_card = atoi(value);
476 recache_device_params = true;
477 }
Simon Wilson19957a32012-04-06 16:17:12 -0700478
Paul McLeaneedc92e2013-12-19 15:46:15 -0800479 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
480 if (param_val >= 0) {
481 adev->out_device = atoi(value);
482 recache_device_params = true;
483 }
484
485 if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) {
486 ret_value = read_alsa_device_config(adev->out_card, adev->out_device, PCM_OUT,
Paul McLean30f41852014-04-16 15:44:20 -0700487 &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800488 output_hardware_config_is_cached = (ret_value == 0);
489 }
Simon Wilson19957a32012-04-06 16:17:12 -0700490
491 pthread_mutex_unlock(&adev->lock);
492 str_parms_destroy(parms);
493
Paul McLeaneedc92e2013-12-19 15:46:15 -0800494 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700495}
496
Paul McLeaneedc92e2013-12-19 15:46:15 -0800497//TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters()
498// could be written in terms of a get_device_parameters(io_type)
499
Paul McLean30f41852014-04-16 15:44:20 -0700500static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
501{
502 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
503
Paul McLeaneedc92e2013-12-19 15:46:15 -0800504 struct stream_out *out = (struct stream_out *) stream;
505 struct audio_device *adev = out->dev;
506
Paul McLean30f41852014-04-16 15:44:20 -0700507 if (adev->out_card < 0 || adev->out_device < 0)
508 return strdup("");
509
Paul McLeaneedc92e2013-12-19 15:46:15 -0800510 unsigned min, max;
511
512 struct str_parms *query = str_parms_create_str(keys);
513 struct str_parms *result = str_parms_create();
514
515 int num_written = 0;
516 char buffer[256];
517 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
518 char* result_str = NULL;
519
520 struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT);
521
522 // These keys are from hardware/libhardware/include/audio.h
523 // supported sample rates
524 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
525 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
526 // if they are different, return a list containing those two values, otherwise just the one.
527 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
528 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
529 num_written = snprintf(buffer, buffer_size, "%d", min);
530 if (min != max) {
Paul McLean30f41852014-04-16 15:44:20 -0700531 snprintf(buffer + num_written, buffer_size - num_written, "|%d", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800532 }
533 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
534 buffer);
535 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
536
537 // supported channel counts
538 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
539 // Similarly for output channels count
540 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
541 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
542 num_written = snprintf(buffer, buffer_size, "%d", min);
543 if (min != max) {
544 snprintf(buffer + num_written, buffer_size - num_written, "|%d", max);
545 }
546 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer);
547 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
548
549 // supported sample formats
550 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
551 // Similarly for output channels count
552 //TODO(pmclean): this is wrong.
553 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
554 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
555 num_written = snprintf(buffer, buffer_size, "%d", min);
556 if (min != max) {
557 snprintf(buffer + num_written, buffer_size - num_written, "|%d", max);
558 }
559 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
560 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
561
562 result_str = str_parms_to_str(result);
563
564 // done with these...
565 str_parms_destroy(query);
566 str_parms_destroy(result);
567
568 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700569}
570
571static uint32_t out_get_latency(const struct audio_stream_out *stream)
572{
Paul McLean30f41852014-04-16 15:44:20 -0700573 struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800574
575 //TODO(pmclean): Do we need a term here for the USB latency
576 // (as reported in the USB descriptors)?
Paul McLean30f41852014-04-16 15:44:20 -0700577 uint32_t latency = (cached_output_hardware_config.period_size
578 * cached_output_hardware_config.period_count * 1000) / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800579 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700580}
581
Paul McLean30f41852014-04-16 15:44:20 -0700582static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700583{
584 return -ENOSYS;
585}
586
Paul McLeaneedc92e2013-12-19 15:46:15 -0800587/* must be called with hw device and output stream mutexes locked */
588static int start_output_stream(struct stream_out *out)
589{
590 struct audio_device *adev = out->dev;
591 int return_val = 0;
592
Paul McLean30f41852014-04-16 15:44:20 -0700593 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
594 adev->out_card, adev->out_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800595
596 out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config);
597 if (out->pcm == NULL) {
598 return -ENOMEM;
599 }
600
601 if (out->pcm && !pcm_is_ready(out->pcm)) {
602 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
603 pcm_close(out->pcm);
604 return -ENOMEM;
605 }
606
Paul McLeaneedc92e2013-12-19 15:46:15 -0800607 return 0;
608}
609
610static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700611{
612 int ret;
613 struct stream_out *out = (struct stream_out *)stream;
614
615 pthread_mutex_lock(&out->dev->lock);
616 pthread_mutex_lock(&out->lock);
617 if (out->standby) {
618 ret = start_output_stream(out);
619 if (ret != 0) {
620 goto err;
621 }
622 out->standby = false;
623 }
624
Paul McLean30f41852014-04-16 15:44:20 -0700625 // Setup conversion buffer
626 // compute maximum potential buffer size.
627 // * 2 for stereo -> quad conversion
628 // * 3/2 for 16bit -> 24 bit conversion
629 int required_conversion_buffer_size = (bytes * 3 * 2) / 2;
630 if (required_conversion_buffer_size > out->conversion_buffer_size) {
631 //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
632 // (and do these conversions themselves)
633 out->conversion_buffer_size = required_conversion_buffer_size;
634 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
635 }
636
Paul McLeaneedc92e2013-12-19 15:46:15 -0800637 void * write_buff = buffer;
638 int num_write_buff_bytes = bytes;
639
640 /*
641 * Num Channels conversion
642 */
643 int num_device_channels = cached_output_hardware_config.channels;
644 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700645 if (num_device_channels != num_req_channels) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800646 num_write_buff_bytes =
Paul McLean30f41852014-04-16 15:44:20 -0700647 expand_channels_16(write_buff, num_req_channels,
648 out->conversion_buffer, num_device_channels,
649 num_write_buff_bytes / sizeof(short));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800650 write_buff = out->conversion_buffer;
651 }
652
653 /*
654 * 16 vs 24-bit logic here
655 */
656 switch (cached_output_hardware_config.format) {
657 case PCM_FORMAT_S16_LE:
658 // the output format is the same as the input format, so just write it out
659 break;
660
661 case PCM_FORMAT_S24_3LE:
662 // 16-bit LE2 - 24-bit LE3
Paul McLean30f41852014-04-16 15:44:20 -0700663 num_write_buff_bytes = convert_16_to_24_3(write_buff,
664 num_write_buff_bytes / sizeof(short),
665 out->conversion_buffer);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800666 write_buff = out->conversion_buffer;
667 break;
668
669 default:
670 // hmmmmm.....
671 ALOGV("usb:Unknown Format!!!");
672 break;
673 }
674
675 if (write_buff != NULL && num_write_buff_bytes != 0) {
676 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
677 }
Simon Wilson19957a32012-04-06 16:17:12 -0700678
679 pthread_mutex_unlock(&out->lock);
680 pthread_mutex_unlock(&out->dev->lock);
681
682 return bytes;
683
684err:
685 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800686 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700687 if (ret != 0) {
688 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
689 out_get_sample_rate(&stream->common));
690 }
691
692 return bytes;
693}
694
Paul McLean30f41852014-04-16 15:44:20 -0700695static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700696{
697 return -EINVAL;
698}
699
700static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
701{
702 return 0;
703}
704
705static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
706{
707 return 0;
708}
709
Paul McLean30f41852014-04-16 15:44:20 -0700710static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700711{
712 return -EINVAL;
713}
714
715static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700716 audio_io_handle_t handle,
717 audio_devices_t devices,
718 audio_output_flags_t flags,
719 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -0700720 struct audio_stream_out **stream_out)
721{
Paul McLean30f41852014-04-16 15:44:20 -0700722 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 -0800723 handle, devices, flags);
724
Simon Wilson19957a32012-04-06 16:17:12 -0700725 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800726
Simon Wilson19957a32012-04-06 16:17:12 -0700727 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700728
729 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
730 if (!out)
731 return -ENOMEM;
732
Paul McLeaneedc92e2013-12-19 15:46:15 -0800733 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -0700734 out->stream.common.get_sample_rate = out_get_sample_rate;
735 out->stream.common.set_sample_rate = out_set_sample_rate;
736 out->stream.common.get_buffer_size = out_get_buffer_size;
737 out->stream.common.get_channels = out_get_channels;
738 out->stream.common.get_format = out_get_format;
739 out->stream.common.set_format = out_set_format;
740 out->stream.common.standby = out_standby;
741 out->stream.common.dump = out_dump;
742 out->stream.common.set_parameters = out_set_parameters;
743 out->stream.common.get_parameters = out_get_parameters;
744 out->stream.common.add_audio_effect = out_add_audio_effect;
745 out->stream.common.remove_audio_effect = out_remove_audio_effect;
746 out->stream.get_latency = out_get_latency;
747 out->stream.set_volume = out_set_volume;
748 out->stream.write = out_write;
749 out->stream.get_render_position = out_get_render_position;
750 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
751
752 out->dev = adev;
753
Paul McLeaneedc92e2013-12-19 15:46:15 -0800754 if (output_hardware_config_is_cached) {
755 config->sample_rate = cached_output_hardware_config.rate;
756
757 config->format = alsa_to_fw_format_id(cached_output_hardware_config.format);
758 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
759 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
760 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
761 //TODO(pmclean) remove this when the above restriction is removed.
762 config->format = AUDIO_FORMAT_PCM_16_BIT;
763 }
764
765 config->channel_mask =
766 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
767 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
768 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
769 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
770 //TODO(pmclean) remove this when the above restriction is removed.
771 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
772 }
773 } else {
774 cached_output_hardware_config = default_alsa_out_config;
775
776 config->format = out_get_format(&out->stream.common);
777 config->channel_mask = out_get_channels(&out->stream.common);
778 config->sample_rate = out_get_sample_rate(&out->stream.common);
779 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800780
781 out->conversion_buffer = NULL;
782 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700783
784 out->standby = true;
785
786 *stream_out = &out->stream;
787 return 0;
788
789err_open:
790 free(out);
791 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800792 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700793}
794
795static void adev_close_output_stream(struct audio_hw_device *dev,
796 struct audio_stream_out *stream)
797{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800798 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -0700799 struct stream_out *out = (struct stream_out *)stream;
800
Paul McLeaneedc92e2013-12-19 15:46:15 -0800801 //TODO(pmclean) why are we doing this when stream get's freed at the end
802 // because it closes the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -0700803 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800804
805 free(out->conversion_buffer);
806 out->conversion_buffer = NULL;
807 out->conversion_buffer_size = 0;
808
Simon Wilson19957a32012-04-06 16:17:12 -0700809 free(stream);
810}
811
812static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
813{
814 return 0;
815}
816
Paul McLean30f41852014-04-16 15:44:20 -0700817static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -0700818{
819 return strdup("");
820}
821
822static int adev_init_check(const struct audio_hw_device *dev)
823{
824 return 0;
825}
826
827static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
828{
829 return -ENOSYS;
830}
831
832static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
833{
834 return -ENOSYS;
835}
836
837static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
838{
839 return 0;
840}
841
842static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
843{
844 return -ENOSYS;
845}
846
847static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
848{
849 return -ENOSYS;
850}
851
852static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700853 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -0700854{
855 return 0;
856}
857
Paul McLeaneedc92e2013-12-19 15:46:15 -0800858/* Helper functions */
859static uint32_t in_get_sample_rate(const struct audio_stream *stream)
860{
Paul McLean30f41852014-04-16 15:44:20 -0700861 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800862}
863
864static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
865{
866 return -ENOSYS;
867}
868
869static size_t in_get_buffer_size(const struct audio_stream *stream)
870{
Paul McLean30f41852014-04-16 15:44:20 -0700871 ALOGV("usb: in_get_buffer_size() = %d",
872 cached_input_hardware_config.period_size * audio_stream_frame_size(stream));
873 return cached_input_hardware_config.period_size * audio_stream_frame_size(stream);
874
Paul McLeaneedc92e2013-12-19 15:46:15 -0800875}
876
877static uint32_t in_get_channels(const struct audio_stream *stream)
878{
Paul McLean30f41852014-04-16 15:44:20 -0700879 // just report stereo for now
880 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800881}
882
883static audio_format_t in_get_format(const struct audio_stream *stream)
884{
885 // just report 16-bit, pcm for now.
886 return AUDIO_FORMAT_PCM_16_BIT;
887}
888
889static int in_set_format(struct audio_stream *stream, audio_format_t format)
890{
891 return -ENOSYS;
892}
893
894static int in_standby(struct audio_stream *stream)
895{
Paul McLean30f41852014-04-16 15:44:20 -0700896 struct stream_in *in = (struct stream_in *) stream;
897
898 pthread_mutex_lock(&in->dev->lock);
899 pthread_mutex_lock(&in->lock);
900
901 if (!in->standby) {
902 pcm_close(in->pcm);
903 in->pcm = NULL;
904 in->standby = true;
905 }
906
907 pthread_mutex_unlock(&in->lock);
908 pthread_mutex_unlock(&in->dev->lock);
909
Paul McLeaneedc92e2013-12-19 15:46:15 -0800910 return 0;
911}
912
913static int in_dump(const struct audio_stream *stream, int fd)
914{
915 return 0;
916}
917
918static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
919{
Paul McLean30f41852014-04-16 15:44:20 -0700920 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800921
922 struct stream_in *in = (struct stream_in *)stream;
923 struct audio_device *adev = in->dev;
924 struct str_parms *parms;
925 char value[32];
926 int param_val;
927 int routing = 0;
928 int ret_value = 0;
929
930 parms = str_parms_create_str(kvpairs);
931 pthread_mutex_lock(&adev->lock);
932
Paul McLean30f41852014-04-16 15:44:20 -0700933 bool recache_device_params = false;
934
Paul McLeaneedc92e2013-12-19 15:46:15 -0800935 // Card/Device
936 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
937 if (param_val >= 0) {
938 adev->in_card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -0700939 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800940 }
941
942 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
943 if (param_val >= 0) {
944 adev->in_device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -0700945 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800946 }
947
Paul McLean30f41852014-04-16 15:44:20 -0700948 if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) {
949 ret_value = read_alsa_device_config(adev->in_card, adev->in_device,
950 PCM_IN, &(cached_input_hardware_config));
951 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800952 }
953
954 pthread_mutex_unlock(&adev->lock);
955 str_parms_destroy(parms);
956
957 return ret_value;
958}
959
960//TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters()
961// could be written in terms of a get_device_parameters(io_type)
962
Paul McLean30f41852014-04-16 15:44:20 -0700963static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
964 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800965
Paul McLean30f41852014-04-16 15:44:20 -0700966 struct stream_in *in = (struct stream_in *)stream;
967 struct audio_device *adev = in->dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800968
Paul McLean30f41852014-04-16 15:44:20 -0700969 if (adev->in_card < 0 || adev->in_device < 0)
970 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800971
Paul McLean30f41852014-04-16 15:44:20 -0700972 struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN);
973 if (alsa_hw_params == NULL)
974 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800975
Paul McLean30f41852014-04-16 15:44:20 -0700976 struct str_parms *query = str_parms_create_str(keys);
977 struct str_parms *result = str_parms_create();
Paul McLeaneedc92e2013-12-19 15:46:15 -0800978
Paul McLean30f41852014-04-16 15:44:20 -0700979 int num_written = 0;
980 char buffer[256];
981 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
982 char* result_str = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800983
Paul McLean30f41852014-04-16 15:44:20 -0700984 unsigned min, max;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800985
Paul McLean30f41852014-04-16 15:44:20 -0700986 // These keys are from hardware/libhardware/include/audio.h
987 // supported sample rates
988 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
989 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
990 // if they are different, return a list containing those two values, otherwise just the one.
991 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
992 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
993 num_written = snprintf(buffer, buffer_size, "%d", min);
994 if (min != max) {
995 snprintf(buffer + num_written, buffer_size - num_written, "|%d", max);
996 }
997 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer);
998 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
Paul McLeaneedc92e2013-12-19 15:46:15 -0800999
Paul McLean30f41852014-04-16 15:44:20 -07001000 // supported channel counts
1001 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
1002 // Similarly for output channels count
1003 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
1004 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
1005 num_written = snprintf(buffer, buffer_size, "%d", min);
1006 if (min != max) {
1007 snprintf(buffer + num_written, buffer_size - num_written, "|%d", max);
1008 }
1009 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer);
1010 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001011
Paul McLean30f41852014-04-16 15:44:20 -07001012 // supported sample formats
1013 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
1014 //TODO(pmclean): this is wrong.
1015 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
1016 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
1017 num_written = snprintf(buffer, buffer_size, "%d", min);
1018 if (min != max) {
1019 snprintf(buffer + num_written, buffer_size - num_written, "|%d", max);
1020 }
1021 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
1022 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001023
Paul McLean30f41852014-04-16 15:44:20 -07001024 result_str = str_parms_to_str(result);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001025
Paul McLean30f41852014-04-16 15:44:20 -07001026 // done with these...
1027 str_parms_destroy(query);
1028 str_parms_destroy(result);
1029
1030 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001031}
1032
1033static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1034{
1035 return 0;
1036}
1037
1038static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1039{
1040 return 0;
1041}
1042
Paul McLean30f41852014-04-16 15:44:20 -07001043static int in_set_gain(struct audio_stream_in *stream, float gain)
1044{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001045 return 0;
1046}
1047
Paul McLean30f41852014-04-16 15:44:20 -07001048/* must be called with hw device and output stream mutexes locked */
1049static int start_input_stream(struct stream_in *in) {
1050 struct audio_device *adev = in->dev;
1051 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001052
Paul McLean30f41852014-04-16 15:44:20 -07001053 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
1054 adev->in_card, adev->in_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001055
Paul McLean30f41852014-04-16 15:44:20 -07001056 in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config);
1057 if (in->pcm == NULL) {
1058 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1059 return -ENOMEM;
1060 }
1061
1062 if (in->pcm && !pcm_is_ready(in->pcm)) {
1063 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1064 pcm_close(in->pcm);
1065 return -ENOMEM;
1066 }
1067
1068 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001069}
1070
Paul McLean30f41852014-04-16 15:44:20 -07001071//TODO(pmclean) mutex stuff here (see out_write)
1072static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1073{
1074 int num_read_buff_bytes = 0;
1075 void * read_buff = buffer;
1076 void * out_buff = buffer;
1077
1078 struct stream_in * in = (struct stream_in *) stream;
1079
1080 pthread_mutex_lock(&in->dev->lock);
1081 pthread_mutex_lock(&in->lock);
1082
1083 if (in->standby) {
1084 if (start_input_stream(in) != 0) {
1085 goto err;
1086 }
1087 in->standby = false;
1088 }
1089
1090 // OK, we need to figure out how much data to read to be able to output the requested
1091 // number of bytes in the HAL format (16-bit, stereo).
1092 num_read_buff_bytes = bytes;
1093 int num_device_channels = cached_input_hardware_config.channels;
1094 int num_req_channels = 2; /* always, for now */
1095
1096 if (num_device_channels != num_req_channels) {
1097 num_read_buff_bytes *= num_device_channels/num_req_channels;
1098 }
1099
1100 if (cached_output_hardware_config.format == PCM_FORMAT_S24_3LE) {
1101 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
1102 }
1103
1104 // Setup/Realloc the conversion buffer (if necessary).
1105 if (num_read_buff_bytes != bytes) {
1106 if (num_read_buff_bytes > in->conversion_buffer_size) {
1107 //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1108 // (and do these conversions themselves)
1109 in->conversion_buffer_size = num_read_buff_bytes;
1110 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1111 }
1112 read_buff = in->conversion_buffer;
1113 }
1114
1115 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1116 /*
1117 * Do any conversions necessary to send the data in the format specified to/by the HAL
1118 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1119 */
1120 if (cached_output_hardware_config.format == PCM_FORMAT_S24_3LE) {
1121 if (num_device_channels != num_req_channels) {
1122 out_buff = read_buff;
1123 }
1124
1125 /* Bit Format Conversion */
1126 num_read_buff_bytes =
1127 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1128 }
1129
1130 if (num_device_channels != num_req_channels) {
1131 out_buff = buffer;
1132 /* Num Channels conversion */
1133 num_read_buff_bytes =
1134 contract_channels_16(read_buff, num_device_channels,
1135 out_buff, num_req_channels,
1136 num_read_buff_bytes / sizeof(short));
1137 }
1138 }
1139
1140err:
1141 pthread_mutex_unlock(&in->lock);
1142 pthread_mutex_unlock(&in->dev->lock);
1143
1144 return num_read_buff_bytes;
1145}
1146
1147static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1148{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001149 return 0;
1150}
1151
Mike Lockwood46a98092012-04-24 16:41:18 -07001152static int adev_open_input_stream(struct audio_hw_device *dev,
1153 audio_io_handle_t handle,
1154 audio_devices_t devices,
Paul McLean30f41852014-04-16 15:44:20 -07001155 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001156 struct audio_stream_in **stream_in)
1157{
Paul McLean30f41852014-04-16 15:44:20 -07001158 ALOGV("usb: in adev_open_input_stream() rate:%d, chanMask:0x%X, fmt:%d",
1159 config->sample_rate, config->channel_mask, config->format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001160
1161 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1162 if (in == NULL)
1163 return -ENOMEM;
1164
1165 // setup function pointers
1166 in->stream.common.get_sample_rate = in_get_sample_rate;
1167 in->stream.common.set_sample_rate = in_set_sample_rate;
1168 in->stream.common.get_buffer_size = in_get_buffer_size;
1169 in->stream.common.get_channels = in_get_channels;
1170 in->stream.common.get_format = in_get_format;
1171 in->stream.common.set_format = in_set_format;
1172 in->stream.common.standby = in_standby;
1173 in->stream.common.dump = in_dump;
1174 in->stream.common.set_parameters = in_set_parameters;
1175 in->stream.common.get_parameters = in_get_parameters;
1176 in->stream.common.add_audio_effect = in_add_audio_effect;
1177 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1178
1179 in->stream.set_gain = in_set_gain;
1180 in->stream.read = in_read;
1181 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1182
Paul McLean30f41852014-04-16 15:44:20 -07001183 in->dev = (struct audio_device *)dev;
1184
1185 if (output_hardware_config_is_cached) {
1186 config->sample_rate = cached_output_hardware_config.rate;
1187
1188 config->format = alsa_to_fw_format_id(cached_output_hardware_config.format);
1189 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
1190 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1191 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
1192 //TODO(pmclean) remove this when the above restriction is removed.
1193 config->format = AUDIO_FORMAT_PCM_16_BIT;
1194 }
1195
1196 config->channel_mask = audio_channel_out_mask_from_count(
1197 cached_output_hardware_config.channels);
1198 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
1199 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1200 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
1201 //TODO(pmclean) remove this when the above restriction is removed.
1202 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1203 }
1204 } else {
1205 cached_input_hardware_config = default_alsa_in_config;
1206
1207 config->format = out_get_format(&in->stream.common);
1208 config->channel_mask = out_get_channels(&in->stream.common);
1209 config->sample_rate = out_get_sample_rate(&in->stream.common);
1210 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001211
1212 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001213
Paul McLean30f41852014-04-16 15:44:20 -07001214 in->conversion_buffer = NULL;
1215 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001216
1217 *stream_in = &in->stream;
1218
1219 return 0;
Simon Wilson19957a32012-04-06 16:17:12 -07001220}
1221
Paul McLean30f41852014-04-16 15:44:20 -07001222static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001223{
Paul McLean30f41852014-04-16 15:44:20 -07001224 struct stream_in *in = (struct stream_in *)stream;
1225
1226 //TODO(pmclean) why are we doing this when stream get's freed at the end
1227 // because it closes the pcm device
1228 in_standby(&stream->common);
1229
1230 free(in->conversion_buffer);
1231
1232 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001233}
1234
1235static int adev_dump(const audio_hw_device_t *device, int fd)
1236{
1237 return 0;
1238}
1239
1240static int adev_close(hw_device_t *device)
1241{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001242 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001243 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001244
1245 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001246 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001247
Simon Wilson19957a32012-04-06 16:17:12 -07001248 return 0;
1249}
1250
Paul McLean30f41852014-04-16 15:44:20 -07001251static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001252{
Simon Wilson19957a32012-04-06 16:17:12 -07001253 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1254 return -EINVAL;
1255
Paul McLeaneedc92e2013-12-19 15:46:15 -08001256 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001257 if (!adev)
1258 return -ENOMEM;
1259
1260 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001261 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001262 adev->hw_device.common.module = (struct hw_module_t *) module;
1263 adev->hw_device.common.close = adev_close;
1264
Simon Wilson19957a32012-04-06 16:17:12 -07001265 adev->hw_device.init_check = adev_init_check;
1266 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1267 adev->hw_device.set_master_volume = adev_set_master_volume;
1268 adev->hw_device.set_mode = adev_set_mode;
1269 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1270 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1271 adev->hw_device.set_parameters = adev_set_parameters;
1272 adev->hw_device.get_parameters = adev_get_parameters;
1273 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1274 adev->hw_device.open_output_stream = adev_open_output_stream;
1275 adev->hw_device.close_output_stream = adev_close_output_stream;
1276 adev->hw_device.open_input_stream = adev_open_input_stream;
1277 adev->hw_device.close_input_stream = adev_close_input_stream;
1278 adev->hw_device.dump = adev_dump;
1279
1280 *device = &adev->hw_device.common;
1281
1282 return 0;
1283}
1284
1285static struct hw_module_methods_t hal_module_methods = {
1286 .open = adev_open,
1287};
1288
1289struct audio_module HAL_MODULE_INFO_SYM = {
1290 .common = {
1291 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001292 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1293 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001294 .id = AUDIO_HARDWARE_MODULE_ID,
1295 .name = "USB audio HW HAL",
1296 .author = "The Android Open Source Project",
1297 .methods = &hal_module_methods,
1298 },
1299};