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