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/ilbc/audio_encoder_ilbc.h" | 11 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
15 #include "webrtc/common_types.h" | 15 #include "webrtc/common_types.h" |
16 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.h" | 16 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.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 namespace { | 22 namespace { |
24 | 23 |
25 const int kSampleRateHz = 8000; | 24 const int kSampleRateHz = 8000; |
26 | 25 |
27 AudioEncoderIlbcConfig CreateConfig(const CodecInst& codec_inst) { | 26 AudioEncoderIlbcConfig CreateConfig(const CodecInst& codec_inst) { |
28 AudioEncoderIlbcConfig config; | 27 AudioEncoderIlbcConfig config; |
29 config.frame_size_ms = codec_inst.pacsize / 8; | 28 config.frame_size_ms = codec_inst.pacsize / 8; |
(...skipping 10 matching lines...) Expand all Loading... |
40 case 60: | 39 case 60: |
41 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s. | 40 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s. |
42 return 13333; | 41 return 13333; |
43 default: | 42 default: |
44 FATAL(); | 43 FATAL(); |
45 } | 44 } |
46 } | 45 } |
47 | 46 |
48 } // namespace | 47 } // namespace |
49 | 48 |
50 rtc::Optional<AudioEncoderIlbcConfig> AudioEncoderIlbcImpl::SdpToConfig( | |
51 const SdpAudioFormat& format) { | |
52 if (STR_CASE_CMP(format.name.c_str(), "ilbc") != 0 || | |
53 format.clockrate_hz != 8000 || format.num_channels != 1) { | |
54 return rtc::Optional<AudioEncoderIlbcConfig>(); | |
55 } | |
56 | |
57 AudioEncoderIlbcConfig config; | |
58 auto ptime_iter = format.parameters.find("ptime"); | |
59 if (ptime_iter != format.parameters.end()) { | |
60 auto ptime = rtc::StringToNumber<int>(ptime_iter->second); | |
61 if (ptime && *ptime > 0) { | |
62 const int whole_packets = *ptime / 10; | |
63 config.frame_size_ms = std::max(20, std::min(whole_packets * 10, 60)); | |
64 } | |
65 } | |
66 return config.IsOk() ? rtc::Optional<AudioEncoderIlbcConfig>(config) | |
67 : rtc::Optional<AudioEncoderIlbcConfig>(); | |
68 } | |
69 | |
70 AudioEncoderIlbcImpl::AudioEncoderIlbcImpl(const AudioEncoderIlbcConfig& config, | 49 AudioEncoderIlbcImpl::AudioEncoderIlbcImpl(const AudioEncoderIlbcConfig& config, |
71 int payload_type) | 50 int payload_type) |
72 : frame_size_ms_(config.frame_size_ms), | 51 : frame_size_ms_(config.frame_size_ms), |
73 payload_type_(payload_type), | 52 payload_type_(payload_type), |
74 num_10ms_frames_per_packet_( | 53 num_10ms_frames_per_packet_( |
75 static_cast<size_t>(config.frame_size_ms / 10)), | 54 static_cast<size_t>(config.frame_size_ms / 10)), |
76 encoder_(nullptr) { | 55 encoder_(nullptr) { |
77 RTC_CHECK(config.IsOk()); | 56 RTC_CHECK(config.IsOk()); |
78 Reset(); | 57 Reset(); |
79 } | 58 } |
80 | 59 |
81 AudioEncoderIlbcImpl::AudioEncoderIlbcImpl(const CodecInst& codec_inst) | 60 AudioEncoderIlbcImpl::AudioEncoderIlbcImpl(const CodecInst& codec_inst) |
82 : AudioEncoderIlbcImpl(CreateConfig(codec_inst), codec_inst.pltype) {} | 61 : AudioEncoderIlbcImpl(CreateConfig(codec_inst), codec_inst.pltype) {} |
83 | 62 |
84 AudioEncoderIlbcImpl::AudioEncoderIlbcImpl(int payload_type, | |
85 const SdpAudioFormat& format) | |
86 : AudioEncoderIlbcImpl(*SdpToConfig(format), payload_type) {} | |
87 | |
88 AudioEncoderIlbcImpl::~AudioEncoderIlbcImpl() { | 63 AudioEncoderIlbcImpl::~AudioEncoderIlbcImpl() { |
89 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); | 64 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); |
90 } | 65 } |
91 | 66 |
92 rtc::Optional<AudioCodecInfo> AudioEncoderIlbcImpl::QueryAudioEncoder( | |
93 const SdpAudioFormat& format) { | |
94 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0) { | |
95 const auto config_opt = SdpToConfig(format); | |
96 if (format.clockrate_hz == 8000 && format.num_channels == 1 && | |
97 config_opt) { | |
98 RTC_DCHECK(config_opt->IsOk()); | |
99 return rtc::Optional<AudioCodecInfo>( | |
100 {rtc::dchecked_cast<int>(kSampleRateHz), 1, | |
101 GetIlbcBitrate(config_opt->frame_size_ms)}); | |
102 } | |
103 } | |
104 return rtc::Optional<AudioCodecInfo>(); | |
105 } | |
106 | |
107 int AudioEncoderIlbcImpl::SampleRateHz() const { | 67 int AudioEncoderIlbcImpl::SampleRateHz() const { |
108 return kSampleRateHz; | 68 return kSampleRateHz; |
109 } | 69 } |
110 | 70 |
111 size_t AudioEncoderIlbcImpl::NumChannels() const { | 71 size_t AudioEncoderIlbcImpl::NumChannels() const { |
112 return 1; | 72 return 1; |
113 } | 73 } |
114 | 74 |
115 size_t AudioEncoderIlbcImpl::Num10MsFramesInNextPacket() const { | 75 size_t AudioEncoderIlbcImpl::Num10MsFramesInNextPacket() const { |
116 return num_10ms_frames_per_packet_; | 76 return num_10ms_frames_per_packet_; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 switch (num_10ms_frames_per_packet_) { | 146 switch (num_10ms_frames_per_packet_) { |
187 case 2: return 38; | 147 case 2: return 38; |
188 case 3: return 50; | 148 case 3: return 50; |
189 case 4: return 2 * 38; | 149 case 4: return 2 * 38; |
190 case 6: return 2 * 50; | 150 case 6: return 2 * 50; |
191 default: FATAL(); | 151 default: FATAL(); |
192 } | 152 } |
193 } | 153 } |
194 | 154 |
195 } // namespace webrtc | 155 } // namespace webrtc |
OLD | NEW |