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 | /* |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 419 | * Maps from bit position in pcm_mask to PCM_ format constants. |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 420 | */ |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 421 | static int8_t const pcm_format_value_map[] = { |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 422 | PCM_FORMAT_S8, /* 00 - SNDRV_PCM_FORMAT_S8 */ |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 423 | PCM_FORMAT_INVALID, /* 01 - SNDRV_PCM_FORMAT_U8 */ |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 424 | PCM_FORMAT_S16_LE, /* 02 - SNDRV_PCM_FORMAT_S16_LE */ |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 425 | PCM_FORMAT_INVALID, /* 03 - SNDRV_PCM_FORMAT_S16_BE */ |
| 426 | PCM_FORMAT_INVALID, /* 04 - SNDRV_PCM_FORMAT_U16_LE */ |
| 427 | PCM_FORMAT_INVALID, /* 05 - SNDRV_PCM_FORMAT_U16_BE */ |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 428 | PCM_FORMAT_S24_3LE, /* 06 - SNDRV_PCM_FORMAT_S24_LE */ |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 429 | PCM_FORMAT_INVALID, /* 07 - SNDRV_PCM_FORMAT_S24_BE */ |
| 430 | PCM_FORMAT_INVALID, /* 08 - SNDRV_PCM_FORMAT_U24_LE */ |
| 431 | PCM_FORMAT_INVALID, /* 09 - SNDRV_PCM_FORMAT_U24_BE */ |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 432 | PCM_FORMAT_S32_LE, /* 10 - SNDRV_PCM_FORMAT_S32_LE */ |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 433 | PCM_FORMAT_INVALID, /* 11 - SNDRV_PCM_FORMAT_S32_BE */ |
| 434 | PCM_FORMAT_INVALID, /* 12 - SNDRV_PCM_FORMAT_U32_LE */ |
| 435 | PCM_FORMAT_INVALID, /* 13 - SNDRV_PCM_FORMAT_U32_BE */ |
| 436 | PCM_FORMAT_INVALID, /* 14 - SNDRV_PCM_FORMAT_FLOAT_LE */ |
| 437 | PCM_FORMAT_INVALID, /* 15 - SNDRV_PCM_FORMAT_FLOAT_BE */ |
| 438 | PCM_FORMAT_INVALID, /* 16 - SNDRV_PCM_FORMAT_FLOAT64_LE */ |
| 439 | PCM_FORMAT_INVALID, /* 17 - SNDRV_PCM_FORMAT_FLOAT64_BE */ |
| 440 | PCM_FORMAT_INVALID, /* 18 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE */ |
| 441 | PCM_FORMAT_INVALID, /* 19 - SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE */ |
| 442 | PCM_FORMAT_INVALID, /* 20 - SNDRV_PCM_FORMAT_MU_LAW */ |
| 443 | PCM_FORMAT_INVALID, /* 21 - SNDRV_PCM_FORMAT_A_LAW */ |
| 444 | PCM_FORMAT_INVALID, /* 22 - SNDRV_PCM_FORMAT_IMA_ADPCM */ |
| 445 | PCM_FORMAT_INVALID, /* 23 - SNDRV_PCM_FORMAT_MPEG */ |
| 446 | PCM_FORMAT_INVALID, /* 24 - SNDRV_PCM_FORMAT_GSM */ |
| 447 | PCM_FORMAT_INVALID, /* 25 -> 30 (not assigned) */ |
| 448 | PCM_FORMAT_INVALID, |
| 449 | PCM_FORMAT_INVALID, |
| 450 | PCM_FORMAT_INVALID, |
| 451 | PCM_FORMAT_INVALID, |
| 452 | PCM_FORMAT_INVALID, |
| 453 | PCM_FORMAT_INVALID, /* 31 - SNDRV_PCM_FORMAT_SPECIAL */ |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 454 | PCM_FORMAT_S24_3LE, /* 32 - SNDRV_PCM_FORMAT_S24_3LE */ /* ??? */ |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 455 | PCM_FORMAT_INVALID, /* 33 - SNDRV_PCM_FORMAT_S24_3BE */ |
| 456 | PCM_FORMAT_INVALID, /* 34 - SNDRV_PCM_FORMAT_U24_3LE */ |
| 457 | PCM_FORMAT_INVALID, /* 35 - SNDRV_PCM_FORMAT_U24_3BE */ |
| 458 | PCM_FORMAT_INVALID, /* 36 - SNDRV_PCM_FORMAT_S20_3LE */ |
| 459 | PCM_FORMAT_INVALID, /* 37 - SNDRV_PCM_FORMAT_S20_3BE */ |
| 460 | PCM_FORMAT_INVALID, /* 38 - SNDRV_PCM_FORMAT_U20_3LE */ |
| 461 | PCM_FORMAT_INVALID, /* 39 - SNDRV_PCM_FORMAT_U20_3BE */ |
| 462 | PCM_FORMAT_INVALID, /* 40 - SNDRV_PCM_FORMAT_S18_3LE */ |
| 463 | PCM_FORMAT_INVALID, /* 41 - SNDRV_PCM_FORMAT_S18_3BE */ |
| 464 | PCM_FORMAT_INVALID, /* 42 - SNDRV_PCM_FORMAT_U18_3LE */ |
| 465 | PCM_FORMAT_INVALID, /* 43 - SNDRV_PCM_FORMAT_U18_3BE */ |
| 466 | PCM_FORMAT_INVALID, /* 44 - SNDRV_PCM_FORMAT_G723_24 */ |
| 467 | PCM_FORMAT_INVALID, /* 45 - SNDRV_PCM_FORMAT_G723_24_1B */ |
| 468 | PCM_FORMAT_INVALID, /* 46 - SNDRV_PCM_FORMAT_G723_40 */ |
| 469 | PCM_FORMAT_INVALID, /* 47 - SNDRV_PCM_FORMAT_G723_40_1B */ |
| 470 | PCM_FORMAT_INVALID, /* 48 - SNDRV_PCM_FORMAT_DSD_U8 */ |
| 471 | PCM_FORMAT_INVALID /* 49 - SNDRV_PCM_FORMAT_DSD_U16_LE */ |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 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 | |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 482 | int table_size = ARRAY_SIZE(pcm_format_value_map); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 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 | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 491 | /* TODO - we don't want a low-level function to be making this decision */ |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 492 | if (table_index != 0) { /* Don't pick 8-bit */ |
| 493 | /* just return the first one */ |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 494 | return (int)pcm_format_value_map[table_index]; |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 495 | } |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 496 | } |
| 497 | bit_mask <<= 1; |
| 498 | table_index++; |
| 499 | } |
| 500 | } |
| 501 | |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 502 | return PCM_FORMAT_INVALID; |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 505 | static bool test_out_sample_rate(struct audio_device_profile* dev_profile, unsigned rate) { |
| 506 | struct pcm_config local_config = cached_output_hardware_config; |
| 507 | local_config.rate = rate; |
| 508 | |
| 509 | bool works = false; /* let's be pessimistic */ |
| 510 | struct pcm * pcm = |
| 511 | pcm_open(dev_profile->card, dev_profile->device, dev_profile->direction, &local_config); |
| 512 | |
| 513 | if (pcm != NULL) { |
| 514 | works = pcm_is_ready(pcm); |
| 515 | pcm_close(pcm); |
| 516 | } |
| 517 | |
| 518 | return works; |
| 519 | } |
| 520 | |
| 521 | /* sort these highest -> lowest */ |
| 522 | static const unsigned std_sample_rates[] = |
| 523 | {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000}; |
| 524 | |
| 525 | static char* enum_std_sample_rates(struct audio_device_profile* dev_profile, |
| 526 | unsigned min, unsigned max) |
| 527 | { |
| 528 | char buffer[128]; |
| 529 | buffer[0] = '\0'; |
| 530 | int buffSize = ARRAY_SIZE(buffer); |
| 531 | |
| 532 | char numBuffer[32]; |
| 533 | |
| 534 | int numEntries = 0; |
| 535 | unsigned index; |
| 536 | for(index = 0; index < ARRAY_SIZE(std_sample_rates); index++) { |
| 537 | if (std_sample_rates[index] >= min && std_sample_rates[index] <= max && |
| 538 | test_out_sample_rate(dev_profile, std_sample_rates[index])) { |
| 539 | if (numEntries++ != 0) { |
| 540 | strncat(buffer, "|", buffSize); |
| 541 | } |
| 542 | snprintf(numBuffer, sizeof(numBuffer), "%u", std_sample_rates[index]); |
| 543 | strncat(buffer, numBuffer, buffSize); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | return strdup(buffer); |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Logging |
| 552 | */ |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 553 | static void log_pcm_mask(const char* mask_name, struct pcm_mask* mask) { |
| 554 | char buff[512]; |
| 555 | char bit_buff[32]; |
| 556 | int buffSize = sizeof(buff)/sizeof(buff[0]); |
| 557 | |
| 558 | buff[0] = '\0'; |
| 559 | |
| 560 | int num_slots = sizeof(mask->bits) / sizeof(mask->bits[0]); |
| 561 | int bits_per_slot = sizeof(mask->bits[0]) * 8; |
| 562 | |
| 563 | int slot_index, bit_index; |
| 564 | strcat(buff, "["); |
| 565 | for (slot_index = 0; slot_index < num_slots; slot_index++) { |
| 566 | unsigned bit_mask = 1; |
| 567 | for (bit_index = 0; bit_index < bits_per_slot; bit_index++) { |
| 568 | strcat(buff, (mask->bits[slot_index] & bit_mask) != 0 ? "1" : "0"); |
| 569 | bit_mask <<= 1; |
| 570 | } |
| 571 | if (slot_index < num_slots - 1) { |
| 572 | strcat(buff, ","); |
| 573 | } |
| 574 | } |
| 575 | strcat(buff, "]"); |
| 576 | |
| 577 | ALOGV("usb:audio_hw - %s mask:%s", mask_name, buff); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 580 | static void log_pcm_params(struct pcm_params * alsa_hw_params) { |
| 581 | ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u", |
| 582 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS), |
| 583 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS)); |
| 584 | ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u", |
| 585 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS), |
| 586 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS)); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 587 | log_pcm_mask("PCM_PARAM_FORMAT", pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT)); |
| 588 | 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] | 589 | ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u", |
| 590 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS), |
| 591 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS)); |
| 592 | ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u", |
| 593 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE), |
| 594 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE)); |
| 595 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u", |
| 596 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME), |
| 597 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME)); |
| 598 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u", |
| 599 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE), |
| 600 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE)); |
| 601 | ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u", |
| 602 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES), |
| 603 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES)); |
| 604 | ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u", |
| 605 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS), |
| 606 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS)); |
| 607 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u", |
| 608 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME), |
| 609 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME)); |
| 610 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u", |
| 611 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE), |
| 612 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE)); |
| 613 | ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u", |
| 614 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES), |
| 615 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES)); |
| 616 | ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u", |
| 617 | pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME), |
| 618 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME)); |
| 619 | } |
| 620 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 621 | /* |
Paul McLean | 33a6b17 | 2014-06-19 12:35:28 -0700 | [diff] [blame] | 622 | * Returns the supplied value rounded up to the next even multiple of 16 |
| 623 | */ |
| 624 | static unsigned int round_to_16_mult(unsigned int size) { |
| 625 | return (size + 15) & 0xFFFFFFF0; |
| 626 | } |
| 627 | |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 628 | /*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] | 629 | #define MIN_BUFF_TIME 5 /* milliseconds */ |
| 630 | |
| 631 | /* |
| 632 | * Returns the system defined minimum period size based on the supplied sample rate |
| 633 | */ |
| 634 | static unsigned int calc_min_period_size(unsigned int sample_rate) { |
| 635 | unsigned int period_size = (sample_rate * MIN_BUFF_TIME) / 1000; |
| 636 | return round_to_16_mult(period_size); |
| 637 | } |
| 638 | |
| 639 | /* |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 640 | * Reads and decodes configuration info from the specified ALSA card/device |
| 641 | */ |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 642 | static int read_alsa_device_config(struct audio_device_profile * dev_profile, |
| 643 | struct pcm_config * config) |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 644 | { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 645 | ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)", |
| 646 | dev_profile->card, dev_profile->device, dev_profile->direction); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 647 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 648 | if (dev_profile->card < 0 || dev_profile->device < 0) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 649 | return -EINVAL; |
| 650 | } |
| 651 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 652 | struct pcm_params * alsa_hw_params = |
| 653 | pcm_params_get(dev_profile->card, dev_profile->device, dev_profile->direction); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 654 | if (alsa_hw_params == NULL) { |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 658 | int ret = 0; |
| 659 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 660 | /* |
| 661 | * This Logging will be useful when testing new USB devices. |
| 662 | */ |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 663 | #ifdef LOG_PCM_PARAMS |
| 664 | log_pcm_params(alsa_hw_params); |
| 665 | #endif |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 666 | |
| 667 | config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 668 | config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
Paul McLean | 33a6b17 | 2014-06-19 12:35:28 -0700 | [diff] [blame] | 669 | config->period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE); |
| 670 | /* round this up to a multiple of 16 */ |
| 671 | config->period_size = round_to_16_mult(config->period_size); |
| 672 | /* make sure it is above a minimum value to minimize jitter */ |
| 673 | unsigned int min_period_size = calc_min_period_size(config->rate); |
| 674 | if (config->period_size < min_period_size) { |
| 675 | config->period_size = min_period_size; |
| 676 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 677 | config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS); |
| 678 | |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 679 | int format = get_pcm_format_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT)); |
Paul McLean | a02bc9d | 2014-07-16 08:40:00 -0700 | [diff] [blame] | 680 | if (format == PCM_FORMAT_INVALID) { |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 681 | ret = -EINVAL; |
| 682 | } else { |
| 683 | config->format = format; |
| 684 | } |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 685 | |
| 686 | pcm_params_free(alsa_hw_params); |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 687 | return ret; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* |
| 691 | * HAl Functions |
| 692 | */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 693 | /** |
| 694 | * NOTE: when multiple mutexes have to be acquired, always respect the |
| 695 | * following order: hw device > out stream |
| 696 | */ |
| 697 | |
| 698 | /* Helper functions */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 699 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 700 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 701 | return cached_output_hardware_config.rate; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 705 | { |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 710 | { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 711 | return cached_output_hardware_config.period_size * |
| 712 | audio_stream_out_frame_size((const struct audio_stream_out *)stream); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 716 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 717 | // Always Stero for now. We will do *some* conversions in this HAL. |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 718 | /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels |
| 719 | rewrite this to return the ACTUAL channel format */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 720 | return AUDIO_CHANNEL_OUT_STEREO; |
| 721 | } |
| 722 | |
| 723 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 724 | { |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 725 | return audio_format_from_pcm_format(cached_output_hardware_config.format); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 729 | { |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 730 | cached_output_hardware_config.format = pcm_format_from_audio_format(format); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static int out_standby(struct audio_stream *stream) |
| 735 | { |
| 736 | struct stream_out *out = (struct stream_out *)stream; |
| 737 | |
| 738 | pthread_mutex_lock(&out->dev->lock); |
| 739 | pthread_mutex_lock(&out->lock); |
| 740 | |
| 741 | if (!out->standby) { |
| 742 | pcm_close(out->pcm); |
| 743 | out->pcm = NULL; |
| 744 | out->standby = true; |
| 745 | } |
| 746 | |
| 747 | pthread_mutex_unlock(&out->lock); |
| 748 | pthread_mutex_unlock(&out->dev->lock); |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static int out_dump(const struct audio_stream *stream, int fd) |
| 754 | { |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 759 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 760 | ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs); |
| 761 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 762 | struct stream_out *out = (struct stream_out *)stream; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 763 | struct str_parms *parms; |
| 764 | char value[32]; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 765 | int param_val; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 766 | int routing = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 767 | int ret_value = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 768 | |
| 769 | parms = str_parms_create_str(kvpairs); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 770 | pthread_mutex_lock(&out->dev->lock); |
| 771 | pthread_mutex_lock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 772 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 773 | bool recache_device_params = false; |
| 774 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 775 | if (param_val >= 0) { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 776 | out->profile->card = atoi(value); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 777 | recache_device_params = true; |
| 778 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 779 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 780 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 781 | if (param_val >= 0) { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 782 | out->profile->device = atoi(value); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 783 | recache_device_params = true; |
| 784 | } |
| 785 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 786 | if (recache_device_params && out->profile->card >= 0 && out->profile->device >= 0) { |
| 787 | ret_value = read_alsa_device_config(out->profile, &cached_output_hardware_config); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 788 | output_hardware_config_is_cached = (ret_value == 0); |
| 789 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 790 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 791 | pthread_mutex_unlock(&out->lock); |
| 792 | pthread_mutex_unlock(&out->dev->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 793 | str_parms_destroy(parms); |
| 794 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 795 | return ret_value; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 798 | 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] | 799 | { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 800 | ALOGV("usb:audio_hw::device_get_parameters() keys:%s", keys); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 801 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 802 | if (dev_profile->card < 0 || dev_profile->device < 0) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 803 | return strdup(""); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 804 | } |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 805 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 806 | unsigned min, max; |
| 807 | |
| 808 | struct str_parms *query = str_parms_create_str(keys); |
| 809 | struct str_parms *result = str_parms_create(); |
| 810 | |
| 811 | int num_written = 0; |
| 812 | char buffer[256]; |
| 813 | int buffer_size = sizeof(buffer) / sizeof(buffer[0]); |
| 814 | char* result_str = NULL; |
| 815 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 816 | struct pcm_params * alsa_hw_params = |
| 817 | pcm_params_get(dev_profile->card, dev_profile->device, dev_profile->direction); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 818 | |
| 819 | // These keys are from hardware/libhardware/include/audio.h |
| 820 | // supported sample rates |
| 821 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 822 | min = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 823 | max = pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 824 | |
| 825 | char* rates_list = enum_std_sample_rates(dev_profile, min, max); |
| 826 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, rates_list); |
| 827 | free(rates_list); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 828 | } // AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES |
| 829 | |
| 830 | // supported channel counts |
| 831 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 832 | // TODO remove this hack when it is superceeded by proper multi-channel support |
| 833 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, |
| 834 | dev_profile->direction == PCM_OUT |
| 835 | ? "AUDIO_CHANNEL_OUT_STEREO" |
| 836 | : "AUDIO_CHANNEL_IN_STEREO"); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 837 | } // AUDIO_PARAMETER_STREAM_SUP_CHANNELS |
| 838 | |
| 839 | // supported sample formats |
| 840 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 841 | // TODO remove this hack when we have support for input in non PCM16 formats |
| 842 | if (dev_profile->direction == PCM_IN) { |
| 843 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, "AUDIO_FORMAT_PCM_16_BIT"); |
| 844 | } else { |
| 845 | struct pcm_mask * format_mask = pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT); |
| 846 | char * format_params = get_format_str_for_mask(format_mask); |
| 847 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, format_params); |
| 848 | free(format_params); |
| 849 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 850 | } // AUDIO_PARAMETER_STREAM_SUP_FORMATS |
| 851 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 852 | pcm_params_free(alsa_hw_params); |
| 853 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 854 | result_str = str_parms_to_str(result); |
| 855 | |
| 856 | // done with these... |
| 857 | str_parms_destroy(query); |
| 858 | str_parms_destroy(result); |
| 859 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 860 | ALOGV("usb:audio_hw::device_get_parameters = %s", result_str); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 861 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 862 | return result_str; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 865 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 866 | { |
| 867 | ALOGV("usb:audio_hw::out out_get_parameters() keys:%s", keys); |
| 868 | |
| 869 | struct stream_out *out = (struct stream_out *) stream; |
| 870 | pthread_mutex_lock(&out->dev->lock); |
| 871 | pthread_mutex_lock(&out->lock); |
| 872 | |
| 873 | char * params_str = device_get_parameters(out->profile, keys); |
| 874 | |
| 875 | pthread_mutex_unlock(&out->lock); |
| 876 | pthread_mutex_unlock(&out->dev->lock); |
| 877 | |
| 878 | return params_str; |
| 879 | } |
| 880 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 881 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 882 | { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 883 | // struct stream_out *out = (struct stream_out *) stream; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 884 | |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 885 | /*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] | 886 | uint32_t latency = (cached_output_hardware_config.period_size |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 887 | * cached_output_hardware_config.period_count * 1000) |
| 888 | / out_get_sample_rate(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 889 | return latency; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 892 | 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] | 893 | { |
| 894 | return -ENOSYS; |
| 895 | } |
| 896 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 897 | /* must be called with hw device and output stream mutexes locked */ |
| 898 | static int start_output_stream(struct stream_out *out) |
| 899 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 900 | int return_val = 0; |
| 901 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 902 | ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)", |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 903 | out->profile->card, out->profile->device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 904 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 905 | out->pcm = pcm_open(out->profile->card, out->profile->device, PCM_OUT, |
| 906 | &cached_output_hardware_config); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 907 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 908 | if (out->pcm == NULL) { |
| 909 | return -ENOMEM; |
| 910 | } |
| 911 | |
| 912 | if (out->pcm && !pcm_is_ready(out->pcm)) { |
| 913 | ALOGE("audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(out->pcm)); |
| 914 | pcm_close(out->pcm); |
| 915 | return -ENOMEM; |
| 916 | } |
| 917 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | 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] | 922 | { |
| 923 | int ret; |
| 924 | struct stream_out *out = (struct stream_out *)stream; |
| 925 | |
| 926 | pthread_mutex_lock(&out->dev->lock); |
| 927 | pthread_mutex_lock(&out->lock); |
| 928 | if (out->standby) { |
| 929 | ret = start_output_stream(out); |
| 930 | if (ret != 0) { |
| 931 | goto err; |
| 932 | } |
| 933 | out->standby = false; |
| 934 | } |
| 935 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 936 | // Setup conversion buffer |
| 937 | // compute maximum potential buffer size. |
| 938 | // * 2 for stereo -> quad conversion |
| 939 | // * 3/2 for 16bit -> 24 bit conversion |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 940 | size_t required_conversion_buffer_size = (bytes * 3 * 2) / 2; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 941 | if (required_conversion_buffer_size > out->conversion_buffer_size) { |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 942 | /* TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 943 | (and do these conversions themselves) */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 944 | out->conversion_buffer_size = required_conversion_buffer_size; |
| 945 | out->conversion_buffer = realloc(out->conversion_buffer, out->conversion_buffer_size); |
| 946 | } |
| 947 | |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 948 | const void * write_buff = buffer; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 949 | int num_write_buff_bytes = bytes; |
| 950 | |
| 951 | /* |
| 952 | * Num Channels conversion |
| 953 | */ |
| 954 | int num_device_channels = cached_output_hardware_config.channels; |
| 955 | int num_req_channels = 2; /* always, for now */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 956 | if (num_device_channels != num_req_channels) { |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 957 | audio_format_t audio_format = out_get_format(&(out->stream.common)); |
| 958 | unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 959 | num_write_buff_bytes = |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 960 | adjust_channels(write_buff, num_req_channels, |
| 961 | out->conversion_buffer, num_device_channels, |
| 962 | sample_size_in_bytes, num_write_buff_bytes); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 963 | write_buff = out->conversion_buffer; |
| 964 | } |
| 965 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 966 | if (write_buff != NULL && num_write_buff_bytes != 0) { |
| 967 | pcm_write(out->pcm, write_buff, num_write_buff_bytes); |
| 968 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 969 | |
| 970 | pthread_mutex_unlock(&out->lock); |
| 971 | pthread_mutex_unlock(&out->dev->lock); |
| 972 | |
| 973 | return bytes; |
| 974 | |
| 975 | err: |
| 976 | pthread_mutex_unlock(&out->lock); |
Amit Shekhar | f9953b7 | 2014-01-30 12:47:34 -0800 | [diff] [blame] | 977 | pthread_mutex_unlock(&out->dev->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 978 | if (ret != 0) { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 979 | usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 980 | out_get_sample_rate(&stream->common)); |
| 981 | } |
| 982 | |
| 983 | return bytes; |
| 984 | } |
| 985 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 986 | 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] | 987 | { |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | |
| 991 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 992 | { |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 997 | { |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1001 | 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] | 1002 | { |
| 1003 | return -EINVAL; |
| 1004 | } |
| 1005 | |
| 1006 | static int adev_open_output_stream(struct audio_hw_device *dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1007 | audio_io_handle_t handle, |
| 1008 | audio_devices_t devices, |
| 1009 | audio_output_flags_t flags, |
| 1010 | struct audio_config *config, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1011 | struct audio_stream_out **stream_out) |
| 1012 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1013 | 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] | 1014 | handle, devices, flags); |
| 1015 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1016 | struct audio_device *adev = (struct audio_device *)dev; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1017 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1018 | struct stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1019 | |
| 1020 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 1021 | if (!out) |
| 1022 | return -ENOMEM; |
| 1023 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1024 | // setup function pointers |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1025 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 1026 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 1027 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 1028 | out->stream.common.get_channels = out_get_channels; |
| 1029 | out->stream.common.get_format = out_get_format; |
| 1030 | out->stream.common.set_format = out_set_format; |
| 1031 | out->stream.common.standby = out_standby; |
| 1032 | out->stream.common.dump = out_dump; |
| 1033 | out->stream.common.set_parameters = out_set_parameters; |
| 1034 | out->stream.common.get_parameters = out_get_parameters; |
| 1035 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 1036 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 1037 | out->stream.get_latency = out_get_latency; |
| 1038 | out->stream.set_volume = out_set_volume; |
| 1039 | out->stream.write = out_write; |
| 1040 | out->stream.get_render_position = out_get_render_position; |
| 1041 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 1042 | |
| 1043 | out->dev = adev; |
| 1044 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1045 | out->profile = &(adev->out_profile); |
| 1046 | out->profile->direction = PCM_OUT; |
| 1047 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1048 | if (output_hardware_config_is_cached) { |
| 1049 | config->sample_rate = cached_output_hardware_config.rate; |
| 1050 | |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 1051 | config->format = audio_format_from_pcm_format(cached_output_hardware_config.format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1052 | |
| 1053 | config->channel_mask = |
| 1054 | audio_channel_out_mask_from_count(cached_output_hardware_config.channels); |
| 1055 | if (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) { |
| 1056 | // Always report STEREO for now. AudioPolicyManagerBase/AudioFlinger dont' understand |
| 1057 | // 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] | 1058 | /*TODO remove this when the above restriction is removed. */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1059 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1060 | } |
| 1061 | } else { |
| 1062 | cached_output_hardware_config = default_alsa_out_config; |
| 1063 | |
| 1064 | config->format = out_get_format(&out->stream.common); |
| 1065 | config->channel_mask = out_get_channels(&out->stream.common); |
| 1066 | config->sample_rate = out_get_sample_rate(&out->stream.common); |
| 1067 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1068 | |
| 1069 | out->conversion_buffer = NULL; |
| 1070 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1071 | |
| 1072 | out->standby = true; |
| 1073 | |
| 1074 | *stream_out = &out->stream; |
| 1075 | return 0; |
| 1076 | |
| 1077 | err_open: |
| 1078 | free(out); |
| 1079 | *stream_out = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1080 | return -ENOSYS; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 1084 | struct audio_stream_out *stream) |
| 1085 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1086 | ALOGV("usb:audio_hw::out adev_close_output_stream()"); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1087 | struct stream_out *out = (struct stream_out *)stream; |
| 1088 | |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 1089 | // Close the pcm device |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1090 | out_standby(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1091 | |
| 1092 | free(out->conversion_buffer); |
| 1093 | out->conversion_buffer = NULL; |
| 1094 | out->conversion_buffer_size = 0; |
| 1095 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1096 | free(stream); |
| 1097 | } |
| 1098 | |
| 1099 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 1100 | { |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1104 | 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] | 1105 | { |
| 1106 | return strdup(""); |
| 1107 | } |
| 1108 | |
| 1109 | static int adev_init_check(const struct audio_hw_device *dev) |
| 1110 | { |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 1115 | { |
| 1116 | return -ENOSYS; |
| 1117 | } |
| 1118 | |
| 1119 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 1120 | { |
| 1121 | return -ENOSYS; |
| 1122 | } |
| 1123 | |
| 1124 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 1125 | { |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 1130 | { |
| 1131 | return -ENOSYS; |
| 1132 | } |
| 1133 | |
| 1134 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 1135 | { |
| 1136 | return -ENOSYS; |
| 1137 | } |
| 1138 | |
| 1139 | 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] | 1140 | const struct audio_config *config) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1141 | { |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1145 | /* Helper functions */ |
| 1146 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 1147 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1148 | return cached_input_hardware_config.rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 1152 | { |
| 1153 | return -ENOSYS; |
| 1154 | } |
| 1155 | |
| 1156 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 1157 | { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 1158 | size_t buffer_size = cached_input_hardware_config.period_size * |
| 1159 | audio_stream_in_frame_size((const struct audio_stream_in *)stream); |
| 1160 | ALOGV("usb: in_get_buffer_size() = %zu", buffer_size); |
| 1161 | return buffer_size; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 1165 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1166 | // just report stereo for now |
| 1167 | return AUDIO_CHANNEL_IN_STEREO; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 1171 | { |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1172 | const struct stream_in * in_stream = (const struct stream_in *)stream; |
| 1173 | |
| 1174 | ALOGV("in_get_format() = %d -> %d", in_stream->input_framework_format, |
| 1175 | audio_format_from_pcm_format(in_stream->input_framework_format)); |
| 1176 | /* return audio_format_from_pcm_format(cached_input_hardware_config.format); */ |
| 1177 | return audio_format_from_pcm_format(in_stream->input_framework_format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 1181 | { |
| 1182 | return -ENOSYS; |
| 1183 | } |
| 1184 | |
| 1185 | static int in_standby(struct audio_stream *stream) |
| 1186 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1187 | struct stream_in *in = (struct stream_in *) stream; |
| 1188 | |
| 1189 | pthread_mutex_lock(&in->dev->lock); |
| 1190 | pthread_mutex_lock(&in->lock); |
| 1191 | |
| 1192 | if (!in->standby) { |
| 1193 | pcm_close(in->pcm); |
| 1194 | in->pcm = NULL; |
| 1195 | in->standby = true; |
| 1196 | } |
| 1197 | |
| 1198 | pthread_mutex_unlock(&in->lock); |
| 1199 | pthread_mutex_unlock(&in->dev->lock); |
| 1200 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | static int in_dump(const struct audio_stream *stream, int fd) |
| 1205 | { |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 1210 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1211 | ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1212 | |
| 1213 | struct stream_in *in = (struct stream_in *)stream; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1214 | struct str_parms *parms; |
| 1215 | char value[32]; |
| 1216 | int param_val; |
| 1217 | int routing = 0; |
| 1218 | int ret_value = 0; |
| 1219 | |
| 1220 | parms = str_parms_create_str(kvpairs); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1221 | pthread_mutex_lock(&in->dev->lock); |
| 1222 | pthread_mutex_lock(&in->lock); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1223 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1224 | bool recache_device_params = false; |
| 1225 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1226 | // Card/Device |
| 1227 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 1228 | if (param_val >= 0) { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1229 | in->profile->card = atoi(value); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1230 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 1234 | if (param_val >= 0) { |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1235 | in->profile->device = atoi(value); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1236 | recache_device_params = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1239 | if (recache_device_params && in->profile->card >= 0 && in->profile->device >= 0) { |
| 1240 | ret_value = read_alsa_device_config(in->profile, &cached_input_hardware_config); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1241 | input_hardware_config_is_cached = (ret_value == 0); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1244 | pthread_mutex_unlock(&in->lock); |
| 1245 | pthread_mutex_unlock(&in->dev->lock); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1246 | str_parms_destroy(parms); |
| 1247 | |
| 1248 | return ret_value; |
| 1249 | } |
| 1250 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1251 | static char * in_get_parameters(const struct audio_stream *stream, const char *keys) { |
| 1252 | ALOGV("usb:audio_hw::in in_get_parameters() keys:%s", keys); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1253 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1254 | struct stream_in *in = (struct stream_in *)stream; |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1255 | pthread_mutex_lock(&in->dev->lock); |
| 1256 | pthread_mutex_lock(&in->lock); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1257 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1258 | char * params_str = device_get_parameters(in->profile, keys); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1259 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1260 | pthread_mutex_unlock(&in->lock); |
| 1261 | pthread_mutex_unlock(&in->dev->lock); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1262 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1263 | return params_str; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1267 | { |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1272 | { |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1276 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 1277 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1278 | return 0; |
| 1279 | } |
| 1280 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1281 | /* must be called with hw device and output stream mutexes locked */ |
| 1282 | static int start_input_stream(struct stream_in *in) { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1283 | int return_val = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1284 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1285 | ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)", |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1286 | in->profile->card, in->profile->device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1287 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1288 | in->pcm = pcm_open(in->profile->card, in->profile->device, PCM_IN, |
| 1289 | &cached_input_hardware_config); |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1290 | if (in->pcm == NULL) { |
| 1291 | ALOGE("usb:audio_hw pcm_open() in->pcm == NULL"); |
| 1292 | return -ENOMEM; |
| 1293 | } |
| 1294 | |
| 1295 | if (in->pcm && !pcm_is_ready(in->pcm)) { |
| 1296 | ALOGE("usb:audio_hw audio_hw pcm_open() failed: %s", pcm_get_error(in->pcm)); |
| 1297 | pcm_close(in->pcm); |
| 1298 | return -ENOMEM; |
| 1299 | } |
| 1300 | |
| 1301 | return 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1302 | } |
| 1303 | |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 1304 | /* TODO mutex stuff here (see out_write) */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1305 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) |
| 1306 | { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1307 | size_t num_read_buff_bytes = 0; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1308 | void * read_buff = buffer; |
| 1309 | void * out_buff = buffer; |
| 1310 | |
| 1311 | struct stream_in * in = (struct stream_in *) stream; |
| 1312 | |
| 1313 | pthread_mutex_lock(&in->dev->lock); |
| 1314 | pthread_mutex_lock(&in->lock); |
| 1315 | |
| 1316 | if (in->standby) { |
| 1317 | if (start_input_stream(in) != 0) { |
| 1318 | goto err; |
| 1319 | } |
| 1320 | in->standby = false; |
| 1321 | } |
| 1322 | |
| 1323 | // OK, we need to figure out how much data to read to be able to output the requested |
| 1324 | // number of bytes in the HAL format (16-bit, stereo). |
| 1325 | num_read_buff_bytes = bytes; |
| 1326 | int num_device_channels = cached_input_hardware_config.channels; |
| 1327 | int num_req_channels = 2; /* always, for now */ |
| 1328 | |
| 1329 | if (num_device_channels != num_req_channels) { |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 1330 | 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] | 1331 | } |
| 1332 | |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1333 | /* Assume (for now) that in->input_framework_format == PCM_FORMAT_S16_LE */ |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1334 | if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) { |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1335 | /* 24-bit USB device */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1336 | num_read_buff_bytes = (3 * num_read_buff_bytes) / 2; |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1337 | } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) { |
| 1338 | /* 32-bit USB device */ |
| 1339 | num_read_buff_bytes = num_read_buff_bytes * 2; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | // Setup/Realloc the conversion buffer (if necessary). |
| 1343 | if (num_read_buff_bytes != bytes) { |
| 1344 | if (num_read_buff_bytes > in->conversion_buffer_size) { |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 1345 | /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 1346 | (and do these conversions themselves) */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1347 | in->conversion_buffer_size = num_read_buff_bytes; |
| 1348 | in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); |
| 1349 | } |
| 1350 | read_buff = in->conversion_buffer; |
| 1351 | } |
| 1352 | |
| 1353 | if (pcm_read(in->pcm, read_buff, num_read_buff_bytes) == 0) { |
| 1354 | /* |
| 1355 | * Do any conversions necessary to send the data in the format specified to/by the HAL |
| 1356 | * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan. |
| 1357 | */ |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1358 | if (cached_input_hardware_config.format != PCM_FORMAT_S16_LE) { |
| 1359 | // we need to convert |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1360 | if (num_device_channels != num_req_channels) { |
| 1361 | out_buff = read_buff; |
| 1362 | } |
| 1363 | |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1364 | if (cached_input_hardware_config.format == PCM_FORMAT_S24_3LE) { |
| 1365 | num_read_buff_bytes = |
| 1366 | convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff); |
| 1367 | } else if (cached_input_hardware_config.format == PCM_FORMAT_S32_LE) { |
| 1368 | num_read_buff_bytes = |
| 1369 | convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff); |
| 1370 | } |
| 1371 | else { |
| 1372 | goto err; |
| 1373 | } |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | if (num_device_channels != num_req_channels) { |
| 1377 | out_buff = buffer; |
| 1378 | /* Num Channels conversion */ |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 1379 | if (num_device_channels != num_req_channels) { |
| 1380 | audio_format_t audio_format = in_get_format(&(in->stream.common)); |
| 1381 | unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
| 1382 | |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 1383 | num_read_buff_bytes = |
Paul McLean | eb19297 | 2014-07-11 16:29:41 -0700 | [diff] [blame] | 1384 | adjust_channels(read_buff, num_device_channels, |
| 1385 | out_buff, num_req_channels, |
| 1386 | sample_size_in_bytes, num_read_buff_bytes); |
Paul McLean | cf61191 | 2014-04-28 13:03:18 -0700 | [diff] [blame] | 1387 | } |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | err: |
| 1392 | pthread_mutex_unlock(&in->lock); |
| 1393 | pthread_mutex_unlock(&in->dev->lock); |
| 1394 | |
| 1395 | return num_read_buff_bytes; |
| 1396 | } |
| 1397 | |
| 1398 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1399 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1403 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1404 | audio_io_handle_t handle, |
| 1405 | audio_devices_t devices, |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1406 | struct audio_config *config, |
Glenn Kasten | 7d973ad | 2014-07-15 11:10:38 -0700 | [diff] [blame] | 1407 | struct audio_stream_in **stream_in, |
| 1408 | audio_input_flags_t flags __unused) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1409 | { |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 1410 | 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] | 1411 | config->sample_rate, config->channel_mask, config->format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1412 | |
| 1413 | 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] | 1414 | int ret = 0; |
| 1415 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1416 | if (in == NULL) |
| 1417 | return -ENOMEM; |
| 1418 | |
| 1419 | // setup function pointers |
| 1420 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1421 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1422 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1423 | in->stream.common.get_channels = in_get_channels; |
| 1424 | in->stream.common.get_format = in_get_format; |
| 1425 | in->stream.common.set_format = in_set_format; |
| 1426 | in->stream.common.standby = in_standby; |
| 1427 | in->stream.common.dump = in_dump; |
| 1428 | in->stream.common.set_parameters = in_set_parameters; |
| 1429 | in->stream.common.get_parameters = in_get_parameters; |
| 1430 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1431 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1432 | |
| 1433 | in->stream.set_gain = in_set_gain; |
| 1434 | in->stream.read = in_read; |
| 1435 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 1436 | |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1437 | in->input_framework_format = PCM_FORMAT_S16_LE; |
| 1438 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1439 | in->dev = (struct audio_device *)dev; |
| 1440 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 1441 | in->profile = &(in->dev->in_profile); |
| 1442 | in->profile->direction = PCM_IN; |
| 1443 | |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1444 | if (!input_hardware_config_is_cached) { |
| 1445 | // just return defaults until we can actually query the device. |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1446 | cached_input_hardware_config = default_alsa_in_config; |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1447 | } |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1448 | |
Paul McLean | 6b1c0fe | 2014-07-02 07:27:41 -0700 | [diff] [blame] | 1449 | /* Rate */ |
| 1450 | /* TODO Check that the requested rate is valid for the connected device */ |
| 1451 | if (config->sample_rate == 0) { |
| 1452 | config->sample_rate = cached_input_hardware_config.rate; |
| 1453 | } else { |
| 1454 | cached_input_hardware_config.rate = config->sample_rate; |
| 1455 | } |
| 1456 | |
| 1457 | /* Format */ |
| 1458 | /* until the framework supports format conversion, just take what it asks for |
| 1459 | * i.e. AUDIO_FORMAT_PCM_16_BIT */ |
| 1460 | /* config->format = audio_format_from_pcm_format(cached_input_hardware_config.format); */ |
| 1461 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
| 1462 | /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input |
| 1463 | * formats */ |
| 1464 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 1465 | } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) { |
| 1466 | /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input |
| 1467 | * formats */ |
| 1468 | } else { |
| 1469 | /* When the framework support other formats, validate here */ |
| 1470 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 1471 | ret = -EINVAL; |
| 1472 | } |
| 1473 | |
| 1474 | /* don't change the cached_input_hardware_config, we will open it as what it is and |
| 1475 | * convert as necessary */ |
| 1476 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 1477 | /* just return AUDIO_CHANNEL_IN_STEREO until the framework supports other input |
| 1478 | * formats */ |
| 1479 | config->channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 1480 | } else if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) { |
| 1481 | /* allow only stereo capture for now */ |
| 1482 | config->channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 1483 | ret = -EINVAL; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1484 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1485 | |
| 1486 | in->standby = true; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1487 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1488 | in->conversion_buffer = NULL; |
| 1489 | in->conversion_buffer_size = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1490 | |
| 1491 | *stream_in = &in->stream; |
| 1492 | |
Eric Laurent | 7661a48 | 2014-06-11 12:00:16 -0700 | [diff] [blame] | 1493 | return ret; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1496 | 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] | 1497 | { |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1498 | struct stream_in *in = (struct stream_in *)stream; |
| 1499 | |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 1500 | // Close the pcm device |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1501 | in_standby(&stream->common); |
| 1502 | |
| 1503 | free(in->conversion_buffer); |
| 1504 | |
| 1505 | free(stream); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1509 | { |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | static int adev_close(hw_device_t *device) |
| 1514 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1515 | struct audio_device *adev = (struct audio_device *)device; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1516 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1517 | |
| 1518 | output_hardware_config_is_cached = false; |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1519 | input_hardware_config_is_cached = false; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1520 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1521 | return 0; |
| 1522 | } |
| 1523 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1524 | 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] | 1525 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1526 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1527 | return -EINVAL; |
| 1528 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1529 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1530 | if (!adev) |
| 1531 | return -ENOMEM; |
| 1532 | |
| 1533 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 1534 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1535 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 1536 | adev->hw_device.common.close = adev_close; |
| 1537 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1538 | adev->hw_device.init_check = adev_init_check; |
| 1539 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1540 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1541 | adev->hw_device.set_mode = adev_set_mode; |
| 1542 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1543 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1544 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1545 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1546 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1547 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1548 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1549 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1550 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1551 | adev->hw_device.dump = adev_dump; |
| 1552 | |
| 1553 | *device = &adev->hw_device.common; |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | static struct hw_module_methods_t hal_module_methods = { |
| 1559 | .open = adev_open, |
| 1560 | }; |
| 1561 | |
| 1562 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1563 | .common = { |
| 1564 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1565 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1566 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1567 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1568 | .name = "USB audio HW HAL", |
| 1569 | .author = "The Android Open Source Project", |
| 1570 | .methods = &hal_module_methods, |
| 1571 | }, |
| 1572 | }; |