summaryrefslogtreecommitdiffstats
path: root/media/base/audio_converter_perftest.cc
blob: c6a475864b71ef251aa66a5dc19dc5528ecb7a2f (plain)
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/time/time.h"
#include "media/base/audio_converter.h"
#include "media/base/fake_audio_render_callback.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_test.h"

namespace media {

static const int kBenchmarkIterations = 200000;

// InputCallback that zero's out the provided AudioBus.
class NullInputProvider : public AudioConverter::InputCallback {
 public:
  NullInputProvider() {}
  ~NullInputProvider() override {}

  double ProvideInput(AudioBus* audio_bus,
                      base::TimeDelta buffer_delay) override {
    audio_bus->Zero();
    return 1;
  }
};

void RunConvertBenchmark(const AudioParameters& in_params,
                         const AudioParameters& out_params,
                         bool fifo,
                         const std::string& trace_name) {
  NullInputProvider fake_input1;
  NullInputProvider fake_input2;
  NullInputProvider fake_input3;
  scoped_ptr<AudioBus> output_bus = AudioBus::Create(out_params);

  AudioConverter converter(in_params, out_params, !fifo);
  converter.AddInput(&fake_input1);
  converter.AddInput(&fake_input2);
  converter.AddInput(&fake_input3);

  base::TimeTicks start = base::TimeTicks::HighResNow();
  for (int i = 0; i < kBenchmarkIterations; ++i) {
    converter.Convert(output_bus.get());
  }
  double runs_per_second = kBenchmarkIterations /
                           (base::TimeTicks::HighResNow() - start).InSecondsF();
  perf_test::PrintResult(
      "audio_converter", "", trace_name, runs_per_second, "runs/s", true);
}

TEST(AudioConverterPerfTest, ConvertBenchmark) {
  // Create input and output parameters to convert between the two most common
  // sets of parameters (as indicated via UMA data).
  AudioParameters input_params(
      AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, 48000, 16, 2048);
  AudioParameters output_params(
      AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 44100, 16, 440);

  RunConvertBenchmark(input_params, output_params, false, "convert");
}

TEST(AudioConverterPerfTest, ConvertBenchmarkFIFO) {
  // Create input and output parameters to convert between common buffer sizes
  // without any resampling for the FIFO vs no FIFO benchmarks.
  AudioParameters input_params(AudioParameters::AUDIO_PCM_LINEAR,
                               CHANNEL_LAYOUT_STEREO,
                               44100,
                               16,
                               2048);
  AudioParameters output_params(
      AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 44100, 16, 440);

  RunConvertBenchmark(input_params, output_params, true, "convert_fifo_only");
  RunConvertBenchmark(input_params, output_params, false,
                      "convert_pass_through");
}

} // namespace media