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