1// Copyright 2013 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
5#include "base/time/time.h"
6#include "media/base/audio_converter.h"
7#include "media/base/fake_audio_render_callback.h"
8#include "testing/gtest/include/gtest/gtest.h"
9#include "testing/perf/perf_test.h"
10
11namespace media {
12
13static const int kBenchmarkIterations = 200000;
14
15// InputCallback that zero's out the provided AudioBus.
16class NullInputProvider : public AudioConverter::InputCallback {
17 public:
18  NullInputProvider() {}
19  virtual ~NullInputProvider() {}
20
21  virtual double ProvideInput(AudioBus* audio_bus,
22                              base::TimeDelta buffer_delay) OVERRIDE {
23    audio_bus->Zero();
24    return 1;
25  }
26};
27
28void RunConvertBenchmark(const AudioParameters& in_params,
29                         const AudioParameters& out_params,
30                         bool fifo,
31                         const std::string& trace_name) {
32  NullInputProvider fake_input1;
33  NullInputProvider fake_input2;
34  NullInputProvider fake_input3;
35  scoped_ptr<AudioBus> output_bus = AudioBus::Create(out_params);
36
37  AudioConverter converter(in_params, out_params, !fifo);
38  converter.AddInput(&fake_input1);
39  converter.AddInput(&fake_input2);
40  converter.AddInput(&fake_input3);
41
42  base::TimeTicks start = base::TimeTicks::HighResNow();
43  for (int i = 0; i < kBenchmarkIterations; ++i) {
44    converter.Convert(output_bus.get());
45  }
46  double runs_per_second = kBenchmarkIterations /
47                           (base::TimeTicks::HighResNow() - start).InSecondsF();
48  perf_test::PrintResult(
49      "audio_converter", "", trace_name, runs_per_second, "runs/s", true);
50}
51
52TEST(AudioConverterPerfTest, ConvertBenchmark) {
53  // Create input and output parameters to convert between the two most common
54  // sets of parameters (as indicated via UMA data).
55  AudioParameters input_params(
56      AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, 48000, 16, 2048);
57  AudioParameters output_params(
58      AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 44100, 16, 440);
59
60  RunConvertBenchmark(input_params, output_params, false, "convert");
61}
62
63TEST(AudioConverterPerfTest, ConvertBenchmarkFIFO) {
64  // Create input and output parameters to convert between common buffer sizes
65  // without any resampling for the FIFO vs no FIFO benchmarks.
66  AudioParameters input_params(AudioParameters::AUDIO_PCM_LINEAR,
67                               CHANNEL_LAYOUT_STEREO,
68                               44100,
69                               16,
70                               2048);
71  AudioParameters output_params(
72      AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 44100, 16, 440);
73
74  RunConvertBenchmark(input_params, output_params, true, "convert_fifo_only");
75  RunConvertBenchmark(input_params, output_params, false,
76                      "convert_pass_through");
77}
78
79} // namespace media
80