audio_timestamp_validator.h 2.8 KB

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