Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc

Issue 1881003003: Reland Remove the deprecated EncodeInternal interface from AudioEncoder (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Renamed ApproximateEncodedBytes to SufficientOutputBufferSize in Opus Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 RTC_CHECK(RecreateEncoderInstance(config)); 93 RTC_CHECK(RecreateEncoderInstance(config));
94 } 94 }
95 95
96 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) 96 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst)
97 : AudioEncoderOpus(CreateConfig(codec_inst)) {} 97 : AudioEncoderOpus(CreateConfig(codec_inst)) {}
98 98
99 AudioEncoderOpus::~AudioEncoderOpus() { 99 AudioEncoderOpus::~AudioEncoderOpus() {
100 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); 100 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_));
101 } 101 }
102 102
103 size_t AudioEncoderOpus::MaxEncodedBytes() const {
104 // Calculate the number of bytes we expect the encoder to produce,
105 // then multiply by two to give a wide margin for error.
106 const size_t bytes_per_millisecond =
107 static_cast<size_t>(config_.bitrate_bps / (1000 * 8) + 1);
108 const size_t approx_encoded_bytes =
109 Num10msFramesPerPacket() * 10 * bytes_per_millisecond;
110 return 2 * approx_encoded_bytes;
111 }
112
113 int AudioEncoderOpus::SampleRateHz() const { 103 int AudioEncoderOpus::SampleRateHz() const {
114 return kSampleRateHz; 104 return kSampleRateHz;
115 } 105 }
116 106
117 size_t AudioEncoderOpus::NumChannels() const { 107 size_t AudioEncoderOpus::NumChannels() const {
118 return config_.num_channels; 108 return config_.num_channels;
119 } 109 }
120 110
121 size_t AudioEncoderOpus::Num10MsFramesInNextPacket() const { 111 size_t AudioEncoderOpus::Num10MsFramesInNextPacket() const {
122 return Num10msFramesPerPacket(); 112 return Num10msFramesPerPacket();
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 first_timestamp_in_buffer_ = rtp_timestamp; 181 first_timestamp_in_buffer_ = rtp_timestamp;
192 182
193 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); 183 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend());
194 if (input_buffer_.size() < 184 if (input_buffer_.size() <
195 (Num10msFramesPerPacket() * SamplesPer10msFrame())) { 185 (Num10msFramesPerPacket() * SamplesPer10msFrame())) {
196 return EncodedInfo(); 186 return EncodedInfo();
197 } 187 }
198 RTC_CHECK_EQ(input_buffer_.size(), 188 RTC_CHECK_EQ(input_buffer_.size(),
199 Num10msFramesPerPacket() * SamplesPer10msFrame()); 189 Num10msFramesPerPacket() * SamplesPer10msFrame());
200 190
201 const size_t max_encoded_bytes = MaxEncodedBytes(); 191 const size_t max_encoded_bytes = SufficientOutputBufferSize();
202 EncodedInfo info; 192 EncodedInfo info;
203 info.encoded_bytes = 193 info.encoded_bytes =
204 encoded->AppendData( 194 encoded->AppendData(
205 max_encoded_bytes, [&] (rtc::ArrayView<uint8_t> encoded) { 195 max_encoded_bytes, [&] (rtc::ArrayView<uint8_t> encoded) {
206 int status = WebRtcOpus_Encode( 196 int status = WebRtcOpus_Encode(
207 inst_, &input_buffer_[0], 197 inst_, &input_buffer_[0],
208 rtc::CheckedDivExact(input_buffer_.size(), 198 rtc::CheckedDivExact(input_buffer_.size(),
209 config_.num_channels), 199 config_.num_channels),
210 rtc::saturated_cast<int16_t>(max_encoded_bytes), 200 rtc::saturated_cast<int16_t>(max_encoded_bytes),
211 encoded.data()); 201 encoded.data());
(...skipping 12 matching lines...) Expand all
224 } 214 }
225 215
226 size_t AudioEncoderOpus::Num10msFramesPerPacket() const { 216 size_t AudioEncoderOpus::Num10msFramesPerPacket() const {
227 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10)); 217 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10));
228 } 218 }
229 219
230 size_t AudioEncoderOpus::SamplesPer10msFrame() const { 220 size_t AudioEncoderOpus::SamplesPer10msFrame() const {
231 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels; 221 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels;
232 } 222 }
233 223
224 size_t AudioEncoderOpus::SufficientOutputBufferSize() const {
225 // Calculate the number of bytes we expect the encoder to produce,
226 // then multiply by two to give a wide margin for error.
227 const size_t bytes_per_millisecond =
228 static_cast<size_t>(config_.bitrate_bps / (1000 * 8) + 1);
229 const size_t approx_encoded_bytes =
230 Num10msFramesPerPacket() * 10 * bytes_per_millisecond;
231 return 2 * approx_encoded_bytes;
232 }
233
234 // If the given config is OK, recreate the Opus encoder instance with those 234 // If the given config is OK, recreate the Opus encoder instance with those
235 // settings, save the config, and return true. Otherwise, do nothing and return 235 // settings, save the config, and return true. Otherwise, do nothing and return
236 // false. 236 // false.
237 bool AudioEncoderOpus::RecreateEncoderInstance(const Config& config) { 237 bool AudioEncoderOpus::RecreateEncoderInstance(const Config& config) {
238 if (!config.IsOk()) 238 if (!config.IsOk())
239 return false; 239 return false;
240 if (inst_) 240 if (inst_)
241 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); 241 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_));
242 input_buffer_.clear(); 242 input_buffer_.clear();
243 input_buffer_.reserve(Num10msFramesPerPacket() * SamplesPer10msFrame()); 243 input_buffer_.reserve(Num10msFramesPerPacket() * SamplesPer10msFrame());
(...skipping 14 matching lines...) Expand all
258 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); 258 RTC_CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_));
259 } 259 }
260 RTC_CHECK_EQ(0, 260 RTC_CHECK_EQ(0,
261 WebRtcOpus_SetPacketLossRate( 261 WebRtcOpus_SetPacketLossRate(
262 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); 262 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5)));
263 config_ = config; 263 config_ = config;
264 return true; 264 return true;
265 } 265 }
266 266
267 } // namespace webrtc 267 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698