AudioPolicyService.cpp revision 57dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfc
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#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 45namespace android { 46 47static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n"; 48static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n"; 49 50static const int kDumpLockRetries = 50; 51static const int kDumpLockSleep = 20000; 52 53static bool checkPermission() { 54 if (getpid() == IPCThreadState::self()->getCallingPid()) return true; 55 bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); 56 if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); 57 return ok; 58} 59 60namespace { 61 extern struct audio_policy_service_ops aps_ops; 62}; 63 64// ---------------------------------------------------------------------------- 65 66AudioPolicyService::AudioPolicyService() 67 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL) 68{ 69 char value[PROPERTY_VALUE_MAX]; 70 const struct hw_module_t *module; 71 int forced_val; 72 int rc; 73 74 Mutex::Autolock _l(mLock); 75 76 // start tone playback thread 77 mTonePlaybackThread = new AudioCommandThread(String8("")); 78 // start audio commands thread 79 mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread")); 80 81 /* instantiate the audio policy manager */ 82 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); 83 if (rc) 84 return; 85 86 rc = audio_policy_dev_open(module, &mpAudioPolicyDev); 87 LOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc)); 88 if (rc) 89 return; 90 91 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, 92 &mpAudioPolicy); 93 LOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc)); 94 if (rc) 95 return; 96 97 rc = mpAudioPolicy->init_check(mpAudioPolicy); 98 LOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc)); 99 if (rc) 100 return; 101 102 property_get("ro.camera.sound.forced", value, "0"); 103 forced_val = strtol(value, NULL, 0); 104 mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val); 105 106 LOGI("Loaded audio policy from %s (%s)", module->name, module->id); 107 108 // load audio pre processing modules 109 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) { 110 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE); 111 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) { 112 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE); 113 } 114} 115 116AudioPolicyService::~AudioPolicyService() 117{ 118 mTonePlaybackThread->exit(); 119 mTonePlaybackThread.clear(); 120 mAudioCommandThread->exit(); 121 mAudioCommandThread.clear(); 122 123 124 // release audio pre processing resources 125 for (size_t i = 0; i < mInputSources.size(); i++) { 126 InputSourceDesc *source = mInputSources.valueAt(i); 127 Vector <EffectDesc *> effects = source->mEffects; 128 for (size_t j = 0; j < effects.size(); j++) { 129 delete effects[j]->mName; 130 Vector <effect_param_t *> params = effects[j]->mParams; 131 for (size_t k = 0; k < params.size(); k++) { 132 delete params[k]; 133 } 134 params.clear(); 135 delete effects[j]; 136 } 137 effects.clear(); 138 delete source; 139 } 140 mInputSources.clear(); 141 142 for (size_t i = 0; i < mInputs.size(); i++) { 143 mInputs.valueAt(i)->mEffects.clear(); 144 delete mInputs.valueAt(i); 145 } 146 mInputs.clear(); 147 148 if (mpAudioPolicy && mpAudioPolicyDev) 149 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); 150 if (mpAudioPolicyDev) 151 audio_policy_dev_close(mpAudioPolicyDev); 152} 153 154status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, 155 audio_policy_dev_state_t state, 156 const char *device_address) 157{ 158 if (mpAudioPolicy == NULL) { 159 return NO_INIT; 160 } 161 if (!checkPermission()) { 162 return PERMISSION_DENIED; 163 } 164 if (!audio_is_output_device(device) && !audio_is_input_device(device)) { 165 return BAD_VALUE; 166 } 167 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && 168 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { 169 return BAD_VALUE; 170 } 171 172 LOGV("setDeviceConnectionState() tid %d", gettid()); 173 Mutex::Autolock _l(mLock); 174 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device, 175 state, device_address); 176} 177 178audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( 179 audio_devices_t device, 180 const char *device_address) 181{ 182 if (mpAudioPolicy == NULL) { 183 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 184 } 185 if (!checkPermission()) { 186 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 187 } 188 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device, 189 device_address); 190} 191 192status_t AudioPolicyService::setPhoneState(int state) 193{ 194 if (mpAudioPolicy == NULL) { 195 return NO_INIT; 196 } 197 if (!checkPermission()) { 198 return PERMISSION_DENIED; 199 } 200 if (state < 0 || state >= AUDIO_MODE_CNT) { 201 return BAD_VALUE; 202 } 203 204 LOGV("setPhoneState() tid %d", gettid()); 205 206 // TODO: check if it is more appropriate to do it in platform specific policy manager 207 AudioSystem::setMode(state); 208 209 Mutex::Autolock _l(mLock); 210 mpAudioPolicy->set_phone_state(mpAudioPolicy, state); 211 return NO_ERROR; 212} 213 214status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask) 215{ 216 if (mpAudioPolicy == NULL) { 217 return NO_INIT; 218 } 219 if (!checkPermission()) { 220 return PERMISSION_DENIED; 221 } 222 223 mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask); 224 return NO_ERROR; 225} 226 227status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, 228 audio_policy_forced_cfg_t config) 229{ 230 if (mpAudioPolicy == NULL) { 231 return NO_INIT; 232 } 233 if (!checkPermission()) { 234 return PERMISSION_DENIED; 235 } 236 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 237 return BAD_VALUE; 238 } 239 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { 240 return BAD_VALUE; 241 } 242 LOGV("setForceUse() tid %d", gettid()); 243 Mutex::Autolock _l(mLock); 244 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config); 245 return NO_ERROR; 246} 247 248audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) 249{ 250 if (mpAudioPolicy == NULL) { 251 return AUDIO_POLICY_FORCE_NONE; 252 } 253 if (!checkPermission()) { 254 return AUDIO_POLICY_FORCE_NONE; 255 } 256 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 257 return AUDIO_POLICY_FORCE_NONE; 258 } 259 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage); 260} 261 262audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, 263 uint32_t samplingRate, 264 uint32_t format, 265 uint32_t channels, 266 audio_policy_output_flags_t flags) 267{ 268 if (mpAudioPolicy == NULL) { 269 return 0; 270 } 271 LOGV("getOutput() tid %d", gettid()); 272 Mutex::Autolock _l(mLock); 273 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags); 274} 275 276status_t AudioPolicyService::startOutput(audio_io_handle_t output, 277 audio_stream_type_t stream, 278 int session) 279{ 280 if (mpAudioPolicy == NULL) { 281 return NO_INIT; 282 } 283 LOGV("startOutput() tid %d", gettid()); 284 Mutex::Autolock _l(mLock); 285 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session); 286} 287 288status_t AudioPolicyService::stopOutput(audio_io_handle_t output, 289 audio_stream_type_t stream, 290 int session) 291{ 292 if (mpAudioPolicy == NULL) { 293 return NO_INIT; 294 } 295 LOGV("stopOutput() tid %d", gettid()); 296 Mutex::Autolock _l(mLock); 297 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session); 298} 299 300void AudioPolicyService::releaseOutput(audio_io_handle_t output) 301{ 302 if (mpAudioPolicy == NULL) { 303 return; 304 } 305 LOGV("releaseOutput() tid %d", gettid()); 306 Mutex::Autolock _l(mLock); 307 mpAudioPolicy->release_output(mpAudioPolicy, output); 308} 309 310audio_io_handle_t AudioPolicyService::getInput(int inputSource, 311 uint32_t samplingRate, 312 uint32_t format, 313 uint32_t channels, 314 audio_in_acoustics_t acoustics, 315 int audioSession) 316{ 317 if (mpAudioPolicy == NULL) { 318 return 0; 319 } 320 Mutex::Autolock _l(mLock); 321 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, 322 format, channels, acoustics); 323 324 if (input == 0) { 325 return input; 326 } 327 // create audio pre processors according to input source 328 ssize_t index = mInputSources.indexOfKey((audio_source_t)inputSource); 329 if (index < 0) { 330 return input; 331 } 332 ssize_t idx = mInputs.indexOfKey(input); 333 InputDesc *inputDesc; 334 if (idx < 0) { 335 inputDesc = new InputDesc(); 336 inputDesc->mSessionId = audioSession; 337 mInputs.add(input, inputDesc); 338 } else { 339 inputDesc = mInputs.valueAt(idx); 340 } 341 342 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects; 343 for (size_t i = 0; i < effects.size(); i++) { 344 EffectDesc *effect = effects[i]; 345 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input); 346 status_t status = fx->initCheck(); 347 if (status != NO_ERROR && status != ALREADY_EXISTS) { 348 LOGW("Failed to create Fx %s on input %d", effect->mName, input); 349 // fx goes out of scope and strong ref on AudioEffect is released 350 continue; 351 } 352 for (size_t j = 0; j < effect->mParams.size(); j++) { 353 fx->setParameter(effect->mParams[j]); 354 } 355 inputDesc->mEffects.add(fx); 356 } 357 setPreProcessorEnabled(inputDesc, true); 358 return input; 359} 360 361status_t AudioPolicyService::startInput(audio_io_handle_t input) 362{ 363 if (mpAudioPolicy == NULL) { 364 return NO_INIT; 365 } 366 Mutex::Autolock _l(mLock); 367 368 return mpAudioPolicy->start_input(mpAudioPolicy, input); 369} 370 371status_t AudioPolicyService::stopInput(audio_io_handle_t input) 372{ 373 if (mpAudioPolicy == NULL) { 374 return NO_INIT; 375 } 376 Mutex::Autolock _l(mLock); 377 378 return mpAudioPolicy->stop_input(mpAudioPolicy, input); 379} 380 381void AudioPolicyService::releaseInput(audio_io_handle_t input) 382{ 383 if (mpAudioPolicy == NULL) { 384 return; 385 } 386 Mutex::Autolock _l(mLock); 387 mpAudioPolicy->release_input(mpAudioPolicy, input); 388 389 ssize_t index = mInputs.indexOfKey(input); 390 if (index < 0) { 391 return; 392 } 393 InputDesc *inputDesc = mInputs.valueAt(index); 394 setPreProcessorEnabled(inputDesc, false); 395 inputDesc->mEffects.clear(); 396 delete inputDesc; 397 mInputs.removeItemsAt(index); 398} 399 400status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, 401 int indexMin, 402 int indexMax) 403{ 404 if (mpAudioPolicy == NULL) { 405 return NO_INIT; 406 } 407 if (!checkPermission()) { 408 return PERMISSION_DENIED; 409 } 410 if (stream < 0 || stream >= AUDIO_STREAM_CNT) { 411 return BAD_VALUE; 412 } 413 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax); 414 return NO_ERROR; 415} 416 417status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index) 418{ 419 if (mpAudioPolicy == NULL) { 420 return NO_INIT; 421 } 422 if (!checkPermission()) { 423 return PERMISSION_DENIED; 424 } 425 if (stream < 0 || stream >= AUDIO_STREAM_CNT) { 426 return BAD_VALUE; 427 } 428 429 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index); 430} 431 432status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index) 433{ 434 if (mpAudioPolicy == NULL) { 435 return NO_INIT; 436 } 437 if (!checkPermission()) { 438 return PERMISSION_DENIED; 439 } 440 if (stream < 0 || stream >= AUDIO_STREAM_CNT) { 441 return BAD_VALUE; 442 } 443 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index); 444} 445 446uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) 447{ 448 if (mpAudioPolicy == NULL) { 449 return 0; 450 } 451 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream); 452} 453 454uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) 455{ 456 if (mpAudioPolicy == NULL) { 457 return 0; 458 } 459 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream); 460} 461 462audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc) 463{ 464 if (mpAudioPolicy == NULL) { 465 return NO_INIT; 466 } 467 Mutex::Autolock _l(mLock); 468 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc); 469} 470 471status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc, 472 audio_io_handle_t io, 473 uint32_t strategy, 474 int session, 475 int id) 476{ 477 if (mpAudioPolicy == NULL) { 478 return NO_INIT; 479 } 480 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id); 481} 482 483status_t AudioPolicyService::unregisterEffect(int id) 484{ 485 if (mpAudioPolicy == NULL) { 486 return NO_INIT; 487 } 488 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id); 489} 490 491bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const 492{ 493 if (mpAudioPolicy == NULL) { 494 return 0; 495 } 496 Mutex::Autolock _l(mLock); 497 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs); 498} 499 500status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession, 501 effect_descriptor_t *descriptors, 502 uint32_t *count) 503{ 504 505 if (mpAudioPolicy == NULL) { 506 *count = 0; 507 return NO_INIT; 508 } 509 Mutex::Autolock _l(mLock); 510 status_t status = NO_ERROR; 511 512 size_t index; 513 for (index = 0; index < mInputs.size(); index++) { 514 if (mInputs.valueAt(index)->mSessionId == audioSession) { 515 break; 516 } 517 } 518 if (index == mInputs.size()) { 519 *count = 0; 520 return BAD_VALUE; 521 } 522 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects; 523 524 for (size_t i = 0; i < effects.size(); i++) { 525 effect_descriptor_t desc = effects[i]->descriptor(); 526 if (i < *count) { 527 memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t)); 528 } 529 } 530 if (effects.size() > *count) { 531 status = NO_MEMORY; 532 } 533 *count = effects.size(); 534 return status; 535} 536 537void AudioPolicyService::binderDied(const wp<IBinder>& who) { 538 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), 539 IPCThreadState::self()->getCallingPid()); 540} 541 542static bool tryLock(Mutex& mutex) 543{ 544 bool locked = false; 545 for (int i = 0; i < kDumpLockRetries; ++i) { 546 if (mutex.tryLock() == NO_ERROR) { 547 locked = true; 548 break; 549 } 550 usleep(kDumpLockSleep); 551 } 552 return locked; 553} 554 555status_t AudioPolicyService::dumpInternals(int fd) 556{ 557 const size_t SIZE = 256; 558 char buffer[SIZE]; 559 String8 result; 560 561 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); 562 result.append(buffer); 563 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); 564 result.append(buffer); 565 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); 566 result.append(buffer); 567 568 write(fd, result.string(), result.size()); 569 return NO_ERROR; 570} 571 572status_t AudioPolicyService::dump(int fd, const Vector<String16>& args) 573{ 574 if (checkCallingPermission(String16("android.permission.DUMP")) == false) { 575 dumpPermissionDenial(fd); 576 } else { 577 bool locked = tryLock(mLock); 578 if (!locked) { 579 String8 result(kDeadlockedString); 580 write(fd, result.string(), result.size()); 581 } 582 583 dumpInternals(fd); 584 if (mAudioCommandThread != NULL) { 585 mAudioCommandThread->dump(fd); 586 } 587 if (mTonePlaybackThread != NULL) { 588 mTonePlaybackThread->dump(fd); 589 } 590 591 if (mpAudioPolicy) { 592 mpAudioPolicy->dump(mpAudioPolicy, fd); 593 } 594 595 if (locked) mLock.unlock(); 596 } 597 return NO_ERROR; 598} 599 600status_t AudioPolicyService::dumpPermissionDenial(int fd) 601{ 602 const size_t SIZE = 256; 603 char buffer[SIZE]; 604 String8 result; 605 snprintf(buffer, SIZE, "Permission Denial: " 606 "can't dump AudioPolicyService from pid=%d, uid=%d\n", 607 IPCThreadState::self()->getCallingPid(), 608 IPCThreadState::self()->getCallingUid()); 609 result.append(buffer); 610 write(fd, result.string(), result.size()); 611 return NO_ERROR; 612} 613 614void AudioPolicyService::setPreProcessorEnabled(InputDesc *inputDesc, bool enabled) 615{ 616 Vector<sp<AudioEffect> > fxVector = inputDesc->mEffects; 617 for (size_t i = 0; i < fxVector.size(); i++) { 618 sp<AudioEffect> fx = fxVector.itemAt(i); 619 fx->setEnabled(enabled); 620 } 621} 622 623status_t AudioPolicyService::onTransact( 624 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 625{ 626 return BnAudioPolicyService::onTransact(code, data, reply, flags); 627} 628 629 630// ----------- AudioPolicyService::AudioCommandThread implementation ---------- 631 632AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) 633 : Thread(false), mName(name) 634{ 635 mpToneGenerator = NULL; 636} 637 638 639AudioPolicyService::AudioCommandThread::~AudioCommandThread() 640{ 641 if (mName != "" && !mAudioCommands.isEmpty()) { 642 release_wake_lock(mName.string()); 643 } 644 mAudioCommands.clear(); 645 if (mpToneGenerator != NULL) delete mpToneGenerator; 646} 647 648void AudioPolicyService::AudioCommandThread::onFirstRef() 649{ 650 if (mName != "") { 651 run(mName.string(), ANDROID_PRIORITY_AUDIO); 652 } else { 653 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO); 654 } 655} 656 657bool AudioPolicyService::AudioCommandThread::threadLoop() 658{ 659 nsecs_t waitTime = INT64_MAX; 660 661 mLock.lock(); 662 while (!exitPending()) 663 { 664 while(!mAudioCommands.isEmpty()) { 665 nsecs_t curTime = systemTime(); 666 // commands are sorted by increasing time stamp: execute them from index 0 and up 667 if (mAudioCommands[0]->mTime <= curTime) { 668 AudioCommand *command = mAudioCommands[0]; 669 mAudioCommands.removeAt(0); 670 mLastCommand = *command; 671 672 switch (command->mCommand) { 673 case START_TONE: { 674 mLock.unlock(); 675 ToneData *data = (ToneData *)command->mParam; 676 LOGV("AudioCommandThread() processing start tone %d on stream %d", 677 data->mType, data->mStream); 678 if (mpToneGenerator != NULL) 679 delete mpToneGenerator; 680 mpToneGenerator = new ToneGenerator(data->mStream, 1.0); 681 mpToneGenerator->startTone(data->mType); 682 delete data; 683 mLock.lock(); 684 }break; 685 case STOP_TONE: { 686 mLock.unlock(); 687 LOGV("AudioCommandThread() processing stop tone"); 688 if (mpToneGenerator != NULL) { 689 mpToneGenerator->stopTone(); 690 delete mpToneGenerator; 691 mpToneGenerator = NULL; 692 } 693 mLock.lock(); 694 }break; 695 case SET_VOLUME: { 696 VolumeData *data = (VolumeData *)command->mParam; 697 LOGV("AudioCommandThread() processing set volume stream %d, \ 698 volume %f, output %d", data->mStream, data->mVolume, data->mIO); 699 command->mStatus = AudioSystem::setStreamVolume(data->mStream, 700 data->mVolume, 701 data->mIO); 702 if (command->mWaitStatus) { 703 command->mCond.signal(); 704 mWaitWorkCV.wait(mLock); 705 } 706 delete data; 707 }break; 708 case SET_PARAMETERS: { 709 ParametersData *data = (ParametersData *)command->mParam; 710 LOGV("AudioCommandThread() processing set parameters string %s, io %d", 711 data->mKeyValuePairs.string(), data->mIO); 712 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); 713 if (command->mWaitStatus) { 714 command->mCond.signal(); 715 mWaitWorkCV.wait(mLock); 716 } 717 delete data; 718 }break; 719 case SET_VOICE_VOLUME: { 720 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam; 721 LOGV("AudioCommandThread() processing set voice volume volume %f", 722 data->mVolume); 723 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); 724 if (command->mWaitStatus) { 725 command->mCond.signal(); 726 mWaitWorkCV.wait(mLock); 727 } 728 delete data; 729 }break; 730 default: 731 LOGW("AudioCommandThread() unknown command %d", command->mCommand); 732 } 733 delete command; 734 waitTime = INT64_MAX; 735 } else { 736 waitTime = mAudioCommands[0]->mTime - curTime; 737 break; 738 } 739 } 740 // release delayed commands wake lock 741 if (mName != "" && mAudioCommands.isEmpty()) { 742 release_wake_lock(mName.string()); 743 } 744 LOGV("AudioCommandThread() going to sleep"); 745 mWaitWorkCV.waitRelative(mLock, waitTime); 746 LOGV("AudioCommandThread() waking up"); 747 } 748 mLock.unlock(); 749 return false; 750} 751 752status_t AudioPolicyService::AudioCommandThread::dump(int fd) 753{ 754 const size_t SIZE = 256; 755 char buffer[SIZE]; 756 String8 result; 757 758 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); 759 result.append(buffer); 760 write(fd, result.string(), result.size()); 761 762 bool locked = tryLock(mLock); 763 if (!locked) { 764 String8 result2(kCmdDeadlockedString); 765 write(fd, result2.string(), result2.size()); 766 } 767 768 snprintf(buffer, SIZE, "- Commands:\n"); 769 result = String8(buffer); 770 result.append(" Command Time Wait pParam\n"); 771 for (int i = 0; i < (int)mAudioCommands.size(); i++) { 772 mAudioCommands[i]->dump(buffer, SIZE); 773 result.append(buffer); 774 } 775 result.append(" Last Command\n"); 776 mLastCommand.dump(buffer, SIZE); 777 result.append(buffer); 778 779 write(fd, result.string(), result.size()); 780 781 if (locked) mLock.unlock(); 782 783 return NO_ERROR; 784} 785 786void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream) 787{ 788 AudioCommand *command = new AudioCommand(); 789 command->mCommand = START_TONE; 790 ToneData *data = new ToneData(); 791 data->mType = type; 792 data->mStream = stream; 793 command->mParam = (void *)data; 794 command->mWaitStatus = false; 795 Mutex::Autolock _l(mLock); 796 insertCommand_l(command); 797 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); 798 mWaitWorkCV.signal(); 799} 800 801void AudioPolicyService::AudioCommandThread::stopToneCommand() 802{ 803 AudioCommand *command = new AudioCommand(); 804 command->mCommand = STOP_TONE; 805 command->mParam = NULL; 806 command->mWaitStatus = false; 807 Mutex::Autolock _l(mLock); 808 insertCommand_l(command); 809 LOGV("AudioCommandThread() adding tone stop"); 810 mWaitWorkCV.signal(); 811} 812 813status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, 814 float volume, 815 int output, 816 int delayMs) 817{ 818 status_t status = NO_ERROR; 819 820 AudioCommand *command = new AudioCommand(); 821 command->mCommand = SET_VOLUME; 822 VolumeData *data = new VolumeData(); 823 data->mStream = stream; 824 data->mVolume = volume; 825 data->mIO = output; 826 command->mParam = data; 827 if (delayMs == 0) { 828 command->mWaitStatus = true; 829 } else { 830 command->mWaitStatus = false; 831 } 832 Mutex::Autolock _l(mLock); 833 insertCommand_l(command, delayMs); 834 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", 835 stream, volume, output); 836 mWaitWorkCV.signal(); 837 if (command->mWaitStatus) { 838 command->mCond.wait(mLock); 839 status = command->mStatus; 840 mWaitWorkCV.signal(); 841 } 842 return status; 843} 844 845status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle, 846 const char *keyValuePairs, 847 int delayMs) 848{ 849 status_t status = NO_ERROR; 850 851 AudioCommand *command = new AudioCommand(); 852 command->mCommand = SET_PARAMETERS; 853 ParametersData *data = new ParametersData(); 854 data->mIO = ioHandle; 855 data->mKeyValuePairs = String8(keyValuePairs); 856 command->mParam = data; 857 if (delayMs == 0) { 858 command->mWaitStatus = true; 859 } else { 860 command->mWaitStatus = false; 861 } 862 Mutex::Autolock _l(mLock); 863 insertCommand_l(command, delayMs); 864 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", 865 keyValuePairs, ioHandle, delayMs); 866 mWaitWorkCV.signal(); 867 if (command->mWaitStatus) { 868 command->mCond.wait(mLock); 869 status = command->mStatus; 870 mWaitWorkCV.signal(); 871 } 872 return status; 873} 874 875status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) 876{ 877 status_t status = NO_ERROR; 878 879 AudioCommand *command = new AudioCommand(); 880 command->mCommand = SET_VOICE_VOLUME; 881 VoiceVolumeData *data = new VoiceVolumeData(); 882 data->mVolume = volume; 883 command->mParam = data; 884 if (delayMs == 0) { 885 command->mWaitStatus = true; 886 } else { 887 command->mWaitStatus = false; 888 } 889 Mutex::Autolock _l(mLock); 890 insertCommand_l(command, delayMs); 891 LOGV("AudioCommandThread() adding set voice volume volume %f", volume); 892 mWaitWorkCV.signal(); 893 if (command->mWaitStatus) { 894 command->mCond.wait(mLock); 895 status = command->mStatus; 896 mWaitWorkCV.signal(); 897 } 898 return status; 899} 900 901// insertCommand_l() must be called with mLock held 902void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs) 903{ 904 ssize_t i; 905 Vector <AudioCommand *> removedCommands; 906 907 command->mTime = systemTime() + milliseconds(delayMs); 908 909 // acquire wake lock to make sure delayed commands are processed 910 if (mName != "" && mAudioCommands.isEmpty()) { 911 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); 912 } 913 914 // check same pending commands with later time stamps and eliminate them 915 for (i = mAudioCommands.size()-1; i >= 0; i--) { 916 AudioCommand *command2 = mAudioCommands[i]; 917 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands 918 if (command2->mTime <= command->mTime) break; 919 if (command2->mCommand != command->mCommand) continue; 920 921 switch (command->mCommand) { 922 case SET_PARAMETERS: { 923 ParametersData *data = (ParametersData *)command->mParam; 924 ParametersData *data2 = (ParametersData *)command2->mParam; 925 if (data->mIO != data2->mIO) break; 926 LOGV("Comparing parameter command %s to new command %s", 927 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); 928 AudioParameter param = AudioParameter(data->mKeyValuePairs); 929 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); 930 for (size_t j = 0; j < param.size(); j++) { 931 String8 key; 932 String8 value; 933 param.getAt(j, key, value); 934 for (size_t k = 0; k < param2.size(); k++) { 935 String8 key2; 936 String8 value2; 937 param2.getAt(k, key2, value2); 938 if (key2 == key) { 939 param2.remove(key2); 940 LOGV("Filtering out parameter %s", key2.string()); 941 break; 942 } 943 } 944 } 945 // if all keys have been filtered out, remove the command. 946 // otherwise, update the key value pairs 947 if (param2.size() == 0) { 948 removedCommands.add(command2); 949 } else { 950 data2->mKeyValuePairs = param2.toString(); 951 } 952 } break; 953 954 case SET_VOLUME: { 955 VolumeData *data = (VolumeData *)command->mParam; 956 VolumeData *data2 = (VolumeData *)command2->mParam; 957 if (data->mIO != data2->mIO) break; 958 if (data->mStream != data2->mStream) break; 959 LOGV("Filtering out volume command on output %d for stream %d", 960 data->mIO, data->mStream); 961 removedCommands.add(command2); 962 } break; 963 case START_TONE: 964 case STOP_TONE: 965 default: 966 break; 967 } 968 } 969 970 // remove filtered commands 971 for (size_t j = 0; j < removedCommands.size(); j++) { 972 // removed commands always have time stamps greater than current command 973 for (size_t k = i + 1; k < mAudioCommands.size(); k++) { 974 if (mAudioCommands[k] == removedCommands[j]) { 975 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand); 976 mAudioCommands.removeAt(k); 977 break; 978 } 979 } 980 } 981 removedCommands.clear(); 982 983 // insert command at the right place according to its time stamp 984 LOGV("inserting command: %d at index %d, num commands %d", 985 command->mCommand, (int)i+1, mAudioCommands.size()); 986 mAudioCommands.insertAt(command, i + 1); 987} 988 989void AudioPolicyService::AudioCommandThread::exit() 990{ 991 LOGV("AudioCommandThread::exit"); 992 { 993 AutoMutex _l(mLock); 994 requestExit(); 995 mWaitWorkCV.signal(); 996 } 997 requestExitAndWait(); 998} 999 1000void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) 1001{ 1002 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", 1003 mCommand, 1004 (int)ns2s(mTime), 1005 (int)ns2ms(mTime)%1000, 1006 mWaitStatus, 1007 mParam); 1008} 1009 1010/******* helpers for the service_ops callbacks defined below *********/ 1011void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, 1012 const char *keyValuePairs, 1013 int delayMs) 1014{ 1015 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs, 1016 delayMs); 1017} 1018 1019int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, 1020 float volume, 1021 audio_io_handle_t output, 1022 int delayMs) 1023{ 1024 return (int)mAudioCommandThread->volumeCommand((int)stream, volume, 1025 (int)output, delayMs); 1026} 1027 1028int AudioPolicyService::startTone(audio_policy_tone_t tone, 1029 audio_stream_type_t stream) 1030{ 1031 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) 1032 LOGE("startTone: illegal tone requested (%d)", tone); 1033 if (stream != AUDIO_STREAM_VOICE_CALL) 1034 LOGE("startTone: illegal stream (%d) requested for tone %d", stream, 1035 tone); 1036 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, 1037 AUDIO_STREAM_VOICE_CALL); 1038 return 0; 1039} 1040 1041int AudioPolicyService::stopTone() 1042{ 1043 mTonePlaybackThread->stopToneCommand(); 1044 return 0; 1045} 1046 1047int AudioPolicyService::setVoiceVolume(float volume, int delayMs) 1048{ 1049 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); 1050} 1051 1052// ---------------------------------------------------------------------------- 1053// Audio pre-processing configuration 1054// ---------------------------------------------------------------------------- 1055 1056const char *AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = { 1057 MIC_SRC_TAG, 1058 VOICE_UL_SRC_TAG, 1059 VOICE_DL_SRC_TAG, 1060 VOICE_CALL_SRC_TAG, 1061 CAMCORDER_SRC_TAG, 1062 VOICE_REC_SRC_TAG, 1063 VOICE_COMM_SRC_TAG 1064}; 1065 1066// returns the audio_source_t enum corresponding to the input source name or 1067// AUDIO_SOURCE_CNT is no match found 1068audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name) 1069{ 1070 int i; 1071 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) { 1072 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) { 1073 LOGV("inputSourceNameToEnum found source %s %d", name, i); 1074 break; 1075 } 1076 } 1077 return (audio_source_t)i; 1078} 1079 1080size_t AudioPolicyService::growParamSize(char *param, 1081 size_t size, 1082 size_t *curSize, 1083 size_t *totSize) 1084{ 1085 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int) 1086 size_t pos = ((*curSize - 1 ) / size + 1) * size; 1087 1088 if (pos + size > *totSize) { 1089 while (pos + size > *totSize) { 1090 *totSize += ((*totSize + 7) / 8) * 4; 1091 } 1092 param = (char *)realloc(param, *totSize); 1093 } 1094 *curSize = pos + size; 1095 return pos; 1096} 1097 1098size_t AudioPolicyService::readParamValue(cnode *node, 1099 char *param, 1100 size_t *curSize, 1101 size_t *totSize) 1102{ 1103 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) { 1104 size_t pos = growParamSize(param, sizeof(short), curSize, totSize); 1105 *(short *)((char *)param + pos) = (short)atoi(node->value); 1106 LOGV("readParamValue() reading short %d", *(short *)((char *)param + pos)); 1107 return sizeof(short); 1108 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) { 1109 size_t pos = growParamSize(param, sizeof(int), curSize, totSize); 1110 *(int *)((char *)param + pos) = atoi(node->value); 1111 LOGV("readParamValue() reading int %d", *(int *)((char *)param + pos)); 1112 return sizeof(int); 1113 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) { 1114 size_t pos = growParamSize(param, sizeof(float), curSize, totSize); 1115 *(float *)((char *)param + pos) = (float)atof(node->value); 1116 LOGV("readParamValue() reading float %f",*(float *)((char *)param + pos)); 1117 return sizeof(float); 1118 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) { 1119 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize); 1120 if (strncmp(node->value, "false", strlen("false") + 1) == 0) { 1121 *(bool *)((char *)param + pos) = false; 1122 } else { 1123 *(bool *)((char *)param + pos) = true; 1124 } 1125 LOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false"); 1126 return sizeof(bool); 1127 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) { 1128 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX); 1129 if (*curSize + len + 1 > *totSize) { 1130 *totSize = *curSize + len + 1; 1131 param = (char *)realloc(param, *totSize); 1132 } 1133 strncpy(param + *curSize, node->value, len); 1134 *curSize += len; 1135 param[*curSize] = '\0'; 1136 LOGV("readParamValue() reading string %s", param + *curSize - len); 1137 return len; 1138 } 1139 LOGW("readParamValue() unknown param type %s", node->name); 1140 return 0; 1141} 1142 1143effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root) 1144{ 1145 cnode *param; 1146 cnode *value; 1147 size_t curSize = sizeof(effect_param_t); 1148 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int); 1149 effect_param_t *fx_param = (effect_param_t *)malloc(totSize); 1150 1151 param = config_find(root, PARAM_TAG); 1152 value = config_find(root, VALUE_TAG); 1153 if (param == NULL && value == NULL) { 1154 // try to parse simple parameter form {int int} 1155 param = root->first_child; 1156 if (param) { 1157 // Note: that a pair of random strings is read as 0 0 1158 int *ptr = (int *)fx_param->data; 1159 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t)); 1160 LOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2); 1161 *ptr++ = atoi(param->name); 1162 *ptr = atoi(param->value); 1163 fx_param->psize = sizeof(int); 1164 fx_param->vsize = sizeof(int); 1165 return fx_param; 1166 } 1167 } 1168 if (param == NULL || value == NULL) { 1169 LOGW("loadEffectParameter() invalid parameter description %s", root->name); 1170 goto error; 1171 } 1172 1173 fx_param->psize = 0; 1174 param = param->first_child; 1175 while (param) { 1176 LOGV("loadEffectParameter() reading param of type %s", param->name); 1177 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize); 1178 if (size == 0) { 1179 goto error; 1180 } 1181 fx_param->psize += size; 1182 param = param->next; 1183 } 1184 1185 // align start of value field on 32 bit boundary 1186 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int); 1187 1188 fx_param->vsize = 0; 1189 value = value->first_child; 1190 while (value) { 1191 LOGV("loadEffectParameter() reading value of type %s", value->name); 1192 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize); 1193 if (size == 0) { 1194 goto error; 1195 } 1196 fx_param->vsize += size; 1197 value = value->next; 1198 } 1199 1200 return fx_param; 1201 1202error: 1203 delete fx_param; 1204 return NULL; 1205} 1206 1207void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params) 1208{ 1209 cnode *node = root->first_child; 1210 while (node) { 1211 LOGV("loadEffectParameters() loading param %s", node->name); 1212 effect_param_t *param = loadEffectParameter(node); 1213 if (param == NULL) { 1214 node = node->next; 1215 continue; 1216 } 1217 params.add(param); 1218 node = node->next; 1219 } 1220} 1221 1222AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource( 1223 cnode *root, 1224 const Vector <EffectDesc *>& effects) 1225{ 1226 cnode *node = root->first_child; 1227 if (node == NULL) { 1228 LOGW("loadInputSource() empty element %s", root->name); 1229 return NULL; 1230 } 1231 InputSourceDesc *source = new InputSourceDesc(); 1232 while (node) { 1233 size_t i; 1234 for (i = 0; i < effects.size(); i++) { 1235 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) { 1236 LOGV("loadInputSource() found effect %s in list", node->name); 1237 break; 1238 } 1239 } 1240 if (i == effects.size()) { 1241 LOGV("loadInputSource() effect %s not in list", node->name); 1242 node = node->next; 1243 continue; 1244 } 1245 EffectDesc *effect = new EffectDesc(*effects[i]); 1246 loadEffectParameters(node, effect->mParams); 1247 LOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow); 1248 source->mEffects.add(effect); 1249 node = node->next; 1250 } 1251 if (source->mEffects.size() == 0) { 1252 LOGW("loadInputSource() no valid effects found in source %s", root->name); 1253 delete source; 1254 return NULL; 1255 } 1256 return source; 1257} 1258 1259status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects) 1260{ 1261 cnode *node = config_find(root, PREPROCESSING_TAG); 1262 if (node == NULL) { 1263 return -ENOENT; 1264 } 1265 node = node->first_child; 1266 while (node) { 1267 audio_source_t source = inputSourceNameToEnum(node->name); 1268 if (source == AUDIO_SOURCE_CNT) { 1269 LOGW("loadInputSources() invalid input source %s", node->name); 1270 node = node->next; 1271 continue; 1272 } 1273 LOGV("loadInputSources() loading input source %s", node->name); 1274 InputSourceDesc *desc = loadInputSource(node, effects); 1275 if (desc == NULL) { 1276 node = node->next; 1277 continue; 1278 } 1279 mInputSources.add(source, desc); 1280 node = node->next; 1281 } 1282 return NO_ERROR; 1283} 1284 1285AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root) 1286{ 1287 cnode *node = config_find(root, UUID_TAG); 1288 if (node == NULL) { 1289 return NULL; 1290 } 1291 effect_uuid_t uuid; 1292 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) { 1293 LOGW("loadEffect() invalid uuid %s", node->value); 1294 return NULL; 1295 } 1296 EffectDesc *effect = new EffectDesc(); 1297 effect->mName = strdup(root->name); 1298 memcpy(&effect->mUuid, &uuid, sizeof(effect_uuid_t)); 1299 1300 return effect; 1301} 1302 1303status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects) 1304{ 1305 cnode *node = config_find(root, EFFECTS_TAG); 1306 if (node == NULL) { 1307 return -ENOENT; 1308 } 1309 node = node->first_child; 1310 while (node) { 1311 LOGV("loadEffects() loading effect %s", node->name); 1312 EffectDesc *effect = loadEffect(node); 1313 if (effect == NULL) { 1314 node = node->next; 1315 continue; 1316 } 1317 effects.add(effect); 1318 node = node->next; 1319 } 1320 return NO_ERROR; 1321} 1322 1323status_t AudioPolicyService::loadPreProcessorConfig(const char *path) 1324{ 1325 cnode *root; 1326 char *data; 1327 1328 data = (char *)load_file(path, NULL); 1329 if (data == NULL) { 1330 return -ENODEV; 1331 } 1332 root = config_node("", ""); 1333 config_load(root, data); 1334 1335 Vector <EffectDesc *> effects; 1336 loadEffects(root, effects); 1337 loadInputSources(root, effects); 1338 1339 config_free(root); 1340 free(root); 1341 free(data); 1342 1343 return NO_ERROR; 1344} 1345 1346/* implementation of the interface to the policy manager */ 1347extern "C" { 1348 1349static audio_io_handle_t aps_open_output(void *service, 1350 uint32_t *pDevices, 1351 uint32_t *pSamplingRate, 1352 uint32_t *pFormat, 1353 uint32_t *pChannels, 1354 uint32_t *pLatencyMs, 1355 audio_policy_output_flags_t flags) 1356{ 1357 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1358 if (af == NULL) { 1359 LOGW("%s: could not get AudioFlinger", __func__); 1360 return 0; 1361 } 1362 1363 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels, 1364 pLatencyMs, flags); 1365} 1366 1367static audio_io_handle_t aps_open_dup_output(void *service, 1368 audio_io_handle_t output1, 1369 audio_io_handle_t output2) 1370{ 1371 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1372 if (af == NULL) { 1373 LOGW("%s: could not get AudioFlinger", __func__); 1374 return 0; 1375 } 1376 return af->openDuplicateOutput(output1, output2); 1377} 1378 1379static int aps_close_output(void *service, audio_io_handle_t output) 1380{ 1381 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1382 if (af == NULL) 1383 return PERMISSION_DENIED; 1384 1385 return af->closeOutput(output); 1386} 1387 1388static int aps_suspend_output(void *service, audio_io_handle_t output) 1389{ 1390 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1391 if (af == NULL) { 1392 LOGW("%s: could not get AudioFlinger", __func__); 1393 return PERMISSION_DENIED; 1394 } 1395 1396 return af->suspendOutput(output); 1397} 1398 1399static int aps_restore_output(void *service, audio_io_handle_t output) 1400{ 1401 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1402 if (af == NULL) { 1403 LOGW("%s: could not get AudioFlinger", __func__); 1404 return PERMISSION_DENIED; 1405 } 1406 1407 return af->restoreOutput(output); 1408} 1409 1410static audio_io_handle_t aps_open_input(void *service, 1411 uint32_t *pDevices, 1412 uint32_t *pSamplingRate, 1413 uint32_t *pFormat, 1414 uint32_t *pChannels, 1415 uint32_t acoustics) 1416{ 1417 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1418 if (af == NULL) { 1419 LOGW("%s: could not get AudioFlinger", __func__); 1420 return 0; 1421 } 1422 1423 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels, 1424 acoustics); 1425} 1426 1427static int aps_close_input(void *service, audio_io_handle_t input) 1428{ 1429 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1430 if (af == NULL) 1431 return PERMISSION_DENIED; 1432 1433 return af->closeInput(input); 1434} 1435 1436static int aps_set_stream_output(void *service, audio_stream_type_t stream, 1437 audio_io_handle_t output) 1438{ 1439 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1440 if (af == NULL) 1441 return PERMISSION_DENIED; 1442 1443 return af->setStreamOutput(stream, output); 1444} 1445 1446static int aps_move_effects(void *service, int session, 1447 audio_io_handle_t src_output, 1448 audio_io_handle_t dst_output) 1449{ 1450 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1451 if (af == NULL) 1452 return PERMISSION_DENIED; 1453 1454 return af->moveEffects(session, (int)src_output, (int)dst_output); 1455} 1456 1457static char * aps_get_parameters(void *service, audio_io_handle_t io_handle, 1458 const char *keys) 1459{ 1460 String8 result = AudioSystem::getParameters(io_handle, String8(keys)); 1461 return strdup(result.string()); 1462} 1463 1464static void aps_set_parameters(void *service, audio_io_handle_t io_handle, 1465 const char *kv_pairs, int delay_ms) 1466{ 1467 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1468 1469 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms); 1470} 1471 1472static int aps_set_stream_volume(void *service, audio_stream_type_t stream, 1473 float volume, audio_io_handle_t output, 1474 int delay_ms) 1475{ 1476 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1477 1478 return audioPolicyService->setStreamVolume(stream, volume, output, 1479 delay_ms); 1480} 1481 1482static int aps_start_tone(void *service, audio_policy_tone_t tone, 1483 audio_stream_type_t stream) 1484{ 1485 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1486 1487 return audioPolicyService->startTone(tone, stream); 1488} 1489 1490static int aps_stop_tone(void *service) 1491{ 1492 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1493 1494 return audioPolicyService->stopTone(); 1495} 1496 1497static int aps_set_voice_volume(void *service, float volume, int delay_ms) 1498{ 1499 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1500 1501 return audioPolicyService->setVoiceVolume(volume, delay_ms); 1502} 1503 1504}; // extern "C" 1505 1506namespace { 1507 struct audio_policy_service_ops aps_ops = { 1508 open_output : aps_open_output, 1509 open_duplicate_output : aps_open_dup_output, 1510 close_output : aps_close_output, 1511 suspend_output : aps_suspend_output, 1512 restore_output : aps_restore_output, 1513 open_input : aps_open_input, 1514 close_input : aps_close_input, 1515 set_stream_volume : aps_set_stream_volume, 1516 set_stream_output : aps_set_stream_output, 1517 set_parameters : aps_set_parameters, 1518 get_parameters : aps_get_parameters, 1519 start_tone : aps_start_tone, 1520 stop_tone : aps_stop_tone, 1521 set_voice_volume : aps_set_voice_volume, 1522 move_effects : aps_move_effects, 1523 }; 1524}; // namespace <unnamed> 1525 1526}; // namespace android 1527