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