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