| 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 <algorithm> | 13 #include <algorithm> |
| 14 #include <limits> | 14 #include <limits> |
| 15 | 15 |
| 16 #include "webrtc/common_types.h" | 16 #include "webrtc/common_types.h" |
| 17 #include "webrtc/modules/audio_coding/codecs/g711/g711_interface.h" | 17 #include "webrtc/modules/audio_coding/codecs/g711/g711_interface.h" |
| 18 #include "webrtc/rtc_base/checks.h" | 18 #include "webrtc/rtc_base/checks.h" |
| 19 #include "webrtc/rtc_base/string_to_number.h" | |
| 20 | 19 |
| 21 namespace webrtc { | 20 namespace webrtc { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 template <typename T> | 24 template <typename T> |
| 26 typename T::Config CreateConfig(const CodecInst& codec_inst) { | 25 typename T::Config CreateConfig(const CodecInst& codec_inst) { |
| 27 typename T::Config config; | 26 typename T::Config config; |
| 28 config.frame_size_ms = codec_inst.pacsize / 8; | 27 config.frame_size_ms = codec_inst.pacsize / 8; |
| 29 config.num_channels = codec_inst.channels; | 28 config.num_channels = codec_inst.channels; |
| 30 config.payload_type = codec_inst.pltype; | 29 config.payload_type = codec_inst.pltype; |
| 31 return config; | 30 return config; |
| 32 } | 31 } |
| 33 | 32 |
| 34 template <typename T> | |
| 35 typename T::Config CreateConfig(int payload_type, | |
| 36 const SdpAudioFormat& format) { | |
| 37 typename T::Config config; | |
| 38 config.frame_size_ms = 20; | |
| 39 auto ptime_iter = format.parameters.find("ptime"); | |
| 40 if (ptime_iter != format.parameters.end()) { | |
| 41 auto ptime = rtc::StringToNumber<int>(ptime_iter->second); | |
| 42 if (ptime && *ptime > 0) { | |
| 43 const int whole_packets = *ptime / 10; | |
| 44 config.frame_size_ms = std::max(10, std::min(whole_packets * 10, 60)); | |
| 45 } | |
| 46 } | |
| 47 config.num_channels = format.num_channels; | |
| 48 config.payload_type = payload_type; | |
| 49 return config; | |
| 50 } | |
| 51 | |
| 52 template <typename T> | |
| 53 rtc::Optional<AudioCodecInfo> QueryAudioEncoderImpl( | |
| 54 const SdpAudioFormat& format) { | |
| 55 if (STR_CASE_CMP(format.name.c_str(), T::GetPayloadName()) == 0 && | |
| 56 format.clockrate_hz == 8000 && format.num_channels >= 1 && | |
| 57 CreateConfig<T>(0, format).IsOk()) { | |
| 58 return rtc::Optional<AudioCodecInfo>({8000, format.num_channels, 64000}); | |
| 59 } | |
| 60 return rtc::Optional<AudioCodecInfo>(); | |
| 61 } | |
| 62 | |
| 63 } // namespace | 33 } // namespace |
| 64 | 34 |
| 65 bool AudioEncoderPcm::Config::IsOk() const { | 35 bool AudioEncoderPcm::Config::IsOk() const { |
| 66 return (frame_size_ms % 10 == 0) && (num_channels >= 1); | 36 return (frame_size_ms % 10 == 0) && (num_channels >= 1); |
| 67 } | 37 } |
| 68 | 38 |
| 69 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) | 39 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) |
| 70 : sample_rate_hz_(sample_rate_hz), | 40 : sample_rate_hz_(sample_rate_hz), |
| 71 num_channels_(config.num_channels), | 41 num_channels_(config.num_channels), |
| 72 payload_type_(config.payload_type), | 42 payload_type_(config.payload_type), |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 return info; | 101 return info; |
| 132 } | 102 } |
| 133 | 103 |
| 134 void AudioEncoderPcm::Reset() { | 104 void AudioEncoderPcm::Reset() { |
| 135 speech_buffer_.clear(); | 105 speech_buffer_.clear(); |
| 136 } | 106 } |
| 137 | 107 |
| 138 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst) | 108 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst) |
| 139 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {} | 109 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {} |
| 140 | 110 |
| 141 AudioEncoderPcmA::AudioEncoderPcmA(int payload_type, | |
| 142 const SdpAudioFormat& format) | |
| 143 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(payload_type, format)) {} | |
| 144 | |
| 145 rtc::Optional<AudioCodecInfo> AudioEncoderPcmA::QueryAudioEncoder( | |
| 146 const SdpAudioFormat& format) { | |
| 147 return QueryAudioEncoderImpl<AudioEncoderPcmA>(format); | |
| 148 } | |
| 149 | |
| 150 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, | 111 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, |
| 151 size_t input_len, | 112 size_t input_len, |
| 152 uint8_t* encoded) { | 113 uint8_t* encoded) { |
| 153 return WebRtcG711_EncodeA(audio, input_len, encoded); | 114 return WebRtcG711_EncodeA(audio, input_len, encoded); |
| 154 } | 115 } |
| 155 | 116 |
| 156 size_t AudioEncoderPcmA::BytesPerSample() const { | 117 size_t AudioEncoderPcmA::BytesPerSample() const { |
| 157 return 1; | 118 return 1; |
| 158 } | 119 } |
| 159 | 120 |
| 160 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const { | 121 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const { |
| 161 return AudioEncoder::CodecType::kPcmA; | 122 return AudioEncoder::CodecType::kPcmA; |
| 162 } | 123 } |
| 163 | 124 |
| 164 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst) | 125 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst) |
| 165 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {} | 126 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {} |
| 166 | 127 |
| 167 AudioEncoderPcmU::AudioEncoderPcmU(int payload_type, | |
| 168 const SdpAudioFormat& format) | |
| 169 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(payload_type, format)) {} | |
| 170 | |
| 171 rtc::Optional<AudioCodecInfo> AudioEncoderPcmU::QueryAudioEncoder( | |
| 172 const SdpAudioFormat& format) { | |
| 173 return QueryAudioEncoderImpl<AudioEncoderPcmU>(format); | |
| 174 } | |
| 175 | |
| 176 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, | 128 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, |
| 177 size_t input_len, | 129 size_t input_len, |
| 178 uint8_t* encoded) { | 130 uint8_t* encoded) { |
| 179 return WebRtcG711_EncodeU(audio, input_len, encoded); | 131 return WebRtcG711_EncodeU(audio, input_len, encoded); |
| 180 } | 132 } |
| 181 | 133 |
| 182 size_t AudioEncoderPcmU::BytesPerSample() const { | 134 size_t AudioEncoderPcmU::BytesPerSample() const { |
| 183 return 1; | 135 return 1; |
| 184 } | 136 } |
| 185 | 137 |
| 186 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const { | 138 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const { |
| 187 return AudioEncoder::CodecType::kPcmU; | 139 return AudioEncoder::CodecType::kPcmU; |
| 188 } | 140 } |
| 189 | 141 |
| 190 } // namespace webrtc | 142 } // namespace webrtc |
| OLD | NEW |