1 /* 2 ** 3 ** Copyright 2012, 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 INCLUDING_FROM_AUDIOFLINGER_H 19 #error This header file should only be included from AudioFlinger.h 20 #endif 21 22 // Checks and monitors OP_PLAY_AUDIO 23 class OpPlayAudioMonitor : public RefBase { 24 public: 25 ~OpPlayAudioMonitor() override; 26 bool hasOpPlayAudio() const; 27 28 static sp<OpPlayAudioMonitor> createIfNeeded( 29 const AttributionSourceState& attributionSource, 30 const audio_attributes_t& attr, int id, 31 audio_stream_type_t streamType); 32 33 private: 34 OpPlayAudioMonitor(const AttributionSourceState& attributionSource, 35 audio_usage_t usage, int id); 36 void onFirstRef() override; 37 static void getPackagesForUid(uid_t uid, Vector<String16>& packages); 38 39 AppOpsManager mAppOpsManager; 40 41 class PlayAudioOpCallback : public BnAppOpsCallback { 42 public: 43 explicit PlayAudioOpCallback(const wp<OpPlayAudioMonitor>& monitor); 44 void opChanged(int32_t op, const String16& packageName) override; 45 46 private: 47 const wp<OpPlayAudioMonitor> mMonitor; 48 }; 49 50 sp<PlayAudioOpCallback> mOpCallback; 51 // called by PlayAudioOpCallback when OP_PLAY_AUDIO is updated in AppOp callback 52 void checkPlayAudioForUsage(); 53 54 std::atomic_bool mHasOpPlayAudio; 55 const AttributionSourceState mAttributionSource; 56 const int32_t mUsage; // on purpose not audio_usage_t because always checked in appOps as int32_t 57 const int mId; // for logging purposes only 58 }; 59 60 // playback track 61 class Track : public TrackBase, public VolumeProvider { 62 public: 63 Track( PlaybackThread *thread, 64 const sp<Client>& client, 65 audio_stream_type_t streamType, 66 const audio_attributes_t& attr, 67 uint32_t sampleRate, 68 audio_format_t format, 69 audio_channel_mask_t channelMask, 70 size_t frameCount, 71 void *buffer, 72 size_t bufferSize, 73 const sp<IMemory>& sharedBuffer, 74 audio_session_t sessionId, 75 pid_t creatorPid, 76 const AttributionSourceState& attributionSource, 77 audio_output_flags_t flags, 78 track_type type, 79 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE, 80 /** default behaviour is to start when there are as many frames 81 * ready as possible (aka. Buffer is full). */ 82 size_t frameCountToBeReady = SIZE_MAX, 83 float speed = 1.0f); 84 virtual ~Track(); 85 virtual status_t initCheck() const; 86 87 void appendDumpHeader(String8& result); 88 void appendDump(String8& result, bool active); 89 virtual status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE, 90 audio_session_t triggerSession = AUDIO_SESSION_NONE); 91 virtual void stop(); 92 void pause(); 93 94 void flush(); 95 void destroy(); 96 97 virtual uint32_t sampleRate() const; 98 streamType()99 audio_stream_type_t streamType() const { 100 return mStreamType; 101 } isOffloaded()102 bool isOffloaded() const 103 { return (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; } isDirect()104 bool isDirect() const override 105 { return (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0; } isOffloadedOrDirect()106 bool isOffloadedOrDirect() const { return (mFlags 107 & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD 108 | AUDIO_OUTPUT_FLAG_DIRECT)) != 0; } isStatic()109 bool isStatic() const { return mSharedBuffer.get() != nullptr; } 110 111 status_t setParameters(const String8& keyValuePairs); 112 status_t selectPresentation(int presentationId, int programId); 113 status_t attachAuxEffect(int EffectId); 114 void setAuxBuffer(int EffectId, int32_t *buffer); auxBuffer()115 int32_t *auxBuffer() const { return mAuxBuffer; } setMainBuffer(effect_buffer_t * buffer)116 void setMainBuffer(effect_buffer_t *buffer) { mMainBuffer = buffer; } mainBuffer()117 effect_buffer_t *mainBuffer() const { return mMainBuffer; } auxEffectId()118 int auxEffectId() const { return mAuxEffectId; } 119 virtual status_t getTimestamp(AudioTimestamp& timestamp); 120 void signal(); 121 status_t getDualMonoMode(audio_dual_mono_mode_t* mode); 122 status_t setDualMonoMode(audio_dual_mono_mode_t mode); 123 status_t getAudioDescriptionMixLevel(float* leveldB); 124 status_t setAudioDescriptionMixLevel(float leveldB); 125 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate); 126 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate); 127 128 // implement FastMixerState::VolumeProvider interface 129 virtual gain_minifloat_packed_t getVolumeLR(); 130 131 virtual status_t setSyncEvent(const sp<SyncEvent>& event); 132 isFastTrack()133 virtual bool isFastTrack() const { return (mFlags & AUDIO_OUTPUT_FLAG_FAST) != 0; } 134 bufferLatencyMs()135 double bufferLatencyMs() const override { 136 return isStatic() ? 0. : TrackBase::bufferLatencyMs(); 137 } 138 139 // implement volume handling. 140 media::VolumeShaper::Status applyVolumeShaper( 141 const sp<media::VolumeShaper::Configuration>& configuration, 142 const sp<media::VolumeShaper::Operation>& operation); 143 sp<media::VolumeShaper::State> getVolumeShaperState(int id); getVolumeHandler()144 sp<media::VolumeHandler> getVolumeHandler() { return mVolumeHandler; } 145 /** Set the computed normalized final volume of the track. 146 * !masterMute * masterVolume * streamVolume * averageLRVolume */ 147 void setFinalVolume(float volume); getFinalVolume()148 float getFinalVolume() const { return mFinalVolume; } 149 150 using SourceMetadatas = std::vector<playback_track_metadata_v7_t>; 151 using MetadataInserter = std::back_insert_iterator<SourceMetadatas>; 152 /** Copy the track metadata in the provided iterator. Thread safe. */ 153 virtual void copyMetadataTo(MetadataInserter& backInserter) const; 154 155 /** Return haptic playback of the track is enabled or not, used in mixer. */ getHapticPlaybackEnabled()156 bool getHapticPlaybackEnabled() const { return mHapticPlaybackEnabled; } 157 /** Set haptic playback of the track is enabled or not, should be 158 * set after query or get callback from vibrator service */ setHapticPlaybackEnabled(bool hapticPlaybackEnabled)159 void setHapticPlaybackEnabled(bool hapticPlaybackEnabled) { 160 mHapticPlaybackEnabled = hapticPlaybackEnabled; 161 } 162 /** Return at what intensity to play haptics, used in mixer. */ getHapticIntensity()163 os::HapticScale getHapticIntensity() const { return mHapticIntensity; } 164 /** Set intensity of haptic playback, should be set after querying vibrator service. */ setHapticIntensity(os::HapticScale hapticIntensity)165 void setHapticIntensity(os::HapticScale hapticIntensity) { 166 if (os::isValidHapticScale(hapticIntensity)) { 167 mHapticIntensity = hapticIntensity; 168 setHapticPlaybackEnabled(mHapticIntensity != os::HapticScale::MUTE); 169 } 170 } getExternalVibration()171 sp<os::ExternalVibration> getExternalVibration() const { return mExternalVibration; } 172 173 void setTeePatches(TeePatches teePatches); 174 tallyUnderrunFrames(size_t frames)175 void tallyUnderrunFrames(size_t frames) override { 176 if (isOut()) { // we expect this from output tracks only 177 mAudioTrackServerProxy->tallyUnderrunFrames(frames); 178 // Fetch absolute numbers from AudioTrackShared as it counts 179 // contiguous underruns as a one -- we want a consistent number. 180 // TODO: isolate this counting into a class. 181 mTrackMetrics.logUnderruns(mAudioTrackServerProxy->getUnderrunCount(), 182 mAudioTrackServerProxy->getUnderrunFrames()); 183 } 184 } 185 getOutputFlags()186 audio_output_flags_t getOutputFlags() const { return mFlags; } getSpeed()187 float getSpeed() const { return mSpeed; } 188 protected: 189 // for numerous 190 friend class PlaybackThread; 191 friend class MixerThread; 192 friend class DirectOutputThread; 193 friend class OffloadThread; 194 195 DISALLOW_COPY_AND_ASSIGN(Track); 196 197 // AudioBufferProvider interface 198 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override; 199 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override; 200 201 // ExtendedAudioBufferProvider interface 202 virtual size_t framesReady() const; 203 virtual int64_t framesReleased() const; 204 virtual void onTimestamp(const ExtendedTimestamp ×tamp); 205 isPausing()206 bool isPausing() const { return mState == PAUSING; } isPaused()207 bool isPaused() const { return mState == PAUSED; } isResuming()208 bool isResuming() const { return mState == RESUMING; } 209 bool isReady() const; setPaused()210 void setPaused() { mState = PAUSED; } 211 void reset(); isFlushPending()212 bool isFlushPending() const { return mFlushHwPending; } 213 void flushAck(); 214 bool isResumePending(); 215 void resumeAck(); 216 // For direct or offloaded tracks ensure that the pause state is acknowledged 217 // by the playback thread in case of an immediate flush. isPausePending()218 bool isPausePending() const { return mPauseHwPending; } 219 void pauseAck(); 220 void updateTrackFrameInfo(int64_t trackFramesReleased, int64_t sinkFramesWritten, 221 uint32_t halSampleRate, const ExtendedTimestamp &timeStamp); 222 sharedBuffer()223 sp<IMemory> sharedBuffer() const { return mSharedBuffer; } 224 225 // presentationComplete checked by frames. (Mixed Tracks). 226 // framesWritten is cumulative, never reset, and is shared all tracks 227 // audioHalFrames is derived from output latency 228 bool presentationComplete(int64_t framesWritten, size_t audioHalFrames); 229 230 // presentationComplete checked by time. (Direct Tracks). 231 bool presentationComplete(uint32_t latencyMs); 232 resetPresentationComplete()233 void resetPresentationComplete() { 234 mPresentationCompleteFrames = 0; 235 mPresentationCompleteTimeNs = 0; 236 } 237 238 // notifyPresentationComplete is called when presentationComplete() detects 239 // that the track is finished stopping. 240 void notifyPresentationComplete(); 241 242 void signalClientFlag(int32_t flag); 243 244 public: 245 void triggerEvents(AudioSystem::sync_event_t type); 246 virtual void invalidate(); 247 void disable(); 248 fastIndex()249 int fastIndex() const { return mFastIndex; } 250 isPlaybackRestricted()251 bool isPlaybackRestricted() const { 252 // The monitor is only created for tracks that can be silenced. 253 return mOpPlayAudioMonitor ? !mOpPlayAudioMonitor->hasOpPlayAudio() : false; } 254 255 protected: 256 257 // FILLED state is used for suppressing volume ramp at begin of playing 258 enum {FS_INVALID, FS_FILLING, FS_FILLED, FS_ACTIVE}; 259 mutable uint8_t mFillingUpStatus; 260 int8_t mRetryCount; 261 262 // see comment at AudioFlinger::PlaybackThread::Track::~Track for why this can't be const 263 sp<IMemory> mSharedBuffer; 264 265 bool mResetDone; 266 const audio_stream_type_t mStreamType; 267 effect_buffer_t *mMainBuffer; 268 269 int32_t *mAuxBuffer; 270 int mAuxEffectId; 271 bool mHasVolumeController; 272 273 // access these three variables only when holding thread lock. 274 LinearMap<int64_t> mFrameMap; // track frame to server frame mapping 275 276 ExtendedTimestamp mSinkTimestamp; 277 278 sp<media::VolumeHandler> mVolumeHandler; // handles multiple VolumeShaper configs and operations 279 280 sp<OpPlayAudioMonitor> mOpPlayAudioMonitor; 281 282 bool mHapticPlaybackEnabled = false; // indicates haptic playback enabled or not 283 // intensity to play haptic data 284 os::HapticScale mHapticIntensity = os::HapticScale::MUTE; 285 class AudioVibrationController : public os::BnExternalVibrationController { 286 public: AudioVibrationController(Track * track)287 explicit AudioVibrationController(Track* track) : mTrack(track) {} 288 binder::Status mute(/*out*/ bool *ret) override; 289 binder::Status unmute(/*out*/ bool *ret) override; 290 private: 291 Track* const mTrack; 292 }; 293 sp<AudioVibrationController> mAudioVibrationController; 294 sp<os::ExternalVibration> mExternalVibration; 295 296 audio_dual_mono_mode_t mDualMonoMode = AUDIO_DUAL_MONO_MODE_OFF; 297 float mAudioDescriptionMixLevel = -std::numeric_limits<float>::infinity(); 298 audio_playback_rate_t mPlaybackRateParameters = AUDIO_PLAYBACK_RATE_INITIALIZER; 299 300 private: 301 void interceptBuffer(const AudioBufferProvider::Buffer& buffer); 302 template <class F> forEachTeePatchTrack(F f)303 void forEachTeePatchTrack(F f) { 304 for (auto& tp : mTeePatches) { f(tp.patchTrack); } 305 }; 306 307 size_t mPresentationCompleteFrames = 0; // (Used for Mixed tracks) 308 // The number of frames written to the 309 // audio HAL when this track is considered fully rendered. 310 // Zero means not monitoring. 311 int64_t mPresentationCompleteTimeNs = 0; // (Used for Direct tracks) 312 // The time when this track is considered fully rendered. 313 // Zero means not monitoring. 314 315 // The following fields are only for fast tracks, and should be in a subclass 316 int mFastIndex; // index within FastMixerState::mFastTracks[]; 317 // either mFastIndex == -1 if not isFastTrack() 318 // or 0 < mFastIndex < FastMixerState::kMaxFast because 319 // index 0 is reserved for normal mixer's submix; 320 // index is allocated statically at track creation time 321 // but the slot is only used if track is active 322 FastTrackUnderruns mObservedUnderruns; // Most recently observed value of 323 // mFastMixerDumpState.mTracks[mFastIndex].mUnderruns 324 volatile float mCachedVolume; // combined master volume and stream type volume; 325 // 'volatile' means accessed without lock or 326 // barrier, but is read/written atomically 327 float mFinalVolume; // combine master volume, stream type volume and track volume 328 sp<AudioTrackServerProxy> mAudioTrackServerProxy; 329 bool mResumeToStopping; // track was paused in stopping state. 330 bool mFlushHwPending; // track requests for thread flush 331 bool mPauseHwPending = false; // direct/offload track request for thread pause 332 audio_output_flags_t mFlags; 333 TeePatches mTeePatches; 334 const float mSpeed; 335 }; // end of Track 336 337 338 // playback track, used by DuplicatingThread 339 class OutputTrack : public Track { 340 public: 341 342 class Buffer : public AudioBufferProvider::Buffer { 343 public: 344 void *mBuffer; 345 }; 346 347 OutputTrack(PlaybackThread *thread, 348 DuplicatingThread *sourceThread, 349 uint32_t sampleRate, 350 audio_format_t format, 351 audio_channel_mask_t channelMask, 352 size_t frameCount, 353 const AttributionSourceState& attributionSource); 354 virtual ~OutputTrack(); 355 356 virtual status_t start(AudioSystem::sync_event_t event = 357 AudioSystem::SYNC_EVENT_NONE, 358 audio_session_t triggerSession = AUDIO_SESSION_NONE); 359 virtual void stop(); 360 ssize_t write(void* data, uint32_t frames); bufferQueueEmpty()361 bool bufferQueueEmpty() const { return mBufferQueue.size() == 0; } isActive()362 bool isActive() const { return mActive; } thread()363 const wp<ThreadBase>& thread() const { return mThread; } 364 365 void copyMetadataTo(MetadataInserter& backInserter) const override; 366 /** Set the metadatas of the upstream tracks. Thread safe. */ 367 void setMetadatas(const SourceMetadatas& metadatas); 368 /** returns client timestamp to the upstream duplicating thread. */ getClientProxyTimestamp()369 ExtendedTimestamp getClientProxyTimestamp() const { 370 // server - kernel difference is not true latency when drained 371 // i.e. mServerProxy->isDrained(). 372 ExtendedTimestamp timestamp; 373 (void) mClientProxy->getTimestamp(×tamp); 374 // On success, the timestamp LOCATION_SERVER and LOCATION_KERNEL 375 // entries will be properly filled. If getTimestamp() 376 // is unsuccessful, then a default initialized timestamp 377 // (with mTimeNs[] filled with -1's) is returned. 378 return timestamp; 379 } 380 381 private: 382 status_t obtainBuffer(AudioBufferProvider::Buffer* buffer, 383 uint32_t waitTimeMs); 384 void clearBufferQueue(); 385 386 void restartIfDisabled(); 387 388 // Maximum number of pending buffers allocated by OutputTrack::write() 389 static const uint8_t kMaxOverFlowBuffers = 10; 390 391 Vector < Buffer* > mBufferQueue; 392 AudioBufferProvider::Buffer mOutBuffer; 393 bool mActive; 394 DuplicatingThread* const mSourceThread; // for waitTimeMs() in write() 395 sp<AudioTrackClientProxy> mClientProxy; 396 397 /** Attributes of the source tracks. 398 * 399 * This member must be accessed with mTrackMetadatasMutex taken. 400 * There is one writer (duplicating thread) and one reader (downstream mixer). 401 * 402 * That means that the duplicating thread can block the downstream mixer 403 * thread and vice versa for the time of the copy. 404 * If this becomes an issue, the metadata could be stored in an atomic raw pointer, 405 * and a exchange with nullptr and delete can be used. 406 * Alternatively a read-copy-update might be implemented. 407 */ 408 SourceMetadatas mTrackMetadatas; 409 /** Protects mTrackMetadatas against concurrent access. */ 410 mutable std::mutex mTrackMetadatasMutex; 411 }; // end of OutputTrack 412 413 // playback track, used by PatchPanel 414 class PatchTrack : public Track, public PatchTrackBase { 415 public: 416 417 PatchTrack(PlaybackThread *playbackThread, 418 audio_stream_type_t streamType, 419 uint32_t sampleRate, 420 audio_channel_mask_t channelMask, 421 audio_format_t format, 422 size_t frameCount, 423 void *buffer, 424 size_t bufferSize, 425 audio_output_flags_t flags, 426 const Timeout& timeout = {}, 427 size_t frameCountToBeReady = 1 /** Default behaviour is to start 428 * as soon as possible to have 429 * the lowest possible latency 430 * even if it might glitch. */); 431 virtual ~PatchTrack(); 432 433 size_t framesReady() const override; 434 435 virtual status_t start(AudioSystem::sync_event_t event = 436 AudioSystem::SYNC_EVENT_NONE, 437 audio_session_t triggerSession = AUDIO_SESSION_NONE); 438 439 // AudioBufferProvider interface 440 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); 441 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); 442 443 // PatchProxyBufferProvider interface 444 virtual status_t obtainBuffer(Proxy::Buffer* buffer, 445 const struct timespec *timeOut = NULL); 446 virtual void releaseBuffer(Proxy::Buffer* buffer); 447 448 private: 449 void restartIfDisabled(); 450 }; // end of PatchTrack 451