OLD | NEW |
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 config.frame_size_ms = *ptime; |
| 43 } |
| 44 } |
| 45 config.num_channels = format.num_channels; |
| 46 config.payload_type = payload_type; |
| 47 return config; |
| 48 } |
| 49 |
| 50 template <typename T> |
| 51 rtc::Optional<AudioFormatInfo> QueryAudioFormatImpl( |
| 52 const SdpAudioFormat& format) { |
| 53 if (STR_CASE_CMP(format.name.c_str(), T::GetPayloadName()) == 0 && |
| 54 format.clockrate_hz == 8000 && format.num_channels >= 1) { |
| 55 typename T::Config config = CreateConfig<T>(0, format); |
| 56 if (config.IsOk()) { |
| 57 return rtc::Optional<AudioFormatInfo>({8000, format.num_channels, 64000}); |
| 58 } |
| 59 } |
| 60 return rtc::Optional<AudioFormatInfo>(); |
| 61 } |
| 62 |
| 63 |
32 } // namespace | 64 } // namespace |
33 | 65 |
34 bool AudioEncoderPcm::Config::IsOk() const { | 66 bool AudioEncoderPcm::Config::IsOk() const { |
35 return (frame_size_ms % 10 == 0) && (num_channels >= 1); | 67 return (frame_size_ms % 10 == 0) && (num_channels >= 1); |
36 } | 68 } |
37 | 69 |
38 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) | 70 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) |
39 : sample_rate_hz_(sample_rate_hz), | 71 : sample_rate_hz_(sample_rate_hz), |
40 num_channels_(config.num_channels), | 72 num_channels_(config.num_channels), |
41 payload_type_(config.payload_type), | 73 payload_type_(config.payload_type), |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 return info; | 132 return info; |
101 } | 133 } |
102 | 134 |
103 void AudioEncoderPcm::Reset() { | 135 void AudioEncoderPcm::Reset() { |
104 speech_buffer_.clear(); | 136 speech_buffer_.clear(); |
105 } | 137 } |
106 | 138 |
107 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst) | 139 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst) |
108 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {} | 140 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {} |
109 | 141 |
| 142 AudioEncoderPcmA::AudioEncoderPcmA(int payload_type, |
| 143 const SdpAudioFormat& format) |
| 144 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(payload_type, format)) {} |
| 145 |
| 146 rtc::Optional<AudioFormatInfo> AudioEncoderPcmA::QueryAudioFormat( |
| 147 const SdpAudioFormat& format) { |
| 148 return QueryAudioFormatImpl<AudioEncoderPcmA>(format); |
| 149 } |
| 150 |
110 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, | 151 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, |
111 size_t input_len, | 152 size_t input_len, |
112 uint8_t* encoded) { | 153 uint8_t* encoded) { |
113 return WebRtcG711_EncodeA(audio, input_len, encoded); | 154 return WebRtcG711_EncodeA(audio, input_len, encoded); |
114 } | 155 } |
115 | 156 |
116 size_t AudioEncoderPcmA::BytesPerSample() const { | 157 size_t AudioEncoderPcmA::BytesPerSample() const { |
117 return 1; | 158 return 1; |
118 } | 159 } |
119 | 160 |
120 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const { | 161 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const { |
121 return AudioEncoder::CodecType::kPcmA; | 162 return AudioEncoder::CodecType::kPcmA; |
122 } | 163 } |
123 | 164 |
124 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst) | 165 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst) |
125 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {} | 166 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {} |
126 | 167 |
| 168 AudioEncoderPcmU::AudioEncoderPcmU(int payload_type, |
| 169 const SdpAudioFormat& format) |
| 170 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(payload_type, format)) {} |
| 171 |
| 172 rtc::Optional<AudioFormatInfo> AudioEncoderPcmU::QueryAudioFormat( |
| 173 const SdpAudioFormat& format) { |
| 174 return QueryAudioFormatImpl<AudioEncoderPcmU>(format); |
| 175 } |
| 176 |
127 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, | 177 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, |
128 size_t input_len, | 178 size_t input_len, |
129 uint8_t* encoded) { | 179 uint8_t* encoded) { |
130 return WebRtcG711_EncodeU(audio, input_len, encoded); | 180 return WebRtcG711_EncodeU(audio, input_len, encoded); |
131 } | 181 } |
132 | 182 |
133 size_t AudioEncoderPcmU::BytesPerSample() const { | 183 size_t AudioEncoderPcmU::BytesPerSample() const { |
134 return 1; | 184 return 1; |
135 } | 185 } |
136 | 186 |
137 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const { | 187 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const { |
138 return AudioEncoder::CodecType::kPcmU; | 188 return AudioEncoder::CodecType::kPcmU; |
139 } | 189 } |
140 | 190 |
141 } // namespace webrtc | 191 } // namespace webrtc |
OLD | NEW |