AudioPolicyService.cpp revision 64760240f931714858a59c1579f07264d7182ba2
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#undef __STRICT_ANSI__ 21#define __STDINT_LIMITS 22#define __STDC_LIMIT_MACROS 23#include <stdint.h> 24 25#include <sys/time.h> 26#include <binder/IServiceManager.h> 27#include <utils/Log.h> 28#include <cutils/properties.h> 29#include <binder/IPCThreadState.h> 30#include <utils/String16.h> 31#include <utils/threads.h> 32#include "AudioPolicyService.h" 33#include <cutils/properties.h> 34#include <dlfcn.h> 35#include <hardware_legacy/power.h> 36 37#include <hardware/hardware.h> 38#include <system/audio.h> 39#include <hardware/audio_policy.h> 40#include <hardware/audio_policy_hal.h> 41 42// ---------------------------------------------------------------------------- 43// the sim build doesn't have gettid 44 45#ifndef HAVE_GETTID 46# define gettid getpid 47#endif 48 49namespace android { 50 51static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n"; 52static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n"; 53 54static const int kDumpLockRetries = 50; 55static const int kDumpLockSleep = 20000; 56 57static bool checkPermission() { 58#ifndef HAVE_ANDROID_OS 59 return true; 60#endif 61 if (getpid() == IPCThreadState::self()->getCallingPid()) return true; 62 bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); 63 if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); 64 return ok; 65} 66 67namespace { 68 extern struct audio_policy_service_ops aps_ops; 69}; 70 71// ---------------------------------------------------------------------------- 72 73AudioPolicyService::AudioPolicyService() 74 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL) 75{ 76 char value[PROPERTY_VALUE_MAX]; 77 const struct hw_module_t *module; 78 int forced_val; 79 int rc; 80 81 Mutex::Autolock _l(mLock); 82 83 // start tone playback thread 84 mTonePlaybackThread = new AudioCommandThread(String8("")); 85 // start audio commands thread 86 mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread")); 87 88 /* instantiate the audio policy manager */ 89 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); 90 if (rc) 91 return; 92 93 rc = audio_policy_dev_open(module, &mpAudioPolicyDev); 94 LOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc)); 95 if (rc) 96 return; 97 98 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, 99 &mpAudioPolicy); 100 LOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc)); 101 if (rc) 102 return; 103 104 rc = mpAudioPolicy->init_check(mpAudioPolicy); 105 LOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc)); 106 if (rc) 107 return; 108 109 property_get("ro.camera.sound.forced", value, "0"); 110 forced_val = strtol(value, NULL, 0); 111 mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val); 112 113 LOGI("Loaded audio policy from %s (%s)", module->name, module->id); 114} 115 116AudioPolicyService::~AudioPolicyService() 117{ 118 mTonePlaybackThread->exit(); 119 mTonePlaybackThread.clear(); 120 mAudioCommandThread->exit(); 121 mAudioCommandThread.clear(); 122 123 if (mpAudioPolicy && mpAudioPolicyDev) 124 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); 125 if (mpAudioPolicyDev) 126 audio_policy_dev_close(mpAudioPolicyDev); 127} 128 129status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, 130 audio_policy_dev_state_t state, 131 const char *device_address) 132{ 133 if (mpAudioPolicy == NULL) { 134 return NO_INIT; 135 } 136 if (!checkPermission()) { 137 return PERMISSION_DENIED; 138 } 139 if (!audio_is_output_device(device) && !audio_is_input_device(device)) { 140 return BAD_VALUE; 141 } 142 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && 143 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { 144 return BAD_VALUE; 145 } 146 147 LOGV("setDeviceConnectionState() tid %d", gettid()); 148 Mutex::Autolock _l(mLock); 149 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device, 150 state, device_address); 151} 152 153audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( 154 audio_devices_t device, 155 const char *device_address) 156{ 157 if (mpAudioPolicy == NULL) { 158 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 159 } 160 if (!checkPermission()) { 161 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 162 } 163 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device, 164 device_address); 165} 166 167status_t AudioPolicyService::setPhoneState(int state) 168{ 169 if (mpAudioPolicy == NULL) { 170 return NO_INIT; 171 } 172 if (!checkPermission()) { 173 return PERMISSION_DENIED; 174 } 175 if (state < 0 || state >= AUDIO_MODE_CNT) { 176 return BAD_VALUE; 177 } 178 179 LOGV("setPhoneState() tid %d", gettid()); 180 181 // TODO: check if it is more appropriate to do it in platform specific policy manager 182 AudioSystem::setMode(state); 183 184 Mutex::Autolock _l(mLock); 185 mpAudioPolicy->set_phone_state(mpAudioPolicy, state); 186 return NO_ERROR; 187} 188 189status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask) 190{ 191 if (mpAudioPolicy == NULL) { 192 return NO_INIT; 193 } 194 if (!checkPermission()) { 195 return PERMISSION_DENIED; 196 } 197 198 mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask); 199 return NO_ERROR; 200} 201 202status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, 203 audio_policy_forced_cfg_t config) 204{ 205 if (mpAudioPolicy == NULL) { 206 return NO_INIT; 207 } 208 if (!checkPermission()) { 209 return PERMISSION_DENIED; 210 } 211 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 212 return BAD_VALUE; 213 } 214 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { 215 return BAD_VALUE; 216 } 217 LOGV("setForceUse() tid %d", gettid()); 218 Mutex::Autolock _l(mLock); 219 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config); 220 return NO_ERROR; 221} 222 223audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) 224{ 225 if (mpAudioPolicy == NULL) { 226 return AUDIO_POLICY_FORCE_NONE; 227 } 228 if (!checkPermission()) { 229 return AUDIO_POLICY_FORCE_NONE; 230 } 231 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 232 return AUDIO_POLICY_FORCE_NONE; 233 } 234 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage); 235} 236 237audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, 238 uint32_t samplingRate, 239 uint32_t format, 240 uint32_t channels, 241 audio_policy_output_flags_t flags) 242{ 243 if (mpAudioPolicy == NULL) { 244 return 0; 245 } 246 LOGV("getOutput() tid %d", gettid()); 247 Mutex::Autolock _l(mLock); 248 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags); 249} 250 251status_t AudioPolicyService::startOutput(audio_io_handle_t output, 252 audio_stream_type_t stream, 253 int session) 254{ 255 if (mpAudioPolicy == NULL) { 256 return NO_INIT; 257 } 258 LOGV("startOutput() tid %d", gettid()); 259 Mutex::Autolock _l(mLock); 260 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session); 261} 262 263status_t AudioPolicyService::stopOutput(audio_io_handle_t output, 264 audio_stream_type_t stream, 265 int session) 266{ 267 if (mpAudioPolicy == NULL) { 268 return NO_INIT; 269 } 270 LOGV("stopOutput() tid %d", gettid()); 271 Mutex::Autolock _l(mLock); 272 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session); 273} 274 275void AudioPolicyService::releaseOutput(audio_io_handle_t output) 276{ 277 if (mpAudioPolicy == NULL) { 278 return; 279 } 280 LOGV("releaseOutput() tid %d", gettid()); 281 Mutex::Autolock _l(mLock); 282 mpAudioPolicy->release_output(mpAudioPolicy, output); 283} 284 285audio_io_handle_t AudioPolicyService::getInput(int inputSource, 286 uint32_t samplingRate, 287 uint32_t format, 288 uint32_t channels, 289 audio_in_acoustics_t acoustics) 290{ 291 if (mpAudioPolicy == NULL) { 292 return 0; 293 } 294 Mutex::Autolock _l(mLock); 295 return mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, format, channels, acoustics); 296} 297 298status_t AudioPolicyService::startInput(audio_io_handle_t input) 299{ 300 if (mpAudioPolicy == NULL) { 301 return NO_INIT; 302 } 303 Mutex::Autolock _l(mLock); 304 return mpAudioPolicy->start_input(mpAudioPolicy, input); 305} 306 307status_t AudioPolicyService::stopInput(audio_io_handle_t input) 308{ 309 if (mpAudioPolicy == NULL) { 310 return NO_INIT; 311 } 312 Mutex::Autolock _l(mLock); 313 return mpAudioPolicy->stop_input(mpAudioPolicy, input); 314} 315 316void AudioPolicyService::releaseInput(audio_io_handle_t input) 317{ 318 if (mpAudioPolicy == NULL) { 319 return; 320 } 321 Mutex::Autolock _l(mLock); 322 mpAudioPolicy->release_input(mpAudioPolicy, input); 323} 324 325status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, 326 int indexMin, 327 int indexMax) 328{ 329 if (mpAudioPolicy == NULL) { 330 return NO_INIT; 331 } 332 if (!checkPermission()) { 333 return PERMISSION_DENIED; 334 } 335 if (stream < 0 || stream >= AUDIO_STREAM_CNT) { 336 return BAD_VALUE; 337 } 338 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax); 339 return NO_ERROR; 340} 341 342status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index) 343{ 344 if (mpAudioPolicy == NULL) { 345 return NO_INIT; 346 } 347 if (!checkPermission()) { 348 return PERMISSION_DENIED; 349 } 350 if (stream < 0 || stream >= AUDIO_STREAM_CNT) { 351 return BAD_VALUE; 352 } 353 354 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index); 355} 356 357status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index) 358{ 359 if (mpAudioPolicy == NULL) { 360 return NO_INIT; 361 } 362 if (!checkPermission()) { 363 return PERMISSION_DENIED; 364 } 365 if (stream < 0 || stream >= AUDIO_STREAM_CNT) { 366 return BAD_VALUE; 367 } 368 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index); 369} 370 371uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) 372{ 373 if (mpAudioPolicy == NULL) { 374 return 0; 375 } 376 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream); 377} 378 379uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) 380{ 381 if (mpAudioPolicy == NULL) { 382 return 0; 383 } 384 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream); 385} 386 387audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc) 388{ 389 if (mpAudioPolicy == NULL) { 390 return NO_INIT; 391 } 392 Mutex::Autolock _l(mLock); 393 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc); 394} 395 396status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc, 397 audio_io_handle_t output, 398 uint32_t strategy, 399 int session, 400 int id) 401{ 402 if (mpAudioPolicy == NULL) { 403 return NO_INIT; 404 } 405 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, output, strategy, session, id); 406} 407 408status_t AudioPolicyService::unregisterEffect(int id) 409{ 410 if (mpAudioPolicy == NULL) { 411 return NO_INIT; 412 } 413 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id); 414} 415 416bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const 417{ 418 if (mpAudioPolicy == NULL) { 419 return 0; 420 } 421 Mutex::Autolock _l(mLock); 422 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs); 423} 424 425void AudioPolicyService::binderDied(const wp<IBinder>& who) { 426 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), 427 IPCThreadState::self()->getCallingPid()); 428} 429 430static bool tryLock(Mutex& mutex) 431{ 432 bool locked = false; 433 for (int i = 0; i < kDumpLockRetries; ++i) { 434 if (mutex.tryLock() == NO_ERROR) { 435 locked = true; 436 break; 437 } 438 usleep(kDumpLockSleep); 439 } 440 return locked; 441} 442 443status_t AudioPolicyService::dumpInternals(int fd) 444{ 445 const size_t SIZE = 256; 446 char buffer[SIZE]; 447 String8 result; 448 449 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); 450 result.append(buffer); 451 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); 452 result.append(buffer); 453 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); 454 result.append(buffer); 455 456 write(fd, result.string(), result.size()); 457 return NO_ERROR; 458} 459 460status_t AudioPolicyService::dump(int fd, const Vector<String16>& args) 461{ 462 if (checkCallingPermission(String16("android.permission.DUMP")) == false) { 463 dumpPermissionDenial(fd); 464 } else { 465 bool locked = tryLock(mLock); 466 if (!locked) { 467 String8 result(kDeadlockedString); 468 write(fd, result.string(), result.size()); 469 } 470 471 dumpInternals(fd); 472 if (mAudioCommandThread != NULL) { 473 mAudioCommandThread->dump(fd); 474 } 475 if (mTonePlaybackThread != NULL) { 476 mTonePlaybackThread->dump(fd); 477 } 478 479 if (mpAudioPolicy) { 480 mpAudioPolicy->dump(mpAudioPolicy, fd); 481 } 482 483 if (locked) mLock.unlock(); 484 } 485 return NO_ERROR; 486} 487 488status_t AudioPolicyService::dumpPermissionDenial(int fd) 489{ 490 const size_t SIZE = 256; 491 char buffer[SIZE]; 492 String8 result; 493 snprintf(buffer, SIZE, "Permission Denial: " 494 "can't dump AudioPolicyService from pid=%d, uid=%d\n", 495 IPCThreadState::self()->getCallingPid(), 496 IPCThreadState::self()->getCallingUid()); 497 result.append(buffer); 498 write(fd, result.string(), result.size()); 499 return NO_ERROR; 500} 501 502status_t AudioPolicyService::onTransact( 503 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 504{ 505 return BnAudioPolicyService::onTransact(code, data, reply, flags); 506} 507 508 509// ----------- AudioPolicyService::AudioCommandThread implementation ---------- 510 511AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) 512 : Thread(false), mName(name) 513{ 514 mpToneGenerator = NULL; 515} 516 517 518AudioPolicyService::AudioCommandThread::~AudioCommandThread() 519{ 520 if (mName != "" && !mAudioCommands.isEmpty()) { 521 release_wake_lock(mName.string()); 522 } 523 mAudioCommands.clear(); 524 if (mpToneGenerator != NULL) delete mpToneGenerator; 525} 526 527void AudioPolicyService::AudioCommandThread::onFirstRef() 528{ 529 if (mName != "") { 530 run(mName.string(), ANDROID_PRIORITY_AUDIO); 531 } else { 532 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO); 533 } 534} 535 536bool AudioPolicyService::AudioCommandThread::threadLoop() 537{ 538 nsecs_t waitTime = INT64_MAX; 539 540 mLock.lock(); 541 while (!exitPending()) 542 { 543 while(!mAudioCommands.isEmpty()) { 544 nsecs_t curTime = systemTime(); 545 // commands are sorted by increasing time stamp: execute them from index 0 and up 546 if (mAudioCommands[0]->mTime <= curTime) { 547 AudioCommand *command = mAudioCommands[0]; 548 mAudioCommands.removeAt(0); 549 mLastCommand = *command; 550 551 switch (command->mCommand) { 552 case START_TONE: { 553 mLock.unlock(); 554 ToneData *data = (ToneData *)command->mParam; 555 LOGV("AudioCommandThread() processing start tone %d on stream %d", 556 data->mType, data->mStream); 557 if (mpToneGenerator != NULL) 558 delete mpToneGenerator; 559 mpToneGenerator = new ToneGenerator(data->mStream, 1.0); 560 mpToneGenerator->startTone(data->mType); 561 delete data; 562 mLock.lock(); 563 }break; 564 case STOP_TONE: { 565 mLock.unlock(); 566 LOGV("AudioCommandThread() processing stop tone"); 567 if (mpToneGenerator != NULL) { 568 mpToneGenerator->stopTone(); 569 delete mpToneGenerator; 570 mpToneGenerator = NULL; 571 } 572 mLock.lock(); 573 }break; 574 case SET_VOLUME: { 575 VolumeData *data = (VolumeData *)command->mParam; 576 LOGV("AudioCommandThread() processing set volume stream %d, \ 577 volume %f, output %d", data->mStream, data->mVolume, data->mIO); 578 command->mStatus = AudioSystem::setStreamVolume(data->mStream, 579 data->mVolume, 580 data->mIO); 581 if (command->mWaitStatus) { 582 command->mCond.signal(); 583 mWaitWorkCV.wait(mLock); 584 } 585 delete data; 586 }break; 587 case SET_PARAMETERS: { 588 ParametersData *data = (ParametersData *)command->mParam; 589 LOGV("AudioCommandThread() processing set parameters string %s, io %d", 590 data->mKeyValuePairs.string(), data->mIO); 591 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); 592 if (command->mWaitStatus) { 593 command->mCond.signal(); 594 mWaitWorkCV.wait(mLock); 595 } 596 delete data; 597 }break; 598 case SET_VOICE_VOLUME: { 599 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam; 600 LOGV("AudioCommandThread() processing set voice volume volume %f", 601 data->mVolume); 602 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); 603 if (command->mWaitStatus) { 604 command->mCond.signal(); 605 mWaitWorkCV.wait(mLock); 606 } 607 delete data; 608 }break; 609 default: 610 LOGW("AudioCommandThread() unknown command %d", command->mCommand); 611 } 612 delete command; 613 waitTime = INT64_MAX; 614 } else { 615 waitTime = mAudioCommands[0]->mTime - curTime; 616 break; 617 } 618 } 619 // release delayed commands wake lock 620 if (mName != "" && mAudioCommands.isEmpty()) { 621 release_wake_lock(mName.string()); 622 } 623 LOGV("AudioCommandThread() going to sleep"); 624 mWaitWorkCV.waitRelative(mLock, waitTime); 625 LOGV("AudioCommandThread() waking up"); 626 } 627 mLock.unlock(); 628 return false; 629} 630 631status_t AudioPolicyService::AudioCommandThread::dump(int fd) 632{ 633 const size_t SIZE = 256; 634 char buffer[SIZE]; 635 String8 result; 636 637 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); 638 result.append(buffer); 639 write(fd, result.string(), result.size()); 640 641 bool locked = tryLock(mLock); 642 if (!locked) { 643 String8 result2(kCmdDeadlockedString); 644 write(fd, result2.string(), result2.size()); 645 } 646 647 snprintf(buffer, SIZE, "- Commands:\n"); 648 result = String8(buffer); 649 result.append(" Command Time Wait pParam\n"); 650 for (int i = 0; i < (int)mAudioCommands.size(); i++) { 651 mAudioCommands[i]->dump(buffer, SIZE); 652 result.append(buffer); 653 } 654 result.append(" Last Command\n"); 655 mLastCommand.dump(buffer, SIZE); 656 result.append(buffer); 657 658 write(fd, result.string(), result.size()); 659 660 if (locked) mLock.unlock(); 661 662 return NO_ERROR; 663} 664 665void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream) 666{ 667 AudioCommand *command = new AudioCommand(); 668 command->mCommand = START_TONE; 669 ToneData *data = new ToneData(); 670 data->mType = type; 671 data->mStream = stream; 672 command->mParam = (void *)data; 673 command->mWaitStatus = false; 674 Mutex::Autolock _l(mLock); 675 insertCommand_l(command); 676 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); 677 mWaitWorkCV.signal(); 678} 679 680void AudioPolicyService::AudioCommandThread::stopToneCommand() 681{ 682 AudioCommand *command = new AudioCommand(); 683 command->mCommand = STOP_TONE; 684 command->mParam = NULL; 685 command->mWaitStatus = false; 686 Mutex::Autolock _l(mLock); 687 insertCommand_l(command); 688 LOGV("AudioCommandThread() adding tone stop"); 689 mWaitWorkCV.signal(); 690} 691 692status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, 693 float volume, 694 int output, 695 int delayMs) 696{ 697 status_t status = NO_ERROR; 698 699 AudioCommand *command = new AudioCommand(); 700 command->mCommand = SET_VOLUME; 701 VolumeData *data = new VolumeData(); 702 data->mStream = stream; 703 data->mVolume = volume; 704 data->mIO = output; 705 command->mParam = data; 706 if (delayMs == 0) { 707 command->mWaitStatus = true; 708 } else { 709 command->mWaitStatus = false; 710 } 711 Mutex::Autolock _l(mLock); 712 insertCommand_l(command, delayMs); 713 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", 714 stream, volume, output); 715 mWaitWorkCV.signal(); 716 if (command->mWaitStatus) { 717 command->mCond.wait(mLock); 718 status = command->mStatus; 719 mWaitWorkCV.signal(); 720 } 721 return status; 722} 723 724status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle, 725 const char *keyValuePairs, 726 int delayMs) 727{ 728 status_t status = NO_ERROR; 729 730 AudioCommand *command = new AudioCommand(); 731 command->mCommand = SET_PARAMETERS; 732 ParametersData *data = new ParametersData(); 733 data->mIO = ioHandle; 734 data->mKeyValuePairs = String8(keyValuePairs); 735 command->mParam = data; 736 if (delayMs == 0) { 737 command->mWaitStatus = true; 738 } else { 739 command->mWaitStatus = false; 740 } 741 Mutex::Autolock _l(mLock); 742 insertCommand_l(command, delayMs); 743 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", 744 keyValuePairs, ioHandle, delayMs); 745 mWaitWorkCV.signal(); 746 if (command->mWaitStatus) { 747 command->mCond.wait(mLock); 748 status = command->mStatus; 749 mWaitWorkCV.signal(); 750 } 751 return status; 752} 753 754status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) 755{ 756 status_t status = NO_ERROR; 757 758 AudioCommand *command = new AudioCommand(); 759 command->mCommand = SET_VOICE_VOLUME; 760 VoiceVolumeData *data = new VoiceVolumeData(); 761 data->mVolume = volume; 762 command->mParam = data; 763 if (delayMs == 0) { 764 command->mWaitStatus = true; 765 } else { 766 command->mWaitStatus = false; 767 } 768 Mutex::Autolock _l(mLock); 769 insertCommand_l(command, delayMs); 770 LOGV("AudioCommandThread() adding set voice volume volume %f", volume); 771 mWaitWorkCV.signal(); 772 if (command->mWaitStatus) { 773 command->mCond.wait(mLock); 774 status = command->mStatus; 775 mWaitWorkCV.signal(); 776 } 777 return status; 778} 779 780// insertCommand_l() must be called with mLock held 781void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs) 782{ 783 ssize_t i; 784 Vector <AudioCommand *> removedCommands; 785 786 command->mTime = systemTime() + milliseconds(delayMs); 787 788 // acquire wake lock to make sure delayed commands are processed 789 if (mName != "" && mAudioCommands.isEmpty()) { 790 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); 791 } 792 793 // check same pending commands with later time stamps and eliminate them 794 for (i = mAudioCommands.size()-1; i >= 0; i--) { 795 AudioCommand *command2 = mAudioCommands[i]; 796 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands 797 if (command2->mTime <= command->mTime) break; 798 if (command2->mCommand != command->mCommand) continue; 799 800 switch (command->mCommand) { 801 case SET_PARAMETERS: { 802 ParametersData *data = (ParametersData *)command->mParam; 803 ParametersData *data2 = (ParametersData *)command2->mParam; 804 if (data->mIO != data2->mIO) break; 805 LOGV("Comparing parameter command %s to new command %s", 806 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); 807 AudioParameter param = AudioParameter(data->mKeyValuePairs); 808 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); 809 for (size_t j = 0; j < param.size(); j++) { 810 String8 key; 811 String8 value; 812 param.getAt(j, key, value); 813 for (size_t k = 0; k < param2.size(); k++) { 814 String8 key2; 815 String8 value2; 816 param2.getAt(k, key2, value2); 817 if (key2 == key) { 818 param2.remove(key2); 819 LOGV("Filtering out parameter %s", key2.string()); 820 break; 821 } 822 } 823 } 824 // if all keys have been filtered out, remove the command. 825 // otherwise, update the key value pairs 826 if (param2.size() == 0) { 827 removedCommands.add(command2); 828 } else { 829 data2->mKeyValuePairs = param2.toString(); 830 } 831 } break; 832 833 case SET_VOLUME: { 834 VolumeData *data = (VolumeData *)command->mParam; 835 VolumeData *data2 = (VolumeData *)command2->mParam; 836 if (data->mIO != data2->mIO) break; 837 if (data->mStream != data2->mStream) break; 838 LOGV("Filtering out volume command on output %d for stream %d", 839 data->mIO, data->mStream); 840 removedCommands.add(command2); 841 } break; 842 case START_TONE: 843 case STOP_TONE: 844 default: 845 break; 846 } 847 } 848 849 // remove filtered commands 850 for (size_t j = 0; j < removedCommands.size(); j++) { 851 // removed commands always have time stamps greater than current command 852 for (size_t k = i + 1; k < mAudioCommands.size(); k++) { 853 if (mAudioCommands[k] == removedCommands[j]) { 854 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand); 855 mAudioCommands.removeAt(k); 856 break; 857 } 858 } 859 } 860 removedCommands.clear(); 861 862 // insert command at the right place according to its time stamp 863 LOGV("inserting command: %d at index %d, num commands %d", 864 command->mCommand, (int)i+1, mAudioCommands.size()); 865 mAudioCommands.insertAt(command, i + 1); 866} 867 868void AudioPolicyService::AudioCommandThread::exit() 869{ 870 LOGV("AudioCommandThread::exit"); 871 { 872 AutoMutex _l(mLock); 873 requestExit(); 874 mWaitWorkCV.signal(); 875 } 876 requestExitAndWait(); 877} 878 879void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) 880{ 881 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", 882 mCommand, 883 (int)ns2s(mTime), 884 (int)ns2ms(mTime)%1000, 885 mWaitStatus, 886 mParam); 887} 888 889/******* helpers for the service_ops callbacks defined below *********/ 890void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, 891 const char *keyValuePairs, 892 int delayMs) 893{ 894 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs, 895 delayMs); 896} 897 898int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, 899 float volume, 900 audio_io_handle_t output, 901 int delayMs) 902{ 903 return (int)mAudioCommandThread->volumeCommand((int)stream, volume, 904 (int)output, delayMs); 905} 906 907int AudioPolicyService::startTone(audio_policy_tone_t tone, 908 audio_stream_type_t stream) 909{ 910 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) 911 LOGE("startTone: illegal tone requested (%d)", tone); 912 if (stream != AUDIO_STREAM_VOICE_CALL) 913 LOGE("startTone: illegal stream (%d) requested for tone %d", stream, 914 tone); 915 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, 916 AUDIO_STREAM_VOICE_CALL); 917 return 0; 918} 919 920int AudioPolicyService::stopTone() 921{ 922 mTonePlaybackThread->stopToneCommand(); 923 return 0; 924} 925 926int AudioPolicyService::setVoiceVolume(float volume, int delayMs) 927{ 928 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); 929} 930 931/* implementation of the interface to the policy manager */ 932extern "C" { 933 934static audio_io_handle_t aps_open_output(void *service, 935 uint32_t *pDevices, 936 uint32_t *pSamplingRate, 937 uint32_t *pFormat, 938 uint32_t *pChannels, 939 uint32_t *pLatencyMs, 940 audio_policy_output_flags_t flags) 941{ 942 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 943 if (af == NULL) { 944 LOGW("%s: could not get AudioFlinger", __func__); 945 return 0; 946 } 947 948 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels, 949 pLatencyMs, flags); 950} 951 952static audio_io_handle_t aps_open_dup_output(void *service, 953 audio_io_handle_t output1, 954 audio_io_handle_t output2) 955{ 956 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 957 if (af == NULL) { 958 LOGW("%s: could not get AudioFlinger", __func__); 959 return 0; 960 } 961 return af->openDuplicateOutput(output1, output2); 962} 963 964static int aps_close_output(void *service, audio_io_handle_t output) 965{ 966 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 967 if (af == NULL) 968 return PERMISSION_DENIED; 969 970 return af->closeOutput(output); 971} 972 973static int aps_suspend_output(void *service, audio_io_handle_t output) 974{ 975 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 976 if (af == NULL) { 977 LOGW("%s: could not get AudioFlinger", __func__); 978 return PERMISSION_DENIED; 979 } 980 981 return af->suspendOutput(output); 982} 983 984static int aps_restore_output(void *service, audio_io_handle_t output) 985{ 986 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 987 if (af == NULL) { 988 LOGW("%s: could not get AudioFlinger", __func__); 989 return PERMISSION_DENIED; 990 } 991 992 return af->restoreOutput(output); 993} 994 995static audio_io_handle_t aps_open_input(void *service, 996 uint32_t *pDevices, 997 uint32_t *pSamplingRate, 998 uint32_t *pFormat, 999 uint32_t *pChannels, 1000 uint32_t acoustics) 1001{ 1002 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1003 if (af == NULL) { 1004 LOGW("%s: could not get AudioFlinger", __func__); 1005 return 0; 1006 } 1007 1008 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels, 1009 acoustics); 1010} 1011 1012static int aps_close_input(void *service, audio_io_handle_t input) 1013{ 1014 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1015 if (af == NULL) 1016 return PERMISSION_DENIED; 1017 1018 return af->closeInput(input); 1019} 1020 1021static int aps_set_stream_output(void *service, audio_stream_type_t stream, 1022 audio_io_handle_t output) 1023{ 1024 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1025 if (af == NULL) 1026 return PERMISSION_DENIED; 1027 1028 return af->setStreamOutput(stream, output); 1029} 1030 1031static int aps_move_effects(void *service, int session, 1032 audio_io_handle_t src_output, 1033 audio_io_handle_t dst_output) 1034{ 1035 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1036 if (af == NULL) 1037 return PERMISSION_DENIED; 1038 1039 return af->moveEffects(session, (int)src_output, (int)dst_output); 1040} 1041 1042static char * aps_get_parameters(void *service, audio_io_handle_t io_handle, 1043 const char *keys) 1044{ 1045 String8 result = AudioSystem::getParameters(io_handle, String8(keys)); 1046 return strdup(result.string()); 1047} 1048 1049static void aps_set_parameters(void *service, audio_io_handle_t io_handle, 1050 const char *kv_pairs, int delay_ms) 1051{ 1052 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1053 1054 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms); 1055} 1056 1057static int aps_set_stream_volume(void *service, audio_stream_type_t stream, 1058 float volume, audio_io_handle_t output, 1059 int delay_ms) 1060{ 1061 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1062 1063 return audioPolicyService->setStreamVolume(stream, volume, output, 1064 delay_ms); 1065} 1066 1067static int aps_start_tone(void *service, audio_policy_tone_t tone, 1068 audio_stream_type_t stream) 1069{ 1070 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1071 1072 return audioPolicyService->startTone(tone, stream); 1073} 1074 1075static int aps_stop_tone(void *service) 1076{ 1077 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1078 1079 return audioPolicyService->stopTone(); 1080} 1081 1082static int aps_set_voice_volume(void *service, float volume, int delay_ms) 1083{ 1084 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1085 1086 return audioPolicyService->setVoiceVolume(volume, delay_ms); 1087} 1088 1089}; // extern "C" 1090 1091namespace { 1092 struct audio_policy_service_ops aps_ops = { 1093 open_output : aps_open_output, 1094 open_duplicate_output : aps_open_dup_output, 1095 close_output : aps_close_output, 1096 suspend_output : aps_suspend_output, 1097 restore_output : aps_restore_output, 1098 open_input : aps_open_input, 1099 close_input : aps_close_input, 1100 set_stream_volume : aps_set_stream_volume, 1101 set_stream_output : aps_set_stream_output, 1102 set_parameters : aps_set_parameters, 1103 get_parameters : aps_get_parameters, 1104 start_tone : aps_start_tone, 1105 stop_tone : aps_stop_tone, 1106 set_voice_volume : aps_set_voice_volume, 1107 move_effects : aps_move_effects, 1108 }; 1109}; // namespace <unnamed> 1110 1111}; // namespace android 1112