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/cng/include/audio_encoder_cng.h" | 11 #include "webrtc/modules/audio_coding/codecs/cng/include/audio_encoder_cng.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
15 | 15 |
16 namespace webrtc { | 16 namespace webrtc { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const int kMaxFrameSizeMs = 60; | 20 const int kMaxFrameSizeMs = 60; |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 AudioEncoderCng::Config::Config() | |
25 : num_channels(1), | |
26 payload_type(13), | |
27 speech_encoder(NULL), | |
28 vad_mode(Vad::kVadNormal), | |
29 sid_frame_interval_ms(100), | |
30 num_cng_coefficients(8), | |
31 vad(NULL) { | |
32 } | |
33 | |
34 bool AudioEncoderCng::Config::IsOk() const { | 24 bool AudioEncoderCng::Config::IsOk() const { |
35 if (num_channels != 1) | 25 if (num_channels != 1) |
36 return false; | 26 return false; |
37 if (!speech_encoder) | 27 if (!speech_encoder) |
38 return false; | 28 return false; |
39 if (num_channels != speech_encoder->NumChannels()) | 29 if (num_channels != speech_encoder->NumChannels()) |
40 return false; | 30 return false; |
41 if (sid_frame_interval_ms < | 31 if (sid_frame_interval_ms < |
42 static_cast<int>(speech_encoder->Max10MsFramesInAPacket() * 10)) | 32 static_cast<int>(speech_encoder->Max10MsFramesInAPacket() * 10)) |
43 return false; | 33 return false; |
(...skipping 17 matching lines...) Expand all Loading... | |
61 CNG_enc_inst* cng_inst; | 51 CNG_enc_inst* cng_inst; |
62 CHECK_EQ(WebRtcCng_CreateEnc(&cng_inst), 0) << "WebRtcCng_CreateEnc failed."; | 52 CHECK_EQ(WebRtcCng_CreateEnc(&cng_inst), 0) << "WebRtcCng_CreateEnc failed."; |
63 cng_inst_.reset(cng_inst); // Transfer ownership to scoped_ptr. | 53 cng_inst_.reset(cng_inst); // Transfer ownership to scoped_ptr. |
64 CHECK_EQ(WebRtcCng_InitEnc(cng_inst_.get(), SampleRateHz(), | 54 CHECK_EQ(WebRtcCng_InitEnc(cng_inst_.get(), SampleRateHz(), |
65 config.sid_frame_interval_ms, | 55 config.sid_frame_interval_ms, |
66 config.num_cng_coefficients), | 56 config.num_cng_coefficients), |
67 0) | 57 0) |
68 << "WebRtcCng_InitEnc failed"; | 58 << "WebRtcCng_InitEnc failed"; |
69 } | 59 } |
70 | 60 |
71 AudioEncoderCng::~AudioEncoderCng() { | 61 AudioEncoderCng::~AudioEncoderCng() = default; |
72 } | |
73 | |
74 int AudioEncoderCng::SampleRateHz() const { | |
75 return speech_encoder_->SampleRateHz(); | |
76 } | |
77 | |
78 int AudioEncoderCng::RtpTimestampRateHz() const { | |
79 return speech_encoder_->RtpTimestampRateHz(); | |
80 } | |
81 | |
82 int AudioEncoderCng::NumChannels() const { | |
83 return 1; | |
84 } | |
85 | 62 |
86 size_t AudioEncoderCng::MaxEncodedBytes() const { | 63 size_t AudioEncoderCng::MaxEncodedBytes() const { |
87 const size_t max_encoded_bytes_active = speech_encoder_->MaxEncodedBytes(); | 64 const size_t max_encoded_bytes_active = speech_encoder_->MaxEncodedBytes(); |
88 const size_t max_encoded_bytes_passive = | 65 const size_t max_encoded_bytes_passive = |
89 rtc::CheckedDivExact(kMaxFrameSizeMs, 10) * SamplesPer10msFrame(); | 66 rtc::CheckedDivExact(kMaxFrameSizeMs, 10) * SamplesPer10msFrame(); |
90 return std::max(max_encoded_bytes_active, max_encoded_bytes_passive); | 67 return std::max(max_encoded_bytes_active, max_encoded_bytes_passive); |
91 } | 68 } |
92 | 69 |
70 int AudioEncoderCng::SampleRateHz() const { | |
71 return speech_encoder_->SampleRateHz(); | |
72 } | |
73 | |
74 int AudioEncoderCng::NumChannels() const { | |
75 return 1; | |
76 } | |
77 | |
78 int AudioEncoderCng::RtpTimestampRateHz() const { | |
79 return speech_encoder_->RtpTimestampRateHz(); | |
80 } | |
81 | |
93 size_t AudioEncoderCng::Num10MsFramesInNextPacket() const { | 82 size_t AudioEncoderCng::Num10MsFramesInNextPacket() const { |
94 return speech_encoder_->Num10MsFramesInNextPacket(); | 83 return speech_encoder_->Num10MsFramesInNextPacket(); |
95 } | 84 } |
96 | 85 |
97 size_t AudioEncoderCng::Max10MsFramesInAPacket() const { | 86 size_t AudioEncoderCng::Max10MsFramesInAPacket() const { |
98 return speech_encoder_->Max10MsFramesInAPacket(); | 87 return speech_encoder_->Max10MsFramesInAPacket(); |
99 } | 88 } |
100 | 89 |
101 int AudioEncoderCng::GetTargetBitrate() const { | 90 int AudioEncoderCng::GetTargetBitrate() const { |
102 return speech_encoder_->GetTargetBitrate(); | 91 return speech_encoder_->GetTargetBitrate(); |
103 } | 92 } |
104 | 93 |
105 void AudioEncoderCng::SetTargetBitrate(int bits_per_second) { | |
106 speech_encoder_->SetTargetBitrate(bits_per_second); | |
107 } | |
108 | |
109 void AudioEncoderCng::SetProjectedPacketLossRate(double fraction) { | |
110 DCHECK_GE(fraction, 0.0); | |
111 DCHECK_LE(fraction, 1.0); | |
112 speech_encoder_->SetProjectedPacketLossRate(fraction); | |
113 } | |
114 | |
115 AudioEncoder::EncodedInfo AudioEncoderCng::EncodeInternal( | 94 AudioEncoder::EncodedInfo AudioEncoderCng::EncodeInternal( |
116 uint32_t rtp_timestamp, | 95 uint32_t rtp_timestamp, |
117 const int16_t* audio, | 96 const int16_t* audio, |
118 size_t max_encoded_bytes, | 97 size_t max_encoded_bytes, |
119 uint8_t* encoded) { | 98 uint8_t* encoded) { |
120 CHECK_GE(max_encoded_bytes, static_cast<size_t>(num_cng_coefficients_ + 1)); | 99 CHECK_GE(max_encoded_bytes, static_cast<size_t>(num_cng_coefficients_ + 1)); |
121 const size_t samples_per_10ms_frame = SamplesPer10msFrame(); | 100 const size_t samples_per_10ms_frame = SamplesPer10msFrame(); |
122 CHECK_EQ(speech_buffer_.size(), | 101 CHECK_EQ(speech_buffer_.size(), |
123 rtp_timestamps_.size() * samples_per_10ms_frame); | 102 rtp_timestamps_.size() * samples_per_10ms_frame); |
124 rtp_timestamps_.push_back(rtp_timestamp); | 103 rtp_timestamps_.push_back(rtp_timestamp); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 } | 155 } |
177 | 156 |
178 speech_buffer_.erase( | 157 speech_buffer_.erase( |
179 speech_buffer_.begin(), | 158 speech_buffer_.begin(), |
180 speech_buffer_.begin() + frames_to_encode * samples_per_10ms_frame); | 159 speech_buffer_.begin() + frames_to_encode * samples_per_10ms_frame); |
181 rtp_timestamps_.erase(rtp_timestamps_.begin(), | 160 rtp_timestamps_.erase(rtp_timestamps_.begin(), |
182 rtp_timestamps_.begin() + frames_to_encode); | 161 rtp_timestamps_.begin() + frames_to_encode); |
183 return info; | 162 return info; |
184 } | 163 } |
185 | 164 |
165 void AudioEncoderCng::Reset() { | |
hlundin-webrtc
2015/09/07 20:00:01
There is state in AudioEncoderCng itself, too. Sho
kwiberg-webrtc
2015/09/08 10:47:45
Done. This required adding reset support to the Va
hlundin-webrtc
2015/09/08 11:21:26
Acknowledged.
| |
166 speech_encoder_->Reset(); | |
167 } | |
168 | |
169 bool AudioEncoderCng::SetFec(bool enable) { | |
170 return speech_encoder_->SetFec(enable); | |
171 } | |
172 | |
173 bool AudioEncoderCng::SetDtx(bool enable) { | |
174 return speech_encoder_->SetDtx(enable); | |
175 } | |
176 | |
177 bool AudioEncoderCng::SetApplication(Application application) { | |
178 return speech_encoder_->SetApplication(application); | |
179 } | |
180 | |
181 bool AudioEncoderCng::SetMaxPlaybackRate(int frequency_hz) { | |
182 return speech_encoder_->SetMaxPlaybackRate(frequency_hz); | |
183 } | |
184 | |
185 void AudioEncoderCng::SetProjectedPacketLossRate(double fraction) { | |
186 speech_encoder_->SetProjectedPacketLossRate(fraction); | |
187 } | |
188 | |
189 void AudioEncoderCng::SetTargetBitrate(int bits_per_second) { | |
190 speech_encoder_->SetTargetBitrate(bits_per_second); | |
191 } | |
192 | |
193 void AudioEncoderCng::SetMaxBitrate(int max_bps) { | |
194 speech_encoder_->SetMaxBitrate(max_bps); | |
195 } | |
196 | |
197 void AudioEncoderCng::SetMaxPayloadSize(int max_payload_size_bytes) { | |
198 speech_encoder_->SetMaxPayloadSize(max_payload_size_bytes); | |
199 } | |
200 | |
186 AudioEncoder::EncodedInfo AudioEncoderCng::EncodePassive( | 201 AudioEncoder::EncodedInfo AudioEncoderCng::EncodePassive( |
187 size_t frames_to_encode, | 202 size_t frames_to_encode, |
188 size_t max_encoded_bytes, | 203 size_t max_encoded_bytes, |
189 uint8_t* encoded) { | 204 uint8_t* encoded) { |
190 bool force_sid = last_frame_active_; | 205 bool force_sid = last_frame_active_; |
191 bool output_produced = false; | 206 bool output_produced = false; |
192 const size_t samples_per_10ms_frame = SamplesPer10msFrame(); | 207 const size_t samples_per_10ms_frame = SamplesPer10msFrame(); |
193 CHECK_GE(max_encoded_bytes, frames_to_encode * samples_per_10ms_frame); | 208 CHECK_GE(max_encoded_bytes, frames_to_encode * samples_per_10ms_frame); |
194 AudioEncoder::EncodedInfo info; | 209 AudioEncoder::EncodedInfo info; |
195 for (size_t i = 0; i < frames_to_encode; ++i) { | 210 for (size_t i = 0; i < frames_to_encode; ++i) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 } | 248 } |
234 } | 249 } |
235 return info; | 250 return info; |
236 } | 251 } |
237 | 252 |
238 size_t AudioEncoderCng::SamplesPer10msFrame() const { | 253 size_t AudioEncoderCng::SamplesPer10msFrame() const { |
239 return rtc::CheckedDivExact(10 * SampleRateHz(), 1000); | 254 return rtc::CheckedDivExact(10 * SampleRateHz(), 1000); |
240 } | 255 } |
241 | 256 |
242 } // namespace webrtc | 257 } // namespace webrtc |
OLD | NEW |