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