OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 "webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.h" |
| 12 |
| 13 #include <vector> |
| 14 |
| 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/logging.h" |
| 17 #include "webrtc/base/optional.h" |
| 18 #include "webrtc/common_types.h" |
| 19 #include "webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.h" |
| 20 #ifdef WEBRTC_CODEC_G722 |
| 21 #include "webrtc/modules/audio_coding/codecs/g722/audio_encoder_g722.h" |
| 22 #endif |
| 23 #ifdef WEBRTC_CODEC_ILBC |
| 24 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" |
| 25 #endif |
| 26 #ifdef WEBRTC_CODEC_ISACFX |
| 27 #include "webrtc/modules/audio_coding/codecs/isac/fix/include/audio_encoder_isac
fix.h" |
| 28 #endif |
| 29 #ifdef WEBRTC_CODEC_ISAC |
| 30 #include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_encoder_isa
c.h" |
| 31 #endif |
| 32 #ifdef WEBRTC_CODEC_OPUS |
| 33 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" |
| 34 #endif |
| 35 #include "webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h" |
| 36 |
| 37 namespace webrtc { |
| 38 |
| 39 namespace { |
| 40 |
| 41 template <typename T> |
| 42 std::unique_ptr<AudioEncoder> AudioEncoderConstructor( |
| 43 int payload_type, |
| 44 const SdpAudioFormat& format) { |
| 45 auto opt_info = T::QueryAudioFormat(format); |
| 46 if (opt_info) { |
| 47 return std::unique_ptr<AudioEncoder>(new T(payload_type, format)); |
| 48 } |
| 49 return nullptr; |
| 50 } |
| 51 |
| 52 struct NamedEncoderFactory { |
| 53 const char *name; |
| 54 rtc::Optional<AudioFormatInfo> (*QueryAudioFormat)( |
| 55 const SdpAudioFormat& format); |
| 56 std::unique_ptr<AudioEncoder> (*MakeAudioEncoder)( |
| 57 int payload_type, |
| 58 const SdpAudioFormat& format); |
| 59 |
| 60 template <typename T> |
| 61 static NamedEncoderFactory ForEncoder() { |
| 62 return {T::GetPayloadName(), |
| 63 T::QueryAudioFormat, |
| 64 AudioEncoderConstructor<T>}; |
| 65 } |
| 66 }; |
| 67 |
| 68 NamedEncoderFactory encoder_factories[] = { |
| 69 #ifdef WEBRTC_CODEC_G722 |
| 70 NamedEncoderFactory::ForEncoder<AudioEncoderG722>(), |
| 71 #endif |
| 72 #ifdef WEBRTC_CODEC_ILBC |
| 73 NamedEncoderFactory::ForEncoder<AudioEncoderIlbc>(), |
| 74 #endif |
| 75 #if defined(WEBRTC_CODEC_ISACFX) |
| 76 NamedEncoderFactory::ForEncoder<AudioEncoderIsacFix>(), |
| 77 #elif defined(WEBRTC_CODEC_ISAC) |
| 78 NamedEncoderFactory::ForEncoder<AudioEncoderIsac>(), |
| 79 #endif |
| 80 |
| 81 #ifdef WEBRTC_CODEC_OPUS |
| 82 NamedEncoderFactory::ForEncoder<AudioEncoderOpus>(), |
| 83 #endif |
| 84 NamedEncoderFactory::ForEncoder<AudioEncoderPcm16B>(), |
| 85 NamedEncoderFactory::ForEncoder<AudioEncoderPcmA>(), |
| 86 NamedEncoderFactory::ForEncoder<AudioEncoderPcmU>(), |
| 87 }; |
| 88 |
| 89 } // namespace |
| 90 |
| 91 class BuiltinAudioEncoderFactory : public AudioEncoderFactory { |
| 92 public: |
| 93 BuiltinAudioEncoderFactory() { |
| 94 // TODO(ossu): Make this a one-time initialization, preferable static. |
| 95 auto add_if_supported = |
| 96 [this] (const SdpAudioFormat& format) { |
| 97 for (const auto& ef : encoder_factories) { |
| 98 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| 99 auto opt_info = ef.QueryAudioFormat(format); |
| 100 if (opt_info) { |
| 101 supported_encoders_.push_back({format, *opt_info}); |
| 102 } |
| 103 } |
| 104 } |
| 105 }; |
| 106 |
| 107 add_if_supported({"opus", 48000, 2, {{"minptime", "10"}, |
| 108 {"useinbandfec", "1"}}}); |
| 109 add_if_supported({"isac", 16000, 1}); |
| 110 add_if_supported({"isac", 32000, 1}); |
| 111 add_if_supported({"G722", 8000, 1}); |
| 112 add_if_supported({"iLBC", 8000, 1}); |
| 113 add_if_supported({"PCMU", 8000, 1}); |
| 114 add_if_supported({"PCMA", 8000, 1}); |
| 115 } |
| 116 |
| 117 std::vector<AudioCodecSpec> GetSupportedEncoders() override { |
| 118 return supported_encoders_; |
| 119 } |
| 120 |
| 121 bool IsSupportedEncoder(const SdpAudioFormat& format) override { |
| 122 for (const auto& ef : encoder_factories) { |
| 123 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| 124 return !!ef.QueryAudioFormat(format); |
| 125 } |
| 126 } |
| 127 return false; |
| 128 } |
| 129 |
| 130 rtc::Optional<AudioFormatInfo> QueryAudioFormat( |
| 131 const SdpAudioFormat& format) override { |
| 132 LOG(LS_INFO) << "Querying for format " << format; |
| 133 for (const auto& ef : encoder_factories) { |
| 134 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| 135 return ef.QueryAudioFormat(format); |
| 136 } |
| 137 } |
| 138 for (const auto& spec : supported_encoders_) { |
| 139 if (STR_CASE_CMP(format.name.c_str(), spec.format.name.c_str()) == 0 && |
| 140 format.clockrate_hz == spec.format.clockrate_hz && |
| 141 format.num_channels == spec.format.num_channels) { |
| 142 return rtc::Optional<AudioFormatInfo>(spec.info); |
| 143 } |
| 144 } |
| 145 return rtc::Optional<AudioFormatInfo>(); |
| 146 } |
| 147 |
| 148 std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| 149 int payload_type, |
| 150 const SdpAudioFormat& format) override { |
| 151 for (const auto& ef : encoder_factories) { |
| 152 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| 153 return ef.MakeAudioEncoder(payload_type, format); |
| 154 } |
| 155 } |
| 156 return nullptr; |
| 157 } |
| 158 |
| 159 private: |
| 160 std::vector<AudioCodecSpec> supported_encoders_; |
| 161 }; |
| 162 |
| 163 rtc::scoped_refptr<AudioEncoderFactory> CreateBuiltinAudioEncoderFactory() { |
| 164 return rtc::scoped_refptr<AudioEncoderFactory>( |
| 165 new rtc::RefCountedObject<BuiltinAudioEncoderFactory>()); |
| 166 } |
| 167 |
| 168 } // namespace webrtc |
OLD | NEW |