OLD | NEW |
---|---|
(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 <memory> | |
12 | |
13 #include "webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.h" | |
14 #include "webrtc/test/gtest.h" | |
15 | |
16 namespace webrtc { | |
17 | |
18 class AudioEncoderFactoryTest | |
19 : public ::testing::TestWithParam<rtc::scoped_refptr<AudioEncoderFactory>> { | |
20 }; | |
21 | |
22 TEST_P(AudioEncoderFactoryTest, SupportsAtLeastOneFormat) { | |
23 auto factory = GetParam(); | |
24 auto supported_encoders = factory->GetSupportedEncoders(); | |
25 EXPECT_FALSE(supported_encoders.empty()); | |
26 } | |
27 | |
28 TEST_P(AudioEncoderFactoryTest, CanQueryAllSupportedFormats) { | |
29 auto factory = GetParam(); | |
30 auto supported_encoders = factory->GetSupportedEncoders(); | |
31 for (const auto& spec : supported_encoders) { | |
32 auto info = factory->QueryAudioEncoder(spec.format); | |
33 EXPECT_TRUE(info); | |
34 } | |
35 } | |
36 | |
37 TEST_P(AudioEncoderFactoryTest, CanConstructAllSupportedEncoders) { | |
38 auto factory = GetParam(); | |
39 auto supported_encoders = factory->GetSupportedEncoders(); | |
40 for (const auto& spec : supported_encoders) { | |
41 auto info = factory->QueryAudioEncoder(spec.format); | |
42 auto encoder = factory->MakeAudioEncoder(127, spec.format); | |
43 EXPECT_TRUE(encoder); | |
44 EXPECT_EQ(encoder->SampleRateHz(), info->sample_rate_hz); | |
45 EXPECT_EQ(static_cast<int>(encoder->NumChannels()), info->num_channels); | |
46 EXPECT_EQ(encoder->RtpTimestampRateHz(), spec.format.clockrate_hz); | |
47 } | |
48 } | |
49 | |
50 TEST_P(AudioEncoderFactoryTest, CanRunAllSupportedEncoders) { | |
51 constexpr int kTestPayloadType = 127; | |
52 auto factory = GetParam(); | |
53 auto supported_encoders = factory->GetSupportedEncoders(); | |
54 for (const auto& spec : supported_encoders) { | |
55 auto encoder = factory->MakeAudioEncoder(kTestPayloadType, spec.format); | |
56 EXPECT_TRUE(encoder); | |
57 encoder->Reset(); | |
58 const int num_samples = | |
59 encoder->SampleRateHz() * encoder->NumChannels() / 100; | |
60 rtc::Buffer out; | |
61 rtc::BufferT<int16_t> audio; | |
62 audio.SetData(num_samples, [](rtc::ArrayView<int16_t> audio) { | |
63 for (size_t i = 0; i != audio.size(); ++i) { | |
64 // Just put some numbers in there, allow for wrap-around. | |
65 audio[i] = static_cast<int16_t>(i); | |
kwiberg-webrtc
2017/03/24 12:43:25
Overflow when casting from unsigned to signed is i
ossu
2017/04/05 14:41:52
Acknowledged.
| |
66 } | |
67 return audio.size(); | |
68 }); | |
69 // This is here to stop the test going forever with a broken encoder. | |
70 constexpr int kMaxEncodeCalls = 100; | |
71 int blocks = 0; | |
72 for (; blocks < kMaxEncodeCalls; ++blocks) { | |
73 AudioEncoder::EncodedInfo info = encoder->Encode( | |
74 blocks * encoder->RtpTimestampRateHz() / 100, audio, &out); | |
75 EXPECT_EQ(info.encoded_bytes, out.size()); | |
76 if (info.encoded_bytes > 0) { | |
77 EXPECT_EQ(0u, info.encoded_timestamp); | |
78 EXPECT_EQ(kTestPayloadType, info.payload_type); | |
79 break; | |
80 } | |
81 } | |
82 ASSERT_LT(blocks, kMaxEncodeCalls); | |
83 const unsigned int next_timestamp = | |
84 blocks * encoder->RtpTimestampRateHz() / 100; | |
85 out.Clear(); | |
86 for (; blocks < kMaxEncodeCalls; ++blocks) { | |
87 AudioEncoder::EncodedInfo info = encoder->Encode( | |
88 blocks * encoder->RtpTimestampRateHz() / 100, audio, &out); | |
89 EXPECT_EQ(info.encoded_bytes, out.size()); | |
90 if (info.encoded_bytes > 0) { | |
91 EXPECT_EQ(next_timestamp, info.encoded_timestamp); | |
92 EXPECT_EQ(kTestPayloadType, info.payload_type); | |
93 break; | |
94 } | |
95 } | |
kwiberg-webrtc
2017/03/24 12:43:25
You never test that the encoder actually returns a
ossu
2017/04/05 14:41:52
I was thinking this check did just that, though th
| |
96 ASSERT_LT(blocks, kMaxEncodeCalls); | |
97 } | |
98 } | |
99 | |
100 INSTANTIATE_TEST_CASE_P( | |
101 BuiltinAudioEncoderFactoryTest, | |
102 AudioEncoderFactoryTest, | |
103 ::testing::Values(CreateBuiltinAudioEncoderFactory())); | |
104 | |
105 TEST(BuiltinAudioEncoderFactoryTest, SupportsTheExpectedFormats) { | |
106 // Check that we claim to support the formats we expect from build flags, and | |
107 // we've ordered them correctly. | |
108 auto factory = CreateBuiltinAudioEncoderFactory(); | |
109 auto specs = factory->GetSupportedEncoders(); | |
110 auto iter = specs.begin(); | |
111 auto next_is_format = [&] (const SdpAudioFormat& format) { | |
112 if (iter != specs.end()) { | |
113 const bool found = iter->format == format; | |
114 ++iter; | |
115 return found; | |
116 } | |
117 return false; | |
118 }; | |
119 #ifdef WEBRTC_CODEC_OPUS | |
120 ASSERT_TRUE(next_is_format( | |
121 {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}})); | |
122 #endif | |
123 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) | |
124 ASSERT_TRUE(next_is_format({"isac", 16000, 1})); | |
125 #endif | |
126 #ifdef WEBRTC_CODEC_ISAC | |
127 ASSERT_TRUE(next_is_format({"isac", 32000, 1})); | |
128 #endif | |
129 #ifdef WEBRTC_CODEC_G722 | |
130 ASSERT_TRUE(next_is_format({"G722", 8000, 1})); | |
131 #endif | |
132 #ifdef WEBRTC_CODEC_ILBC | |
133 ASSERT_TRUE(next_is_format({"ilbc", 8000, 1})); | |
134 #endif | |
135 ASSERT_TRUE(next_is_format({"pcmu", 8000, 1})); | |
136 ASSERT_TRUE(next_is_format({"pcma", 8000, 1})); | |
kwiberg-webrtc
2017/03/24 12:43:25
Can you use ASSERT_THAT with ElementsAre() or some
ossu
2017/04/05 14:41:52
I can! And it gives a neat printout when it fails!
| |
137 } | |
138 } // namespace webrtc | |
OLD | NEW |