Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.cc

Issue 2695243005: Injectable audio encoders: BuiltinAudioEncoderFactory (Closed)
Patch Set: Moved ANA frame length calculation into its own function. Improved "ptime" parsing in non-opus code… Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.h" 11 #include "webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.h"
12 12
13 #include <limits> 13 #include <limits>
14 14
15 #include "webrtc/base/checks.h" 15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/string_to_number.h"
16 #include "webrtc/common_types.h" 17 #include "webrtc/common_types.h"
17 #include "webrtc/modules/audio_coding/codecs/g711/g711_interface.h" 18 #include "webrtc/modules/audio_coding/codecs/g711/g711_interface.h"
18 19
19 namespace webrtc { 20 namespace webrtc {
20 21
21 namespace { 22 namespace {
22 23
23 template <typename T> 24 template <typename T>
24 typename T::Config CreateConfig(const CodecInst& codec_inst) { 25 typename T::Config CreateConfig(const CodecInst& codec_inst) {
25 typename T::Config config; 26 typename T::Config config;
26 config.frame_size_ms = codec_inst.pacsize / 8; 27 config.frame_size_ms = codec_inst.pacsize / 8;
27 config.num_channels = codec_inst.channels; 28 config.num_channels = codec_inst.channels;
28 config.payload_type = codec_inst.pltype; 29 config.payload_type = codec_inst.pltype;
29 return config; 30 return config;
30 } 31 }
31 32
33 template <typename T>
34 typename T::Config CreateConfig(int payload_type,
35 const SdpAudioFormat& format) {
36 typename T::Config config;
37 config.frame_size_ms = 20;
38 auto ptime_iter = format.parameters.find("ptime");
39 if (ptime_iter != format.parameters.end()) {
40 auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
41 if (ptime && *ptime > 0) {
42 const int whole_packets = *ptime / 10;
43 config.frame_size_ms = std::max(10, std::min(whole_packets * 10, 60));
44 }
45 }
46 config.num_channels = format.num_channels;
47 config.payload_type = payload_type;
48 return config;
49 }
50
51 template <typename T>
52 rtc::Optional<AudioCodecInfo> QueryAudioEncoderImpl(
53 const SdpAudioFormat& format) {
54 if (STR_CASE_CMP(format.name.c_str(), T::GetPayloadName()) == 0 &&
55 format.clockrate_hz == 8000 && format.num_channels >= 1 &&
56 CreateConfig<T>(0, format).IsOk()) {
57 return rtc::Optional<AudioCodecInfo>({8000, format.num_channels, 64000});
58 }
59 return rtc::Optional<AudioCodecInfo>();
60 }
61
32 } // namespace 62 } // namespace
33 63
34 bool AudioEncoderPcm::Config::IsOk() const { 64 bool AudioEncoderPcm::Config::IsOk() const {
35 return (frame_size_ms % 10 == 0) && (num_channels >= 1); 65 return (frame_size_ms % 10 == 0) && (num_channels >= 1);
36 } 66 }
37 67
38 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) 68 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz)
39 : sample_rate_hz_(sample_rate_hz), 69 : sample_rate_hz_(sample_rate_hz),
40 num_channels_(config.num_channels), 70 num_channels_(config.num_channels),
41 payload_type_(config.payload_type), 71 payload_type_(config.payload_type),
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 return info; 130 return info;
101 } 131 }
102 132
103 void AudioEncoderPcm::Reset() { 133 void AudioEncoderPcm::Reset() {
104 speech_buffer_.clear(); 134 speech_buffer_.clear();
105 } 135 }
106 136
107 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst) 137 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst)
108 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {} 138 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {}
109 139
140 AudioEncoderPcmA::AudioEncoderPcmA(int payload_type,
141 const SdpAudioFormat& format)
142 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(payload_type, format)) {}
143
144 rtc::Optional<AudioCodecInfo> AudioEncoderPcmA::QueryAudioEncoder(
145 const SdpAudioFormat& format) {
146 return QueryAudioEncoderImpl<AudioEncoderPcmA>(format);
147 }
148
110 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, 149 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio,
111 size_t input_len, 150 size_t input_len,
112 uint8_t* encoded) { 151 uint8_t* encoded) {
113 return WebRtcG711_EncodeA(audio, input_len, encoded); 152 return WebRtcG711_EncodeA(audio, input_len, encoded);
114 } 153 }
115 154
116 size_t AudioEncoderPcmA::BytesPerSample() const { 155 size_t AudioEncoderPcmA::BytesPerSample() const {
117 return 1; 156 return 1;
118 } 157 }
119 158
120 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const { 159 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const {
121 return AudioEncoder::CodecType::kPcmA; 160 return AudioEncoder::CodecType::kPcmA;
122 } 161 }
123 162
124 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst) 163 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst)
125 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {} 164 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {}
126 165
166 AudioEncoderPcmU::AudioEncoderPcmU(int payload_type,
167 const SdpAudioFormat& format)
168 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(payload_type, format)) {}
169
170 rtc::Optional<AudioCodecInfo> AudioEncoderPcmU::QueryAudioEncoder(
171 const SdpAudioFormat& format) {
172 return QueryAudioEncoderImpl<AudioEncoderPcmU>(format);
173 }
174
127 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, 175 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio,
128 size_t input_len, 176 size_t input_len,
129 uint8_t* encoded) { 177 uint8_t* encoded) {
130 return WebRtcG711_EncodeU(audio, input_len, encoded); 178 return WebRtcG711_EncodeU(audio, input_len, encoded);
131 } 179 }
132 180
133 size_t AudioEncoderPcmU::BytesPerSample() const { 181 size_t AudioEncoderPcmU::BytesPerSample() const {
134 return 1; 182 return 1;
135 } 183 }
136 184
137 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const { 185 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const {
138 return AudioEncoder::CodecType::kPcmU; 186 return AudioEncoder::CodecType::kPcmU;
139 } 187 }
140 188
141 } // namespace webrtc 189 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698