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