1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_AUDIO_FAST_MIXER_DUMP_STATE_H
18 #define ANDROID_AUDIO_FAST_MIXER_DUMP_STATE_H
19 
20 #include <stdint.h>
21 #include <audio_utils/TimestampVerifier.h>
22 #include "Configuration.h"
23 #include "FastThreadDumpState.h"
24 #include "FastMixerState.h"
25 
26 namespace android {
27 
28 // Describes the underrun status for a single "pull" attempt
29 enum FastTrackUnderrunStatus {
30     UNDERRUN_FULL,      // framesReady() is full frame count, no underrun
31     UNDERRUN_PARTIAL,   // framesReady() is non-zero but < full frame count, partial underrun
32     UNDERRUN_EMPTY,     // framesReady() is zero, total underrun
33 };
34 
35 // Underrun counters are not reset to zero for new tracks or if track generation changes.
36 // This packed representation is used to keep the information atomic.
37 union FastTrackUnderruns {
FastTrackUnderruns()38     FastTrackUnderruns() { mAtomic = 0;
39             static_assert(sizeof(FastTrackUnderruns) == sizeof(uint32_t), "FastTrackUnderrun"); }
FastTrackUnderruns(const FastTrackUnderruns & copyFrom)40     FastTrackUnderruns(const FastTrackUnderruns& copyFrom) : mAtomic(copyFrom.mAtomic) { }
41     FastTrackUnderruns& operator=(const FastTrackUnderruns& rhs)
42             { if (this != &rhs) mAtomic = rhs.mAtomic; return *this; }
43     struct {
44 #define UNDERRUN_BITS 10
45 #define UNDERRUN_MASK ((1 << UNDERRUN_BITS) - 1)
46         uint32_t mFull    : UNDERRUN_BITS; // framesReady() is full frame count
47         uint32_t mPartial : UNDERRUN_BITS; // framesReady() is non-zero but < full frame count
48         uint32_t mEmpty   : UNDERRUN_BITS; // framesReady() is zero
49         FastTrackUnderrunStatus mMostRecent : 2;    // status of most recent framesReady()
50     }        mBitFields;
51 private:
52     uint32_t mAtomic;
53 };
54 
55 // Represents the dump state of a fast track
56 struct FastTrackDump {
FastTrackDumpFastTrackDump57     FastTrackDump() : mFramesReady(0) { }
~FastTrackDumpFastTrackDump58     /*virtual*/ ~FastTrackDump() { }
59     FastTrackUnderruns  mUnderruns;
60     size_t              mFramesReady;        // most recent value only; no long-term statistics kept
61     int64_t             mFramesWritten;      // last value from track
62 };
63 
64 struct FastMixerDumpState : FastThreadDumpState {
65     FastMixerDumpState();
66     /*virtual*/ ~FastMixerDumpState();
67 
68     void dump(int fd) const;    // should only be called on a stable copy, not the original
69 
70     double   mLatencyMs = 0.;   // measured latency, default of 0 if no valid timestamp read.
71     uint32_t mWriteSequence;    // incremented before and after each write()
72     uint32_t mFramesWritten;    // total number of frames written successfully
73     uint32_t mNumTracks;        // total number of active fast tracks
74     uint32_t mWriteErrors;      // total number of write() errors
75     uint32_t mSampleRate;
76     size_t   mFrameCount;
77     uint32_t mTrackMask;        // mask of active tracks
78     FastTrackDump   mTracks[FastMixerState::kMaxFastTracks];
79 
80     // For timestamp statistics.
81     TimestampVerifier<int64_t /* frame count */, int64_t /* time ns */> mTimestampVerifier;
82 };
83 
84 }   // android
85 
86 #endif  // ANDROID_AUDIO_FAST_MIXER_DUMP_STATE_H
87