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 "modules.usbaudio.audio_hal" 18 /*#define LOG_NDEBUG 0*/ 19 20 #include <errno.h> 21 #include <inttypes.h> 22 #include <pthread.h> 23 #include <stdint.h> 24 #include <stdlib.h> 25 #include <sys/time.h> 26 27 #include <log/log.h> 28 #include <cutils/list.h> 29 #include <cutils/str_parms.h> 30 #include <cutils/properties.h> 31 32 #include <hardware/audio.h> 33 #include <hardware/audio_alsaops.h> 34 #include <hardware/hardware.h> 35 36 #include <system/audio.h> 37 38 #include <tinyalsa/asoundlib.h> 39 40 #include <audio_utils/channels.h> 41 42 #include "alsa_device_profile.h" 43 #include "alsa_device_proxy.h" 44 #include "alsa_logging.h" 45 46 #define DEFAULT_INPUT_BUFFER_SIZE_MS 20 47 48 /* Lock play & record samples rates at or above this threshold */ 49 #define RATELOCK_THRESHOLD 96000 50 51 struct audio_device { 52 struct audio_hw_device hw_device; 53 54 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 55 56 /* output */ 57 alsa_device_profile out_profile; 58 struct listnode output_stream_list; 59 60 /* input */ 61 alsa_device_profile in_profile; 62 struct listnode input_stream_list; 63 64 /* lock input & output sample rates */ 65 /*FIXME - How do we address multiple output streams? */ 66 uint32_t device_sample_rate; 67 68 bool mic_muted; 69 70 bool standby; 71 }; 72 73 struct stream_lock { 74 pthread_mutex_t lock; /* see note below on mutex acquisition order */ 75 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */ 76 }; 77 78 struct stream_out { 79 struct audio_stream_out stream; 80 81 struct stream_lock lock; 82 83 bool standby; 84 85 struct audio_device *adev; /* hardware information - only using this for the lock */ 86 87 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */ 88 alsa_device_proxy proxy; /* state of the stream */ 89 90 unsigned hal_channel_count; /* channel count exposed to AudioFlinger. 91 * This may differ from the device channel count when 92 * the device is not compatible with AudioFlinger 93 * capabilities, e.g. exposes too many channels or 94 * too few channels. */ 95 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks 96 * so the proxy doesn't have a channel_mask, but 97 * audio HALs need to talk about channel masks 98 * so expose the one calculated by 99 * adev_open_output_stream */ 100 101 struct listnode list_node; 102 103 void * conversion_buffer; /* any conversions are put into here 104 * they could come from here too if 105 * there was a previous conversion */ 106 size_t conversion_buffer_size; /* in bytes */ 107 }; 108 109 struct stream_in { 110 struct audio_stream_in stream; 111 112 struct stream_lock lock; 113 114 bool standby; 115 116 struct audio_device *adev; /* hardware information - only using this for the lock */ 117 118 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */ 119 alsa_device_proxy proxy; /* state of the stream */ 120 121 unsigned hal_channel_count; /* channel count exposed to AudioFlinger. 122 * This may differ from the device channel count when 123 * the device is not compatible with AudioFlinger 124 * capabilities, e.g. exposes too many channels or 125 * too few channels. */ 126 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks 127 * so the proxy doesn't have a channel_mask, but 128 * audio HALs need to talk about channel masks 129 * so expose the one calculated by 130 * adev_open_input_stream */ 131 132 struct listnode list_node; 133 134 /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */ 135 void * conversion_buffer; /* any conversions are put into here 136 * they could come from here too if 137 * there was a previous conversion */ 138 size_t conversion_buffer_size; /* in bytes */ 139 }; 140 141 /* 142 * Locking Helpers 143 */ 144 /* 145 * NOTE: when multiple mutexes have to be acquired, always take the 146 * stream_in or stream_out mutex first, followed by the audio_device mutex. 147 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by 148 * higher priority playback or capture thread. 149 */ 150 151 static void stream_lock_init(struct stream_lock *lock) { 152 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL); 153 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL); 154 } 155 156 static void stream_lock(struct stream_lock *lock) { 157 pthread_mutex_lock(&lock->pre_lock); 158 pthread_mutex_lock(&lock->lock); 159 pthread_mutex_unlock(&lock->pre_lock); 160 } 161 162 static void stream_unlock(struct stream_lock *lock) { 163 pthread_mutex_unlock(&lock->lock); 164 } 165 166 static void device_lock(struct audio_device *adev) { 167 pthread_mutex_lock(&adev->lock); 168 } 169 170 static int device_try_lock(struct audio_device *adev) { 171 return pthread_mutex_trylock(&adev->lock); 172 } 173 174 static void device_unlock(struct audio_device *adev) { 175 pthread_mutex_unlock(&adev->lock); 176 } 177 178 /* 179 * streams list management 180 */ 181 static void adev_add_stream_to_list( 182 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) { 183 device_lock(adev); 184 185 list_add_tail(list, stream_node); 186 187 device_unlock(adev); 188 } 189 190 static void adev_remove_stream_from_list( 191 struct audio_device* adev, struct listnode* stream_node) { 192 device_lock(adev); 193 194 list_remove(stream_node); 195 196 device_unlock(adev); 197 } 198 199 /* 200 * Extract the card and device numbers from the supplied key/value pairs. 201 * kvpairs A null-terminated string containing the key/value pairs or card and device. 202 * i.e. "card=1;device=42" 203 * card A pointer to a variable to receive the parsed-out card number. 204 * device A pointer to a variable to receive the parsed-out device number. 205 * NOTE: The variables pointed to by card and device return -1 (undefined) if the 206 * associated key/value pair is not found in the provided string. 207 * Return true if the kvpairs string contain a card/device spec, false otherwise. 208 */ 209 static bool parse_card_device_params(const char *kvpairs, int *card, int *device) 210 { 211 struct str_parms * parms = str_parms_create_str(kvpairs); 212 char value[32]; 213 int param_val; 214 215 // initialize to "undefined" state. 216 *card = -1; 217 *device = -1; 218 219 param_val = str_parms_get_str(parms, "card", value, sizeof(value)); 220 if (param_val >= 0) { 221 *card = atoi(value); 222 } 223 224 param_val = str_parms_get_str(parms, "device", value, sizeof(value)); 225 if (param_val >= 0) { 226 *device = atoi(value); 227 } 228 229 str_parms_destroy(parms); 230 231 return *card >= 0 && *device >= 0; 232 } 233 234 static char * device_get_parameters(alsa_device_profile * profile, const char * keys) 235 { 236 if (profile->card < 0 || profile->device < 0) { 237 return strdup(""); 238 } 239 240 struct str_parms *query = str_parms_create_str(keys); 241 struct str_parms *result = str_parms_create(); 242 243 /* These keys are from hardware/libhardware/include/audio.h */ 244 /* supported sample rates */ 245 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { 246 char* rates_list = profile_get_sample_rate_strs(profile); 247 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, 248 rates_list); 249 free(rates_list); 250 } 251 252 /* supported channel counts */ 253 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { 254 char* channels_list = profile_get_channel_count_strs(profile); 255 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, 256 channels_list); 257 free(channels_list); 258 } 259 260 /* supported sample formats */ 261 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { 262 char * format_params = profile_get_format_strs(profile); 263 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, 264 format_params); 265 free(format_params); 266 } 267 str_parms_destroy(query); 268 269 char* result_str = str_parms_to_str(result); 270 str_parms_destroy(result); 271 272 ALOGV("device_get_parameters = %s", result_str); 273 274 return result_str; 275 } 276 277 /* 278 * HAl Functions 279 */ 280 /** 281 * NOTE: when multiple mutexes have to be acquired, always respect the 282 * following order: hw device > out stream 283 */ 284 285 /* 286 * OUT functions 287 */ 288 static uint32_t out_get_sample_rate(const struct audio_stream *stream) 289 { 290 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy); 291 ALOGV("out_get_sample_rate() = %d", rate); 292 return rate; 293 } 294 295 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 296 { 297 return 0; 298 } 299 300 static size_t out_get_buffer_size(const struct audio_stream *stream) 301 { 302 const struct stream_out* out = (const struct stream_out*)stream; 303 size_t buffer_size = 304 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream)); 305 return buffer_size; 306 } 307 308 static uint32_t out_get_channels(const struct audio_stream *stream) 309 { 310 const struct stream_out *out = (const struct stream_out*)stream; 311 return out->hal_channel_mask; 312 } 313 314 static audio_format_t out_get_format(const struct audio_stream *stream) 315 { 316 /* Note: The HAL doesn't do any FORMAT conversion at this time. It 317 * Relies on the framework to provide data in the specified format. 318 * This could change in the future. 319 */ 320 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy; 321 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy)); 322 return format; 323 } 324 325 static int out_set_format(struct audio_stream *stream, audio_format_t format) 326 { 327 return 0; 328 } 329 330 static int out_standby(struct audio_stream *stream) 331 { 332 struct stream_out *out = (struct stream_out *)stream; 333 334 stream_lock(&out->lock); 335 if (!out->standby) { 336 device_lock(out->adev); 337 proxy_close(&out->proxy); 338 device_unlock(out->adev); 339 out->standby = true; 340 } 341 stream_unlock(&out->lock); 342 return 0; 343 } 344 345 static int out_dump(const struct audio_stream *stream, int fd) { 346 const struct stream_out* out_stream = (const struct stream_out*) stream; 347 348 if (out_stream != NULL) { 349 dprintf(fd, "Output Profile:\n"); 350 profile_dump(out_stream->profile, fd); 351 352 dprintf(fd, "Output Proxy:\n"); 353 proxy_dump(&out_stream->proxy, fd); 354 } 355 356 return 0; 357 } 358 359 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 360 { 361 ALOGV("out_set_parameters() keys:%s", kvpairs); 362 363 struct stream_out *out = (struct stream_out *)stream; 364 365 int routing = 0; 366 int ret_value = 0; 367 int card = -1; 368 int device = -1; 369 370 if (!parse_card_device_params(kvpairs, &card, &device)) { 371 // nothing to do 372 return ret_value; 373 } 374 375 stream_lock(&out->lock); 376 /* Lock the device because that is where the profile lives */ 377 device_lock(out->adev); 378 379 if (!profile_is_cached_for(out->profile, card, device)) { 380 /* cannot read pcm device info if playback is active */ 381 if (!out->standby) 382 ret_value = -ENOSYS; 383 else { 384 int saved_card = out->profile->card; 385 int saved_device = out->profile->device; 386 out->profile->card = card; 387 out->profile->device = device; 388 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL; 389 if (ret_value != 0) { 390 out->profile->card = saved_card; 391 out->profile->device = saved_device; 392 } 393 } 394 } 395 396 device_unlock(out->adev); 397 stream_unlock(&out->lock); 398 399 return ret_value; 400 } 401 402 static char * out_get_parameters(const struct audio_stream *stream, const char *keys) 403 { 404 struct stream_out *out = (struct stream_out *)stream; 405 stream_lock(&out->lock); 406 device_lock(out->adev); 407 408 char * params_str = device_get_parameters(out->profile, keys); 409 410 device_unlock(out->adev); 411 stream_unlock(&out->lock); 412 return params_str; 413 } 414 415 static uint32_t out_get_latency(const struct audio_stream_out *stream) 416 { 417 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy; 418 return proxy_get_latency(proxy); 419 } 420 421 static int out_set_volume(struct audio_stream_out *stream, float left, float right) 422 { 423 return -ENOSYS; 424 } 425 426 /* must be called with hw device and output stream mutexes locked */ 427 static int start_output_stream(struct stream_out *out) 428 { 429 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device); 430 431 return proxy_open(&out->proxy); 432 } 433 434 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes) 435 { 436 int ret; 437 struct stream_out *out = (struct stream_out *)stream; 438 439 stream_lock(&out->lock); 440 if (out->standby) { 441 device_lock(out->adev); 442 ret = start_output_stream(out); 443 device_unlock(out->adev); 444 if (ret != 0) { 445 goto err; 446 } 447 out->standby = false; 448 } 449 450 alsa_device_proxy* proxy = &out->proxy; 451 const void * write_buff = buffer; 452 int num_write_buff_bytes = bytes; 453 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */ 454 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */ 455 if (num_device_channels != num_req_channels) { 456 /* allocate buffer */ 457 const size_t required_conversion_buffer_size = 458 bytes * num_device_channels / num_req_channels; 459 if (required_conversion_buffer_size > out->conversion_buffer_size) { 460 out->conversion_buffer_size = required_conversion_buffer_size; 461 out->conversion_buffer = realloc(out->conversion_buffer, 462 out->conversion_buffer_size); 463 } 464 /* convert data */ 465 const audio_format_t audio_format = out_get_format(&(out->stream.common)); 466 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); 467 num_write_buff_bytes = 468 adjust_channels(write_buff, num_req_channels, 469 out->conversion_buffer, num_device_channels, 470 sample_size_in_bytes, num_write_buff_bytes); 471 write_buff = out->conversion_buffer; 472 } 473 474 if (write_buff != NULL && num_write_buff_bytes != 0) { 475 proxy_write(&out->proxy, write_buff, num_write_buff_bytes); 476 } 477 478 stream_unlock(&out->lock); 479 480 return bytes; 481 482 err: 483 stream_unlock(&out->lock); 484 if (ret != 0) { 485 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / 486 out_get_sample_rate(&stream->common)); 487 } 488 489 return bytes; 490 } 491 492 static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames) 493 { 494 return -EINVAL; 495 } 496 497 static int out_get_presentation_position(const struct audio_stream_out *stream, 498 uint64_t *frames, struct timespec *timestamp) 499 { 500 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier 501 stream_lock(&out->lock); 502 503 const alsa_device_proxy *proxy = &out->proxy; 504 const int ret = proxy_get_presentation_position(proxy, frames, timestamp); 505 506 stream_unlock(&out->lock); 507 return ret; 508 } 509 510 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 511 { 512 return 0; 513 } 514 515 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 516 { 517 return 0; 518 } 519 520 static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp) 521 { 522 return -EINVAL; 523 } 524 525 static int adev_open_output_stream(struct audio_hw_device *hw_dev, 526 audio_io_handle_t handle, 527 audio_devices_t devicesSpec __unused, 528 audio_output_flags_t flags, 529 struct audio_config *config, 530 struct audio_stream_out **stream_out, 531 const char *address /*__unused*/) 532 { 533 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s", 534 handle, devicesSpec, flags, address); 535 536 struct stream_out *out; 537 538 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 539 if (out == NULL) { 540 return -ENOMEM; 541 } 542 543 /* setup function pointers */ 544 out->stream.common.get_sample_rate = out_get_sample_rate; 545 out->stream.common.set_sample_rate = out_set_sample_rate; 546 out->stream.common.get_buffer_size = out_get_buffer_size; 547 out->stream.common.get_channels = out_get_channels; 548 out->stream.common.get_format = out_get_format; 549 out->stream.common.set_format = out_set_format; 550 out->stream.common.standby = out_standby; 551 out->stream.common.dump = out_dump; 552 out->stream.common.set_parameters = out_set_parameters; 553 out->stream.common.get_parameters = out_get_parameters; 554 out->stream.common.add_audio_effect = out_add_audio_effect; 555 out->stream.common.remove_audio_effect = out_remove_audio_effect; 556 out->stream.get_latency = out_get_latency; 557 out->stream.set_volume = out_set_volume; 558 out->stream.write = out_write; 559 out->stream.get_render_position = out_get_render_position; 560 out->stream.get_presentation_position = out_get_presentation_position; 561 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 562 563 stream_lock_init(&out->lock); 564 565 out->adev = (struct audio_device *)hw_dev; 566 device_lock(out->adev); 567 out->profile = &out->adev->out_profile; 568 569 // build this to hand to the alsa_device_proxy 570 struct pcm_config proxy_config; 571 memset(&proxy_config, 0, sizeof(proxy_config)); 572 573 /* Pull out the card/device pair */ 574 parse_card_device_params(address, &(out->profile->card), &(out->profile->device)); 575 576 profile_read_device_info(out->profile); 577 578 int ret = 0; 579 580 /* Rate */ 581 if (config->sample_rate == 0) { 582 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile); 583 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) { 584 proxy_config.rate = config->sample_rate; 585 } else { 586 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile); 587 ret = -EINVAL; 588 } 589 590 out->adev->device_sample_rate = config->sample_rate; 591 device_unlock(out->adev); 592 593 /* Format */ 594 if (config->format == AUDIO_FORMAT_DEFAULT) { 595 proxy_config.format = profile_get_default_format(out->profile); 596 config->format = audio_format_from_pcm_format(proxy_config.format); 597 } else { 598 enum pcm_format fmt = pcm_format_from_audio_format(config->format); 599 if (profile_is_format_valid(out->profile, fmt)) { 600 proxy_config.format = fmt; 601 } else { 602 proxy_config.format = profile_get_default_format(out->profile); 603 config->format = audio_format_from_pcm_format(proxy_config.format); 604 ret = -EINVAL; 605 } 606 } 607 608 /* Channels */ 609 bool calc_mask = false; 610 if (config->channel_mask == AUDIO_CHANNEL_NONE) { 611 /* query case */ 612 out->hal_channel_count = profile_get_default_channel_count(out->profile); 613 calc_mask = true; 614 } else { 615 /* explicit case */ 616 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask); 617 } 618 619 /* The Framework is currently limited to no more than this number of channels */ 620 if (out->hal_channel_count > FCC_8) { 621 out->hal_channel_count = FCC_8; 622 calc_mask = true; 623 } 624 625 if (calc_mask) { 626 /* need to calculate the mask from channel count either because this is the query case 627 * or the specified mask isn't valid for this device, or is more then the FW can handle */ 628 config->channel_mask = out->hal_channel_count <= FCC_2 629 /* position mask for mono and stereo*/ 630 ? audio_channel_out_mask_from_count(out->hal_channel_count) 631 /* otherwise indexed */ 632 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count); 633 } 634 635 out->hal_channel_mask = config->channel_mask; 636 637 // Validate the "logical" channel count against support in the "actual" profile. 638 // if they differ, choose the "actual" number of channels *closest* to the "logical". 639 // and store THAT in proxy_config.channels 640 proxy_config.channels = profile_get_closest_channel_count(out->profile, out->hal_channel_count); 641 proxy_prepare(&out->proxy, out->profile, &proxy_config); 642 643 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */ 644 ret = 0; 645 646 out->conversion_buffer = NULL; 647 out->conversion_buffer_size = 0; 648 649 out->standby = true; 650 651 /* Save the stream for adev_dump() */ 652 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node); 653 654 *stream_out = &out->stream; 655 656 return ret; 657 658 err_open: 659 free(out); 660 *stream_out = NULL; 661 return -ENOSYS; 662 } 663 664 static void adev_close_output_stream(struct audio_hw_device *hw_dev, 665 struct audio_stream_out *stream) 666 { 667 struct stream_out *out = (struct stream_out *)stream; 668 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device); 669 670 adev_remove_stream_from_list(out->adev, &out->list_node); 671 672 /* Close the pcm device */ 673 out_standby(&stream->common); 674 675 free(out->conversion_buffer); 676 677 out->conversion_buffer = NULL; 678 out->conversion_buffer_size = 0; 679 680 device_lock(out->adev); 681 out->adev->device_sample_rate = 0; 682 device_unlock(out->adev); 683 684 free(stream); 685 } 686 687 static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev, 688 const struct audio_config *config) 689 { 690 /* TODO This needs to be calculated based on format/channels/rate */ 691 return 320; 692 } 693 694 /* 695 * IN functions 696 */ 697 static uint32_t in_get_sample_rate(const struct audio_stream *stream) 698 { 699 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy); 700 ALOGV("in_get_sample_rate() = %d", rate); 701 return rate; 702 } 703 704 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 705 { 706 ALOGV("in_set_sample_rate(%d) - NOPE", rate); 707 return -ENOSYS; 708 } 709 710 static size_t in_get_buffer_size(const struct audio_stream *stream) 711 { 712 const struct stream_in * in = ((const struct stream_in*)stream); 713 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream)); 714 } 715 716 static uint32_t in_get_channels(const struct audio_stream *stream) 717 { 718 const struct stream_in *in = (const struct stream_in*)stream; 719 return in->hal_channel_mask; 720 } 721 722 static audio_format_t in_get_format(const struct audio_stream *stream) 723 { 724 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy; 725 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy)); 726 return format; 727 } 728 729 static int in_set_format(struct audio_stream *stream, audio_format_t format) 730 { 731 ALOGV("in_set_format(%d) - NOPE", format); 732 733 return -ENOSYS; 734 } 735 736 static int in_standby(struct audio_stream *stream) 737 { 738 struct stream_in *in = (struct stream_in *)stream; 739 740 stream_lock(&in->lock); 741 if (!in->standby) { 742 device_lock(in->adev); 743 proxy_close(&in->proxy); 744 device_unlock(in->adev); 745 in->standby = true; 746 } 747 748 stream_unlock(&in->lock); 749 750 return 0; 751 } 752 753 static int in_dump(const struct audio_stream *stream, int fd) 754 { 755 const struct stream_in* in_stream = (const struct stream_in*)stream; 756 if (in_stream != NULL) { 757 dprintf(fd, "Input Profile:\n"); 758 profile_dump(in_stream->profile, fd); 759 760 dprintf(fd, "Input Proxy:\n"); 761 proxy_dump(&in_stream->proxy, fd); 762 } 763 764 return 0; 765 } 766 767 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 768 { 769 ALOGV("in_set_parameters() keys:%s", kvpairs); 770 771 struct stream_in *in = (struct stream_in *)stream; 772 773 char value[32]; 774 int param_val; 775 int routing = 0; 776 int ret_value = 0; 777 int card = -1; 778 int device = -1; 779 780 if (!parse_card_device_params(kvpairs, &card, &device)) { 781 // nothing to do 782 return ret_value; 783 } 784 785 stream_lock(&in->lock); 786 device_lock(in->adev); 787 788 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) { 789 /* cannot read pcm device info if playback is active */ 790 if (!in->standby) 791 ret_value = -ENOSYS; 792 else { 793 int saved_card = in->profile->card; 794 int saved_device = in->profile->device; 795 in->profile->card = card; 796 in->profile->device = device; 797 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL; 798 if (ret_value != 0) { 799 in->profile->card = saved_card; 800 in->profile->device = saved_device; 801 } 802 } 803 } 804 805 device_unlock(in->adev); 806 stream_unlock(&in->lock); 807 808 return ret_value; 809 } 810 811 static char * in_get_parameters(const struct audio_stream *stream, const char *keys) 812 { 813 struct stream_in *in = (struct stream_in *)stream; 814 815 stream_lock(&in->lock); 816 device_lock(in->adev); 817 818 char * params_str = device_get_parameters(in->profile, keys); 819 820 device_unlock(in->adev); 821 stream_unlock(&in->lock); 822 823 return params_str; 824 } 825 826 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 827 { 828 return 0; 829 } 830 831 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 832 { 833 return 0; 834 } 835 836 static int in_set_gain(struct audio_stream_in *stream, float gain) 837 { 838 return 0; 839 } 840 841 /* must be called with hw device and output stream mutexes locked */ 842 static int start_input_stream(struct stream_in *in) 843 { 844 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device); 845 846 return proxy_open(&in->proxy); 847 } 848 849 /* TODO mutex stuff here (see out_write) */ 850 static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) 851 { 852 size_t num_read_buff_bytes = 0; 853 void * read_buff = buffer; 854 void * out_buff = buffer; 855 int ret = 0; 856 857 struct stream_in * in = (struct stream_in *)stream; 858 859 stream_lock(&in->lock); 860 if (in->standby) { 861 device_lock(in->adev); 862 ret = start_input_stream(in); 863 device_unlock(in->adev); 864 if (ret != 0) { 865 goto err; 866 } 867 in->standby = false; 868 } 869 870 alsa_device_profile * profile = in->profile; 871 872 /* 873 * OK, we need to figure out how much data to read to be able to output the requested 874 * number of bytes in the HAL format (16-bit, stereo). 875 */ 876 num_read_buff_bytes = bytes; 877 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */ 878 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */ 879 880 if (num_device_channels != num_req_channels) { 881 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels; 882 } 883 884 /* Setup/Realloc the conversion buffer (if necessary). */ 885 if (num_read_buff_bytes != bytes) { 886 if (num_read_buff_bytes > in->conversion_buffer_size) { 887 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats 888 (and do these conversions themselves) */ 889 in->conversion_buffer_size = num_read_buff_bytes; 890 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); 891 } 892 read_buff = in->conversion_buffer; 893 } 894 895 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes); 896 if (ret == 0) { 897 if (num_device_channels != num_req_channels) { 898 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels); 899 900 out_buff = buffer; 901 /* Num Channels conversion */ 902 if (num_device_channels != num_req_channels) { 903 audio_format_t audio_format = in_get_format(&(in->stream.common)); 904 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); 905 906 num_read_buff_bytes = 907 adjust_channels(read_buff, num_device_channels, 908 out_buff, num_req_channels, 909 sample_size_in_bytes, num_read_buff_bytes); 910 } 911 } 912 913 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */ 914 if (num_read_buff_bytes > 0 && in->adev->mic_muted) 915 memset(buffer, 0, num_read_buff_bytes); 916 } else { 917 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged 918 } 919 920 err: 921 stream_unlock(&in->lock); 922 return num_read_buff_bytes; 923 } 924 925 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 926 { 927 return 0; 928 } 929 930 static int adev_open_input_stream(struct audio_hw_device *hw_dev, 931 audio_io_handle_t handle, 932 audio_devices_t devicesSpec __unused, 933 struct audio_config *config, 934 struct audio_stream_in **stream_in, 935 audio_input_flags_t flags __unused, 936 const char *address /*__unused*/, 937 audio_source_t source __unused) 938 { 939 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8, 940 config->sample_rate, config->channel_mask, config->format); 941 942 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 943 int ret = 0; 944 945 if (in == NULL) { 946 return -ENOMEM; 947 } 948 949 /* setup function pointers */ 950 in->stream.common.get_sample_rate = in_get_sample_rate; 951 in->stream.common.set_sample_rate = in_set_sample_rate; 952 in->stream.common.get_buffer_size = in_get_buffer_size; 953 in->stream.common.get_channels = in_get_channels; 954 in->stream.common.get_format = in_get_format; 955 in->stream.common.set_format = in_set_format; 956 in->stream.common.standby = in_standby; 957 in->stream.common.dump = in_dump; 958 in->stream.common.set_parameters = in_set_parameters; 959 in->stream.common.get_parameters = in_get_parameters; 960 in->stream.common.add_audio_effect = in_add_audio_effect; 961 in->stream.common.remove_audio_effect = in_remove_audio_effect; 962 963 in->stream.set_gain = in_set_gain; 964 in->stream.read = in_read; 965 in->stream.get_input_frames_lost = in_get_input_frames_lost; 966 967 stream_lock_init(&in->lock); 968 969 in->adev = (struct audio_device *)hw_dev; 970 device_lock(in->adev); 971 972 in->profile = &in->adev->in_profile; 973 974 struct pcm_config proxy_config; 975 memset(&proxy_config, 0, sizeof(proxy_config)); 976 977 /* Pull out the card/device pair */ 978 parse_card_device_params(address, &(in->profile->card), &(in->profile->device)); 979 980 profile_read_device_info(in->profile); 981 982 /* Rate */ 983 if (config->sample_rate == 0) { 984 config->sample_rate = profile_get_default_sample_rate(in->profile); 985 } 986 987 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */ 988 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */ 989 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0; 990 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate; 991 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) { 992 in->adev->device_sample_rate = proxy_config.rate = config->sample_rate; 993 } else { 994 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile); 995 ret = -EINVAL; 996 } 997 device_unlock(in->adev); 998 999 /* Format */ 1000 if (config->format == AUDIO_FORMAT_DEFAULT) { 1001 proxy_config.format = profile_get_default_format(in->profile); 1002 config->format = audio_format_from_pcm_format(proxy_config.format); 1003 } else { 1004 enum pcm_format fmt = pcm_format_from_audio_format(config->format); 1005 if (profile_is_format_valid(in->profile, fmt)) { 1006 proxy_config.format = fmt; 1007 } else { 1008 proxy_config.format = profile_get_default_format(in->profile); 1009 config->format = audio_format_from_pcm_format(proxy_config.format); 1010 ret = -EINVAL; 1011 } 1012 } 1013 1014 /* Channels */ 1015 bool calc_mask = false; 1016 if (config->channel_mask == AUDIO_CHANNEL_NONE) { 1017 /* query case */ 1018 in->hal_channel_count = profile_get_default_channel_count(in->profile); 1019 calc_mask = true; 1020 } else { 1021 /* explicit case */ 1022 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask); 1023 } 1024 1025 /* The Framework is currently limited to no more than this number of channels */ 1026 if (in->hal_channel_count > FCC_8) { 1027 in->hal_channel_count = FCC_8; 1028 calc_mask = true; 1029 } 1030 1031 if (calc_mask) { 1032 /* need to calculate the mask from channel count either because this is the query case 1033 * or the specified mask isn't valid for this device, or is more then the FW can handle */ 1034 in->hal_channel_mask = in->hal_channel_count <= FCC_2 1035 /* position mask for mono & stereo */ 1036 ? audio_channel_in_mask_from_count(in->hal_channel_count) 1037 /* otherwise indexed */ 1038 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count); 1039 1040 // if we change the mask... 1041 if (in->hal_channel_mask != config->channel_mask && 1042 config->channel_mask != AUDIO_CHANNEL_NONE) { 1043 config->channel_mask = in->hal_channel_mask; 1044 ret = -EINVAL; 1045 } 1046 } else { 1047 in->hal_channel_mask = config->channel_mask; 1048 } 1049 1050 if (ret == 0) { 1051 // Validate the "logical" channel count against support in the "actual" profile. 1052 // if they differ, choose the "actual" number of channels *closest* to the "logical". 1053 // and store THAT in proxy_config.channels 1054 proxy_config.channels = 1055 profile_get_closest_channel_count(in->profile, in->hal_channel_count); 1056 proxy_prepare(&in->proxy, in->profile, &proxy_config); 1057 1058 in->standby = true; 1059 1060 in->conversion_buffer = NULL; 1061 in->conversion_buffer_size = 0; 1062 1063 *stream_in = &in->stream; 1064 1065 /* Save this for adev_dump() */ 1066 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node); 1067 } else { 1068 // Deallocate this stream on error, because AudioFlinger won't call 1069 // adev_close_input_stream() in this case. 1070 *stream_in = NULL; 1071 free(in); 1072 } 1073 1074 return ret; 1075 } 1076 1077 static void adev_close_input_stream(struct audio_hw_device *hw_dev, 1078 struct audio_stream_in *stream) 1079 { 1080 struct stream_in *in = (struct stream_in *)stream; 1081 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device); 1082 1083 adev_remove_stream_from_list(in->adev, &in->list_node); 1084 1085 /* Close the pcm device */ 1086 in_standby(&stream->common); 1087 1088 free(in->conversion_buffer); 1089 1090 free(stream); 1091 } 1092 1093 /* 1094 * ADEV Functions 1095 */ 1096 static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs) 1097 { 1098 return 0; 1099 } 1100 1101 static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys) 1102 { 1103 return strdup(""); 1104 } 1105 1106 static int adev_init_check(const struct audio_hw_device *hw_dev) 1107 { 1108 return 0; 1109 } 1110 1111 static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume) 1112 { 1113 return -ENOSYS; 1114 } 1115 1116 static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume) 1117 { 1118 return -ENOSYS; 1119 } 1120 1121 static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode) 1122 { 1123 return 0; 1124 } 1125 1126 static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state) 1127 { 1128 struct audio_device * adev = (struct audio_device *)hw_dev; 1129 device_lock(adev); 1130 adev->mic_muted = state; 1131 device_unlock(adev); 1132 return -ENOSYS; 1133 } 1134 1135 static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state) 1136 { 1137 return -ENOSYS; 1138 } 1139 1140 static int adev_dump(const struct audio_hw_device *device, int fd) 1141 { 1142 dprintf(fd, "\nUSB audio module:\n"); 1143 1144 struct audio_device* adev = (struct audio_device*)device; 1145 const int kNumRetries = 3; 1146 const int kSleepTimeMS = 500; 1147 1148 // use device_try_lock() in case we dumpsys during a deadlock 1149 int retry = kNumRetries; 1150 while (retry > 0 && device_try_lock(adev) != 0) { 1151 sleep(kSleepTimeMS); 1152 retry--; 1153 } 1154 1155 if (retry > 0) { 1156 if (list_empty(&adev->output_stream_list)) { 1157 dprintf(fd, " No output streams.\n"); 1158 } else { 1159 struct listnode* node; 1160 list_for_each(node, &adev->output_stream_list) { 1161 struct audio_stream* stream = 1162 (struct audio_stream *)node_to_item(node, struct stream_out, list_node); 1163 out_dump(stream, fd); 1164 } 1165 } 1166 1167 if (list_empty(&adev->input_stream_list)) { 1168 dprintf(fd, "\n No input streams.\n"); 1169 } else { 1170 struct listnode* node; 1171 list_for_each(node, &adev->input_stream_list) { 1172 struct audio_stream* stream = 1173 (struct audio_stream *)node_to_item(node, struct stream_in, list_node); 1174 in_dump(stream, fd); 1175 } 1176 } 1177 1178 device_unlock(adev); 1179 } else { 1180 // Couldn't lock 1181 dprintf(fd, " Could not obtain device lock.\n"); 1182 } 1183 1184 return 0; 1185 } 1186 1187 static int adev_close(hw_device_t *device) 1188 { 1189 struct audio_device *adev = (struct audio_device *)device; 1190 free(device); 1191 1192 return 0; 1193 } 1194 1195 static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device) 1196 { 1197 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) 1198 return -EINVAL; 1199 1200 struct audio_device *adev = calloc(1, sizeof(struct audio_device)); 1201 if (!adev) 1202 return -ENOMEM; 1203 1204 profile_init(&adev->out_profile, PCM_OUT); 1205 profile_init(&adev->in_profile, PCM_IN); 1206 1207 list_init(&adev->output_stream_list); 1208 list_init(&adev->input_stream_list); 1209 1210 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; 1211 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 1212 adev->hw_device.common.module = (struct hw_module_t *)module; 1213 adev->hw_device.common.close = adev_close; 1214 1215 adev->hw_device.init_check = adev_init_check; 1216 adev->hw_device.set_voice_volume = adev_set_voice_volume; 1217 adev->hw_device.set_master_volume = adev_set_master_volume; 1218 adev->hw_device.set_mode = adev_set_mode; 1219 adev->hw_device.set_mic_mute = adev_set_mic_mute; 1220 adev->hw_device.get_mic_mute = adev_get_mic_mute; 1221 adev->hw_device.set_parameters = adev_set_parameters; 1222 adev->hw_device.get_parameters = adev_get_parameters; 1223 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; 1224 adev->hw_device.open_output_stream = adev_open_output_stream; 1225 adev->hw_device.close_output_stream = adev_close_output_stream; 1226 adev->hw_device.open_input_stream = adev_open_input_stream; 1227 adev->hw_device.close_input_stream = adev_close_input_stream; 1228 adev->hw_device.dump = adev_dump; 1229 1230 *device = &adev->hw_device.common; 1231 1232 return 0; 1233 } 1234 1235 static struct hw_module_methods_t hal_module_methods = { 1236 .open = adev_open, 1237 }; 1238 1239 struct audio_module HAL_MODULE_INFO_SYM = { 1240 .common = { 1241 .tag = HARDWARE_MODULE_TAG, 1242 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 1243 .hal_api_version = HARDWARE_HAL_API_VERSION, 1244 .id = AUDIO_HARDWARE_MODULE_ID, 1245 .name = "USB audio HW HAL", 1246 .author = "The Android Open Source Project", 1247 .methods = &hal_module_methods, 1248 }, 1249 }; 1250