1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_FILTERS_AUDIO_TIMESTAMP_VALIDATOR_H_
6 #define MEDIA_FILTERS_AUDIO_TIMESTAMP_VALIDATOR_H_
8 #include "base/memory/raw_ptr.h"
9 #include "base/time/time.h"
10 #include "media/base/audio_buffer.h"
11 #include "media/base/audio_decoder_config.h"
12 #include "media/base/audio_timestamp_helper.h"
13 #include "media/base/decoder_buffer.h"
14 #include "media/base/media_log.h"
15 #include "media/base/timestamp_constants.h"
19 class MEDIA_EXPORT AudioTimestampValidator {
21 AudioTimestampValidator(const AudioDecoderConfig& decoder_config,
24 AudioTimestampValidator(const AudioTimestampValidator&) = delete;
25 AudioTimestampValidator& operator=(const AudioTimestampValidator&) = delete;
27 ~AudioTimestampValidator();
29 // These methods monitor DecoderBuffer timestamps for gaps for the purpose of
30 // warning developers when gaps may cause AV sync drift. A DecoderBuffer's
31 // timestamp should roughly equal the timestamp of the previous buffer offset
32 // by the previous buffer's duration.
33 void CheckForTimestampGap(const DecoderBuffer& buffer);
34 void RecordOutputDuration(const AudioBuffer& buffer);
37 bool has_codec_delay_;
38 raw_ptr<MediaLog> media_log_;
40 // Accumulates time from decoded audio frames. We adjust the base timestamp as
41 // needed for the first few buffers (stabilization period) of decoded output
42 // to account for pre-skip and codec delay. See CheckForTimestampGap().
43 std::unique_ptr<AudioTimestampHelper> audio_output_ts_helper_;
45 base::TimeDelta audio_base_ts_;
47 // Initially false, set to true when we observe gap between encoded timestamps
48 // match gap between output decoder buffers.
49 bool reached_stable_state_;
51 // Counts attempts to adjust |audio_output_ts_helper_| base offset in effort
52 // to form expectation for encoded timestamps based on decoded output. Give up
53 // making adjustments when count exceeds |limit_unstable_audio_tries_|.
54 int num_unstable_audio_tries_;
56 // Limits the number of attempts to stabilize audio timestamp expectations.
57 int limit_unstable_audio_tries_;
59 // How many milliseconds can DecoderBuffer timestamps differ from expectations
60 // before we MEDIA_LOG warn developers. Threshold initially set from
61 // kGapWarningThresholdMsec. Once hit, the threshold is increased by
62 // the detected gap amount. This avoids log spam while still emitting
63 // logs if things get worse. See CheckTimestampForGap().
64 uint32_t drift_warning_threshold_msec_;
66 // Tracks the number of MEDIA_LOG warnings when large timestamp gap detected.
67 int num_timestamp_gap_warnings_ = 0;
72 #endif // MEDIA_FILTERS_AUDIO_TIMESTAMP_VALIDATOR_H_