1 /* 2 ** 3 ** Copyright 2007, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #ifndef ANDROID_AUDIO_FLINGER_H 19 #define ANDROID_AUDIO_FLINGER_H 20 21 #include <stdint.h> 22 #include <sys/types.h> 23 #include <limits.h> 24 25 #include <common_time/cc_helper.h> 26 27 #include <media/IAudioFlinger.h> 28 #include <media/IAudioFlingerClient.h> 29 #include <media/IAudioTrack.h> 30 #include <media/IAudioRecord.h> 31 #include <media/AudioSystem.h> 32 #include <media/AudioTrack.h> 33 34 #include <utils/Atomic.h> 35 #include <utils/Errors.h> 36 #include <utils/threads.h> 37 #include <utils/SortedVector.h> 38 #include <utils/TypeHelpers.h> 39 #include <utils/Vector.h> 40 41 #include <binder/BinderService.h> 42 #include <binder/MemoryDealer.h> 43 44 #include <system/audio.h> 45 #include <hardware/audio.h> 46 #include <hardware/audio_policy.h> 47 48 #include <media/AudioBufferProvider.h> 49 #include <media/ExtendedAudioBufferProvider.h> 50 #include "FastMixer.h" 51 #include <media/nbaio/NBAIO.h> 52 #include "AudioWatchdog.h" 53 54 #include <powermanager/IPowerManager.h> 55 56 #include <media/nbaio/NBLog.h> 57 58 namespace android { 59 60 class audio_track_cblk_t; 61 class effect_param_cblk_t; 62 class AudioMixer; 63 class AudioBuffer; 64 class AudioResampler; 65 class FastMixer; 66 class ServerProxy; 67 68 // ---------------------------------------------------------------------------- 69 70 // AudioFlinger has a hard-coded upper limit of 2 channels for capture and playback. 71 // There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect. 72 // Adding full support for > 2 channel capture or playback would require more than simply changing 73 // this #define. There is an independent hard-coded upper limit in AudioMixer; 74 // removing that AudioMixer limit would be necessary but insufficient to support > 2 channels. 75 // The macro FCC_2 highlights some (but not all) places where there is are 2-channel assumptions. 76 // Search also for "2", "left", "right", "[0]", "[1]", ">> 16", "<< 16", etc. 77 #define FCC_2 2 // FCC_2 = Fixed Channel Count 2 78 79 static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3); 80 81 #define MAX_GAIN 4096.0f 82 #define MAX_GAIN_INT 0x1000 83 84 #define INCLUDING_FROM_AUDIOFLINGER_H 85 86 class AudioFlinger : 87 public BinderService<AudioFlinger>, 88 public BnAudioFlinger 89 { 90 friend class BinderService<AudioFlinger>; // for AudioFlinger() 91 public: 92 static const char* getServiceName() { return "media.audio_flinger"; } 93 94 virtual status_t dump(int fd, const Vector<String16>& args); 95 96 // IAudioFlinger interface, in binder opcode order 97 virtual sp<IAudioTrack> createTrack( 98 audio_stream_type_t streamType, 99 uint32_t sampleRate, 100 audio_format_t format, 101 audio_channel_mask_t channelMask, 102 size_t frameCount, 103 IAudioFlinger::track_flags_t *flags, 104 const sp<IMemory>& sharedBuffer, 105 audio_io_handle_t output, 106 pid_t tid, 107 int *sessionId, 108 status_t *status); 109 110 virtual sp<IAudioRecord> openRecord( 111 audio_io_handle_t input, 112 uint32_t sampleRate, 113 audio_format_t format, 114 audio_channel_mask_t channelMask, 115 size_t frameCount, 116 IAudioFlinger::track_flags_t flags, 117 pid_t tid, 118 int *sessionId, 119 status_t *status); 120 121 virtual uint32_t sampleRate(audio_io_handle_t output) const; 122 virtual int channelCount(audio_io_handle_t output) const; 123 virtual audio_format_t format(audio_io_handle_t output) const; 124 virtual size_t frameCount(audio_io_handle_t output) const; 125 virtual uint32_t latency(audio_io_handle_t output) const; 126 127 virtual status_t setMasterVolume(float value); 128 virtual status_t setMasterMute(bool muted); 129 130 virtual float masterVolume() const; 131 virtual bool masterMute() const; 132 133 virtual status_t setStreamVolume(audio_stream_type_t stream, float value, 134 audio_io_handle_t output); 135 virtual status_t setStreamMute(audio_stream_type_t stream, bool muted); 136 137 virtual float streamVolume(audio_stream_type_t stream, 138 audio_io_handle_t output) const; 139 virtual bool streamMute(audio_stream_type_t stream) const; 140 141 virtual status_t setMode(audio_mode_t mode); 142 143 virtual status_t setMicMute(bool state); 144 virtual bool getMicMute() const; 145 146 virtual status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs); 147 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) const; 148 149 virtual void registerClient(const sp<IAudioFlingerClient>& client); 150 151 virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format, 152 audio_channel_mask_t channelMask) const; 153 154 virtual audio_io_handle_t openOutput(audio_module_handle_t module, 155 audio_devices_t *pDevices, 156 uint32_t *pSamplingRate, 157 audio_format_t *pFormat, 158 audio_channel_mask_t *pChannelMask, 159 uint32_t *pLatencyMs, 160 audio_output_flags_t flags); 161 162 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, 163 audio_io_handle_t output2); 164 165 virtual status_t closeOutput(audio_io_handle_t output); 166 167 virtual status_t suspendOutput(audio_io_handle_t output); 168 169 virtual status_t restoreOutput(audio_io_handle_t output); 170 171 virtual audio_io_handle_t openInput(audio_module_handle_t module, 172 audio_devices_t *pDevices, 173 uint32_t *pSamplingRate, 174 audio_format_t *pFormat, 175 audio_channel_mask_t *pChannelMask); 176 177 virtual status_t closeInput(audio_io_handle_t input); 178 179 virtual status_t setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output); 180 181 virtual status_t setVoiceVolume(float volume); 182 183 virtual status_t getRenderPosition(size_t *halFrames, size_t *dspFrames, 184 audio_io_handle_t output) const; 185 186 virtual unsigned int getInputFramesLost(audio_io_handle_t ioHandle) const; 187 188 virtual int newAudioSessionId(); 189 190 virtual void acquireAudioSessionId(int audioSession); 191 192 virtual void releaseAudioSessionId(int audioSession); 193 194 virtual status_t queryNumberEffects(uint32_t *numEffects) const; 195 196 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor) const; 197 198 virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid, 199 effect_descriptor_t *descriptor) const; 200 201 virtual sp<IEffect> createEffect( 202 effect_descriptor_t *pDesc, 203 const sp<IEffectClient>& effectClient, 204 int32_t priority, 205 audio_io_handle_t io, 206 int sessionId, 207 status_t *status, 208 int *id, 209 int *enabled); 210 211 virtual status_t moveEffects(int sessionId, audio_io_handle_t srcOutput, 212 audio_io_handle_t dstOutput); 213 214 virtual audio_module_handle_t loadHwModule(const char *name); 215 216 virtual uint32_t getPrimaryOutputSamplingRate(); 217 virtual size_t getPrimaryOutputFrameCount(); 218 219 virtual status_t onTransact( 220 uint32_t code, 221 const Parcel& data, 222 Parcel* reply, 223 uint32_t flags); 224 225 // end of IAudioFlinger interface 226 227 sp<NBLog::Writer> newWriter_l(size_t size, const char *name); 228 void unregisterWriter(const sp<NBLog::Writer>& writer); 229 private: 230 static const size_t kLogMemorySize = 10 * 1024; 231 sp<MemoryDealer> mLogMemoryDealer; // == 0 when NBLog is disabled 232 public: 233 234 class SyncEvent; 235 236 typedef void (*sync_event_callback_t)(const wp<SyncEvent>& event) ; 237 238 class SyncEvent : public RefBase { 239 public: 240 SyncEvent(AudioSystem::sync_event_t type, 241 int triggerSession, 242 int listenerSession, 243 sync_event_callback_t callBack, 244 void *cookie) 245 : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession), 246 mCallback(callBack), mCookie(cookie) 247 {} 248 249 virtual ~SyncEvent() {} 250 251 void trigger() { Mutex::Autolock _l(mLock); if (mCallback) mCallback(this); } 252 bool isCancelled() const { Mutex::Autolock _l(mLock); return (mCallback == NULL); } 253 void cancel() { Mutex::Autolock _l(mLock); mCallback = NULL; } 254 AudioSystem::sync_event_t type() const { return mType; } 255 int triggerSession() const { return mTriggerSession; } 256 int listenerSession() const { return mListenerSession; } 257 void *cookie() const { return mCookie; } 258 259 private: 260 const AudioSystem::sync_event_t mType; 261 const int mTriggerSession; 262 const int mListenerSession; 263 sync_event_callback_t mCallback; 264 void * const mCookie; 265 mutable Mutex mLock; 266 }; 267 268 sp<SyncEvent> createSyncEvent(AudioSystem::sync_event_t type, 269 int triggerSession, 270 int listenerSession, 271 sync_event_callback_t callBack, 272 void *cookie); 273 274 private: 275 class AudioHwDevice; // fwd declaration for findSuitableHwDev_l 276 277 audio_mode_t getMode() const { return mMode; } 278 279 bool btNrecIsOff() const { return mBtNrecIsOff; } 280 281 AudioFlinger(); 282 virtual ~AudioFlinger(); 283 284 // call in any IAudioFlinger method that accesses mPrimaryHardwareDev 285 status_t initCheck() const { return mPrimaryHardwareDev == NULL ? 286 NO_INIT : NO_ERROR; } 287 288 // RefBase 289 virtual void onFirstRef(); 290 291 AudioHwDevice* findSuitableHwDev_l(audio_module_handle_t module, 292 audio_devices_t devices); 293 void purgeStaleEffects_l(); 294 295 // standby delay for MIXER and DUPLICATING playback threads is read from property 296 // ro.audio.flinger_standbytime_ms or defaults to kDefaultStandbyTimeInNsecs 297 static nsecs_t mStandbyTimeInNsecs; 298 299 // incremented by 2 when screen state changes, bit 0 == 1 means "off" 300 // AudioFlinger::setParameters() updates, other threads read w/o lock 301 static uint32_t mScreenState; 302 303 // Internal dump utilities. 304 static const int kDumpLockRetries = 50; 305 static const int kDumpLockSleepUs = 20000; 306 static bool dumpTryLock(Mutex& mutex); 307 void dumpPermissionDenial(int fd, const Vector<String16>& args); 308 void dumpClients(int fd, const Vector<String16>& args); 309 void dumpInternals(int fd, const Vector<String16>& args); 310 311 // --- Client --- 312 class Client : public RefBase { 313 public: 314 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid); 315 virtual ~Client(); 316 sp<MemoryDealer> heap() const; 317 pid_t pid() const { return mPid; } 318 sp<AudioFlinger> audioFlinger() const { return mAudioFlinger; } 319 320 bool reserveTimedTrack(); 321 void releaseTimedTrack(); 322 323 private: 324 Client(const Client&); 325 Client& operator = (const Client&); 326 const sp<AudioFlinger> mAudioFlinger; 327 const sp<MemoryDealer> mMemoryDealer; 328 const pid_t mPid; 329 330 Mutex mTimedTrackLock; 331 int mTimedTrackCount; 332 }; 333 334 // --- Notification Client --- 335 class NotificationClient : public IBinder::DeathRecipient { 336 public: 337 NotificationClient(const sp<AudioFlinger>& audioFlinger, 338 const sp<IAudioFlingerClient>& client, 339 pid_t pid); 340 virtual ~NotificationClient(); 341 342 sp<IAudioFlingerClient> audioFlingerClient() const { return mAudioFlingerClient; } 343 344 // IBinder::DeathRecipient 345 virtual void binderDied(const wp<IBinder>& who); 346 347 private: 348 NotificationClient(const NotificationClient&); 349 NotificationClient& operator = (const NotificationClient&); 350 351 const sp<AudioFlinger> mAudioFlinger; 352 const pid_t mPid; 353 const sp<IAudioFlingerClient> mAudioFlingerClient; 354 }; 355 356 class TrackHandle; 357 class RecordHandle; 358 class RecordThread; 359 class PlaybackThread; 360 class MixerThread; 361 class DirectOutputThread; 362 class DuplicatingThread; 363 class Track; 364 class RecordTrack; 365 class EffectModule; 366 class EffectHandle; 367 class EffectChain; 368 struct AudioStreamOut; 369 struct AudioStreamIn; 370 371 struct stream_type_t { 372 stream_type_t() 373 : volume(1.0f), 374 mute(false) 375 { 376 } 377 float volume; 378 bool mute; 379 }; 380 381 // --- PlaybackThread --- 382 383 #include "Threads.h" 384 385 #include "Effects.h" 386 387 // server side of the client's IAudioTrack 388 class TrackHandle : public android::BnAudioTrack { 389 public: 390 TrackHandle(const sp<PlaybackThread::Track>& track); 391 virtual ~TrackHandle(); 392 virtual sp<IMemory> getCblk() const; 393 virtual status_t start(); 394 virtual void stop(); 395 virtual void flush(); 396 virtual void pause(); 397 virtual status_t attachAuxEffect(int effectId); 398 virtual status_t allocateTimedBuffer(size_t size, 399 sp<IMemory>* buffer); 400 virtual status_t queueTimedBuffer(const sp<IMemory>& buffer, 401 int64_t pts); 402 virtual status_t setMediaTimeTransform(const LinearTransform& xform, 403 int target); 404 virtual status_t onTransact( 405 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 406 private: 407 const sp<PlaybackThread::Track> mTrack; 408 }; 409 410 // server side of the client's IAudioRecord 411 class RecordHandle : public android::BnAudioRecord { 412 public: 413 RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack); 414 virtual ~RecordHandle(); 415 virtual sp<IMemory> getCblk() const; 416 virtual status_t start(int /*AudioSystem::sync_event_t*/ event, int triggerSession); 417 virtual void stop(); 418 virtual status_t onTransact( 419 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 420 private: 421 const sp<RecordThread::RecordTrack> mRecordTrack; 422 423 // for use from destructor 424 void stop_nonvirtual(); 425 }; 426 427 PlaybackThread *checkPlaybackThread_l(audio_io_handle_t output) const; 428 MixerThread *checkMixerThread_l(audio_io_handle_t output) const; 429 RecordThread *checkRecordThread_l(audio_io_handle_t input) const; 430 // no range check, AudioFlinger::mLock held 431 bool streamMute_l(audio_stream_type_t stream) const 432 { return mStreamTypes[stream].mute; } 433 // no range check, doesn't check per-thread stream volume, AudioFlinger::mLock held 434 float streamVolume_l(audio_stream_type_t stream) const 435 { return mStreamTypes[stream].volume; } 436 void audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2); 437 438 // allocate an audio_io_handle_t, session ID, or effect ID 439 uint32_t nextUniqueId(); 440 441 status_t moveEffectChain_l(int sessionId, 442 PlaybackThread *srcThread, 443 PlaybackThread *dstThread, 444 bool reRegister); 445 // return thread associated with primary hardware device, or NULL 446 PlaybackThread *primaryPlaybackThread_l() const; 447 audio_devices_t primaryOutputDevice_l() const; 448 449 sp<PlaybackThread> getEffectThread_l(int sessionId, int EffectId); 450 451 452 void removeClient_l(pid_t pid); 453 void removeNotificationClient(pid_t pid); 454 455 class AudioHwDevice { 456 public: 457 enum Flags { 458 AHWD_CAN_SET_MASTER_VOLUME = 0x1, 459 AHWD_CAN_SET_MASTER_MUTE = 0x2, 460 }; 461 462 AudioHwDevice(const char *moduleName, 463 audio_hw_device_t *hwDevice, 464 Flags flags) 465 : mModuleName(strdup(moduleName)) 466 , mHwDevice(hwDevice) 467 , mFlags(flags) { } 468 /*virtual*/ ~AudioHwDevice() { free((void *)mModuleName); } 469 470 bool canSetMasterVolume() const { 471 return (0 != (mFlags & AHWD_CAN_SET_MASTER_VOLUME)); 472 } 473 474 bool canSetMasterMute() const { 475 return (0 != (mFlags & AHWD_CAN_SET_MASTER_MUTE)); 476 } 477 478 const char *moduleName() const { return mModuleName; } 479 audio_hw_device_t *hwDevice() const { return mHwDevice; } 480 private: 481 const char * const mModuleName; 482 audio_hw_device_t * const mHwDevice; 483 Flags mFlags; 484 }; 485 486 // AudioStreamOut and AudioStreamIn are immutable, so their fields are const. 487 // For emphasis, we could also make all pointers to them be "const *", 488 // but that would clutter the code unnecessarily. 489 490 struct AudioStreamOut { 491 AudioHwDevice* const audioHwDev; 492 audio_stream_out_t* const stream; 493 494 audio_hw_device_t* hwDev() const { return audioHwDev->hwDevice(); } 495 496 AudioStreamOut(AudioHwDevice *dev, audio_stream_out_t *out) : 497 audioHwDev(dev), stream(out) {} 498 }; 499 500 struct AudioStreamIn { 501 AudioHwDevice* const audioHwDev; 502 audio_stream_in_t* const stream; 503 504 audio_hw_device_t* hwDev() const { return audioHwDev->hwDevice(); } 505 506 AudioStreamIn(AudioHwDevice *dev, audio_stream_in_t *in) : 507 audioHwDev(dev), stream(in) {} 508 }; 509 510 // for mAudioSessionRefs only 511 struct AudioSessionRef { 512 AudioSessionRef(int sessionid, pid_t pid) : 513 mSessionid(sessionid), mPid(pid), mCnt(1) {} 514 const int mSessionid; 515 const pid_t mPid; 516 int mCnt; 517 }; 518 519 mutable Mutex mLock; 520 521 DefaultKeyedVector< pid_t, wp<Client> > mClients; // see ~Client() 522 523 mutable Mutex mHardwareLock; 524 // NOTE: If both mLock and mHardwareLock mutexes must be held, 525 // always take mLock before mHardwareLock 526 527 // These two fields are immutable after onFirstRef(), so no lock needed to access 528 AudioHwDevice* mPrimaryHardwareDev; // mAudioHwDevs[0] or NULL 529 DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*> mAudioHwDevs; 530 531 // for dump, indicates which hardware operation is currently in progress (but not stream ops) 532 enum hardware_call_state { 533 AUDIO_HW_IDLE = 0, // no operation in progress 534 AUDIO_HW_INIT, // init_check 535 AUDIO_HW_OUTPUT_OPEN, // open_output_stream 536 AUDIO_HW_OUTPUT_CLOSE, // unused 537 AUDIO_HW_INPUT_OPEN, // unused 538 AUDIO_HW_INPUT_CLOSE, // unused 539 AUDIO_HW_STANDBY, // unused 540 AUDIO_HW_SET_MASTER_VOLUME, // set_master_volume 541 AUDIO_HW_GET_ROUTING, // unused 542 AUDIO_HW_SET_ROUTING, // unused 543 AUDIO_HW_GET_MODE, // unused 544 AUDIO_HW_SET_MODE, // set_mode 545 AUDIO_HW_GET_MIC_MUTE, // get_mic_mute 546 AUDIO_HW_SET_MIC_MUTE, // set_mic_mute 547 AUDIO_HW_SET_VOICE_VOLUME, // set_voice_volume 548 AUDIO_HW_SET_PARAMETER, // set_parameters 549 AUDIO_HW_GET_INPUT_BUFFER_SIZE, // get_input_buffer_size 550 AUDIO_HW_GET_MASTER_VOLUME, // get_master_volume 551 AUDIO_HW_GET_PARAMETER, // get_parameters 552 AUDIO_HW_SET_MASTER_MUTE, // set_master_mute 553 AUDIO_HW_GET_MASTER_MUTE, // get_master_mute 554 }; 555 556 mutable hardware_call_state mHardwareStatus; // for dump only 557 558 559 DefaultKeyedVector< audio_io_handle_t, sp<PlaybackThread> > mPlaybackThreads; 560 stream_type_t mStreamTypes[AUDIO_STREAM_CNT]; 561 562 // member variables below are protected by mLock 563 float mMasterVolume; 564 bool mMasterMute; 565 // end of variables protected by mLock 566 567 DefaultKeyedVector< audio_io_handle_t, sp<RecordThread> > mRecordThreads; 568 569 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients; 570 volatile int32_t mNextUniqueId; // updated by android_atomic_inc 571 audio_mode_t mMode; 572 bool mBtNrecIsOff; 573 574 // protected by mLock 575 Vector<AudioSessionRef*> mAudioSessionRefs; 576 577 float masterVolume_l() const; 578 bool masterMute_l() const; 579 audio_module_handle_t loadHwModule_l(const char *name); 580 581 Vector < sp<SyncEvent> > mPendingSyncEvents; // sync events awaiting for a session 582 // to be created 583 584 private: 585 sp<Client> registerPid_l(pid_t pid); // always returns non-0 586 587 // for use from destructor 588 status_t closeOutput_nonvirtual(audio_io_handle_t output); 589 status_t closeInput_nonvirtual(audio_io_handle_t input); 590 591 // do not use #ifdef here, since AudioFlinger.h is included by more than one module 592 //#ifdef TEE_SINK 593 // all record threads serially share a common tee sink, which is re-created on format change 594 sp<NBAIO_Sink> mRecordTeeSink; 595 sp<NBAIO_Source> mRecordTeeSource; 596 //#endif 597 598 public: 599 600 #ifdef TEE_SINK 601 // tee sink, if enabled by property, allows dumpsys to write most recent audio to .wav file 602 static void dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id = 0); 603 604 // whether tee sink is enabled by property 605 static bool mTeeSinkInputEnabled; 606 static bool mTeeSinkOutputEnabled; 607 static bool mTeeSinkTrackEnabled; 608 609 // runtime configured size of each tee sink pipe, in frames 610 static size_t mTeeSinkInputFrames; 611 static size_t mTeeSinkOutputFrames; 612 static size_t mTeeSinkTrackFrames; 613 614 // compile-time default size of tee sink pipes, in frames 615 // 0x200000 stereo 16-bit PCM frames = 47.5 seconds at 44.1 kHz, 8 megabytes 616 static const size_t kTeeSinkInputFramesDefault = 0x200000; 617 static const size_t kTeeSinkOutputFramesDefault = 0x200000; 618 static const size_t kTeeSinkTrackFramesDefault = 0x1000; 619 #endif 620 621 }; 622 623 #undef INCLUDING_FROM_AUDIOFLINGER_H 624 625 // ---------------------------------------------------------------------------- 626 627 }; // namespace android 628 629 #endif // ANDROID_AUDIO_FLINGER_H 630