blob: 4b50d584150582358625a4192c242eec865540be [file] [log] [blame]
Simon Wilson19957a32012-04-06 16:17:12 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "usb_audio_hw"
Paul McLeancf611912014-04-28 13:03:18 -070018/*#define LOG_NDEBUG 0*/
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070021#include <inttypes.h>
Simon Wilson19957a32012-04-06 16:17:12 -070022#include <pthread.h>
23#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070024#include <stdlib.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070025#include <sys/time.h>
Simon Wilson19957a32012-04-06 16:17:12 -070026
Mark Salyzyn88e458a2014-04-28 12:30:44 -070027#include <log/log.h>
Simon Wilson19957a32012-04-06 16:17:12 -070028#include <cutils/str_parms.h>
29#include <cutils/properties.h>
30
31#include <hardware/hardware.h>
32#include <system/audio.h>
33#include <hardware/audio.h>
34
35#include <tinyalsa/asoundlib.h>
36
Paul McLeaneedc92e2013-12-19 15:46:15 -080037/* This is the default configuration to hand to The Framework on the initial
38 * adev_open_output_stream(). Actual device attributes will be used on the subsequent
39 * adev_open_output_stream() after the card and device number have been set in out_set_parameters()
40 */
41#define OUT_PERIOD_SIZE 1024
42#define OUT_PERIOD_COUNT 4
43#define OUT_SAMPLING_RATE 44100
44
45struct pcm_config default_alsa_out_config = {
Simon Wilson19957a32012-04-06 16:17:12 -070046 .channels = 2,
Paul McLeaneedc92e2013-12-19 15:46:15 -080047 .rate = OUT_SAMPLING_RATE,
48 .period_size = OUT_PERIOD_SIZE,
49 .period_count = OUT_PERIOD_COUNT,
Simon Wilson19957a32012-04-06 16:17:12 -070050 .format = PCM_FORMAT_S16_LE,
51};
52
Paul McLeaneedc92e2013-12-19 15:46:15 -080053/*
54 * Input defaults. See comment above.
55 */
56#define IN_PERIOD_SIZE 1024
57#define IN_PERIOD_COUNT 4
58#define IN_SAMPLING_RATE 44100
59
60struct pcm_config default_alsa_in_config = {
61 .channels = 2,
62 .rate = IN_SAMPLING_RATE,
63 .period_size = IN_PERIOD_SIZE,
64 .period_count = IN_PERIOD_COUNT,
65 .format = PCM_FORMAT_S16_LE,
66 .start_threshold = 1,
67 .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT),
68};
69
Simon Wilson19957a32012-04-06 16:17:12 -070070struct audio_device {
71 struct audio_hw_device hw_device;
72
73 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080074
75 /* output */
76 int out_card;
77 int out_device;
78
79 /* input */
80 int in_card;
81 int in_device;
82
Simon Wilson19957a32012-04-06 16:17:12 -070083 bool standby;
84};
85
86struct stream_out {
87 struct audio_stream_out stream;
88
Paul McLeaneedc92e2013-12-19 15:46:15 -080089 pthread_mutex_t lock; /* see note below on mutex acquisition order */
90 struct pcm *pcm; /* state of the stream */
91 bool standby;
92
93 struct audio_device *dev; /* hardware information */
94
95 void * conversion_buffer; /* any conversions are put into here
96 * they could come from here too if
97 * there was a previous conversion */
98 size_t conversion_buffer_size; /* in bytes */
99};
100
101/*
102 * Output Configuration Cache
Paul McLean30f41852014-04-16 15:44:20 -0700103 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
Paul McLeaneedc92e2013-12-19 15:46:15 -0800104 * but that will involve changes in The Framework.
105 */
106static struct pcm_config cached_output_hardware_config;
107static bool output_hardware_config_is_cached = false;
108
109struct stream_in {
110 struct audio_stream_in stream;
111
Simon Wilson19957a32012-04-06 16:17:12 -0700112 pthread_mutex_t lock; /* see note below on mutex acquisition order */
113 struct pcm *pcm;
114 bool standby;
115
116 struct audio_device *dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800117
118 struct audio_config hal_pcm_config;
119
Paul McLeaneedc92e2013-12-19 15:46:15 -0800120// struct resampler_itfe *resampler;
121// struct resampler_buffer_provider buf_provider;
Paul McLean30f41852014-04-16 15:44:20 -0700122
Paul McLeaneedc92e2013-12-19 15:46:15 -0800123 int read_status;
Paul McLean30f41852014-04-16 15:44:20 -0700124
125 // We may need to read more data from the device in order to data reduce to 16bit, 4chan */
126 void * conversion_buffer; /* any conversions are put into here
127 * they could come from here too if
128 * there was a previous conversion */
129 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700130};
131
Paul McLeaneedc92e2013-12-19 15:46:15 -0800132/*
Paul McLean30f41852014-04-16 15:44:20 -0700133 * Input Configuration Cache
134 * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure
135 * but that will involve changes in The Framework.
136 */
137static struct pcm_config cached_input_hardware_config;
138static bool input_hardware_config_is_cached = false;
139
140/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800141 * Utility
142 */
143/*
144 * Translates from ALSA format ID to ANDROID_AUDIO_CORE format ID
145 * (see master/system/core/include/core/audio.h)
146 * TODO(pmclean) Replace with audio_format_from_pcm_format() (in hardware/audio_alsaops.h).
147 * post-integration.
148 */
149static audio_format_t alsa_to_fw_format_id(int alsa_fmt_id)
150{
151 switch (alsa_fmt_id) {
152 case PCM_FORMAT_S8:
153 return AUDIO_FORMAT_PCM_8_BIT;
154
155 case PCM_FORMAT_S24_3LE:
156 //TODO(pmclean) make sure this is the 'right' sort of 24-bit
157 return AUDIO_FORMAT_PCM_8_24_BIT;
158
159 case PCM_FORMAT_S32_LE:
160 case PCM_FORMAT_S24_LE:
161 return AUDIO_FORMAT_PCM_32_BIT;
162 }
163
164 return AUDIO_FORMAT_PCM_16_BIT;
165}
166
167/*
168 * Data Conversions
169 */
170/*
171 * Convert a buffer of PCM16LE samples to packed (3-byte) PCM24LE samples.
172 * in_buff points to the buffer of PCM16 samples
173 * num_in_samples size of input buffer in SAMPLES
174 * out_buff points to the buffer to receive converted PCM24 LE samples.
Paul McLean30f41852014-04-16 15:44:20 -0700175 * returns
176 * the number of BYTES of output data.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800177 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
178 * support PCM24_3LE (24-bit, packed).
Paul McLean30f41852014-04-16 15:44:20 -0700179 * NOTE:
180 * We're just filling the low-order byte of the PCM24LE samples with 0.
181 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeaneedc92e2013-12-19 15:46:15 -0800182 * TODO(pmclean, hung) Move this to a utilities module.
183 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700184static size_t convert_16_to_24_3(const short * in_buff, size_t num_in_samples, unsigned char * out_buff) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800185 /*
186 * Move from back to front so that the conversion can be done in-place
187 * i.e. in_buff == out_buff
188 */
189 int in_buff_size_in_bytes = num_in_samples * 2;
190 /* we need 3 bytes in the output for every 2 bytes in the input */
191 int out_buff_size_in_bytes = ((3 * in_buff_size_in_bytes) / 2);
192 unsigned char* dst_ptr = out_buff + out_buff_size_in_bytes - 1;
Paul McLean30f41852014-04-16 15:44:20 -0700193 size_t src_smpl_index;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700194 const unsigned char* src_ptr = ((const unsigned char *)in_buff) + in_buff_size_in_bytes - 1;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800195 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
196 *dst_ptr-- = *src_ptr--; /* hi-byte */
197 *dst_ptr-- = *src_ptr--; /* low-byte */
Paul McLean30f41852014-04-16 15:44:20 -0700198 /*TODO(pmclean) - we might want to consider dithering the lowest byte. */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800199 *dst_ptr-- = 0; /* zero-byte */
200 }
201
202 /* return number of *bytes* generated */
203 return out_buff_size_in_bytes;
204}
205
206/*
Paul McLean30f41852014-04-16 15:44:20 -0700207 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
208 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800209 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700210 * out_buff points to the buffer to receive converted PCM16LE LE samples.
211 * returns
212 * the number of BYTES of output data.
213 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
214 * support PCM24_3LE (24-bit, packed).
215 * NOTE:
216 * We're just filling the low-order byte of the PCM24LE samples with 0.
217 * This conversion is safe to do in-place (in_buff == out_buff).
218 * TODO(pmclean, hung) Move this to a utilities module.
219 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700220static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples, short * out_buff) {
Paul McLean30f41852014-04-16 15:44:20 -0700221 /*
222 * Move from front to back so that the conversion can be done in-place
223 * i.e. in_buff == out_buff
224 */
225 /* we need 2 bytes in the output for every 3 bytes in the input */
226 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700227 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700228 size_t src_smpl_index;
229 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
230 src_ptr++; /* lowest-(skip)-byte */
231 *dst_ptr++ = *src_ptr++; /* low-byte */
232 *dst_ptr++ = *src_ptr++; /* high-byte */
233 }
234
235 /* return number of *bytes* generated: */
236 return num_in_samples * 2;
237}
238
239/*
240 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
241 * (where N < M).
242 * in_buff points to the buffer of PCM16 samples
243 * in_buff_channels Specifies the number of channels in the input buffer.
Paul McLeaneedc92e2013-12-19 15:46:15 -0800244 * out_buff points to the buffer to receive converted PCM16 samples.
Paul McLean30f41852014-04-16 15:44:20 -0700245 * out_buff_channels Specifies the number of channels in the output buffer.
246 * num_in_samples size of input buffer in SAMPLES
247 * returns
248 * the number of BYTES of output data.
249 * NOTE
250 * channels > N are filled with silence.
251 * This conversion is safe to do in-place (in_buff == out_buff)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800252 * We are doing this since we *always* present to The Framework as STEREO device, but need to
253 * support 4-channel devices.
254 * TODO(pmclean, hung) Move this to a utilities module.
255 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700256static size_t expand_channels_16(const short* in_buff, int in_buff_chans,
Paul McLean30f41852014-04-16 15:44:20 -0700257 short* out_buff, int out_buff_chans,
258 size_t num_in_samples) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800259 /*
260 * Move from back to front so that the conversion can be done in-place
261 * i.e. in_buff == out_buff
Paul McLean30f41852014-04-16 15:44:20 -0700262 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
Paul McLeaneedc92e2013-12-19 15:46:15 -0800263 */
Paul McLean30f41852014-04-16 15:44:20 -0700264 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
265
266 short* dst_ptr = out_buff + num_out_samples - 1;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700267 size_t src_index;
268 const short* src_ptr = in_buff + num_in_samples - 1;
Paul McLean30f41852014-04-16 15:44:20 -0700269 int num_zero_chans = out_buff_chans - in_buff_chans;
270 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
271 int dst_offset;
272 for(dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) {
273 *dst_ptr-- = 0;
274 }
275 for(; dst_offset < out_buff_chans; dst_offset++) {
276 *dst_ptr-- = *src_ptr--;
277 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800278 }
279
280 /* return number of *bytes* generated */
Paul McLean30f41852014-04-16 15:44:20 -0700281 return num_out_samples * sizeof(short);
282}
283
284/*
285 * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels
286 * (where N > M).
287 * in_buff points to the buffer of PCM16 samples
288 * in_buff_channels Specifies the number of channels in the input buffer.
289 * out_buff points to the buffer to receive converted PCM16 samples.
290 * out_buff_channels Specifies the number of channels in the output buffer.
291 * num_in_samples size of input buffer in SAMPLES
292 * returns
293 * the number of BYTES of output data.
294 * NOTE
295 * channels > N are thrown away.
296 * This conversion is safe to do in-place (in_buff == out_buff)
297 * We are doing this since we *always* present to The Framework as STEREO device, but need to
298 * support 4-channel devices.
299 * TODO(pmclean, hung) Move this to a utilities module.
300 */
301static size_t contract_channels_16(short* in_buff, int in_buff_chans,
302 short* out_buff, int out_buff_chans,
303 size_t num_in_samples) {
304 /*
305 * Move from front to back so that the conversion can be done in-place
306 * i.e. in_buff == out_buff
307 * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans
308 */
309 int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans;
310
311 int num_skip_samples = in_buff_chans - out_buff_chans;
312
313 short* dst_ptr = out_buff;
314 short* src_ptr = in_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700315 size_t src_index;
Paul McLean30f41852014-04-16 15:44:20 -0700316 for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) {
317 int dst_offset;
318 for(dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) {
319 *dst_ptr++ = *src_ptr++;
320 }
321 src_ptr += num_skip_samples;
322 }
323
324 /* return number of *bytes* generated */
325 return num_out_samples * sizeof(short);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800326}
327
328/*
329 * ALSA Utilities
330 */
331/*
332 * gets the ALSA bit-format flag from a bits-per-sample value.
333 * TODO(pmclean, hung) Move this to a utilities module.
334 */
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700335static int bits_to_alsa_format(unsigned int bits_per_sample, int default_format)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800336{
337 enum pcm_format format;
338 for (format = PCM_FORMAT_S16_LE; format < PCM_FORMAT_MAX; format++) {
339 if (pcm_format_to_bits(format) == bits_per_sample) {
Paul McLean30f41852014-04-16 15:44:20 -0700340 return format;
341 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800342 }
343 return default_format;
344}
345
Paul McLeancf611912014-04-28 13:03:18 -0700346static void log_pcm_params(struct pcm_params * alsa_hw_params) {
347 ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u",
348 pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS),
349 pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS));
350 ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u",
351 pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS),
352 pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS));
353 ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u",
354 pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
355 pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
356 ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u",
357 pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
358 pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
359 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u",
360 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME),
361 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME));
362 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u",
363 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE),
364 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE));
365 ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u",
366 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES),
367 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES));
368 ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u",
369 pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS),
370 pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS));
371 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u",
372 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME),
373 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME));
374 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u",
375 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE),
376 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE));
377 ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u",
378 pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES),
379 pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES));
380 ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u",
381 pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME),
382 pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME));
383}
384
Paul McLeaneedc92e2013-12-19 15:46:15 -0800385/*
386 * Reads and decodes configuration info from the specified ALSA card/device
387 */
388static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config)
389{
Paul McLean30f41852014-04-16 15:44:20 -0700390 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 -0800391
392 if (card < 0 || device < 0) {
393 return -EINVAL;
394 }
395
396 struct pcm_params * alsa_hw_params = pcm_params_get(card, device, io_type);
397 if (alsa_hw_params == NULL) {
398 return -EINVAL;
399 }
400
401 /*
402 * This Logging will be useful when testing new USB devices.
403 */
Paul McLeancf611912014-04-28 13:03:18 -0700404 /* log_pcm_params(alsa_hw_params); */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800405
406 config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
407 config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
408 config->period_size = pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS);
409 config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS);
410
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700411 unsigned int bits_per_sample = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800412 config->format = bits_to_alsa_format(bits_per_sample, PCM_FORMAT_S16_LE);
413
414 return 0;
415}
416
417/*
418 * HAl Functions
419 */
Simon Wilson19957a32012-04-06 16:17:12 -0700420/**
421 * NOTE: when multiple mutexes have to be acquired, always respect the
422 * following order: hw device > out stream
423 */
424
425/* Helper functions */
Simon Wilson19957a32012-04-06 16:17:12 -0700426static uint32_t out_get_sample_rate(const struct audio_stream *stream)
427{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800428 return cached_output_hardware_config.rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700429}
430
431static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
432{
433 return 0;
434}
435
436static size_t out_get_buffer_size(const struct audio_stream *stream)
437{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800438 return cached_output_hardware_config.period_size * audio_stream_frame_size(stream);
Simon Wilson19957a32012-04-06 16:17:12 -0700439}
440
441static uint32_t out_get_channels(const struct audio_stream *stream)
442{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800443 // Always Stero for now. We will do *some* conversions in this HAL.
444 // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary channels
445 // rewrite this to return the ACTUAL channel format
Simon Wilson19957a32012-04-06 16:17:12 -0700446 return AUDIO_CHANNEL_OUT_STEREO;
447}
448
449static audio_format_t out_get_format(const struct audio_stream *stream)
450{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800451 // Always return 16-bit PCM. We will do *some* conversions in this HAL.
452 // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary PCM formats
453 // rewrite this to return the ACTUAL data format
Simon Wilson19957a32012-04-06 16:17:12 -0700454 return AUDIO_FORMAT_PCM_16_BIT;
455}
456
457static int out_set_format(struct audio_stream *stream, audio_format_t format)
458{
459 return 0;
460}
461
462static int out_standby(struct audio_stream *stream)
463{
464 struct stream_out *out = (struct stream_out *)stream;
465
466 pthread_mutex_lock(&out->dev->lock);
467 pthread_mutex_lock(&out->lock);
468
469 if (!out->standby) {
470 pcm_close(out->pcm);
471 out->pcm = NULL;
472 out->standby = true;
473 }
474
475 pthread_mutex_unlock(&out->lock);
476 pthread_mutex_unlock(&out->dev->lock);
477
478 return 0;
479}
480
481static int out_dump(const struct audio_stream *stream, int fd)
482{
483 return 0;
484}
485
486static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
487{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800488 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
489
Simon Wilson19957a32012-04-06 16:17:12 -0700490 struct stream_out *out = (struct stream_out *)stream;
491 struct audio_device *adev = out->dev;
492 struct str_parms *parms;
493 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800494 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700495 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800496 int ret_value = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700497
498 parms = str_parms_create_str(kvpairs);
499 pthread_mutex_lock(&adev->lock);
500
Paul McLeaneedc92e2013-12-19 15:46:15 -0800501 bool recache_device_params = false;
502 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
503 if (param_val >= 0) {
504 adev->out_card = atoi(value);
505 recache_device_params = true;
506 }
Simon Wilson19957a32012-04-06 16:17:12 -0700507
Paul McLeaneedc92e2013-12-19 15:46:15 -0800508 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
509 if (param_val >= 0) {
510 adev->out_device = atoi(value);
511 recache_device_params = true;
512 }
513
514 if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) {
515 ret_value = read_alsa_device_config(adev->out_card, adev->out_device, PCM_OUT,
Paul McLean30f41852014-04-16 15:44:20 -0700516 &cached_output_hardware_config);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800517 output_hardware_config_is_cached = (ret_value == 0);
518 }
Simon Wilson19957a32012-04-06 16:17:12 -0700519
520 pthread_mutex_unlock(&adev->lock);
521 str_parms_destroy(parms);
522
Paul McLeaneedc92e2013-12-19 15:46:15 -0800523 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700524}
525
Paul McLeaneedc92e2013-12-19 15:46:15 -0800526//TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters()
527// could be written in terms of a get_device_parameters(io_type)
528
Paul McLean30f41852014-04-16 15:44:20 -0700529static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
530{
531 ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys);
532
Paul McLeaneedc92e2013-12-19 15:46:15 -0800533 struct stream_out *out = (struct stream_out *) stream;
534 struct audio_device *adev = out->dev;
535
Paul McLean30f41852014-04-16 15:44:20 -0700536 if (adev->out_card < 0 || adev->out_device < 0)
537 return strdup("");
538
Paul McLeaneedc92e2013-12-19 15:46:15 -0800539 unsigned min, max;
540
541 struct str_parms *query = str_parms_create_str(keys);
542 struct str_parms *result = str_parms_create();
543
544 int num_written = 0;
545 char buffer[256];
546 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
547 char* result_str = NULL;
548
549 struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT);
550
551 // These keys are from hardware/libhardware/include/audio.h
552 // supported sample rates
553 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
554 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
555 // if they are different, return a list containing those two values, otherwise just the one.
556 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
557 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700558 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800559 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700560 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800561 }
562 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
563 buffer);
564 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
565
566 // supported channel counts
567 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
568 // Similarly for output channels count
569 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
570 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700571 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800572 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700573 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800574 }
575 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer);
576 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
577
578 // supported sample formats
579 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
580 // Similarly for output channels count
581 //TODO(pmclean): this is wrong.
582 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
583 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700584 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800585 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700586 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800587 }
588 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
589 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
590
591 result_str = str_parms_to_str(result);
592
593 // done with these...
594 str_parms_destroy(query);
595 str_parms_destroy(result);
596
597 return result_str;
Simon Wilson19957a32012-04-06 16:17:12 -0700598}
599
600static uint32_t out_get_latency(const struct audio_stream_out *stream)
601{
Paul McLean30f41852014-04-16 15:44:20 -0700602 struct stream_out *out = (struct stream_out *) stream;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800603
604 //TODO(pmclean): Do we need a term here for the USB latency
605 // (as reported in the USB descriptors)?
Paul McLean30f41852014-04-16 15:44:20 -0700606 uint32_t latency = (cached_output_hardware_config.period_size
607 * cached_output_hardware_config.period_count * 1000) / out_get_sample_rate(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800608 return latency;
Simon Wilson19957a32012-04-06 16:17:12 -0700609}
610
Paul McLean30f41852014-04-16 15:44:20 -0700611static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700612{
613 return -ENOSYS;
614}
615
Paul McLeaneedc92e2013-12-19 15:46:15 -0800616/* must be called with hw device and output stream mutexes locked */
617static int start_output_stream(struct stream_out *out)
618{
619 struct audio_device *adev = out->dev;
620 int return_val = 0;
621
Paul McLean30f41852014-04-16 15:44:20 -0700622 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
623 adev->out_card, adev->out_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800624
625 out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config);
626 if (out->pcm == NULL) {
627 return -ENOMEM;
628 }
629
630 if (out->pcm && !pcm_is_ready(out->pcm)) {
631 ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm));
632 pcm_close(out->pcm);
633 return -ENOMEM;
634 }
635
Paul McLeaneedc92e2013-12-19 15:46:15 -0800636 return 0;
637}
638
639static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700640{
641 int ret;
642 struct stream_out *out = (struct stream_out *)stream;
643
644 pthread_mutex_lock(&out->dev->lock);
645 pthread_mutex_lock(&out->lock);
646 if (out->standby) {
647 ret = start_output_stream(out);
648 if (ret != 0) {
649 goto err;
650 }
651 out->standby = false;
652 }
653
Paul McLean30f41852014-04-16 15:44:20 -0700654 // Setup conversion buffer
655 // compute maximum potential buffer size.
656 // * 2 for stereo -> quad conversion
657 // * 3/2 for 16bit -> 24 bit conversion
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700658 size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2;
Paul McLean30f41852014-04-16 15:44:20 -0700659 if (required_conversion_buffer_size > out->conversion_buffer_size) {
660 //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
661 // (and do these conversions themselves)
662 out->conversion_buffer_size = required_conversion_buffer_size;
663 out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size);
664 }
665
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700666 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800667 int num_write_buff_bytes = bytes;
668
669 /*
670 * Num Channels conversion
671 */
672 int num_device_channels = cached_output_hardware_config.channels;
673 int num_req_channels = 2; /* always, for now */
Paul McLean30f41852014-04-16 15:44:20 -0700674 if (num_device_channels != num_req_channels) {
Paul McLeaneedc92e2013-12-19 15:46:15 -0800675 num_write_buff_bytes =
Paul McLean30f41852014-04-16 15:44:20 -0700676 expand_channels_16(write_buff, num_req_channels,
677 out->conversion_buffer, num_device_channels,
678 num_write_buff_bytes / sizeof(short));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800679 write_buff = out->conversion_buffer;
680 }
681
682 /*
683 * 16 vs 24-bit logic here
684 */
685 switch (cached_output_hardware_config.format) {
686 case PCM_FORMAT_S16_LE:
687 // the output format is the same as the input format, so just write it out
688 break;
689
690 case PCM_FORMAT_S24_3LE:
691 // 16-bit LE2 - 24-bit LE3
Paul McLean30f41852014-04-16 15:44:20 -0700692 num_write_buff_bytes = convert_16_to_24_3(write_buff,
693 num_write_buff_bytes / sizeof(short),
694 out->conversion_buffer);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800695 write_buff = out->conversion_buffer;
696 break;
697
698 default:
699 // hmmmmm.....
700 ALOGV("usb:Unknown Format!!!");
701 break;
702 }
703
704 if (write_buff != NULL && num_write_buff_bytes != 0) {
705 pcm_write(out->pcm, write_buff, num_write_buff_bytes);
706 }
Simon Wilson19957a32012-04-06 16:17:12 -0700707
708 pthread_mutex_unlock(&out->lock);
709 pthread_mutex_unlock(&out->dev->lock);
710
711 return bytes;
712
713err:
714 pthread_mutex_unlock(&out->lock);
Amit Shekharf9953b72014-01-30 12:47:34 -0800715 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700716 if (ret != 0) {
717 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
718 out_get_sample_rate(&stream->common));
719 }
720
721 return bytes;
722}
723
Paul McLean30f41852014-04-16 15:44:20 -0700724static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700725{
726 return -EINVAL;
727}
728
729static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
730{
731 return 0;
732}
733
734static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
735{
736 return 0;
737}
738
Paul McLean30f41852014-04-16 15:44:20 -0700739static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700740{
741 return -EINVAL;
742}
743
744static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700745 audio_io_handle_t handle,
746 audio_devices_t devices,
747 audio_output_flags_t flags,
748 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -0700749 struct audio_stream_out **stream_out)
750{
Paul McLean30f41852014-04-16 15:44:20 -0700751 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 -0800752 handle, devices, flags);
753
Simon Wilson19957a32012-04-06 16:17:12 -0700754 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800755
Simon Wilson19957a32012-04-06 16:17:12 -0700756 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700757
758 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
759 if (!out)
760 return -ENOMEM;
761
Paul McLeaneedc92e2013-12-19 15:46:15 -0800762 // setup function pointers
Simon Wilson19957a32012-04-06 16:17:12 -0700763 out->stream.common.get_sample_rate = out_get_sample_rate;
764 out->stream.common.set_sample_rate = out_set_sample_rate;
765 out->stream.common.get_buffer_size = out_get_buffer_size;
766 out->stream.common.get_channels = out_get_channels;
767 out->stream.common.get_format = out_get_format;
768 out->stream.common.set_format = out_set_format;
769 out->stream.common.standby = out_standby;
770 out->stream.common.dump = out_dump;
771 out->stream.common.set_parameters = out_set_parameters;
772 out->stream.common.get_parameters = out_get_parameters;
773 out->stream.common.add_audio_effect = out_add_audio_effect;
774 out->stream.common.remove_audio_effect = out_remove_audio_effect;
775 out->stream.get_latency = out_get_latency;
776 out->stream.set_volume = out_set_volume;
777 out->stream.write = out_write;
778 out->stream.get_render_position = out_get_render_position;
779 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
780
781 out->dev = adev;
782
Paul McLeaneedc92e2013-12-19 15:46:15 -0800783 if (output_hardware_config_is_cached) {
784 config->sample_rate = cached_output_hardware_config.rate;
785
786 config->format = alsa_to_fw_format_id(cached_output_hardware_config.format);
787 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
788 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
789 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
790 //TODO(pmclean) remove this when the above restriction is removed.
791 config->format = AUDIO_FORMAT_PCM_16_BIT;
792 }
793
794 config->channel_mask =
795 audio_channel_out_mask_from_count(cached_output_hardware_config.channels);
796 if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) {
797 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
798 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
799 //TODO(pmclean) remove this when the above restriction is removed.
800 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
801 }
802 } else {
803 cached_output_hardware_config = default_alsa_out_config;
804
805 config->format = out_get_format(&out->stream.common);
806 config->channel_mask = out_get_channels(&out->stream.common);
807 config->sample_rate = out_get_sample_rate(&out->stream.common);
808 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800809
810 out->conversion_buffer = NULL;
811 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700812
813 out->standby = true;
814
815 *stream_out = &out->stream;
816 return 0;
817
818err_open:
819 free(out);
820 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800821 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700822}
823
824static void adev_close_output_stream(struct audio_hw_device *dev,
825 struct audio_stream_out *stream)
826{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800827 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -0700828 struct stream_out *out = (struct stream_out *)stream;
829
Paul McLeaneedc92e2013-12-19 15:46:15 -0800830 //TODO(pmclean) why are we doing this when stream get's freed at the end
831 // because it closes the pcm device
Simon Wilson19957a32012-04-06 16:17:12 -0700832 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800833
834 free(out->conversion_buffer);
835 out->conversion_buffer = NULL;
836 out->conversion_buffer_size = 0;
837
Simon Wilson19957a32012-04-06 16:17:12 -0700838 free(stream);
839}
840
841static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
842{
843 return 0;
844}
845
Paul McLean30f41852014-04-16 15:44:20 -0700846static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -0700847{
848 return strdup("");
849}
850
851static int adev_init_check(const struct audio_hw_device *dev)
852{
853 return 0;
854}
855
856static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
857{
858 return -ENOSYS;
859}
860
861static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
862{
863 return -ENOSYS;
864}
865
866static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
867{
868 return 0;
869}
870
871static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
872{
873 return -ENOSYS;
874}
875
876static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
877{
878 return -ENOSYS;
879}
880
881static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700882 const struct audio_config *config)
Simon Wilson19957a32012-04-06 16:17:12 -0700883{
884 return 0;
885}
886
Paul McLeaneedc92e2013-12-19 15:46:15 -0800887/* Helper functions */
888static uint32_t in_get_sample_rate(const struct audio_stream *stream)
889{
Paul McLean30f41852014-04-16 15:44:20 -0700890 return cached_input_hardware_config.rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800891}
892
893static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
894{
895 return -ENOSYS;
896}
897
898static size_t in_get_buffer_size(const struct audio_stream *stream)
899{
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700900 ALOGV("usb: in_get_buffer_size() = %zu",
Paul McLean30f41852014-04-16 15:44:20 -0700901 cached_input_hardware_config.period_size * audio_stream_frame_size(stream));
902 return cached_input_hardware_config.period_size * audio_stream_frame_size(stream);
903
Paul McLeaneedc92e2013-12-19 15:46:15 -0800904}
905
906static uint32_t in_get_channels(const struct audio_stream *stream)
907{
Paul McLean30f41852014-04-16 15:44:20 -0700908 // just report stereo for now
909 return AUDIO_CHANNEL_IN_STEREO;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800910}
911
912static audio_format_t in_get_format(const struct audio_stream *stream)
913{
914 // just report 16-bit, pcm for now.
915 return AUDIO_FORMAT_PCM_16_BIT;
916}
917
918static int in_set_format(struct audio_stream *stream, audio_format_t format)
919{
920 return -ENOSYS;
921}
922
923static int in_standby(struct audio_stream *stream)
924{
Paul McLean30f41852014-04-16 15:44:20 -0700925 struct stream_in *in = (struct stream_in *) stream;
926
927 pthread_mutex_lock(&in->dev->lock);
928 pthread_mutex_lock(&in->lock);
929
930 if (!in->standby) {
931 pcm_close(in->pcm);
932 in->pcm = NULL;
933 in->standby = true;
934 }
935
936 pthread_mutex_unlock(&in->lock);
937 pthread_mutex_unlock(&in->dev->lock);
938
Paul McLeaneedc92e2013-12-19 15:46:15 -0800939 return 0;
940}
941
942static int in_dump(const struct audio_stream *stream, int fd)
943{
944 return 0;
945}
946
947static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
948{
Paul McLean30f41852014-04-16 15:44:20 -0700949 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800950
951 struct stream_in *in = (struct stream_in *)stream;
952 struct audio_device *adev = in->dev;
953 struct str_parms *parms;
954 char value[32];
955 int param_val;
956 int routing = 0;
957 int ret_value = 0;
958
959 parms = str_parms_create_str(kvpairs);
960 pthread_mutex_lock(&adev->lock);
961
Paul McLean30f41852014-04-16 15:44:20 -0700962 bool recache_device_params = false;
963
Paul McLeaneedc92e2013-12-19 15:46:15 -0800964 // Card/Device
965 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
966 if (param_val >= 0) {
967 adev->in_card = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -0700968 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800969 }
970
971 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
972 if (param_val >= 0) {
973 adev->in_device = atoi(value);
Paul McLean30f41852014-04-16 15:44:20 -0700974 recache_device_params = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800975 }
976
Paul McLean30f41852014-04-16 15:44:20 -0700977 if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) {
978 ret_value = read_alsa_device_config(adev->in_card, adev->in_device,
979 PCM_IN, &(cached_input_hardware_config));
980 input_hardware_config_is_cached = (ret_value == 0);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800981 }
982
983 pthread_mutex_unlock(&adev->lock);
984 str_parms_destroy(parms);
985
986 return ret_value;
987}
988
989//TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters()
990// could be written in terms of a get_device_parameters(io_type)
991
Paul McLean30f41852014-04-16 15:44:20 -0700992static char * in_get_parameters(const struct audio_stream *stream, const char *keys) {
993 ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800994
Paul McLean30f41852014-04-16 15:44:20 -0700995 struct stream_in *in = (struct stream_in *)stream;
996 struct audio_device *adev = in->dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800997
Paul McLean30f41852014-04-16 15:44:20 -0700998 if (adev->in_card < 0 || adev->in_device < 0)
999 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001000
Paul McLean30f41852014-04-16 15:44:20 -07001001 struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN);
1002 if (alsa_hw_params == NULL)
1003 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -08001004
Paul McLean30f41852014-04-16 15:44:20 -07001005 struct str_parms *query = str_parms_create_str(keys);
1006 struct str_parms *result = str_parms_create();
Paul McLeaneedc92e2013-12-19 15:46:15 -08001007
Paul McLean30f41852014-04-16 15:44:20 -07001008 int num_written = 0;
1009 char buffer[256];
1010 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
1011 char* result_str = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001012
Paul McLean30f41852014-04-16 15:44:20 -07001013 unsigned min, max;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001014
Paul McLean30f41852014-04-16 15:44:20 -07001015 // These keys are from hardware/libhardware/include/audio.h
1016 // supported sample rates
1017 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
1018 // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so
1019 // if they are different, return a list containing those two values, otherwise just the one.
1020 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE);
1021 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001022 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001023 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001024 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001025 }
1026 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer);
1027 } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES
Paul McLeaneedc92e2013-12-19 15:46:15 -08001028
Paul McLean30f41852014-04-16 15:44:20 -07001029 // supported channel counts
1030 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
1031 // Similarly for output channels count
1032 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS);
1033 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001034 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001035 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001036 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001037 }
1038 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer);
1039 } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001040
Paul McLean30f41852014-04-16 15:44:20 -07001041 // supported sample formats
1042 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
1043 //TODO(pmclean): this is wrong.
1044 min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
1045 max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS);
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001046 num_written = snprintf(buffer, buffer_size, "%u", min);
Paul McLean30f41852014-04-16 15:44:20 -07001047 if (min != max) {
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001048 snprintf(buffer + num_written, buffer_size - num_written, "|%u", max);
Paul McLean30f41852014-04-16 15:44:20 -07001049 }
1050 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer);
1051 } // AUDIO_PARAMETER_STREAM_SUP_FORMATS
Paul McLeaneedc92e2013-12-19 15:46:15 -08001052
Paul McLean30f41852014-04-16 15:44:20 -07001053 result_str = str_parms_to_str(result);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001054
Paul McLean30f41852014-04-16 15:44:20 -07001055 // done with these...
1056 str_parms_destroy(query);
1057 str_parms_destroy(result);
1058
1059 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001060}
1061
1062static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1063{
1064 return 0;
1065}
1066
1067static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1068{
1069 return 0;
1070}
1071
Paul McLean30f41852014-04-16 15:44:20 -07001072static int in_set_gain(struct audio_stream_in *stream, float gain)
1073{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001074 return 0;
1075}
1076
Paul McLean30f41852014-04-16 15:44:20 -07001077/* must be called with hw device and output stream mutexes locked */
1078static int start_input_stream(struct stream_in *in) {
1079 struct audio_device *adev = in->dev;
1080 int return_val = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001081
Paul McLean30f41852014-04-16 15:44:20 -07001082 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
1083 adev->in_card, adev->in_device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001084
Paul McLean30f41852014-04-16 15:44:20 -07001085 in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config);
1086 if (in->pcm == NULL) {
1087 ALOGE("usb:audio_hw pcm_open() in->pcm == NULL");
1088 return -ENOMEM;
1089 }
1090
1091 if (in->pcm && !pcm_is_ready(in->pcm)) {
1092 ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm));
1093 pcm_close(in->pcm);
1094 return -ENOMEM;
1095 }
1096
1097 return 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001098}
1099
Paul McLean30f41852014-04-16 15:44:20 -07001100//TODO(pmclean) mutex stuff here (see out_write)
1101static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1102{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001103 size_t num_read_buff_bytes = 0;
Paul McLean30f41852014-04-16 15:44:20 -07001104 void * read_buff = buffer;
1105 void * out_buff = buffer;
1106
1107 struct stream_in * in = (struct stream_in *) stream;
1108
Paul McLeancf611912014-04-28 13:03:18 -07001109 ALOGV("usb: in_read(%d)", bytes);
1110
Paul McLean30f41852014-04-16 15:44:20 -07001111 pthread_mutex_lock(&in->dev->lock);
1112 pthread_mutex_lock(&in->lock);
1113
1114 if (in->standby) {
1115 if (start_input_stream(in) != 0) {
1116 goto err;
1117 }
1118 in->standby = false;
1119 }
1120
1121 // OK, we need to figure out how much data to read to be able to output the requested
1122 // number of bytes in the HAL format (16-bit, stereo).
1123 num_read_buff_bytes = bytes;
1124 int num_device_channels = cached_input_hardware_config.channels;
1125 int num_req_channels = 2; /* always, for now */
1126
1127 if (num_device_channels != num_req_channels) {
Paul McLeancf611912014-04-28 13:03:18 -07001128 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
Paul McLean30f41852014-04-16 15:44:20 -07001129 }
1130
Eric Laurent7661a482014-06-11 12:00:16 -07001131 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001132 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
1133 }
1134
1135 // Setup/Realloc the conversion buffer (if necessary).
1136 if (num_read_buff_bytes != bytes) {
1137 if (num_read_buff_bytes > in->conversion_buffer_size) {
1138 //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1139 // (and do these conversions themselves)
1140 in->conversion_buffer_size = num_read_buff_bytes;
1141 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1142 }
1143 read_buff = in->conversion_buffer;
1144 }
1145
1146 if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) {
1147 /*
1148 * Do any conversions necessary to send the data in the format specified to/by the HAL
1149 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
1150 */
Eric Laurent7661a482014-06-11 12:00:16 -07001151 if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) {
Paul McLean30f41852014-04-16 15:44:20 -07001152 if (num_device_channels != num_req_channels) {
1153 out_buff = read_buff;
1154 }
1155
1156 /* Bit Format Conversion */
1157 num_read_buff_bytes =
1158 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
1159 }
1160
1161 if (num_device_channels != num_req_channels) {
1162 out_buff = buffer;
1163 /* Num Channels conversion */
Paul McLeancf611912014-04-28 13:03:18 -07001164 if (num_device_channels < num_req_channels) {
1165 num_read_buff_bytes =
1166 contract_channels_16(read_buff, num_device_channels,
1167 out_buff, num_req_channels,
1168 num_read_buff_bytes / sizeof(short));
1169 } else {
1170 num_read_buff_bytes =
1171 expand_channels_16(read_buff, num_device_channels,
1172 out_buff, num_req_channels,
1173 num_read_buff_bytes / sizeof(short));
1174 }
Paul McLean30f41852014-04-16 15:44:20 -07001175 }
1176 }
1177
1178err:
1179 pthread_mutex_unlock(&in->lock);
1180 pthread_mutex_unlock(&in->dev->lock);
1181
1182 return num_read_buff_bytes;
1183}
1184
1185static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1186{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001187 return 0;
1188}
1189
Mike Lockwood46a98092012-04-24 16:41:18 -07001190static int adev_open_input_stream(struct audio_hw_device *dev,
1191 audio_io_handle_t handle,
1192 audio_devices_t devices,
Paul McLean30f41852014-04-16 15:44:20 -07001193 struct audio_config *config,
Simon Wilson19957a32012-04-06 16:17:12 -07001194 struct audio_stream_in **stream_in)
1195{
Mark Salyzyn88e458a2014-04-28 12:30:44 -07001196 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLean30f41852014-04-16 15:44:20 -07001197 config->sample_rate, config->channel_mask, config->format);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001198
1199 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Eric Laurent7661a482014-06-11 12:00:16 -07001200 int ret = 0;
1201
Paul McLeaneedc92e2013-12-19 15:46:15 -08001202 if (in == NULL)
1203 return -ENOMEM;
1204
1205 // setup function pointers
1206 in->stream.common.get_sample_rate = in_get_sample_rate;
1207 in->stream.common.set_sample_rate = in_set_sample_rate;
1208 in->stream.common.get_buffer_size = in_get_buffer_size;
1209 in->stream.common.get_channels = in_get_channels;
1210 in->stream.common.get_format = in_get_format;
1211 in->stream.common.set_format = in_set_format;
1212 in->stream.common.standby = in_standby;
1213 in->stream.common.dump = in_dump;
1214 in->stream.common.set_parameters = in_set_parameters;
1215 in->stream.common.get_parameters = in_get_parameters;
1216 in->stream.common.add_audio_effect = in_add_audio_effect;
1217 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1218
1219 in->stream.set_gain = in_set_gain;
1220 in->stream.read = in_read;
1221 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1222
Paul McLean30f41852014-04-16 15:44:20 -07001223 in->dev = (struct audio_device *)dev;
1224
Eric Laurent7661a482014-06-11 12:00:16 -07001225 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO)
1226 ret = -EINVAL;
Paul McLean30f41852014-04-16 15:44:20 -07001227
Eric Laurent7661a482014-06-11 12:00:16 -07001228 if (input_hardware_config_is_cached) {
1229 config->sample_rate = cached_input_hardware_config.rate;
1230
1231 config->format = alsa_to_fw_format_id(cached_input_hardware_config.format);
Paul McLean30f41852014-04-16 15:44:20 -07001232 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
1233 // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1234 // formats with more other format, so we won't get chosen (say with a 24bit DAC).
1235 //TODO(pmclean) remove this when the above restriction is removed.
1236 config->format = AUDIO_FORMAT_PCM_16_BIT;
1237 }
1238
Eric Laurent7661a482014-06-11 12:00:16 -07001239 config->channel_mask = audio_channel_in_mask_from_count(
1240 cached_input_hardware_config.channels);
1241 if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
Paul McLean30f41852014-04-16 15:44:20 -07001242 // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand
1243 // formats with more channels, so we won't get chosen (say with a 4-channel DAC).
1244 //TODO(pmclean) remove this when the above restriction is removed.
Eric Laurent7661a482014-06-11 12:00:16 -07001245 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
Paul McLean30f41852014-04-16 15:44:20 -07001246 }
1247 } else {
1248 cached_input_hardware_config = default_alsa_in_config;
1249
Eric Laurent7661a482014-06-11 12:00:16 -07001250 config->format = in_get_format(&in->stream.common);
1251 config->channel_mask = in_get_channels(&in->stream.common);
1252 config->sample_rate = in_get_sample_rate(&in->stream.common);
Paul McLean30f41852014-04-16 15:44:20 -07001253 }
Paul McLeaneedc92e2013-12-19 15:46:15 -08001254
1255 in->standby = true;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001256
Paul McLean30f41852014-04-16 15:44:20 -07001257 in->conversion_buffer = NULL;
1258 in->conversion_buffer_size = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001259
1260 *stream_in = &in->stream;
1261
Eric Laurent7661a482014-06-11 12:00:16 -07001262 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -07001263}
1264
Paul McLean30f41852014-04-16 15:44:20 -07001265static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
Simon Wilson19957a32012-04-06 16:17:12 -07001266{
Paul McLean30f41852014-04-16 15:44:20 -07001267 struct stream_in *in = (struct stream_in *)stream;
1268
1269 //TODO(pmclean) why are we doing this when stream get's freed at the end
1270 // because it closes the pcm device
1271 in_standby(&stream->common);
1272
1273 free(in->conversion_buffer);
1274
1275 free(stream);
Simon Wilson19957a32012-04-06 16:17:12 -07001276}
1277
1278static int adev_dump(const audio_hw_device_t *device, int fd)
1279{
1280 return 0;
1281}
1282
1283static int adev_close(hw_device_t *device)
1284{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001285 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001286 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001287
1288 output_hardware_config_is_cached = false;
Paul McLean30f41852014-04-16 15:44:20 -07001289 input_hardware_config_is_cached = false;
Paul McLeaneedc92e2013-12-19 15:46:15 -08001290
Simon Wilson19957a32012-04-06 16:17:12 -07001291 return 0;
1292}
1293
Paul McLean30f41852014-04-16 15:44:20 -07001294static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001295{
Simon Wilson19957a32012-04-06 16:17:12 -07001296 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1297 return -EINVAL;
1298
Paul McLeaneedc92e2013-12-19 15:46:15 -08001299 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001300 if (!adev)
1301 return -ENOMEM;
1302
1303 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001304 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Simon Wilson19957a32012-04-06 16:17:12 -07001305 adev->hw_device.common.module = (struct hw_module_t *) module;
1306 adev->hw_device.common.close = adev_close;
1307
Simon Wilson19957a32012-04-06 16:17:12 -07001308 adev->hw_device.init_check = adev_init_check;
1309 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1310 adev->hw_device.set_master_volume = adev_set_master_volume;
1311 adev->hw_device.set_mode = adev_set_mode;
1312 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1313 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1314 adev->hw_device.set_parameters = adev_set_parameters;
1315 adev->hw_device.get_parameters = adev_get_parameters;
1316 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1317 adev->hw_device.open_output_stream = adev_open_output_stream;
1318 adev->hw_device.close_output_stream = adev_close_output_stream;
1319 adev->hw_device.open_input_stream = adev_open_input_stream;
1320 adev->hw_device.close_input_stream = adev_close_input_stream;
1321 adev->hw_device.dump = adev_dump;
1322
1323 *device = &adev->hw_device.common;
1324
1325 return 0;
1326}
1327
1328static struct hw_module_methods_t hal_module_methods = {
1329 .open = adev_open,
1330};
1331
1332struct audio_module HAL_MODULE_INFO_SYM = {
1333 .common = {
1334 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001335 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1336 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001337 .id = AUDIO_HARDWARE_MODULE_ID,
1338 .name = "USB audio HW HAL",
1339 .author = "The Android Open Source Project",
1340 .methods = &hal_module_methods,
1341 },
1342};