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/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/safe_conversions.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/ilbc/ilbc.h" | 19 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.h" |
18 | 20 |
19 namespace webrtc { | 21 namespace webrtc { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 const int kSampleRateHz = 8000; | 25 const int kSampleRateHz = 8000; |
24 | 26 |
25 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) { | 27 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) { |
26 AudioEncoderIlbc::Config config; | 28 AudioEncoderIlbc::Config config; |
27 config.frame_size_ms = codec_inst.pacsize / 8; | 29 config.frame_size_ms = codec_inst.pacsize / 8; |
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 AudioEncoderIlbc::Config CreateConfig(int payload_type, |
| 35 const SdpAudioFormat& format) { |
| 36 AudioEncoderIlbc::Config config; |
| 37 config.payload_type = payload_type; |
| 38 auto ptime_iter = format.parameters.find("ptime"); |
| 39 if (ptime_iter != format.parameters.end()) { |
| 40 auto ptime = rtc::StringToNumber<int>(ptime_iter->second); |
| 41 if (ptime && *ptime > 0) { |
| 42 const int whole_packets = *ptime / 10; |
| 43 config.frame_size_ms = std::max(20, std::min(whole_packets * 10, 60)); |
| 44 } |
| 45 } |
| 46 return config; |
| 47 } |
| 48 |
| 49 int GetIlbcBitrate(int ptime) { |
| 50 switch (ptime) { |
| 51 case 20: case 40: |
| 52 // 38 bytes per frame of 20 ms => 15200 bits/s. |
| 53 return 15200; |
| 54 case 30: case 60: |
| 55 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s. |
| 56 return 13333; |
| 57 default: |
| 58 FATAL(); |
| 59 } |
| 60 } |
| 61 |
32 } // namespace | 62 } // namespace |
33 | 63 |
34 // static | 64 // static |
35 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket; | 65 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket; |
36 | 66 |
37 bool AudioEncoderIlbc::Config::IsOk() const { | 67 bool AudioEncoderIlbc::Config::IsOk() const { |
38 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 || | 68 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 || |
39 frame_size_ms == 60) && | 69 frame_size_ms == 60) && |
40 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <= | 70 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <= |
41 kMaxSamplesPerPacket; | 71 kMaxSamplesPerPacket; |
42 } | 72 } |
43 | 73 |
44 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config) | 74 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config) |
45 : config_(config), | 75 : config_(config), |
46 num_10ms_frames_per_packet_( | 76 num_10ms_frames_per_packet_( |
47 static_cast<size_t>(config.frame_size_ms / 10)), | 77 static_cast<size_t>(config.frame_size_ms / 10)), |
48 encoder_(nullptr) { | 78 encoder_(nullptr) { |
49 Reset(); | 79 Reset(); |
50 } | 80 } |
51 | 81 |
52 AudioEncoderIlbc::AudioEncoderIlbc(const CodecInst& codec_inst) | 82 AudioEncoderIlbc::AudioEncoderIlbc(const CodecInst& codec_inst) |
53 : AudioEncoderIlbc(CreateConfig(codec_inst)) {} | 83 : AudioEncoderIlbc(CreateConfig(codec_inst)) {} |
54 | 84 |
| 85 AudioEncoderIlbc::AudioEncoderIlbc(int payload_type, |
| 86 const SdpAudioFormat& format) |
| 87 : AudioEncoderIlbc(CreateConfig(payload_type, format)) {} |
| 88 |
| 89 rtc::Optional<AudioCodecInfo> AudioEncoderIlbc::QueryAudioEncoder( |
| 90 const SdpAudioFormat& format) { |
| 91 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 && |
| 92 format.clockrate_hz == 8000 && format.num_channels == 1) { |
| 93 Config config = CreateConfig(0, format); |
| 94 if (config.IsOk()) { |
| 95 return rtc::Optional<AudioCodecInfo>( |
| 96 {kSampleRateHz, 1, GetIlbcBitrate(config.frame_size_ms)}); |
| 97 } |
| 98 } |
| 99 |
| 100 return rtc::Optional<AudioCodecInfo>(); |
| 101 } |
| 102 |
55 AudioEncoderIlbc::~AudioEncoderIlbc() { | 103 AudioEncoderIlbc::~AudioEncoderIlbc() { |
56 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); | 104 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); |
57 } | 105 } |
58 | 106 |
59 int AudioEncoderIlbc::SampleRateHz() const { | 107 int AudioEncoderIlbc::SampleRateHz() const { |
60 return kSampleRateHz; | 108 return kSampleRateHz; |
61 } | 109 } |
62 | 110 |
63 size_t AudioEncoderIlbc::NumChannels() const { | 111 size_t AudioEncoderIlbc::NumChannels() const { |
64 return 1; | 112 return 1; |
65 } | 113 } |
66 | 114 |
67 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const { | 115 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const { |
68 return num_10ms_frames_per_packet_; | 116 return num_10ms_frames_per_packet_; |
69 } | 117 } |
70 | 118 |
71 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const { | 119 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const { |
72 return num_10ms_frames_per_packet_; | 120 return num_10ms_frames_per_packet_; |
73 } | 121 } |
74 | 122 |
75 int AudioEncoderIlbc::GetTargetBitrate() const { | 123 int AudioEncoderIlbc::GetTargetBitrate() const { |
76 switch (num_10ms_frames_per_packet_) { | 124 return GetIlbcBitrate(rtc::dchecked_cast<int>(num_10ms_frames_per_packet_) * |
77 case 2: case 4: | 125 10); |
78 // 38 bytes per frame of 20 ms => 15200 bits/s. | |
79 return 15200; | |
80 case 3: case 6: | |
81 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s. | |
82 return 13333; | |
83 default: | |
84 FATAL(); | |
85 } | |
86 } | 126 } |
87 | 127 |
88 AudioEncoder::EncodedInfo AudioEncoderIlbc::EncodeImpl( | 128 AudioEncoder::EncodedInfo AudioEncoderIlbc::EncodeImpl( |
89 uint32_t rtp_timestamp, | 129 uint32_t rtp_timestamp, |
90 rtc::ArrayView<const int16_t> audio, | 130 rtc::ArrayView<const int16_t> audio, |
91 rtc::Buffer* encoded) { | 131 rtc::Buffer* encoded) { |
92 | 132 |
93 // Save timestamp if starting a new packet. | 133 // Save timestamp if starting a new packet. |
94 if (num_10ms_frames_buffered_ == 0) | 134 if (num_10ms_frames_buffered_ == 0) |
95 first_timestamp_in_buffer_ = rtp_timestamp; | 135 first_timestamp_in_buffer_ = rtp_timestamp; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 switch (num_10ms_frames_per_packet_) { | 187 switch (num_10ms_frames_per_packet_) { |
148 case 2: return 38; | 188 case 2: return 38; |
149 case 3: return 50; | 189 case 3: return 50; |
150 case 4: return 2 * 38; | 190 case 4: return 2 * 38; |
151 case 6: return 2 * 50; | 191 case 6: return 2 * 50; |
152 default: FATAL(); | 192 default: FATAL(); |
153 } | 193 } |
154 } | 194 } |
155 | 195 |
156 } // namespace webrtc | 196 } // namespace webrtc |
OLD | NEW |