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