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> |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
| 23 | #include <stdint.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 25 | #include <sys/time.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 26 | |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 27 | #include <log/log.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 28 | #include <cutils/str_parms.h> |
| 29 | #include <cutils/properties.h> |
| 30 | |
| 31 | #include <hardware/hardware.h> |
| 32 | #include <system/audio.h> |
| 33 | #include <hardware/audio.h> |
| 34 | |
| 35 | #include <tinyalsa/asoundlib.h> |
| 36 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 37 | /* This is the default configuration to hand to The Framework on the initial |
| 38 | * adev_open_output_stream(). Actual device attributes will be used on the subsequent |
| 39 | * adev_open_output_stream() after the card and device number have been set in out_set_parameters() |
| 40 | */ |
| 41 | #define OUT_PERIOD_SIZE 1024 |
| 42 | #define OUT_PERIOD_COUNT 4 |
| 43 | #define OUT_SAMPLING_RATE 44100 |
| 44 | |
| 45 | struct pcm_config default_alsa_out_config = { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 46 | .channels = 2, |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 47 | .rate = OUT_SAMPLING_RATE, |
| 48 | .period_size = OUT_PERIOD_SIZE, |
| 49 | .period_count = OUT_PERIOD_COUNT, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 50 | .format = PCM_FORMAT_S16_LE, |
| 51 | }; |
| 52 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 53 | /* |
| 54 | * Input defaults. See comment above. |
| 55 | */ |
| 56 | #define IN_PERIOD_SIZE 1024 |
| 57 | #define IN_PERIOD_COUNT 4 |
| 58 | #define IN_SAMPLING_RATE 44100 |
| 59 | |
| 60 | struct pcm_config default_alsa_in_config = { |
| 61 | .channels = 2, |
| 62 | .rate = IN_SAMPLING_RATE, |
| 63 | .period_size = IN_PERIOD_SIZE, |
| 64 | .period_count = IN_PERIOD_COUNT, |
| 65 | .format = PCM_FORMAT_S16_LE, |
| 66 | .start_threshold = 1, |
| 67 | .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT), |
| 68 | }; |
| 69 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 70 | struct audio_device { |
| 71 | struct audio_hw_device hw_device; |
| 72 | |
| 73 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 74 | |
| 75 | /* output */ |
| 76 | int out_card; |
| 77 | int out_device; |
| 78 | |
| 79 | /* input */ |
| 80 | int in_card; |
| 81 | int in_device; |
| 82 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 83 | bool standby; |
| 84 | }; |
| 85 | |
| 86 | struct stream_out { |
| 87 | struct audio_stream_out stream; |
| 88 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 89 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 90 | struct pcm *pcm; /* state of the stream */ |
| 91 | bool standby; |
| 92 | |
| 93 | struct audio_device *dev; /* hardware information */ |
| 94 | |
| 95 | void * conversion_buffer; /* any conversions are put into here |
| 96 | * they could come from here too if |
| 97 | * there was a previous conversion */ |
| 98 | size_t conversion_buffer_size; /* in bytes */ |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Output Configuration Cache |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 103 | * 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] | 104 | * but that will involve changes in The Framework. |
| 105 | */ |
| 106 | static struct pcm_config cached_output_hardware_config; |
| 107 | static bool output_hardware_config_is_cached = false; |
| 108 | |
| 109 | struct stream_in { |
| 110 | struct audio_stream_in stream; |
| 111 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 112 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 113 | struct pcm *pcm; |
| 114 | bool standby; |
| 115 | |
| 116 | struct audio_device *dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 117 | |
| 118 | struct audio_config hal_pcm_config; |
| 119 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 120 | // struct resampler_itfe *resampler; |
| 121 | // struct resampler_buffer_provider buf_provider; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 122 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 123 | int read_status; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 124 | |
| 125 | // We may need to read more data from the device in order to data reduce to 16bit, 4chan */ |
| 126 | void * conversion_buffer; /* any conversions are put into here |
| 127 | * they could come from here too if |
| 128 | * there was a previous conversion */ |
| 129 | size_t conversion_buffer_size; /* in bytes */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 132 | /* |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 133 | * Input Configuration Cache |
| 134 | * FIXME(pmclean) This is not reentrant. Should probably be moved into the stream structure |
| 135 | * but that will involve changes in The Framework. |
| 136 | */ |
| 137 | static struct pcm_config cached_input_hardware_config; |
| 138 | static bool input_hardware_config_is_cached = false; |
| 139 | |
| 140 | /* |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 141 | * Utility |
| 142 | */ |
| 143 | /* |
| 144 | * Translates from ALSA format ID to ANDROID_AUDIO_CORE format ID |
| 145 | * (see master/system/core/include/core/audio.h) |
| 146 | * TODO(pmclean) Replace with audio_format_from_pcm_format() (in hardware/audio_alsaops.h). |
| 147 | * post-integration. |
| 148 | */ |
| 149 | static audio_format_t alsa_to_fw_format_id(int alsa_fmt_id) |
| 150 | { |
| 151 | switch (alsa_fmt_id) { |
| 152 | case PCM_FORMAT_S8: |
| 153 | return AUDIO_FORMAT_PCM_8_BIT; |
| 154 | |
| 155 | case PCM_FORMAT_S24_3LE: |
| 156 | //TODO(pmclean) make sure this is the 'right' sort of 24-bit |
| 157 | return AUDIO_FORMAT_PCM_8_24_BIT; |
| 158 | |
| 159 | case PCM_FORMAT_S32_LE: |
| 160 | case PCM_FORMAT_S24_LE: |
| 161 | return AUDIO_FORMAT_PCM_32_BIT; |
| 162 | } |
| 163 | |
| 164 | return AUDIO_FORMAT_PCM_16_BIT; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Data Conversions |
| 169 | */ |
| 170 | /* |
| 171 | * Convert a buffer of PCM16LE samples to packed (3-byte) PCM24LE samples. |
| 172 | * in_buff points to the buffer of PCM16 samples |
| 173 | * num_in_samples size of input buffer in SAMPLES |
| 174 | * out_buff points to the buffer to receive converted PCM24 LE samples. |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 175 | * returns |
| 176 | * the number of BYTES of output data. |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 177 | * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to |
| 178 | * support PCM24_3LE (24-bit, packed). |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 179 | * NOTE: |
| 180 | * We're just filling the low-order byte of the PCM24LE samples with 0. |
| 181 | * This conversion is safe to do in-place (in_buff == out_buff). |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 182 | * TODO(pmclean, hung) Move this to a utilities module. |
| 183 | */ |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 184 | static size_t convert_16_to_24_3(const short * in_buff, size_t num_in_samples, unsigned char * out_buff) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 185 | /* |
| 186 | * Move from back to front so that the conversion can be done in-place |
| 187 | * i.e. in_buff == out_buff |
| 188 | */ |
| 189 | int in_buff_size_in_bytes = num_in_samples * 2; |
| 190 | /* we need 3 bytes in the output for every 2 bytes in the input */ |
| 191 | int out_buff_size_in_bytes = ((3 * in_buff_size_in_bytes) / 2); |
| 192 | unsigned char* dst_ptr = out_buff + out_buff_size_in_bytes - 1; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 193 | size_t src_smpl_index; |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 194 | const unsigned char* src_ptr = ((const unsigned char *)in_buff) + in_buff_size_in_bytes - 1; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 195 | for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) { |
| 196 | *dst_ptr-- = *src_ptr--; /* hi-byte */ |
| 197 | *dst_ptr-- = *src_ptr--; /* low-byte */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 198 | /*TODO(pmclean) - we might want to consider dithering the lowest byte. */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 199 | *dst_ptr-- = 0; /* zero-byte */ |
| 200 | } |
| 201 | |
| 202 | /* return number of *bytes* generated */ |
| 203 | return out_buff_size_in_bytes; |
| 204 | } |
| 205 | |
| 206 | /* |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 207 | * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples. |
| 208 | * in_buff points to the buffer of PCM24LE samples |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 209 | * num_in_samples size of input buffer in SAMPLES |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 210 | * out_buff points to the buffer to receive converted PCM16LE LE samples. |
| 211 | * returns |
| 212 | * the number of BYTES of output data. |
| 213 | * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to |
| 214 | * support PCM24_3LE (24-bit, packed). |
| 215 | * NOTE: |
| 216 | * We're just filling the low-order byte of the PCM24LE samples with 0. |
| 217 | * This conversion is safe to do in-place (in_buff == out_buff). |
| 218 | * TODO(pmclean, hung) Move this to a utilities module. |
| 219 | */ |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 220 | static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples, short * out_buff) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 221 | /* |
| 222 | * Move from front to back so that the conversion can be done in-place |
| 223 | * i.e. in_buff == out_buff |
| 224 | */ |
| 225 | /* we need 2 bytes in the output for every 3 bytes in the input */ |
| 226 | unsigned char* dst_ptr = (unsigned char*)out_buff; |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 227 | const unsigned char* src_ptr = in_buff; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 228 | size_t src_smpl_index; |
| 229 | for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) { |
| 230 | src_ptr++; /* lowest-(skip)-byte */ |
| 231 | *dst_ptr++ = *src_ptr++; /* low-byte */ |
| 232 | *dst_ptr++ = *src_ptr++; /* high-byte */ |
| 233 | } |
| 234 | |
| 235 | /* return number of *bytes* generated: */ |
| 236 | return num_in_samples * 2; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels |
| 241 | * (where N < M). |
| 242 | * in_buff points to the buffer of PCM16 samples |
| 243 | * in_buff_channels Specifies the number of channels in the input buffer. |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 244 | * out_buff points to the buffer to receive converted PCM16 samples. |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 245 | * out_buff_channels Specifies the number of channels in the output buffer. |
| 246 | * num_in_samples size of input buffer in SAMPLES |
| 247 | * returns |
| 248 | * the number of BYTES of output data. |
| 249 | * NOTE |
| 250 | * channels > N are filled with silence. |
| 251 | * This conversion is safe to do in-place (in_buff == out_buff) |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 252 | * We are doing this since we *always* present to The Framework as STEREO device, but need to |
| 253 | * support 4-channel devices. |
| 254 | * TODO(pmclean, hung) Move this to a utilities module. |
| 255 | */ |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 256 | static size_t expand_channels_16(const short* in_buff, int in_buff_chans, |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 257 | short* out_buff, int out_buff_chans, |
| 258 | size_t num_in_samples) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 259 | /* |
| 260 | * Move from back to front so that the conversion can be done in-place |
| 261 | * i.e. in_buff == out_buff |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 262 | * 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] | 263 | */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 264 | int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans; |
| 265 | |
| 266 | short* dst_ptr = out_buff + num_out_samples - 1; |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 267 | size_t src_index; |
| 268 | const short* src_ptr = in_buff + num_in_samples - 1; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 269 | int num_zero_chans = out_buff_chans - in_buff_chans; |
| 270 | for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) { |
| 271 | int dst_offset; |
| 272 | for(dst_offset = 0; dst_offset < num_zero_chans; dst_offset++) { |
| 273 | *dst_ptr-- = 0; |
| 274 | } |
| 275 | for(; dst_offset < out_buff_chans; dst_offset++) { |
| 276 | *dst_ptr-- = *src_ptr--; |
| 277 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* return number of *bytes* generated */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 281 | return num_out_samples * sizeof(short); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Convert a buffer of N-channel, interleaved PCM16 samples to M-channel PCM16 channels |
| 286 | * (where N > M). |
| 287 | * in_buff points to the buffer of PCM16 samples |
| 288 | * in_buff_channels Specifies the number of channels in the input buffer. |
| 289 | * out_buff points to the buffer to receive converted PCM16 samples. |
| 290 | * out_buff_channels Specifies the number of channels in the output buffer. |
| 291 | * num_in_samples size of input buffer in SAMPLES |
| 292 | * returns |
| 293 | * the number of BYTES of output data. |
| 294 | * NOTE |
| 295 | * channels > N are thrown away. |
| 296 | * This conversion is safe to do in-place (in_buff == out_buff) |
| 297 | * We are doing this since we *always* present to The Framework as STEREO device, but need to |
| 298 | * support 4-channel devices. |
| 299 | * TODO(pmclean, hung) Move this to a utilities module. |
| 300 | */ |
| 301 | static size_t contract_channels_16(short* in_buff, int in_buff_chans, |
| 302 | short* out_buff, int out_buff_chans, |
| 303 | size_t num_in_samples) { |
| 304 | /* |
| 305 | * Move from front to back so that the conversion can be done in-place |
| 306 | * i.e. in_buff == out_buff |
| 307 | * NOTE: num_in_samples * out_buff_channels must be an even multiple of in_buff_chans |
| 308 | */ |
| 309 | int num_out_samples = (num_in_samples * out_buff_chans)/in_buff_chans; |
| 310 | |
| 311 | int num_skip_samples = in_buff_chans - out_buff_chans; |
| 312 | |
| 313 | short* dst_ptr = out_buff; |
| 314 | short* src_ptr = in_buff; |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 315 | size_t src_index; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 316 | for (src_index = 0; src_index < num_in_samples; src_index += in_buff_chans) { |
| 317 | int dst_offset; |
| 318 | for(dst_offset = 0; dst_offset < out_buff_chans; dst_offset++) { |
| 319 | *dst_ptr++ = *src_ptr++; |
| 320 | } |
| 321 | src_ptr += num_skip_samples; |
| 322 | } |
| 323 | |
| 324 | /* return number of *bytes* generated */ |
| 325 | return num_out_samples * sizeof(short); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
| 329 | * ALSA Utilities |
| 330 | */ |
| 331 | /* |
| 332 | * gets the ALSA bit-format flag from a bits-per-sample value. |
| 333 | * TODO(pmclean, hung) Move this to a utilities module. |
| 334 | */ |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 335 | static int bits_to_alsa_format(unsigned int bits_per_sample, int default_format) |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 336 | { |
| 337 | enum pcm_format format; |
| 338 | for (format = PCM_FORMAT_S16_LE; format < PCM_FORMAT_MAX; format++) { |
| 339 | if (pcm_format_to_bits(format) == bits_per_sample) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 340 | return format; |
| 341 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 342 | } |
| 343 | return default_format; |
| 344 | } |
| 345 | |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 346 | static void log_pcm_params(struct pcm_params * alsa_hw_params) { |
| 347 | ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u", |
| 348 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS), |
| 349 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS)); |
| 350 | ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u", |
| 351 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS), |
| 352 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS)); |
| 353 | ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u", |
| 354 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS), |
| 355 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS)); |
| 356 | ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u", |
| 357 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE), |
| 358 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE)); |
| 359 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u", |
| 360 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME), |
| 361 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME)); |
| 362 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u", |
| 363 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE), |
| 364 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE)); |
| 365 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u", |
| 366 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES), |
| 367 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES)); |
| 368 | ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u", |
| 369 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS), |
| 370 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS)); |
| 371 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u", |
| 372 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME), |
| 373 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME)); |
| 374 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u", |
| 375 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE), |
| 376 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE)); |
| 377 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u", |
| 378 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES), |
| 379 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES)); |
| 380 | ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u", |
| 381 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME), |
| 382 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME)); |
| 383 | } |
| 384 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 385 | /* |
| 386 | * Reads and decodes configuration info from the specified ALSA card/device |
| 387 | */ |
| 388 | static int read_alsa_device_config(int card, int device, int io_type, struct pcm_config * config) |
| 389 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 390 | 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] | 391 | |
| 392 | if (card < 0 || device < 0) { |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | struct pcm_params * alsa_hw_params = pcm_params_get(card, device, io_type); |
| 397 | if (alsa_hw_params == NULL) { |
| 398 | return -EINVAL; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * This Logging will be useful when testing new USB devices. |
| 403 | */ |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 404 | /* log_pcm_params(alsa_hw_params); */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 405 | |
| 406 | config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 407 | config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 408 | config->period_size = pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS); |
| 409 | config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS); |
| 410 | |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 411 | unsigned int bits_per_sample = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 412 | config->format = bits_to_alsa_format(bits_per_sample, PCM_FORMAT_S16_LE); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * HAl Functions |
| 419 | */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 420 | /** |
| 421 | * NOTE: when multiple mutexes have to be acquired, always respect the |
| 422 | * following order: hw device > out stream |
| 423 | */ |
| 424 | |
| 425 | /* Helper functions */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 426 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 427 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 428 | return cached_output_hardware_config.rate; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 432 | { |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 437 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 438 | return cached_output_hardware_config.period_size * audio_stream_frame_size(stream); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 442 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 443 | // Always Stero for now. We will do *some* conversions in this HAL. |
| 444 | // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary channels |
| 445 | // rewrite this to return the ACTUAL channel format |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 446 | return AUDIO_CHANNEL_OUT_STEREO; |
| 447 | } |
| 448 | |
| 449 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 450 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 451 | // Always return 16-bit PCM. We will do *some* conversions in this HAL. |
| 452 | // TODO(pmclean) When AudioPolicyManager & AudioFlinger supports arbitrary PCM formats |
| 453 | // rewrite this to return the ACTUAL data format |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 454 | return AUDIO_FORMAT_PCM_16_BIT; |
| 455 | } |
| 456 | |
| 457 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 458 | { |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int out_standby(struct audio_stream *stream) |
| 463 | { |
| 464 | struct stream_out *out = (struct stream_out *)stream; |
| 465 | |
| 466 | pthread_mutex_lock(&out->dev->lock); |
| 467 | pthread_mutex_lock(&out->lock); |
| 468 | |
| 469 | if (!out->standby) { |
| 470 | pcm_close(out->pcm); |
| 471 | out->pcm = NULL; |
| 472 | out->standby = true; |
| 473 | } |
| 474 | |
| 475 | pthread_mutex_unlock(&out->lock); |
| 476 | pthread_mutex_unlock(&out->dev->lock); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int out_dump(const struct audio_stream *stream, int fd) |
| 482 | { |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 487 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 488 | ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs); |
| 489 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 490 | struct stream_out *out = (struct stream_out *)stream; |
| 491 | struct audio_device *adev = out->dev; |
| 492 | struct str_parms *parms; |
| 493 | char value[32]; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 494 | int param_val; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 495 | int routing = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 496 | int ret_value = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 497 | |
| 498 | parms = str_parms_create_str(kvpairs); |
| 499 | pthread_mutex_lock(&adev->lock); |
| 500 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 501 | bool recache_device_params = false; |
| 502 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 503 | if (param_val >= 0) { |
| 504 | adev->out_card = atoi(value); |
| 505 | recache_device_params = true; |
| 506 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 507 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 508 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 509 | if (param_val >= 0) { |
| 510 | adev->out_device = atoi(value); |
| 511 | recache_device_params = true; |
| 512 | } |
| 513 | |
| 514 | if (recache_device_params && adev->out_card >= 0 && adev->out_device >= 0) { |
| 515 | ret_value = read_alsa_device_config(adev->out_card, adev->out_device, PCM_OUT, |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 516 | &cached_output_hardware_config); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 517 | output_hardware_config_is_cached = (ret_value == 0); |
| 518 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 519 | |
| 520 | pthread_mutex_unlock(&adev->lock); |
| 521 | str_parms_destroy(parms); |
| 522 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 523 | return ret_value; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 526 | //TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters() |
| 527 | // could be written in terms of a get_device_parameters(io_type) |
| 528 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 529 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 530 | { |
| 531 | ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys); |
| 532 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 533 | struct stream_out *out = (struct stream_out *) stream; |
| 534 | struct audio_device *adev = out->dev; |
| 535 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 536 | if (adev->out_card < 0 || adev->out_device < 0) |
| 537 | return strdup(""); |
| 538 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 539 | unsigned min, max; |
| 540 | |
| 541 | struct str_parms *query = str_parms_create_str(keys); |
| 542 | struct str_parms *result = str_parms_create(); |
| 543 | |
| 544 | int num_written = 0; |
| 545 | char buffer[256]; |
| 546 | int buffer_size = sizeof(buffer) / sizeof(buffer[0]); |
| 547 | char* result_str = NULL; |
| 548 | |
| 549 | struct pcm_params * alsa_hw_params = pcm_params_get(adev->out_card, adev->out_device, PCM_OUT); |
| 550 | |
| 551 | // These keys are from hardware/libhardware/include/audio.h |
| 552 | // supported sample rates |
| 553 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 554 | // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so |
| 555 | // if they are different, return a list containing those two values, otherwise just the one. |
| 556 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 557 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE); |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 558 | num_written = snprintf(buffer, buffer_size, "%u", min); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 559 | if (min != max) { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 560 | snprintf(buffer + num_written, buffer_size - num_written, "|%u", max); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 561 | } |
| 562 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, |
| 563 | buffer); |
| 564 | } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES |
| 565 | |
| 566 | // supported channel counts |
| 567 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 568 | // Similarly for output channels count |
| 569 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 570 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS); |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 571 | num_written = snprintf(buffer, buffer_size, "%u", min); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 572 | if (min != max) { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 573 | snprintf(buffer + num_written, buffer_size - num_written, "|%u", max); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 574 | } |
| 575 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, buffer); |
| 576 | } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS |
| 577 | |
| 578 | // supported sample formats |
| 579 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 580 | // Similarly for output channels count |
| 581 | //TODO(pmclean): this is wrong. |
| 582 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
| 583 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 584 | num_written = snprintf(buffer, buffer_size, "%u", min); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 585 | if (min != max) { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 586 | snprintf(buffer + num_written, buffer_size - num_written, "|%u", max); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 587 | } |
| 588 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer); |
| 589 | } // AUDIO_PARAMETER_STREAM_SUP_FORMATS |
| 590 | |
| 591 | result_str = str_parms_to_str(result); |
| 592 | |
| 593 | // done with these... |
| 594 | str_parms_destroy(query); |
| 595 | str_parms_destroy(result); |
| 596 | |
| 597 | return result_str; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 601 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 602 | struct stream_out *out = (struct stream_out *) stream; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 603 | |
| 604 | //TODO(pmclean): Do we need a term here for the USB latency |
| 605 | // (as reported in the USB descriptors)? |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 606 | uint32_t latency = (cached_output_hardware_config.period_size |
| 607 | * 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] | 608 | return latency; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 611 | 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] | 612 | { |
| 613 | return -ENOSYS; |
| 614 | } |
| 615 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 616 | /* must be called with hw device and output stream mutexes locked */ |
| 617 | static int start_output_stream(struct stream_out *out) |
| 618 | { |
| 619 | struct audio_device *adev = out->dev; |
| 620 | int return_val = 0; |
| 621 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 622 | ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)", |
| 623 | adev->out_card, adev->out_device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 624 | |
| 625 | out->pcm = pcm_open(adev->out_card, adev->out_device, PCM_OUT, &cached_output_hardware_config); |
| 626 | if (out->pcm == NULL) { |
| 627 | return -ENOMEM; |
| 628 | } |
| 629 | |
| 630 | if (out->pcm && !pcm_is_ready(out->pcm)) { |
| 631 | ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm)); |
| 632 | pcm_close(out->pcm); |
| 633 | return -ENOMEM; |
| 634 | } |
| 635 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | 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] | 640 | { |
| 641 | int ret; |
| 642 | struct stream_out *out = (struct stream_out *)stream; |
| 643 | |
| 644 | pthread_mutex_lock(&out->dev->lock); |
| 645 | pthread_mutex_lock(&out->lock); |
| 646 | if (out->standby) { |
| 647 | ret = start_output_stream(out); |
| 648 | if (ret != 0) { |
| 649 | goto err; |
| 650 | } |
| 651 | out->standby = false; |
| 652 | } |
| 653 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 654 | // Setup conversion buffer |
| 655 | // compute maximum potential buffer size. |
| 656 | // * 2 for stereo -> quad conversion |
| 657 | // * 3/2 for 16bit -> 24 bit conversion |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 658 | size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 659 | if (required_conversion_buffer_size > out->conversion_buffer_size) { |
| 660 | //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 661 | // (and do these conversions themselves) |
| 662 | out->conversion_buffer_size = required_conversion_buffer_size; |
| 663 | out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size); |
| 664 | } |
| 665 | |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 666 | const void * write_buff = buffer; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 667 | int num_write_buff_bytes = bytes; |
| 668 | |
| 669 | /* |
| 670 | * Num Channels conversion |
| 671 | */ |
| 672 | int num_device_channels = cached_output_hardware_config.channels; |
| 673 | int num_req_channels = 2; /* always, for now */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 674 | if (num_device_channels != num_req_channels) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 675 | num_write_buff_bytes = |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 676 | expand_channels_16(write_buff, num_req_channels, |
| 677 | out->conversion_buffer, num_device_channels, |
| 678 | num_write_buff_bytes / sizeof(short)); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 679 | write_buff = out->conversion_buffer; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * 16 vs 24-bit logic here |
| 684 | */ |
| 685 | switch (cached_output_hardware_config.format) { |
| 686 | case PCM_FORMAT_S16_LE: |
| 687 | // the output format is the same as the input format, so just write it out |
| 688 | break; |
| 689 | |
| 690 | case PCM_FORMAT_S24_3LE: |
| 691 | // 16-bit LE2 - 24-bit LE3 |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 692 | num_write_buff_bytes = convert_16_to_24_3(write_buff, |
| 693 | num_write_buff_bytes / sizeof(short), |
| 694 | out->conversion_buffer); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 695 | write_buff = out->conversion_buffer; |
| 696 | break; |
| 697 | |
| 698 | default: |
| 699 | // hmmmmm..... |
| 700 | ALOGV("usb:Unknown Format!!!"); |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | if (write_buff != NULL && num_write_buff_bytes != 0) { |
| 705 | pcm_write(out->pcm, write_buff, num_write_buff_bytes); |
| 706 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 707 | |
| 708 | pthread_mutex_unlock(&out->lock); |
| 709 | pthread_mutex_unlock(&out->dev->lock); |
| 710 | |
| 711 | return bytes; |
| 712 | |
| 713 | err: |
| 714 | pthread_mutex_unlock(&out->lock); |
Amit Shekhar | f9953b7 | 2014-01-30 12:47:34 -0800 | [diff] [blame] | 715 | pthread_mutex_unlock(&out->dev->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 716 | if (ret != 0) { |
| 717 | usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / |
| 718 | out_get_sample_rate(&stream->common)); |
| 719 | } |
| 720 | |
| 721 | return bytes; |
| 722 | } |
| 723 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 724 | 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] | 725 | { |
| 726 | return -EINVAL; |
| 727 | } |
| 728 | |
| 729 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 730 | { |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 735 | { |
| 736 | return 0; |
| 737 | } |
| 738 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 739 | 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] | 740 | { |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | |
| 744 | static int adev_open_output_stream(struct audio_hw_device *dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 745 | audio_io_handle_t handle, |
| 746 | audio_devices_t devices, |
| 747 | audio_output_flags_t flags, |
| 748 | struct audio_config *config, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 749 | struct audio_stream_out **stream_out) |
| 750 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 751 | 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] | 752 | handle, devices, flags); |
| 753 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 754 | struct audio_device *adev = (struct audio_device *)dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 755 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 756 | struct stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 757 | |
| 758 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 759 | if (!out) |
| 760 | return -ENOMEM; |
| 761 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 762 | // setup function pointers |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 763 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 764 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 765 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 766 | out->stream.common.get_channels = out_get_channels; |
| 767 | out->stream.common.get_format = out_get_format; |
| 768 | out->stream.common.set_format = out_set_format; |
| 769 | out->stream.common.standby = out_standby; |
| 770 | out->stream.common.dump = out_dump; |
| 771 | out->stream.common.set_parameters = out_set_parameters; |
| 772 | out->stream.common.get_parameters = out_get_parameters; |
| 773 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 774 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 775 | out->stream.get_latency = out_get_latency; |
| 776 | out->stream.set_volume = out_set_volume; |
| 777 | out->stream.write = out_write; |
| 778 | out->stream.get_render_position = out_get_render_position; |
| 779 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 780 | |
| 781 | out->dev = adev; |
| 782 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 783 | if (output_hardware_config_is_cached) { |
| 784 | config->sample_rate = cached_output_hardware_config.rate; |
| 785 | |
| 786 | config->format = alsa_to_fw_format_id(cached_output_hardware_config.format); |
| 787 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 788 | // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 789 | // formats with more other format, so we won't get chosen (say with a 24bit DAC). |
| 790 | //TODO(pmclean) remove this when the above restriction is removed. |
| 791 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 792 | } |
| 793 | |
| 794 | config->channel_mask = |
| 795 | audio_channel_out_mask_from_count(cached_output_hardware_config.channels); |
| 796 | if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) { |
| 797 | // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 798 | // formats with more channels, so we won't get chosen (say with a 4-channel DAC). |
| 799 | //TODO(pmclean) remove this when the above restriction is removed. |
| 800 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 801 | } |
| 802 | } else { |
| 803 | cached_output_hardware_config = default_alsa_out_config; |
| 804 | |
| 805 | config->format = out_get_format(&out->stream.common); |
| 806 | config->channel_mask = out_get_channels(&out->stream.common); |
| 807 | config->sample_rate = out_get_sample_rate(&out->stream.common); |
| 808 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 809 | |
| 810 | out->conversion_buffer = NULL; |
| 811 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 812 | |
| 813 | out->standby = true; |
| 814 | |
| 815 | *stream_out = &out->stream; |
| 816 | return 0; |
| 817 | |
| 818 | err_open: |
| 819 | free(out); |
| 820 | *stream_out = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 821 | return -ENOSYS; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 825 | struct audio_stream_out *stream) |
| 826 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 827 | ALOGV("usb:audio_hw::out adev_close_output_stream()"); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 828 | struct stream_out *out = (struct stream_out *)stream; |
| 829 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 830 | //TODO(pmclean) why are we doing this when stream get's freed at the end |
| 831 | // because it closes the pcm device |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 832 | out_standby(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 833 | |
| 834 | free(out->conversion_buffer); |
| 835 | out->conversion_buffer = NULL; |
| 836 | out->conversion_buffer_size = 0; |
| 837 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 838 | free(stream); |
| 839 | } |
| 840 | |
| 841 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 842 | { |
| 843 | return 0; |
| 844 | } |
| 845 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | return strdup(""); |
| 849 | } |
| 850 | |
| 851 | static int adev_init_check(const struct audio_hw_device *dev) |
| 852 | { |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 857 | { |
| 858 | return -ENOSYS; |
| 859 | } |
| 860 | |
| 861 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 862 | { |
| 863 | return -ENOSYS; |
| 864 | } |
| 865 | |
| 866 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 867 | { |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 872 | { |
| 873 | return -ENOSYS; |
| 874 | } |
| 875 | |
| 876 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 877 | { |
| 878 | return -ENOSYS; |
| 879 | } |
| 880 | |
| 881 | 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] | 882 | const struct audio_config *config) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 883 | { |
| 884 | return 0; |
| 885 | } |
| 886 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 887 | /* Helper functions */ |
| 888 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 889 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 890 | return cached_input_hardware_config.rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 894 | { |
| 895 | return -ENOSYS; |
| 896 | } |
| 897 | |
| 898 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 899 | { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 900 | ALOGV("usb: in_get_buffer_size() = %zu", |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 901 | cached_input_hardware_config.period_size * audio_stream_frame_size(stream)); |
| 902 | return cached_input_hardware_config.period_size * audio_stream_frame_size(stream); |
| 903 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 907 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 908 | // just report stereo for now |
| 909 | return AUDIO_CHANNEL_IN_STEREO; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 913 | { |
| 914 | // just report 16-bit, pcm for now. |
| 915 | return AUDIO_FORMAT_PCM_16_BIT; |
| 916 | } |
| 917 | |
| 918 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 919 | { |
| 920 | return -ENOSYS; |
| 921 | } |
| 922 | |
| 923 | static int in_standby(struct audio_stream *stream) |
| 924 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 925 | struct stream_in *in = (struct stream_in *) stream; |
| 926 | |
| 927 | pthread_mutex_lock(&in->dev->lock); |
| 928 | pthread_mutex_lock(&in->lock); |
| 929 | |
| 930 | if (!in->standby) { |
| 931 | pcm_close(in->pcm); |
| 932 | in->pcm = NULL; |
| 933 | in->standby = true; |
| 934 | } |
| 935 | |
| 936 | pthread_mutex_unlock(&in->lock); |
| 937 | pthread_mutex_unlock(&in->dev->lock); |
| 938 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | static int in_dump(const struct audio_stream *stream, int fd) |
| 943 | { |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 948 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 949 | ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 950 | |
| 951 | struct stream_in *in = (struct stream_in *)stream; |
| 952 | struct audio_device *adev = in->dev; |
| 953 | struct str_parms *parms; |
| 954 | char value[32]; |
| 955 | int param_val; |
| 956 | int routing = 0; |
| 957 | int ret_value = 0; |
| 958 | |
| 959 | parms = str_parms_create_str(kvpairs); |
| 960 | pthread_mutex_lock(&adev->lock); |
| 961 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 962 | bool recache_device_params = false; |
| 963 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 964 | // Card/Device |
| 965 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 966 | if (param_val >= 0) { |
| 967 | adev->in_card = atoi(value); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 968 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 972 | if (param_val >= 0) { |
| 973 | adev->in_device = atoi(value); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 974 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 975 | } |
| 976 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 977 | if (recache_device_params && adev->in_card >= 0 && adev->in_device >= 0) { |
| 978 | ret_value = read_alsa_device_config(adev->in_card, adev->in_device, |
| 979 | PCM_IN, &(cached_input_hardware_config)); |
| 980 | input_hardware_config_is_cached = (ret_value == 0); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | pthread_mutex_unlock(&adev->lock); |
| 984 | str_parms_destroy(parms); |
| 985 | |
| 986 | return ret_value; |
| 987 | } |
| 988 | |
| 989 | //TODO(pmclean) it seems like both out_get_parameters() and in_get_parameters() |
| 990 | // could be written in terms of a get_device_parameters(io_type) |
| 991 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 992 | static char * in_get_parameters(const struct audio_stream *stream, const char *keys) { |
| 993 | ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 994 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 995 | struct stream_in *in = (struct stream_in *)stream; |
| 996 | struct audio_device *adev = in->dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 997 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 998 | if (adev->in_card < 0 || adev->in_device < 0) |
| 999 | return strdup(""); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1000 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1001 | struct pcm_params * alsa_hw_params = pcm_params_get(adev->in_card, adev->in_device, PCM_IN); |
| 1002 | if (alsa_hw_params == NULL) |
| 1003 | return strdup(""); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1004 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1005 | struct str_parms *query = str_parms_create_str(keys); |
| 1006 | struct str_parms *result = str_parms_create(); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1007 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1008 | int num_written = 0; |
| 1009 | char buffer[256]; |
| 1010 | int buffer_size = sizeof(buffer) / sizeof(buffer[0]); |
| 1011 | char* result_str = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1012 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1013 | unsigned min, max; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1014 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1015 | // These keys are from hardware/libhardware/include/audio.h |
| 1016 | // supported sample rates |
| 1017 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 1018 | // pcm_hw_params doesn't have a list of supported samples rates, just a min and a max, so |
| 1019 | // if they are different, return a list containing those two values, otherwise just the one. |
| 1020 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 1021 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE); |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1022 | num_written = snprintf(buffer, buffer_size, "%u", min); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1023 | if (min != max) { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1024 | snprintf(buffer + num_written, buffer_size - num_written, "|%u", max); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1025 | } |
| 1026 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SAMPLING_RATE, buffer); |
| 1027 | } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1028 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1029 | // supported channel counts |
| 1030 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 1031 | // Similarly for output channels count |
| 1032 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 1033 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS); |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1034 | num_written = snprintf(buffer, buffer_size, "%u", min); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1035 | if (min != max) { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1036 | snprintf(buffer + num_written, buffer_size - num_written, "|%u", max); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1037 | } |
| 1038 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_CHANNELS, buffer); |
| 1039 | } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1040 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1041 | // supported sample formats |
| 1042 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 1043 | //TODO(pmclean): this is wrong. |
| 1044 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
| 1045 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS); |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1046 | num_written = snprintf(buffer, buffer_size, "%u", min); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1047 | if (min != max) { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1048 | snprintf(buffer + num_written, buffer_size - num_written, "|%u", max); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1049 | } |
| 1050 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, buffer); |
| 1051 | } // AUDIO_PARAMETER_STREAM_SUP_FORMATS |
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 | result_str = str_parms_to_str(result); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1054 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1055 | // done with these... |
| 1056 | str_parms_destroy(query); |
| 1057 | str_parms_destroy(result); |
| 1058 | |
| 1059 | return result_str; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1063 | { |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1068 | { |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1072 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 1073 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
| 1076 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1077 | /* must be called with hw device and output stream mutexes locked */ |
| 1078 | static int start_input_stream(struct stream_in *in) { |
| 1079 | struct audio_device *adev = in->dev; |
| 1080 | int return_val = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1081 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1082 | ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)", |
| 1083 | adev->in_card, adev->in_device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1084 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1085 | in->pcm = pcm_open(adev->in_card, adev->in_device, PCM_IN, &cached_input_hardware_config); |
| 1086 | if (in->pcm == NULL) { |
| 1087 | ALOGE("usb:audio_hw pcm_open() in->pcm == NULL"); |
| 1088 | return -ENOMEM; |
| 1089 | } |
| 1090 | |
| 1091 | if (in->pcm && !pcm_is_ready(in->pcm)) { |
| 1092 | ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm)); |
| 1093 | pcm_close(in->pcm); |
| 1094 | return -ENOMEM; |
| 1095 | } |
| 1096 | |
| 1097 | return 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1100 | //TODO(pmclean) mutex stuff here (see out_write) |
| 1101 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) |
| 1102 | { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1103 | size_t num_read_buff_bytes = 0; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1104 | void * read_buff = buffer; |
| 1105 | void * out_buff = buffer; |
| 1106 | |
| 1107 | struct stream_in * in = (struct stream_in *) stream; |
| 1108 | |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 1109 | ALOGV("usb: in_read(%d)", bytes); |
| 1110 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1111 | pthread_mutex_lock(&in->dev->lock); |
| 1112 | pthread_mutex_lock(&in->lock); |
| 1113 | |
| 1114 | if (in->standby) { |
| 1115 | if (start_input_stream(in) != 0) { |
| 1116 | goto err; |
| 1117 | } |
| 1118 | in->standby = false; |
| 1119 | } |
| 1120 | |
| 1121 | // OK, we need to figure out how much data to read to be able to output the requested |
| 1122 | // number of bytes in the HAL format (16-bit, stereo). |
| 1123 | num_read_buff_bytes = bytes; |
| 1124 | int num_device_channels = cached_input_hardware_config.channels; |
| 1125 | int num_req_channels = 2; /* always, for now */ |
| 1126 | |
| 1127 | if (num_device_channels != num_req_channels) { |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 1128 | 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] | 1129 | } |
| 1130 | |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1131 | if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1132 | num_read_buff_bytes = (3 * num_read_buff_bytes) / 2; |
| 1133 | } |
| 1134 | |
| 1135 | // Setup/Realloc the conversion buffer (if necessary). |
| 1136 | if (num_read_buff_bytes != bytes) { |
| 1137 | if (num_read_buff_bytes > in->conversion_buffer_size) { |
| 1138 | //TODO(pmclean) - remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 1139 | // (and do these conversions themselves) |
| 1140 | in->conversion_buffer_size = num_read_buff_bytes; |
| 1141 | in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); |
| 1142 | } |
| 1143 | read_buff = in->conversion_buffer; |
| 1144 | } |
| 1145 | |
| 1146 | if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) { |
| 1147 | /* |
| 1148 | * Do any conversions necessary to send the data in the format specified to/by the HAL |
| 1149 | * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan. |
| 1150 | */ |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1151 | if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1152 | if (num_device_channels != num_req_channels) { |
| 1153 | out_buff = read_buff; |
| 1154 | } |
| 1155 | |
| 1156 | /* Bit Format Conversion */ |
| 1157 | num_read_buff_bytes = |
| 1158 | convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff); |
| 1159 | } |
| 1160 | |
| 1161 | if (num_device_channels != num_req_channels) { |
| 1162 | out_buff = buffer; |
| 1163 | /* Num Channels conversion */ |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 1164 | if (num_device_channels < num_req_channels) { |
| 1165 | num_read_buff_bytes = |
| 1166 | contract_channels_16(read_buff, num_device_channels, |
| 1167 | out_buff, num_req_channels, |
| 1168 | num_read_buff_bytes / sizeof(short)); |
| 1169 | } else { |
| 1170 | num_read_buff_bytes = |
| 1171 | expand_channels_16(read_buff, num_device_channels, |
| 1172 | out_buff, num_req_channels, |
| 1173 | num_read_buff_bytes / sizeof(short)); |
| 1174 | } |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | err: |
| 1179 | pthread_mutex_unlock(&in->lock); |
| 1180 | pthread_mutex_unlock(&in->dev->lock); |
| 1181 | |
| 1182 | return num_read_buff_bytes; |
| 1183 | } |
| 1184 | |
| 1185 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1186 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1190 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1191 | audio_io_handle_t handle, |
| 1192 | audio_devices_t devices, |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1193 | struct audio_config *config, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1194 | struct audio_stream_in **stream_in) |
| 1195 | { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1196 | ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8, |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1197 | config->sample_rate, config->channel_mask, config->format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1198 | |
| 1199 | struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1200 | int ret = 0; |
| 1201 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1202 | if (in == NULL) |
| 1203 | return -ENOMEM; |
| 1204 | |
| 1205 | // setup function pointers |
| 1206 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1207 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1208 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1209 | in->stream.common.get_channels = in_get_channels; |
| 1210 | in->stream.common.get_format = in_get_format; |
| 1211 | in->stream.common.set_format = in_set_format; |
| 1212 | in->stream.common.standby = in_standby; |
| 1213 | in->stream.common.dump = in_dump; |
| 1214 | in->stream.common.set_parameters = in_set_parameters; |
| 1215 | in->stream.common.get_parameters = in_get_parameters; |
| 1216 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1217 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1218 | |
| 1219 | in->stream.set_gain = in_set_gain; |
| 1220 | in->stream.read = in_read; |
| 1221 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 1222 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1223 | in->dev = (struct audio_device *)dev; |
| 1224 | |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1225 | if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) |
| 1226 | ret = -EINVAL; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1227 | |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1228 | if (input_hardware_config_is_cached) { |
| 1229 | config->sample_rate = cached_input_hardware_config.rate; |
| 1230 | |
| 1231 | config->format = alsa_to_fw_format_id(cached_input_hardware_config.format); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1232 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1233 | // Always report PCM16 for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 1234 | // formats with more other format, so we won't get chosen (say with a 24bit DAC). |
| 1235 | //TODO(pmclean) remove this when the above restriction is removed. |
| 1236 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 1237 | } |
| 1238 | |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1239 | config->channel_mask = audio_channel_in_mask_from_count( |
| 1240 | cached_input_hardware_config.channels); |
| 1241 | if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1242 | // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 1243 | // formats with more channels, so we won't get chosen (say with a 4-channel DAC). |
| 1244 | //TODO(pmclean) remove this when the above restriction is removed. |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1245 | config->channel_mask = AUDIO_CHANNEL_IN_STEREO; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1246 | } |
| 1247 | } else { |
| 1248 | cached_input_hardware_config = default_alsa_in_config; |
| 1249 | |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1250 | config->format = in_get_format(&in->stream.common); |
| 1251 | config->channel_mask = in_get_channels(&in->stream.common); |
| 1252 | config->sample_rate = in_get_sample_rate(&in->stream.common); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1253 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1254 | |
| 1255 | in->standby = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1256 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1257 | in->conversion_buffer = NULL; |
| 1258 | in->conversion_buffer_size = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1259 | |
| 1260 | *stream_in = &in->stream; |
| 1261 | |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1262 | return ret; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1265 | 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] | 1266 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1267 | struct stream_in *in = (struct stream_in *)stream; |
| 1268 | |
| 1269 | //TODO(pmclean) why are we doing this when stream get's freed at the end |
| 1270 | // because it closes the pcm device |
| 1271 | in_standby(&stream->common); |
| 1272 | |
| 1273 | free(in->conversion_buffer); |
| 1274 | |
| 1275 | free(stream); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1279 | { |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static int adev_close(hw_device_t *device) |
| 1284 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1285 | struct audio_device *adev = (struct audio_device *)device; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1286 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1287 | |
| 1288 | output_hardware_config_is_cached = false; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1289 | input_hardware_config_is_cached = false; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1290 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1291 | return 0; |
| 1292 | } |
| 1293 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1294 | 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] | 1295 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1296 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1297 | return -EINVAL; |
| 1298 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1299 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1300 | if (!adev) |
| 1301 | return -ENOMEM; |
| 1302 | |
| 1303 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 1304 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1305 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 1306 | adev->hw_device.common.close = adev_close; |
| 1307 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1308 | adev->hw_device.init_check = adev_init_check; |
| 1309 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1310 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1311 | adev->hw_device.set_mode = adev_set_mode; |
| 1312 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1313 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1314 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1315 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1316 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1317 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1318 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1319 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1320 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1321 | adev->hw_device.dump = adev_dump; |
| 1322 | |
| 1323 | *device = &adev->hw_device.common; |
| 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static struct hw_module_methods_t hal_module_methods = { |
| 1329 | .open = adev_open, |
| 1330 | }; |
| 1331 | |
| 1332 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1333 | .common = { |
| 1334 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1335 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1336 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1337 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1338 | .name = "USB audio HW HAL", |
| 1339 | .author = "The Android Open Source Project", |
| 1340 | .methods = &hal_module_methods, |
| 1341 | }, |
| 1342 | }; |