1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "api/audio_codecs/opus/audio_encoder_opus.h"
12 #include "api/test/metrics/global_metrics_logger_and_exporter.h"
13 #include "api/test/metrics/metric.h"
14 #include "modules/audio_coding/neteq/tools/audio_loop.h"
15 #include "rtc_base/time_utils.h"
16 #include "test/gtest.h"
17 #include "test/testsupport/file_utils.h"
18
19 namespace webrtc {
20 namespace {
21
22 using ::webrtc::test::GetGlobalMetricsLogger;
23 using ::webrtc::test::ImprovementDirection;
24 using ::webrtc::test::Unit;
25
RunComplexityTest(const AudioEncoderOpusConfig & config)26 int64_t RunComplexityTest(const AudioEncoderOpusConfig& config) {
27 // Create encoder.
28 constexpr int payload_type = 17;
29 const auto encoder = AudioEncoderOpus::MakeAudioEncoder(config, payload_type);
30 // Open speech file.
31 const std::string kInputFileName =
32 webrtc::test::ResourcePath("audio_coding/speech_mono_32_48kHz", "pcm");
33 test::AudioLoop audio_loop;
34 constexpr int kSampleRateHz = 48000;
35 EXPECT_EQ(kSampleRateHz, encoder->SampleRateHz());
36 constexpr size_t kMaxLoopLengthSamples =
37 kSampleRateHz * 10; // 10 second loop.
38 constexpr size_t kInputBlockSizeSamples =
39 10 * kSampleRateHz / 1000; // 60 ms.
40 EXPECT_TRUE(audio_loop.Init(kInputFileName, kMaxLoopLengthSamples,
41 kInputBlockSizeSamples));
42 // Encode.
43 const int64_t start_time_ms = rtc::TimeMillis();
44 AudioEncoder::EncodedInfo info;
45 rtc::Buffer encoded(500);
46 uint32_t rtp_timestamp = 0u;
47 for (size_t i = 0; i < 10000; ++i) {
48 encoded.Clear();
49 info = encoder->Encode(rtp_timestamp, audio_loop.GetNextBlock(), &encoded);
50 rtp_timestamp += kInputBlockSizeSamples;
51 }
52 return rtc::TimeMillis() - start_time_ms;
53 }
54
55 // This test encodes an audio file using Opus twice with different bitrates
56 // (~11 kbps and 15.5 kbps). The runtime for each is measured, and the ratio
57 // between the two is calculated and tracked. This test explicitly sets the
58 // low_rate_complexity to 9. When running on desktop platforms, this is the same
59 // as the regular complexity, and the expectation is that the resulting ratio
60 // should be less than 100% (since the encoder runs faster at lower bitrates,
61 // given a fixed complexity setting). On the other hand, when running on
62 // mobiles, the regular complexity is 5, and we expect the resulting ratio to
63 // be higher, since we have explicitly asked for a higher complexity setting at
64 // the lower rate.
TEST(AudioEncoderOpusComplexityAdaptationTest,Adaptation_On)65 TEST(AudioEncoderOpusComplexityAdaptationTest, Adaptation_On) {
66 // Create config.
67 AudioEncoderOpusConfig config;
68 // The limit -- including the hysteresis window -- at which the complexity
69 // shuold be increased.
70 config.bitrate_bps = 11000 - 1;
71 config.low_rate_complexity = 9;
72 int64_t runtime_10999bps = RunComplexityTest(config);
73
74 config.bitrate_bps = 15500;
75 int64_t runtime_15500bps = RunComplexityTest(config);
76
77 GetGlobalMetricsLogger()->LogSingleValueMetric(
78 "opus_encoding_complexity_ratio", "adaptation_on",
79 100.0 * runtime_10999bps / runtime_15500bps, Unit::kPercent,
80 ImprovementDirection::kNeitherIsBetter);
81 }
82
83 // This test is identical to the one above, but without the complexity
84 // adaptation enabled (neither on desktop, nor on mobile). The expectation is
85 // that the resulting ratio is less than 100% at all times.
TEST(AudioEncoderOpusComplexityAdaptationTest,Adaptation_Off)86 TEST(AudioEncoderOpusComplexityAdaptationTest, Adaptation_Off) {
87 // Create config.
88 AudioEncoderOpusConfig config;
89 // The limit -- including the hysteresis window -- at which the complexity
90 // shuold be increased (but not in this test since complexity adaptation is
91 // disabled).
92 config.bitrate_bps = 11000 - 1;
93 int64_t runtime_10999bps = RunComplexityTest(config);
94
95 config.bitrate_bps = 15500;
96 int64_t runtime_15500bps = RunComplexityTest(config);
97
98 GetGlobalMetricsLogger()->LogSingleValueMetric(
99 "opus_encoding_complexity_ratio", "adaptation_off",
100 100.0 * runtime_10999bps / runtime_15500bps, Unit::kPercent,
101 ImprovementDirection::kNeitherIsBetter);
102 }
103
104 } // namespace
105 } // namespace webrtc
106