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 // base for record and playback
23 class TrackBase : public ExtendedAudioBufferProvider, public RefBase {
24 
25 public:
26     enum track_state : int32_t {
27         IDLE,
28         FLUSHED,        // for PlaybackTracks only
29         STOPPED,
30         // next 2 states are currently used for fast tracks
31         // and offloaded tracks only
32         STOPPING_1,     // waiting for first underrun
33         STOPPING_2,     // waiting for presentation complete
34         RESUMING,       // for PlaybackTracks only
35         ACTIVE,
36         PAUSING,
37         PAUSED,
38         STARTING_1,     // for RecordTrack only
39         STARTING_2,     // for RecordTrack only
40     };
41 
42     // where to allocate the data buffer
43     enum alloc_type {
44         ALLOC_CBLK,     // allocate immediately after control block
45         ALLOC_READONLY, // allocate from a separate read-only heap per thread
46         ALLOC_PIPE,     // do not allocate; use the pipe buffer
47         ALLOC_LOCAL,    // allocate a local buffer
48         ALLOC_NONE,     // do not allocate:use the buffer passed to TrackBase constructor
49     };
50 
51     enum track_type {
52         TYPE_DEFAULT,
53         TYPE_OUTPUT,
54         TYPE_PATCH,
55     };
56 
57                         TrackBase(ThreadBase *thread,
58                                 const sp<Client>& client,
59                                 const audio_attributes_t& mAttr,
60                                 uint32_t sampleRate,
61                                 audio_format_t format,
62                                 audio_channel_mask_t channelMask,
63                                 size_t frameCount,
64                                 void *buffer,
65                                 size_t bufferSize,
66                                 audio_session_t sessionId,
67                                 pid_t creatorPid,
68                                 uid_t uid,
69                                 bool isOut,
70                                 alloc_type alloc = ALLOC_CBLK,
71                                 track_type type = TYPE_DEFAULT,
72                                 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE,
73                                 std::string metricsId = {});
74     virtual             ~TrackBase();
75     virtual status_t    initCheck() const;
76 
77     virtual status_t    start(AudioSystem::sync_event_t event,
78                              audio_session_t triggerSession) = 0;
79     virtual void        stop() = 0;
getCblk()80             sp<IMemory> getCblk() const { return mCblkMemory; }
cblk()81             audio_track_cblk_t* cblk() const { return mCblk; }
sessionId()82             audio_session_t sessionId() const { return mSessionId; }
uid()83             uid_t       uid() const { return mUid; }
creatorPid()84             pid_t       creatorPid() const { return mCreatorPid; }
85 
portId()86             audio_port_handle_t portId() const { return mPortId; }
87     virtual status_t    setSyncEvent(const sp<SyncEvent>& event);
88 
getBuffers()89             sp<IMemory> getBuffers() const { return mBufferMemory; }
buffer()90             void*       buffer() const { return mBuffer; }
bufferSize()91             size_t      bufferSize() const { return mBufferSize; }
92     virtual bool        isFastTrack() const = 0;
93     virtual bool        isDirect() const = 0;
isOutputTrack()94             bool        isOutputTrack() const { return (mType == TYPE_OUTPUT); }
isPatchTrack()95             bool        isPatchTrack() const { return (mType == TYPE_PATCH); }
isExternalTrack()96             bool        isExternalTrack() const { return !isOutputTrack() && !isPatchTrack(); }
97 
invalidate()98     virtual void        invalidate() {
99                             if (mIsInvalid) return;
100                             mTrackMetrics.logInvalidate();
101                             mIsInvalid = true;
102                         }
isInvalid()103             bool        isInvalid() const { return mIsInvalid; }
104 
terminate()105             void        terminate() { mTerminated = true; }
isTerminated()106             bool        isTerminated() const { return mTerminated; }
107 
attributes()108     audio_attributes_t  attributes() const { return mAttr; }
109 
canBeSpatialized()110             bool canBeSpatialized() const { return mIsOut && (mAttr.flags
111                     & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) == 0; }
112 
113 #ifdef TEE_SINK
dumpTee(int fd,const std::string & reason)114            void         dumpTee(int fd, const std::string &reason) const {
115                                 mTee.dump(fd, reason);
116                         }
117 #endif
118 
119             /** returns the buffer contents size converted to time in milliseconds
120              * for PCM Playback or Record streaming tracks. The return value is zero for
121              * PCM static tracks and not defined for non-PCM tracks.
122              *
123              * This may be called without the thread lock.
124              */
bufferLatencyMs()125     virtual double      bufferLatencyMs() const {
126                             return mServerProxy->framesReadySafe() * 1000 / sampleRate();
127                         }
128 
129             /** returns whether the track supports server latency computation.
130              * This is set in the constructor and constant throughout the track lifetime.
131              */
132 
isServerLatencySupported()133             bool        isServerLatencySupported() const { return mServerLatencySupported; }
134 
135             /** computes the server latency for PCM Playback or Record track
136              * to the device sink/source.  This is the time for the next frame in the track buffer
137              * written or read from the server thread to the device source or sink.
138              *
139              * This may be called without the thread lock, but latencyMs and fromTrack
140              * may be not be synchronized. For example PatchPanel may not obtain the
141              * thread lock before calling.
142              *
143              * \param latencyMs on success is set to the latency in milliseconds of the
144              *        next frame written/read by the server thread to/from the track buffer
145              *        from the device source/sink.
146              * \param fromTrack on success is set to true if latency was computed directly
147              *        from the track timestamp; otherwise set to false if latency was
148              *        estimated from the server timestamp.
149              *        fromTrack may be nullptr or omitted if not required.
150              *
151              * \returns OK or INVALID_OPERATION on failure.
152              */
153             status_t    getServerLatencyMs(double *latencyMs, bool *fromTrack = nullptr) const {
154                             if (!isServerLatencySupported()) {
155                                 return INVALID_OPERATION;
156                             }
157 
158                             // if no thread lock is acquired, these atomics are not
159                             // synchronized with each other, considered a benign race.
160 
161                             const double serverLatencyMs = mServerLatencyMs.load();
162                             if (serverLatencyMs == 0.) {
163                                 return INVALID_OPERATION;
164                             }
165                             if (fromTrack != nullptr) {
166                                 *fromTrack = mServerLatencyFromTrack.load();
167                             }
168                             *latencyMs = serverLatencyMs;
169                             return OK;
170                         }
171 
172             /** computes the total client latency for PCM Playback or Record tracks
173              * for the next client app access to the device sink/source; i.e. the
174              * server latency plus the buffer latency.
175              *
176              * This may be called without the thread lock, but latencyMs and fromTrack
177              * may be not be synchronized. For example PatchPanel may not obtain the
178              * thread lock before calling.
179              *
180              * \param latencyMs on success is set to the latency in milliseconds of the
181              *        next frame written/read by the client app to/from the track buffer
182              *        from the device sink/source.
183              * \param fromTrack on success is set to true if latency was computed directly
184              *        from the track timestamp; otherwise set to false if latency was
185              *        estimated from the server timestamp.
186              *        fromTrack may be nullptr or omitted if not required.
187              *
188              * \returns OK or INVALID_OPERATION on failure.
189              */
190             status_t    getTrackLatencyMs(double *latencyMs, bool *fromTrack = nullptr) const {
191                             double serverLatencyMs;
192                             status_t status = getServerLatencyMs(&serverLatencyMs, fromTrack);
193                             if (status == OK) {
194                                 *latencyMs = serverLatencyMs + bufferLatencyMs();
195                             }
196                             return status;
197                         }
198 
199            // TODO: Consider making this external.
200            struct FrameTime {
201                int64_t frames;
202                int64_t timeNs;
203            };
204 
205            // KernelFrameTime is updated per "mix" period even for non-pcm tracks.
getKernelFrameTime(FrameTime * ft)206            void         getKernelFrameTime(FrameTime *ft) const {
207                            *ft = mKernelFrameTime.load();
208                         }
209 
format()210            audio_format_t format() const { return mFormat; }
id()211            int id() const { return mId; }
212 
getTrackStateAsString()213     const char *getTrackStateAsString() const {
214         if (isTerminated()) {
215             return "TERMINATED";
216         }
217         switch (mState) {
218         case IDLE:
219             return "IDLE";
220         case STOPPING_1: // for Fast and Offload
221             return "STOPPING_1";
222         case STOPPING_2: // for Fast and Offload
223             return "STOPPING_2";
224         case STOPPED:
225             return "STOPPED";
226         case RESUMING:
227             return "RESUMING";
228         case ACTIVE:
229             return "ACTIVE";
230         case PAUSING:
231             return "PAUSING";
232         case PAUSED:
233             return "PAUSED";
234         case FLUSHED:
235             return "FLUSHED";
236         case STARTING_1: // for RecordTrack
237             return "STARTING_1";
238         case STARTING_2: // for RecordTrack
239             return "STARTING_2";
240         default:
241             return "UNKNOWN";
242         }
243     }
244 
245     // Called by the PlaybackThread to indicate that the track is becoming active
246     // and a new interval should start with a given device list.
logBeginInterval(const std::string & devices)247     void logBeginInterval(const std::string& devices) {
248         mTrackMetrics.logBeginInterval(devices);
249     }
250 
251     // Called by the PlaybackThread to indicate the track is no longer active.
logEndInterval()252     void logEndInterval() {
253         mTrackMetrics.logEndInterval();
254     }
255 
256     // Called to tally underrun frames in playback.
tallyUnderrunFrames(size_t)257     virtual void tallyUnderrunFrames(size_t /* frames */) {}
258 
channelMask()259     audio_channel_mask_t channelMask() const { return mChannelMask; }
260 
261     /** @return true if the track has changed (metadata or volume) since
262      *          the last time this function was called,
263      *          true if this function was never called since the track creation,
264      *          false otherwise.
265      *  Thread safe.
266      */
readAndClearHasChanged()267     bool readAndClearHasChanged() { return !mChangeNotified.test_and_set(); }
268 
269     /** Set that a metadata has changed and needs to be notified to backend. Thread safe. */
setMetadataHasChanged()270     void setMetadataHasChanged() { mChangeNotified.clear(); }
271 
272 protected:
273     DISALLOW_COPY_AND_ASSIGN(TrackBase);
274 
releaseCblk()275     void releaseCblk() {
276         if (mCblk != nullptr) {
277             mState.clear();
278             mCblk->~audio_track_cblk_t();   // destroy our shared-structure.
279             if (mClient == 0) {
280                 free(mCblk);
281             }
282             mCblk = nullptr;
283         }
284     }
285 
286     // AudioBufferProvider interface
287     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0;
288     virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
289 
290     // ExtendedAudioBufferProvider interface is only needed for Track,
291     // but putting it in TrackBase avoids the complexity of virtual inheritance
framesReady()292     virtual size_t  framesReady() const { return SIZE_MAX; }
293 
channelCount()294     uint32_t channelCount() const { return mChannelCount; }
295 
frameSize()296     size_t frameSize() const { return mFrameSize; }
297 
sampleRate()298     virtual uint32_t sampleRate() const { return mSampleRate; }
299 
isStopped()300     bool isStopped() const {
301         return (mState == STOPPED || mState == FLUSHED);
302     }
303 
304     // for fast tracks and offloaded tracks only
isStopping()305     bool isStopping() const {
306         return mState == STOPPING_1 || mState == STOPPING_2;
307     }
isStopping_1()308     bool isStopping_1() const {
309         return mState == STOPPING_1;
310     }
isStopping_2()311     bool isStopping_2() const {
312         return mState == STOPPING_2;
313     }
314 
315     // Upper case characters are final states.
316     // Lower case characters are transitory.
getTrackStateAsCodedString()317     const char *getTrackStateAsCodedString() const {
318         if (isTerminated()) {
319             return "T ";
320         }
321         switch (mState) {
322         case IDLE:
323             return "I ";
324         case STOPPING_1: // for Fast and Offload
325             return "s1";
326         case STOPPING_2: // for Fast and Offload
327             return "s2";
328         case STOPPED:
329             return "S ";
330         case RESUMING:
331             return "r ";
332         case ACTIVE:
333             return "A ";
334         case PAUSING:
335             return "p ";
336         case PAUSED:
337             return "P ";
338         case FLUSHED:
339             return "F ";
340         case STARTING_1: // for RecordTrack
341             return "r1";
342         case STARTING_2: // for RecordTrack
343             return "r2";
344         default:
345             return "? ";
346         }
347     }
348 
isOut()349     bool isOut() const { return mIsOut; }
350                                     // true for Track, false for RecordTrack,
351                                     // this could be a track type if needed later
352 
353     const wp<ThreadBase> mThread;
354     /*const*/ sp<Client> mClient;   // see explanation at ~TrackBase() why not const
355     sp<IMemory>         mCblkMemory;
356     audio_track_cblk_t* mCblk;
357     sp<IMemory>         mBufferMemory;  // currently non-0 for fast RecordTrack only
358     void*               mBuffer;    // start of track buffer, typically in shared memory
359                                     // except for OutputTrack when it is in local memory
360     size_t              mBufferSize; // size of mBuffer in bytes
361     // we don't really need a lock for these
362     MirroredVariable<track_state>  mState;
363     const audio_attributes_t mAttr;
364     const uint32_t      mSampleRate;    // initial sample rate only; for tracks which
365                         // support dynamic rates, the current value is in control block
366     const audio_format_t mFormat;
367     const audio_channel_mask_t mChannelMask;
368     const uint32_t      mChannelCount;
369     const size_t        mFrameSize; // AudioFlinger's view of frame size in shared memory,
370                                     // where for AudioTrack (but not AudioRecord),
371                                     // 8-bit PCM samples are stored as 16-bit
372     const size_t        mFrameCount;// size of track buffer given at createTrack() or
373                                     // createRecord(), and then adjusted as needed
374 
375     const audio_session_t mSessionId;
376     uid_t               mUid;
377     Vector < sp<SyncEvent> >mSyncEvents;
378     const bool          mIsOut;
379     sp<ServerProxy>     mServerProxy;
380     const int           mId;
381 #ifdef TEE_SINK
382     NBAIO_Tee           mTee;
383 #endif
384     bool                mTerminated;
385     track_type          mType;      // must be one of TYPE_DEFAULT, TYPE_OUTPUT, TYPE_PATCH ...
386     audio_io_handle_t   mThreadIoHandle; // I/O handle of the thread the track is attached to
387     audio_port_handle_t mPortId; // unique ID for this track used by audio policy
388     bool                mIsInvalid; // non-resettable latch, set by invalidate()
389 
390     // It typically takes 5 threadloop mix iterations for latency to stabilize.
391     // However, this can be 12+ iterations for BT.
392     // To be sure, we wait for latency to dip (it usually increases at the start)
393     // to assess stability and then log to MediaMetrics.
394     // Rapid start / pause calls may cause inaccurate numbers.
395     static inline constexpr int32_t LOG_START_COUNTDOWN = 12;
396     int32_t             mLogStartCountdown = 0; // Mixer period countdown
397     int64_t             mLogStartTimeNs = 0;    // Monotonic time at start()
398     int64_t             mLogStartFrames = 0;    // Timestamp frames at start()
399     double              mLogLatencyMs = 0.;     // Track the last log latency
400 
401     TrackMetrics        mTrackMetrics;
402 
403     bool                mServerLatencySupported = false;
404     std::atomic<bool>   mServerLatencyFromTrack{}; // latency from track or server timestamp.
405     std::atomic<double> mServerLatencyMs{};        // last latency pushed from server thread.
406     std::atomic<FrameTime> mKernelFrameTime{};     // last frame time on kernel side.
407     const pid_t         mCreatorPid;  // can be different from mclient->pid() for instance
408                                       // when created by NuPlayer on behalf of a client
409 
410     // If the last track change was notified to the client with readAndClearHasChanged
411     std::atomic_flag    mChangeNotified = ATOMIC_FLAG_INIT;
412 };
413 
414 // PatchProxyBufferProvider interface is implemented by PatchTrack and PatchRecord.
415 // it provides buffer access methods that map those of a ClientProxy (see AudioTrackShared.h)
416 class PatchProxyBufferProvider
417 {
418 public:
419 
~PatchProxyBufferProvider()420     virtual ~PatchProxyBufferProvider() {}
421 
422     virtual bool        producesBufferOnDemand() const = 0;
423     virtual status_t    obtainBuffer(Proxy::Buffer* buffer,
424                                      const struct timespec *requested = NULL) = 0;
425     virtual void        releaseBuffer(Proxy::Buffer* buffer) = 0;
426 };
427 
428 class PatchTrackBase : public PatchProxyBufferProvider
429 {
430 public:
431     using Timeout = std::optional<std::chrono::nanoseconds>;
432                         PatchTrackBase(sp<ClientProxy> proxy, const ThreadBase& thread,
433                                        const Timeout& timeout);
434             void        setPeerTimeout(std::chrono::nanoseconds timeout);
435             template <typename T>
setPeerProxy(const sp<T> & proxy,bool holdReference)436             void        setPeerProxy(const sp<T> &proxy, bool holdReference) {
437                             mPeerReferenceHold = holdReference ? proxy : nullptr;
438                             mPeerProxy = proxy.get();
439                         }
clearPeerProxy()440             void        clearPeerProxy() {
441                             mPeerReferenceHold.clear();
442                             mPeerProxy = nullptr;
443                         }
444 
producesBufferOnDemand()445             bool        producesBufferOnDemand() const override { return false; }
446 
447 protected:
448     const sp<ClientProxy>       mProxy;
449     sp<RefBase>                 mPeerReferenceHold;   // keeps mPeerProxy alive during access.
450     PatchProxyBufferProvider*   mPeerProxy = nullptr;
451     struct timespec             mPeerTimeout{};
452 
453 };
454