OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 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 struct NamedEncoderFactory { |
| 42 const char *name; |
| 43 rtc::Optional<AudioCodecInfo> (*QueryAudioEncoder)( |
| 44 const SdpAudioFormat& format); |
| 45 std::unique_ptr<AudioEncoder> (*MakeAudioEncoder)( |
| 46 int payload_type, |
| 47 const SdpAudioFormat& format); |
| 48 |
| 49 template <typename T> |
| 50 static NamedEncoderFactory ForEncoder() { |
| 51 auto constructor = |
| 52 [](int payload_type, const SdpAudioFormat& format) { |
| 53 auto opt_info = T::QueryAudioEncoder(format); |
| 54 if (opt_info) { |
| 55 return std::unique_ptr<AudioEncoder>(new T(payload_type, format)); |
| 56 } |
| 57 return std::unique_ptr<AudioEncoder>(); |
| 58 }; |
| 59 |
| 60 return {T::GetPayloadName(), T::QueryAudioEncoder, constructor}; |
| 61 } |
| 62 }; |
| 63 |
| 64 NamedEncoderFactory encoder_factories[] = { |
| 65 #ifdef WEBRTC_CODEC_G722 |
| 66 NamedEncoderFactory::ForEncoder<AudioEncoderG722>(), |
| 67 #endif |
| 68 #ifdef WEBRTC_CODEC_ILBC |
| 69 NamedEncoderFactory::ForEncoder<AudioEncoderIlbc>(), |
| 70 #endif |
| 71 #if defined(WEBRTC_CODEC_ISACFX) |
| 72 NamedEncoderFactory::ForEncoder<AudioEncoderIsacFix>(), |
| 73 #elif defined(WEBRTC_CODEC_ISAC) |
| 74 NamedEncoderFactory::ForEncoder<AudioEncoderIsac>(), |
| 75 #endif |
| 76 |
| 77 #ifdef WEBRTC_CODEC_OPUS |
| 78 NamedEncoderFactory::ForEncoder<AudioEncoderOpus>(), |
| 79 #endif |
| 80 NamedEncoderFactory::ForEncoder<AudioEncoderPcm16B>(), |
| 81 NamedEncoderFactory::ForEncoder<AudioEncoderPcmA>(), |
| 82 NamedEncoderFactory::ForEncoder<AudioEncoderPcmU>(), |
| 83 }; |
| 84 } // namespace |
| 85 |
| 86 class BuiltinAudioEncoderFactory : public AudioEncoderFactory { |
| 87 public: |
| 88 std::vector<AudioCodecSpec> GetSupportedEncoders() override { |
| 89 static const SdpAudioFormat desired_encoders[] = { |
| 90 {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}}, |
| 91 {"isac", 16000, 1}, |
| 92 {"isac", 32000, 1}, |
| 93 {"G722", 8000, 1}, |
| 94 {"iLBC", 8000, 1}, |
| 95 {"PCMU", 8000, 1}, |
| 96 {"PCMA", 8000, 1}, |
| 97 }; |
| 98 |
| 99 // Initialize thread-safely, once, on first use. |
| 100 static const std::vector<AudioCodecSpec> specs = [] { |
| 101 std::vector<AudioCodecSpec> specs; |
| 102 for (const auto& format : desired_encoders) { |
| 103 for (const auto& ef : encoder_factories) { |
| 104 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| 105 auto opt_info = ef.QueryAudioEncoder(format); |
| 106 if (opt_info) { |
| 107 specs.push_back({format, *opt_info}); |
| 108 } |
| 109 } |
| 110 } |
| 111 } |
| 112 return specs; |
| 113 }(); |
| 114 return specs; |
| 115 } |
| 116 |
| 117 rtc::Optional<AudioCodecInfo> QueryAudioEncoder( |
| 118 const SdpAudioFormat& format) override { |
| 119 for (const auto& ef : encoder_factories) { |
| 120 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| 121 return ef.QueryAudioEncoder(format); |
| 122 } |
| 123 } |
| 124 return rtc::Optional<AudioCodecInfo>(); |
| 125 } |
| 126 |
| 127 std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| 128 int payload_type, |
| 129 const SdpAudioFormat& format) override { |
| 130 for (const auto& ef : encoder_factories) { |
| 131 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { |
| 132 return ef.MakeAudioEncoder(payload_type, format); |
| 133 } |
| 134 } |
| 135 return nullptr; |
| 136 } |
| 137 }; |
| 138 |
| 139 rtc::scoped_refptr<AudioEncoderFactory> CreateBuiltinAudioEncoderFactory() { |
| 140 return rtc::scoped_refptr<AudioEncoderFactory>( |
| 141 new rtc::RefCountedObject<BuiltinAudioEncoderFactory>()); |
| 142 } |
| 143 |
| 144 } // namespace webrtc |
OLD | NEW |