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