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