| 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 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| 103 int AudioEncoderOpus::SampleRateHz() const { | 113 int AudioEncoderOpus::SampleRateHz() const { |
| 104 return kSampleRateHz; | 114 return kSampleRateHz; |
| 105 } | 115 } |
| 106 | 116 |
| 107 size_t AudioEncoderOpus::NumChannels() const { | 117 size_t AudioEncoderOpus::NumChannels() const { |
| 108 return config_.num_channels; | 118 return config_.num_channels; |
| 109 } | 119 } |
| 110 | 120 |
| 111 size_t AudioEncoderOpus::Num10MsFramesInNextPacket() const { | 121 size_t AudioEncoderOpus::Num10MsFramesInNextPacket() const { |
| 112 return Num10msFramesPerPacket(); | 122 return Num10msFramesPerPacket(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 first_timestamp_in_buffer_ = rtp_timestamp; | 191 first_timestamp_in_buffer_ = rtp_timestamp; |
| 182 | 192 |
| 183 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); | 193 input_buffer_.insert(input_buffer_.end(), audio.cbegin(), audio.cend()); |
| 184 if (input_buffer_.size() < | 194 if (input_buffer_.size() < |
| 185 (Num10msFramesPerPacket() * SamplesPer10msFrame())) { | 195 (Num10msFramesPerPacket() * SamplesPer10msFrame())) { |
| 186 return EncodedInfo(); | 196 return EncodedInfo(); |
| 187 } | 197 } |
| 188 RTC_CHECK_EQ(input_buffer_.size(), | 198 RTC_CHECK_EQ(input_buffer_.size(), |
| 189 Num10msFramesPerPacket() * SamplesPer10msFrame()); | 199 Num10msFramesPerPacket() * SamplesPer10msFrame()); |
| 190 | 200 |
| 191 const size_t max_encoded_bytes = ApproximateEncodedBytes(); | 201 const size_t max_encoded_bytes = MaxEncodedBytes(); |
| 192 EncodedInfo info; | 202 EncodedInfo info; |
| 193 info.encoded_bytes = | 203 info.encoded_bytes = |
| 194 encoded->AppendData( | 204 encoded->AppendData( |
| 195 max_encoded_bytes, [&] (rtc::ArrayView<uint8_t> encoded) { | 205 max_encoded_bytes, [&] (rtc::ArrayView<uint8_t> encoded) { |
| 196 int status = WebRtcOpus_Encode( | 206 int status = WebRtcOpus_Encode( |
| 197 inst_, &input_buffer_[0], | 207 inst_, &input_buffer_[0], |
| 198 rtc::CheckedDivExact(input_buffer_.size(), | 208 rtc::CheckedDivExact(input_buffer_.size(), |
| 199 config_.num_channels), | 209 config_.num_channels), |
| 200 rtc::saturated_cast<int16_t>(max_encoded_bytes), | 210 rtc::saturated_cast<int16_t>(max_encoded_bytes), |
| 201 encoded.data()); | 211 encoded.data()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 214 } | 224 } |
| 215 | 225 |
| 216 size_t AudioEncoderOpus::Num10msFramesPerPacket() const { | 226 size_t AudioEncoderOpus::Num10msFramesPerPacket() const { |
| 217 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10)); | 227 return static_cast<size_t>(rtc::CheckedDivExact(config_.frame_size_ms, 10)); |
| 218 } | 228 } |
| 219 | 229 |
| 220 size_t AudioEncoderOpus::SamplesPer10msFrame() const { | 230 size_t AudioEncoderOpus::SamplesPer10msFrame() const { |
| 221 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels; | 231 return rtc::CheckedDivExact(kSampleRateHz, 100) * config_.num_channels; |
| 222 } | 232 } |
| 223 | 233 |
| 224 size_t AudioEncoderOpus::ApproximateEncodedBytes() 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 Loading... |
| 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 |
| OLD | NEW |