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