AudioFlinger.h revision 20b9ef0b55c9150ae11057ab997ae61be2d496ef
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 "Configuration.h" 22#include <deque> 23#include <stdint.h> 24#include <sys/types.h> 25#include <limits.h> 26 27#include <cutils/compiler.h> 28 29#include <media/IAudioFlinger.h> 30#include <media/IAudioFlingerClient.h> 31#include <media/IAudioTrack.h> 32#include <media/IAudioRecord.h> 33#include <media/AudioSystem.h> 34#include <media/AudioTrack.h> 35 36#include <utils/Atomic.h> 37#include <utils/Errors.h> 38#include <utils/threads.h> 39#include <utils/SortedVector.h> 40#include <utils/TypeHelpers.h> 41#include <utils/Vector.h> 42 43#include <binder/BinderService.h> 44#include <binder/MemoryDealer.h> 45 46#include <system/audio.h> 47#include <system/audio_policy.h> 48 49#include <media/audiohal/StreamHalInterface.h> 50#include <media/AudioBufferProvider.h> 51#include <media/ExtendedAudioBufferProvider.h> 52 53#include "FastCapture.h" 54#include "FastMixer.h" 55#include <media/nbaio/NBAIO.h> 56#include "AudioWatchdog.h" 57#include "AudioMixer.h" 58#include "AudioStreamOut.h" 59#include "SpdifStreamOut.h" 60#include "AudioHwDevice.h" 61#include "LinearMap.h" 62 63#include <powermanager/IPowerManager.h> 64 65#include <media/nbaio/NBLog.h> 66#include <private/media/AudioTrackShared.h> 67 68namespace android { 69 70struct audio_track_cblk_t; 71struct effect_param_cblk_t; 72class AudioMixer; 73class AudioBuffer; 74class AudioResampler; 75class DeviceHalInterface; 76class DevicesFactoryHalInterface; 77class EffectsFactoryHalInterface; 78class FastMixer; 79class PassthruBufferProvider; 80class ServerProxy; 81 82// ---------------------------------------------------------------------------- 83 84static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3); 85 86 87// Max shared memory size for audio tracks and audio records per client process 88static const size_t kClientSharedHeapSizeBytes = 1024*1024; 89// Shared memory size multiplier for non low ram devices 90static const size_t kClientSharedHeapSizeMultiplier = 4; 91 92#define INCLUDING_FROM_AUDIOFLINGER_H 93 94class AudioFlinger : 95 public BinderService<AudioFlinger>, 96 public BnAudioFlinger 97{ 98 friend class BinderService<AudioFlinger>; // for AudioFlinger() 99public: 100 static const char* getServiceName() ANDROID_API { return "media.audio_flinger"; } 101 102 virtual status_t dump(int fd, const Vector<String16>& args); 103 104 // IAudioFlinger interface, in binder opcode order 105 virtual sp<IAudioTrack> createTrack( 106 audio_stream_type_t streamType, 107 uint32_t sampleRate, 108 audio_format_t format, 109 audio_channel_mask_t channelMask, 110 size_t *pFrameCount, 111 audio_output_flags_t *flags, 112 const sp<IMemory>& sharedBuffer, 113 audio_io_handle_t output, 114 pid_t pid, 115 pid_t tid, 116 audio_session_t *sessionId, 117 int clientUid, 118 status_t *status /*non-NULL*/, 119 audio_port_handle_t portId); 120 121 virtual sp<IAudioRecord> openRecord( 122 audio_io_handle_t input, 123 uint32_t sampleRate, 124 audio_format_t format, 125 audio_channel_mask_t channelMask, 126 const String16& opPackageName, 127 size_t *pFrameCount, 128 audio_input_flags_t *flags, 129 pid_t pid, 130 pid_t tid, 131 int clientUid, 132 audio_session_t *sessionId, 133 size_t *notificationFrames, 134 sp<IMemory>& cblk, 135 sp<IMemory>& buffers, 136 status_t *status /*non-NULL*/, 137 audio_port_handle_t portId); 138 139 virtual uint32_t sampleRate(audio_io_handle_t ioHandle) const; 140 virtual audio_format_t format(audio_io_handle_t output) const; 141 virtual size_t frameCount(audio_io_handle_t ioHandle) const; 142 virtual size_t frameCountHAL(audio_io_handle_t ioHandle) const; 143 virtual uint32_t latency(audio_io_handle_t output) const; 144 145 virtual status_t setMasterVolume(float value); 146 virtual status_t setMasterMute(bool muted); 147 148 virtual float masterVolume() const; 149 virtual bool masterMute() const; 150 151 virtual status_t setStreamVolume(audio_stream_type_t stream, float value, 152 audio_io_handle_t output); 153 virtual status_t setStreamMute(audio_stream_type_t stream, bool muted); 154 155 virtual float streamVolume(audio_stream_type_t stream, 156 audio_io_handle_t output) const; 157 virtual bool streamMute(audio_stream_type_t stream) const; 158 159 virtual status_t setMode(audio_mode_t mode); 160 161 virtual status_t setMicMute(bool state); 162 virtual bool getMicMute() const; 163 164 virtual status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs); 165 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) const; 166 167 virtual void registerClient(const sp<IAudioFlingerClient>& client); 168 169 virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format, 170 audio_channel_mask_t channelMask) const; 171 172 virtual status_t openOutput(audio_module_handle_t module, 173 audio_io_handle_t *output, 174 audio_config_t *config, 175 audio_devices_t *devices, 176 const String8& address, 177 uint32_t *latencyMs, 178 audio_output_flags_t flags); 179 180 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, 181 audio_io_handle_t output2); 182 183 virtual status_t closeOutput(audio_io_handle_t output); 184 185 virtual status_t suspendOutput(audio_io_handle_t output); 186 187 virtual status_t restoreOutput(audio_io_handle_t output); 188 189 virtual status_t openInput(audio_module_handle_t module, 190 audio_io_handle_t *input, 191 audio_config_t *config, 192 audio_devices_t *device, 193 const String8& address, 194 audio_source_t source, 195 audio_input_flags_t flags); 196 197 virtual status_t closeInput(audio_io_handle_t input); 198 199 virtual status_t invalidateStream(audio_stream_type_t stream); 200 201 virtual status_t setVoiceVolume(float volume); 202 203 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, 204 audio_io_handle_t output) const; 205 206 virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const; 207 208 // This is the binder API. For the internal API see nextUniqueId(). 209 virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use); 210 211 virtual void acquireAudioSessionId(audio_session_t audioSession, pid_t pid); 212 213 virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid); 214 215 virtual status_t queryNumberEffects(uint32_t *numEffects) const; 216 217 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor) const; 218 219 virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid, 220 effect_descriptor_t *descriptor) const; 221 222 virtual sp<IEffect> createEffect( 223 effect_descriptor_t *pDesc, 224 const sp<IEffectClient>& effectClient, 225 int32_t priority, 226 audio_io_handle_t io, 227 audio_session_t sessionId, 228 const String16& opPackageName, 229 status_t *status /*non-NULL*/, 230 int *id, 231 int *enabled); 232 233 virtual status_t moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput, 234 audio_io_handle_t dstOutput); 235 236 virtual audio_module_handle_t loadHwModule(const char *name); 237 238 virtual uint32_t getPrimaryOutputSamplingRate(); 239 virtual size_t getPrimaryOutputFrameCount(); 240 241 virtual status_t setLowRamDevice(bool isLowRamDevice); 242 243 /* List available audio ports and their attributes */ 244 virtual status_t listAudioPorts(unsigned int *num_ports, 245 struct audio_port *ports); 246 247 /* Get attributes for a given audio port */ 248 virtual status_t getAudioPort(struct audio_port *port); 249 250 /* Create an audio patch between several source and sink ports */ 251 virtual status_t createAudioPatch(const struct audio_patch *patch, 252 audio_patch_handle_t *handle); 253 254 /* Release an audio patch */ 255 virtual status_t releaseAudioPatch(audio_patch_handle_t handle); 256 257 /* List existing audio patches */ 258 virtual status_t listAudioPatches(unsigned int *num_patches, 259 struct audio_patch *patches); 260 261 /* Set audio port configuration */ 262 virtual status_t setAudioPortConfig(const struct audio_port_config *config); 263 264 /* Get the HW synchronization source used for an audio session */ 265 virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId); 266 267 /* Indicate JAVA services are ready (scheduling, power management ...) */ 268 virtual status_t systemReady(); 269 270 virtual status_t onTransact( 271 uint32_t code, 272 const Parcel& data, 273 Parcel* reply, 274 uint32_t flags); 275 276 // end of IAudioFlinger interface 277 278 sp<NBLog::Writer> newWriter_l(size_t size, const char *name); 279 void unregisterWriter(const sp<NBLog::Writer>& writer); 280 sp<EffectsFactoryHalInterface> getEffectsFactory(); 281private: 282 static const size_t kLogMemorySize = 40 * 1024; 283 sp<MemoryDealer> mLogMemoryDealer; // == 0 when NBLog is disabled 284 // When a log writer is unregistered, it is done lazily so that media.log can continue to see it 285 // for as long as possible. The memory is only freed when it is needed for another log writer. 286 Vector< sp<NBLog::Writer> > mUnregisteredWriters; 287 Mutex mUnregisteredWritersLock; 288public: 289 290 class SyncEvent; 291 292 typedef void (*sync_event_callback_t)(const wp<SyncEvent>& event) ; 293 294 class SyncEvent : public RefBase { 295 public: 296 SyncEvent(AudioSystem::sync_event_t type, 297 audio_session_t triggerSession, 298 audio_session_t listenerSession, 299 sync_event_callback_t callBack, 300 wp<RefBase> cookie) 301 : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession), 302 mCallback(callBack), mCookie(cookie) 303 {} 304 305 virtual ~SyncEvent() {} 306 307 void trigger() { Mutex::Autolock _l(mLock); if (mCallback) mCallback(this); } 308 bool isCancelled() const { Mutex::Autolock _l(mLock); return (mCallback == NULL); } 309 void cancel() { Mutex::Autolock _l(mLock); mCallback = NULL; } 310 AudioSystem::sync_event_t type() const { return mType; } 311 audio_session_t triggerSession() const { return mTriggerSession; } 312 audio_session_t listenerSession() const { return mListenerSession; } 313 wp<RefBase> cookie() const { return mCookie; } 314 315 private: 316 const AudioSystem::sync_event_t mType; 317 const audio_session_t mTriggerSession; 318 const audio_session_t mListenerSession; 319 sync_event_callback_t mCallback; 320 const wp<RefBase> mCookie; 321 mutable Mutex mLock; 322 }; 323 324 sp<SyncEvent> createSyncEvent(AudioSystem::sync_event_t type, 325 audio_session_t triggerSession, 326 audio_session_t listenerSession, 327 sync_event_callback_t callBack, 328 const wp<RefBase>& cookie); 329 330private: 331 332 audio_mode_t getMode() const { return mMode; } 333 334 bool btNrecIsOff() const { return mBtNrecIsOff; } 335 336 AudioFlinger() ANDROID_API; 337 virtual ~AudioFlinger(); 338 339 // call in any IAudioFlinger method that accesses mPrimaryHardwareDev 340 status_t initCheck() const { return mPrimaryHardwareDev == NULL ? 341 NO_INIT : NO_ERROR; } 342 343 // RefBase 344 virtual void onFirstRef(); 345 346 AudioHwDevice* findSuitableHwDev_l(audio_module_handle_t module, 347 audio_devices_t devices); 348 void purgeStaleEffects_l(); 349 350 // Set kEnableExtendedChannels to true to enable greater than stereo output 351 // for the MixerThread and device sink. Number of channels allowed is 352 // FCC_2 <= channels <= AudioMixer::MAX_NUM_CHANNELS. 353 static const bool kEnableExtendedChannels = true; 354 355 // Returns true if channel mask is permitted for the PCM sink in the MixerThread 356 static inline bool isValidPcmSinkChannelMask(audio_channel_mask_t channelMask) { 357 switch (audio_channel_mask_get_representation(channelMask)) { 358 case AUDIO_CHANNEL_REPRESENTATION_POSITION: { 359 uint32_t channelCount = FCC_2; // stereo is default 360 if (kEnableExtendedChannels) { 361 channelCount = audio_channel_count_from_out_mask(channelMask); 362 if (channelCount < FCC_2 // mono is not supported at this time 363 || channelCount > AudioMixer::MAX_NUM_CHANNELS) { 364 return false; 365 } 366 } 367 // check that channelMask is the "canonical" one we expect for the channelCount. 368 return channelMask == audio_channel_out_mask_from_count(channelCount); 369 } 370 case AUDIO_CHANNEL_REPRESENTATION_INDEX: 371 if (kEnableExtendedChannels) { 372 const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); 373 if (channelCount >= FCC_2 // mono is not supported at this time 374 && channelCount <= AudioMixer::MAX_NUM_CHANNELS) { 375 return true; 376 } 377 } 378 return false; 379 default: 380 return false; 381 } 382 } 383 384 // Set kEnableExtendedPrecision to true to use extended precision in MixerThread 385 static const bool kEnableExtendedPrecision = true; 386 387 // Returns true if format is permitted for the PCM sink in the MixerThread 388 static inline bool isValidPcmSinkFormat(audio_format_t format) { 389 switch (format) { 390 case AUDIO_FORMAT_PCM_16_BIT: 391 return true; 392 case AUDIO_FORMAT_PCM_FLOAT: 393 case AUDIO_FORMAT_PCM_24_BIT_PACKED: 394 case AUDIO_FORMAT_PCM_32_BIT: 395 case AUDIO_FORMAT_PCM_8_24_BIT: 396 return kEnableExtendedPrecision; 397 default: 398 return false; 399 } 400 } 401 402 // standby delay for MIXER and DUPLICATING playback threads is read from property 403 // ro.audio.flinger_standbytime_ms or defaults to kDefaultStandbyTimeInNsecs 404 static nsecs_t mStandbyTimeInNsecs; 405 406 // incremented by 2 when screen state changes, bit 0 == 1 means "off" 407 // AudioFlinger::setParameters() updates, other threads read w/o lock 408 static uint32_t mScreenState; 409 410 // Internal dump utilities. 411 static const int kDumpLockRetries = 50; 412 static const int kDumpLockSleepUs = 20000; 413 static bool dumpTryLock(Mutex& mutex); 414 void dumpPermissionDenial(int fd, const Vector<String16>& args); 415 void dumpClients(int fd, const Vector<String16>& args); 416 void dumpInternals(int fd, const Vector<String16>& args); 417 418 // --- Client --- 419 class Client : public RefBase { 420 public: 421 Client(const sp<AudioFlinger>& audioFlinger, pid_t pid); 422 virtual ~Client(); 423 sp<MemoryDealer> heap() const; 424 pid_t pid() const { return mPid; } 425 sp<AudioFlinger> audioFlinger() const { return mAudioFlinger; } 426 427 private: 428 Client(const Client&); 429 Client& operator = (const Client&); 430 const sp<AudioFlinger> mAudioFlinger; 431 sp<MemoryDealer> mMemoryDealer; 432 const pid_t mPid; 433 }; 434 435 // --- Notification Client --- 436 class NotificationClient : public IBinder::DeathRecipient { 437 public: 438 NotificationClient(const sp<AudioFlinger>& audioFlinger, 439 const sp<IAudioFlingerClient>& client, 440 pid_t pid); 441 virtual ~NotificationClient(); 442 443 sp<IAudioFlingerClient> audioFlingerClient() const { return mAudioFlingerClient; } 444 445 // IBinder::DeathRecipient 446 virtual void binderDied(const wp<IBinder>& who); 447 448 private: 449 NotificationClient(const NotificationClient&); 450 NotificationClient& operator = (const NotificationClient&); 451 452 const sp<AudioFlinger> mAudioFlinger; 453 const pid_t mPid; 454 const sp<IAudioFlingerClient> mAudioFlingerClient; 455 }; 456 457 class TrackHandle; 458 class RecordHandle; 459 class RecordThread; 460 class PlaybackThread; 461 class MixerThread; 462 class DirectOutputThread; 463 class OffloadThread; 464 class DuplicatingThread; 465 class AsyncCallbackThread; 466 class Track; 467 class RecordTrack; 468 class EffectModule; 469 class EffectHandle; 470 class EffectChain; 471 472 struct AudioStreamIn; 473 474 struct stream_type_t { 475 stream_type_t() 476 : volume(1.0f), 477 mute(false) 478 { 479 } 480 float volume; 481 bool mute; 482 }; 483 484 // --- PlaybackThread --- 485 486#include "Threads.h" 487 488#include "Effects.h" 489 490#include "PatchPanel.h" 491 492 // server side of the client's IAudioTrack 493 class TrackHandle : public android::BnAudioTrack { 494 public: 495 explicit TrackHandle(const sp<PlaybackThread::Track>& track); 496 virtual ~TrackHandle(); 497 virtual sp<IMemory> getCblk() const; 498 virtual status_t start(); 499 virtual void stop(); 500 virtual void flush(); 501 virtual void pause(); 502 virtual status_t attachAuxEffect(int effectId); 503 virtual status_t setParameters(const String8& keyValuePairs); 504 virtual status_t getTimestamp(AudioTimestamp& timestamp); 505 virtual void signal(); // signal playback thread for a change in control block 506 507 virtual status_t onTransact( 508 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 509 510 private: 511 const sp<PlaybackThread::Track> mTrack; 512 }; 513 514 // server side of the client's IAudioRecord 515 class RecordHandle : public android::BnAudioRecord { 516 public: 517 explicit RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack); 518 virtual ~RecordHandle(); 519 virtual status_t start(int /*AudioSystem::sync_event_t*/ event, 520 audio_session_t triggerSession); 521 virtual void stop(); 522 virtual status_t onTransact( 523 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 524 private: 525 const sp<RecordThread::RecordTrack> mRecordTrack; 526 527 // for use from destructor 528 void stop_nonvirtual(); 529 }; 530 531 532 ThreadBase *checkThread_l(audio_io_handle_t ioHandle) const; 533 PlaybackThread *checkPlaybackThread_l(audio_io_handle_t output) const; 534 MixerThread *checkMixerThread_l(audio_io_handle_t output) const; 535 RecordThread *checkRecordThread_l(audio_io_handle_t input) const; 536 sp<RecordThread> openInput_l(audio_module_handle_t module, 537 audio_io_handle_t *input, 538 audio_config_t *config, 539 audio_devices_t device, 540 const String8& address, 541 audio_source_t source, 542 audio_input_flags_t flags); 543 sp<PlaybackThread> openOutput_l(audio_module_handle_t module, 544 audio_io_handle_t *output, 545 audio_config_t *config, 546 audio_devices_t devices, 547 const String8& address, 548 audio_output_flags_t flags); 549 550 void closeOutputFinish(const sp<PlaybackThread>& thread); 551 void closeInputFinish(const sp<RecordThread>& thread); 552 553 // no range check, AudioFlinger::mLock held 554 bool streamMute_l(audio_stream_type_t stream) const 555 { return mStreamTypes[stream].mute; } 556 // no range check, doesn't check per-thread stream volume, AudioFlinger::mLock held 557 float streamVolume_l(audio_stream_type_t stream) const 558 { return mStreamTypes[stream].volume; } 559 void ioConfigChanged(audio_io_config_event event, 560 const sp<AudioIoDescriptor>& ioDesc, 561 pid_t pid = 0); 562 563 // Allocate an audio_unique_id_t. 564 // Specific types are audio_io_handle_t, audio_session_t, effect ID (int), 565 // audio_module_handle_t, and audio_patch_handle_t. 566 // They all share the same ID space, but the namespaces are actually independent 567 // because there are separate KeyedVectors for each kind of ID. 568 // The return value is cast to the specific type depending on how the ID will be used. 569 // FIXME This API does not handle rollover to zero (for unsigned IDs), 570 // or from positive to negative (for signed IDs). 571 // Thus it may fail by returning an ID of the wrong sign, 572 // or by returning a non-unique ID. 573 // This is the internal API. For the binder API see newAudioUniqueId(). 574 audio_unique_id_t nextUniqueId(audio_unique_id_use_t use); 575 576 status_t moveEffectChain_l(audio_session_t sessionId, 577 PlaybackThread *srcThread, 578 PlaybackThread *dstThread, 579 bool reRegister); 580 581 // return thread associated with primary hardware device, or NULL 582 PlaybackThread *primaryPlaybackThread_l() const; 583 audio_devices_t primaryOutputDevice_l() const; 584 585 // return the playback thread with smallest HAL buffer size, and prefer fast 586 PlaybackThread *fastPlaybackThread_l() const; 587 588 sp<PlaybackThread> getEffectThread_l(audio_session_t sessionId, int EffectId); 589 590 591 void removeClient_l(pid_t pid); 592 void removeNotificationClient(pid_t pid); 593 bool isNonOffloadableGlobalEffectEnabled_l(); 594 void onNonOffloadableGlobalEffectEnable(); 595 bool isSessionAcquired_l(audio_session_t audioSession); 596 597 // Store an effect chain to mOrphanEffectChains keyed vector. 598 // Called when a thread exits and effects are still attached to it. 599 // If effects are later created on the same session, they will reuse the same 600 // effect chain and same instances in the effect library. 601 // return ALREADY_EXISTS if a chain with the same session already exists in 602 // mOrphanEffectChains. Note that this should never happen as there is only one 603 // chain for a given session and it is attached to only one thread at a time. 604 status_t putOrphanEffectChain_l(const sp<EffectChain>& chain); 605 // Get an effect chain for the specified session in mOrphanEffectChains and remove 606 // it if found. Returns 0 if not found (this is the most common case). 607 sp<EffectChain> getOrphanEffectChain_l(audio_session_t session); 608 // Called when the last effect handle on an effect instance is removed. If this 609 // effect belongs to an effect chain in mOrphanEffectChains, the chain is updated 610 // and removed from mOrphanEffectChains if it does not contain any effect. 611 // Return true if the effect was found in mOrphanEffectChains, false otherwise. 612 bool updateOrphanEffectChains(const sp<EffectModule>& effect); 613 614 void broacastParametersToRecordThreads_l(const String8& keyValuePairs); 615 616 // AudioStreamIn is immutable, so their fields are const. 617 // For emphasis, we could also make all pointers to them be "const *", 618 // but that would clutter the code unnecessarily. 619 620 struct AudioStreamIn { 621 AudioHwDevice* const audioHwDev; 622 sp<StreamInHalInterface> stream; 623 audio_input_flags_t flags; 624 625 sp<DeviceHalInterface> hwDev() const { return audioHwDev->hwDevice(); } 626 627 AudioStreamIn(AudioHwDevice *dev, sp<StreamInHalInterface> in, audio_input_flags_t flags) : 628 audioHwDev(dev), stream(in), flags(flags) {} 629 }; 630 631 // for mAudioSessionRefs only 632 struct AudioSessionRef { 633 AudioSessionRef(audio_session_t sessionid, pid_t pid) : 634 mSessionid(sessionid), mPid(pid), mCnt(1) {} 635 const audio_session_t mSessionid; 636 const pid_t mPid; 637 int mCnt; 638 }; 639 640 mutable Mutex mLock; 641 // protects mClients and mNotificationClients. 642 // must be locked after mLock and ThreadBase::mLock if both must be locked 643 // avoids acquiring AudioFlinger::mLock from inside thread loop. 644 mutable Mutex mClientLock; 645 // protected by mClientLock 646 DefaultKeyedVector< pid_t, wp<Client> > mClients; // see ~Client() 647 648 mutable Mutex mHardwareLock; 649 // NOTE: If both mLock and mHardwareLock mutexes must be held, 650 // always take mLock before mHardwareLock 651 652 // These two fields are immutable after onFirstRef(), so no lock needed to access 653 AudioHwDevice* mPrimaryHardwareDev; // mAudioHwDevs[0] or NULL 654 DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*> mAudioHwDevs; 655 656 sp<DevicesFactoryHalInterface> mDevicesFactoryHal; 657 658 // for dump, indicates which hardware operation is currently in progress (but not stream ops) 659 enum hardware_call_state { 660 AUDIO_HW_IDLE = 0, // no operation in progress 661 AUDIO_HW_INIT, // init_check 662 AUDIO_HW_OUTPUT_OPEN, // open_output_stream 663 AUDIO_HW_OUTPUT_CLOSE, // unused 664 AUDIO_HW_INPUT_OPEN, // unused 665 AUDIO_HW_INPUT_CLOSE, // unused 666 AUDIO_HW_STANDBY, // unused 667 AUDIO_HW_SET_MASTER_VOLUME, // set_master_volume 668 AUDIO_HW_GET_ROUTING, // unused 669 AUDIO_HW_SET_ROUTING, // unused 670 AUDIO_HW_GET_MODE, // unused 671 AUDIO_HW_SET_MODE, // set_mode 672 AUDIO_HW_GET_MIC_MUTE, // get_mic_mute 673 AUDIO_HW_SET_MIC_MUTE, // set_mic_mute 674 AUDIO_HW_SET_VOICE_VOLUME, // set_voice_volume 675 AUDIO_HW_SET_PARAMETER, // set_parameters 676 AUDIO_HW_GET_INPUT_BUFFER_SIZE, // get_input_buffer_size 677 AUDIO_HW_GET_MASTER_VOLUME, // get_master_volume 678 AUDIO_HW_GET_PARAMETER, // get_parameters 679 AUDIO_HW_SET_MASTER_MUTE, // set_master_mute 680 AUDIO_HW_GET_MASTER_MUTE, // get_master_mute 681 }; 682 683 mutable hardware_call_state mHardwareStatus; // for dump only 684 685 686 DefaultKeyedVector< audio_io_handle_t, sp<PlaybackThread> > mPlaybackThreads; 687 stream_type_t mStreamTypes[AUDIO_STREAM_CNT]; 688 689 // member variables below are protected by mLock 690 float mMasterVolume; 691 bool mMasterMute; 692 // end of variables protected by mLock 693 694 DefaultKeyedVector< audio_io_handle_t, sp<RecordThread> > mRecordThreads; 695 696 // protected by mClientLock 697 DefaultKeyedVector< pid_t, sp<NotificationClient> > mNotificationClients; 698 699 // updated by atomic_fetch_add_explicit 700 volatile atomic_uint_fast32_t mNextUniqueIds[AUDIO_UNIQUE_ID_USE_MAX]; 701 702 audio_mode_t mMode; 703 bool mBtNrecIsOff; 704 705 // protected by mLock 706 Vector<AudioSessionRef*> mAudioSessionRefs; 707 708 float masterVolume_l() const; 709 bool masterMute_l() const; 710 audio_module_handle_t loadHwModule_l(const char *name); 711 712 Vector < sp<SyncEvent> > mPendingSyncEvents; // sync events awaiting for a session 713 // to be created 714 715 // Effect chains without a valid thread 716 DefaultKeyedVector< audio_session_t , sp<EffectChain> > mOrphanEffectChains; 717 718 // list of sessions for which a valid HW A/V sync ID was retrieved from the HAL 719 DefaultKeyedVector< audio_session_t , audio_hw_sync_t >mHwAvSyncIds; 720private: 721 sp<Client> registerPid(pid_t pid); // always returns non-0 722 723 // for use from destructor 724 status_t closeOutput_nonvirtual(audio_io_handle_t output); 725 void closeOutputInternal_l(const sp<PlaybackThread>& thread); 726 status_t closeInput_nonvirtual(audio_io_handle_t input); 727 void closeInputInternal_l(const sp<RecordThread>& thread); 728 void setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId); 729 730 status_t checkStreamType(audio_stream_type_t stream) const; 731 732#ifdef TEE_SINK 733 // all record threads serially share a common tee sink, which is re-created on format change 734 sp<NBAIO_Sink> mRecordTeeSink; 735 sp<NBAIO_Source> mRecordTeeSource; 736#endif 737 738public: 739 740#ifdef TEE_SINK 741 // tee sink, if enabled by property, allows dumpsys to write most recent audio to .wav file 742 static void dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id = 0); 743 744 // whether tee sink is enabled by property 745 static bool mTeeSinkInputEnabled; 746 static bool mTeeSinkOutputEnabled; 747 static bool mTeeSinkTrackEnabled; 748 749 // runtime configured size of each tee sink pipe, in frames 750 static size_t mTeeSinkInputFrames; 751 static size_t mTeeSinkOutputFrames; 752 static size_t mTeeSinkTrackFrames; 753 754 // compile-time default size of tee sink pipes, in frames 755 // 0x200000 stereo 16-bit PCM frames = 47.5 seconds at 44.1 kHz, 8 megabytes 756 static const size_t kTeeSinkInputFramesDefault = 0x200000; 757 static const size_t kTeeSinkOutputFramesDefault = 0x200000; 758 static const size_t kTeeSinkTrackFramesDefault = 0x200000; 759#endif 760 761 // This method reads from a variable without mLock, but the variable is updated under mLock. So 762 // we might read a stale value, or a value that's inconsistent with respect to other variables. 763 // In this case, it's safe because the return value isn't used for making an important decision. 764 // The reason we don't want to take mLock is because it could block the caller for a long time. 765 bool isLowRamDevice() const { return mIsLowRamDevice; } 766 767private: 768 bool mIsLowRamDevice; 769 bool mIsDeviceTypeKnown; 770 nsecs_t mGlobalEffectEnableTime; // when a global effect was last enabled 771 772 sp<PatchPanel> mPatchPanel; 773 sp<EffectsFactoryHalInterface> mEffectsFactoryHal; 774 775 bool mSystemReady; 776}; 777 778#undef INCLUDING_FROM_AUDIOFLINGER_H 779 780std::string formatToString(audio_format_t format); 781std::string inputFlagsToString(audio_input_flags_t flags); 782std::string outputFlagsToString(audio_output_flags_t flags); 783std::string devicesToString(audio_devices_t devices); 784const char *sourceToString(audio_source_t source); 785 786// ---------------------------------------------------------------------------- 787 788} // namespace android 789 790#endif // ANDROID_AUDIO_FLINGER_H 791