AudioPolicyService.cpp revision 1c333e252cbca3337c1bedbc57a005f3b7d23fdb
1/* 2 * Copyright (C) 2009 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 "AudioPolicyService" 18//#define LOG_NDEBUG 0 19 20#include "Configuration.h" 21#undef __STRICT_ANSI__ 22#define __STDINT_LIMITS 23#define __STDC_LIMIT_MACROS 24#include <stdint.h> 25 26#include <sys/time.h> 27#include <binder/IServiceManager.h> 28#include <utils/Log.h> 29#include <cutils/properties.h> 30#include <binder/IPCThreadState.h> 31#include <utils/String16.h> 32#include <utils/threads.h> 33#include "AudioPolicyService.h" 34#include "ServiceUtilities.h" 35#include <hardware_legacy/power.h> 36#include <media/AudioEffect.h> 37#include <media/EffectsFactoryApi.h> 38 39#include <hardware/hardware.h> 40#include <system/audio.h> 41#include <system/audio_policy.h> 42#include <hardware/audio_policy.h> 43#include <audio_effects/audio_effects_conf.h> 44#include <media/AudioParameter.h> 45 46namespace android { 47 48static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; 49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; 50 51static const int kDumpLockRetries = 50; 52static const int kDumpLockSleepUs = 20000; 53 54static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds 55 56namespace { 57 extern struct audio_policy_service_ops aps_ops; 58}; 59 60// ---------------------------------------------------------------------------- 61 62AudioPolicyService::AudioPolicyService() 63 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL), 64 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL) 65{ 66 char value[PROPERTY_VALUE_MAX]; 67 const struct hw_module_t *module; 68 int forced_val; 69 int rc; 70 71 Mutex::Autolock _l(mLock); 72 73 // start tone playback thread 74 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this); 75 // start audio commands thread 76 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this); 77 // start output activity command thread 78 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this); 79 80#ifdef USE_LEGACY_AUDIO_POLICY 81 ALOGI("AudioPolicyService CSTOR in legacy mode"); 82 83 /* instantiate the audio policy manager */ 84 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); 85 if (rc) { 86 return; 87 } 88 rc = audio_policy_dev_open(module, &mpAudioPolicyDev); 89 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc)); 90 if (rc) { 91 return; 92 } 93 94 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, 95 &mpAudioPolicy); 96 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc)); 97 if (rc) { 98 return; 99 } 100 101 rc = mpAudioPolicy->init_check(mpAudioPolicy); 102 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc)); 103 if (rc) { 104 return; 105 } 106 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id); 107#else 108 ALOGI("AudioPolicyService CSTOR in new mode"); 109 110 mAudioPolicyClient = new AudioPolicyClient(this); 111 mAudioPolicyManager = new AudioPolicyManager(mAudioPolicyClient); 112#endif 113 114 // load audio pre processing modules 115 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) { 116 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE); 117 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) { 118 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE); 119 } 120} 121 122AudioPolicyService::~AudioPolicyService() 123{ 124 mTonePlaybackThread->exit(); 125 mAudioCommandThread->exit(); 126 mOutputCommandThread->exit(); 127 128 // release audio pre processing resources 129 for (size_t i = 0; i < mInputSources.size(); i++) { 130 delete mInputSources.valueAt(i); 131 } 132 mInputSources.clear(); 133 134 for (size_t i = 0; i < mInputs.size(); i++) { 135 mInputs.valueAt(i)->mEffects.clear(); 136 delete mInputs.valueAt(i); 137 } 138 mInputs.clear(); 139 140#ifdef USE_LEGACY_AUDIO_POLICY 141 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) { 142 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); 143 } 144 if (mpAudioPolicyDev != NULL) { 145 audio_policy_dev_close(mpAudioPolicyDev); 146 } 147#else 148 delete mAudioPolicyManager; 149 delete mAudioPolicyClient; 150#endif 151} 152 153status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch, 154 audio_patch_handle_t *handle, 155 int delayMs) 156{ 157 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs); 158} 159 160status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle, 161 int delayMs) 162{ 163 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs); 164} 165 166 167void AudioPolicyService::binderDied(const wp<IBinder>& who) { 168 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(), 169 IPCThreadState::self()->getCallingPid()); 170} 171 172static bool tryLock(Mutex& mutex) 173{ 174 bool locked = false; 175 for (int i = 0; i < kDumpLockRetries; ++i) { 176 if (mutex.tryLock() == NO_ERROR) { 177 locked = true; 178 break; 179 } 180 usleep(kDumpLockSleepUs); 181 } 182 return locked; 183} 184 185status_t AudioPolicyService::dumpInternals(int fd) 186{ 187 const size_t SIZE = 256; 188 char buffer[SIZE]; 189 String8 result; 190 191#ifdef USE_LEGACY_AUDIO_POLICY 192 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); 193#else 194 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager); 195#endif 196 result.append(buffer); 197 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); 198 result.append(buffer); 199 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); 200 result.append(buffer); 201 202 write(fd, result.string(), result.size()); 203 return NO_ERROR; 204} 205 206status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused) 207{ 208 if (!dumpAllowed()) { 209 dumpPermissionDenial(fd); 210 } else { 211 bool locked = tryLock(mLock); 212 if (!locked) { 213 String8 result(kDeadlockedString); 214 write(fd, result.string(), result.size()); 215 } 216 217 dumpInternals(fd); 218 if (mAudioCommandThread != 0) { 219 mAudioCommandThread->dump(fd); 220 } 221 if (mTonePlaybackThread != 0) { 222 mTonePlaybackThread->dump(fd); 223 } 224 225#ifdef USE_LEGACY_AUDIO_POLICY 226 if (mpAudioPolicy) { 227 mpAudioPolicy->dump(mpAudioPolicy, fd); 228 } 229#else 230 if (mAudioPolicyManager) { 231 mAudioPolicyManager->dump(fd); 232 } 233#endif 234 235 if (locked) mLock.unlock(); 236 } 237 return NO_ERROR; 238} 239 240status_t AudioPolicyService::dumpPermissionDenial(int fd) 241{ 242 const size_t SIZE = 256; 243 char buffer[SIZE]; 244 String8 result; 245 snprintf(buffer, SIZE, "Permission Denial: " 246 "can't dump AudioPolicyService from pid=%d, uid=%d\n", 247 IPCThreadState::self()->getCallingPid(), 248 IPCThreadState::self()->getCallingUid()); 249 result.append(buffer); 250 write(fd, result.string(), result.size()); 251 return NO_ERROR; 252} 253 254void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled) 255{ 256 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects; 257 for (size_t i = 0; i < fxVector.size(); i++) { 258 fxVector.itemAt(i)->setEnabled(enabled); 259 } 260} 261 262status_t AudioPolicyService::onTransact( 263 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 264{ 265 return BnAudioPolicyService::onTransact(code, data, reply, flags); 266} 267 268 269// ----------- AudioPolicyService::AudioCommandThread implementation ---------- 270 271AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name, 272 const wp<AudioPolicyService>& service) 273 : Thread(false), mName(name), mService(service) 274{ 275 mpToneGenerator = NULL; 276} 277 278 279AudioPolicyService::AudioCommandThread::~AudioCommandThread() 280{ 281 if (!mAudioCommands.isEmpty()) { 282 release_wake_lock(mName.string()); 283 } 284 mAudioCommands.clear(); 285 delete mpToneGenerator; 286} 287 288void AudioPolicyService::AudioCommandThread::onFirstRef() 289{ 290 run(mName.string(), ANDROID_PRIORITY_AUDIO); 291} 292 293bool AudioPolicyService::AudioCommandThread::threadLoop() 294{ 295 nsecs_t waitTime = INT64_MAX; 296 297 mLock.lock(); 298 while (!exitPending()) 299 { 300 while (!mAudioCommands.isEmpty()) { 301 nsecs_t curTime = systemTime(); 302 // commands are sorted by increasing time stamp: execute them from index 0 and up 303 if (mAudioCommands[0]->mTime <= curTime) { 304 sp<AudioCommand> command = mAudioCommands[0]; 305 mAudioCommands.removeAt(0); 306 mLastCommand = command; 307 308 switch (command->mCommand) { 309 case START_TONE: { 310 mLock.unlock(); 311 ToneData *data = (ToneData *)command->mParam.get(); 312 ALOGV("AudioCommandThread() processing start tone %d on stream %d", 313 data->mType, data->mStream); 314 delete mpToneGenerator; 315 mpToneGenerator = new ToneGenerator(data->mStream, 1.0); 316 mpToneGenerator->startTone(data->mType); 317 mLock.lock(); 318 }break; 319 case STOP_TONE: { 320 mLock.unlock(); 321 ALOGV("AudioCommandThread() processing stop tone"); 322 if (mpToneGenerator != NULL) { 323 mpToneGenerator->stopTone(); 324 delete mpToneGenerator; 325 mpToneGenerator = NULL; 326 } 327 mLock.lock(); 328 }break; 329 case SET_VOLUME: { 330 VolumeData *data = (VolumeData *)command->mParam.get(); 331 ALOGV("AudioCommandThread() processing set volume stream %d, \ 332 volume %f, output %d", data->mStream, data->mVolume, data->mIO); 333 command->mStatus = AudioSystem::setStreamVolume(data->mStream, 334 data->mVolume, 335 data->mIO); 336 }break; 337 case SET_PARAMETERS: { 338 ParametersData *data = (ParametersData *)command->mParam.get(); 339 ALOGV("AudioCommandThread() processing set parameters string %s, io %d", 340 data->mKeyValuePairs.string(), data->mIO); 341 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); 342 }break; 343 case SET_VOICE_VOLUME: { 344 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); 345 ALOGV("AudioCommandThread() processing set voice volume volume %f", 346 data->mVolume); 347 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); 348 }break; 349 case STOP_OUTPUT: { 350 StopOutputData *data = (StopOutputData *)command->mParam.get(); 351 ALOGV("AudioCommandThread() processing stop output %d", 352 data->mIO); 353 sp<AudioPolicyService> svc = mService.promote(); 354 if (svc == 0) { 355 break; 356 } 357 mLock.unlock(); 358 svc->doStopOutput(data->mIO, data->mStream, data->mSession); 359 mLock.lock(); 360 }break; 361 case RELEASE_OUTPUT: { 362 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get(); 363 ALOGV("AudioCommandThread() processing release output %d", 364 data->mIO); 365 sp<AudioPolicyService> svc = mService.promote(); 366 if (svc == 0) { 367 break; 368 } 369 mLock.unlock(); 370 svc->doReleaseOutput(data->mIO); 371 mLock.lock(); 372 }break; 373 case CREATE_AUDIO_PATCH: { 374 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get(); 375 ALOGV("AudioCommandThread() processing create audio patch"); 376 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 377 if (af == 0) { 378 command->mStatus = PERMISSION_DENIED; 379 } else { 380 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle); 381 } 382 } break; 383 case RELEASE_AUDIO_PATCH: { 384 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get(); 385 ALOGV("AudioCommandThread() processing release audio patch"); 386 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 387 if (af == 0) { 388 command->mStatus = PERMISSION_DENIED; 389 } else { 390 command->mStatus = af->releaseAudioPatch(data->mHandle); 391 } 392 } break; 393 default: 394 ALOGW("AudioCommandThread() unknown command %d", command->mCommand); 395 } 396 { 397 Mutex::Autolock _l(command->mLock); 398 if (command->mWaitStatus) { 399 command->mWaitStatus = false; 400 command->mCond.signal(); 401 } 402 } 403 waitTime = INT64_MAX; 404 } else { 405 waitTime = mAudioCommands[0]->mTime - curTime; 406 break; 407 } 408 } 409 // release delayed commands wake lock 410 if (mAudioCommands.isEmpty()) { 411 release_wake_lock(mName.string()); 412 } 413 ALOGV("AudioCommandThread() going to sleep"); 414 mWaitWorkCV.waitRelative(mLock, waitTime); 415 ALOGV("AudioCommandThread() waking up"); 416 } 417 mLock.unlock(); 418 return false; 419} 420 421status_t AudioPolicyService::AudioCommandThread::dump(int fd) 422{ 423 const size_t SIZE = 256; 424 char buffer[SIZE]; 425 String8 result; 426 427 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); 428 result.append(buffer); 429 write(fd, result.string(), result.size()); 430 431 bool locked = tryLock(mLock); 432 if (!locked) { 433 String8 result2(kCmdDeadlockedString); 434 write(fd, result2.string(), result2.size()); 435 } 436 437 snprintf(buffer, SIZE, "- Commands:\n"); 438 result = String8(buffer); 439 result.append(" Command Time Wait pParam\n"); 440 for (size_t i = 0; i < mAudioCommands.size(); i++) { 441 mAudioCommands[i]->dump(buffer, SIZE); 442 result.append(buffer); 443 } 444 result.append(" Last Command\n"); 445 if (mLastCommand != 0) { 446 mLastCommand->dump(buffer, SIZE); 447 result.append(buffer); 448 } else { 449 result.append(" none\n"); 450 } 451 452 write(fd, result.string(), result.size()); 453 454 if (locked) mLock.unlock(); 455 456 return NO_ERROR; 457} 458 459void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type, 460 audio_stream_type_t stream) 461{ 462 sp<AudioCommand> command = new AudioCommand(); 463 command->mCommand = START_TONE; 464 sp<ToneData> data = new ToneData(); 465 data->mType = type; 466 data->mStream = stream; 467 command->mParam = data; 468 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); 469 sendCommand(command); 470} 471 472void AudioPolicyService::AudioCommandThread::stopToneCommand() 473{ 474 sp<AudioCommand> command = new AudioCommand(); 475 command->mCommand = STOP_TONE; 476 ALOGV("AudioCommandThread() adding tone stop"); 477 sendCommand(command); 478} 479 480status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, 481 float volume, 482 audio_io_handle_t output, 483 int delayMs) 484{ 485 sp<AudioCommand> command = new AudioCommand(); 486 command->mCommand = SET_VOLUME; 487 sp<VolumeData> data = new VolumeData(); 488 data->mStream = stream; 489 data->mVolume = volume; 490 data->mIO = output; 491 command->mParam = data; 492 command->mWaitStatus = true; 493 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", 494 stream, volume, output); 495 return sendCommand(command, delayMs); 496} 497 498status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, 499 const char *keyValuePairs, 500 int delayMs) 501{ 502 sp<AudioCommand> command = new AudioCommand(); 503 command->mCommand = SET_PARAMETERS; 504 sp<ParametersData> data = new ParametersData(); 505 data->mIO = ioHandle; 506 data->mKeyValuePairs = String8(keyValuePairs); 507 command->mParam = data; 508 command->mWaitStatus = true; 509 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", 510 keyValuePairs, ioHandle, delayMs); 511 return sendCommand(command, delayMs); 512} 513 514status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) 515{ 516 sp<AudioCommand> command = new AudioCommand(); 517 command->mCommand = SET_VOICE_VOLUME; 518 sp<VoiceVolumeData> data = new VoiceVolumeData(); 519 data->mVolume = volume; 520 command->mParam = data; 521 command->mWaitStatus = true; 522 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); 523 return sendCommand(command, delayMs); 524} 525 526void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output, 527 audio_stream_type_t stream, 528 int session) 529{ 530 sp<AudioCommand> command = new AudioCommand(); 531 command->mCommand = STOP_OUTPUT; 532 sp<StopOutputData> data = new StopOutputData(); 533 data->mIO = output; 534 data->mStream = stream; 535 data->mSession = session; 536 command->mParam = data; 537 ALOGV("AudioCommandThread() adding stop output %d", output); 538 sendCommand(command); 539} 540 541void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output) 542{ 543 sp<AudioCommand> command = new AudioCommand(); 544 command->mCommand = RELEASE_OUTPUT; 545 sp<ReleaseOutputData> data = new ReleaseOutputData(); 546 data->mIO = output; 547 command->mParam = data; 548 ALOGV("AudioCommandThread() adding release output %d", output); 549 sendCommand(command); 550} 551 552status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand( 553 const struct audio_patch *patch, 554 audio_patch_handle_t *handle, 555 int delayMs) 556{ 557 status_t status = NO_ERROR; 558 559 sp<AudioCommand> command = new AudioCommand(); 560 command->mCommand = CREATE_AUDIO_PATCH; 561 CreateAudioPatchData *data = new CreateAudioPatchData(); 562 data->mPatch = *patch; 563 data->mHandle = *handle; 564 command->mParam = data; 565 command->mWaitStatus = true; 566 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs); 567 status = sendCommand(command, delayMs); 568 if (status == NO_ERROR) { 569 *handle = data->mHandle; 570 } 571 return status; 572} 573 574status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle, 575 int delayMs) 576{ 577 sp<AudioCommand> command = new AudioCommand(); 578 command->mCommand = RELEASE_AUDIO_PATCH; 579 ReleaseAudioPatchData *data = new ReleaseAudioPatchData(); 580 data->mHandle = handle; 581 command->mParam = data; 582 command->mWaitStatus = true; 583 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs); 584 return sendCommand(command, delayMs); 585} 586 587status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs) 588{ 589 { 590 Mutex::Autolock _l(mLock); 591 insertCommand_l(command, delayMs); 592 mWaitWorkCV.signal(); 593 } 594 Mutex::Autolock _l(command->mLock); 595 while (command->mWaitStatus) { 596 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs); 597 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) { 598 command->mStatus = TIMED_OUT; 599 command->mWaitStatus = false; 600 } 601 } 602 return command->mStatus; 603} 604 605 606// insertCommand_l() must be called with mLock held 607void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs) 608{ 609 ssize_t i; // not size_t because i will count down to -1 610 Vector < sp<AudioCommand> > removedCommands; 611 command->mTime = systemTime() + milliseconds(delayMs); 612 613 // acquire wake lock to make sure delayed commands are processed 614 if (mAudioCommands.isEmpty()) { 615 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); 616 } 617 618 // check same pending commands with later time stamps and eliminate them 619 for (i = mAudioCommands.size()-1; i >= 0; i--) { 620 sp<AudioCommand> command2 = mAudioCommands[i]; 621 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands 622 if (command2->mTime <= command->mTime) break; 623 if (command2->mCommand != command->mCommand) continue; 624 625 switch (command->mCommand) { 626 case SET_PARAMETERS: { 627 ParametersData *data = (ParametersData *)command->mParam.get(); 628 ParametersData *data2 = (ParametersData *)command2->mParam.get(); 629 if (data->mIO != data2->mIO) break; 630 ALOGV("Comparing parameter command %s to new command %s", 631 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); 632 AudioParameter param = AudioParameter(data->mKeyValuePairs); 633 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); 634 for (size_t j = 0; j < param.size(); j++) { 635 String8 key; 636 String8 value; 637 param.getAt(j, key, value); 638 for (size_t k = 0; k < param2.size(); k++) { 639 String8 key2; 640 String8 value2; 641 param2.getAt(k, key2, value2); 642 if (key2 == key) { 643 param2.remove(key2); 644 ALOGV("Filtering out parameter %s", key2.string()); 645 break; 646 } 647 } 648 } 649 // if all keys have been filtered out, remove the command. 650 // otherwise, update the key value pairs 651 if (param2.size() == 0) { 652 removedCommands.add(command2); 653 } else { 654 data2->mKeyValuePairs = param2.toString(); 655 } 656 command->mTime = command2->mTime; 657 // force delayMs to non 0 so that code below does not request to wait for 658 // command status as the command is now delayed 659 delayMs = 1; 660 } break; 661 662 case SET_VOLUME: { 663 VolumeData *data = (VolumeData *)command->mParam.get(); 664 VolumeData *data2 = (VolumeData *)command2->mParam.get(); 665 if (data->mIO != data2->mIO) break; 666 if (data->mStream != data2->mStream) break; 667 ALOGV("Filtering out volume command on output %d for stream %d", 668 data->mIO, data->mStream); 669 removedCommands.add(command2); 670 command->mTime = command2->mTime; 671 // force delayMs to non 0 so that code below does not request to wait for 672 // command status as the command is now delayed 673 delayMs = 1; 674 } break; 675 case START_TONE: 676 case STOP_TONE: 677 default: 678 break; 679 } 680 } 681 682 // remove filtered commands 683 for (size_t j = 0; j < removedCommands.size(); j++) { 684 // removed commands always have time stamps greater than current command 685 for (size_t k = i + 1; k < mAudioCommands.size(); k++) { 686 if (mAudioCommands[k].get() == removedCommands[j].get()) { 687 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); 688 mAudioCommands.removeAt(k); 689 break; 690 } 691 } 692 } 693 removedCommands.clear(); 694 695 // Disable wait for status if delay is not 0 696 if (delayMs != 0) { 697 command->mWaitStatus = false; 698 } 699 700 // insert command at the right place according to its time stamp 701 ALOGV("inserting command: %d at index %d, num commands %d", 702 command->mCommand, (int)i+1, mAudioCommands.size()); 703 mAudioCommands.insertAt(command, i + 1); 704} 705 706void AudioPolicyService::AudioCommandThread::exit() 707{ 708 ALOGV("AudioCommandThread::exit"); 709 { 710 AutoMutex _l(mLock); 711 requestExit(); 712 mWaitWorkCV.signal(); 713 } 714 requestExitAndWait(); 715} 716 717void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) 718{ 719 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", 720 mCommand, 721 (int)ns2s(mTime), 722 (int)ns2ms(mTime)%1000, 723 mWaitStatus, 724 mParam.get()); 725} 726 727/******* helpers for the service_ops callbacks defined below *********/ 728void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, 729 const char *keyValuePairs, 730 int delayMs) 731{ 732 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, 733 delayMs); 734} 735 736int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, 737 float volume, 738 audio_io_handle_t output, 739 int delayMs) 740{ 741 return (int)mAudioCommandThread->volumeCommand(stream, volume, 742 output, delayMs); 743} 744 745int AudioPolicyService::startTone(audio_policy_tone_t tone, 746 audio_stream_type_t stream) 747{ 748 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) { 749 ALOGE("startTone: illegal tone requested (%d)", tone); 750 } 751 if (stream != AUDIO_STREAM_VOICE_CALL) { 752 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream, 753 tone); 754 } 755 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, 756 AUDIO_STREAM_VOICE_CALL); 757 return 0; 758} 759 760int AudioPolicyService::stopTone() 761{ 762 mTonePlaybackThread->stopToneCommand(); 763 return 0; 764} 765 766int AudioPolicyService::setVoiceVolume(float volume, int delayMs) 767{ 768 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); 769} 770 771// ---------------------------------------------------------------------------- 772// Audio pre-processing configuration 773// ---------------------------------------------------------------------------- 774 775/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = { 776 MIC_SRC_TAG, 777 VOICE_UL_SRC_TAG, 778 VOICE_DL_SRC_TAG, 779 VOICE_CALL_SRC_TAG, 780 CAMCORDER_SRC_TAG, 781 VOICE_REC_SRC_TAG, 782 VOICE_COMM_SRC_TAG 783}; 784 785// returns the audio_source_t enum corresponding to the input source name or 786// AUDIO_SOURCE_CNT is no match found 787audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name) 788{ 789 int i; 790 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) { 791 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) { 792 ALOGV("inputSourceNameToEnum found source %s %d", name, i); 793 break; 794 } 795 } 796 return (audio_source_t)i; 797} 798 799size_t AudioPolicyService::growParamSize(char *param, 800 size_t size, 801 size_t *curSize, 802 size_t *totSize) 803{ 804 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int) 805 size_t pos = ((*curSize - 1 ) / size + 1) * size; 806 807 if (pos + size > *totSize) { 808 while (pos + size > *totSize) { 809 *totSize += ((*totSize + 7) / 8) * 4; 810 } 811 param = (char *)realloc(param, *totSize); 812 } 813 *curSize = pos + size; 814 return pos; 815} 816 817size_t AudioPolicyService::readParamValue(cnode *node, 818 char *param, 819 size_t *curSize, 820 size_t *totSize) 821{ 822 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) { 823 size_t pos = growParamSize(param, sizeof(short), curSize, totSize); 824 *(short *)((char *)param + pos) = (short)atoi(node->value); 825 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos)); 826 return sizeof(short); 827 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) { 828 size_t pos = growParamSize(param, sizeof(int), curSize, totSize); 829 *(int *)((char *)param + pos) = atoi(node->value); 830 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos)); 831 return sizeof(int); 832 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) { 833 size_t pos = growParamSize(param, sizeof(float), curSize, totSize); 834 *(float *)((char *)param + pos) = (float)atof(node->value); 835 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos)); 836 return sizeof(float); 837 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) { 838 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize); 839 if (strncmp(node->value, "false", strlen("false") + 1) == 0) { 840 *(bool *)((char *)param + pos) = false; 841 } else { 842 *(bool *)((char *)param + pos) = true; 843 } 844 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false"); 845 return sizeof(bool); 846 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) { 847 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX); 848 if (*curSize + len + 1 > *totSize) { 849 *totSize = *curSize + len + 1; 850 param = (char *)realloc(param, *totSize); 851 } 852 strncpy(param + *curSize, node->value, len); 853 *curSize += len; 854 param[*curSize] = '\0'; 855 ALOGV("readParamValue() reading string %s", param + *curSize - len); 856 return len; 857 } 858 ALOGW("readParamValue() unknown param type %s", node->name); 859 return 0; 860} 861 862effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root) 863{ 864 cnode *param; 865 cnode *value; 866 size_t curSize = sizeof(effect_param_t); 867 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int); 868 effect_param_t *fx_param = (effect_param_t *)malloc(totSize); 869 870 param = config_find(root, PARAM_TAG); 871 value = config_find(root, VALUE_TAG); 872 if (param == NULL && value == NULL) { 873 // try to parse simple parameter form {int int} 874 param = root->first_child; 875 if (param != NULL) { 876 // Note: that a pair of random strings is read as 0 0 877 int *ptr = (int *)fx_param->data; 878 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t)); 879 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2); 880 *ptr++ = atoi(param->name); 881 *ptr = atoi(param->value); 882 fx_param->psize = sizeof(int); 883 fx_param->vsize = sizeof(int); 884 return fx_param; 885 } 886 } 887 if (param == NULL || value == NULL) { 888 ALOGW("loadEffectParameter() invalid parameter description %s", root->name); 889 goto error; 890 } 891 892 fx_param->psize = 0; 893 param = param->first_child; 894 while (param) { 895 ALOGV("loadEffectParameter() reading param of type %s", param->name); 896 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize); 897 if (size == 0) { 898 goto error; 899 } 900 fx_param->psize += size; 901 param = param->next; 902 } 903 904 // align start of value field on 32 bit boundary 905 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int); 906 907 fx_param->vsize = 0; 908 value = value->first_child; 909 while (value) { 910 ALOGV("loadEffectParameter() reading value of type %s", value->name); 911 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize); 912 if (size == 0) { 913 goto error; 914 } 915 fx_param->vsize += size; 916 value = value->next; 917 } 918 919 return fx_param; 920 921error: 922 free(fx_param); 923 return NULL; 924} 925 926void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params) 927{ 928 cnode *node = root->first_child; 929 while (node) { 930 ALOGV("loadEffectParameters() loading param %s", node->name); 931 effect_param_t *param = loadEffectParameter(node); 932 if (param == NULL) { 933 node = node->next; 934 continue; 935 } 936 params.add(param); 937 node = node->next; 938 } 939} 940 941AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource( 942 cnode *root, 943 const Vector <EffectDesc *>& effects) 944{ 945 cnode *node = root->first_child; 946 if (node == NULL) { 947 ALOGW("loadInputSource() empty element %s", root->name); 948 return NULL; 949 } 950 InputSourceDesc *source = new InputSourceDesc(); 951 while (node) { 952 size_t i; 953 for (i = 0; i < effects.size(); i++) { 954 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) { 955 ALOGV("loadInputSource() found effect %s in list", node->name); 956 break; 957 } 958 } 959 if (i == effects.size()) { 960 ALOGV("loadInputSource() effect %s not in list", node->name); 961 node = node->next; 962 continue; 963 } 964 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy 965 loadEffectParameters(node, effect->mParams); 966 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow); 967 source->mEffects.add(effect); 968 node = node->next; 969 } 970 if (source->mEffects.size() == 0) { 971 ALOGW("loadInputSource() no valid effects found in source %s", root->name); 972 delete source; 973 return NULL; 974 } 975 return source; 976} 977 978status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects) 979{ 980 cnode *node = config_find(root, PREPROCESSING_TAG); 981 if (node == NULL) { 982 return -ENOENT; 983 } 984 node = node->first_child; 985 while (node) { 986 audio_source_t source = inputSourceNameToEnum(node->name); 987 if (source == AUDIO_SOURCE_CNT) { 988 ALOGW("loadInputSources() invalid input source %s", node->name); 989 node = node->next; 990 continue; 991 } 992 ALOGV("loadInputSources() loading input source %s", node->name); 993 InputSourceDesc *desc = loadInputSource(node, effects); 994 if (desc == NULL) { 995 node = node->next; 996 continue; 997 } 998 mInputSources.add(source, desc); 999 node = node->next; 1000 } 1001 return NO_ERROR; 1002} 1003 1004AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root) 1005{ 1006 cnode *node = config_find(root, UUID_TAG); 1007 if (node == NULL) { 1008 return NULL; 1009 } 1010 effect_uuid_t uuid; 1011 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) { 1012 ALOGW("loadEffect() invalid uuid %s", node->value); 1013 return NULL; 1014 } 1015 return new EffectDesc(root->name, uuid); 1016} 1017 1018status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects) 1019{ 1020 cnode *node = config_find(root, EFFECTS_TAG); 1021 if (node == NULL) { 1022 return -ENOENT; 1023 } 1024 node = node->first_child; 1025 while (node) { 1026 ALOGV("loadEffects() loading effect %s", node->name); 1027 EffectDesc *effect = loadEffect(node); 1028 if (effect == NULL) { 1029 node = node->next; 1030 continue; 1031 } 1032 effects.add(effect); 1033 node = node->next; 1034 } 1035 return NO_ERROR; 1036} 1037 1038status_t AudioPolicyService::loadPreProcessorConfig(const char *path) 1039{ 1040 cnode *root; 1041 char *data; 1042 1043 data = (char *)load_file(path, NULL); 1044 if (data == NULL) { 1045 return -ENODEV; 1046 } 1047 root = config_node("", ""); 1048 config_load(root, data); 1049 1050 Vector <EffectDesc *> effects; 1051 loadEffects(root, effects); 1052 loadInputSources(root, effects); 1053 1054 // delete effects to fix memory leak. 1055 // as effects is local var and valgrind would treat this as memory leak 1056 // and although it only did in mediaserver init, but free it in case mediaserver reboot 1057 size_t i; 1058 for (i = 0; i < effects.size(); i++) { 1059 delete effects[i]; 1060 } 1061 1062 config_free(root); 1063 free(root); 1064 free(data); 1065 1066 return NO_ERROR; 1067} 1068 1069extern "C" { 1070audio_module_handle_t aps_load_hw_module(void *service __unused, 1071 const char *name); 1072audio_io_handle_t aps_open_output(void *service __unused, 1073 audio_devices_t *pDevices, 1074 uint32_t *pSamplingRate, 1075 audio_format_t *pFormat, 1076 audio_channel_mask_t *pChannelMask, 1077 uint32_t *pLatencyMs, 1078 audio_output_flags_t flags); 1079 1080audio_io_handle_t aps_open_output_on_module(void *service __unused, 1081 audio_module_handle_t module, 1082 audio_devices_t *pDevices, 1083 uint32_t *pSamplingRate, 1084 audio_format_t *pFormat, 1085 audio_channel_mask_t *pChannelMask, 1086 uint32_t *pLatencyMs, 1087 audio_output_flags_t flags, 1088 const audio_offload_info_t *offloadInfo); 1089audio_io_handle_t aps_open_dup_output(void *service __unused, 1090 audio_io_handle_t output1, 1091 audio_io_handle_t output2); 1092int aps_close_output(void *service __unused, audio_io_handle_t output); 1093int aps_suspend_output(void *service __unused, audio_io_handle_t output); 1094int aps_restore_output(void *service __unused, audio_io_handle_t output); 1095audio_io_handle_t aps_open_input(void *service __unused, 1096 audio_devices_t *pDevices, 1097 uint32_t *pSamplingRate, 1098 audio_format_t *pFormat, 1099 audio_channel_mask_t *pChannelMask, 1100 audio_in_acoustics_t acoustics __unused); 1101audio_io_handle_t aps_open_input_on_module(void *service __unused, 1102 audio_module_handle_t module, 1103 audio_devices_t *pDevices, 1104 uint32_t *pSamplingRate, 1105 audio_format_t *pFormat, 1106 audio_channel_mask_t *pChannelMask); 1107int aps_close_input(void *service __unused, audio_io_handle_t input); 1108int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream); 1109int aps_move_effects(void *service __unused, int session, 1110 audio_io_handle_t src_output, 1111 audio_io_handle_t dst_output); 1112char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle, 1113 const char *keys); 1114void aps_set_parameters(void *service, audio_io_handle_t io_handle, 1115 const char *kv_pairs, int delay_ms); 1116int aps_set_stream_volume(void *service, audio_stream_type_t stream, 1117 float volume, audio_io_handle_t output, 1118 int delay_ms); 1119int aps_start_tone(void *service, audio_policy_tone_t tone, 1120 audio_stream_type_t stream); 1121int aps_stop_tone(void *service); 1122int aps_set_voice_volume(void *service, float volume, int delay_ms); 1123}; 1124 1125namespace { 1126 struct audio_policy_service_ops aps_ops = { 1127 .open_output = aps_open_output, 1128 .open_duplicate_output = aps_open_dup_output, 1129 .close_output = aps_close_output, 1130 .suspend_output = aps_suspend_output, 1131 .restore_output = aps_restore_output, 1132 .open_input = aps_open_input, 1133 .close_input = aps_close_input, 1134 .set_stream_volume = aps_set_stream_volume, 1135 .invalidate_stream = aps_invalidate_stream, 1136 .set_parameters = aps_set_parameters, 1137 .get_parameters = aps_get_parameters, 1138 .start_tone = aps_start_tone, 1139 .stop_tone = aps_stop_tone, 1140 .set_voice_volume = aps_set_voice_volume, 1141 .move_effects = aps_move_effects, 1142 .load_hw_module = aps_load_hw_module, 1143 .open_output_on_module = aps_open_output_on_module, 1144 .open_input_on_module = aps_open_input_on_module, 1145 }; 1146}; // namespace <unnamed> 1147 1148}; // namespace android 1149