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

Unified Diff: webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory_unittest.cc

Issue 2695243005: Injectable audio encoders: BuiltinAudioEncoderFactory (Closed)
Patch Set: Cleaned up Opus code a bit. kHz -> Hz in comment picked a bunch of lint Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory_unittest.cc
diff --git a/webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory_unittest.cc b/webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..79d5a996c80eefdea857740d4f379c5ece417d75
--- /dev/null
+++ b/webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory_unittest.cc
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <memory>
+
+#include "webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.h"
+#include "webrtc/test/gtest.h"
+
+namespace webrtc {
+
+class AudioEncoderFactoryTest
+ : public ::testing::TestWithParam<rtc::scoped_refptr<AudioEncoderFactory>> {
+};
+
+TEST_P(AudioEncoderFactoryTest, SupportsAtLeastOneFormat) {
+ auto factory = GetParam();
+ auto supported_encoders = factory->GetSupportedEncoders();
+ EXPECT_FALSE(supported_encoders.empty());
+}
+
+TEST_P(AudioEncoderFactoryTest, CanQueryAllSupportedFormats) {
+ auto factory = GetParam();
+ auto supported_encoders = factory->GetSupportedEncoders();
+ for (const auto& spec : supported_encoders) {
+ auto info = factory->QueryAudioEncoder(spec.format);
+ EXPECT_TRUE(info);
+ }
+}
+
+TEST_P(AudioEncoderFactoryTest, CanConstructAllSupportedEncoders) {
+ auto factory = GetParam();
+ auto supported_encoders = factory->GetSupportedEncoders();
+ for (const auto& spec : supported_encoders) {
+ auto info = factory->QueryAudioEncoder(spec.format);
+ auto encoder = factory->MakeAudioEncoder(127, spec.format);
+ EXPECT_TRUE(encoder);
+ EXPECT_EQ(encoder->SampleRateHz(), info->sample_rate_hz);
+ EXPECT_EQ(static_cast<int>(encoder->NumChannels()), info->num_channels);
+ EXPECT_EQ(encoder->RtpTimestampRateHz(), spec.format.clockrate_hz);
+ }
+}
+
+TEST_P(AudioEncoderFactoryTest, CanRunAllSupportedEncoders) {
+ constexpr int kTestPayloadType = 127;
+ auto factory = GetParam();
+ auto supported_encoders = factory->GetSupportedEncoders();
+ for (const auto& spec : supported_encoders) {
+ auto encoder = factory->MakeAudioEncoder(kTestPayloadType, spec.format);
+ EXPECT_TRUE(encoder);
+ encoder->Reset();
+ const int num_samples =
+ encoder->SampleRateHz() * encoder->NumChannels() / 100;
+ rtc::Buffer out;
+ rtc::BufferT<int16_t> audio;
+ audio.SetData(num_samples, [](rtc::ArrayView<int16_t> audio) {
+ for (size_t i = 0; i != audio.size(); ++i) {
+ // Just put some numbers in there, allow for wrap-around.
+ 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.
+ }
+ return audio.size();
+ });
+ // This is here to stop the test going forever with a broken encoder.
+ constexpr int kMaxEncodeCalls = 100;
+ int blocks = 0;
+ for (; blocks < kMaxEncodeCalls; ++blocks) {
+ AudioEncoder::EncodedInfo info = encoder->Encode(
+ blocks * encoder->RtpTimestampRateHz() / 100, audio, &out);
+ EXPECT_EQ(info.encoded_bytes, out.size());
+ if (info.encoded_bytes > 0) {
+ EXPECT_EQ(0u, info.encoded_timestamp);
+ EXPECT_EQ(kTestPayloadType, info.payload_type);
+ break;
+ }
+ }
+ ASSERT_LT(blocks, kMaxEncodeCalls);
+ const unsigned int next_timestamp =
+ blocks * encoder->RtpTimestampRateHz() / 100;
+ out.Clear();
+ for (; blocks < kMaxEncodeCalls; ++blocks) {
+ AudioEncoder::EncodedInfo info = encoder->Encode(
+ blocks * encoder->RtpTimestampRateHz() / 100, audio, &out);
+ EXPECT_EQ(info.encoded_bytes, out.size());
+ if (info.encoded_bytes > 0) {
+ EXPECT_EQ(next_timestamp, info.encoded_timestamp);
+ EXPECT_EQ(kTestPayloadType, info.payload_type);
+ break;
+ }
+ }
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
+ ASSERT_LT(blocks, kMaxEncodeCalls);
+ }
+}
+
+INSTANTIATE_TEST_CASE_P(
+ BuiltinAudioEncoderFactoryTest,
+ AudioEncoderFactoryTest,
+ ::testing::Values(CreateBuiltinAudioEncoderFactory()));
+
+TEST(BuiltinAudioEncoderFactoryTest, SupportsTheExpectedFormats) {
+ // Check that we claim to support the formats we expect from build flags, and
+ // we've ordered them correctly.
+ auto factory = CreateBuiltinAudioEncoderFactory();
+ auto specs = factory->GetSupportedEncoders();
+ auto iter = specs.begin();
+ auto next_is_format = [&] (const SdpAudioFormat& format) {
+ if (iter != specs.end()) {
+ const bool found = iter->format == format;
+ ++iter;
+ return found;
+ }
+ return false;
+ };
+#ifdef WEBRTC_CODEC_OPUS
+ ASSERT_TRUE(next_is_format(
+ {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}}));
+#endif
+#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
+ ASSERT_TRUE(next_is_format({"isac", 16000, 1}));
+#endif
+#ifdef WEBRTC_CODEC_ISAC
+ ASSERT_TRUE(next_is_format({"isac", 32000, 1}));
+#endif
+#ifdef WEBRTC_CODEC_G722
+ ASSERT_TRUE(next_is_format({"G722", 8000, 1}));
+#endif
+#ifdef WEBRTC_CODEC_ILBC
+ ASSERT_TRUE(next_is_format({"ilbc", 8000, 1}));
+#endif
+ ASSERT_TRUE(next_is_format({"pcmu", 8000, 1}));
+ 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!
+}
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698