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> | 13 #include <algorithm> |
14 | 14 |
15 #include "webrtc/common_types.h" | 15 #include "webrtc/common_types.h" |
16 #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" | 16 #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" |
17 #include "webrtc/rtc_base/checks.h" | 17 #include "webrtc/rtc_base/checks.h" |
18 #include "webrtc/rtc_base/safe_conversions.h" | 18 #include "webrtc/rtc_base/safe_conversions.h" |
19 #include "webrtc/rtc_base/string_to_number.h" | |
20 | 19 |
21 namespace webrtc { | 20 namespace webrtc { |
22 | 21 |
23 size_t AudioEncoderPcm16B::EncodeCall(const int16_t* audio, | 22 size_t AudioEncoderPcm16B::EncodeCall(const int16_t* audio, |
24 size_t input_len, | 23 size_t input_len, |
25 uint8_t* encoded) { | 24 uint8_t* encoded) { |
26 return WebRtcPcm16b_Encode(audio, input_len, encoded); | 25 return WebRtcPcm16b_Encode(audio, input_len, encoded); |
27 } | 26 } |
28 | 27 |
29 size_t AudioEncoderPcm16B::BytesPerSample() const { | 28 size_t AudioEncoderPcm16B::BytesPerSample() const { |
30 return 2; | 29 return 2; |
31 } | 30 } |
32 | 31 |
33 AudioEncoder::CodecType AudioEncoderPcm16B::GetCodecType() const { | 32 AudioEncoder::CodecType AudioEncoderPcm16B::GetCodecType() const { |
34 return CodecType::kOther; | 33 return CodecType::kOther; |
35 } | 34 } |
36 | 35 |
37 namespace { | 36 namespace { |
| 37 |
38 AudioEncoderPcm16B::Config CreateConfig(const CodecInst& codec_inst) { | 38 AudioEncoderPcm16B::Config CreateConfig(const CodecInst& codec_inst) { |
39 AudioEncoderPcm16B::Config config; | 39 AudioEncoderPcm16B::Config config; |
40 config.num_channels = codec_inst.channels; | 40 config.num_channels = codec_inst.channels; |
41 config.sample_rate_hz = codec_inst.plfreq; | 41 config.sample_rate_hz = codec_inst.plfreq; |
42 config.frame_size_ms = rtc::CheckedDivExact( | 42 config.frame_size_ms = rtc::CheckedDivExact( |
43 codec_inst.pacsize, rtc::CheckedDivExact(config.sample_rate_hz, 1000)); | 43 codec_inst.pacsize, rtc::CheckedDivExact(config.sample_rate_hz, 1000)); |
44 config.payload_type = codec_inst.pltype; | 44 config.payload_type = codec_inst.pltype; |
45 return config; | 45 return config; |
46 } | 46 } |
47 | 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 } | |
65 } // namespace | 48 } // namespace |
66 | 49 |
67 bool AudioEncoderPcm16B::Config::IsOk() const { | 50 bool AudioEncoderPcm16B::Config::IsOk() const { |
68 if ((sample_rate_hz != 8000) && (sample_rate_hz != 16000) && | 51 if ((sample_rate_hz != 8000) && (sample_rate_hz != 16000) && |
69 (sample_rate_hz != 32000) && (sample_rate_hz != 48000)) | 52 (sample_rate_hz != 32000) && (sample_rate_hz != 48000)) |
70 return false; | 53 return false; |
71 return AudioEncoderPcm::Config::IsOk(); | 54 return AudioEncoderPcm::Config::IsOk(); |
72 } | 55 } |
73 | 56 |
74 AudioEncoderPcm16B::AudioEncoderPcm16B(const CodecInst& codec_inst) | 57 AudioEncoderPcm16B::AudioEncoderPcm16B(const CodecInst& codec_inst) |
75 : AudioEncoderPcm16B(CreateConfig(codec_inst)) {} | 58 : AudioEncoderPcm16B(CreateConfig(codec_inst)) {} |
76 | 59 |
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 constexpr int bits_per_sample = 16; | |
88 return rtc::Optional<AudioCodecInfo>( | |
89 {config.sample_rate_hz, config.num_channels, | |
90 config.sample_rate_hz * bits_per_sample * | |
91 rtc::dchecked_cast<int>(config.num_channels)}); | |
92 } | |
93 } | |
94 return rtc::Optional<AudioCodecInfo>(); | |
95 } | |
96 | |
97 } // namespace webrtc | 60 } // namespace webrtc |
OLD | NEW |