| 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 <limits> | 14 #include <limits> |
| 14 | 15 |
| 15 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/string_to_number.h" |
| 16 #include "webrtc/common_types.h" | 18 #include "webrtc/common_types.h" |
| 17 #include "webrtc/modules/audio_coding/codecs/g711/g711_interface.h" | 19 #include "webrtc/modules/audio_coding/codecs/g711/g711_interface.h" |
| 18 | 20 |
| 19 namespace webrtc { | 21 namespace webrtc { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 template <typename T> | 25 template <typename T> |
| 24 typename T::Config CreateConfig(const CodecInst& codec_inst) { | 26 typename T::Config CreateConfig(const CodecInst& codec_inst) { |
| 25 typename T::Config config; | 27 typename T::Config config; |
| 26 config.frame_size_ms = codec_inst.pacsize / 8; | 28 config.frame_size_ms = codec_inst.pacsize / 8; |
| 27 config.num_channels = codec_inst.channels; | 29 config.num_channels = codec_inst.channels; |
| 28 config.payload_type = codec_inst.pltype; | 30 config.payload_type = codec_inst.pltype; |
| 29 return config; | 31 return config; |
| 30 } | 32 } |
| 31 | 33 |
| 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 |
| 32 } // namespace | 63 } // namespace |
| 33 | 64 |
| 34 bool AudioEncoderPcm::Config::IsOk() const { | 65 bool AudioEncoderPcm::Config::IsOk() const { |
| 35 return (frame_size_ms % 10 == 0) && (num_channels >= 1); | 66 return (frame_size_ms % 10 == 0) && (num_channels >= 1); |
| 36 } | 67 } |
| 37 | 68 |
| 38 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) | 69 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) |
| 39 : sample_rate_hz_(sample_rate_hz), | 70 : sample_rate_hz_(sample_rate_hz), |
| 40 num_channels_(config.num_channels), | 71 num_channels_(config.num_channels), |
| 41 payload_type_(config.payload_type), | 72 payload_type_(config.payload_type), |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 return info; | 131 return info; |
| 101 } | 132 } |
| 102 | 133 |
| 103 void AudioEncoderPcm::Reset() { | 134 void AudioEncoderPcm::Reset() { |
| 104 speech_buffer_.clear(); | 135 speech_buffer_.clear(); |
| 105 } | 136 } |
| 106 | 137 |
| 107 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst) | 138 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst) |
| 108 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {} | 139 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {} |
| 109 | 140 |
| 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 |
| 110 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, | 150 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, |
| 111 size_t input_len, | 151 size_t input_len, |
| 112 uint8_t* encoded) { | 152 uint8_t* encoded) { |
| 113 return WebRtcG711_EncodeA(audio, input_len, encoded); | 153 return WebRtcG711_EncodeA(audio, input_len, encoded); |
| 114 } | 154 } |
| 115 | 155 |
| 116 size_t AudioEncoderPcmA::BytesPerSample() const { | 156 size_t AudioEncoderPcmA::BytesPerSample() const { |
| 117 return 1; | 157 return 1; |
| 118 } | 158 } |
| 119 | 159 |
| 120 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const { | 160 AudioEncoder::CodecType AudioEncoderPcmA::GetCodecType() const { |
| 121 return AudioEncoder::CodecType::kPcmA; | 161 return AudioEncoder::CodecType::kPcmA; |
| 122 } | 162 } |
| 123 | 163 |
| 124 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst) | 164 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst) |
| 125 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {} | 165 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {} |
| 126 | 166 |
| 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 |
| 127 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, | 176 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, |
| 128 size_t input_len, | 177 size_t input_len, |
| 129 uint8_t* encoded) { | 178 uint8_t* encoded) { |
| 130 return WebRtcG711_EncodeU(audio, input_len, encoded); | 179 return WebRtcG711_EncodeU(audio, input_len, encoded); |
| 131 } | 180 } |
| 132 | 181 |
| 133 size_t AudioEncoderPcmU::BytesPerSample() const { | 182 size_t AudioEncoderPcmU::BytesPerSample() const { |
| 134 return 1; | 183 return 1; |
| 135 } | 184 } |
| 136 | 185 |
| 137 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const { | 186 AudioEncoder::CodecType AudioEncoderPcmU::GetCodecType() const { |
| 138 return AudioEncoder::CodecType::kPcmU; | 187 return AudioEncoder::CodecType::kPcmU; |
| 139 } | 188 } |
| 140 | 189 |
| 141 } // namespace webrtc | 190 } // namespace webrtc |
| OLD | NEW |