| 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/pcm16b/audio_encoder_pcm16b.h" | 11 #include "webrtc/modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h" |
| 12 | 12 |
| 13 #include <algorithm> |
| 14 |
| 13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/safe_conversions.h" |
| 17 #include "webrtc/base/string_to_number.h" |
| 14 #include "webrtc/common_types.h" | 18 #include "webrtc/common_types.h" |
| 15 #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" | 19 #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" |
| 16 | 20 |
| 17 namespace webrtc { | 21 namespace webrtc { |
| 18 | 22 |
| 19 size_t AudioEncoderPcm16B::EncodeCall(const int16_t* audio, | 23 size_t AudioEncoderPcm16B::EncodeCall(const int16_t* audio, |
| 20 size_t input_len, | 24 size_t input_len, |
| 21 uint8_t* encoded) { | 25 uint8_t* encoded) { |
| 22 return WebRtcPcm16b_Encode(audio, input_len, encoded); | 26 return WebRtcPcm16b_Encode(audio, input_len, encoded); |
| 23 } | 27 } |
| 24 | 28 |
| 25 size_t AudioEncoderPcm16B::BytesPerSample() const { | 29 size_t AudioEncoderPcm16B::BytesPerSample() const { |
| 26 return 2; | 30 return 2; |
| 27 } | 31 } |
| 28 | 32 |
| 29 AudioEncoder::CodecType AudioEncoderPcm16B::GetCodecType() const { | 33 AudioEncoder::CodecType AudioEncoderPcm16B::GetCodecType() const { |
| 30 return CodecType::kOther; | 34 return CodecType::kOther; |
| 31 } | 35 } |
| 32 | 36 |
| 33 namespace { | 37 namespace { |
| 34 AudioEncoderPcm16B::Config CreateConfig(const CodecInst& codec_inst) { | 38 AudioEncoderPcm16B::Config CreateConfig(const CodecInst& codec_inst) { |
| 35 AudioEncoderPcm16B::Config config; | 39 AudioEncoderPcm16B::Config config; |
| 36 config.num_channels = codec_inst.channels; | 40 config.num_channels = codec_inst.channels; |
| 37 config.sample_rate_hz = codec_inst.plfreq; | 41 config.sample_rate_hz = codec_inst.plfreq; |
| 38 config.frame_size_ms = rtc::CheckedDivExact( | 42 config.frame_size_ms = rtc::CheckedDivExact( |
| 39 codec_inst.pacsize, rtc::CheckedDivExact(config.sample_rate_hz, 1000)); | 43 codec_inst.pacsize, rtc::CheckedDivExact(config.sample_rate_hz, 1000)); |
| 40 config.payload_type = codec_inst.pltype; | 44 config.payload_type = codec_inst.pltype; |
| 41 return config; | 45 return config; |
| 42 } | 46 } |
| 47 |
| 48 AudioEncoderPcm16B::Config CreateConfig(int payload_type, |
| 49 const SdpAudioFormat& format) { |
| 50 AudioEncoderPcm16B::Config config; |
| 51 config.num_channels = format.num_channels; |
| 52 config.sample_rate_hz = format.clockrate_hz; |
| 53 config.frame_size_ms = 10; |
| 54 auto ptime_iter = format.parameters.find("ptime"); |
| 55 if (ptime_iter != format.parameters.end()) { |
| 56 auto ptime = rtc::StringToNumber<int>(ptime_iter->second); |
| 57 if (ptime && *ptime > 0) { |
| 58 const int whole_packets = *ptime / 10; |
| 59 config.frame_size_ms = std::max(10, std::min(whole_packets * 10, 60)); |
| 60 } |
| 61 } |
| 62 config.payload_type = payload_type; |
| 63 return config; |
| 64 } |
| 43 } // namespace | 65 } // namespace |
| 44 | 66 |
| 45 bool AudioEncoderPcm16B::Config::IsOk() const { | 67 bool AudioEncoderPcm16B::Config::IsOk() const { |
| 46 if ((sample_rate_hz != 8000) && (sample_rate_hz != 16000) && | 68 if ((sample_rate_hz != 8000) && (sample_rate_hz != 16000) && |
| 47 (sample_rate_hz != 32000) && (sample_rate_hz != 48000)) | 69 (sample_rate_hz != 32000) && (sample_rate_hz != 48000)) |
| 48 return false; | 70 return false; |
| 49 return AudioEncoderPcm::Config::IsOk(); | 71 return AudioEncoderPcm::Config::IsOk(); |
| 50 } | 72 } |
| 51 | 73 |
| 52 AudioEncoderPcm16B::AudioEncoderPcm16B(const CodecInst& codec_inst) | 74 AudioEncoderPcm16B::AudioEncoderPcm16B(const CodecInst& codec_inst) |
| 53 : AudioEncoderPcm16B(CreateConfig(codec_inst)) {} | 75 : AudioEncoderPcm16B(CreateConfig(codec_inst)) {} |
| 54 | 76 |
| 77 AudioEncoderPcm16B::AudioEncoderPcm16B(int payload_type, |
| 78 const SdpAudioFormat& format) |
| 79 : AudioEncoderPcm16B(CreateConfig(payload_type, format)) {} |
| 80 |
| 81 rtc::Optional<AudioCodecInfo> AudioEncoderPcm16B::QueryAudioEncoder( |
| 82 const SdpAudioFormat& format) { |
| 83 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 && |
| 84 format.num_channels >= 1) { |
| 85 Config config = CreateConfig(0, format); |
| 86 if (config.IsOk()) { |
| 87 return rtc::Optional<AudioCodecInfo>( |
| 88 {config.sample_rate_hz, config.num_channels, |
| 89 config.sample_rate_hz * 2 * |
| 90 rtc::dchecked_cast<int>(config.num_channels)}); |
| 91 } |
| 92 } |
| 93 return rtc::Optional<AudioCodecInfo>(); |
| 94 } |
| 95 |
| 55 } // namespace webrtc | 96 } // namespace webrtc |
| OLD | NEW |