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