AudioPolicyService.cpp revision db7c079f284f6e91266f6653ae0ec198b1c5006e
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 491status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) 492{ 493 if (mpAudioPolicy == NULL) { 494 return NO_INIT; 495 } 496 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled); 497} 498 499bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const 500{ 501 if (mpAudioPolicy == NULL) { 502 return 0; 503 } 504 Mutex::Autolock _l(mLock); 505 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs); 506} 507 508status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession, 509 effect_descriptor_t *descriptors, 510 uint32_t *count) 511{ 512 513 if (mpAudioPolicy == NULL) { 514 *count = 0; 515 return NO_INIT; 516 } 517 Mutex::Autolock _l(mLock); 518 status_t status = NO_ERROR; 519 520 size_t index; 521 for (index = 0; index < mInputs.size(); index++) { 522 if (mInputs.valueAt(index)->mSessionId == audioSession) { 523 break; 524 } 525 } 526 if (index == mInputs.size()) { 527 *count = 0; 528 return BAD_VALUE; 529 } 530 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects; 531 532 for (size_t i = 0; i < effects.size(); i++) { 533 effect_descriptor_t desc = effects[i]->descriptor(); 534 if (i < *count) { 535 memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t)); 536 } 537 } 538 if (effects.size() > *count) { 539 status = NO_MEMORY; 540 } 541 *count = effects.size(); 542 return status; 543} 544 545void AudioPolicyService::binderDied(const wp<IBinder>& who) { 546 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), 547 IPCThreadState::self()->getCallingPid()); 548} 549 550static bool tryLock(Mutex& mutex) 551{ 552 bool locked = false; 553 for (int i = 0; i < kDumpLockRetries; ++i) { 554 if (mutex.tryLock() == NO_ERROR) { 555 locked = true; 556 break; 557 } 558 usleep(kDumpLockSleep); 559 } 560 return locked; 561} 562 563status_t AudioPolicyService::dumpInternals(int fd) 564{ 565 const size_t SIZE = 256; 566 char buffer[SIZE]; 567 String8 result; 568 569 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); 570 result.append(buffer); 571 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); 572 result.append(buffer); 573 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); 574 result.append(buffer); 575 576 write(fd, result.string(), result.size()); 577 return NO_ERROR; 578} 579 580status_t AudioPolicyService::dump(int fd, const Vector<String16>& args) 581{ 582 if (checkCallingPermission(String16("android.permission.DUMP")) == false) { 583 dumpPermissionDenial(fd); 584 } else { 585 bool locked = tryLock(mLock); 586 if (!locked) { 587 String8 result(kDeadlockedString); 588 write(fd, result.string(), result.size()); 589 } 590 591 dumpInternals(fd); 592 if (mAudioCommandThread != NULL) { 593 mAudioCommandThread->dump(fd); 594 } 595 if (mTonePlaybackThread != NULL) { 596 mTonePlaybackThread->dump(fd); 597 } 598 599 if (mpAudioPolicy) { 600 mpAudioPolicy->dump(mpAudioPolicy, fd); 601 } 602 603 if (locked) mLock.unlock(); 604 } 605 return NO_ERROR; 606} 607 608status_t AudioPolicyService::dumpPermissionDenial(int fd) 609{ 610 const size_t SIZE = 256; 611 char buffer[SIZE]; 612 String8 result; 613 snprintf(buffer, SIZE, "Permission Denial: " 614 "can't dump AudioPolicyService from pid=%d, uid=%d\n", 615 IPCThreadState::self()->getCallingPid(), 616 IPCThreadState::self()->getCallingUid()); 617 result.append(buffer); 618 write(fd, result.string(), result.size()); 619 return NO_ERROR; 620} 621 622void AudioPolicyService::setPreProcessorEnabled(InputDesc *inputDesc, bool enabled) 623{ 624 Vector<sp<AudioEffect> > fxVector = inputDesc->mEffects; 625 for (size_t i = 0; i < fxVector.size(); i++) { 626 sp<AudioEffect> fx = fxVector.itemAt(i); 627 fx->setEnabled(enabled); 628 } 629} 630 631status_t AudioPolicyService::onTransact( 632 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 633{ 634 return BnAudioPolicyService::onTransact(code, data, reply, flags); 635} 636 637 638// ----------- AudioPolicyService::AudioCommandThread implementation ---------- 639 640AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) 641 : Thread(false), mName(name) 642{ 643 mpToneGenerator = NULL; 644} 645 646 647AudioPolicyService::AudioCommandThread::~AudioCommandThread() 648{ 649 if (mName != "" && !mAudioCommands.isEmpty()) { 650 release_wake_lock(mName.string()); 651 } 652 mAudioCommands.clear(); 653 if (mpToneGenerator != NULL) delete mpToneGenerator; 654} 655 656void AudioPolicyService::AudioCommandThread::onFirstRef() 657{ 658 if (mName != "") { 659 run(mName.string(), ANDROID_PRIORITY_AUDIO); 660 } else { 661 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO); 662 } 663} 664 665bool AudioPolicyService::AudioCommandThread::threadLoop() 666{ 667 nsecs_t waitTime = INT64_MAX; 668 669 mLock.lock(); 670 while (!exitPending()) 671 { 672 while(!mAudioCommands.isEmpty()) { 673 nsecs_t curTime = systemTime(); 674 // commands are sorted by increasing time stamp: execute them from index 0 and up 675 if (mAudioCommands[0]->mTime <= curTime) { 676 AudioCommand *command = mAudioCommands[0]; 677 mAudioCommands.removeAt(0); 678 mLastCommand = *command; 679 680 switch (command->mCommand) { 681 case START_TONE: { 682 mLock.unlock(); 683 ToneData *data = (ToneData *)command->mParam; 684 LOGV("AudioCommandThread() processing start tone %d on stream %d", 685 data->mType, data->mStream); 686 if (mpToneGenerator != NULL) 687 delete mpToneGenerator; 688 mpToneGenerator = new ToneGenerator(data->mStream, 1.0); 689 mpToneGenerator->startTone(data->mType); 690 delete data; 691 mLock.lock(); 692 }break; 693 case STOP_TONE: { 694 mLock.unlock(); 695 LOGV("AudioCommandThread() processing stop tone"); 696 if (mpToneGenerator != NULL) { 697 mpToneGenerator->stopTone(); 698 delete mpToneGenerator; 699 mpToneGenerator = NULL; 700 } 701 mLock.lock(); 702 }break; 703 case SET_VOLUME: { 704 VolumeData *data = (VolumeData *)command->mParam; 705 LOGV("AudioCommandThread() processing set volume stream %d, \ 706 volume %f, output %d", data->mStream, data->mVolume, data->mIO); 707 command->mStatus = AudioSystem::setStreamVolume(data->mStream, 708 data->mVolume, 709 data->mIO); 710 if (command->mWaitStatus) { 711 command->mCond.signal(); 712 mWaitWorkCV.wait(mLock); 713 } 714 delete data; 715 }break; 716 case SET_PARAMETERS: { 717 ParametersData *data = (ParametersData *)command->mParam; 718 LOGV("AudioCommandThread() processing set parameters string %s, io %d", 719 data->mKeyValuePairs.string(), data->mIO); 720 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); 721 if (command->mWaitStatus) { 722 command->mCond.signal(); 723 mWaitWorkCV.wait(mLock); 724 } 725 delete data; 726 }break; 727 case SET_VOICE_VOLUME: { 728 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam; 729 LOGV("AudioCommandThread() processing set voice volume volume %f", 730 data->mVolume); 731 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); 732 if (command->mWaitStatus) { 733 command->mCond.signal(); 734 mWaitWorkCV.wait(mLock); 735 } 736 delete data; 737 }break; 738 default: 739 LOGW("AudioCommandThread() unknown command %d", command->mCommand); 740 } 741 delete command; 742 waitTime = INT64_MAX; 743 } else { 744 waitTime = mAudioCommands[0]->mTime - curTime; 745 break; 746 } 747 } 748 // release delayed commands wake lock 749 if (mName != "" && mAudioCommands.isEmpty()) { 750 release_wake_lock(mName.string()); 751 } 752 LOGV("AudioCommandThread() going to sleep"); 753 mWaitWorkCV.waitRelative(mLock, waitTime); 754 LOGV("AudioCommandThread() waking up"); 755 } 756 mLock.unlock(); 757 return false; 758} 759 760status_t AudioPolicyService::AudioCommandThread::dump(int fd) 761{ 762 const size_t SIZE = 256; 763 char buffer[SIZE]; 764 String8 result; 765 766 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); 767 result.append(buffer); 768 write(fd, result.string(), result.size()); 769 770 bool locked = tryLock(mLock); 771 if (!locked) { 772 String8 result2(kCmdDeadlockedString); 773 write(fd, result2.string(), result2.size()); 774 } 775 776 snprintf(buffer, SIZE, "- Commands:\n"); 777 result = String8(buffer); 778 result.append(" Command Time Wait pParam\n"); 779 for (int i = 0; i < (int)mAudioCommands.size(); i++) { 780 mAudioCommands[i]->dump(buffer, SIZE); 781 result.append(buffer); 782 } 783 result.append(" Last Command\n"); 784 mLastCommand.dump(buffer, SIZE); 785 result.append(buffer); 786 787 write(fd, result.string(), result.size()); 788 789 if (locked) mLock.unlock(); 790 791 return NO_ERROR; 792} 793 794void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream) 795{ 796 AudioCommand *command = new AudioCommand(); 797 command->mCommand = START_TONE; 798 ToneData *data = new ToneData(); 799 data->mType = type; 800 data->mStream = stream; 801 command->mParam = (void *)data; 802 command->mWaitStatus = false; 803 Mutex::Autolock _l(mLock); 804 insertCommand_l(command); 805 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); 806 mWaitWorkCV.signal(); 807} 808 809void AudioPolicyService::AudioCommandThread::stopToneCommand() 810{ 811 AudioCommand *command = new AudioCommand(); 812 command->mCommand = STOP_TONE; 813 command->mParam = NULL; 814 command->mWaitStatus = false; 815 Mutex::Autolock _l(mLock); 816 insertCommand_l(command); 817 LOGV("AudioCommandThread() adding tone stop"); 818 mWaitWorkCV.signal(); 819} 820 821status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, 822 float volume, 823 int output, 824 int delayMs) 825{ 826 status_t status = NO_ERROR; 827 828 AudioCommand *command = new AudioCommand(); 829 command->mCommand = SET_VOLUME; 830 VolumeData *data = new VolumeData(); 831 data->mStream = stream; 832 data->mVolume = volume; 833 data->mIO = output; 834 command->mParam = data; 835 if (delayMs == 0) { 836 command->mWaitStatus = true; 837 } else { 838 command->mWaitStatus = false; 839 } 840 Mutex::Autolock _l(mLock); 841 insertCommand_l(command, delayMs); 842 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", 843 stream, volume, output); 844 mWaitWorkCV.signal(); 845 if (command->mWaitStatus) { 846 command->mCond.wait(mLock); 847 status = command->mStatus; 848 mWaitWorkCV.signal(); 849 } 850 return status; 851} 852 853status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle, 854 const char *keyValuePairs, 855 int delayMs) 856{ 857 status_t status = NO_ERROR; 858 859 AudioCommand *command = new AudioCommand(); 860 command->mCommand = SET_PARAMETERS; 861 ParametersData *data = new ParametersData(); 862 data->mIO = ioHandle; 863 data->mKeyValuePairs = String8(keyValuePairs); 864 command->mParam = data; 865 if (delayMs == 0) { 866 command->mWaitStatus = true; 867 } else { 868 command->mWaitStatus = false; 869 } 870 Mutex::Autolock _l(mLock); 871 insertCommand_l(command, delayMs); 872 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", 873 keyValuePairs, ioHandle, delayMs); 874 mWaitWorkCV.signal(); 875 if (command->mWaitStatus) { 876 command->mCond.wait(mLock); 877 status = command->mStatus; 878 mWaitWorkCV.signal(); 879 } 880 return status; 881} 882 883status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) 884{ 885 status_t status = NO_ERROR; 886 887 AudioCommand *command = new AudioCommand(); 888 command->mCommand = SET_VOICE_VOLUME; 889 VoiceVolumeData *data = new VoiceVolumeData(); 890 data->mVolume = volume; 891 command->mParam = data; 892 if (delayMs == 0) { 893 command->mWaitStatus = true; 894 } else { 895 command->mWaitStatus = false; 896 } 897 Mutex::Autolock _l(mLock); 898 insertCommand_l(command, delayMs); 899 LOGV("AudioCommandThread() adding set voice volume volume %f", volume); 900 mWaitWorkCV.signal(); 901 if (command->mWaitStatus) { 902 command->mCond.wait(mLock); 903 status = command->mStatus; 904 mWaitWorkCV.signal(); 905 } 906 return status; 907} 908 909// insertCommand_l() must be called with mLock held 910void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs) 911{ 912 ssize_t i; 913 Vector <AudioCommand *> removedCommands; 914 915 command->mTime = systemTime() + milliseconds(delayMs); 916 917 // acquire wake lock to make sure delayed commands are processed 918 if (mName != "" && mAudioCommands.isEmpty()) { 919 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); 920 } 921 922 // check same pending commands with later time stamps and eliminate them 923 for (i = mAudioCommands.size()-1; i >= 0; i--) { 924 AudioCommand *command2 = mAudioCommands[i]; 925 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands 926 if (command2->mTime <= command->mTime) break; 927 if (command2->mCommand != command->mCommand) continue; 928 929 switch (command->mCommand) { 930 case SET_PARAMETERS: { 931 ParametersData *data = (ParametersData *)command->mParam; 932 ParametersData *data2 = (ParametersData *)command2->mParam; 933 if (data->mIO != data2->mIO) break; 934 LOGV("Comparing parameter command %s to new command %s", 935 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); 936 AudioParameter param = AudioParameter(data->mKeyValuePairs); 937 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); 938 for (size_t j = 0; j < param.size(); j++) { 939 String8 key; 940 String8 value; 941 param.getAt(j, key, value); 942 for (size_t k = 0; k < param2.size(); k++) { 943 String8 key2; 944 String8 value2; 945 param2.getAt(k, key2, value2); 946 if (key2 == key) { 947 param2.remove(key2); 948 LOGV("Filtering out parameter %s", key2.string()); 949 break; 950 } 951 } 952 } 953 // if all keys have been filtered out, remove the command. 954 // otherwise, update the key value pairs 955 if (param2.size() == 0) { 956 removedCommands.add(command2); 957 } else { 958 data2->mKeyValuePairs = param2.toString(); 959 } 960 } break; 961 962 case SET_VOLUME: { 963 VolumeData *data = (VolumeData *)command->mParam; 964 VolumeData *data2 = (VolumeData *)command2->mParam; 965 if (data->mIO != data2->mIO) break; 966 if (data->mStream != data2->mStream) break; 967 LOGV("Filtering out volume command on output %d for stream %d", 968 data->mIO, data->mStream); 969 removedCommands.add(command2); 970 } break; 971 case START_TONE: 972 case STOP_TONE: 973 default: 974 break; 975 } 976 } 977 978 // remove filtered commands 979 for (size_t j = 0; j < removedCommands.size(); j++) { 980 // removed commands always have time stamps greater than current command 981 for (size_t k = i + 1; k < mAudioCommands.size(); k++) { 982 if (mAudioCommands[k] == removedCommands[j]) { 983 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand); 984 mAudioCommands.removeAt(k); 985 break; 986 } 987 } 988 } 989 removedCommands.clear(); 990 991 // insert command at the right place according to its time stamp 992 LOGV("inserting command: %d at index %d, num commands %d", 993 command->mCommand, (int)i+1, mAudioCommands.size()); 994 mAudioCommands.insertAt(command, i + 1); 995} 996 997void AudioPolicyService::AudioCommandThread::exit() 998{ 999 LOGV("AudioCommandThread::exit"); 1000 { 1001 AutoMutex _l(mLock); 1002 requestExit(); 1003 mWaitWorkCV.signal(); 1004 } 1005 requestExitAndWait(); 1006} 1007 1008void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) 1009{ 1010 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", 1011 mCommand, 1012 (int)ns2s(mTime), 1013 (int)ns2ms(mTime)%1000, 1014 mWaitStatus, 1015 mParam); 1016} 1017 1018/******* helpers for the service_ops callbacks defined below *********/ 1019void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, 1020 const char *keyValuePairs, 1021 int delayMs) 1022{ 1023 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs, 1024 delayMs); 1025} 1026 1027int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, 1028 float volume, 1029 audio_io_handle_t output, 1030 int delayMs) 1031{ 1032 return (int)mAudioCommandThread->volumeCommand((int)stream, volume, 1033 (int)output, delayMs); 1034} 1035 1036int AudioPolicyService::startTone(audio_policy_tone_t tone, 1037 audio_stream_type_t stream) 1038{ 1039 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) 1040 LOGE("startTone: illegal tone requested (%d)", tone); 1041 if (stream != AUDIO_STREAM_VOICE_CALL) 1042 LOGE("startTone: illegal stream (%d) requested for tone %d", stream, 1043 tone); 1044 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, 1045 AUDIO_STREAM_VOICE_CALL); 1046 return 0; 1047} 1048 1049int AudioPolicyService::stopTone() 1050{ 1051 mTonePlaybackThread->stopToneCommand(); 1052 return 0; 1053} 1054 1055int AudioPolicyService::setVoiceVolume(float volume, int delayMs) 1056{ 1057 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); 1058} 1059 1060// ---------------------------------------------------------------------------- 1061// Audio pre-processing configuration 1062// ---------------------------------------------------------------------------- 1063 1064const char *AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = { 1065 MIC_SRC_TAG, 1066 VOICE_UL_SRC_TAG, 1067 VOICE_DL_SRC_TAG, 1068 VOICE_CALL_SRC_TAG, 1069 CAMCORDER_SRC_TAG, 1070 VOICE_REC_SRC_TAG, 1071 VOICE_COMM_SRC_TAG 1072}; 1073 1074// returns the audio_source_t enum corresponding to the input source name or 1075// AUDIO_SOURCE_CNT is no match found 1076audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name) 1077{ 1078 int i; 1079 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) { 1080 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) { 1081 LOGV("inputSourceNameToEnum found source %s %d", name, i); 1082 break; 1083 } 1084 } 1085 return (audio_source_t)i; 1086} 1087 1088size_t AudioPolicyService::growParamSize(char *param, 1089 size_t size, 1090 size_t *curSize, 1091 size_t *totSize) 1092{ 1093 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int) 1094 size_t pos = ((*curSize - 1 ) / size + 1) * size; 1095 1096 if (pos + size > *totSize) { 1097 while (pos + size > *totSize) { 1098 *totSize += ((*totSize + 7) / 8) * 4; 1099 } 1100 param = (char *)realloc(param, *totSize); 1101 } 1102 *curSize = pos + size; 1103 return pos; 1104} 1105 1106size_t AudioPolicyService::readParamValue(cnode *node, 1107 char *param, 1108 size_t *curSize, 1109 size_t *totSize) 1110{ 1111 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) { 1112 size_t pos = growParamSize(param, sizeof(short), curSize, totSize); 1113 *(short *)((char *)param + pos) = (short)atoi(node->value); 1114 LOGV("readParamValue() reading short %d", *(short *)((char *)param + pos)); 1115 return sizeof(short); 1116 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) { 1117 size_t pos = growParamSize(param, sizeof(int), curSize, totSize); 1118 *(int *)((char *)param + pos) = atoi(node->value); 1119 LOGV("readParamValue() reading int %d", *(int *)((char *)param + pos)); 1120 return sizeof(int); 1121 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) { 1122 size_t pos = growParamSize(param, sizeof(float), curSize, totSize); 1123 *(float *)((char *)param + pos) = (float)atof(node->value); 1124 LOGV("readParamValue() reading float %f",*(float *)((char *)param + pos)); 1125 return sizeof(float); 1126 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) { 1127 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize); 1128 if (strncmp(node->value, "false", strlen("false") + 1) == 0) { 1129 *(bool *)((char *)param + pos) = false; 1130 } else { 1131 *(bool *)((char *)param + pos) = true; 1132 } 1133 LOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false"); 1134 return sizeof(bool); 1135 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) { 1136 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX); 1137 if (*curSize + len + 1 > *totSize) { 1138 *totSize = *curSize + len + 1; 1139 param = (char *)realloc(param, *totSize); 1140 } 1141 strncpy(param + *curSize, node->value, len); 1142 *curSize += len; 1143 param[*curSize] = '\0'; 1144 LOGV("readParamValue() reading string %s", param + *curSize - len); 1145 return len; 1146 } 1147 LOGW("readParamValue() unknown param type %s", node->name); 1148 return 0; 1149} 1150 1151effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root) 1152{ 1153 cnode *param; 1154 cnode *value; 1155 size_t curSize = sizeof(effect_param_t); 1156 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int); 1157 effect_param_t *fx_param = (effect_param_t *)malloc(totSize); 1158 1159 param = config_find(root, PARAM_TAG); 1160 value = config_find(root, VALUE_TAG); 1161 if (param == NULL && value == NULL) { 1162 // try to parse simple parameter form {int int} 1163 param = root->first_child; 1164 if (param) { 1165 // Note: that a pair of random strings is read as 0 0 1166 int *ptr = (int *)fx_param->data; 1167 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t)); 1168 LOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2); 1169 *ptr++ = atoi(param->name); 1170 *ptr = atoi(param->value); 1171 fx_param->psize = sizeof(int); 1172 fx_param->vsize = sizeof(int); 1173 return fx_param; 1174 } 1175 } 1176 if (param == NULL || value == NULL) { 1177 LOGW("loadEffectParameter() invalid parameter description %s", root->name); 1178 goto error; 1179 } 1180 1181 fx_param->psize = 0; 1182 param = param->first_child; 1183 while (param) { 1184 LOGV("loadEffectParameter() reading param of type %s", param->name); 1185 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize); 1186 if (size == 0) { 1187 goto error; 1188 } 1189 fx_param->psize += size; 1190 param = param->next; 1191 } 1192 1193 // align start of value field on 32 bit boundary 1194 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int); 1195 1196 fx_param->vsize = 0; 1197 value = value->first_child; 1198 while (value) { 1199 LOGV("loadEffectParameter() reading value of type %s", value->name); 1200 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize); 1201 if (size == 0) { 1202 goto error; 1203 } 1204 fx_param->vsize += size; 1205 value = value->next; 1206 } 1207 1208 return fx_param; 1209 1210error: 1211 delete fx_param; 1212 return NULL; 1213} 1214 1215void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params) 1216{ 1217 cnode *node = root->first_child; 1218 while (node) { 1219 LOGV("loadEffectParameters() loading param %s", node->name); 1220 effect_param_t *param = loadEffectParameter(node); 1221 if (param == NULL) { 1222 node = node->next; 1223 continue; 1224 } 1225 params.add(param); 1226 node = node->next; 1227 } 1228} 1229 1230AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource( 1231 cnode *root, 1232 const Vector <EffectDesc *>& effects) 1233{ 1234 cnode *node = root->first_child; 1235 if (node == NULL) { 1236 LOGW("loadInputSource() empty element %s", root->name); 1237 return NULL; 1238 } 1239 InputSourceDesc *source = new InputSourceDesc(); 1240 while (node) { 1241 size_t i; 1242 for (i = 0; i < effects.size(); i++) { 1243 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) { 1244 LOGV("loadInputSource() found effect %s in list", node->name); 1245 break; 1246 } 1247 } 1248 if (i == effects.size()) { 1249 LOGV("loadInputSource() effect %s not in list", node->name); 1250 node = node->next; 1251 continue; 1252 } 1253 EffectDesc *effect = new EffectDesc(*effects[i]); 1254 loadEffectParameters(node, effect->mParams); 1255 LOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow); 1256 source->mEffects.add(effect); 1257 node = node->next; 1258 } 1259 if (source->mEffects.size() == 0) { 1260 LOGW("loadInputSource() no valid effects found in source %s", root->name); 1261 delete source; 1262 return NULL; 1263 } 1264 return source; 1265} 1266 1267status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects) 1268{ 1269 cnode *node = config_find(root, PREPROCESSING_TAG); 1270 if (node == NULL) { 1271 return -ENOENT; 1272 } 1273 node = node->first_child; 1274 while (node) { 1275 audio_source_t source = inputSourceNameToEnum(node->name); 1276 if (source == AUDIO_SOURCE_CNT) { 1277 LOGW("loadInputSources() invalid input source %s", node->name); 1278 node = node->next; 1279 continue; 1280 } 1281 LOGV("loadInputSources() loading input source %s", node->name); 1282 InputSourceDesc *desc = loadInputSource(node, effects); 1283 if (desc == NULL) { 1284 node = node->next; 1285 continue; 1286 } 1287 mInputSources.add(source, desc); 1288 node = node->next; 1289 } 1290 return NO_ERROR; 1291} 1292 1293AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root) 1294{ 1295 cnode *node = config_find(root, UUID_TAG); 1296 if (node == NULL) { 1297 return NULL; 1298 } 1299 effect_uuid_t uuid; 1300 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) { 1301 LOGW("loadEffect() invalid uuid %s", node->value); 1302 return NULL; 1303 } 1304 EffectDesc *effect = new EffectDesc(); 1305 effect->mName = strdup(root->name); 1306 memcpy(&effect->mUuid, &uuid, sizeof(effect_uuid_t)); 1307 1308 return effect; 1309} 1310 1311status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects) 1312{ 1313 cnode *node = config_find(root, EFFECTS_TAG); 1314 if (node == NULL) { 1315 return -ENOENT; 1316 } 1317 node = node->first_child; 1318 while (node) { 1319 LOGV("loadEffects() loading effect %s", node->name); 1320 EffectDesc *effect = loadEffect(node); 1321 if (effect == NULL) { 1322 node = node->next; 1323 continue; 1324 } 1325 effects.add(effect); 1326 node = node->next; 1327 } 1328 return NO_ERROR; 1329} 1330 1331status_t AudioPolicyService::loadPreProcessorConfig(const char *path) 1332{ 1333 cnode *root; 1334 char *data; 1335 1336 data = (char *)load_file(path, NULL); 1337 if (data == NULL) { 1338 return -ENODEV; 1339 } 1340 root = config_node("", ""); 1341 config_load(root, data); 1342 1343 Vector <EffectDesc *> effects; 1344 loadEffects(root, effects); 1345 loadInputSources(root, effects); 1346 1347 config_free(root); 1348 free(root); 1349 free(data); 1350 1351 return NO_ERROR; 1352} 1353 1354/* implementation of the interface to the policy manager */ 1355extern "C" { 1356 1357static audio_io_handle_t aps_open_output(void *service, 1358 uint32_t *pDevices, 1359 uint32_t *pSamplingRate, 1360 uint32_t *pFormat, 1361 uint32_t *pChannels, 1362 uint32_t *pLatencyMs, 1363 audio_policy_output_flags_t flags) 1364{ 1365 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1366 if (af == NULL) { 1367 LOGW("%s: could not get AudioFlinger", __func__); 1368 return 0; 1369 } 1370 1371 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels, 1372 pLatencyMs, flags); 1373} 1374 1375static audio_io_handle_t aps_open_dup_output(void *service, 1376 audio_io_handle_t output1, 1377 audio_io_handle_t output2) 1378{ 1379 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1380 if (af == NULL) { 1381 LOGW("%s: could not get AudioFlinger", __func__); 1382 return 0; 1383 } 1384 return af->openDuplicateOutput(output1, output2); 1385} 1386 1387static int aps_close_output(void *service, audio_io_handle_t output) 1388{ 1389 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1390 if (af == NULL) 1391 return PERMISSION_DENIED; 1392 1393 return af->closeOutput(output); 1394} 1395 1396static int aps_suspend_output(void *service, audio_io_handle_t output) 1397{ 1398 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1399 if (af == NULL) { 1400 LOGW("%s: could not get AudioFlinger", __func__); 1401 return PERMISSION_DENIED; 1402 } 1403 1404 return af->suspendOutput(output); 1405} 1406 1407static int aps_restore_output(void *service, audio_io_handle_t output) 1408{ 1409 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1410 if (af == NULL) { 1411 LOGW("%s: could not get AudioFlinger", __func__); 1412 return PERMISSION_DENIED; 1413 } 1414 1415 return af->restoreOutput(output); 1416} 1417 1418static audio_io_handle_t aps_open_input(void *service, 1419 uint32_t *pDevices, 1420 uint32_t *pSamplingRate, 1421 uint32_t *pFormat, 1422 uint32_t *pChannels, 1423 uint32_t acoustics) 1424{ 1425 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1426 if (af == NULL) { 1427 LOGW("%s: could not get AudioFlinger", __func__); 1428 return 0; 1429 } 1430 1431 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels, 1432 acoustics); 1433} 1434 1435static int aps_close_input(void *service, audio_io_handle_t input) 1436{ 1437 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1438 if (af == NULL) 1439 return PERMISSION_DENIED; 1440 1441 return af->closeInput(input); 1442} 1443 1444static int aps_set_stream_output(void *service, audio_stream_type_t stream, 1445 audio_io_handle_t output) 1446{ 1447 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1448 if (af == NULL) 1449 return PERMISSION_DENIED; 1450 1451 return af->setStreamOutput(stream, output); 1452} 1453 1454static int aps_move_effects(void *service, int session, 1455 audio_io_handle_t src_output, 1456 audio_io_handle_t dst_output) 1457{ 1458 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); 1459 if (af == NULL) 1460 return PERMISSION_DENIED; 1461 1462 return af->moveEffects(session, (int)src_output, (int)dst_output); 1463} 1464 1465static char * aps_get_parameters(void *service, audio_io_handle_t io_handle, 1466 const char *keys) 1467{ 1468 String8 result = AudioSystem::getParameters(io_handle, String8(keys)); 1469 return strdup(result.string()); 1470} 1471 1472static void aps_set_parameters(void *service, audio_io_handle_t io_handle, 1473 const char *kv_pairs, int delay_ms) 1474{ 1475 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1476 1477 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms); 1478} 1479 1480static int aps_set_stream_volume(void *service, audio_stream_type_t stream, 1481 float volume, audio_io_handle_t output, 1482 int delay_ms) 1483{ 1484 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1485 1486 return audioPolicyService->setStreamVolume(stream, volume, output, 1487 delay_ms); 1488} 1489 1490static int aps_start_tone(void *service, audio_policy_tone_t tone, 1491 audio_stream_type_t stream) 1492{ 1493 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1494 1495 return audioPolicyService->startTone(tone, stream); 1496} 1497 1498static int aps_stop_tone(void *service) 1499{ 1500 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1501 1502 return audioPolicyService->stopTone(); 1503} 1504 1505static int aps_set_voice_volume(void *service, float volume, int delay_ms) 1506{ 1507 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; 1508 1509 return audioPolicyService->setVoiceVolume(volume, delay_ms); 1510} 1511 1512}; // extern "C" 1513 1514namespace { 1515 struct audio_policy_service_ops aps_ops = { 1516 open_output : aps_open_output, 1517 open_duplicate_output : aps_open_dup_output, 1518 close_output : aps_close_output, 1519 suspend_output : aps_suspend_output, 1520 restore_output : aps_restore_output, 1521 open_input : aps_open_input, 1522 close_input : aps_close_input, 1523 set_stream_volume : aps_set_stream_volume, 1524 set_stream_output : aps_set_stream_output, 1525 set_parameters : aps_set_parameters, 1526 get_parameters : aps_get_parameters, 1527 start_tone : aps_start_tone, 1528 stop_tone : aps_stop_tone, 1529 set_voice_volume : aps_set_voice_volume, 1530 move_effects : aps_move_effects, 1531 }; 1532}; // namespace <unnamed> 1533 1534}; // namespace android 1535