Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1018)

Side by Side Diff: webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory_unittest.cc

Issue 2695243005: Injectable audio encoders: BuiltinAudioEncoderFactory (Closed)
Patch Set: Fix build problems on Windows, Android and downstream. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 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 <limits>
12 #include <memory>
13 #include <vector>
14
15 #include "webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.h"
16 #include "webrtc/test/gmock.h"
17 #include "webrtc/test/gtest.h"
18
19 namespace webrtc {
20
21 class AudioEncoderFactoryTest
22 : public ::testing::TestWithParam<rtc::scoped_refptr<AudioEncoderFactory>> {
23 };
24
25 TEST_P(AudioEncoderFactoryTest, SupportsAtLeastOneFormat) {
26 auto factory = GetParam();
27 auto supported_encoders = factory->GetSupportedEncoders();
28 EXPECT_FALSE(supported_encoders.empty());
29 }
30
31 TEST_P(AudioEncoderFactoryTest, CanQueryAllSupportedFormats) {
32 auto factory = GetParam();
33 auto supported_encoders = factory->GetSupportedEncoders();
34 for (const auto& spec : supported_encoders) {
35 auto info = factory->QueryAudioEncoder(spec.format);
36 EXPECT_TRUE(info);
37 }
38 }
39
40 TEST_P(AudioEncoderFactoryTest, CanConstructAllSupportedEncoders) {
41 auto factory = GetParam();
42 auto supported_encoders = factory->GetSupportedEncoders();
43 for (const auto& spec : supported_encoders) {
44 auto info = factory->QueryAudioEncoder(spec.format);
45 auto encoder = factory->MakeAudioEncoder(127, spec.format);
46 EXPECT_TRUE(encoder);
47 EXPECT_EQ(encoder->SampleRateHz(), info->sample_rate_hz);
48 EXPECT_EQ(encoder->NumChannels(), info->num_channels);
49 EXPECT_EQ(encoder->RtpTimestampRateHz(), spec.format.clockrate_hz);
50 }
51 }
52
53 TEST_P(AudioEncoderFactoryTest, CanRunAllSupportedEncoders) {
54 constexpr int kTestPayloadType = 127;
55 auto factory = GetParam();
56 auto supported_encoders = factory->GetSupportedEncoders();
57 for (const auto& spec : supported_encoders) {
58 auto encoder = factory->MakeAudioEncoder(kTestPayloadType, spec.format);
59 EXPECT_TRUE(encoder);
60 encoder->Reset();
61 const int num_samples =
62 encoder->SampleRateHz() * encoder->NumChannels() / 100;
63 rtc::Buffer out;
64 rtc::BufferT<int16_t> audio;
65 audio.SetData(num_samples, [](rtc::ArrayView<int16_t> audio) {
66 for (size_t i = 0; i != audio.size(); ++i) {
67 // Just put some numbers in there, ensure they're within range.
68 audio[i] =
69 static_cast<int16_t>(i & std::numeric_limits<int16_t>::max());
70 }
71 return audio.size();
72 });
73 // This is here to stop the test going forever with a broken encoder.
74 constexpr int kMaxEncodeCalls = 100;
75 int blocks = 0;
76 for (; blocks < kMaxEncodeCalls; ++blocks) {
77 AudioEncoder::EncodedInfo info = encoder->Encode(
78 blocks * encoder->RtpTimestampRateHz() / 100, audio, &out);
79 EXPECT_EQ(info.encoded_bytes, out.size());
80 if (info.encoded_bytes > 0) {
81 EXPECT_EQ(0u, info.encoded_timestamp);
82 EXPECT_EQ(kTestPayloadType, info.payload_type);
83 break;
84 }
85 }
86 ASSERT_LT(blocks, kMaxEncodeCalls);
87 const unsigned int next_timestamp =
88 blocks * encoder->RtpTimestampRateHz() / 100;
89 out.Clear();
90 for (; blocks < kMaxEncodeCalls; ++blocks) {
91 AudioEncoder::EncodedInfo info = encoder->Encode(
92 blocks * encoder->RtpTimestampRateHz() / 100, audio, &out);
93 EXPECT_EQ(info.encoded_bytes, out.size());
94 if (info.encoded_bytes > 0) {
95 EXPECT_EQ(next_timestamp, info.encoded_timestamp);
96 EXPECT_EQ(kTestPayloadType, info.payload_type);
97 break;
98 }
99 }
100 ASSERT_LT(blocks, kMaxEncodeCalls);
101 }
102 }
103
104 INSTANTIATE_TEST_CASE_P(BuiltinAudioEncoderFactoryTest,
105 AudioEncoderFactoryTest,
106 ::testing::Values(CreateBuiltinAudioEncoderFactory()));
107
108 TEST(BuiltinAudioEncoderFactoryTest, SupportsTheExpectedFormats) {
109 using ::testing::ElementsAreArray;
110 // Check that we claim to support the formats we expect from build flags, and
111 // we've ordered them correctly.
112 auto factory = CreateBuiltinAudioEncoderFactory();
113 auto specs = factory->GetSupportedEncoders();
114
115 const std::vector<SdpAudioFormat> supported_formats = [&specs] {
116 std::vector<SdpAudioFormat> formats;
117 for (const auto& spec : specs) {
118 formats.push_back(spec.format);
119 }
120 return formats;
121 }();
122
123 const std::vector<SdpAudioFormat> expected_formats = {
124 #ifdef WEBRTC_CODEC_OPUS
125 {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}},
126 #endif
127 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
128 {"isac", 16000, 1},
129 #endif
130 #ifdef WEBRTC_CODEC_ISAC
131 {"isac", 32000, 1},
132 #endif
133 #ifdef WEBRTC_CODEC_G722
134 {"G722", 8000, 1},
135 #endif
136 #ifdef WEBRTC_CODEC_ILBC
137 {"ilbc", 8000, 1},
138 #endif
139 {"pcmu", 8000, 1},
140 {"pcma", 8000, 1}
141 };
142
143 ASSERT_THAT(supported_formats, ElementsAreArray(expected_formats));
144 }
145 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698