diff --git a/webrtc/modules/audio_coding/BUILD.gn b/webrtc/modules/audio_coding/BUILD.gn index 37978181a5..a51a73ac91 100644 --- a/webrtc/modules/audio_coding/BUILD.gn +++ b/webrtc/modules/audio_coding/BUILD.gn @@ -39,6 +39,33 @@ if (!build_with_mozilla && !build_with_chromium) { audio_codec_deps += [ ":red" ] } +source_set("audio_decoder_factory_interface") { + sources = [ + "codecs/audio_decoder_factory.h", + "codecs/audio_format.cc", + "codecs/audio_format.h", + ] + configs += [ "../..:common_config" ] + public_configs = [ "../..:common_inherited_config" ] + deps = [ + "../..:webrtc_common", + ] +} + +source_set("builtin_audio_decoder_factory") { + sources = [ + "codecs/builtin_audio_decoder_factory.cc", + "codecs/builtin_audio_decoder_factory.h", + ] + configs += [ "../..:common_config" ] + public_configs = [ "../..:common_inherited_config" ] + deps = [ + "../..:webrtc_common", + ":audio_decoder_factory_interface", + ] + audio_codec_deps + defines = audio_codec_defines +} + source_set("rent_a_codec") { sources = [ "acm2/acm_codec_database.cc", diff --git a/webrtc/modules/audio_coding/audio_coding.gypi b/webrtc/modules/audio_coding/audio_coding.gypi index e460853ac1..af2cb3c49d 100644 --- a/webrtc/modules/audio_coding/audio_coding.gypi +++ b/webrtc/modules/audio_coding/audio_coding.gypi @@ -68,6 +68,50 @@ 'audio_coding_defines': '<(audio_codec_defines)', }, 'targets': [ + { + 'target_name': 'audio_decoder_factory_interface', + 'type': 'static_library', + 'dependencies': [ + '<(webrtc_root)/common.gyp:webrtc_common', + ], + 'include_dirs': [ + '<(webrtc_root)', + ], + 'direct_dependent_settings': { + 'include_dirs': [ + '<(webrtc_root)', + ], + }, + 'sources': [ + 'codecs/audio_decoder_factory.h', + 'codecs/audio_format.cc', + 'codecs/audio_format.h', + ], + }, + { + 'target_name': 'builtin_audio_decoder_factory', + 'type': 'static_library', + 'defines': [ + '<@(audio_codec_defines)', + ], + 'dependencies': [ + '<(webrtc_root)/common.gyp:webrtc_common', + '<@(audio_codec_dependencies)', + 'audio_decoder_factory_interface', + ], + 'include_dirs': [ + '<(webrtc_root)', + ], + 'direct_dependent_settings': { + 'include_dirs': [ + '<(webrtc_root)', + ], + }, + 'sources': [ + 'codecs/builtin_audio_decoder_factory.cc', + 'codecs/builtin_audio_decoder_factory.h', + ], + }, { 'target_name': 'rent_a_codec', 'type': 'static_library', diff --git a/webrtc/modules/audio_coding/codecs/audio_decoder_factory.h b/webrtc/modules/audio_coding/codecs/audio_decoder_factory.h new file mode 100644 index 0000000000..12b9778091 --- /dev/null +++ b/webrtc/modules/audio_coding/codecs/audio_decoder_factory.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 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_MODULES_AUDIO_CODING_CODECS_AUDIO_DECODER_FACTORY_H_ +#define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_DECODER_FACTORY_H_ + +#include +#include + +#include "webrtc/modules/audio_coding/codecs/audio_decoder.h" +#include "webrtc/modules/audio_coding/codecs/audio_format.h" + +namespace webrtc { + +// A factory that creates AudioDecoders. +// NOTE: This class is still under development and may change without notice. +class AudioDecoderFactory { + public: + virtual ~AudioDecoderFactory() = default; + + virtual std::vector GetSupportedFormats() = 0; + + virtual std::unique_ptr MakeAudioDecoder( + const SdpAudioFormat& format) = 0; +}; + +} // namespace webrtc + +#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_DECODER_FACTORY_H_ diff --git a/webrtc/modules/audio_coding/codecs/audio_decoder_factory_unittest.cc b/webrtc/modules/audio_coding/codecs/audio_decoder_factory_unittest.cc new file mode 100644 index 0000000000..a1ae05d82a --- /dev/null +++ b/webrtc/modules/audio_coding/codecs/audio_decoder_factory_unittest.cc @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2016 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 "testing/gtest/include/gtest/gtest.h" +#include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h" + +namespace webrtc { + +TEST(AudioDecoderFactoryTest, CreateUnknownDecoder) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("rey", 8000, 1))); +} + +TEST(AudioDecoderFactoryTest, CreatePcmu) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + // PCMu supports 8 kHz, and any number of channels. + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 0))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 1))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 2))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 3))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 16000, 1))); +} + +TEST(AudioDecoderFactoryTest, CreatePcma) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + // PCMa supports 8 kHz, and any number of channels. + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 0))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 1))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 2))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 3))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("pcma", 16000, 1))); +} + +TEST(AudioDecoderFactoryTest, CreateIlbc) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + // iLBC supports 8 kHz, 1 channel. + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 8000, 0))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 8000, 1))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 8000, 2))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 16000, 1))); + + // iLBC actually uses a 16 kHz sample rate instead of the nominal 8 kHz. + // TODO(kwiberg): Uncomment this once AudioDecoder has a SampleRateHz method. + // std::unique_ptr dec = + // adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 8000, 1)); + // EXPECT_EQ(16000, dec->SampleRateHz()); +} + +TEST(AudioDecoderFactoryTest, CreateIsac) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + // iSAC supports 16 kHz, 1 channel. The float implementation additionally + // supports 32 kHz, 1 channel. + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("isac", 16000, 0))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("isac", 16000, 1))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("isac", 16000, 2))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("isac", 8000, 1))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("isac", 48000, 1))); +#ifdef WEBRTC_ARCH_ARM + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("isac", 32000, 1))); +#else + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("isac", 32000, 1))); +#endif +} + +TEST(AudioDecoderFactoryTest, CreateL16) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + // L16 supports any clock rate, any number of channels. + const int clockrates[] = {8000, 16000, 32000, 48000}; + const int num_channels[] = {1, 2, 3, 4711}; + for (int clockrate : clockrates) { + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("l16", clockrate, 0))); + for (int channels : num_channels) { + EXPECT_TRUE( + adf->MakeAudioDecoder(SdpAudioFormat("l16", clockrate, channels))); + } + } +} + +TEST(AudioDecoderFactoryTest, CreateG722) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + // g722 supports 8 kHz, 1-2 channels. + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 0))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 1))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 2))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 3))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("g722", 16000, 1))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("g722", 32000, 1))); +} + +TEST(AudioDecoderFactoryTest, CreateOpus) { + std::unique_ptr adf = CreateBuiltinAudioDecoderFactory(); + ASSERT_TRUE(adf); + // Opus supports 48 kHz, 1-2 channels. + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 0))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 1))); + EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 2))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 3))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 8000, 1))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 16000, 1))); + EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 32000, 1))); +} + +} // namespace webrtc diff --git a/webrtc/modules/audio_coding/codecs/audio_format.cc b/webrtc/modules/audio_coding/codecs/audio_format.cc new file mode 100644 index 0000000000..b2acf09de6 --- /dev/null +++ b/webrtc/modules/audio_coding/codecs/audio_format.cc @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2016 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/modules/audio_coding/codecs/audio_format.h" + +namespace webrtc { + +SdpAudioFormat::SdpAudioFormat() = default; +SdpAudioFormat::SdpAudioFormat(const SdpAudioFormat&) = default; +SdpAudioFormat::SdpAudioFormat(SdpAudioFormat&&) = default; + +SdpAudioFormat::SdpAudioFormat(const char* name, + int clockrate_hz, + int num_channels) + : name(name), clockrate_hz(clockrate_hz), num_channels(num_channels) {} + +SdpAudioFormat::~SdpAudioFormat() = default; +SdpAudioFormat& SdpAudioFormat::operator=(const SdpAudioFormat&) = default; +SdpAudioFormat& SdpAudioFormat::operator=(SdpAudioFormat&&) = default; + +void swap(SdpAudioFormat& a, SdpAudioFormat& b) { + using std::swap; + swap(a.name, b.name); + swap(a.clockrate_hz, b.clockrate_hz); + swap(a.num_channels, b.num_channels); + swap(a.parameters, b.parameters); +} + +std::ostream& operator<<(std::ostream& os, const SdpAudioFormat& saf) { + os << "{name: " << saf.name; + os << ", clockrate_hz: " << saf.clockrate_hz; + os << ", num_channels: " << saf.num_channels; + os << ", parameters: {"; + const char* sep = ""; + for (const auto& kv : saf.parameters) { + os << sep << kv.first << ": " << kv.second; + sep = ", "; + } + os << "}}"; + return os; +} + +} // namespace webrtc diff --git a/webrtc/modules/audio_coding/codecs/audio_format.h b/webrtc/modules/audio_coding/codecs/audio_format.h new file mode 100644 index 0000000000..7ad7edd429 --- /dev/null +++ b/webrtc/modules/audio_coding/codecs/audio_format.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2016 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_MODULES_AUDIO_CODING_CODECS_AUDIO_FORMAT_H_ +#define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_FORMAT_H_ + +#include +#include +#include +#include + +namespace webrtc { + +// SDP specification for a single audio codec. +// NOTE: This class is still under development and may change without notice. +struct SdpAudioFormat { + // TODO(kwiberg): Get rid of the default constructor when rtc::Optional no + // longer requires it. + SdpAudioFormat(); + SdpAudioFormat(const SdpAudioFormat&); + SdpAudioFormat(SdpAudioFormat&&); + SdpAudioFormat(const char* name, int clockrate_hz, int num_channels); + ~SdpAudioFormat(); + + SdpAudioFormat& operator=(const SdpAudioFormat&); + SdpAudioFormat& operator=(SdpAudioFormat&&); + + using Parameters = std::map; + std::string name; + int clockrate_hz; + int num_channels; + Parameters parameters; + // Parameters feedback_parameters; ?? +}; + +void swap(SdpAudioFormat& a, SdpAudioFormat& b); +std::ostream& operator<<(std::ostream& os, const SdpAudioFormat& saf); + +} // namespace webrtc + +#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_FORMAT_H_ diff --git a/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.cc b/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.cc new file mode 100644 index 0000000000..12a6afe2cc --- /dev/null +++ b/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.cc @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2016 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/modules/audio_coding/codecs/builtin_audio_decoder_factory.h" + +#include + +#include "webrtc/base/checks.h" +#include "webrtc/common_types.h" +#include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h" +#include "webrtc/modules/audio_coding/codecs/g711/audio_decoder_pcm.h" +#ifdef WEBRTC_CODEC_G722 +#include "webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h" +#endif +#ifdef WEBRTC_CODEC_ILBC +#include "webrtc/modules/audio_coding/codecs/ilbc/audio_decoder_ilbc.h" +#endif +#ifdef WEBRTC_CODEC_ISACFX +#include "webrtc/modules/audio_coding/codecs/isac/fix/include/audio_decoder_isacfix.h" +#endif +#ifdef WEBRTC_CODEC_ISAC +#include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_decoder_isac.h" +#endif +#ifdef WEBRTC_CODEC_OPUS +#include "webrtc/modules/audio_coding/codecs/opus/audio_decoder_opus.h" +#endif +#include "webrtc/modules/audio_coding/codecs/pcm16b/audio_decoder_pcm16b.h" + +namespace webrtc { + +namespace { + +struct NamedDecoderConstructor { + const char* name; + std::unique_ptr (*constructor)(int clockrate_hz, + int num_channels); +}; + +std::unique_ptr Unique(AudioDecoder* d) { + return std::unique_ptr(d); +} + +// TODO(kwiberg): These factory functions should probably be moved to each +// decoder. +NamedDecoderConstructor decoder_constructors[] = { + {"pcmu", + [](int clockrate_hz, int num_channels) { + return clockrate_hz == 8000 && num_channels >= 1 + ? Unique(new AudioDecoderPcmU(num_channels)) + : nullptr; + }}, + {"pcma", + [](int clockrate_hz, int num_channels) { + return clockrate_hz == 8000 && num_channels >= 1 + ? Unique(new AudioDecoderPcmA(num_channels)) + : nullptr; + }}, +#ifdef WEBRTC_CODEC_ILBC + {"ilbc", + [](int clockrate_hz, int num_channels) { + return clockrate_hz == 8000 && num_channels == 1 + ? Unique(new AudioDecoderIlbc) + : nullptr; + }}, +#endif +#if defined(WEBRTC_CODEC_ISACFX) + {"isac", + [](int clockrate_hz, int num_channels) { + return clockrate_hz == 16000 && num_channels == 1 + ? Unique(new AudioDecoderIsacFix) + : nullptr; + }}, +#elif defined(WEBRTC_CODEC_ISAC) + {"isac", + [](int clockrate_hz, int num_channels) { + return (clockrate_hz == 16000 || clockrate_hz == 32000) && + num_channels == 1 + ? Unique(new AudioDecoderIsac) + : nullptr; + }}, +#endif + {"l16", + [](int clockrate_hz, int num_channels) { + return num_channels >= 1 ? Unique(new AudioDecoderPcm16B(num_channels)) + : nullptr; + }}, +#ifdef WEBRTC_CODEC_G722 + {"g722", + [](int clockrate_hz, int num_channels) { + if (clockrate_hz == 8000) { + if (num_channels == 1) + return Unique(new AudioDecoderG722); + if (num_channels == 2) + return Unique(new AudioDecoderG722Stereo); + } + return Unique(nullptr); + }}, +#endif +#ifdef WEBRTC_CODEC_OPUS + {"opus", + [](int clockrate_hz, int num_channels) { + return clockrate_hz == 48000 && (num_channels == 1 || num_channels == 2) + ? Unique(new AudioDecoderOpus(num_channels)) + : nullptr; + }}, +#endif +}; + +class BuiltinAudioDecoderFactory : public AudioDecoderFactory { + public: + std::vector GetSupportedFormats() override { + FATAL() << "Not implemented yet!"; + } + + std::unique_ptr MakeAudioDecoder( + const SdpAudioFormat& format) override { + for (const auto& dc : decoder_constructors) { + if (STR_CASE_CMP(format.name.c_str(), dc.name) == 0) { + return std::unique_ptr( + dc.constructor(format.clockrate_hz, format.num_channels)); + } + } + return nullptr; + } +}; + +} // namespace + +std::unique_ptr CreateBuiltinAudioDecoderFactory() { + return std::unique_ptr(new BuiltinAudioDecoderFactory); +} + +} // namespace webrtc diff --git a/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h b/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h new file mode 100644 index 0000000000..7234c160b5 --- /dev/null +++ b/webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2016 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_MODULES_AUDIO_CODING_CODECS_BUILTIN_AUDIO_DECODER_FACTORY_H_ +#define WEBRTC_MODULES_AUDIO_CODING_CODECS_BUILTIN_AUDIO_DECODER_FACTORY_H_ + +#include + +#include "webrtc/modules/audio_coding/codecs/audio_decoder_factory.h" + +namespace webrtc { + +// Creates a new factory that can create the built-in types of audio decoders. +// NOTE: This function is still under development and may change without notice. +std::unique_ptr CreateBuiltinAudioDecoderFactory(); + +} // namespace webrtc + +#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_BUILTIN_AUDIO_DECODER_FACTORY_H_ diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp index 502454cef8..3b362086eb 100644 --- a/webrtc/modules/modules.gyp +++ b/webrtc/modules/modules.gyp @@ -133,6 +133,7 @@ 'audio_processing', 'audioproc_test_utils', 'bitrate_controller', + 'builtin_audio_decoder_factory', 'bwe_simulator', 'cng', 'isac_fix', @@ -167,6 +168,7 @@ '<(webrtc_root)/tools/tools.gyp:agc_test_utils', ], 'sources': [ + 'audio_coding/codecs/audio_decoder_factory_unittest.cc', 'audio_coding/codecs/cng/audio_encoder_cng_unittest.cc', 'audio_coding/acm2/acm_receiver_unittest_oldapi.cc', 'audio_coding/acm2/audio_coding_module_unittest_oldapi.cc',