Chromium Code Reviews| Index: webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.cc |
| diff --git a/webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.cc b/webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..baf270d2cbeec0ccbf63fa61d11ee22602e48f4d |
| --- /dev/null |
| +++ b/webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.cc |
| @@ -0,0 +1,168 @@ |
| +/* |
| + * 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_encoder_factory.h" |
| + |
| +#include <vector> |
| + |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/base/logging.h" |
| +#include "webrtc/base/optional.h" |
| +#include "webrtc/common_types.h" |
| +#include "webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.h" |
| +#ifdef WEBRTC_CODEC_G722 |
| +#include "webrtc/modules/audio_coding/codecs/g722/audio_encoder_g722.h" |
| +#endif |
| +#ifdef WEBRTC_CODEC_ILBC |
| +#include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" |
| +#endif |
| +#ifdef WEBRTC_CODEC_ISACFX |
| +#include "webrtc/modules/audio_coding/codecs/isac/fix/include/audio_encoder_isacfix.h" |
| +#endif |
| +#ifdef WEBRTC_CODEC_ISAC |
| +#include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h" |
| +#endif |
| +#ifdef WEBRTC_CODEC_OPUS |
| +#include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" |
| +#endif |
| +#include "webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h" |
| + |
| +namespace webrtc { |
| + |
| +namespace { |
| + |
| +template <typename T> |
| +std::unique_ptr<AudioEncoder> AudioEncoderConstructor( |
| + int payload_type, |
| + const SdpAudioFormat& format) { |
| + auto opt_info = T::QueryAudioFormat(format); |
| + if (opt_info) { |
| + return std::unique_ptr<AudioEncoder>(new T(payload_type, format)); |
| + } |
| + return nullptr; |
| +} |
|
kwiberg-webrtc
2017/02/19 21:41:10
To reduce the scope, this could be a private membe
ossu
2017/02/20 12:20:26
True. Will do.
|
| + |
| +struct NamedEncoderFactory { |
| + const char *name; |
| + rtc::Optional<AudioFormatInfo> (*QueryAudioFormat)( |
| + const SdpAudioFormat& format); |
| + std::unique_ptr<AudioEncoder> (*MakeAudioEncoder)( |
| + int payload_type, |
| + const SdpAudioFormat& format); |
| + |
| + template <typename T> |
| + static NamedEncoderFactory ForEncoder() { |
| + return {T::GetPayloadName(), |
| + T::QueryAudioFormat, |
| + AudioEncoderConstructor<T>}; |
| + } |
| +}; |
| + |
| +NamedEncoderFactory encoder_factories[] = { |
| +#ifdef WEBRTC_CODEC_G722 |
| + NamedEncoderFactory::ForEncoder<AudioEncoderG722>(), |
| +#endif |
| +#ifdef WEBRTC_CODEC_ILBC |
| + NamedEncoderFactory::ForEncoder<AudioEncoderIlbc>(), |
| +#endif |
| +#if defined(WEBRTC_CODEC_ISACFX) |
| + NamedEncoderFactory::ForEncoder<AudioEncoderIsacFix>(), |
| +#elif defined(WEBRTC_CODEC_ISAC) |
| + NamedEncoderFactory::ForEncoder<AudioEncoderIsac>(), |
| +#endif |
| + |
| +#ifdef WEBRTC_CODEC_OPUS |
| + NamedEncoderFactory::ForEncoder<AudioEncoderOpus>(), |
| +#endif |
| + NamedEncoderFactory::ForEncoder<AudioEncoderPcm16B>(), |
| + NamedEncoderFactory::ForEncoder<AudioEncoderPcmA>(), |
| + NamedEncoderFactory::ForEncoder<AudioEncoderPcmU>(), |
| +}; |
| + |
| +} // namespace |
| + |
| +class BuiltinAudioEncoderFactory : public AudioEncoderFactory { |
| + public: |
| + BuiltinAudioEncoderFactory() { |
| + // TODO(ossu): Make this a one-time initialization, preferable static. |
|
kwiberg-webrtc
2017/02/19 21:41:10
Yes.
|
| + auto add_if_supported = |
| + [this] (const SdpAudioFormat& format) { |
| + for (const auto& ef : encoder_factories) { |
| + if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| + auto opt_info = ef.QueryAudioFormat(format); |
| + if (opt_info) { |
| + supported_encoders_.push_back({format, *opt_info}); |
| + } |
| + } |
| + } |
| + }; |
| + |
| + add_if_supported({"opus", 48000, 2, {{"minptime", "10"}, |
| + {"useinbandfec", "1"}}}); |
| + add_if_supported({"isac", 16000, 1}); |
| + add_if_supported({"isac", 32000, 1}); |
| + add_if_supported({"G722", 8000, 1}); |
| + add_if_supported({"iLBC", 8000, 1}); |
| + add_if_supported({"PCMU", 8000, 1}); |
| + add_if_supported({"PCMA", 8000, 1}); |
| + } |
| + |
| + std::vector<AudioCodecSpec> GetSupportedEncoders() override { |
| + return supported_encoders_; |
| + } |
| + |
| + bool IsSupportedEncoder(const SdpAudioFormat& format) override { |
| + for (const auto& ef : encoder_factories) { |
| + if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| + return !!ef.QueryAudioFormat(format); |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + rtc::Optional<AudioFormatInfo> QueryAudioFormat( |
| + const SdpAudioFormat& format) override { |
| + LOG(LS_INFO) << "Querying for format " << format; |
| + for (const auto& ef : encoder_factories) { |
| + if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| + return ef.QueryAudioFormat(format); |
| + } |
| + } |
| + for (const auto& spec : supported_encoders_) { |
| + if (STR_CASE_CMP(format.name.c_str(), spec.format.name.c_str()) == 0 && |
| + format.clockrate_hz == spec.format.clockrate_hz && |
| + format.num_channels == spec.format.num_channels) { |
| + return rtc::Optional<AudioFormatInfo>(spec.info); |
| + } |
| + } |
|
kwiberg-webrtc
2017/02/19 21:41:10
Maybe I'm just confused, but why are you looking i
ossu
2017/02/20 12:20:25
Hmm... either I forgot to remove my temporary impl
|
| + return rtc::Optional<AudioFormatInfo>(); |
| + } |
| + |
| + std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| + int payload_type, |
| + const SdpAudioFormat& format) override { |
| + for (const auto& ef : encoder_factories) { |
| + if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| + return ef.MakeAudioEncoder(payload_type, format); |
| + } |
| + } |
| + return nullptr; |
| + } |
| + |
| + private: |
| + std::vector<AudioCodecSpec> supported_encoders_; |
| +}; |
| + |
| +rtc::scoped_refptr<AudioEncoderFactory> CreateBuiltinAudioEncoderFactory() { |
| + return rtc::scoped_refptr<AudioEncoderFactory>( |
| + new rtc::RefCountedObject<BuiltinAudioEncoderFactory>()); |
| +} |
| + |
| +} // namespace webrtc |