| 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 <memory> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "webrtc/base/checks.h" | |
| 17 #include "webrtc/base/logging.h" | |
| 18 #include "webrtc/base/optional.h" | |
| 19 #include "webrtc/common_types.h" | |
| 20 #include "webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.h" | |
| 21 #ifdef WEBRTC_CODEC_G722 | |
| 22 #include "webrtc/modules/audio_coding/codecs/g722/audio_encoder_g722.h" | |
| 23 #endif | |
| 24 #ifdef WEBRTC_CODEC_ILBC | |
| 25 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" | |
| 26 #endif | |
| 27 #ifdef WEBRTC_CODEC_ISACFX | |
| 28 #include "webrtc/modules/audio_coding/codecs/isac/fix/include/audio_encoder_isac
fix.h" | |
| 29 #endif | |
| 30 #ifdef WEBRTC_CODEC_ISAC | |
| 31 #include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_encoder_isa
c.h" | |
| 32 #endif | |
| 33 #ifdef WEBRTC_CODEC_OPUS | |
| 34 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" | |
| 35 #endif | |
| 36 #include "webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h" | |
| 37 | |
| 38 namespace webrtc { | |
| 39 | |
| 40 namespace { | |
| 41 | |
| 42 struct NamedEncoderFactory { | |
| 43 const char* name; | |
| 44 rtc::Optional<AudioCodecInfo> (*QueryAudioEncoder)( | |
| 45 const SdpAudioFormat& format); | |
| 46 std::unique_ptr<AudioEncoder> ( | |
| 47 *MakeAudioEncoder)(int payload_type, const SdpAudioFormat& format); | |
| 48 | |
| 49 template <typename T> | |
| 50 static NamedEncoderFactory ForEncoder() { | |
| 51 auto constructor = [](int payload_type, const SdpAudioFormat& format) { | |
| 52 auto opt_info = T::QueryAudioEncoder(format); | |
| 53 if (opt_info) { | |
| 54 return std::unique_ptr<AudioEncoder>(new T(payload_type, format)); | |
| 55 } | |
| 56 return std::unique_ptr<AudioEncoder>(); | |
| 57 }; | |
| 58 | |
| 59 return {T::GetPayloadName(), T::QueryAudioEncoder, constructor}; | |
| 60 } | |
| 61 }; | |
| 62 | |
| 63 NamedEncoderFactory encoder_factories[] = { | |
| 64 #ifdef WEBRTC_CODEC_G722 | |
| 65 NamedEncoderFactory::ForEncoder<AudioEncoderG722>(), | |
| 66 #endif | |
| 67 #ifdef WEBRTC_CODEC_ILBC | |
| 68 NamedEncoderFactory::ForEncoder<AudioEncoderIlbc>(), | |
| 69 #endif | |
| 70 #if defined(WEBRTC_CODEC_ISACFX) | |
| 71 NamedEncoderFactory::ForEncoder<AudioEncoderIsacFix>(), | |
| 72 #elif defined(WEBRTC_CODEC_ISAC) | |
| 73 NamedEncoderFactory::ForEncoder<AudioEncoderIsac>(), | |
| 74 #endif | |
| 75 | |
| 76 #ifdef WEBRTC_CODEC_OPUS | |
| 77 NamedEncoderFactory::ForEncoder<AudioEncoderOpus>(), | |
| 78 #endif | |
| 79 NamedEncoderFactory::ForEncoder<AudioEncoderPcm16B>(), | |
| 80 NamedEncoderFactory::ForEncoder<AudioEncoderPcmA>(), | |
| 81 NamedEncoderFactory::ForEncoder<AudioEncoderPcmU>(), | |
| 82 }; | |
| 83 } // namespace | |
| 84 | |
| 85 class BuiltinAudioEncoderFactory : public AudioEncoderFactory { | |
| 86 public: | |
| 87 std::vector<AudioCodecSpec> GetSupportedEncoders() override { | |
| 88 static const SdpAudioFormat desired_encoders[] = { | |
| 89 {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}}, | |
| 90 {"isac", 16000, 1}, | |
| 91 {"isac", 32000, 1}, | |
| 92 {"G722", 8000, 1}, | |
| 93 {"iLBC", 8000, 1}, | |
| 94 {"PCMU", 8000, 1}, | |
| 95 {"PCMA", 8000, 1}, | |
| 96 }; | |
| 97 | |
| 98 // Initialize thread-safely, once, on first use. | |
| 99 static const std::vector<AudioCodecSpec> specs = [] { | |
| 100 std::vector<AudioCodecSpec> specs; | |
| 101 for (const auto& format : desired_encoders) { | |
| 102 for (const auto& ef : encoder_factories) { | |
| 103 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { | |
| 104 auto opt_info = ef.QueryAudioEncoder(format); | |
| 105 if (opt_info) { | |
| 106 specs.push_back({format, *opt_info}); | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 return specs; | |
| 112 }(); | |
| 113 return specs; | |
| 114 } | |
| 115 | |
| 116 rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | |
| 117 const SdpAudioFormat& format) override { | |
| 118 for (const auto& ef : encoder_factories) { | |
| 119 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { | |
| 120 return ef.QueryAudioEncoder(format); | |
| 121 } | |
| 122 } | |
| 123 return rtc::Optional<AudioCodecInfo>(); | |
| 124 } | |
| 125 | |
| 126 std::unique_ptr<AudioEncoder> MakeAudioEncoder( | |
| 127 int payload_type, | |
| 128 const SdpAudioFormat& format) override { | |
| 129 for (const auto& ef : encoder_factories) { | |
| 130 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { | |
| 131 return ef.MakeAudioEncoder(payload_type, format); | |
| 132 } | |
| 133 } | |
| 134 return nullptr; | |
| 135 } | |
| 136 }; | |
| 137 | |
| 138 rtc::scoped_refptr<AudioEncoderFactory> CreateBuiltinAudioEncoderFactory() { | |
| 139 return rtc::scoped_refptr<AudioEncoderFactory>( | |
| 140 new rtc::RefCountedObject<BuiltinAudioEncoderFactory>()); | |
| 141 } | |
| 142 | |
| 143 } // namespace webrtc | |
| OLD | NEW |