1 /* 2 ** Copyright 2010, 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 #include <math.h> 18 19 //#define LOG_NDEBUG 0 20 21 #define LOG_TAG "AudioHardware" 22 23 #include <utils/Log.h> 24 #include <utils/String8.h> 25 26 #include <stdio.h> 27 #include <unistd.h> 28 #include <sys/ioctl.h> 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <sys/resource.h> 32 #include <dlfcn.h> 33 #include <fcntl.h> 34 35 #include "AudioHardware.h" 36 #include <media/AudioRecord.h> 37 #include <hardware_legacy/power.h> 38 39 extern "C" { 40 #include "alsa_audio.h" 41 } 42 43 44 namespace android { 45 46 const uint32_t AudioHardware::inputSamplingRates[] = { 47 8000, 11025, 16000, 22050, 44100 48 }; 49 50 // trace driver operations for dump 51 // 52 #define DRIVER_TRACE 53 54 enum { 55 DRV_NONE, 56 DRV_PCM_OPEN, 57 DRV_PCM_CLOSE, 58 DRV_PCM_WRITE, 59 DRV_PCM_READ, 60 DRV_MIXER_OPEN, 61 DRV_MIXER_CLOSE, 62 DRV_MIXER_GET, 63 DRV_MIXER_SEL 64 }; 65 66 #ifdef DRIVER_TRACE 67 #define TRACE_DRIVER_IN(op) mDriverOp = op; 68 #define TRACE_DRIVER_OUT mDriverOp = DRV_NONE; 69 #else 70 #define TRACE_DRIVER_IN(op) 71 #define TRACE_DRIVER_OUT 72 #endif 73 74 // ---------------------------------------------------------------------------- 75 76 const char *AudioHardware::inputPathNameDefault = "Default"; 77 const char *AudioHardware::inputPathNameCamcorder = "Camcorder"; 78 const char *AudioHardware::inputPathNameVoiceRecognition = "Voice Recognition"; 79 const char *AudioHardware::inputPathNameVoiceCommunication = "Voice Communication"; 80 81 AudioHardware::AudioHardware() : 82 mInit(false), 83 mMicMute(false), 84 mPcm(NULL), 85 mMixer(NULL), 86 mPcmOpenCnt(0), 87 mMixerOpenCnt(0), 88 mInCallAudioMode(false), 89 mVoiceVol(1.0f), 90 mInputSource(AUDIO_SOURCE_DEFAULT), 91 mBluetoothNrec(true), 92 mTTYMode(TTY_MODE_OFF), 93 mSecRilLibHandle(NULL), 94 mRilClient(0), 95 mActivatedCP(false), 96 mDriverOp(DRV_NONE) 97 { 98 loadRILD(); 99 mInit = true; 100 } 101 102 AudioHardware::~AudioHardware() 103 { 104 for (size_t index = 0; index < mInputs.size(); index++) { 105 closeInputStream(mInputs[index].get()); 106 } 107 mInputs.clear(); 108 closeOutputStream((AudioStreamOut*)mOutput.get()); 109 110 if (mMixer) { 111 TRACE_DRIVER_IN(DRV_MIXER_CLOSE) 112 mixer_close(mMixer); 113 TRACE_DRIVER_OUT 114 } 115 if (mPcm) { 116 TRACE_DRIVER_IN(DRV_PCM_CLOSE) 117 pcm_close(mPcm); 118 TRACE_DRIVER_OUT 119 } 120 121 if (mSecRilLibHandle) { 122 if (disconnectRILD(mRilClient) != RIL_CLIENT_ERR_SUCCESS) 123 LOGE("Disconnect_RILD() error"); 124 125 if (closeClientRILD(mRilClient) != RIL_CLIENT_ERR_SUCCESS) 126 LOGE("CloseClient_RILD() error"); 127 128 mRilClient = 0; 129 130 dlclose(mSecRilLibHandle); 131 mSecRilLibHandle = NULL; 132 } 133 134 mInit = false; 135 } 136 137 status_t AudioHardware::initCheck() 138 { 139 return mInit ? NO_ERROR : NO_INIT; 140 } 141 142 void AudioHardware::loadRILD(void) 143 { 144 mSecRilLibHandle = dlopen("libsecril-client.so", RTLD_NOW); 145 146 if (mSecRilLibHandle) { 147 LOGV("libsecril-client.so is loaded"); 148 149 openClientRILD = (HRilClient (*)(void)) 150 dlsym(mSecRilLibHandle, "OpenClient_RILD"); 151 disconnectRILD = (int (*)(HRilClient)) 152 dlsym(mSecRilLibHandle, "Disconnect_RILD"); 153 closeClientRILD = (int (*)(HRilClient)) 154 dlsym(mSecRilLibHandle, "CloseClient_RILD"); 155 isConnectedRILD = (int (*)(HRilClient)) 156 dlsym(mSecRilLibHandle, "isConnected_RILD"); 157 connectRILD = (int (*)(HRilClient)) 158 dlsym(mSecRilLibHandle, "Connect_RILD"); 159 setCallVolume = (int (*)(HRilClient, SoundType, int)) 160 dlsym(mSecRilLibHandle, "SetCallVolume"); 161 setCallAudioPath = (int (*)(HRilClient, AudioPath)) 162 dlsym(mSecRilLibHandle, "SetCallAudioPath"); 163 setCallClockSync = (int (*)(HRilClient, SoundClockCondition)) 164 dlsym(mSecRilLibHandle, "SetCallClockSync"); 165 166 if (!openClientRILD || !disconnectRILD || !closeClientRILD || 167 !isConnectedRILD || !connectRILD || 168 !setCallVolume || !setCallAudioPath || !setCallClockSync) { 169 LOGE("Can't load all functions from libsecril-client.so"); 170 171 dlclose(mSecRilLibHandle); 172 mSecRilLibHandle = NULL; 173 } else { 174 mRilClient = openClientRILD(); 175 if (!mRilClient) { 176 LOGE("OpenClient_RILD() error"); 177 178 dlclose(mSecRilLibHandle); 179 mSecRilLibHandle = NULL; 180 } 181 } 182 } else { 183 LOGE("Can't load libsecril-client.so"); 184 } 185 } 186 187 status_t AudioHardware::connectRILDIfRequired(void) 188 { 189 if (!mSecRilLibHandle) { 190 LOGE("connectIfRequired() lib is not loaded"); 191 return INVALID_OPERATION; 192 } 193 194 if (isConnectedRILD(mRilClient)) { 195 return OK; 196 } 197 198 if (connectRILD(mRilClient) != RIL_CLIENT_ERR_SUCCESS) { 199 LOGE("Connect_RILD() error"); 200 return INVALID_OPERATION; 201 } 202 203 return OK; 204 } 205 206 AudioStreamOut* AudioHardware::openOutputStream( 207 uint32_t devices, int *format, uint32_t *channels, 208 uint32_t *sampleRate, status_t *status) 209 { 210 sp <AudioStreamOutALSA> out; 211 status_t rc; 212 213 { // scope for the lock 214 Mutex::Autolock lock(mLock); 215 216 // only one output stream allowed 217 if (mOutput != 0) { 218 if (status) { 219 *status = INVALID_OPERATION; 220 } 221 return NULL; 222 } 223 224 out = new AudioStreamOutALSA(); 225 226 rc = out->set(this, devices, format, channels, sampleRate); 227 if (rc == NO_ERROR) { 228 mOutput = out; 229 } 230 } 231 232 if (rc != NO_ERROR) { 233 if (out != 0) { 234 out.clear(); 235 } 236 } 237 if (status) { 238 *status = rc; 239 } 240 241 return out.get(); 242 } 243 244 void AudioHardware::closeOutputStream(AudioStreamOut* out) { 245 sp <AudioStreamOutALSA> spOut; 246 { 247 Mutex::Autolock lock(mLock); 248 if (mOutput == 0 || mOutput.get() != out) { 249 LOGW("Attempt to close invalid output stream"); 250 return; 251 } 252 spOut = mOutput; 253 mOutput.clear(); 254 } 255 spOut.clear(); 256 } 257 258 AudioStreamIn* AudioHardware::openInputStream( 259 uint32_t devices, int *format, uint32_t *channels, 260 uint32_t *sampleRate, status_t *status, 261 AudioSystem::audio_in_acoustics acoustic_flags) 262 { 263 // check for valid input source 264 if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) { 265 if (status) { 266 *status = BAD_VALUE; 267 } 268 return NULL; 269 } 270 271 status_t rc = NO_ERROR; 272 sp <AudioStreamInALSA> in; 273 274 { // scope for the lock 275 Mutex::Autolock lock(mLock); 276 277 in = new AudioStreamInALSA(); 278 rc = in->set(this, devices, format, channels, sampleRate, acoustic_flags); 279 if (rc == NO_ERROR) { 280 mInputs.add(in); 281 } 282 } 283 284 if (rc != NO_ERROR) { 285 if (in != 0) { 286 in.clear(); 287 } 288 } 289 if (status) { 290 *status = rc; 291 } 292 293 LOGV("AudioHardware::openInputStream()%p", in.get()); 294 return in.get(); 295 } 296 297 void AudioHardware::closeInputStream(AudioStreamIn* in) { 298 299 sp<AudioStreamInALSA> spIn; 300 { 301 Mutex::Autolock lock(mLock); 302 303 ssize_t index = mInputs.indexOf((AudioStreamInALSA *)in); 304 if (index < 0) { 305 LOGW("Attempt to close invalid input stream"); 306 return; 307 } 308 spIn = mInputs[index]; 309 mInputs.removeAt(index); 310 } 311 LOGV("AudioHardware::closeInputStream()%p", in); 312 spIn.clear(); 313 } 314 315 316 status_t AudioHardware::setMode(int mode) 317 { 318 sp<AudioStreamOutALSA> spOut; 319 sp<AudioStreamInALSA> spIn; 320 status_t status; 321 322 // Mutex acquisition order is always out -> in -> hw 323 AutoMutex lock(mLock); 324 325 spOut = mOutput; 326 while (spOut != 0) { 327 if (!spOut->checkStandby()) { 328 int cnt = spOut->prepareLock(); 329 mLock.unlock(); 330 spOut->lock(); 331 mLock.lock(); 332 // make sure that another thread did not change output state while the 333 // mutex is released 334 if ((spOut == mOutput) && (cnt == spOut->standbyCnt())) { 335 break; 336 } 337 spOut->unlock(); 338 spOut = mOutput; 339 } else { 340 spOut.clear(); 341 } 342 } 343 // spOut is not 0 here only if the output is active 344 345 spIn = getActiveInput_l(); 346 while (spIn != 0) { 347 int cnt = spIn->prepareLock(); 348 mLock.unlock(); 349 spIn->lock(); 350 mLock.lock(); 351 // make sure that another thread did not change input state while the 352 // mutex is released 353 if ((spIn == getActiveInput_l()) && (cnt == spIn->standbyCnt())) { 354 break; 355 } 356 spIn->unlock(); 357 spIn = getActiveInput_l(); 358 } 359 // spIn is not 0 here only if the input is active 360 361 int prevMode = mMode; 362 status = AudioHardwareBase::setMode(mode); 363 LOGV("setMode() : new %d, old %d", mMode, prevMode); 364 if (status == NO_ERROR) { 365 // activate call clock in radio when entering in call or ringtone mode 366 if (prevMode == AudioSystem::MODE_NORMAL) 367 { 368 if ((!mActivatedCP) && (mSecRilLibHandle) && (connectRILDIfRequired() == OK)) { 369 setCallClockSync(mRilClient, SOUND_CLOCK_START); 370 mActivatedCP = true; 371 } 372 } 373 374 if (mMode == AudioSystem::MODE_IN_CALL && !mInCallAudioMode) { 375 if (spOut != 0) { 376 LOGV("setMode() in call force output standby"); 377 spOut->doStandby_l(); 378 } 379 if (spIn != 0) { 380 LOGV("setMode() in call force input standby"); 381 spIn->doStandby_l(); 382 } 383 384 LOGV("setMode() openPcmOut_l()"); 385 openPcmOut_l(); 386 openMixer_l(); 387 setInputSource_l(AUDIO_SOURCE_DEFAULT); 388 setVoiceVolume_l(mVoiceVol); 389 mInCallAudioMode = true; 390 } 391 if (mMode == AudioSystem::MODE_NORMAL && mInCallAudioMode) { 392 setInputSource_l(mInputSource); 393 if (mMixer != NULL) { 394 TRACE_DRIVER_IN(DRV_MIXER_GET) 395 struct mixer_ctl *ctl= mixer_get_control(mMixer, "Playback Path", 0); 396 TRACE_DRIVER_OUT 397 if (ctl != NULL) { 398 LOGV("setMode() reset Playback Path to RCV"); 399 TRACE_DRIVER_IN(DRV_MIXER_SEL) 400 mixer_ctl_select(ctl, "RCV"); 401 TRACE_DRIVER_OUT 402 } 403 } 404 LOGV("setMode() closePcmOut_l()"); 405 closeMixer_l(); 406 closePcmOut_l(); 407 408 if (spOut != 0) { 409 LOGV("setMode() off call force output standby"); 410 spOut->doStandby_l(); 411 } 412 if (spIn != 0) { 413 LOGV("setMode() off call force input standby"); 414 spIn->doStandby_l(); 415 } 416 417 mInCallAudioMode = false; 418 } 419 420 if (mMode == AudioSystem::MODE_NORMAL) { 421 if(mActivatedCP) 422 mActivatedCP = false; 423 } 424 } 425 426 if (spIn != 0) { 427 spIn->unlock(); 428 } 429 if (spOut != 0) { 430 spOut->unlock(); 431 } 432 433 return status; 434 } 435 436 status_t AudioHardware::setMicMute(bool state) 437 { 438 LOGV("setMicMute(%d) mMicMute %d", state, mMicMute); 439 sp<AudioStreamInALSA> spIn; 440 { 441 AutoMutex lock(mLock); 442 if (mMicMute != state) { 443 mMicMute = state; 444 // in call mute is handled by RIL 445 if (mMode != AudioSystem::MODE_IN_CALL) { 446 spIn = getActiveInput_l(); 447 } 448 } 449 } 450 451 if (spIn != 0) { 452 spIn->standby(); 453 } 454 455 return NO_ERROR; 456 } 457 458 status_t AudioHardware::getMicMute(bool* state) 459 { 460 *state = mMicMute; 461 return NO_ERROR; 462 } 463 464 status_t AudioHardware::setParameters(const String8& keyValuePairs) 465 { 466 AudioParameter param = AudioParameter(keyValuePairs); 467 String8 value; 468 String8 key; 469 const char BT_NREC_KEY[] = "bt_headset_nrec"; 470 const char BT_NREC_VALUE_ON[] = "on"; 471 const char TTY_MODE_KEY[] = "tty_mode"; 472 const char TTY_MODE_VALUE_OFF[] = "tty_off"; 473 const char TTY_MODE_VALUE_VCO[] = "tty_vco"; 474 const char TTY_MODE_VALUE_HCO[] = "tty_hco"; 475 const char TTY_MODE_VALUE_FULL[] = "tty_full"; 476 477 key = String8(BT_NREC_KEY); 478 if (param.get(key, value) == NO_ERROR) { 479 if (value == BT_NREC_VALUE_ON) { 480 mBluetoothNrec = true; 481 } else { 482 mBluetoothNrec = false; 483 LOGD("Turning noise reduction and echo cancellation off for BT " 484 "headset"); 485 } 486 param.remove(String8(BT_NREC_KEY)); 487 } 488 489 key = String8(TTY_MODE_KEY); 490 if (param.get(key, value) == NO_ERROR) { 491 int ttyMode; 492 if (value == TTY_MODE_VALUE_OFF) { 493 ttyMode = TTY_MODE_OFF; 494 } else if (value == TTY_MODE_VALUE_VCO) { 495 ttyMode = TTY_MODE_VCO; 496 } else if (value == TTY_MODE_VALUE_HCO) { 497 ttyMode = TTY_MODE_HCO; 498 } else if (value == TTY_MODE_VALUE_FULL) { 499 ttyMode = TTY_MODE_FULL; 500 } else { 501 return BAD_VALUE; 502 } 503 504 if (ttyMode != mTTYMode) { 505 LOGV("new tty mode %d", ttyMode); 506 mTTYMode = ttyMode; 507 if (mOutput != 0 && mMode == AudioSystem::MODE_IN_CALL) { 508 setIncallPath_l(mOutput->device()); 509 } 510 } 511 param.remove(String8(TTY_MODE_KEY)); 512 } 513 514 return NO_ERROR; 515 } 516 517 String8 AudioHardware::getParameters(const String8& keys) 518 { 519 AudioParameter request = AudioParameter(keys); 520 AudioParameter reply = AudioParameter(); 521 522 LOGV("getParameters() %s", keys.string()); 523 524 return reply.toString(); 525 } 526 527 size_t AudioHardware::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) 528 { 529 if (format != AudioSystem::PCM_16_BIT) { 530 LOGW("getInputBufferSize bad format: %d", format); 531 return 0; 532 } 533 if (channelCount < 1 || channelCount > 2) { 534 LOGW("getInputBufferSize bad channel count: %d", channelCount); 535 return 0; 536 } 537 if (sampleRate != 8000 && sampleRate != 11025 && sampleRate != 16000 && 538 sampleRate != 22050 && sampleRate != 44100) { 539 LOGW("getInputBufferSize bad sample rate: %d", sampleRate); 540 return 0; 541 } 542 543 return AudioStreamInALSA::getBufferSize(sampleRate, channelCount); 544 } 545 546 status_t AudioHardware::setVoiceVolume(float volume) 547 { 548 AutoMutex lock(mLock); 549 550 setVoiceVolume_l(volume); 551 552 return NO_ERROR; 553 } 554 555 void AudioHardware::setVoiceVolume_l(float volume) 556 { 557 LOGD("### setVoiceVolume_l"); 558 559 mVoiceVol = volume; 560 561 if ( (AudioSystem::MODE_IN_CALL == mMode) && (mSecRilLibHandle) && 562 (connectRILDIfRequired() == OK) ) { 563 564 uint32_t device = AudioSystem::DEVICE_OUT_EARPIECE; 565 if (mOutput != 0) { 566 device = mOutput->device(); 567 } 568 int int_volume = (int)(volume * 5); 569 SoundType type; 570 571 LOGD("### route(%d) call volume(%f)", device, volume); 572 switch (device) { 573 case AudioSystem::DEVICE_OUT_EARPIECE: 574 LOGD("### earpiece call volume"); 575 type = SOUND_TYPE_VOICE; 576 break; 577 578 case AudioSystem::DEVICE_OUT_SPEAKER: 579 LOGD("### speaker call volume"); 580 type = SOUND_TYPE_SPEAKER; 581 break; 582 583 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO: 584 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 585 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 586 LOGD("### bluetooth call volume"); 587 type = SOUND_TYPE_BTVOICE; 588 break; 589 590 case AudioSystem::DEVICE_OUT_WIRED_HEADSET: 591 case AudioSystem::DEVICE_OUT_WIRED_HEADPHONE: // Use receive path with 3 pole headset. 592 LOGD("### headset call volume"); 593 type = SOUND_TYPE_HEADSET; 594 break; 595 596 default: 597 LOGW("### Call volume setting error!!!0x%08x \n", device); 598 type = SOUND_TYPE_VOICE; 599 break; 600 } 601 setCallVolume(mRilClient, type, int_volume); 602 } 603 604 } 605 606 status_t AudioHardware::setMasterVolume(float volume) 607 { 608 LOGV("Set master volume to %f.\n", volume); 609 // We return an error code here to let the audioflinger do in-software 610 // volume on top of the maximum volume that we set through the SND API. 611 // return error - software mixer will handle it 612 return -1; 613 } 614 615 static const int kDumpLockRetries = 50; 616 static const int kDumpLockSleep = 20000; 617 618 static bool tryLock(Mutex& mutex) 619 { 620 bool locked = false; 621 for (int i = 0; i < kDumpLockRetries; ++i) { 622 if (mutex.tryLock() == NO_ERROR) { 623 locked = true; 624 break; 625 } 626 usleep(kDumpLockSleep); 627 } 628 return locked; 629 } 630 631 status_t AudioHardware::dump(int fd, const Vector<String16>& args) 632 { 633 const size_t SIZE = 256; 634 char buffer[SIZE]; 635 String8 result; 636 637 bool locked = tryLock(mLock); 638 if (!locked) { 639 snprintf(buffer, SIZE, "\n\tAudioHardware maybe deadlocked\n"); 640 } else { 641 mLock.unlock(); 642 } 643 644 snprintf(buffer, SIZE, "\tInit %s\n", (mInit) ? "OK" : "Failed"); 645 result.append(buffer); 646 snprintf(buffer, SIZE, "\tMic Mute %s\n", (mMicMute) ? "ON" : "OFF"); 647 result.append(buffer); 648 snprintf(buffer, SIZE, "\tmPcm: %p\n", mPcm); 649 result.append(buffer); 650 snprintf(buffer, SIZE, "\tmPcmOpenCnt: %d\n", mPcmOpenCnt); 651 result.append(buffer); 652 snprintf(buffer, SIZE, "\tmMixer: %p\n", mMixer); 653 result.append(buffer); 654 snprintf(buffer, SIZE, "\tmMixerOpenCnt: %d\n", mMixerOpenCnt); 655 result.append(buffer); 656 snprintf(buffer, SIZE, "\tIn Call Audio Mode %s\n", 657 (mInCallAudioMode) ? "ON" : "OFF"); 658 result.append(buffer); 659 snprintf(buffer, SIZE, "\tInput source %d\n", mInputSource); 660 result.append(buffer); 661 snprintf(buffer, SIZE, "\tmSecRilLibHandle: %p\n", mSecRilLibHandle); 662 result.append(buffer); 663 snprintf(buffer, SIZE, "\tmRilClient: %p\n", mRilClient); 664 result.append(buffer); 665 snprintf(buffer, SIZE, "\tCP %s\n", 666 (mActivatedCP) ? "Activated" : "Deactivated"); 667 result.append(buffer); 668 snprintf(buffer, SIZE, "\tmDriverOp: %d\n", mDriverOp); 669 result.append(buffer); 670 671 snprintf(buffer, SIZE, "\n\tmOutput %p dump:\n", mOutput.get()); 672 result.append(buffer); 673 write(fd, result.string(), result.size()); 674 if (mOutput != 0) { 675 mOutput->dump(fd, args); 676 } 677 678 snprintf(buffer, SIZE, "\n\t%d inputs opened:\n", mInputs.size()); 679 write(fd, buffer, strlen(buffer)); 680 for (size_t i = 0; i < mInputs.size(); i++) { 681 snprintf(buffer, SIZE, "\t- input %d dump:\n", i); 682 write(fd, buffer, strlen(buffer)); 683 mInputs[i]->dump(fd, args); 684 } 685 686 return NO_ERROR; 687 } 688 689 status_t AudioHardware::setIncallPath_l(uint32_t device) 690 { 691 LOGV("setIncallPath_l: device %x", device); 692 693 // Setup sound path for CP clocking 694 if ((mSecRilLibHandle) && 695 (connectRILDIfRequired() == OK)) { 696 697 if (mMode == AudioSystem::MODE_IN_CALL) { 698 LOGD("### incall mode route (%d)", device); 699 AudioPath path; 700 switch(device){ 701 case AudioSystem::DEVICE_OUT_EARPIECE: 702 LOGD("### incall mode earpiece route"); 703 path = SOUND_AUDIO_PATH_HANDSET; 704 break; 705 706 case AudioSystem::DEVICE_OUT_SPEAKER: 707 LOGD("### incall mode speaker route"); 708 path = SOUND_AUDIO_PATH_SPEAKER; 709 break; 710 711 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO: 712 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 713 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 714 LOGD("### incall mode bluetooth route %s NR", mBluetoothNrec ? "" : "NO"); 715 if (mBluetoothNrec) { 716 path = SOUND_AUDIO_PATH_BLUETOOTH; 717 } else { 718 path = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR; 719 } 720 break; 721 722 case AudioSystem::DEVICE_OUT_WIRED_HEADPHONE : 723 LOGD("### incall mode headphone route"); 724 path = SOUND_AUDIO_PATH_HEADPHONE; 725 break; 726 case AudioSystem::DEVICE_OUT_WIRED_HEADSET : 727 LOGD("### incall mode headset route"); 728 path = SOUND_AUDIO_PATH_HEADSET; 729 break; 730 default: 731 LOGW("### incall mode Error!! route = [%d]", device); 732 path = SOUND_AUDIO_PATH_HANDSET; 733 break; 734 } 735 736 setCallAudioPath(mRilClient, path); 737 738 if (mMixer != NULL) { 739 TRACE_DRIVER_IN(DRV_MIXER_GET) 740 struct mixer_ctl *ctl= mixer_get_control(mMixer, "Voice Call Path", 0); 741 TRACE_DRIVER_OUT 742 LOGE_IF(ctl == NULL, "setIncallPath_l() could not get mixer ctl"); 743 if (ctl != NULL) { 744 LOGV("setIncallPath_l() Voice Call Path, (%x)", device); 745 TRACE_DRIVER_IN(DRV_MIXER_SEL) 746 mixer_ctl_select(ctl, getVoiceRouteFromDevice(device)); 747 TRACE_DRIVER_OUT 748 } 749 } 750 } 751 } 752 return NO_ERROR; 753 } 754 755 struct pcm *AudioHardware::openPcmOut_l() 756 { 757 LOGD("openPcmOut_l() mPcmOpenCnt: %d", mPcmOpenCnt); 758 if (mPcmOpenCnt++ == 0) { 759 if (mPcm != NULL) { 760 LOGE("openPcmOut_l() mPcmOpenCnt == 0 and mPcm == %p\n", mPcm); 761 mPcmOpenCnt--; 762 return NULL; 763 } 764 unsigned flags = PCM_OUT; 765 766 flags |= (AUDIO_HW_OUT_PERIOD_MULT - 1) << PCM_PERIOD_SZ_SHIFT; 767 flags |= (AUDIO_HW_OUT_PERIOD_CNT - PCM_PERIOD_CNT_MIN) << PCM_PERIOD_CNT_SHIFT; 768 769 TRACE_DRIVER_IN(DRV_PCM_OPEN) 770 mPcm = pcm_open(flags); 771 TRACE_DRIVER_OUT 772 if (!pcm_ready(mPcm)) { 773 LOGE("openPcmOut_l() cannot open pcm_out driver: %s\n", pcm_error(mPcm)); 774 TRACE_DRIVER_IN(DRV_PCM_CLOSE) 775 pcm_close(mPcm); 776 TRACE_DRIVER_OUT 777 mPcmOpenCnt--; 778 mPcm = NULL; 779 } 780 } 781 return mPcm; 782 } 783 784 void AudioHardware::closePcmOut_l() 785 { 786 LOGD("closePcmOut_l() mPcmOpenCnt: %d", mPcmOpenCnt); 787 if (mPcmOpenCnt == 0) { 788 LOGE("closePcmOut_l() mPcmOpenCnt == 0"); 789 return; 790 } 791 792 if (--mPcmOpenCnt == 0) { 793 TRACE_DRIVER_IN(DRV_PCM_CLOSE) 794 pcm_close(mPcm); 795 TRACE_DRIVER_OUT 796 mPcm = NULL; 797 } 798 } 799 800 struct mixer *AudioHardware::openMixer_l() 801 { 802 LOGV("openMixer_l() mMixerOpenCnt: %d", mMixerOpenCnt); 803 if (mMixerOpenCnt++ == 0) { 804 if (mMixer != NULL) { 805 LOGE("openMixer_l() mMixerOpenCnt == 0 and mMixer == %p\n", mMixer); 806 mMixerOpenCnt--; 807 return NULL; 808 } 809 TRACE_DRIVER_IN(DRV_MIXER_OPEN) 810 mMixer = mixer_open(); 811 TRACE_DRIVER_OUT 812 if (mMixer == NULL) { 813 LOGE("openMixer_l() cannot open mixer"); 814 mMixerOpenCnt--; 815 return NULL; 816 } 817 } 818 return mMixer; 819 } 820 821 void AudioHardware::closeMixer_l() 822 { 823 LOGV("closeMixer_l() mMixerOpenCnt: %d", mMixerOpenCnt); 824 if (mMixerOpenCnt == 0) { 825 LOGE("closeMixer_l() mMixerOpenCnt == 0"); 826 return; 827 } 828 829 if (--mMixerOpenCnt == 0) { 830 TRACE_DRIVER_IN(DRV_MIXER_CLOSE) 831 mixer_close(mMixer); 832 TRACE_DRIVER_OUT 833 mMixer = NULL; 834 } 835 } 836 837 const char *AudioHardware::getOutputRouteFromDevice(uint32_t device) 838 { 839 switch (device) { 840 case AudioSystem::DEVICE_OUT_EARPIECE: 841 return "RCV"; 842 case AudioSystem::DEVICE_OUT_SPEAKER: 843 if (mMode == AudioSystem::MODE_RINGTONE) return "RING_SPK"; 844 else return "SPK"; 845 case AudioSystem::DEVICE_OUT_WIRED_HEADPHONE: 846 if (mMode == AudioSystem::MODE_RINGTONE) return "RING_NO_MIC"; 847 else return "HP_NO_MIC"; 848 case AudioSystem::DEVICE_OUT_WIRED_HEADSET: 849 if (mMode == AudioSystem::MODE_RINGTONE) return "RING_HP"; 850 else return "HP"; 851 case (AudioSystem::DEVICE_OUT_SPEAKER|AudioSystem::DEVICE_OUT_WIRED_HEADPHONE): 852 case (AudioSystem::DEVICE_OUT_SPEAKER|AudioSystem::DEVICE_OUT_WIRED_HEADSET): 853 if (mMode == AudioSystem::MODE_RINGTONE) return "RING_SPK_HP"; 854 else return "SPK_HP"; 855 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO: 856 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 857 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 858 return "BT"; 859 default: 860 return "OFF"; 861 } 862 } 863 864 const char *AudioHardware::getVoiceRouteFromDevice(uint32_t device) 865 { 866 switch (device) { 867 case AudioSystem::DEVICE_OUT_EARPIECE: 868 return "RCV"; 869 case AudioSystem::DEVICE_OUT_SPEAKER: 870 return "SPK"; 871 case AudioSystem::DEVICE_OUT_WIRED_HEADPHONE: 872 case AudioSystem::DEVICE_OUT_WIRED_HEADSET: 873 switch (mTTYMode) { 874 case TTY_MODE_VCO: 875 return "TTY_VCO"; 876 case TTY_MODE_HCO: 877 return "TTY_HCO"; 878 case TTY_MODE_FULL: 879 return "TTY_FULL"; 880 case TTY_MODE_OFF: 881 default: 882 if (device == AudioSystem::DEVICE_OUT_WIRED_HEADPHONE) { 883 return "HP_NO_MIC"; 884 } else { 885 return "HP"; 886 } 887 } 888 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO: 889 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 890 case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 891 return "BT"; 892 default: 893 return "OFF"; 894 } 895 } 896 897 const char *AudioHardware::getInputRouteFromDevice(uint32_t device) 898 { 899 if (mMicMute) { 900 return "MIC OFF"; 901 } 902 903 switch (device) { 904 case AudioSystem::DEVICE_IN_BUILTIN_MIC: 905 return "Main Mic"; 906 case AudioSystem::DEVICE_IN_WIRED_HEADSET: 907 return "Hands Free Mic"; 908 case AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET: 909 return "BT Sco Mic"; 910 default: 911 return "MIC OFF"; 912 } 913 } 914 915 uint32_t AudioHardware::getInputSampleRate(uint32_t sampleRate) 916 { 917 uint32_t i; 918 uint32_t prevDelta; 919 uint32_t delta; 920 921 for (i = 0, prevDelta = 0xFFFFFFFF; i < sizeof(inputSamplingRates)/sizeof(uint32_t); i++, prevDelta = delta) { 922 delta = abs(sampleRate - inputSamplingRates[i]); 923 if (delta > prevDelta) break; 924 } 925 // i is always > 0 here 926 return inputSamplingRates[i-1]; 927 } 928 929 // getActiveInput_l() must be called with mLock held 930 sp <AudioHardware::AudioStreamInALSA> AudioHardware::getActiveInput_l() 931 { 932 sp< AudioHardware::AudioStreamInALSA> spIn; 933 934 for (size_t i = 0; i < mInputs.size(); i++) { 935 // return first input found not being in standby mode 936 // as only one input can be in this state 937 if (!mInputs[i]->checkStandby()) { 938 spIn = mInputs[i]; 939 break; 940 } 941 } 942 943 return spIn; 944 } 945 946 status_t AudioHardware::setInputSource_l(audio_source source) 947 { 948 LOGV("setInputSource_l(%d)", source); 949 if (source != mInputSource) { 950 if ((source == AUDIO_SOURCE_DEFAULT) || (mMode != AudioSystem::MODE_IN_CALL)) { 951 if (mMixer) { 952 TRACE_DRIVER_IN(DRV_MIXER_GET) 953 struct mixer_ctl *ctl= mixer_get_control(mMixer, "Input Source", 0); 954 TRACE_DRIVER_OUT 955 if (ctl == NULL) { 956 return NO_INIT; 957 } 958 const char* sourceName; 959 switch (source) { 960 case AUDIO_SOURCE_DEFAULT: // intended fall-through 961 case AUDIO_SOURCE_MIC: 962 sourceName = inputPathNameDefault; 963 break; 964 case AUDIO_SOURCE_VOICE_COMMUNICATION: 965 sourceName = inputPathNameVoiceCommunication; 966 break; 967 case AUDIO_SOURCE_CAMCORDER: 968 sourceName = inputPathNameCamcorder; 969 break; 970 case AUDIO_SOURCE_VOICE_RECOGNITION: 971 sourceName = inputPathNameVoiceRecognition; 972 break; 973 case AUDIO_SOURCE_VOICE_UPLINK: // intended fall-through 974 case AUDIO_SOURCE_VOICE_DOWNLINK: // intended fall-through 975 case AUDIO_SOURCE_VOICE_CALL: // intended fall-through 976 default: 977 return NO_INIT; 978 } 979 LOGV("mixer_ctl_select, Input Source, (%s)", sourceName); 980 TRACE_DRIVER_IN(DRV_MIXER_SEL) 981 mixer_ctl_select(ctl, sourceName); 982 TRACE_DRIVER_OUT 983 } 984 } 985 mInputSource = source; 986 } 987 988 return NO_ERROR; 989 } 990 991 992 //------------------------------------------------------------------------------ 993 // AudioStreamOutALSA 994 //------------------------------------------------------------------------------ 995 996 AudioHardware::AudioStreamOutALSA::AudioStreamOutALSA() : 997 mHardware(0), mPcm(0), mMixer(0), mRouteCtl(0), 998 mStandby(true), mDevices(0), mChannels(AUDIO_HW_OUT_CHANNELS), 999 mSampleRate(AUDIO_HW_OUT_SAMPLERATE), mBufferSize(AUDIO_HW_OUT_PERIOD_BYTES), 1000 mDriverOp(DRV_NONE), mStandbyCnt(0), mSleepReq(false) 1001 { 1002 } 1003 1004 status_t AudioHardware::AudioStreamOutALSA::set( 1005 AudioHardware* hw, uint32_t devices, int *pFormat, 1006 uint32_t *pChannels, uint32_t *pRate) 1007 { 1008 int lFormat = pFormat ? *pFormat : 0; 1009 uint32_t lChannels = pChannels ? *pChannels : 0; 1010 uint32_t lRate = pRate ? *pRate : 0; 1011 1012 mHardware = hw; 1013 mDevices = devices; 1014 1015 // fix up defaults 1016 if (lFormat == 0) lFormat = format(); 1017 if (lChannels == 0) lChannels = channels(); 1018 if (lRate == 0) lRate = sampleRate(); 1019 1020 // check values 1021 if ((lFormat != format()) || 1022 (lChannels != channels()) || 1023 (lRate != sampleRate())) { 1024 if (pFormat) *pFormat = format(); 1025 if (pChannels) *pChannels = channels(); 1026 if (pRate) *pRate = sampleRate(); 1027 return BAD_VALUE; 1028 } 1029 1030 if (pFormat) *pFormat = lFormat; 1031 if (pChannels) *pChannels = lChannels; 1032 if (pRate) *pRate = lRate; 1033 1034 mChannels = lChannels; 1035 mSampleRate = lRate; 1036 mBufferSize = AUDIO_HW_OUT_PERIOD_BYTES; 1037 1038 return NO_ERROR; 1039 } 1040 1041 AudioHardware::AudioStreamOutALSA::~AudioStreamOutALSA() 1042 { 1043 standby(); 1044 } 1045 1046 ssize_t AudioHardware::AudioStreamOutALSA::write(const void* buffer, size_t bytes) 1047 { 1048 // LOGV("AudioStreamOutALSA::write(%p, %u)", buffer, bytes); 1049 status_t status = NO_INIT; 1050 const uint8_t* p = static_cast<const uint8_t*>(buffer); 1051 int ret; 1052 1053 if (mHardware == NULL) return NO_INIT; 1054 1055 if (mSleepReq) { 1056 // 10ms are always shorter than the time to reconfigure the audio path 1057 // which is the only condition when mSleepReq would be true. 1058 usleep(10000); 1059 } 1060 1061 { // scope for the lock 1062 1063 AutoMutex lock(mLock); 1064 1065 if (mStandby) { 1066 AutoMutex hwLock(mHardware->lock()); 1067 1068 LOGD("AudioHardware pcm playback is exiting standby."); 1069 acquire_wake_lock (PARTIAL_WAKE_LOCK, "AudioOutLock"); 1070 1071 sp<AudioStreamInALSA> spIn = mHardware->getActiveInput_l(); 1072 while (spIn != 0) { 1073 int cnt = spIn->prepareLock(); 1074 mHardware->lock().unlock(); 1075 // Mutex acquisition order is always out -> in -> hw 1076 spIn->lock(); 1077 mHardware->lock().lock(); 1078 // make sure that another thread did not change input state 1079 // while the mutex is released 1080 if ((spIn == mHardware->getActiveInput_l()) && 1081 (cnt == spIn->standbyCnt())) { 1082 LOGV("AudioStreamOutALSA::write() force input standby"); 1083 spIn->close_l(); 1084 break; 1085 } 1086 spIn->unlock(); 1087 spIn = mHardware->getActiveInput_l(); 1088 } 1089 // spIn is not 0 here only if the input was active and has been 1090 // closed above 1091 1092 // open output before input 1093 open_l(); 1094 1095 if (spIn != 0) { 1096 if (spIn->open_l() != NO_ERROR) { 1097 spIn->doStandby_l(); 1098 } 1099 spIn->unlock(); 1100 } 1101 if (mPcm == NULL) { 1102 release_wake_lock("AudioOutLock"); 1103 goto Error; 1104 } 1105 mStandby = false; 1106 } 1107 1108 TRACE_DRIVER_IN(DRV_PCM_WRITE) 1109 ret = pcm_write(mPcm,(void*) p, bytes); 1110 TRACE_DRIVER_OUT 1111 1112 if (ret == 0) { 1113 return bytes; 1114 } 1115 LOGW("write error: %d", errno); 1116 status = -errno; 1117 } 1118 Error: 1119 1120 standby(); 1121 1122 // Simulate audio output timing in case of error 1123 usleep((((bytes * 1000) / frameSize()) * 1000) / sampleRate()); 1124 1125 return status; 1126 } 1127 1128 status_t AudioHardware::AudioStreamOutALSA::standby() 1129 { 1130 if (mHardware == NULL) return NO_INIT; 1131 1132 mSleepReq = true; 1133 { 1134 AutoMutex lock(mLock); 1135 mSleepReq = false; 1136 1137 { // scope for the AudioHardware lock 1138 AutoMutex hwLock(mHardware->lock()); 1139 1140 doStandby_l(); 1141 } 1142 } 1143 1144 return NO_ERROR; 1145 } 1146 1147 void AudioHardware::AudioStreamOutALSA::doStandby_l() 1148 { 1149 mStandbyCnt++; 1150 1151 if (!mStandby) { 1152 LOGD("AudioHardware pcm playback is going to standby."); 1153 release_wake_lock("AudioOutLock"); 1154 mStandby = true; 1155 } 1156 1157 close_l(); 1158 } 1159 1160 void AudioHardware::AudioStreamOutALSA::close_l() 1161 { 1162 if (mMixer) { 1163 mHardware->closeMixer_l(); 1164 mMixer = NULL; 1165 mRouteCtl = NULL; 1166 } 1167 if (mPcm) { 1168 mHardware->closePcmOut_l(); 1169 mPcm = NULL; 1170 } 1171 } 1172 1173 status_t AudioHardware::AudioStreamOutALSA::open_l() 1174 { 1175 LOGV("open pcm_out driver"); 1176 mPcm = mHardware->openPcmOut_l(); 1177 if (mPcm == NULL) { 1178 return NO_INIT; 1179 } 1180 1181 mMixer = mHardware->openMixer_l(); 1182 if (mMixer) { 1183 LOGV("open playback normal"); 1184 TRACE_DRIVER_IN(DRV_MIXER_GET) 1185 mRouteCtl = mixer_get_control(mMixer, "Playback Path", 0); 1186 TRACE_DRIVER_OUT 1187 } 1188 if (mHardware->mode() != AudioSystem::MODE_IN_CALL) { 1189 const char *route = mHardware->getOutputRouteFromDevice(mDevices); 1190 LOGV("write() wakeup setting route %s", route); 1191 if (mRouteCtl) { 1192 TRACE_DRIVER_IN(DRV_MIXER_SEL) 1193 mixer_ctl_select(mRouteCtl, route); 1194 TRACE_DRIVER_OUT 1195 } 1196 } 1197 return NO_ERROR; 1198 } 1199 1200 status_t AudioHardware::AudioStreamOutALSA::dump(int fd, const Vector<String16>& args) 1201 { 1202 const size_t SIZE = 256; 1203 char buffer[SIZE]; 1204 String8 result; 1205 1206 bool locked = tryLock(mLock); 1207 if (!locked) { 1208 snprintf(buffer, SIZE, "\n\t\tAudioStreamOutALSA maybe deadlocked\n"); 1209 } else { 1210 mLock.unlock(); 1211 } 1212 1213 snprintf(buffer, SIZE, "\t\tmHardware: %p\n", mHardware); 1214 result.append(buffer); 1215 snprintf(buffer, SIZE, "\t\tmPcm: %p\n", mPcm); 1216 result.append(buffer); 1217 snprintf(buffer, SIZE, "\t\tmMixer: %p\n", mMixer); 1218 result.append(buffer); 1219 snprintf(buffer, SIZE, "\t\tmRouteCtl: %p\n", mRouteCtl); 1220 result.append(buffer); 1221 snprintf(buffer, SIZE, "\t\tStandby %s\n", (mStandby) ? "ON" : "OFF"); 1222 result.append(buffer); 1223 snprintf(buffer, SIZE, "\t\tmDevices: 0x%08x\n", mDevices); 1224 result.append(buffer); 1225 snprintf(buffer, SIZE, "\t\tmChannels: 0x%08x\n", mChannels); 1226 result.append(buffer); 1227 snprintf(buffer, SIZE, "\t\tmSampleRate: %d\n", mSampleRate); 1228 result.append(buffer); 1229 snprintf(buffer, SIZE, "\t\tmBufferSize: %d\n", mBufferSize); 1230 result.append(buffer); 1231 snprintf(buffer, SIZE, "\t\tmDriverOp: %d\n", mDriverOp); 1232 result.append(buffer); 1233 1234 ::write(fd, result.string(), result.size()); 1235 1236 return NO_ERROR; 1237 } 1238 1239 bool AudioHardware::AudioStreamOutALSA::checkStandby() 1240 { 1241 return mStandby; 1242 } 1243 1244 status_t AudioHardware::AudioStreamOutALSA::setParameters(const String8& keyValuePairs) 1245 { 1246 AudioParameter param = AudioParameter(keyValuePairs); 1247 status_t status = NO_ERROR; 1248 int device; 1249 LOGD("AudioStreamOutALSA::setParameters() %s", keyValuePairs.string()); 1250 1251 if (mHardware == NULL) return NO_INIT; 1252 1253 mSleepReq = true; 1254 { 1255 AutoMutex lock(mLock); 1256 mSleepReq = false; 1257 if (param.getInt(String8(AudioParameter::keyRouting), device) == NO_ERROR) 1258 { 1259 if (device != 0) { 1260 AutoMutex hwLock(mHardware->lock()); 1261 1262 if (mDevices != (uint32_t)device) { 1263 mDevices = (uint32_t)device; 1264 if (mHardware->mode() != AudioSystem::MODE_IN_CALL) { 1265 doStandby_l(); 1266 } 1267 } 1268 if (mHardware->mode() == AudioSystem::MODE_IN_CALL) { 1269 mHardware->setIncallPath_l(device); 1270 } 1271 } 1272 param.remove(String8(AudioParameter::keyRouting)); 1273 } 1274 } 1275 1276 if (param.size()) { 1277 status = BAD_VALUE; 1278 } 1279 1280 1281 return status; 1282 1283 } 1284 1285 String8 AudioHardware::AudioStreamOutALSA::getParameters(const String8& keys) 1286 { 1287 AudioParameter param = AudioParameter(keys); 1288 String8 value; 1289 String8 key = String8(AudioParameter::keyRouting); 1290 1291 if (param.get(key, value) == NO_ERROR) { 1292 param.addInt(key, (int)mDevices); 1293 } 1294 1295 LOGV("AudioStreamOutALSA::getParameters() %s", param.toString().string()); 1296 return param.toString(); 1297 } 1298 1299 status_t AudioHardware::AudioStreamOutALSA::getRenderPosition(uint32_t *dspFrames) 1300 { 1301 //TODO 1302 return INVALID_OPERATION; 1303 } 1304 1305 int AudioHardware::AudioStreamOutALSA::prepareLock() 1306 { 1307 // request sleep next time write() is called so that caller can acquire 1308 // mLock 1309 mSleepReq = true; 1310 return mStandbyCnt; 1311 } 1312 1313 void AudioHardware::AudioStreamOutALSA::lock() 1314 { 1315 mLock.lock(); 1316 mSleepReq = false; 1317 } 1318 1319 void AudioHardware::AudioStreamOutALSA::unlock() { 1320 mLock.unlock(); 1321 } 1322 1323 //------------------------------------------------------------------------------ 1324 // AudioStreamInALSA 1325 //------------------------------------------------------------------------------ 1326 1327 AudioHardware::AudioStreamInALSA::AudioStreamInALSA() : 1328 mHardware(0), mPcm(0), mMixer(0), mRouteCtl(0), 1329 mStandby(true), mDevices(0), mChannels(AUDIO_HW_IN_CHANNELS), mChannelCount(1), 1330 mSampleRate(AUDIO_HW_IN_SAMPLERATE), mBufferSize(AUDIO_HW_IN_PERIOD_BYTES), 1331 mDownSampler(NULL), mReadStatus(NO_ERROR), mDriverOp(DRV_NONE), 1332 mStandbyCnt(0), mSleepReq(false) 1333 { 1334 } 1335 1336 status_t AudioHardware::AudioStreamInALSA::set( 1337 AudioHardware* hw, uint32_t devices, int *pFormat, 1338 uint32_t *pChannels, uint32_t *pRate, AudioSystem::audio_in_acoustics acoustics) 1339 { 1340 if (pFormat == 0 || *pFormat != AUDIO_HW_IN_FORMAT) { 1341 *pFormat = AUDIO_HW_IN_FORMAT; 1342 return BAD_VALUE; 1343 } 1344 if (pRate == 0) { 1345 return BAD_VALUE; 1346 } 1347 uint32_t rate = AudioHardware::getInputSampleRate(*pRate); 1348 if (rate != *pRate) { 1349 *pRate = rate; 1350 return BAD_VALUE; 1351 } 1352 1353 if (pChannels == 0 || (*pChannels != AudioSystem::CHANNEL_IN_MONO && 1354 *pChannels != AudioSystem::CHANNEL_IN_STEREO)) { 1355 *pChannels = AUDIO_HW_IN_CHANNELS; 1356 return BAD_VALUE; 1357 } 1358 1359 mHardware = hw; 1360 1361 LOGV("AudioStreamInALSA::set(%d, %d, %u)", *pFormat, *pChannels, *pRate); 1362 1363 mBufferSize = getBufferSize(*pRate, AudioSystem::popCount(*pChannels)); 1364 mDevices = devices; 1365 mChannels = *pChannels; 1366 mChannelCount = AudioSystem::popCount(mChannels); 1367 mSampleRate = rate; 1368 if (mSampleRate != AUDIO_HW_OUT_SAMPLERATE) { 1369 mDownSampler = new AudioHardware::DownSampler(mSampleRate, 1370 mChannelCount, 1371 AUDIO_HW_IN_PERIOD_SZ, 1372 this); 1373 status_t status = mDownSampler->initCheck(); 1374 if (status != NO_ERROR) { 1375 delete mDownSampler; 1376 LOGW("AudioStreamInALSA::set() downsampler init failed: %d", status); 1377 return status; 1378 } 1379 1380 mPcmIn = new int16_t[AUDIO_HW_IN_PERIOD_SZ * mChannelCount]; 1381 } 1382 return NO_ERROR; 1383 } 1384 1385 AudioHardware::AudioStreamInALSA::~AudioStreamInALSA() 1386 { 1387 standby(); 1388 if (mDownSampler != NULL) { 1389 delete mDownSampler; 1390 if (mPcmIn != NULL) { 1391 delete[] mPcmIn; 1392 } 1393 } 1394 } 1395 1396 ssize_t AudioHardware::AudioStreamInALSA::read(void* buffer, ssize_t bytes) 1397 { 1398 // LOGV("AudioStreamInALSA::read(%p, %u)", buffer, bytes); 1399 status_t status = NO_INIT; 1400 int ret; 1401 1402 if (mHardware == NULL) return NO_INIT; 1403 1404 if (mSleepReq) { 1405 // 10ms are always shorter than the time to reconfigure the audio path 1406 // which is the only condition when mSleepReq would be true. 1407 usleep(10000); 1408 } 1409 1410 { // scope for the lock 1411 AutoMutex lock(mLock); 1412 1413 if (mStandby) { 1414 AutoMutex hwLock(mHardware->lock()); 1415 1416 LOGD("AudioHardware pcm capture is exiting standby."); 1417 acquire_wake_lock (PARTIAL_WAKE_LOCK, "AudioInLock"); 1418 1419 sp<AudioStreamOutALSA> spOut = mHardware->output(); 1420 while (spOut != 0) { 1421 if (!spOut->checkStandby()) { 1422 int cnt = spOut->prepareLock(); 1423 mHardware->lock().unlock(); 1424 mLock.unlock(); 1425 // Mutex acquisition order is always out -> in -> hw 1426 spOut->lock(); 1427 mLock.lock(); 1428 mHardware->lock().lock(); 1429 // make sure that another thread did not change output state 1430 // while the mutex is released 1431 if ((spOut == mHardware->output()) && (cnt == spOut->standbyCnt())) { 1432 LOGV("AudioStreamInALSA::read() force output standby"); 1433 spOut->close_l(); 1434 break; 1435 } 1436 spOut->unlock(); 1437 spOut = mHardware->output(); 1438 } else { 1439 spOut.clear(); 1440 } 1441 } 1442 // spOut is not 0 here only if the output was active and has been 1443 // closed above 1444 1445 // open output before input 1446 if (spOut != 0) { 1447 if (spOut->open_l() != NO_ERROR) { 1448 spOut->doStandby_l(); 1449 } 1450 spOut->unlock(); 1451 } 1452 1453 open_l(); 1454 1455 if (mPcm == NULL) { 1456 release_wake_lock("AudioInLock"); 1457 goto Error; 1458 } 1459 mStandby = false; 1460 } 1461 1462 1463 if (mDownSampler != NULL) { 1464 size_t frames = bytes / frameSize(); 1465 size_t framesIn = 0; 1466 mReadStatus = 0; 1467 do { 1468 size_t outframes = frames - framesIn; 1469 mDownSampler->resample( 1470 (int16_t *)buffer + (framesIn * mChannelCount), 1471 &outframes); 1472 framesIn += outframes; 1473 } while ((framesIn < frames) && mReadStatus == 0); 1474 ret = mReadStatus; 1475 bytes = framesIn * frameSize(); 1476 } else { 1477 TRACE_DRIVER_IN(DRV_PCM_READ) 1478 ret = pcm_read(mPcm, buffer, bytes); 1479 TRACE_DRIVER_OUT 1480 } 1481 1482 if (ret == 0) { 1483 return bytes; 1484 } 1485 1486 LOGW("read error: %d", ret); 1487 status = ret; 1488 } 1489 1490 Error: 1491 1492 standby(); 1493 1494 // Simulate audio output timing in case of error 1495 usleep((((bytes * 1000) / frameSize()) * 1000) / sampleRate()); 1496 1497 return status; 1498 } 1499 1500 status_t AudioHardware::AudioStreamInALSA::standby() 1501 { 1502 if (mHardware == NULL) return NO_INIT; 1503 1504 mSleepReq = true; 1505 { 1506 AutoMutex lock(mLock); 1507 mSleepReq = false; 1508 1509 { // scope for AudioHardware lock 1510 AutoMutex hwLock(mHardware->lock()); 1511 1512 doStandby_l(); 1513 } 1514 } 1515 return NO_ERROR; 1516 } 1517 1518 void AudioHardware::AudioStreamInALSA::doStandby_l() 1519 { 1520 mStandbyCnt++; 1521 1522 if (!mStandby) { 1523 LOGD("AudioHardware pcm capture is going to standby."); 1524 release_wake_lock("AudioInLock"); 1525 mStandby = true; 1526 } 1527 close_l(); 1528 } 1529 1530 void AudioHardware::AudioStreamInALSA::close_l() 1531 { 1532 if (mMixer) { 1533 mHardware->closeMixer_l(); 1534 mMixer = NULL; 1535 mRouteCtl = NULL; 1536 } 1537 1538 if (mPcm) { 1539 TRACE_DRIVER_IN(DRV_PCM_CLOSE) 1540 pcm_close(mPcm); 1541 TRACE_DRIVER_OUT 1542 mPcm = NULL; 1543 } 1544 } 1545 1546 status_t AudioHardware::AudioStreamInALSA::open_l() 1547 { 1548 unsigned flags = PCM_IN; 1549 if (mChannels == AudioSystem::CHANNEL_IN_MONO) { 1550 flags |= PCM_MONO; 1551 } 1552 flags |= (AUDIO_HW_IN_PERIOD_MULT - 1) << PCM_PERIOD_SZ_SHIFT; 1553 flags |= (AUDIO_HW_IN_PERIOD_CNT - PCM_PERIOD_CNT_MIN) 1554 << PCM_PERIOD_CNT_SHIFT; 1555 1556 LOGV("open pcm_in driver"); 1557 TRACE_DRIVER_IN(DRV_PCM_OPEN) 1558 mPcm = pcm_open(flags); 1559 TRACE_DRIVER_OUT 1560 if (!pcm_ready(mPcm)) { 1561 LOGE("cannot open pcm_in driver: %s\n", pcm_error(mPcm)); 1562 TRACE_DRIVER_IN(DRV_PCM_CLOSE) 1563 pcm_close(mPcm); 1564 TRACE_DRIVER_OUT 1565 mPcm = NULL; 1566 return NO_INIT; 1567 } 1568 1569 if (mDownSampler != NULL) { 1570 mInPcmInBuf = 0; 1571 mDownSampler->reset(); 1572 } 1573 1574 mMixer = mHardware->openMixer_l(); 1575 if (mMixer) { 1576 TRACE_DRIVER_IN(DRV_MIXER_GET) 1577 mRouteCtl = mixer_get_control(mMixer, "Capture MIC Path", 0); 1578 TRACE_DRIVER_OUT 1579 } 1580 1581 if (mHardware->mode() != AudioSystem::MODE_IN_CALL) { 1582 const char *route = mHardware->getInputRouteFromDevice(mDevices); 1583 LOGV("read() wakeup setting route %s", route); 1584 if (mRouteCtl) { 1585 TRACE_DRIVER_IN(DRV_MIXER_SEL) 1586 mixer_ctl_select(mRouteCtl, route); 1587 TRACE_DRIVER_OUT 1588 } 1589 } 1590 1591 return NO_ERROR; 1592 } 1593 1594 status_t AudioHardware::AudioStreamInALSA::dump(int fd, const Vector<String16>& args) 1595 { 1596 const size_t SIZE = 256; 1597 char buffer[SIZE]; 1598 String8 result; 1599 1600 bool locked = tryLock(mLock); 1601 if (!locked) { 1602 snprintf(buffer, SIZE, "\n\t\tAudioStreamInALSA maybe deadlocked\n"); 1603 } else { 1604 mLock.unlock(); 1605 } 1606 1607 snprintf(buffer, SIZE, "\t\tmHardware: %p\n", mHardware); 1608 result.append(buffer); 1609 snprintf(buffer, SIZE, "\t\tmPcm: %p\n", mPcm); 1610 result.append(buffer); 1611 snprintf(buffer, SIZE, "\t\tmMixer: %p\n", mMixer); 1612 result.append(buffer); 1613 snprintf(buffer, SIZE, "\t\tStandby %s\n", (mStandby) ? "ON" : "OFF"); 1614 result.append(buffer); 1615 snprintf(buffer, SIZE, "\t\tmDevices: 0x%08x\n", mDevices); 1616 result.append(buffer); 1617 snprintf(buffer, SIZE, "\t\tmChannels: 0x%08x\n", mChannels); 1618 result.append(buffer); 1619 snprintf(buffer, SIZE, "\t\tmSampleRate: %d\n", mSampleRate); 1620 result.append(buffer); 1621 snprintf(buffer, SIZE, "\t\tmBufferSize: %d\n", mBufferSize); 1622 result.append(buffer); 1623 snprintf(buffer, SIZE, "\t\tmDriverOp: %d\n", mDriverOp); 1624 result.append(buffer); 1625 write(fd, result.string(), result.size()); 1626 1627 return NO_ERROR; 1628 } 1629 1630 bool AudioHardware::AudioStreamInALSA::checkStandby() 1631 { 1632 return mStandby; 1633 } 1634 1635 status_t AudioHardware::AudioStreamInALSA::setParameters(const String8& keyValuePairs) 1636 { 1637 AudioParameter param = AudioParameter(keyValuePairs); 1638 status_t status = NO_ERROR; 1639 int value; 1640 1641 LOGD("AudioStreamInALSA::setParameters() %s", keyValuePairs.string()); 1642 1643 if (mHardware == NULL) return NO_INIT; 1644 1645 mSleepReq = true; 1646 { 1647 AutoMutex lock(mLock); 1648 mSleepReq = false; 1649 1650 if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR) { 1651 AutoMutex hwLock(mHardware->lock()); 1652 1653 mHardware->openMixer_l(); 1654 mHardware->setInputSource_l((audio_source)value); 1655 mHardware->closeMixer_l(); 1656 1657 param.remove(String8(AudioParameter::keyInputSource)); 1658 } 1659 1660 if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) 1661 { 1662 if (value != 0) { 1663 AutoMutex hwLock(mHardware->lock()); 1664 1665 if (mDevices != (uint32_t)value) { 1666 mDevices = (uint32_t)value; 1667 if (mHardware->mode() != AudioSystem::MODE_IN_CALL) { 1668 doStandby_l(); 1669 } 1670 } 1671 } 1672 param.remove(String8(AudioParameter::keyRouting)); 1673 } 1674 } 1675 1676 1677 if (param.size()) { 1678 status = BAD_VALUE; 1679 } 1680 1681 return status; 1682 1683 } 1684 1685 String8 AudioHardware::AudioStreamInALSA::getParameters(const String8& keys) 1686 { 1687 AudioParameter param = AudioParameter(keys); 1688 String8 value; 1689 String8 key = String8(AudioParameter::keyRouting); 1690 1691 if (param.get(key, value) == NO_ERROR) { 1692 param.addInt(key, (int)mDevices); 1693 } 1694 1695 LOGV("AudioStreamInALSA::getParameters() %s", param.toString().string()); 1696 return param.toString(); 1697 } 1698 1699 status_t AudioHardware::AudioStreamInALSA::getNextBuffer(AudioHardware::BufferProvider::Buffer* buffer) 1700 { 1701 if (mPcm == NULL) { 1702 buffer->raw = NULL; 1703 buffer->frameCount = 0; 1704 mReadStatus = NO_INIT; 1705 return NO_INIT; 1706 } 1707 1708 if (mInPcmInBuf == 0) { 1709 TRACE_DRIVER_IN(DRV_PCM_READ) 1710 mReadStatus = pcm_read(mPcm,(void*) mPcmIn, AUDIO_HW_IN_PERIOD_SZ * frameSize()); 1711 TRACE_DRIVER_OUT 1712 if (mReadStatus != 0) { 1713 buffer->raw = NULL; 1714 buffer->frameCount = 0; 1715 return mReadStatus; 1716 } 1717 mInPcmInBuf = AUDIO_HW_IN_PERIOD_SZ; 1718 } 1719 1720 buffer->frameCount = (buffer->frameCount > mInPcmInBuf) ? mInPcmInBuf : buffer->frameCount; 1721 buffer->i16 = mPcmIn + (AUDIO_HW_IN_PERIOD_SZ - mInPcmInBuf) * mChannelCount; 1722 1723 return mReadStatus; 1724 } 1725 1726 void AudioHardware::AudioStreamInALSA::releaseBuffer(Buffer* buffer) 1727 { 1728 mInPcmInBuf -= buffer->frameCount; 1729 } 1730 1731 size_t AudioHardware::AudioStreamInALSA::getBufferSize(uint32_t sampleRate, int channelCount) 1732 { 1733 size_t ratio; 1734 1735 switch (sampleRate) { 1736 case 8000: 1737 case 11025: 1738 ratio = 4; 1739 break; 1740 case 16000: 1741 case 22050: 1742 ratio = 2; 1743 break; 1744 case 44100: 1745 default: 1746 ratio = 1; 1747 break; 1748 } 1749 1750 return (AUDIO_HW_IN_PERIOD_SZ*channelCount*sizeof(int16_t)) / ratio ; 1751 } 1752 1753 int AudioHardware::AudioStreamInALSA::prepareLock() 1754 { 1755 // request sleep next time read() is called so that caller can acquire 1756 // mLock 1757 mSleepReq = true; 1758 return mStandbyCnt; 1759 } 1760 1761 void AudioHardware::AudioStreamInALSA::lock() 1762 { 1763 mLock.lock(); 1764 mSleepReq = false; 1765 } 1766 1767 void AudioHardware::AudioStreamInALSA::unlock() { 1768 mLock.unlock(); 1769 } 1770 1771 //------------------------------------------------------------------------------ 1772 // DownSampler 1773 //------------------------------------------------------------------------------ 1774 1775 /* 1776 * 2.30 fixed point FIR filter coefficients for conversion 44100 -> 22050. 1777 * (Works equivalently for 22010 -> 11025 or any other halving, of course.) 1778 * 1779 * Transition band from about 18 kHz, passband ripple < 0.1 dB, 1780 * stopband ripple at about -55 dB, linear phase. 1781 * 1782 * Design and display in MATLAB or Octave using: 1783 * 1784 * filter = fir1(19, 0.5); filter = round(filter * 2**30); freqz(filter * 2**-30); 1785 */ 1786 static const int32_t filter_22khz_coeff[] = { 1787 2089257, 2898328, -5820678, -10484531, 1788 19038724, 30542725, -50469415, -81505260, 1789 152544464, 478517512, 478517512, 152544464, 1790 -81505260, -50469415, 30542725, 19038724, 1791 -10484531, -5820678, 2898328, 2089257, 1792 }; 1793 #define NUM_COEFF_22KHZ (sizeof(filter_22khz_coeff) / sizeof(filter_22khz_coeff[0])) 1794 #define OVERLAP_22KHZ (NUM_COEFF_22KHZ - 2) 1795 1796 /* 1797 * Convolution of signals A and reverse(B). (In our case, the filter response 1798 * is symmetric, so the reversing doesn't matter.) 1799 * A is taken to be in 0.16 fixed-point, and B is taken to be in 2.30 fixed-point. 1800 * The answer will be in 16.16 fixed-point, unclipped. 1801 * 1802 * This function would probably be the prime candidate for SIMD conversion if 1803 * you want more speed. 1804 */ 1805 int32_t fir_convolve(const int16_t* a, const int32_t* b, int num_samples) 1806 { 1807 int32_t sum = 1 << 13; 1808 for (int i = 0; i < num_samples; ++i) { 1809 sum += a[i] * (b[i] >> 16); 1810 } 1811 return sum >> 14; 1812 } 1813 1814 /* Clip from 16.16 fixed-point to 0.16 fixed-point. */ 1815 int16_t clip(int32_t x) 1816 { 1817 if (x < -32768) { 1818 return -32768; 1819 } else if (x > 32767) { 1820 return 32767; 1821 } else { 1822 return x; 1823 } 1824 } 1825 1826 /* 1827 * Convert a chunk from 44 kHz to 22 kHz. Will update num_samples_in and num_samples_out 1828 * accordingly, since it may leave input samples in the buffer due to overlap. 1829 * 1830 * Input and output are taken to be in 0.16 fixed-point. 1831 */ 1832 void resample_2_1(int16_t* input, int16_t* output, int* num_samples_in, int* num_samples_out) 1833 { 1834 if (*num_samples_in < (int)NUM_COEFF_22KHZ) { 1835 *num_samples_out = 0; 1836 return; 1837 } 1838 1839 int odd_smp = *num_samples_in & 0x1; 1840 int num_samples = *num_samples_in - odd_smp - OVERLAP_22KHZ; 1841 1842 for (int i = 0; i < num_samples; i += 2) { 1843 output[i / 2] = clip(fir_convolve(input + i, filter_22khz_coeff, NUM_COEFF_22KHZ)); 1844 } 1845 1846 memmove(input, input + num_samples, (OVERLAP_22KHZ + odd_smp) * sizeof(*input)); 1847 *num_samples_out = num_samples / 2; 1848 *num_samples_in = OVERLAP_22KHZ + odd_smp; 1849 } 1850 1851 /* 1852 * 2.30 fixed point FIR filter coefficients for conversion 22050 -> 16000, 1853 * or 11025 -> 8000. 1854 * 1855 * Transition band from about 14 kHz, passband ripple < 0.1 dB, 1856 * stopband ripple at about -50 dB, linear phase. 1857 * 1858 * Design and display in MATLAB or Octave using: 1859 * 1860 * filter = fir1(23, 16000 / 22050); filter = round(filter * 2**30); freqz(filter * 2**-30); 1861 */ 1862 static const int32_t filter_16khz_coeff[] = { 1863 2057290, -2973608, 1880478, 4362037, 1864 -14639744, 18523609, -1609189, -38502470, 1865 78073125, -68353935, -59103896, 617555440, 1866 617555440, -59103896, -68353935, 78073125, 1867 -38502470, -1609189, 18523609, -14639744, 1868 4362037, 1880478, -2973608, 2057290, 1869 }; 1870 #define NUM_COEFF_16KHZ (sizeof(filter_16khz_coeff) / sizeof(filter_16khz_coeff[0])) 1871 #define OVERLAP_16KHZ (NUM_COEFF_16KHZ - 1) 1872 1873 /* 1874 * Convert a chunk from 22 kHz to 16 kHz. Will update num_samples_in and 1875 * num_samples_out accordingly, since it may leave input samples in the buffer 1876 * due to overlap. 1877 * 1878 * This implementation is rather ad-hoc; it first low-pass filters the data 1879 * into a temporary buffer, and then converts chunks of 441 input samples at a 1880 * time into 320 output samples by simple linear interpolation. A better 1881 * implementation would use a polyphase filter bank to do these two operations 1882 * in one step. 1883 * 1884 * Input and output are taken to be in 0.16 fixed-point. 1885 */ 1886 1887 #define RESAMPLE_16KHZ_SAMPLES_IN 441 1888 #define RESAMPLE_16KHZ_SAMPLES_OUT 320 1889 1890 void resample_441_320(int16_t* input, int16_t* output, int* num_samples_in, int* num_samples_out) 1891 { 1892 const int num_blocks = (*num_samples_in - OVERLAP_16KHZ) / RESAMPLE_16KHZ_SAMPLES_IN; 1893 if (num_blocks < 1) { 1894 *num_samples_out = 0; 1895 return; 1896 } 1897 1898 for (int i = 0; i < num_blocks; ++i) { 1899 uint32_t tmp[RESAMPLE_16KHZ_SAMPLES_IN]; 1900 for (int j = 0; j < RESAMPLE_16KHZ_SAMPLES_IN; ++j) { 1901 tmp[j] = fir_convolve(input + i * RESAMPLE_16KHZ_SAMPLES_IN + j, 1902 filter_16khz_coeff, 1903 NUM_COEFF_16KHZ); 1904 } 1905 1906 const float step_float = (float)RESAMPLE_16KHZ_SAMPLES_IN / (float)RESAMPLE_16KHZ_SAMPLES_OUT; 1907 const uint32_t step = (uint32_t)(step_float * 32768.0f + 0.5f); // 17.15 fixed point 1908 1909 uint32_t in_sample_num = 0; // 17.15 fixed point 1910 for (int j = 0; j < RESAMPLE_16KHZ_SAMPLES_OUT; ++j, in_sample_num += step) { 1911 const uint32_t whole = in_sample_num >> 15; 1912 const uint32_t frac = (in_sample_num & 0x7fff); // 0.15 fixed point 1913 const int32_t s1 = tmp[whole]; 1914 const int32_t s2 = tmp[whole + 1]; 1915 *output++ = clip(s1 + (((s2 - s1) * (int32_t)frac) >> 15)); 1916 } 1917 1918 } 1919 1920 const int samples_consumed = num_blocks * RESAMPLE_16KHZ_SAMPLES_IN; 1921 memmove(input, input + samples_consumed, (*num_samples_in - samples_consumed) * sizeof(*input)); 1922 *num_samples_in -= samples_consumed; 1923 *num_samples_out = RESAMPLE_16KHZ_SAMPLES_OUT * num_blocks; 1924 } 1925 1926 1927 AudioHardware::DownSampler::DownSampler(uint32_t outSampleRate, 1928 uint32_t channelCount, 1929 uint32_t frameCount, 1930 AudioHardware::BufferProvider* provider) 1931 : mStatus(NO_INIT), mProvider(provider), mSampleRate(outSampleRate), 1932 mChannelCount(channelCount), mFrameCount(frameCount), 1933 mInLeft(NULL), mInRight(NULL), mTmpLeft(NULL), mTmpRight(NULL), 1934 mTmp2Left(NULL), mTmp2Right(NULL), mOutLeft(NULL), mOutRight(NULL) 1935 1936 { 1937 LOGV("AudioHardware::DownSampler() cstor %p SR %d channels %d frames %d", 1938 this, mSampleRate, mChannelCount, mFrameCount); 1939 1940 if (mSampleRate != 8000 && mSampleRate != 11025 && mSampleRate != 16000 && 1941 mSampleRate != 22050) { 1942 LOGW("AudioHardware::DownSampler cstor: bad sampling rate: %d", mSampleRate); 1943 return; 1944 } 1945 1946 mInLeft = new int16_t[mFrameCount]; 1947 mInRight = new int16_t[mFrameCount]; 1948 mTmpLeft = new int16_t[mFrameCount]; 1949 mTmpRight = new int16_t[mFrameCount]; 1950 mTmp2Left = new int16_t[mFrameCount]; 1951 mTmp2Right = new int16_t[mFrameCount]; 1952 mOutLeft = new int16_t[mFrameCount]; 1953 mOutRight = new int16_t[mFrameCount]; 1954 1955 mStatus = NO_ERROR; 1956 } 1957 1958 AudioHardware::DownSampler::~DownSampler() 1959 { 1960 if (mInLeft) delete[] mInLeft; 1961 if (mInRight) delete[] mInRight; 1962 if (mTmpLeft) delete[] mTmpLeft; 1963 if (mTmpRight) delete[] mTmpRight; 1964 if (mTmp2Left) delete[] mTmp2Left; 1965 if (mTmp2Right) delete[] mTmp2Right; 1966 if (mOutLeft) delete[] mOutLeft; 1967 if (mOutRight) delete[] mOutRight; 1968 } 1969 1970 void AudioHardware::DownSampler::reset() 1971 { 1972 mInInBuf = 0; 1973 mInTmpBuf = 0; 1974 mInTmp2Buf = 0; 1975 mOutBufPos = 0; 1976 mInOutBuf = 0; 1977 } 1978 1979 1980 int AudioHardware::DownSampler::resample(int16_t* out, size_t *outFrameCount) 1981 { 1982 if (mStatus != NO_ERROR) { 1983 return mStatus; 1984 } 1985 1986 if (out == NULL || outFrameCount == NULL) { 1987 return BAD_VALUE; 1988 } 1989 1990 int16_t *outLeft = mTmp2Left; 1991 int16_t *outRight = mTmp2Left; 1992 if (mSampleRate == 22050) { 1993 outLeft = mTmpLeft; 1994 outRight = mTmpRight; 1995 } else if (mSampleRate == 8000){ 1996 outLeft = mOutLeft; 1997 outRight = mOutRight; 1998 } 1999 2000 int outFrames = 0; 2001 int remaingFrames = *outFrameCount; 2002 2003 if (mInOutBuf) { 2004 int frames = (remaingFrames > mInOutBuf) ? mInOutBuf : remaingFrames; 2005 2006 for (int i = 0; i < frames; ++i) { 2007 out[i] = outLeft[mOutBufPos + i]; 2008 } 2009 if (mChannelCount == 2) { 2010 for (int i = 0; i < frames; ++i) { 2011 out[i * 2] = outLeft[mOutBufPos + i]; 2012 out[i * 2 + 1] = outRight[mOutBufPos + i]; 2013 } 2014 } 2015 remaingFrames -= frames; 2016 mInOutBuf -= frames; 2017 mOutBufPos += frames; 2018 outFrames += frames; 2019 } 2020 2021 while (remaingFrames) { 2022 LOGW_IF((mInOutBuf != 0), "mInOutBuf should be 0 here"); 2023 2024 AudioHardware::BufferProvider::Buffer buf; 2025 buf.frameCount = mFrameCount - mInInBuf; 2026 int ret = mProvider->getNextBuffer(&buf); 2027 if (buf.raw == NULL) { 2028 *outFrameCount = outFrames; 2029 return ret; 2030 } 2031 2032 for (size_t i = 0; i < buf.frameCount; ++i) { 2033 mInLeft[i + mInInBuf] = buf.i16[i]; 2034 } 2035 if (mChannelCount == 2) { 2036 for (size_t i = 0; i < buf.frameCount; ++i) { 2037 mInLeft[i + mInInBuf] = buf.i16[i * 2]; 2038 mInRight[i + mInInBuf] = buf.i16[i * 2 + 1]; 2039 } 2040 } 2041 mInInBuf += buf.frameCount; 2042 mProvider->releaseBuffer(&buf); 2043 2044 /* 44010 -> 22050 */ 2045 { 2046 int samples_in_left = mInInBuf; 2047 int samples_out_left; 2048 resample_2_1(mInLeft, mTmpLeft + mInTmpBuf, &samples_in_left, &samples_out_left); 2049 2050 if (mChannelCount == 2) { 2051 int samples_in_right = mInInBuf; 2052 int samples_out_right; 2053 resample_2_1(mInRight, mTmpRight + mInTmpBuf, &samples_in_right, &samples_out_right); 2054 } 2055 2056 mInInBuf = samples_in_left; 2057 mInTmpBuf += samples_out_left; 2058 mInOutBuf = samples_out_left; 2059 } 2060 2061 if (mSampleRate == 11025 || mSampleRate == 8000) { 2062 /* 22050 - > 11025 */ 2063 int samples_in_left = mInTmpBuf; 2064 int samples_out_left; 2065 resample_2_1(mTmpLeft, mTmp2Left + mInTmp2Buf, &samples_in_left, &samples_out_left); 2066 2067 if (mChannelCount == 2) { 2068 int samples_in_right = mInTmpBuf; 2069 int samples_out_right; 2070 resample_2_1(mTmpRight, mTmp2Right + mInTmp2Buf, &samples_in_right, &samples_out_right); 2071 } 2072 2073 2074 mInTmpBuf = samples_in_left; 2075 mInTmp2Buf += samples_out_left; 2076 mInOutBuf = samples_out_left; 2077 2078 if (mSampleRate == 8000) { 2079 /* 11025 -> 8000*/ 2080 int samples_in_left = mInTmp2Buf; 2081 int samples_out_left; 2082 resample_441_320(mTmp2Left, mOutLeft, &samples_in_left, &samples_out_left); 2083 2084 if (mChannelCount == 2) { 2085 int samples_in_right = mInTmp2Buf; 2086 int samples_out_right; 2087 resample_441_320(mTmp2Right, mOutRight, &samples_in_right, &samples_out_right); 2088 } 2089 2090 mInTmp2Buf = samples_in_left; 2091 mInOutBuf = samples_out_left; 2092 } else { 2093 mInTmp2Buf = 0; 2094 } 2095 2096 } else if (mSampleRate == 16000) { 2097 /* 22050 -> 16000*/ 2098 int samples_in_left = mInTmpBuf; 2099 int samples_out_left; 2100 resample_441_320(mTmpLeft, mTmp2Left, &samples_in_left, &samples_out_left); 2101 2102 if (mChannelCount == 2) { 2103 int samples_in_right = mInTmpBuf; 2104 int samples_out_right; 2105 resample_441_320(mTmpRight, mTmp2Right, &samples_in_right, &samples_out_right); 2106 } 2107 2108 mInTmpBuf = samples_in_left; 2109 mInOutBuf = samples_out_left; 2110 } else { 2111 mInTmpBuf = 0; 2112 } 2113 2114 int frames = (remaingFrames > mInOutBuf) ? mInOutBuf : remaingFrames; 2115 2116 for (int i = 0; i < frames; ++i) { 2117 out[outFrames + i] = outLeft[i]; 2118 } 2119 if (mChannelCount == 2) { 2120 for (int i = 0; i < frames; ++i) { 2121 out[(outFrames + i) * 2] = outLeft[i]; 2122 out[(outFrames + i) * 2 + 1] = outRight[i]; 2123 } 2124 } 2125 remaingFrames -= frames; 2126 outFrames += frames; 2127 mOutBufPos = frames; 2128 mInOutBuf -= frames; 2129 } 2130 2131 return 0; 2132 } 2133 2134 2135 2136 2137 2138 2139 2140 //------------------------------------------------------------------------------ 2141 // Factory 2142 //------------------------------------------------------------------------------ 2143 2144 extern "C" AudioHardwareInterface* createAudioHardware(void) { 2145 return new AudioHardware(); 2146 } 2147 2148 }; // namespace android 2149