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 | cf61191 | 2014-04-28 13:03:18 -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 | |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame^] | 345 | static void log_pcm_params(struct pcm_params * alsa_hw_params) { |
| 346 | ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u", |
| 347 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS), |
| 348 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS)); |
| 349 | ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u", |
| 350 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS), |
| 351 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS)); |
| 352 | ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u", |
| 353 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS), |
| 354 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS)); |
| 355 | ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u", |
| 356 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE), |
| 357 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE)); |
| 358 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u", |
| 359 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME), |
| 360 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME)); |
| 361 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u", |
| 362 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE), |
| 363 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE)); |
| 364 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u", |
| 365 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES), |
| 366 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES)); |
| 367 | ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u", |
| 368 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS), |
| 369 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS)); |
| 370 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u", |
| 371 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME), |
| 372 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME)); |
| 373 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u", |
| 374 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE), |
| 375 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE)); |
| 376 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u", |
| 377 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES), |
| 378 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES)); |
| 379 | ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u", |
| 380 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME), |
| 381 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME)); |
| 382 | } |
| 383 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 384 | /* |
| 385 | * Reads and decodes configuration info from the specified ALSA card/device |
| 386 | */ |
| 387 | static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config) |
| 388 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 389 | 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] | 390 | |
| 391 | if (card < 0 || device < 0) { |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
| 395 | struct pcm_params * alsa_hw_params = pcm_params_get(card, device, io_type); |
| 396 | if (alsa_hw_params == NULL) { |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * This Logging will be useful when testing new USB devices. |
| 402 | */ |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame^] | 403 | /* log_pcm_params(alsa_hw_params); */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 404 | |
| 405 | config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 406 | config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 407 | config->period_size = pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS); |
| 408 | config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS); |
| 409 | |
| 410 | int bits_per_sample = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
| 411 | config->format = bits_to_alsa_format(bits_per_sample, PCM_FORMAT_S16_LE); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * HAl Functions |
| 418 | */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 419 | /** |
| 420 | * NOTE: when multiple mutexes have to be acquired, always respect the |
| 421 | * following order: hw device > out stream |
| 422 | */ |
| 423 | |
| 424 | /* Helper functions */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 425 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 426 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 427 | return cached_output_hardware_config.rate; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 431 | { |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 436 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 437 | return cached_output_hardware_config.period_size * audio_stream_frame_size(stream); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 441 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 442 | // Always Stero for now. We will do *some* conversions in this HAL. |
| 443 | // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary channels |
| 444 | // rewrite this to return the ACTUAL channel format |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 445 | return AUDIO_CHANNEL_OUT_STEREO; |
| 446 | } |
| 447 | |
| 448 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 449 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 450 | // Always return 16-bit PCM. We will do *some* conversions in this HAL. |
| 451 | // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary PCM formats |
| 452 | // rewrite this to return the ACTUAL data format |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 453 | return AUDIO_FORMAT_PCM_16_BIT; |
| 454 | } |
| 455 | |
| 456 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 457 | { |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int out_standby(struct audio_stream *stream) |
| 462 | { |
| 463 | struct stream_out *out = (struct stream_out *)stream; |
| 464 | |
| 465 | pthread_mutex_lock(&out->dev->lock); |
| 466 | pthread_mutex_lock(&out->lock); |
| 467 | |
| 468 | if (!out->standby) { |
| 469 | pcm_close(out->pcm); |
| 470 | out->pcm = NULL; |
| 471 | out->standby = true; |
| 472 | } |
| 473 | |
| 474 | pthread_mutex_unlock(&out->lock); |
| 475 | pthread_mutex_unlock(&out->dev->lock); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int out_dump(const struct audio_stream *stream, int fd) |
| 481 | { |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 486 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 487 | ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs); |
| 488 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 489 | struct stream_out *out = (struct stream_out *)stream; |
| 490 | struct audio_device *adev = out->dev; |
| 491 | struct str_parms *parms; |
| 492 | char value[32]; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 493 | int param_val; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 494 | int routing = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 495 | int ret_value = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 496 | |
| 497 | parms = str_parms_create_str(kvpairs); |
| 498 | pthread_mutex_lock(&adev->lock); |
| 499 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 500 | bool recache_device_params = false; |
| 501 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 502 | if (param_val >= 0) { |
| 503 | adev->out_card = atoi(value); |
| 504 | recache_device_params = true; |
| 505 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 506 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 507 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 508 | if (param_val >= 0) { |
| 509 | adev->out_device = atoi(value); |
| 510 | recache_device_params = true; |
| 511 | } |
| 512 | |
| 513 | if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) { |
| 514 | 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] | 515 | &cached_output_hardware_config); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 516 | output_hardware_config_is_cached = (ret_value == 0); |
| 517 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 518 | |
| 519 | pthread_mutex_unlock(&adev->lock); |
| 520 | str_parms_destroy(parms); |
| 521 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 522 | return ret_value; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 525 | //TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters() |
| 526 | // could be written in terms of a get_device_parameters(io_type) |
| 527 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 528 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 529 | { |
| 530 | ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys); |
| 531 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 532 | struct stream_out *out = (struct stream_out *) stream; |
| 533 | struct audio_device *adev = out->dev; |
| 534 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 535 | if (adev->out_card < 0 || adev->out_device < 0) |
| 536 | return strdup(""); |
| 537 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 538 | unsigned min, max; |
| 539 | |
| 540 | struct str_parms *query = str_parms_create_str(keys); |
| 541 | struct str_parms *result = str_parms_create(); |
| 542 | |
| 543 | int num_written = 0; |
| 544 | char buffer[256]; |
| 545 | int buffer_size = sizeof(buffer) / sizeof(buffer[0]); |
| 546 | char* result_str = NULL; |
| 547 | |
| 548 | struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT); |
| 549 | |
| 550 | // These keys are from hardware/libhardware/include/audio.h |
| 551 | // supported sample rates |
| 552 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 553 | // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so |
| 554 | // if they are different, return a list containing those two values, otherwise just the one. |
| 555 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 556 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE); |
| 557 | num_written = snprintf(buffer, buffer_size, "%d", min); |
| 558 | if (min != max) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 559 | snprintf(buffer + num_written, buffer_size - num_written, "|%d", max); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 560 | } |
| 561 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, |
| 562 | buffer); |
| 563 | } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES |
| 564 | |
| 565 | // supported channel counts |
| 566 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 567 | // Similarly for output channels count |
| 568 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 569 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 570 | num_written = snprintf(buffer, buffer_size, "%d", min); |
| 571 | if (min != max) { |
| 572 | snprintf(buffer + num_written, buffer_size - num_written, "|%d", max); |
| 573 | } |
| 574 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer); |
| 575 | } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS |
| 576 | |
| 577 | // supported sample formats |
| 578 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 579 | // Similarly for output channels count |
| 580 | //TODO(pmclean): this is wrong. |
| 581 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
| 582 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
| 583 | num_written = snprintf(buffer, buffer_size, "%d", min); |
| 584 | if (min != max) { |
| 585 | snprintf(buffer + num_written, buffer_size - num_written, "|%d", max); |
| 586 | } |
| 587 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer); |
| 588 | } // AUDIO_PARAMETER_STREAM_SUP_FORMATS |
| 589 | |
| 590 | result_str = str_parms_to_str(result); |
| 591 | |
| 592 | // done with these... |
| 593 | str_parms_destroy(query); |
| 594 | str_parms_destroy(result); |
| 595 | |
| 596 | return result_str; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 600 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 601 | struct stream_out *out = (struct stream_out *) stream; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 602 | |
| 603 | //TODO(pmclean): Do we need a term here for the USB latency |
| 604 | // (as reported in the USB descriptors)? |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 605 | uint32_t latency = (cached_output_hardware_config.period_size |
| 606 | * 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] | 607 | return latency; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 610 | 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] | 611 | { |
| 612 | return -ENOSYS; |
| 613 | } |
| 614 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 615 | /* must be called with hw device and output stream mutexes locked */ |
| 616 | static int start_output_stream(struct stream_out *out) |
| 617 | { |
| 618 | struct audio_device *adev = out->dev; |
| 619 | int return_val = 0; |
| 620 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 621 | ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)", |
| 622 | adev->out_card, adev->out_device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 623 | |
| 624 | out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config); |
| 625 | if (out->pcm == NULL) { |
| 626 | return -ENOMEM; |
| 627 | } |
| 628 | |
| 629 | if (out->pcm && !pcm_is_ready(out->pcm)) { |
| 630 | ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm)); |
| 631 | pcm_close(out->pcm); |
| 632 | return -ENOMEM; |
| 633 | } |
| 634 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | 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] | 639 | { |
| 640 | int ret; |
| 641 | struct stream_out *out = (struct stream_out *)stream; |
| 642 | |
| 643 | pthread_mutex_lock(&out->dev->lock); |
| 644 | pthread_mutex_lock(&out->lock); |
| 645 | if (out->standby) { |
| 646 | ret = start_output_stream(out); |
| 647 | if (ret != 0) { |
| 648 | goto err; |
| 649 | } |
| 650 | out->standby = false; |
| 651 | } |
| 652 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 653 | // Setup conversion buffer |
| 654 | // compute maximum potential buffer size. |
| 655 | // * 2 for stereo -> quad conversion |
| 656 | // * 3/2 for 16bit -> 24 bit conversion |
| 657 | int required_conversion_buffer_size = (bytes * 3 * 2) / 2; |
| 658 | if (required_conversion_buffer_size > out->conversion_buffer_size) { |
| 659 | //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 660 | // (and do these conversions themselves) |
| 661 | out->conversion_buffer_size = required_conversion_buffer_size; |
| 662 | out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size); |
| 663 | } |
| 664 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 665 | void * write_buff = buffer; |
| 666 | int num_write_buff_bytes = bytes; |
| 667 | |
| 668 | /* |
| 669 | * Num Channels conversion |
| 670 | */ |
| 671 | int num_device_channels = cached_output_hardware_config.channels; |
| 672 | int num_req_channels = 2; /* always, for now */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 673 | if (num_device_channels != num_req_channels) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 674 | num_write_buff_bytes = |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 675 | expand_channels_16(write_buff, num_req_channels, |
| 676 | out->conversion_buffer, num_device_channels, |
| 677 | num_write_buff_bytes / sizeof(short)); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 678 | write_buff = out->conversion_buffer; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * 16 vs 24-bit logic here |
| 683 | */ |
| 684 | switch (cached_output_hardware_config.format) { |
| 685 | case PCM_FORMAT_S16_LE: |
| 686 | // the output format is the same as the input format, so just write it out |
| 687 | break; |
| 688 | |
| 689 | case PCM_FORMAT_S24_3LE: |
| 690 | // 16-bit LE2 - 24-bit LE3 |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 691 | num_write_buff_bytes = convert_16_to_24_3(write_buff, |
| 692 | num_write_buff_bytes / sizeof(short), |
| 693 | out->conversion_buffer); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 694 | write_buff = out->conversion_buffer; |
| 695 | break; |
| 696 | |
| 697 | default: |
| 698 | // hmmmmm..... |
| 699 | ALOGV("usb:Unknown Format!!!"); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | if (write_buff != NULL && num_write_buff_bytes != 0) { |
| 704 | pcm_write(out->pcm, write_buff, num_write_buff_bytes); |
| 705 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 706 | |
| 707 | pthread_mutex_unlock(&out->lock); |
| 708 | pthread_mutex_unlock(&out->dev->lock); |
| 709 | |
| 710 | return bytes; |
| 711 | |
| 712 | err: |
| 713 | pthread_mutex_unlock(&out->lock); |
Amit Shekhar | f9953b7 | 2014-01-30 12:47:34 -0800 | [diff] [blame] | 714 | pthread_mutex_unlock(&out->dev->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 715 | if (ret != 0) { |
| 716 | usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / |
| 717 | out_get_sample_rate(&stream->common)); |
| 718 | } |
| 719 | |
| 720 | return bytes; |
| 721 | } |
| 722 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 723 | 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] | 724 | { |
| 725 | return -EINVAL; |
| 726 | } |
| 727 | |
| 728 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 729 | { |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 734 | { |
| 735 | return 0; |
| 736 | } |
| 737 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 738 | 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] | 739 | { |
| 740 | return -EINVAL; |
| 741 | } |
| 742 | |
| 743 | static int adev_open_output_stream(struct audio_hw_device *dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 744 | audio_io_handle_t handle, |
| 745 | audio_devices_t devices, |
| 746 | audio_output_flags_t flags, |
| 747 | struct audio_config *config, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 748 | struct audio_stream_out **stream_out) |
| 749 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 750 | 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] | 751 | handle, devices, flags); |
| 752 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 753 | struct audio_device *adev = (struct audio_device *)dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 754 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 755 | struct stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 756 | |
| 757 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 758 | if (!out) |
| 759 | return -ENOMEM; |
| 760 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 761 | // setup function pointers |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 762 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 763 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 764 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 765 | out->stream.common.get_channels = out_get_channels; |
| 766 | out->stream.common.get_format = out_get_format; |
| 767 | out->stream.common.set_format = out_set_format; |
| 768 | out->stream.common.standby = out_standby; |
| 769 | out->stream.common.dump = out_dump; |
| 770 | out->stream.common.set_parameters = out_set_parameters; |
| 771 | out->stream.common.get_parameters = out_get_parameters; |
| 772 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 773 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 774 | out->stream.get_latency = out_get_latency; |
| 775 | out->stream.set_volume = out_set_volume; |
| 776 | out->stream.write = out_write; |
| 777 | out->stream.get_render_position = out_get_render_position; |
| 778 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 779 | |
| 780 | out->dev = adev; |
| 781 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 782 | if (output_hardware_config_is_cached) { |
| 783 | config->sample_rate = cached_output_hardware_config.rate; |
| 784 | |
| 785 | config->format = alsa_to_fw_format_id(cached_output_hardware_config.format); |
| 786 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 787 | // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 788 | // formats with more other format, so we won't get chosen (say with a 24bit DAC). |
| 789 | //TODO(pmclean) remove this when the above restriction is removed. |
| 790 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 791 | } |
| 792 | |
| 793 | config->channel_mask = |
| 794 | audio_channel_out_mask_from_count(cached_output_hardware_config.channels); |
| 795 | if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) { |
| 796 | // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 797 | // formats with more channels, so we won't get chosen (say with a 4-channel DAC). |
| 798 | //TODO(pmclean) remove this when the above restriction is removed. |
| 799 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 800 | } |
| 801 | } else { |
| 802 | cached_output_hardware_config = default_alsa_out_config; |
| 803 | |
| 804 | config->format = out_get_format(&out->stream.common); |
| 805 | config->channel_mask = out_get_channels(&out->stream.common); |
| 806 | config->sample_rate = out_get_sample_rate(&out->stream.common); |
| 807 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 808 | |
| 809 | out->conversion_buffer = NULL; |
| 810 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 811 | |
| 812 | out->standby = true; |
| 813 | |
| 814 | *stream_out = &out->stream; |
| 815 | return 0; |
| 816 | |
| 817 | err_open: |
| 818 | free(out); |
| 819 | *stream_out = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 820 | return -ENOSYS; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 824 | struct audio_stream_out *stream) |
| 825 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 826 | ALOGV("usb:audio_hw::out adev_close_output_stream()"); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 827 | struct stream_out *out = (struct stream_out *)stream; |
| 828 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 829 | //TODO(pmclean) why are we doing this when stream get's freed at the end |
| 830 | // because it closes the pcm device |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 831 | out_standby(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 832 | |
| 833 | free(out->conversion_buffer); |
| 834 | out->conversion_buffer = NULL; |
| 835 | out->conversion_buffer_size = 0; |
| 836 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 837 | free(stream); |
| 838 | } |
| 839 | |
| 840 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 841 | { |
| 842 | return 0; |
| 843 | } |
| 844 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 845 | 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] | 846 | { |
| 847 | return strdup(""); |
| 848 | } |
| 849 | |
| 850 | static int adev_init_check(const struct audio_hw_device *dev) |
| 851 | { |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 856 | { |
| 857 | return -ENOSYS; |
| 858 | } |
| 859 | |
| 860 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 861 | { |
| 862 | return -ENOSYS; |
| 863 | } |
| 864 | |
| 865 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 866 | { |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 871 | { |
| 872 | return -ENOSYS; |
| 873 | } |
| 874 | |
| 875 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 876 | { |
| 877 | return -ENOSYS; |
| 878 | } |
| 879 | |
| 880 | 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] | 881 | const struct audio_config *config) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 882 | { |
| 883 | return 0; |
| 884 | } |
| 885 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 886 | /* Helper functions */ |
| 887 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 888 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 889 | return cached_input_hardware_config.rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 893 | { |
| 894 | return -ENOSYS; |
| 895 | } |
| 896 | |
| 897 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 898 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 899 | ALOGV("usb: in_get_buffer_size() = %d", |
| 900 | cached_input_hardware_config.period_size * audio_stream_frame_size(stream)); |
| 901 | return cached_input_hardware_config.period_size * audio_stream_frame_size(stream); |
| 902 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 906 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 907 | // just report stereo for now |
| 908 | return AUDIO_CHANNEL_IN_STEREO; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 912 | { |
| 913 | // just report 16-bit, pcm for now. |
| 914 | return AUDIO_FORMAT_PCM_16_BIT; |
| 915 | } |
| 916 | |
| 917 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 918 | { |
| 919 | return -ENOSYS; |
| 920 | } |
| 921 | |
| 922 | static int in_standby(struct audio_stream *stream) |
| 923 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 924 | struct stream_in *in = (struct stream_in *) stream; |
| 925 | |
| 926 | pthread_mutex_lock(&in->dev->lock); |
| 927 | pthread_mutex_lock(&in->lock); |
| 928 | |
| 929 | if (!in->standby) { |
| 930 | pcm_close(in->pcm); |
| 931 | in->pcm = NULL; |
| 932 | in->standby = true; |
| 933 | } |
| 934 | |
| 935 | pthread_mutex_unlock(&in->lock); |
| 936 | pthread_mutex_unlock(&in->dev->lock); |
| 937 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | static int in_dump(const struct audio_stream *stream, int fd) |
| 942 | { |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 947 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 948 | ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 949 | |
| 950 | struct stream_in *in = (struct stream_in *)stream; |
| 951 | struct audio_device *adev = in->dev; |
| 952 | struct str_parms *parms; |
| 953 | char value[32]; |
| 954 | int param_val; |
| 955 | int routing = 0; |
| 956 | int ret_value = 0; |
| 957 | |
| 958 | parms = str_parms_create_str(kvpairs); |
| 959 | pthread_mutex_lock(&adev->lock); |
| 960 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 961 | bool recache_device_params = false; |
| 962 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 963 | // Card/Device |
| 964 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 965 | if (param_val >= 0) { |
| 966 | adev->in_card = atoi(value); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 967 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 971 | if (param_val >= 0) { |
| 972 | adev->in_device = atoi(value); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 973 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 976 | if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) { |
| 977 | ret_value = read_alsa_device_config(adev->in_card, adev->in_device, |
| 978 | PCM_IN, &(cached_input_hardware_config)); |
| 979 | input_hardware_config_is_cached = (ret_value == 0); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | pthread_mutex_unlock(&adev->lock); |
| 983 | str_parms_destroy(parms); |
| 984 | |
| 985 | return ret_value; |
| 986 | } |
| 987 | |
| 988 | //TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters() |
| 989 | // could be written in terms of a get_device_parameters(io_type) |
| 990 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 991 | static char * in_get_parameters(const struct audio_stream *stream, const char *keys) { |
| 992 | ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 993 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 994 | struct stream_in *in = (struct stream_in *)stream; |
| 995 | struct audio_device *adev = in->dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 996 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 997 | if (adev->in_card < 0 || adev->in_device < 0) |
| 998 | return strdup(""); |
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 | struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN); |
| 1001 | if (alsa_hw_params == NULL) |
| 1002 | return strdup(""); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1003 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1004 | struct str_parms *query = str_parms_create_str(keys); |
| 1005 | struct str_parms *result = str_parms_create(); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1006 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1007 | int num_written = 0; |
| 1008 | char buffer[256]; |
| 1009 | int buffer_size = sizeof(buffer) / sizeof(buffer[0]); |
| 1010 | char* result_str = NULL; |
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 | unsigned min, max; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1013 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1014 | // These keys are from hardware/libhardware/include/audio.h |
| 1015 | // supported sample rates |
| 1016 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 1017 | // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so |
| 1018 | // if they are different, return a list containing those two values, otherwise just the one. |
| 1019 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 1020 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE); |
| 1021 | num_written = snprintf(buffer, buffer_size, "%d", min); |
| 1022 | if (min != max) { |
| 1023 | snprintf(buffer + num_written, buffer_size - num_written, "|%d", max); |
| 1024 | } |
| 1025 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer); |
| 1026 | } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1027 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1028 | // supported channel counts |
| 1029 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 1030 | // Similarly for output channels count |
| 1031 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 1032 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 1033 | num_written = snprintf(buffer, buffer_size, "%d", min); |
| 1034 | if (min != max) { |
| 1035 | snprintf(buffer + num_written, buffer_size - num_written, "|%d", max); |
| 1036 | } |
| 1037 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer); |
| 1038 | } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1039 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1040 | // supported sample formats |
| 1041 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 1042 | //TODO(pmclean): this is wrong. |
| 1043 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
| 1044 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
| 1045 | num_written = snprintf(buffer, buffer_size, "%d", min); |
| 1046 | if (min != max) { |
| 1047 | snprintf(buffer + num_written, buffer_size - num_written, "|%d", max); |
| 1048 | } |
| 1049 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer); |
| 1050 | } // AUDIO_PARAMETER_STREAM_SUP_FORMATS |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1051 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1052 | result_str = str_parms_to_str(result); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1053 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1054 | // done with these... |
| 1055 | str_parms_destroy(query); |
| 1056 | str_parms_destroy(result); |
| 1057 | |
| 1058 | return result_str; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1062 | { |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1067 | { |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1071 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 1072 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1073 | return 0; |
| 1074 | } |
| 1075 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1076 | /* must be called with hw device and output stream mutexes locked */ |
| 1077 | static int start_input_stream(struct stream_in *in) { |
| 1078 | struct audio_device *adev = in->dev; |
| 1079 | int return_val = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1080 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1081 | ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)", |
| 1082 | adev->in_card, adev->in_device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1083 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1084 | in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config); |
| 1085 | if (in->pcm == NULL) { |
| 1086 | ALOGE("usb:audio_hw pcm_open() in->pcm == NULL"); |
| 1087 | return -ENOMEM; |
| 1088 | } |
| 1089 | |
| 1090 | if (in->pcm && !pcm_is_ready(in->pcm)) { |
| 1091 | ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm)); |
| 1092 | pcm_close(in->pcm); |
| 1093 | return -ENOMEM; |
| 1094 | } |
| 1095 | |
| 1096 | return 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1099 | //TODO(pmclean) mutex stuff here (see out_write) |
| 1100 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) |
| 1101 | { |
| 1102 | int num_read_buff_bytes = 0; |
| 1103 | void * read_buff = buffer; |
| 1104 | void * out_buff = buffer; |
| 1105 | |
| 1106 | struct stream_in * in = (struct stream_in *) stream; |
| 1107 | |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame^] | 1108 | ALOGV("usb: in_read(%d)", bytes); |
| 1109 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1110 | pthread_mutex_lock(&in->dev->lock); |
| 1111 | pthread_mutex_lock(&in->lock); |
| 1112 | |
| 1113 | if (in->standby) { |
| 1114 | if (start_input_stream(in) != 0) { |
| 1115 | goto err; |
| 1116 | } |
| 1117 | in->standby = false; |
| 1118 | } |
| 1119 | |
| 1120 | // OK, we need to figure out how much data to read to be able to output the requested |
| 1121 | // number of bytes in the HAL format (16-bit, stereo). |
| 1122 | num_read_buff_bytes = bytes; |
| 1123 | int num_device_channels = cached_input_hardware_config.channels; |
| 1124 | int num_req_channels = 2; /* always, for now */ |
| 1125 | |
| 1126 | if (num_device_channels != num_req_channels) { |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame^] | 1127 | num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | if (cached_output_hardware_config.format == PCM_FORMAT_S24_3LE) { |
| 1131 | num_read_buff_bytes = (3 * num_read_buff_bytes) / 2; |
| 1132 | } |
| 1133 | |
| 1134 | // Setup/Realloc the conversion buffer (if necessary). |
| 1135 | if (num_read_buff_bytes != bytes) { |
| 1136 | if (num_read_buff_bytes > in->conversion_buffer_size) { |
| 1137 | //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 1138 | // (and do these conversions themselves) |
| 1139 | in->conversion_buffer_size = num_read_buff_bytes; |
| 1140 | in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); |
| 1141 | } |
| 1142 | read_buff = in->conversion_buffer; |
| 1143 | } |
| 1144 | |
| 1145 | if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) { |
| 1146 | /* |
| 1147 | * Do any conversions necessary to send the data in the format specified to/by the HAL |
| 1148 | * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan. |
| 1149 | */ |
| 1150 | if (cached_output_hardware_config.format == PCM_FORMAT_S24_3LE) { |
| 1151 | if (num_device_channels != num_req_channels) { |
| 1152 | out_buff = read_buff; |
| 1153 | } |
| 1154 | |
| 1155 | /* Bit Format Conversion */ |
| 1156 | num_read_buff_bytes = |
| 1157 | convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff); |
| 1158 | } |
| 1159 | |
| 1160 | if (num_device_channels != num_req_channels) { |
| 1161 | out_buff = buffer; |
| 1162 | /* Num Channels conversion */ |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame^] | 1163 | if (num_device_channels < num_req_channels) { |
| 1164 | num_read_buff_bytes = |
| 1165 | contract_channels_16(read_buff, num_device_channels, |
| 1166 | out_buff, num_req_channels, |
| 1167 | num_read_buff_bytes / sizeof(short)); |
| 1168 | } else { |
| 1169 | num_read_buff_bytes = |
| 1170 | expand_channels_16(read_buff, num_device_channels, |
| 1171 | out_buff, num_req_channels, |
| 1172 | num_read_buff_bytes / sizeof(short)); |
| 1173 | } |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | err: |
| 1178 | pthread_mutex_unlock(&in->lock); |
| 1179 | pthread_mutex_unlock(&in->dev->lock); |
| 1180 | |
| 1181 | return num_read_buff_bytes; |
| 1182 | } |
| 1183 | |
| 1184 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1185 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1189 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1190 | audio_io_handle_t handle, |
| 1191 | audio_devices_t devices, |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1192 | struct audio_config *config, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1193 | struct audio_stream_in **stream_in) |
| 1194 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1195 | ALOGV("usb: in adev_open_input_stream() rate:%d, chanMask:0x%X, fmt:%d", |
| 1196 | config->sample_rate, config->channel_mask, config->format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1197 | |
| 1198 | struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 1199 | if (in == NULL) |
| 1200 | return -ENOMEM; |
| 1201 | |
| 1202 | // setup function pointers |
| 1203 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1204 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1205 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1206 | in->stream.common.get_channels = in_get_channels; |
| 1207 | in->stream.common.get_format = in_get_format; |
| 1208 | in->stream.common.set_format = in_set_format; |
| 1209 | in->stream.common.standby = in_standby; |
| 1210 | in->stream.common.dump = in_dump; |
| 1211 | in->stream.common.set_parameters = in_set_parameters; |
| 1212 | in->stream.common.get_parameters = in_get_parameters; |
| 1213 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1214 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1215 | |
| 1216 | in->stream.set_gain = in_set_gain; |
| 1217 | in->stream.read = in_read; |
| 1218 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 1219 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1220 | in->dev = (struct audio_device *)dev; |
| 1221 | |
| 1222 | if (output_hardware_config_is_cached) { |
| 1223 | config->sample_rate = cached_output_hardware_config.rate; |
| 1224 | |
| 1225 | config->format = alsa_to_fw_format_id(cached_output_hardware_config.format); |
| 1226 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1227 | // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 1228 | // formats with more other format, so we won't get chosen (say with a 24bit DAC). |
| 1229 | //TODO(pmclean) remove this when the above restriction is removed. |
| 1230 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 1231 | } |
| 1232 | |
| 1233 | config->channel_mask = audio_channel_out_mask_from_count( |
| 1234 | cached_output_hardware_config.channels); |
| 1235 | if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) { |
| 1236 | // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 1237 | // formats with more channels, so we won't get chosen (say with a 4-channel DAC). |
| 1238 | //TODO(pmclean) remove this when the above restriction is removed. |
| 1239 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1240 | } |
| 1241 | } else { |
| 1242 | cached_input_hardware_config = default_alsa_in_config; |
| 1243 | |
| 1244 | config->format = out_get_format(&in->stream.common); |
| 1245 | config->channel_mask = out_get_channels(&in->stream.common); |
| 1246 | config->sample_rate = out_get_sample_rate(&in->stream.common); |
| 1247 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1248 | |
| 1249 | in->standby = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1250 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1251 | in->conversion_buffer = NULL; |
| 1252 | in->conversion_buffer_size = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1253 | |
| 1254 | *stream_in = &in->stream; |
| 1255 | |
| 1256 | return 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1259 | 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] | 1260 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1261 | struct stream_in *in = (struct stream_in *)stream; |
| 1262 | |
| 1263 | //TODO(pmclean) why are we doing this when stream get's freed at the end |
| 1264 | // because it closes the pcm device |
| 1265 | in_standby(&stream->common); |
| 1266 | |
| 1267 | free(in->conversion_buffer); |
| 1268 | |
| 1269 | free(stream); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1273 | { |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int adev_close(hw_device_t *device) |
| 1278 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1279 | struct audio_device *adev = (struct audio_device *)device; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1280 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1281 | |
| 1282 | output_hardware_config_is_cached = false; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1283 | input_hardware_config_is_cached = false; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1284 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1285 | return 0; |
| 1286 | } |
| 1287 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1288 | 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] | 1289 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1290 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1291 | return -EINVAL; |
| 1292 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1293 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1294 | if (!adev) |
| 1295 | return -ENOMEM; |
| 1296 | |
| 1297 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 1298 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1299 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 1300 | adev->hw_device.common.close = adev_close; |
| 1301 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1302 | adev->hw_device.init_check = adev_init_check; |
| 1303 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1304 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1305 | adev->hw_device.set_mode = adev_set_mode; |
| 1306 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1307 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1308 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1309 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1310 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1311 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1312 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1313 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1314 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1315 | adev->hw_device.dump = adev_dump; |
| 1316 | |
| 1317 | *device = &adev->hw_device.common; |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | static struct hw_module_methods_t hal_module_methods = { |
| 1323 | .open = adev_open, |
| 1324 | }; |
| 1325 | |
| 1326 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1327 | .common = { |
| 1328 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1329 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1330 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1331 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1332 | .name = "USB audio HW HAL", |
| 1333 | .author = "The Android Open Source Project", |
| 1334 | .methods = &hal_module_methods, |
| 1335 | }, |
| 1336 | }; |