| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 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 | |
| Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 17 | #pragma once |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 18 | |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 19 | #include <stdint.h> |
| Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 20 | #include <type_traits> |
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 21 | #include <audio_utils/TimestampVerifier.h> |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 22 | #include "Configuration.h" |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 23 | #include "FastThreadDumpState.h" |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 24 | #include "FastMixerState.h" |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 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 { |
| 38 | FastTrackUnderruns() { mAtomic = 0; |
| Glenn Kasten | 91164e7 | 2016-03-15 15:55:01 -0700 | [diff] [blame] | 39 | static_assert(sizeof(FastTrackUnderruns) == sizeof(uint32_t), "FastTrackUnderrun"); } |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 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 { |
| Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 57 | FastTrackUnderruns mUnderruns; |
| Andy Hung | fd18f3c | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 58 | size_t mFramesReady = 0; // most recent value only; no long-term statistics kept |
| Andy Hung | a9b9adc | 2023-06-01 12:17:59 -0700 | [diff] [blame] | 59 | int64_t mFramesWritten = 0; // last value from track |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 62 | // No virtuals. |
| 63 | static_assert(!std::is_polymorphic_v<FastTrackDump>); |
| 64 | |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 65 | struct FastMixerDumpState : FastThreadDumpState { |
| Eric Tan | 2942a4e | 2018-09-12 17:41:27 -0700 | [diff] [blame] | 66 | void dump(int fd) const; // should only be called on a stable copy, not the original |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 67 | |
| Andy Hung | fd18f3c | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 68 | double mLatencyMs = 0.; // measured latency, default of 0 if no valid timestamp read. |
| 69 | uint32_t mWriteSequence = 0; // incremented before and after each write() |
| 70 | uint32_t mFramesWritten = 0; // total number of frames written successfully |
| 71 | uint32_t mNumTracks = 0; // total number of active fast tracks |
| 72 | uint32_t mWriteErrors = 0; // total number of write() errors |
| 73 | uint32_t mSampleRate = 0; |
| 74 | size_t mFrameCount = 0; |
| 75 | uint32_t mTrackMask = 0; // mask of active tracks |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 76 | FastTrackDump mTracks[FastMixerState::kMaxFastTracks]; |
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 77 | |
| 78 | // For timestamp statistics. |
| 79 | TimestampVerifier<int64_t /* frame count */, int64_t /* time ns */> mTimestampVerifier; |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 82 | // No virtuals. |
| 83 | static_assert(!std::is_polymorphic_v<FastMixerDumpState>); |
| Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 84 | |
| Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 85 | } // namespace android |