Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "alsa_device_profile" |
| 18 | /*#define LOG_NDEBUG 0*/ |
| 19 | /*#define LOG_PCM_PARAMS 0*/ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #include <log/log.h> |
| 27 | |
| 28 | #include "alsa_device_profile.h" |
| 29 | #include "format.h" |
| 30 | #include "logging.h" |
| 31 | |
| 32 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 33 | |
| 34 | /*#define ENABLE_MULTI_CHANNEL 1*/ |
| 35 | |
| 36 | /*TODO - Evaluate if this value should/can be retrieved from a device-specific property */ |
| 37 | #define BUFF_DURATION_MS 5 |
| 38 | |
| 39 | #define DEFAULT_PERIOD_SIZE 1024 |
| 40 | |
| 41 | static const char * const format_string_map[] = { |
| 42 | "AUDIO_FORMAT_PCM_16_BIT", /* "PCM_FORMAT_S16_LE", */ |
| 43 | "AUDIO_FORMAT_PCM_32_BIT", /* "PCM_FORMAT_S32_LE", */ |
| 44 | "AUDIO_FORMAT_PCM_8_BIT", /* "PCM_FORMAT_S8", */ |
| 45 | "AUDIO_FORMAT_PCM_8_24_BIT", /* "PCM_FORMAT_S24_LE", */ |
| 46 | "AUDIO_FORMAT_PCM_24_BIT_PACKED"/* "PCM_FORMAT_S24_3LE" */ |
| 47 | }; |
| 48 | |
| 49 | static const unsigned const format_byte_size_map[] = { |
| 50 | 2, /* PCM_FORMAT_S16_LE */ |
| 51 | 4, /* PCM_FORMAT_S32_LE */ |
| 52 | 1, /* PCM_FORMAT_S8 */ |
| 53 | 4, /* PCM_FORMAT_S24_LE */ |
| 54 | 3, /* PCM_FORMAT_S24_3LE */ |
| 55 | }; |
| 56 | |
| 57 | extern int8_t const pcm_format_value_map[50]; |
| 58 | |
| 59 | /* sort these highest -> lowest (to default to best quality) */ |
| 60 | static const unsigned std_sample_rates[] = |
| 61 | {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000}; |
| 62 | |
| 63 | void profile_init(alsa_device_profile* profile, int direction) |
| 64 | { |
| 65 | profile->card = profile->device = -1; |
| 66 | profile->direction = direction; |
| 67 | |
| 68 | /* Fill the attribute arrays with invalid values */ |
| 69 | size_t index; |
| 70 | for (index = 0; index < ARRAY_SIZE(profile->formats); index++) { |
| 71 | profile->formats[index] = PCM_FORMAT_INVALID; |
| 72 | } |
| 73 | |
| 74 | for (index = 0; index < ARRAY_SIZE(profile->sample_rates); index++) { |
| 75 | profile->sample_rates[index] = 0; |
| 76 | } |
| 77 | |
| 78 | for (index = 0; index < ARRAY_SIZE(profile->channel_counts); index++) { |
| 79 | profile->channel_counts[index] = 0; |
| 80 | } |
| 81 | |
| 82 | profile->min_period_size = profile->max_period_size = 0; |
| 83 | profile->min_channel_count = profile->max_channel_count = DEFAULT_CHANNEL_COUNT; |
| 84 | |
| 85 | profile->is_valid = false; |
| 86 | } |
| 87 | |
| 88 | bool profile_is_initialized(alsa_device_profile* profile) |
| 89 | { |
| 90 | return profile->card >= 0 && profile->device >= 0; |
| 91 | } |
| 92 | |
| 93 | bool profile_is_valid(alsa_device_profile* profile) { |
| 94 | return profile->is_valid; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Returns the supplied value rounded up to the next even multiple of 16 |
| 99 | */ |
| 100 | static unsigned int round_to_16_mult(unsigned int size) |
| 101 | { |
| 102 | return (size + 15) & ~15; // 0xFFFFFFF0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Returns the system defined minimum period size based on the supplied sample rate. |
| 107 | */ |
| 108 | unsigned profile_calc_min_period_size(alsa_device_profile* profile, unsigned sample_rate) |
| 109 | { |
| 110 | ALOGV("profile_calc_min_period_size(%p, rate:%d)", profile, sample_rate); |
| 111 | if (profile == NULL) { |
| 112 | return DEFAULT_PERIOD_SIZE; |
| 113 | } else { |
| 114 | unsigned num_sample_frames = (sample_rate * BUFF_DURATION_MS) / 1000; |
| 115 | if (num_sample_frames < profile->min_period_size) { |
| 116 | num_sample_frames = profile->min_period_size; |
| 117 | } |
| 118 | return round_to_16_mult(num_sample_frames) * 2; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | unsigned int profile_get_period_size(alsa_device_profile* profile, unsigned sample_rate) |
| 123 | { |
| 124 | // return profile->default_config.period_size; |
| 125 | unsigned int period_size = profile_calc_min_period_size(profile, sample_rate); |
| 126 | ALOGV("profile_get_period_size(rate:%d) = %d", sample_rate, period_size); |
| 127 | return period_size; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Sample Rate |
| 132 | */ |
| 133 | unsigned profile_get_default_sample_rate(alsa_device_profile* profile) |
| 134 | { |
| 135 | /* |
| 136 | * TODO this won't be right in general. we should store a preferred rate as we are scanning. |
| 137 | * But right now it will return the highest rate, which may be correct. |
| 138 | */ |
| 139 | return profile_is_valid(profile) ? profile->sample_rates[0] : DEFAULT_SAMPLE_RATE; |
| 140 | } |
| 141 | |
| 142 | bool profile_is_sample_rate_valid(alsa_device_profile* profile, unsigned rate) |
| 143 | { |
| 144 | if (profile_is_valid(profile)) { |
| 145 | size_t index; |
| 146 | for (index = 0; profile->sample_rates[index] != 0; index++) { |
| 147 | if (profile->sample_rates[index] == rate) { |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } else { |
| 154 | return rate == DEFAULT_SAMPLE_RATE; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Format |
| 160 | */ |
| 161 | enum pcm_format profile_get_default_format(alsa_device_profile* profile) |
| 162 | { |
| 163 | /* |
| 164 | * TODO this won't be right in general. we should store a preferred format as we are scanning. |
| 165 | */ |
| 166 | return profile_is_valid(profile) ? profile->formats[0] : DEFAULT_SAMPLE_FORMAT; |
| 167 | } |
| 168 | |
| 169 | bool profile_is_format_valid(alsa_device_profile* profile, enum pcm_format fmt) { |
| 170 | if (profile_is_valid(profile)) { |
| 171 | size_t index; |
| 172 | for (index = 0; profile->formats[index] != PCM_FORMAT_INVALID; index++) { |
| 173 | if (profile->formats[index] == fmt) { |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } else { |
| 180 | return fmt == DEFAULT_SAMPLE_FORMAT; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Channels |
| 186 | */ |
| 187 | unsigned profile_get_default_channel_count(alsa_device_profile* profile) |
| 188 | { |
| 189 | return profile_is_valid(profile) ? profile->channel_counts[0] : DEFAULT_CHANNEL_COUNT; |
| 190 | } |
| 191 | |
| 192 | bool profile_is_channel_count_valid(alsa_device_profile* profile, unsigned count) |
| 193 | { |
| 194 | if (profile_is_initialized(profile)) { |
| 195 | return count >= profile->min_channel_count && count <= profile->max_channel_count; |
| 196 | } else { |
| 197 | return count == DEFAULT_CHANNEL_COUNT; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static bool profile_test_sample_rate(alsa_device_profile* profile, unsigned rate) |
| 202 | { |
| 203 | struct pcm_config config = profile->default_config; |
| 204 | config.rate = rate; |
| 205 | |
| 206 | bool works = false; /* let's be pessimistic */ |
| 207 | struct pcm * pcm = pcm_open(profile->card, profile->device, |
| 208 | profile->direction, &config); |
| 209 | |
| 210 | if (pcm != NULL) { |
| 211 | works = pcm_is_ready(pcm); |
| 212 | pcm_close(pcm); |
| 213 | } |
| 214 | |
| 215 | return works; |
| 216 | } |
| 217 | |
| 218 | static unsigned profile_enum_sample_rates(alsa_device_profile* profile, unsigned min, unsigned max) |
| 219 | { |
| 220 | unsigned num_entries = 0; |
| 221 | unsigned index; |
| 222 | |
| 223 | for (index = 0; index < ARRAY_SIZE(std_sample_rates) && |
| 224 | num_entries < ARRAY_SIZE(profile->sample_rates) - 1; |
| 225 | index++) { |
| 226 | if (std_sample_rates[index] >= min && std_sample_rates[index] <= max |
| 227 | && profile_test_sample_rate(profile, std_sample_rates[index])) { |
| 228 | profile->sample_rates[num_entries++] = std_sample_rates[index]; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return num_entries; /* return # of supported rates */ |
| 233 | } |
| 234 | |
| 235 | static unsigned profile_enum_sample_formats(alsa_device_profile* profile, struct pcm_mask * mask) |
| 236 | { |
| 237 | const int num_slots = ARRAY_SIZE(mask->bits); |
| 238 | const int bits_per_slot = sizeof(mask->bits[0]) * 8; |
| 239 | |
| 240 | const int table_size = ARRAY_SIZE(pcm_format_value_map); |
| 241 | |
| 242 | int slot_index, bit_index, table_index; |
| 243 | table_index = 0; |
| 244 | int num_written = 0; |
| 245 | for (slot_index = 0; slot_index < num_slots && table_index < table_size; |
| 246 | slot_index++) { |
| 247 | unsigned bit_mask = 1; |
| 248 | for (bit_index = 0; |
| 249 | bit_index < bits_per_slot && table_index < table_size; |
| 250 | bit_index++) { |
| 251 | if ((mask->bits[slot_index] & bit_mask) != 0) { |
| 252 | enum pcm_format format = pcm_format_value_map[table_index]; |
| 253 | /* Never return invalid (unrecognized) or 8-bit */ |
| 254 | if (format != PCM_FORMAT_INVALID && format != PCM_FORMAT_S8) { |
| 255 | profile->formats[num_written++] = format; |
| 256 | if (num_written == ARRAY_SIZE(profile->formats) - 1) { |
| 257 | /* leave at least one PCM_FORMAT_INVALID at the end */ |
| 258 | return num_written; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | bit_mask <<= 1; |
| 263 | table_index++; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return num_written; |
| 268 | } |
| 269 | |
| 270 | static unsigned profile_enum_channel_counts(alsa_device_profile* profile, unsigned min, unsigned max) |
| 271 | { |
| 272 | static const unsigned std_channel_counts[] = {8, 4, 2, 1}; |
| 273 | |
| 274 | unsigned num_counts = 0; |
| 275 | unsigned index; |
| 276 | /* TODO write a profile_test_channel_count() */ |
| 277 | /* Ensure there is at least one invalid channel count to terminate the channel counts array */ |
| 278 | for (index = 0; index < ARRAY_SIZE(std_channel_counts) && |
| 279 | num_counts < ARRAY_SIZE(profile->channel_counts) - 1; |
| 280 | index++) { |
| 281 | /* TODO Do we want a channel counts test? */ |
| 282 | if (std_channel_counts[index] >= min && std_channel_counts[index] <= max /* && |
| 283 | profile_test_channel_count(profile, std_channel_counts[index])*/) { |
| 284 | profile->channel_counts[num_counts++] = std_channel_counts[index]; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return num_counts; /* return # of supported counts */ |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Reads and decodes configuration info from the specified ALSA card/device. |
| 293 | */ |
| 294 | static int read_alsa_device_config(alsa_device_profile * profile, struct pcm_config * config) |
| 295 | { |
| 296 | ALOGV("usb:audio_hw - read_alsa_device_config(c:%d d:%d t:0x%X)", |
| 297 | profile->card, profile->device, profile->direction); |
| 298 | |
| 299 | if (profile->card < 0 || profile->device < 0) { |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | struct pcm_params * alsa_hw_params = |
| 304 | pcm_params_get(profile->card, profile->device, profile->direction); |
| 305 | if (alsa_hw_params == NULL) { |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | profile->min_period_size = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE); |
| 310 | profile->max_period_size = pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE); |
| 311 | |
| 312 | profile->min_channel_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 313 | profile->max_channel_count = pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 314 | |
| 315 | int ret = 0; |
| 316 | |
| 317 | /* |
| 318 | * This Logging will be useful when testing new USB devices. |
| 319 | */ |
| 320 | #ifdef LOG_PCM_PARAMS |
| 321 | log_pcm_params(alsa_hw_params); |
| 322 | #endif |
| 323 | |
| 324 | config->channels = pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS); |
| 325 | config->rate = pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE); |
| 326 | config->period_size = profile_calc_min_period_size(profile, config->rate); |
| 327 | config->period_count = pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS); |
| 328 | config->format = get_pcm_format_for_mask(pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT)); |
| 329 | #ifdef LOG_PCM_PARAMS |
| 330 | log_pcm_config(config, "read_alsa_device_config"); |
| 331 | #endif |
| 332 | if (config->format == PCM_FORMAT_INVALID) { |
| 333 | ret = -EINVAL; |
| 334 | } |
| 335 | |
| 336 | pcm_params_free(alsa_hw_params); |
| 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | bool profile_read_device_info(alsa_device_profile* profile) |
| 342 | { |
| 343 | if (!profile_is_initialized(profile)) { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | /* let's get some defaults */ |
| 348 | read_alsa_device_config(profile, &profile->default_config); |
| 349 | ALOGV("default_config chans:%d rate:%d format:%d count:%d size:%d", |
| 350 | profile->default_config.channels, profile->default_config.rate, |
| 351 | profile->default_config.format, profile->default_config.period_count, |
| 352 | profile->default_config.period_size); |
| 353 | |
| 354 | struct pcm_params * alsa_hw_params = pcm_params_get(profile->card, |
| 355 | profile->device, |
| 356 | profile->direction); |
| 357 | if (alsa_hw_params == NULL) { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | /* Formats */ |
| 362 | struct pcm_mask * format_mask = pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT); |
| 363 | profile_enum_sample_formats(profile, format_mask); |
| 364 | |
| 365 | /* Channels */ |
| 366 | profile_enum_channel_counts( |
| 367 | profile, pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS), |
| 368 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS)); |
| 369 | |
| 370 | /* Sample Rates */ |
| 371 | profile_enum_sample_rates( |
| 372 | profile, pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE), |
| 373 | pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE)); |
| 374 | |
| 375 | profile->is_valid = true; |
| 376 | |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | char * profile_get_sample_rate_strs(alsa_device_profile* profile) |
| 381 | { |
| 382 | char buffer[128]; |
| 383 | buffer[0] = '\0'; |
| 384 | int buffSize = ARRAY_SIZE(buffer); |
| 385 | |
| 386 | char numBuffer[32]; |
| 387 | |
| 388 | int numEntries = 0; |
| 389 | unsigned index; |
| 390 | for (index = 0; profile->sample_rates[index] != 0; index++) { |
| 391 | if (numEntries++ != 0) { |
| 392 | strncat(buffer, "|", buffSize); |
| 393 | } |
| 394 | snprintf(numBuffer, sizeof(numBuffer), "%u", profile->sample_rates[index]); |
| 395 | strncat(buffer, numBuffer, buffSize); |
| 396 | } |
| 397 | |
| 398 | return strdup(buffer); |
| 399 | } |
| 400 | |
| 401 | char * profile_get_format_strs(alsa_device_profile* profile) |
| 402 | { |
| 403 | /* TODO remove this hack when we have support for input in non PCM16 formats */ |
| 404 | if (profile->direction == PCM_IN) { |
| 405 | return strdup("AUDIO_FORMAT_PCM_16_BIT"); |
| 406 | } |
| 407 | |
| 408 | char buffer[128]; |
| 409 | buffer[0] = '\0'; |
| 410 | int buffSize = ARRAY_SIZE(buffer); |
| 411 | |
| 412 | int numEntries = 0; |
| 413 | unsigned index = 0; |
| 414 | for (index = 0; profile->formats[index] != PCM_FORMAT_INVALID; index++) { |
| 415 | if (numEntries++ != 0) { |
| 416 | strncat(buffer, "|", buffSize); |
| 417 | } |
| 418 | strncat(buffer, format_string_map[profile->formats[index]], buffSize); |
| 419 | } |
| 420 | |
| 421 | return strdup(buffer); |
| 422 | } |
| 423 | |
| 424 | char * profile_get_channel_count_strs(alsa_device_profile* profile) |
| 425 | { |
| 426 | #ifndef ENABLE_MULTI_CHANNEL |
| 427 | /* TODO remove this hack when it is superceeded by proper multi-channel support */ |
| 428 | return strdup( |
| 429 | profile->direction == PCM_OUT ? |
| 430 | "AUDIO_CHANNEL_OUT_STEREO" : "AUDIO_CHANNEL_IN_STEREO"); |
| 431 | #else |
| 432 | /* |
| 433 | * Maps from a channel-count (the table index), to a corresponding AUDIO_CHANNEL_OUT_ string. |
| 434 | * (see audio_channel_out_mask_from_count() in audio.h. |
| 435 | */ |
| 436 | static const char * const out_chans_strs[] = { |
| 437 | /* 0 */"AUDIO_CHANNEL_NONE", |
| 438 | /* 1 */"AUDIO_CHANNEL_OUT_MONO", |
| 439 | /* 2 */"AUDIO_CHANNEL_OUT_STEREO", |
| 440 | /* 3 */"AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_FRONT_CENTER", |
| 441 | /* 4 */"AUDIO_CHANNEL_OUT_QUAD", |
| 442 | /* 5 */"AUDIO_CHANNEL_OUT_QUAD|AUDIO_CHANNEL_OUT_FRONT_CENTER", |
| 443 | /* 6 */"AUDIO_CHANNEL_OUT_5POINT1", |
| 444 | /* 7 */"AUDIO_CHANNEL_OUT_5POINT1|AUDIO_CHANNEL_OUT_BACK_CENTER", |
| 445 | /* 8 */"AUDIO_CHANNEL_OUT_7POINT1" |
| 446 | }; |
| 447 | |
| 448 | /* |
| 449 | * Maps from a channel-count (the table index), to a corresponding AUDIO_CHANNEL_IN_ string. |
| 450 | * (see audio_channel_in_mask_from_count() in audio.h. |
| 451 | */ |
| 452 | static const char * const in_chans_strs[] = |
| 453 | { |
| 454 | /* 0 */"AUDIO_CHANNEL_NONE", |
| 455 | /* 1 */"AUDIO_CHANNEL_IN_MONO", |
| 456 | /* 2 */"AUDIO_CHANNEL_IN_STEREO", |
| 457 | /* 3 */"", |
| 458 | /* 4 */ |
| 459 | "AUDIO_CHANNEL_IN_LEFT|AUDIO_CHANNEL_IN_RIGHT|AUDIO_CHANNEL_IN_FRONT|AUDIO_CHANNEL_IN_BACK" |
| 460 | }; |
| 461 | |
| 462 | char buffer[256]; |
| 463 | buffer[0] = '\0'; |
| 464 | int buffSize = ARRAY_SIZE(buffer); |
| 465 | |
| 466 | int numEntries = 0; |
| 467 | unsigned index = 0; |
| 468 | for (index = 0; profile->channel_counts[index] != 0; index++) { |
| 469 | if (numEntries++ != 0) { |
| 470 | strncat(buffer, "|", buffSize); |
| 471 | } |
| 472 | if (profile->direction == PCM_OUT) { |
| 473 | strncat(buffer, out_chans_strs[profile->channel_counts[index]], buffSize); |
| 474 | } else { |
| 475 | strncat(buffer, in_chans_strs[profile->channel_counts[index]], buffSize); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return strdup(buffer); |
| 480 | #endif |
| 481 | } |