1 /* 2 * Copyright 2021 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 PLAYBACK_DURATION_ACCUMULATOR_H_ 18 19 namespace android { 20 21 // Accumulates playback duration by processing render times of individual frames and by ignoring 22 // frames rendered during inactive playbacks such as seeking, pausing, or re-buffering. 23 class PlaybackDurationAccumulator { 24 private: 25 // Controls the maximum delta between render times before considering the playback is not 26 // active and has stalled. 27 static const int64_t MAX_PRESENTATION_DURATION_NS = 500 * 1000 * 1000; 28 29 public: PlaybackDurationAccumulator()30 PlaybackDurationAccumulator() { 31 mPlaybackDurationNs = 0; 32 mPreviousRenderTimeNs = 0; 33 } 34 35 // Process a render time expressed in nanoseconds. onFrameRendered(int64_t newRenderTimeNs)36 void onFrameRendered(int64_t newRenderTimeNs) { 37 // If we detect wrap-around or out of order frames, just ignore the duration for this 38 // and the next frame. 39 if (newRenderTimeNs < mPreviousRenderTimeNs) { 40 mPreviousRenderTimeNs = 0; 41 } 42 if (mPreviousRenderTimeNs > 0) { 43 int64_t presentationDurationNs = newRenderTimeNs - mPreviousRenderTimeNs; 44 if (presentationDurationNs < MAX_PRESENTATION_DURATION_NS) { 45 mPlaybackDurationNs += presentationDurationNs; 46 } 47 } 48 mPreviousRenderTimeNs = newRenderTimeNs; 49 } 50 getDurationInSeconds()51 int64_t getDurationInSeconds() { 52 return mPlaybackDurationNs / 1000 / 1000 / 1000; // Nanoseconds to seconds. 53 } 54 55 private: 56 // The playback duration accumulated so far. 57 int64_t mPlaybackDurationNs; 58 // The previous render time used to compute the next presentation duration. 59 int64_t mPreviousRenderTimeNs; 60 }; 61 62 } // android 63 64 #endif // PLAYBACK_DURATION_ACCUMULATOR_H_ 65 66