1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259

media / filters / audio_timestamp_validator_unittest.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/filters/audio_timestamp_validator.h"

#include <array>
#include <tuple>

#include "base/time/time.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/media_util.h"
#include "media/base/mock_media_log.h"
#include "media/base/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::HasSubstr;

namespace media {

// Constants to specify the type of audio data used.
static const AudioCodec kCodec = AudioCodec::kVorbis;
static const SampleFormat kSampleFormat = kSampleFormatPlanarF32;
static const base::TimeDelta kSeekPreroll;
static const int kSamplesPerSecond = 10000;
static const base::TimeDelta kBufferDuration = base::Milliseconds(20);
static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
static const int kChannelCount = 2;
static const int kChannels = ChannelLayoutToChannelCount(kChannelLayout);
static const int kFramesPerBuffer = kBufferDuration.InMicroseconds() *
                                    kSamplesPerSecond /
                                    base::Time::kMicrosecondsPerSecond;

// Params are:
// 1. Output delay: number of encoded buffers before first decoded output
// 2. Codec delay: number of frames of codec delay in decoder config
// 3. Front discard: front discard for the first buffer
using ValidatorTestParams = testing::tuple<int, int, base::TimeDelta>;

class AudioTimestampValidatorTest
    : public testing::Test,
      public ::testing::WithParamInterface<ValidatorTestParams> {
 public:
  AudioTimestampValidatorTest() = default;

 protected:
  void SetUp() override {
    output_delay_ = testing::get<0>(GetParam());
    codec_delay_ = testing::get<1>(GetParam());
    front_discard_ = testing::get<2>(GetParam());
  }

  int output_delay_;

  int codec_delay_;

  base::TimeDelta front_discard_;

  testing::StrictMock<MockMediaLog> media_log_;
};

TEST_P(AudioTimestampValidatorTest, WarnForEraticTimes) {
  AudioDecoderConfig decoder_config;
  decoder_config.Initialize(kCodec, kSampleFormat, kChannelLayout,
                            kSamplesPerSecond, EmptyExtraData(),
                            EncryptionScheme::kUnencrypted, kSeekPreroll,
                            codec_delay_);

  // Validator should fail to stabilize pattern for timestamp expectations.
  EXPECT_MEDIA_LOG(
      HasSubstr("Failed to reconcile encoded audio times "
                "with decoded output."));

  // No gap warnings should be emitted because the timestamps expectations never
  // stabilized.
  EXPECT_MEDIA_LOG(HasSubstr("timestamp gap detected")).Times(0);

  AudioTimestampValidator validator(decoder_config, &media_log_);

  const auto kRandomOffsets = std::to_array<base::TimeDelta>(
      {base::Milliseconds(100), base::Milliseconds(350)});

  for (int i = 0; i < 100; ++i) {
    // Each buffer's timestamp is kBufferDuration from the previous buffer.
    auto encoded_buffer = base::MakeRefCounted<DecoderBuffer>(0);

    // Ping-pong between two random offsets to prevent validator from
    // stabilizing timestamp pattern.
    base::TimeDelta randomOffset =
        kRandomOffsets[i % std::size(kRandomOffsets)];
    encoded_buffer->set_timestamp(i * kBufferDuration + randomOffset);

    if (i == 0) {
      encoded_buffer->set_discard_padding(
          std::make_pair(front_discard_, base::TimeDelta()));
    }

    validator.CheckForTimestampGap(*encoded_buffer);

    if (i >= output_delay_) {
      // kFramesPerBuffer is derived to perfectly match kBufferDuration, so
      // no gaps exists as long as timestamps are exactly kBufferDuration apart.
      scoped_refptr<AudioBuffer> decoded_buffer = MakeAudioBuffer<float>(
          kSampleFormat, kChannelLayout, kChannelCount, kSamplesPerSecond, 1.0f,
          0.0f, kFramesPerBuffer, i * kBufferDuration);
      validator.RecordOutputDuration(*decoded_buffer);
    }
  }
}

TEST_P(AudioTimestampValidatorTest, NoWarningForValidTimes) {
  AudioDecoderConfig decoder_config;
  decoder_config.Initialize(kCodec, kSampleFormat, kChannelLayout,
                            kSamplesPerSecond, EmptyExtraData(),
                            EncryptionScheme::kUnencrypted, kSeekPreroll,
                            codec_delay_);

  // Validator should quickly stabilize pattern for timestamp expectations.
  EXPECT_MEDIA_LOG(HasSubstr("Failed to reconcile encoded audio times "
                             "with decoded output."))
      .Times(0);

  // Expect no gap warnings for series of buffers with valid timestamps.
  EXPECT_MEDIA_LOG(HasSubstr("timestamp gap detected")).Times(0);

  AudioTimestampValidator validator(decoder_config, &media_log_);

  for (int i = 0; i < 100; ++i) {
    // Each buffer's timestamp is kBufferDuration from the previous buffer.
    auto encoded_buffer = base::MakeRefCounted<DecoderBuffer>(0);
    encoded_buffer->set_timestamp(i * kBufferDuration);

    if (i == 0) {
      encoded_buffer->set_discard_padding(
          std::make_pair(front_discard_, base::TimeDelta()));
    }

    validator.CheckForTimestampGap(*encoded_buffer);

    if (i >= output_delay_) {
      // kFramesPerBuffer is derived to perfectly match kBufferDuration, so
      // no gaps exists as long as timestamps are exactly kBufferDuration apart.
      scoped_refptr<AudioBuffer> decoded_buffer = MakeAudioBuffer<float>(
          kSampleFormat, kChannelLayout, kChannelCount, kSamplesPerSecond, 1.0f,
          0.0f, kFramesPerBuffer, i * kBufferDuration);
      validator.RecordOutputDuration(*decoded_buffer);
    }
  }
}

TEST_P(AudioTimestampValidatorTest, SingleWarnForSingleLargeGap) {
  AudioDecoderConfig decoder_config;
  decoder_config.Initialize(kCodec, kSampleFormat, kChannelLayout,
                            kSamplesPerSecond, EmptyExtraData(),
                            EncryptionScheme::kUnencrypted, kSeekPreroll,
                            codec_delay_);

  AudioTimestampValidator validator(decoder_config, &media_log_);

  // Validator should quickly stabilize pattern for timestamp expectations.
  EXPECT_MEDIA_LOG(HasSubstr("Failed to reconcile encoded audio times "
                             "with decoded output."))
      .Times(0);

  for (int i = 0; i < 100; ++i) {
    // Halfway through the stream, introduce sudden gap of 50 milliseconds.
    base::TimeDelta offset;
    if (i >= 50)
      offset = base::Milliseconds(100);

    // This gap never widens, so expect only a single warning when its first
    // introduced.
    if (i == 50)
      EXPECT_MEDIA_LOG(HasSubstr("timestamp gap detected"));

    auto encoded_buffer = base::MakeRefCounted<DecoderBuffer>(0);
    encoded_buffer->set_timestamp(i * kBufferDuration + offset);

    if (i == 0) {
      encoded_buffer->set_discard_padding(
          std::make_pair(front_discard_, base::TimeDelta()));
    }

    validator.CheckForTimestampGap(*encoded_buffer);

    if (i >= output_delay_) {
      // kFramesPerBuffer is derived to perfectly match kBufferDuration, so
      // no gaps exists as long as timestamps are exactly kBufferDuration apart.
      scoped_refptr<AudioBuffer> decoded_buffer = MakeAudioBuffer<float>(
          kSampleFormat, kChannelLayout, kChannelCount, kSamplesPerSecond, 1.0f,
          0.0f, kFramesPerBuffer, i * kBufferDuration);
      validator.RecordOutputDuration(*decoded_buffer);
    }
  }
}

TEST_P(AudioTimestampValidatorTest, RepeatedWarnForSlowAccumulatingDrift) {
  AudioDecoderConfig decoder_config;
  decoder_config.Initialize(kCodec, kSampleFormat, kChannelLayout,
                            kSamplesPerSecond, EmptyExtraData(),
                            EncryptionScheme::kUnencrypted, kSeekPreroll,
                            codec_delay_);

  AudioTimestampValidator validator(decoder_config, &media_log_);

  EXPECT_MEDIA_LOG(HasSubstr("Failed to reconcile encoded audio times "
                             "with decoded output."))
      .Times(0);

  int num_timestamp_gap_warnings = 0;
  const int kMaxTimestampGapWarnings = 10;  // Must be the same as in .cc

  for (int i = 0; i < 100; ++i) {
    // Wait for delayed output to begin plus an additional two iterations to
    // start using drift offset. The the two iterations without offset will
    // allow the validator to stabilize the pattern of timestamps and begin
    // checking for gaps. Once stable, increase offset by 1 millisecond for each
    // iteration.
    base::TimeDelta offset;
    if (i >= output_delay_ + 2)
      offset = i * base::Milliseconds(1);

    auto encoded_buffer = base::MakeRefCounted<DecoderBuffer>(0);
    encoded_buffer->set_timestamp((i * kBufferDuration) + offset);

    // Expect gap warnings to start when drift hits 50 milliseconds. Warnings
    // should continue as the gap widens until log limit is hit.

    if (offset > base::Milliseconds(50)) {
      EXPECT_LIMITED_MEDIA_LOG(HasSubstr("timestamp gap detected"),
                               num_timestamp_gap_warnings,
                               kMaxTimestampGapWarnings);
    }

    validator.CheckForTimestampGap(*encoded_buffer);

    if (i >= output_delay_) {
      // kFramesPerBuffer is derived to perfectly match kBufferDuration, so
      // no gaps exists as long as timestamps are exactly kBufferDuration apart.
      scoped_refptr<AudioBuffer> decoded_buffer = MakeAudioBuffer<float>(
          kSampleFormat, kChannelLayout, kChannelCount, kSamplesPerSecond, 1.0f,
          0.0f, kFramesPerBuffer, i * kBufferDuration);
      validator.RecordOutputDuration(*decoded_buffer);
    }
  }
}

// Test with cartesian product of various output delay, codec delay, and front
// discard values. These simulate configurations for different containers/codecs
// which present different challenges when building timestamp expectations.
INSTANTIATE_TEST_SUITE_P(
    All,
    AudioTimestampValidatorTest,
    ::testing::Combine(::testing::Values(0, 10),             // output delay
                       ::testing::Values(0, 512),            // codec delay
                       ::testing::Values(base::TimeDelta(),  // front discard
                                         base::Milliseconds(65))));

}  // namespace media