Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
|
kwiberg-webrtc
2017/03/15 13:33:17
+1
| |
| 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 | |
|
the sun
2017/03/14 20:48:29
Can you file an issue to deprecate these compiler
ossu
2017/03/15 11:26:52
How do we want this to work in future? Should our
kwiberg-webrtc
2017/03/15 13:33:17
Should we deprecate them? Isn't the idea to provid
| |
| 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<AudioFormatInfo> (*QueryAudioFormat)( | |
| 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::QueryAudioFormat(format); | |
| 54 if (opt_info) { | |
|
kwiberg-webrtc
2017/03/15 13:33:17
Why store the return value in a local variable?
| |
| 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::QueryAudioFormat, 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 { | |
|
kwiberg-webrtc
2017/03/15 13:33:17
Put this class too in the anonymous namespace?
Als
ossu
2017/03/16 18:03:57
It cannot be final, since it requires wrapping in
kwiberg-webrtc
2017/03/17 10:20:01
Ah, OK.
| |
| 87 public: | |
| 88 BuiltinAudioEncoderFactory() {} | |
|
kwiberg-webrtc
2017/03/15 13:33:17
Is this necessary?
ossu
2017/03/16 18:03:57
Probably not.
| |
| 89 | |
| 90 std::vector<AudioCodecSpec> GetSupportedEncoders() override { | |
| 91 return GetSupportedEncodersInternal(); | |
|
kwiberg-webrtc
2017/03/15 13:33:17
Why delegate to a private method?
ossu
2017/03/16 18:03:57
I believe I used the internal method at some point
| |
| 92 } | |
| 93 | |
| 94 bool IsSupportedEncoder(const SdpAudioFormat& format) override { | |
| 95 return !!QueryAudioFormat(format); | |
|
the sun
2017/03/14 20:48:29
Ah, haha. What about just having one method then?
ossu
2017/03/15 11:26:52
I'm considering whether there is enough semantic d
kwiberg-webrtc
2017/03/17 10:20:01
+1 to not having two separate methods, both becaus
| |
| 96 } | |
| 97 | |
| 98 rtc::Optional<AudioFormatInfo> QueryAudioFormat( | |
| 99 const SdpAudioFormat& format) override { | |
| 100 for (const auto& ef : encoder_factories) { | |
| 101 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { | |
| 102 return ef.QueryAudioFormat(format); | |
| 103 } | |
| 104 } | |
| 105 return rtc::Optional<AudioFormatInfo>(); | |
| 106 } | |
| 107 | |
| 108 std::unique_ptr<AudioEncoder> MakeAudioEncoder( | |
| 109 int payload_type, | |
| 110 const SdpAudioFormat& format) override { | |
| 111 for (const auto& ef : encoder_factories) { | |
| 112 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { | |
| 113 return ef.MakeAudioEncoder(payload_type, format); | |
| 114 } | |
| 115 } | |
| 116 return nullptr; | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 static const std::vector<AudioCodecSpec>& GetSupportedEncodersInternal() { | |
| 121 static const SdpAudioFormat desired_encoders[] = { | |
| 122 {"opus", 48000, 2, {{"minptime", "10"}, | |
| 123 {"useinbandfec", "1"}}}, | |
| 124 {"isac", 16000, 1}, | |
| 125 {"isac", 32000, 1}, | |
| 126 {"G722", 8000, 1}, | |
| 127 {"iLBC", 8000, 1}, | |
| 128 {"PCMU", 8000, 1}, | |
| 129 {"PCMA", 8000, 1}, | |
| 130 }; | |
|
kwiberg-webrtc
2017/03/15 13:33:17
Move this into the lambda?
ossu
2017/03/16 18:03:57
I can't honestly tell you why it is reachable by t
kwiberg-webrtc
2017/03/17 10:20:01
Yeah, you're right, that's surprising---I wouldn't
| |
| 131 | |
| 132 // Although this looks a bit strange, it means specs need only be | |
| 133 // initialized once, and that that initialization is thread-safe. | |
|
kwiberg-webrtc
2017/03/15 13:33:17
That static variables are initialized once, on fir
ossu
2017/03/16 18:03:57
Sure, it would be broken if it weren't, but I'm no
kwiberg-webrtc
2017/03/17 10:20:01
Acknowledged.
| |
| 134 static std::vector<AudioCodecSpec> supported_encoders = [] { | |
|
kwiberg-webrtc
2017/03/15 13:33:17
const? For thread safety...
ossu
2017/03/16 18:03:57
Is that more thread-safe?
kwiberg-webrtc
2017/03/17 10:20:01
Well, yes---initialization of mutable statics is t
| |
| 135 std::vector<AudioCodecSpec> supported_encoders; | |
|
kwiberg-webrtc
2017/03/15 13:33:17
Call this something else, since shadowing is confu
ossu
2017/03/16 18:03:57
The shadowing here is intended, but perhaps unnece
kwiberg-webrtc
2017/03/17 10:20:01
Acknowledged.
| |
| 136 for (const auto& format : desired_encoders) { | |
| 137 for (const auto& ef : encoder_factories) { | |
| 138 if (STR_CASE_CMP(format.name.c_str(), ef.name) == 0) { | |
| 139 auto opt_info = ef.QueryAudioFormat(format); | |
| 140 if (opt_info) { | |
| 141 supported_encoders.push_back({format, *opt_info}); | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 return supported_encoders; | |
| 147 }(); | |
| 148 return supported_encoders; | |
| 149 } | |
| 150 }; | |
| 151 | |
| 152 rtc::scoped_refptr<AudioEncoderFactory> CreateBuiltinAudioEncoderFactory() { | |
| 153 return rtc::scoped_refptr<AudioEncoderFactory>( | |
| 154 new rtc::RefCountedObject<BuiltinAudioEncoderFactory>()); | |
| 155 } | |
| 156 | |
|
the sun
2017/03/14 20:48:29
I'm missing a unit test for this class.
ossu
2017/03/15 11:26:52
I'll add a few generic tests that could be re-used
kwiberg-webrtc
2017/03/17 10:20:01
Acknowledged.
| |
| 157 } // namespace webrtc | |
| OLD | NEW |