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