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