Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1 | /* |
| 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 18 | /* #define LOG_NDEBUG 0 */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 19 | |
| 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 36 | /* 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 | |
| 44 | struct pcm_config default_alsa_out_config = { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 45 | .channels = 2, |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 46 | .rate = OUT_SAMPLING_RATE, |
| 47 | .period_size = OUT_PERIOD_SIZE, |
| 48 | .period_count = OUT_PERIOD_COUNT, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 49 | .format = PCM_FORMAT_S16_LE, |
| 50 | }; |
| 51 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 52 | /* |
| 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 | |
| 59 | struct 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 69 | struct audio_device { |
| 70 | struct audio_hw_device hw_device; |
| 71 | |
| 72 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 73 | |
| 74 | /* output */ |
| 75 | int out_card; |
| 76 | int out_device; |
| 77 | |
| 78 | /* input */ |
| 79 | int in_card; |
| 80 | int in_device; |
| 81 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 82 | bool standby; |
| 83 | }; |
| 84 | |
| 85 | struct stream_out { |
| 86 | struct audio_stream_out stream; |
| 87 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 88 | 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 102 | * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 103 | * but that will involve changes in The Framework. |
| 104 | */ |
| 105 | static struct pcm_config cached_output_hardware_config; |
| 106 | static bool output_hardware_config_is_cached = false; |
| 107 | |
| 108 | struct stream_in { |
| 109 | struct audio_stream_in stream; |
| 110 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 111 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 116 | |
| 117 | struct audio_config hal_pcm_config; |
| 118 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 119 | // struct resampler_itfe *resampler; |
| 120 | // struct resampler_buffer_provider buf_provider; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 121 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 122 | int read_status; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 123 | |
| 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 131 | /* |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 132 | * 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 | */ |
| 136 | static struct pcm_config cached_input_hardware_config; |
| 137 | static bool input_hardware_config_is_cached = false; |
| 138 | |
| 139 | /* |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 140 | * 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 | */ |
| 148 | static 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 174 | * returns |
| 175 | * the number of BYTES of output data. |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 176 | * 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 178 | * 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 181 | * TODO(pmclean, hung) Move this to a utilities module. |
| 182 | */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 183 | static size_t convert_16_to_24_3(short * in_buff, size_t num_in_samples, unsigned char * out_buff) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 184 | /* |
| 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 192 | size_t src_smpl_index; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 193 | 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 197 | /*TODO(pmclean) - we might want to consider dithering the lowest byte. */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 198 | *dst_ptr-- = 0; /* zero-byte */ |
| 199 | } |
| 200 | |
| 201 | /* return number of *bytes* generated */ |
| 202 | return out_buff_size_in_bytes; |
| 203 | } |
| 204 | |
| 205 | /* |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 206 | * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples. |
| 207 | * in_buff points to the buffer of PCM24LE samples |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 208 | * num_in_samples size of input buffer in SAMPLES |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 209 | * 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 | */ |
| 219 | static 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 243 | * out_buff points to the buffer to receive converted PCM16 samples. |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 244 | * 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 251 | * 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 255 | static 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 258 | /* |
| 259 | * Move from back to front so that the conversion can be done in-place |
| 260 | * i.e. in_buff == out_buff |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 261 | * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 262 | */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 263 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 266 | int src_index; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 267 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* return number of *bytes* generated */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 280 | 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 | */ |
| 300 | static 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 325 | } |
| 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 | */ |
| 334 | static 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 339 | return format; |
| 340 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 341 | } |
| 342 | return default_format; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Reads and decodes configuration info from the specified ALSA card/device |
| 347 | */ |
| 348 | static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config) |
| 349 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 350 | ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)",card, device, io_type); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 351 | |
| 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 391 | /** |
| 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 397 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 398 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 399 | return cached_output_hardware_config.rate; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 403 | { |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 408 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 409 | return cached_output_hardware_config.period_size * audio_stream_frame_size(stream); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 413 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 414 | // 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 417 | return AUDIO_CHANNEL_OUT_STEREO; |
| 418 | } |
| 419 | |
| 420 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 421 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 422 | // 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 425 | return AUDIO_FORMAT_PCM_16_BIT; |
| 426 | } |
| 427 | |
| 428 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 429 | { |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static 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 | |
| 452 | static int out_dump(const struct audio_stream *stream, int fd) |
| 453 | { |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 458 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 459 | ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs); |
| 460 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 461 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 465 | int param_val; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 466 | int routing = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 467 | int ret_value = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 468 | |
| 469 | parms = str_parms_create_str(kvpairs); |
| 470 | pthread_mutex_lock(&adev->lock); |
| 471 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 472 | 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 478 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 479 | 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 487 | &cached_output_hardware_config); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 488 | output_hardware_config_is_cached = (ret_value == 0); |
| 489 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 490 | |
| 491 | pthread_mutex_unlock(&adev->lock); |
| 492 | str_parms_destroy(parms); |
| 493 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 494 | return ret_value; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 497 | //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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 500 | static 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 504 | struct stream_out *out = (struct stream_out *) stream; |
| 505 | struct audio_device *adev = out->dev; |
| 506 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 507 | if (adev->out_card < 0 || adev->out_device < 0) |
| 508 | return strdup(""); |
| 509 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 510 | 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 531 | snprintf(buffer + num_written, buffer_size - num_written, "|%d", max); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 532 | } |
| 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 572 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 573 | struct stream_out *out = (struct stream_out *) stream; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 574 | |
| 575 | //TODO(pmclean): Do we need a term here for the USB latency |
| 576 | // (as reported in the USB descriptors)? |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 577 | uint32_t latency = (cached_output_hardware_config.period_size |
| 578 | * cached_output_hardware_config.period_count * 1000) / out_get_sample_rate(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 579 | return latency; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 582 | static int out_set_volume(struct audio_stream_out *stream, float left, float right) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 583 | { |
| 584 | return -ENOSYS; |
| 585 | } |
| 586 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 587 | /* must be called with hw device and output stream mutexes locked */ |
| 588 | static int start_output_stream(struct stream_out *out) |
| 589 | { |
| 590 | struct audio_device *adev = out->dev; |
| 591 | int return_val = 0; |
| 592 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 593 | ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)", |
| 594 | adev->out_card, adev->out_device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 595 | |
| 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 611 | { |
| 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 625 | // 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 637 | 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 645 | if (num_device_channels != num_req_channels) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 646 | num_write_buff_bytes = |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 647 | expand_channels_16(write_buff, num_req_channels, |
| 648 | out->conversion_buffer, num_device_channels, |
| 649 | num_write_buff_bytes / sizeof(short)); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 650 | 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 663 | num_write_buff_bytes = convert_16_to_24_3(write_buff, |
| 664 | num_write_buff_bytes / sizeof(short), |
| 665 | out->conversion_buffer); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 666 | 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 678 | |
| 679 | pthread_mutex_unlock(&out->lock); |
| 680 | pthread_mutex_unlock(&out->dev->lock); |
| 681 | |
| 682 | return bytes; |
| 683 | |
| 684 | err: |
| 685 | pthread_mutex_unlock(&out->lock); |
Amit Shekhar | f9953b7 | 2014-01-30 12:47:34 -0800 | [diff] [blame] | 686 | pthread_mutex_unlock(&out->dev->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 687 | 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 695 | static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 696 | { |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | |
| 700 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 701 | { |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 706 | { |
| 707 | return 0; |
| 708 | } |
| 709 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 710 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 711 | { |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | |
| 715 | static int adev_open_output_stream(struct audio_hw_device *dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 716 | audio_io_handle_t handle, |
| 717 | audio_devices_t devices, |
| 718 | audio_output_flags_t flags, |
| 719 | struct audio_config *config, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 720 | struct audio_stream_out **stream_out) |
| 721 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 722 | ALOGV("usb:audio_hw::out adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X", |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 723 | handle, devices, flags); |
| 724 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 725 | struct audio_device *adev = (struct audio_device *)dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 726 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 727 | struct stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 728 | |
| 729 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 730 | if (!out) |
| 731 | return -ENOMEM; |
| 732 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 733 | // setup function pointers |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 734 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 754 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 780 | |
| 781 | out->conversion_buffer = NULL; |
| 782 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 783 | |
| 784 | out->standby = true; |
| 785 | |
| 786 | *stream_out = &out->stream; |
| 787 | return 0; |
| 788 | |
| 789 | err_open: |
| 790 | free(out); |
| 791 | *stream_out = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 792 | return -ENOSYS; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 796 | struct audio_stream_out *stream) |
| 797 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 798 | ALOGV("usb:audio_hw::out adev_close_output_stream()"); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 799 | struct stream_out *out = (struct stream_out *)stream; |
| 800 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 801 | //TODO(pmclean) why are we doing this when stream get's freed at the end |
| 802 | // because it closes the pcm device |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 803 | out_standby(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 804 | |
| 805 | free(out->conversion_buffer); |
| 806 | out->conversion_buffer = NULL; |
| 807 | out->conversion_buffer_size = 0; |
| 808 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 809 | free(stream); |
| 810 | } |
| 811 | |
| 812 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 813 | { |
| 814 | return 0; |
| 815 | } |
| 816 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 817 | static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 818 | { |
| 819 | return strdup(""); |
| 820 | } |
| 821 | |
| 822 | static int adev_init_check(const struct audio_hw_device *dev) |
| 823 | { |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 828 | { |
| 829 | return -ENOSYS; |
| 830 | } |
| 831 | |
| 832 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 833 | { |
| 834 | return -ENOSYS; |
| 835 | } |
| 836 | |
| 837 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 838 | { |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 843 | { |
| 844 | return -ENOSYS; |
| 845 | } |
| 846 | |
| 847 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 848 | { |
| 849 | return -ENOSYS; |
| 850 | } |
| 851 | |
| 852 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 853 | const struct audio_config *config) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 854 | { |
| 855 | return 0; |
| 856 | } |
| 857 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 858 | /* Helper functions */ |
| 859 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 860 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 861 | return cached_input_hardware_config.rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 865 | { |
| 866 | return -ENOSYS; |
| 867 | } |
| 868 | |
| 869 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 870 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 871 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 878 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 879 | // just report stereo for now |
| 880 | return AUDIO_CHANNEL_IN_STEREO; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static 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 | |
| 889 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 890 | { |
| 891 | return -ENOSYS; |
| 892 | } |
| 893 | |
| 894 | static int in_standby(struct audio_stream *stream) |
| 895 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 896 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | static int in_dump(const struct audio_stream *stream, int fd) |
| 914 | { |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 919 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 920 | ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 921 | |
| 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 933 | bool recache_device_params = false; |
| 934 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 935 | // 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 939 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 940 | } |
| 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 945 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 948 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 952 | } |
| 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 963 | static 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 965 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 966 | struct stream_in *in = (struct stream_in *)stream; |
| 967 | struct audio_device *adev = in->dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 968 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 969 | if (adev->in_card < 0 || adev->in_device < 0) |
| 970 | return strdup(""); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 971 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 972 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 975 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 976 | struct str_parms *query = str_parms_create_str(keys); |
| 977 | struct str_parms *result = str_parms_create(); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 978 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 979 | int num_written = 0; |
| 980 | char buffer[256]; |
| 981 | int buffer_size = sizeof(buffer) / sizeof(buffer[0]); |
| 982 | char* result_str = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 983 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 984 | unsigned min, max; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 985 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 986 | // 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 999 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1000 | // 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1011 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1012 | // 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1023 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1024 | result_str = str_parms_to_str(result); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1025 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1026 | // done with these... |
| 1027 | str_parms_destroy(query); |
| 1028 | str_parms_destroy(result); |
| 1029 | |
| 1030 | return result_str; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1034 | { |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1039 | { |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1043 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 1044 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1045 | return 0; |
| 1046 | } |
| 1047 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1048 | /* must be called with hw device and output stream mutexes locked */ |
| 1049 | static int start_input_stream(struct stream_in *in) { |
| 1050 | struct audio_device *adev = in->dev; |
| 1051 | int return_val = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1052 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1053 | ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)", |
| 1054 | adev->in_card, adev->in_device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1055 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1056 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1071 | //TODO(pmclean) mutex stuff here (see out_write) |
| 1072 | static 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 | |
| 1140 | err: |
| 1141 | pthread_mutex_unlock(&in->lock); |
| 1142 | pthread_mutex_unlock(&in->dev->lock); |
| 1143 | |
| 1144 | return num_read_buff_bytes; |
| 1145 | } |
| 1146 | |
| 1147 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1148 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1149 | return 0; |
| 1150 | } |
| 1151 | |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1152 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1153 | audio_io_handle_t handle, |
| 1154 | audio_devices_t devices, |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1155 | struct audio_config *config, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1156 | struct audio_stream_in **stream_in) |
| 1157 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1158 | ALOGV("usb: in adev_open_input_stream() rate:%d, chanMask:0x%X, fmt:%d", |
| 1159 | config->sample_rate, config->channel_mask, config->format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1160 | |
| 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 McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1183 | 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 McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1211 | |
| 1212 | in->standby = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1213 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1214 | in->conversion_buffer = NULL; |
| 1215 | in->conversion_buffer_size = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1216 | |
| 1217 | *stream_in = &in->stream; |
| 1218 | |
| 1219 | return 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1222 | static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1223 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1224 | 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 Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1236 | { |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | static int adev_close(hw_device_t *device) |
| 1241 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1242 | struct audio_device *adev = (struct audio_device *)device; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1243 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1244 | |
| 1245 | output_hardware_config_is_cached = false; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1246 | input_hardware_config_is_cached = false; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1247 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1248 | return 0; |
| 1249 | } |
| 1250 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame^] | 1251 | static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1252 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1253 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1254 | return -EINVAL; |
| 1255 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1256 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1257 | if (!adev) |
| 1258 | return -ENOMEM; |
| 1259 | |
| 1260 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 1261 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1262 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 1263 | adev->hw_device.common.close = adev_close; |
| 1264 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1265 | 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 | |
| 1285 | static struct hw_module_methods_t hal_module_methods = { |
| 1286 | .open = adev_open, |
| 1287 | }; |
| 1288 | |
| 1289 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1290 | .common = { |
| 1291 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1292 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1293 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1294 | .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 | }; |