From 653158338e27faca4dabc9acfe9204bd1e2f2810 Mon Sep 17 00:00:00 2001 From: kwiberg Date: Fri, 16 Jun 2017 10:42:05 -0700 Subject: [PATCH] Templated AudioEncoderFactory No real encoder implements the correct API yet, so we're just testing dummies. BUG=webrtc:7823 Review-Url: https://codereview.webrtc.org/2935643002 Cr-Commit-Position: refs/heads/master@{#18637} --- webrtc/BUILD.gn | 1 + webrtc/api/audio_codecs/BUILD.gn | 1 + .../audio_encoder_factory_template.h | 142 ++++++++++++++++++ webrtc/api/audio_codecs/test/BUILD.gn | 29 ++++ ...audio_encoder_factory_template_unittest.cc | 120 +++++++++++++++ 5 files changed, 293 insertions(+) create mode 100644 webrtc/api/audio_codecs/audio_encoder_factory_template.h create mode 100644 webrtc/api/audio_codecs/test/BUILD.gn create mode 100644 webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc diff --git a/webrtc/BUILD.gn b/webrtc/BUILD.gn index fe6a0d432f..053512e99b 100644 --- a/webrtc/BUILD.gn +++ b/webrtc/BUILD.gn @@ -386,6 +386,7 @@ if (rtc_include_tests) { testonly = true deps = [ "api:rtc_api_unittests", + "api/audio_codecs/test:audio_codecs_api_unittests", "base:rtc_base_approved_unittests", "base:rtc_base_unittests", "base:rtc_numerics_unittests", diff --git a/webrtc/api/audio_codecs/BUILD.gn b/webrtc/api/audio_codecs/BUILD.gn index 0b51db8bcc..ca0cf95b42 100644 --- a/webrtc/api/audio_codecs/BUILD.gn +++ b/webrtc/api/audio_codecs/BUILD.gn @@ -20,6 +20,7 @@ rtc_source_set("audio_codecs_api") { "audio_encoder.cc", "audio_encoder.h", "audio_encoder_factory.h", + "audio_encoder_factory_template.h", "audio_format.cc", "audio_format.h", ] diff --git a/webrtc/api/audio_codecs/audio_encoder_factory_template.h b/webrtc/api/audio_codecs/audio_encoder_factory_template.h new file mode 100644 index 0000000000..a5d5c309a8 --- /dev/null +++ b/webrtc/api/audio_codecs/audio_encoder_factory_template.h @@ -0,0 +1,142 @@ +/* + * 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. + */ + +#ifndef WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ +#define WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ + +#include +#include + +#include "webrtc/api/audio_codecs/audio_encoder_factory.h" +#include "webrtc/base/scoped_ref_ptr.h" + +namespace webrtc { + +namespace audio_encoder_factory_template_impl { + +template +struct Helper; + +// Base case: 0 template parameters. +template <> +struct Helper<> { + static void AppendSupportedEncoders(std::vector* specs) {} + static rtc::Optional QueryAudioEncoder( + const SdpAudioFormat& format) { + return rtc::Optional(); + } + static std::unique_ptr MakeAudioEncoder( + int payload_type, + const SdpAudioFormat& format) { + return nullptr; + } +}; + +// Inductive case: Called with n + 1 template parameters; calls subroutines +// with n template parameters. +template +struct Helper { + static void AppendSupportedEncoders(std::vector* specs) { + T::AppendSupportedEncoders(specs); + Helper::AppendSupportedEncoders(specs); + } + static rtc::Optional QueryAudioEncoder( + const SdpAudioFormat& format) { + auto opt_config = T::SdpToConfig(format); + return opt_config ? rtc::Optional( + T::QueryAudioEncoder(*opt_config)) + : Helper::QueryAudioEncoder(format); + } + static std::unique_ptr MakeAudioEncoder( + int payload_type, + const SdpAudioFormat& format) { + auto opt_config = T::SdpToConfig(format); + if (opt_config) { + return T::MakeAudioEncoder(*opt_config, payload_type); + } else { + return Helper::MakeAudioEncoder(payload_type, format); + } + } +}; + +template +class AudioEncoderFactoryT : public AudioEncoderFactory { + public: + std::vector GetSupportedEncoders() override { + std::vector specs; + Helper::AppendSupportedEncoders(&specs); + return specs; + } + + rtc::Optional QueryAudioEncoder( + const SdpAudioFormat& format) override { + return Helper::QueryAudioEncoder(format); + } + + std::unique_ptr MakeAudioEncoder( + int payload_type, + const SdpAudioFormat& format) override { + return Helper::MakeAudioEncoder(payload_type, format); + } +}; + +} // namespace audio_encoder_factory_template_impl + +// Make an AudioEncoderFactory that can create instances of the given encoders. +// +// Each encoder type is given as a template argument to the function; it should +// be a struct with the following static member functions: +// +// // Converts |audio_format| to a ConfigType instance. Returns an empty +// // optional if |audio_format| doesn't correctly specify an encoder of our +// // type. +// rtc::Optional SdpToConfig(const SdpAudioFormat& audio_format); +// +// // Appends zero or more AudioCodecSpecs to the list that will be returned +// // by AudioEncoderFactory::GetSupportedEncoders(). +// void AppendSupportedEncoders(std::vector* specs); +// +// // Returns information about how this format would be encoded. Used to +// // implement AudioEncoderFactory::QueryAudioEncoder(). +// AudioCodecInfo QueryAudioEncoder(const ConfigType& config); +// +// // Creates an AudioEncoder for the specified format. Used to implement +// // AudioEncoderFactory::MakeAudioEncoder(). +// std::unique_ptr MakeAudioEncoder(const ConfigType& config, +// int payload_type); +// +// ConfigType should be a type that encapsulates all the settings needed to +// create an AudioDecoder. +// +// Whenever it tries to do something, the new factory will try each of the +// encoders in the order they were specified in the template argument list, +// stopping at the first one that claims to be able to do the job. +// +// NOTE: This function is still under development and may change without notice. +// +// TODO(kwiberg): Point at CreateBuiltinAudioEncoderFactory() for an example of +// how it is used. +template +rtc::scoped_refptr CreateAudioEncoderFactory() { + // There's no technical reason we couldn't allow zero template parameters, + // but such a factory couldn't create any encoders, and callers can do this + // by mistake by simply forgetting the <> altogether. So we forbid it in + // order to prevent caller foot-shooting. + static_assert(sizeof...(Ts) >= 1, + "Caller must give at least one template parameter"); + + return rtc::scoped_refptr( + new rtc::RefCountedObject< + audio_encoder_factory_template_impl::AudioEncoderFactoryT>()); +} + +} // namespace webrtc + +#endif // WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ diff --git a/webrtc/api/audio_codecs/test/BUILD.gn b/webrtc/api/audio_codecs/test/BUILD.gn new file mode 100644 index 0000000000..7f2aa399d4 --- /dev/null +++ b/webrtc/api/audio_codecs/test/BUILD.gn @@ -0,0 +1,29 @@ +# 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. + +import("../../../webrtc.gni") +if (is_android) { + import("//build/config/android/config.gni") + import("//build/config/android/rules.gni") +} + +if (rtc_include_tests) { + rtc_source_set("audio_codecs_api_unittests") { + testonly = true + sources = [ + "audio_encoder_factory_template_unittest.cc", + ] + deps = [ + "..:audio_codecs_api", + "../../../base:rtc_base_approved", + "../../../test:audio_codec_mocks", + "../../../test:test_support", + "//testing/gmock", + ] + } +} diff --git a/webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc b/webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc new file mode 100644 index 0000000000..15097b9318 --- /dev/null +++ b/webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc @@ -0,0 +1,120 @@ +/* + * 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 "webrtc/api/audio_codecs/audio_encoder_factory_template.h" +#include "webrtc/base/ptr_util.h" +#include "webrtc/test/gmock.h" +#include "webrtc/test/gtest.h" +#include "webrtc/test/mock_audio_encoder.h" + +namespace webrtc { + +namespace { + +struct BogusParams { + static SdpAudioFormat AudioFormat() { return {"bogus", 8000, 1}; } + static AudioCodecInfo CodecInfo() { return {8000, 1, 12345}; } +}; + +struct ShamParams { + static SdpAudioFormat AudioFormat() { + return {"sham", 16000, 2, {{"param", "value"}}}; + } + static AudioCodecInfo CodecInfo() { return {16000, 2, 23456}; } +}; + +struct MyLittleConfig { + SdpAudioFormat audio_format; +}; + +template +struct AudioEncoderFakeApi { + static rtc::Optional SdpToConfig( + const SdpAudioFormat& audio_format) { + if (Params::AudioFormat() == audio_format) { + MyLittleConfig config = {audio_format}; + return rtc::Optional(config); + } else { + return rtc::Optional(); + } + } + + static void AppendSupportedEncoders(std::vector* specs) { + specs->push_back({Params::AudioFormat(), Params::CodecInfo()}); + } + + static AudioCodecInfo QueryAudioEncoder(const MyLittleConfig&) { + return Params::CodecInfo(); + } + + static std::unique_ptr MakeAudioEncoder(const MyLittleConfig&, + int payload_type) { + auto enc = rtc::MakeUnique>(); + EXPECT_CALL(*enc, SampleRateHz()) + .WillOnce(testing::Return(Params::CodecInfo().sample_rate_hz)); + EXPECT_CALL(*enc, Die()); + return std::move(enc); + } +}; + +} // namespace + +TEST(AudioEncoderFactoryTemplateTest, NoEncoderTypes) { + rtc::scoped_refptr factory( + new rtc::RefCountedObject< + audio_encoder_factory_template_impl::AudioEncoderFactoryT<>>()); + EXPECT_THAT(factory->GetSupportedEncoders(), testing::IsEmpty()); + EXPECT_EQ(rtc::Optional(), + factory->QueryAudioEncoder({"foo", 8000, 1})); + EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"bar", 16000, 1})); +} + +TEST(AudioEncoderFactoryTemplateTest, OneEncoderType) { + auto factory = CreateAudioEncoderFactory>(); + EXPECT_THAT(factory->GetSupportedEncoders(), + testing::ElementsAre( + AudioCodecSpec{{"bogus", 8000, 1}, {8000, 1, 12345}})); + EXPECT_EQ(rtc::Optional(), + factory->QueryAudioEncoder({"foo", 8000, 1})); + EXPECT_EQ(rtc::Optional({8000, 1, 12345}), + factory->QueryAudioEncoder({"bogus", 8000, 1})); + EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"bar", 16000, 1})); + auto enc = factory->MakeAudioEncoder(17, {"bogus", 8000, 1}); + ASSERT_NE(nullptr, enc); + EXPECT_EQ(8000, enc->SampleRateHz()); +} + +TEST(AudioEncoderFactoryTemplateTest, TwoEncoderTypes) { + auto factory = CreateAudioEncoderFactory, + AudioEncoderFakeApi>(); + EXPECT_THAT(factory->GetSupportedEncoders(), + testing::ElementsAre( + AudioCodecSpec{{"bogus", 8000, 1}, {8000, 1, 12345}}, + AudioCodecSpec{{"sham", 16000, 2, {{"param", "value"}}}, + {16000, 2, 23456}})); + EXPECT_EQ(rtc::Optional(), + factory->QueryAudioEncoder({"foo", 8000, 1})); + EXPECT_EQ(rtc::Optional({8000, 1, 12345}), + factory->QueryAudioEncoder({"bogus", 8000, 1})); + EXPECT_EQ( + rtc::Optional({16000, 2, 23456}), + factory->QueryAudioEncoder({"sham", 16000, 2, {{"param", "value"}}})); + EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"bar", 16000, 1})); + auto enc1 = factory->MakeAudioEncoder(17, {"bogus", 8000, 1}); + ASSERT_NE(nullptr, enc1); + EXPECT_EQ(8000, enc1->SampleRateHz()); + EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"sham", 16000, 2})); + auto enc2 = + factory->MakeAudioEncoder(17, {"sham", 16000, 2, {{"param", "value"}}}); + ASSERT_NE(nullptr, enc2); + EXPECT_EQ(16000, enc2->SampleRateHz()); +} + +} // namespace webrtc