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/memory/ref_counted.h"
10 #include "base/time/time.h"
11 #include "media/base/audio_buffer.h"
12 #include "media/base/audio_decoder_config.h"
13 #include "media/base/audio_timestamp_helper.h"
14 #include "media/base/decoder_buffer.h"
15 #include "media/base/media_log.h"
16 #include "media/base/timestamp_constants.h"
20 class MEDIA_EXPORT AudioTimestampValidator {
22 AudioTimestampValidator(const AudioDecoderConfig& decoder_config,
25 AudioTimestampValidator(const AudioTimestampValidator&) = delete;
26 AudioTimestampValidator& operator=(const AudioTimestampValidator&) = delete;
28 ~AudioTimestampValidator();
30 // These methods monitor DecoderBuffer timestamps for gaps for the purpose of
31 // warning developers when gaps may cause AV sync drift. A DecoderBuffer's
32 // timestamp should roughly equal the timestamp of the previous buffer offset
33 // by the previous buffer's duration.
34 void CheckForTimestampGap(const DecoderBuffer& buffer);
35 void RecordOutputDuration(const AudioBuffer& buffer);
38 bool has_codec_delay_;
39 raw_ptr<MediaLog> media_log_;
41 // Accumulates time from decoded audio frames. We adjust the base timestamp as
42 // needed for the first few buffers (stabilization period) of decoded output
43 // to account for pre-skip and codec delay. See CheckForTimestampGap().
44 std::unique_ptr<AudioTimestampHelper> audio_output_ts_helper_;
46 base::TimeDelta audio_base_ts_;
48 // Initially false, set to true when we observe gap between encoded timestamps
49 // match gap between output decoder buffers.
50 bool reached_stable_state_;
52 // Counts attempts to adjust |audio_output_ts_helper_| base offset in effort
53 // to form expectation for encoded timestamps based on decoded output. Give up
54 // making adjustments when count exceeds |limit_unstable_audio_tries_|.
55 int num_unstable_audio_tries_;
57 // Limits the number of attempts to stabilize audio timestamp expectations.
58 int limit_unstable_audio_tries_;
60 // How many milliseconds can DecoderBuffer timestamps differ from expectations
61 // before we MEDIA_LOG warn developers. Threshold initially set from
62 // kGapWarningThresholdMsec. Once hit, the threshold is increased by
63 // the detected gap amount. This avoids log spam while still emitting
64 // logs if things get worse. See CheckTimestampForGap().
65 uint32_t drift_warning_threshold_msec_;
67 // Tracks the number of MEDIA_LOG warnings when large timestamp gap detected.
68 int num_timestamp_gap_warnings_ = 0;
73 #endif // MEDIA_FILTERS_AUDIO_TIMESTAMP_VALIDATOR_H_