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