AudioSystem.cpp revision c2f1f07084818942352c6bbfb36af9b6b330eb4e
1/* 2 * Copyright (C) 2006-2007 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 "AudioSystem" 18//#define LOG_NDEBUG 0 19 20#include <utils/Log.h> 21#include <binder/IServiceManager.h> 22#include <media/AudioSystem.h> 23#include <media/IAudioPolicyService.h> 24#include <math.h> 25 26// ---------------------------------------------------------------------------- 27// the sim build doesn't have gettid 28 29#ifndef HAVE_GETTID 30# define gettid getpid 31#endif 32 33// ---------------------------------------------------------------------------- 34 35namespace android { 36 37// client singleton for AudioFlinger binder interface 38Mutex AudioSystem::gLock; 39sp<IAudioFlinger> AudioSystem::gAudioFlinger; 40sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; 41audio_error_callback AudioSystem::gAudioErrorCallback = NULL; 42// Cached values 43DefaultKeyedVector<int, audio_io_handle_t> AudioSystem::gStreamOutputMap(0); 44DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0); 45 46// Cached values for recording queries 47uint32_t AudioSystem::gPrevInSamplingRate = 16000; 48int AudioSystem::gPrevInFormat = AudioSystem::PCM_16_BIT; 49int AudioSystem::gPrevInChannelCount = 1; 50size_t AudioSystem::gInBuffSize = 0; 51 52 53// establish binder interface to AudioFlinger service 54const sp<IAudioFlinger>& AudioSystem::get_audio_flinger() 55{ 56 Mutex::Autolock _l(gLock); 57 if (gAudioFlinger.get() == 0) { 58 sp<IServiceManager> sm = defaultServiceManager(); 59 sp<IBinder> binder; 60 do { 61 binder = sm->getService(String16("media.audio_flinger")); 62 if (binder != 0) 63 break; 64 LOGW("AudioFlinger not published, waiting..."); 65 usleep(500000); // 0.5 s 66 } while(true); 67 if (gAudioFlingerClient == NULL) { 68 gAudioFlingerClient = new AudioFlingerClient(); 69 } else { 70 if (gAudioErrorCallback) { 71 gAudioErrorCallback(NO_ERROR); 72 } 73 } 74 binder->linkToDeath(gAudioFlingerClient); 75 gAudioFlinger = interface_cast<IAudioFlinger>(binder); 76 gAudioFlinger->registerClient(gAudioFlingerClient); 77 } 78 LOGE_IF(gAudioFlinger==0, "no AudioFlinger!?"); 79 80 return gAudioFlinger; 81} 82 83status_t AudioSystem::muteMicrophone(bool state) { 84 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 85 if (af == 0) return PERMISSION_DENIED; 86 return af->setMicMute(state); 87} 88 89status_t AudioSystem::isMicrophoneMuted(bool* state) { 90 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 91 if (af == 0) return PERMISSION_DENIED; 92 *state = af->getMicMute(); 93 return NO_ERROR; 94} 95 96status_t AudioSystem::setMasterVolume(float value) 97{ 98 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 99 if (af == 0) return PERMISSION_DENIED; 100 af->setMasterVolume(value); 101 return NO_ERROR; 102} 103 104status_t AudioSystem::setMasterMute(bool mute) 105{ 106 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 107 if (af == 0) return PERMISSION_DENIED; 108 af->setMasterMute(mute); 109 return NO_ERROR; 110} 111 112status_t AudioSystem::getMasterVolume(float* volume) 113{ 114 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 115 if (af == 0) return PERMISSION_DENIED; 116 *volume = af->masterVolume(); 117 return NO_ERROR; 118} 119 120status_t AudioSystem::getMasterMute(bool* mute) 121{ 122 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 123 if (af == 0) return PERMISSION_DENIED; 124 *mute = af->masterMute(); 125 return NO_ERROR; 126} 127 128status_t AudioSystem::setStreamVolume(int stream, float value, void *output) 129{ 130 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE; 131 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 132 if (af == 0) return PERMISSION_DENIED; 133 af->setStreamVolume(stream, value, output); 134 return NO_ERROR; 135} 136 137status_t AudioSystem::setStreamMute(int stream, bool mute) 138{ 139 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE; 140 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 141 if (af == 0) return PERMISSION_DENIED; 142 af->setStreamMute(stream, mute); 143 return NO_ERROR; 144} 145 146status_t AudioSystem::getStreamVolume(int stream, float* volume, void *output) 147{ 148 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE; 149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 150 if (af == 0) return PERMISSION_DENIED; 151 *volume = af->streamVolume(stream, output); 152 return NO_ERROR; 153} 154 155status_t AudioSystem::getStreamMute(int stream, bool* mute) 156{ 157 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE; 158 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 159 if (af == 0) return PERMISSION_DENIED; 160 *mute = af->streamMute(stream); 161 return NO_ERROR; 162} 163 164status_t AudioSystem::setMode(int mode) 165{ 166 if (mode >= NUM_MODES) return BAD_VALUE; 167 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 168 if (af == 0) return PERMISSION_DENIED; 169 return af->setMode(mode); 170} 171 172 173status_t AudioSystem::isMusicActive(bool* state) { 174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 175 if (af == 0) return PERMISSION_DENIED; 176 *state = af->isMusicActive(); 177 return NO_ERROR; 178} 179 180 181status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) { 182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 183 if (af == 0) return PERMISSION_DENIED; 184 return af->setParameters(ioHandle, keyValuePairs); 185} 186 187String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) { 188 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 189 String8 result = String8(""); 190 if (af == 0) return result; 191 192 result = af->getParameters(ioHandle, keys); 193 return result; 194} 195 196// convert volume steps to natural log scale 197 198// change this value to change volume scaling 199static const float dBPerStep = 0.5f; 200// shouldn't need to touch these 201static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; 202static const float dBConvertInverse = 1.0f / dBConvert; 203 204float AudioSystem::linearToLog(int volume) 205{ 206 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; 207 // LOGD("linearToLog(%d)=%f", volume, v); 208 // return v; 209 return volume ? exp(float(100 - volume) * dBConvert) : 0; 210} 211 212int AudioSystem::logToLinear(float volume) 213{ 214 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; 215 // LOGD("logTolinear(%d)=%f", v, volume); 216 // return v; 217 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; 218} 219 220status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType) 221{ 222 OutputDescriptor *outputDesc; 223 audio_io_handle_t output; 224 225 if (streamType == DEFAULT) { 226 streamType = MUSIC; 227 } 228 229 output = getOutput((stream_type)streamType); 230 if (output == 0) { 231 return PERMISSION_DENIED; 232 } 233 234 gLock.lock(); 235 outputDesc = AudioSystem::gOutputs.valueFor(output); 236 if (outputDesc == 0) { 237 LOGV("getOutputSamplingRate() no output descriptor for output %p in gOutputs", output); 238 gLock.unlock(); 239 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 240 if (af == 0) return PERMISSION_DENIED; 241 *samplingRate = af->sampleRate(output); 242 } else { 243 LOGV("getOutputSamplingRate() reading from output desc"); 244 *samplingRate = outputDesc->samplingRate; 245 gLock.unlock(); 246 } 247 248 LOGV("getOutputSamplingRate() streamType %d, output %p, sampling rate %d", streamType, output, *samplingRate); 249 250 return NO_ERROR; 251} 252 253status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType) 254{ 255 OutputDescriptor *outputDesc; 256 audio_io_handle_t output; 257 258 if (streamType == DEFAULT) { 259 streamType = MUSIC; 260 } 261 262 output = getOutput((stream_type)streamType); 263 if (output == 0) { 264 return PERMISSION_DENIED; 265 } 266 267 gLock.lock(); 268 outputDesc = AudioSystem::gOutputs.valueFor(output); 269 if (outputDesc == 0) { 270 gLock.unlock(); 271 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 272 if (af == 0) return PERMISSION_DENIED; 273 *frameCount = af->frameCount(output); 274 } else { 275 *frameCount = outputDesc->frameCount; 276 gLock.unlock(); 277 } 278 279 LOGV("getOutputFrameCount() streamType %d, output %p, frameCount %d", streamType, output, *frameCount); 280 281 return NO_ERROR; 282} 283 284status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType) 285{ 286 OutputDescriptor *outputDesc; 287 audio_io_handle_t output; 288 289 if (streamType == DEFAULT) { 290 streamType = MUSIC; 291 } 292 293 output = getOutput((stream_type)streamType); 294 if (output == 0) { 295 return PERMISSION_DENIED; 296 } 297 298 gLock.lock(); 299 outputDesc = AudioSystem::gOutputs.valueFor(output); 300 if (outputDesc == 0) { 301 gLock.unlock(); 302 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 303 if (af == 0) return PERMISSION_DENIED; 304 *latency = af->latency(output); 305 } else { 306 *latency = outputDesc->latency; 307 gLock.unlock(); 308 } 309 310 LOGV("getOutputLatency() streamType %d, output %p, latency %d", streamType, output, *latency); 311 312 return NO_ERROR; 313} 314 315status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount, 316 size_t* buffSize) 317{ 318 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values 319 if ((gInBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat) 320 || (channelCount != gPrevInChannelCount)) { 321 // save the request params 322 gPrevInSamplingRate = sampleRate; 323 gPrevInFormat = format; 324 gPrevInChannelCount = channelCount; 325 326 gInBuffSize = 0; 327 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 328 if (af == 0) { 329 return PERMISSION_DENIED; 330 } 331 gInBuffSize = af->getInputBufferSize(sampleRate, format, channelCount); 332 } 333 *buffSize = gInBuffSize; 334 335 return NO_ERROR; 336} 337 338// --------------------------------------------------------------------------- 339 340void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) { 341 Mutex::Autolock _l(AudioSystem::gLock); 342 343 AudioSystem::gAudioFlinger.clear(); 344 345 if (gAudioErrorCallback) { 346 gAudioErrorCallback(DEAD_OBJECT); 347 } 348 LOGW("AudioFlinger server died!"); 349} 350 351void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, void *param1, void *param2) { 352 LOGV("ioConfigChanged() event %d", event); 353 audio_io_handle_t ioHandle = (audio_io_handle_t)param1; 354 OutputDescriptor *desc; 355 uint32_t stream; 356 357 if (param1 == 0) return; 358 359 Mutex::Autolock _l(AudioSystem::gLock); 360 361 switch (event) { 362 case STREAM_CONFIG_CHANGED: 363 if (param2 == 0) break; 364 stream = *(uint32_t *)param2; 365 LOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %p", stream, ioHandle); 366 if (gStreamOutputMap.indexOfKey(stream) >= 0) { 367 gStreamOutputMap.replaceValueFor(stream, ioHandle); 368 } 369 break; 370 case OUTPUT_OPENED: { 371 if (gOutputs.indexOfKey(ioHandle) >= 0) { 372 LOGV("ioConfigChanged() opening already existing output! %p", ioHandle); 373 break; 374 } 375 if (param2 == 0) break; 376 desc = (OutputDescriptor *)param2; 377 378 OutputDescriptor *outputDesc = new OutputDescriptor(*desc); 379 gOutputs.add(ioHandle, outputDesc); 380 LOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d", 381 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency); 382 } break; 383 case OUTPUT_CLOSED: { 384 if (gOutputs.indexOfKey(ioHandle) < 0) { 385 LOGW("ioConfigChanged() closing unknow output! %p", ioHandle); 386 break; 387 } 388 LOGV("ioConfigChanged() output %p closed", ioHandle); 389 390 gOutputs.removeItem(ioHandle); 391 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) { 392 if (gStreamOutputMap.valueAt(i) == ioHandle) { 393 gStreamOutputMap.removeItemsAt(i); 394 } 395 } 396 } break; 397 398 case OUTPUT_CONFIG_CHANGED: { 399 int index = gOutputs.indexOfKey(ioHandle); 400 if (index < 0) { 401 LOGW("ioConfigChanged() modifying unknow output! %p", ioHandle); 402 break; 403 } 404 if (param2 == 0) break; 405 desc = (OutputDescriptor *)param2; 406 407 LOGV("ioConfigChanged() new config for output %p samplingRate %d, format %d channels %d frameCount %d latency %d", 408 ioHandle, desc->samplingRate, desc->format, 409 desc->channels, desc->frameCount, desc->latency); 410 OutputDescriptor *outputDesc = gOutputs.valueAt(index); 411 delete outputDesc; 412 outputDesc = new OutputDescriptor(*desc); 413 gOutputs.replaceValueFor(ioHandle, outputDesc); 414 } break; 415 case INPUT_OPENED: 416 case INPUT_CLOSED: 417 case INPUT_CONFIG_CHANGED: 418 break; 419 420 } 421} 422 423void AudioSystem::setErrorCallback(audio_error_callback cb) { 424 Mutex::Autolock _l(gLock); 425 gAudioErrorCallback = cb; 426} 427 428bool AudioSystem::routedToA2dpOutput(int streamType) { 429 switch(streamType) { 430 case MUSIC: 431 case VOICE_CALL: 432 case BLUETOOTH_SCO: 433 case SYSTEM: 434 return true; 435 default: 436 return false; 437 } 438} 439 440 441// client singleton for AudioPolicyService binder interface 442sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; 443sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; 444 445 446// establish binder interface to AudioFlinger service 447const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service() 448{ 449 gLock.lock(); 450 if (gAudioPolicyService.get() == 0) { 451 sp<IServiceManager> sm = defaultServiceManager(); 452 sp<IBinder> binder; 453 do { 454 binder = sm->getService(String16("media.audio_policy")); 455 if (binder != 0) 456 break; 457 LOGW("AudioPolicyService not published, waiting..."); 458 usleep(500000); // 0.5 s 459 } while(true); 460 if (gAudioPolicyServiceClient == NULL) { 461 gAudioPolicyServiceClient = new AudioPolicyServiceClient(); 462 } 463 binder->linkToDeath(gAudioPolicyServiceClient); 464 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); 465 gLock.unlock(); 466 } else { 467 gLock.unlock(); 468 } 469 return gAudioPolicyService; 470} 471 472status_t AudioSystem::setDeviceConnectionState(audio_devices device, 473 device_connection_state state, 474 const char *device_address) 475{ 476 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 477 if (aps == 0) return PERMISSION_DENIED; 478 479 return aps->setDeviceConnectionState(device, state, device_address); 480} 481 482AudioSystem::device_connection_state AudioSystem::getDeviceConnectionState(audio_devices device, 483 const char *device_address) 484{ 485 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 486 if (aps == 0) return DEVICE_STATE_UNAVAILABLE; 487 488 return aps->getDeviceConnectionState(device, device_address); 489} 490 491status_t AudioSystem::setPhoneState(int state) 492{ 493 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 494 if (aps == 0) return PERMISSION_DENIED; 495 496 return aps->setPhoneState(state); 497} 498 499status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask) 500{ 501 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 502 if (aps == 0) return PERMISSION_DENIED; 503 return aps->setRingerMode(mode, mask); 504} 505 506status_t AudioSystem::setForceUse(force_use usage, forced_config config) 507{ 508 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 509 if (aps == 0) return PERMISSION_DENIED; 510 return aps->setForceUse(usage, config); 511} 512 513AudioSystem::forced_config AudioSystem::getForceUse(force_use usage) 514{ 515 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 516 if (aps == 0) return FORCE_NONE; 517 return aps->getForceUse(usage); 518} 519 520 521audio_io_handle_t AudioSystem::getOutput(stream_type stream, 522 uint32_t samplingRate, 523 uint32_t format, 524 uint32_t channels, 525 output_flags flags) 526{ 527 audio_io_handle_t output = NULL; 528 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0) { 529 Mutex::Autolock _l(gLock); 530 output = AudioSystem::gStreamOutputMap.valueFor(stream); 531 LOGV_IF((output != NULL), "getOutput() read %p from cache for stream %d", output, stream); 532 } 533 if (output == NULL) { 534 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 535 if (aps == 0) return NULL; 536 output = aps->getOutput(stream, samplingRate, format, channels, flags); 537 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0) { 538 Mutex::Autolock _l(gLock); 539 AudioSystem::gStreamOutputMap.add(stream, output); 540 } 541 } 542 return output; 543} 544 545status_t AudioSystem::startOutput(audio_io_handle_t output, AudioSystem::stream_type stream) 546{ 547 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 548 if (aps == 0) return PERMISSION_DENIED; 549 return aps->startOutput(output, stream); 550} 551 552status_t AudioSystem::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream) 553{ 554 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 555 if (aps == 0) return PERMISSION_DENIED; 556 return aps->stopOutput(output, stream); 557} 558 559void AudioSystem::releaseOutput(audio_io_handle_t output) 560{ 561 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 562 if (aps == 0) return; 563 aps->releaseOutput(output); 564} 565 566audio_io_handle_t AudioSystem::getInput(int inputSource, 567 uint32_t samplingRate, 568 uint32_t format, 569 uint32_t channels, 570 audio_in_acoustics acoustics) 571{ 572 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 573 if (aps == 0) return NULL; 574 return aps->getInput(inputSource, samplingRate, format, channels, acoustics); 575} 576 577status_t AudioSystem::startInput(audio_io_handle_t input) 578{ 579 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 580 if (aps == 0) return PERMISSION_DENIED; 581 return aps->startInput(input); 582} 583 584status_t AudioSystem::stopInput(audio_io_handle_t input) 585{ 586 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 587 if (aps == 0) return PERMISSION_DENIED; 588 return aps->stopInput(input); 589} 590 591void AudioSystem::releaseInput(audio_io_handle_t input) 592{ 593 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 594 if (aps == 0) return; 595 aps->releaseInput(input); 596} 597 598status_t AudioSystem::initStreamVolume(stream_type stream, 599 int indexMin, 600 int indexMax) 601{ 602 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 603 if (aps == 0) return PERMISSION_DENIED; 604 return aps->initStreamVolume(stream, indexMin, indexMax); 605} 606 607status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index) 608{ 609 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 610 if (aps == 0) return PERMISSION_DENIED; 611 return aps->setStreamVolumeIndex(stream, index); 612} 613 614status_t AudioSystem::getStreamVolumeIndex(stream_type stream, int *index) 615{ 616 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 617 if (aps == 0) return PERMISSION_DENIED; 618 return aps->getStreamVolumeIndex(stream, index); 619} 620 621// --------------------------------------------------------------------------- 622 623void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) { 624 Mutex::Autolock _l(AudioSystem::gLock); 625 AudioSystem::gAudioPolicyService.clear(); 626 627 LOGW("AudioPolicyService server died!"); 628} 629 630// --------------------------------------------------------------------------- 631 632 633// use emulated popcount optimization 634// http://www.df.lth.se/~john_e/gems/gem002d.html 635uint32_t AudioSystem::popCount(uint32_t u) 636{ 637 u = ((u&0x55555555) + ((u>>1)&0x55555555)); 638 u = ((u&0x33333333) + ((u>>2)&0x33333333)); 639 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f)); 640 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff)); 641 u = ( u&0x0000ffff) + (u>>16); 642 return u; 643} 644 645bool AudioSystem::isOutputDevice(audio_devices device) 646{ 647 if ((popCount(device) == 1 ) && 648 ((device & ~AudioSystem::DEVICE_OUT_ALL) == 0)) { 649 return true; 650 } else { 651 return false; 652 } 653} 654 655bool AudioSystem::isInputDevice(audio_devices device) 656{ 657 if ((popCount(device) == 1 ) && 658 ((device & ~AudioSystem::DEVICE_IN_ALL) == 0)) { 659 return true; 660 } else { 661 return false; 662 } 663} 664 665bool AudioSystem::isA2dpDevice(audio_devices device) 666{ 667 if ((popCount(device) == 1 ) && 668 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP | 669 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 670 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER))) { 671 return true; 672 } else { 673 return false; 674 } 675} 676 677bool AudioSystem::isBluetoothScoDevice(audio_devices device) 678{ 679 if ((popCount(device) == 1 ) && 680 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_SCO | 681 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET | 682 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT))) { 683 return true; 684 } else { 685 return false; 686 } 687} 688 689bool AudioSystem::isLowVisibility(stream_type stream) 690{ 691 if (stream == AudioSystem::SYSTEM || stream == AudioSystem::NOTIFICATION) { 692 return true; 693 } else { 694 return false; 695 } 696} 697 698bool AudioSystem::isInputChannel(uint32_t channel) 699{ 700 if ((channel & ~AudioSystem::CHANNEL_IN_ALL) == 0) { 701 return true; 702 } else { 703 return false; 704 } 705} 706 707bool AudioSystem::isOutputChannel(uint32_t channel) 708{ 709 if ((channel & ~AudioSystem::CHANNEL_OUT_ALL) == 0) { 710 return true; 711 } else { 712 return false; 713 } 714} 715 716bool AudioSystem::isValidFormat(uint32_t format) 717{ 718 switch (format & MAIN_FORMAT_MASK) { 719 case PCM: 720 case MP3: 721 case AMR_NB: 722 case AMR_WB: 723 case AAC: 724 case HE_AAC_V1: 725 case HE_AAC_V2: 726 case VORBIS: 727 return true; 728 default: 729 return false; 730 } 731} 732 733bool AudioSystem::isLinearPCM(uint32_t format) 734{ 735 switch (format) { 736 case PCM_16_BIT: 737 case PCM_8_BIT: 738 return true; 739 default: 740 return false; 741 } 742} 743 744//------------------------- AudioParameter class implementation --------------- 745 746const char *AudioParameter::keyRouting = "routing"; 747const char *AudioParameter::keySamplingRate = "sampling_rate"; 748const char *AudioParameter::keyFormat = "format"; 749const char *AudioParameter::keyChannels = "channels"; 750const char *AudioParameter::keyFrameCount = "frame_count"; 751 752AudioParameter::AudioParameter(const String8& keyValuePairs) 753{ 754 char *str = new char[keyValuePairs.length()+1]; 755 mKeyValuePairs = keyValuePairs; 756 757 strcpy(str, keyValuePairs.string()); 758 char *pair = strtok(str, ";"); 759 while (pair != NULL) { 760 if (strlen(pair) != 0) { 761 size_t eqIdx = strcspn(pair, "="); 762 String8 key = String8(pair, eqIdx); 763 String8 value; 764 if (eqIdx == strlen(pair)) { 765 value = String8(""); 766 } else { 767 value = String8(pair + eqIdx + 1); 768 } 769 if (mParameters.indexOfKey(key) < 0) { 770 mParameters.add(key, value); 771 } else { 772 mParameters.replaceValueFor(key, value); 773 } 774 } else { 775 LOGV("AudioParameter() cstor empty key value pair"); 776 } 777 pair = strtok(NULL, ";"); 778 } 779 780 delete[] str; 781} 782 783AudioParameter::~AudioParameter() 784{ 785 mParameters.clear(); 786} 787 788String8 AudioParameter::toString() 789{ 790 String8 str = String8(""); 791 792 size_t size = mParameters.size(); 793 for (size_t i = 0; i < size; i++) { 794 str += mParameters.keyAt(i); 795 str += "="; 796 str += mParameters.valueAt(i); 797 if (i < (size - 1)) str += ";"; 798 } 799 return str; 800} 801 802status_t AudioParameter::add(const String8& key, const String8& value) 803{ 804 if (mParameters.indexOfKey(key) < 0) { 805 mParameters.add(key, value); 806 return NO_ERROR; 807 } else { 808 mParameters.replaceValueFor(key, value); 809 return ALREADY_EXISTS; 810 } 811} 812 813status_t AudioParameter::addInt(const String8& key, const int value) 814{ 815 char str[12]; 816 if (snprintf(str, 12, "%d", value) > 0) { 817 String8 str8 = String8(str); 818 return add(key, str8); 819 } else { 820 return BAD_VALUE; 821 } 822} 823 824status_t AudioParameter::addFloat(const String8& key, const float value) 825{ 826 char str[23]; 827 if (snprintf(str, 23, "%.10f", value) > 0) { 828 String8 str8 = String8(str); 829 return add(key, str8); 830 } else { 831 return BAD_VALUE; 832 } 833} 834 835status_t AudioParameter::remove(const String8& key) 836{ 837 if (mParameters.indexOfKey(key) >= 0) { 838 mParameters.removeItem(key); 839 return NO_ERROR; 840 } else { 841 return BAD_VALUE; 842 } 843} 844 845status_t AudioParameter::get(const String8& key, String8& value) 846{ 847 if (mParameters.indexOfKey(key) >= 0) { 848 value = mParameters.valueFor(key); 849 return NO_ERROR; 850 } else { 851 return BAD_VALUE; 852 } 853} 854 855status_t AudioParameter::getInt(const String8& key, int& value) 856{ 857 String8 str8; 858 status_t result = get(key, str8); 859 value = 0; 860 if (result == NO_ERROR) { 861 int val; 862 if (sscanf(str8.string(), "%d", &val) == 1) { 863 value = val; 864 } else { 865 result = INVALID_OPERATION; 866 } 867 } 868 return result; 869} 870 871status_t AudioParameter::getFloat(const String8& key, float& value) 872{ 873 String8 str8; 874 status_t result = get(key, str8); 875 value = 0; 876 if (result == NO_ERROR) { 877 float val; 878 if (sscanf(str8.string(), "%f", &val) == 1) { 879 value = val; 880 } else { 881 result = INVALID_OPERATION; 882 } 883 } 884 return result; 885} 886 887}; // namespace android 888 889