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