OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 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/api/audio_codecs/ilbc/audio_encoder_ilbc.h" | 11 #include "webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h" |
12 | 12 |
13 #include <memory> | 13 #include <memory> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
| 16 #include "webrtc/common_types.h" |
16 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" | 17 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h" |
17 #include "webrtc/rtc_base/ptr_util.h" | 18 #include "webrtc/rtc_base/ptr_util.h" |
18 #include "webrtc/rtc_base/safe_conversions.h" | 19 #include "webrtc/rtc_base/safe_conversions.h" |
| 20 #include "webrtc/rtc_base/safe_minmax.h" |
| 21 #include "webrtc/rtc_base/string_to_number.h" |
19 | 22 |
20 namespace webrtc { | 23 namespace webrtc { |
21 namespace { | 24 namespace { |
22 int GetIlbcBitrate(int ptime) { | 25 int GetIlbcBitrate(int ptime) { |
23 switch (ptime) { | 26 switch (ptime) { |
24 case 20: | 27 case 20: |
25 case 40: | 28 case 40: |
26 // 38 bytes per frame of 20 ms => 15200 bits/s. | 29 // 38 bytes per frame of 20 ms => 15200 bits/s. |
27 return 15200; | 30 return 15200; |
28 case 30: | 31 case 30: |
29 case 60: | 32 case 60: |
30 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s. | 33 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s. |
31 return 13333; | 34 return 13333; |
32 default: | 35 default: |
33 FATAL(); | 36 FATAL(); |
34 } | 37 } |
35 } | 38 } |
36 } // namespace | 39 } // namespace |
37 | 40 |
38 rtc::Optional<AudioEncoderIlbcConfig> AudioEncoderIlbc::SdpToConfig( | 41 rtc::Optional<AudioEncoderIlbcConfig> AudioEncoderIlbc::SdpToConfig( |
39 const SdpAudioFormat& format) { | 42 const SdpAudioFormat& format) { |
40 return AudioEncoderIlbcImpl::SdpToConfig(format); | 43 if (STR_CASE_CMP(format.name.c_str(), "ILBC") != 0 || |
| 44 format.clockrate_hz != 8000 || format.num_channels != 1) { |
| 45 return rtc::Optional<AudioEncoderIlbcConfig>(); |
| 46 } |
| 47 |
| 48 AudioEncoderIlbcConfig config; |
| 49 auto ptime_iter = format.parameters.find("ptime"); |
| 50 if (ptime_iter != format.parameters.end()) { |
| 51 auto ptime = rtc::StringToNumber<int>(ptime_iter->second); |
| 52 if (ptime && *ptime > 0) { |
| 53 const int whole_packets = *ptime / 10; |
| 54 config.frame_size_ms = rtc::SafeClamp<int>(whole_packets * 10, 20, 60); |
| 55 } |
| 56 } |
| 57 return config.IsOk() ? rtc::Optional<AudioEncoderIlbcConfig>(config) |
| 58 : rtc::Optional<AudioEncoderIlbcConfig>(); |
41 } | 59 } |
42 | 60 |
43 void AudioEncoderIlbc::AppendSupportedEncoders( | 61 void AudioEncoderIlbc::AppendSupportedEncoders( |
44 std::vector<AudioCodecSpec>* specs) { | 62 std::vector<AudioCodecSpec>* specs) { |
45 const SdpAudioFormat fmt = {"ILBC", 8000, 1}; | 63 const SdpAudioFormat fmt = {"ILBC", 8000, 1}; |
46 const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt)); | 64 const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt)); |
47 specs->push_back({fmt, info}); | 65 specs->push_back({fmt, info}); |
48 } | 66 } |
49 | 67 |
50 AudioCodecInfo AudioEncoderIlbc::QueryAudioEncoder( | 68 AudioCodecInfo AudioEncoderIlbc::QueryAudioEncoder( |
51 const AudioEncoderIlbcConfig& config) { | 69 const AudioEncoderIlbcConfig& config) { |
52 RTC_DCHECK(config.IsOk()); | 70 RTC_DCHECK(config.IsOk()); |
53 return {8000, 1, GetIlbcBitrate(config.frame_size_ms)}; | 71 return {8000, 1, GetIlbcBitrate(config.frame_size_ms)}; |
54 } | 72 } |
55 | 73 |
56 std::unique_ptr<AudioEncoder> AudioEncoderIlbc::MakeAudioEncoder( | 74 std::unique_ptr<AudioEncoder> AudioEncoderIlbc::MakeAudioEncoder( |
57 const AudioEncoderIlbcConfig& config, | 75 const AudioEncoderIlbcConfig& config, |
58 int payload_type) { | 76 int payload_type) { |
59 RTC_DCHECK(config.IsOk()); | 77 RTC_DCHECK(config.IsOk()); |
60 return rtc::MakeUnique<AudioEncoderIlbcImpl>(config, payload_type); | 78 return rtc::MakeUnique<AudioEncoderIlbcImpl>(config, payload_type); |
61 } | 79 } |
62 | 80 |
63 } // namespace webrtc | 81 } // namespace webrtc |
OLD | NEW |