| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 | 62 |
| 63 AudioEncoderOpus::AudioEncoderOpus(const Config& config) | 63 AudioEncoderOpus::AudioEncoderOpus(const Config& config) |
| 64 : num_10ms_frames_per_packet_( | 64 : num_10ms_frames_per_packet_( |
| 65 static_cast<size_t>(rtc::CheckedDivExact(config.frame_size_ms, 10))), | 65 static_cast<size_t>(rtc::CheckedDivExact(config.frame_size_ms, 10))), |
| 66 num_channels_(config.num_channels), | 66 num_channels_(config.num_channels), |
| 67 payload_type_(config.payload_type), | 67 payload_type_(config.payload_type), |
| 68 application_(config.application), | 68 application_(config.application), |
| 69 dtx_enabled_(config.dtx_enabled), | 69 dtx_enabled_(config.dtx_enabled), |
| 70 samples_per_10ms_frame_(static_cast<size_t>( | 70 samples_per_10ms_frame_( |
| 71 rtc::CheckedDivExact(kSampleRateHz, 100) * num_channels_)), | 71 rtc::CheckedDivExact(kSampleRateHz, 100) * num_channels_), |
| 72 packet_loss_rate_(0.0) { | 72 packet_loss_rate_(0.0) { |
| 73 CHECK(config.IsOk()); | 73 CHECK(config.IsOk()); |
| 74 input_buffer_.reserve(num_10ms_frames_per_packet_ * samples_per_10ms_frame_); | 74 input_buffer_.reserve(num_10ms_frames_per_packet_ * samples_per_10ms_frame_); |
| 75 CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, num_channels_, application_)); | 75 CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, num_channels_, application_)); |
| 76 SetTargetBitrate(config.bitrate_bps); | 76 SetTargetBitrate(config.bitrate_bps); |
| 77 if (config.fec_enabled) { | 77 if (config.fec_enabled) { |
| 78 CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); | 78 CHECK_EQ(0, WebRtcOpus_EnableFec(inst_)); |
| 79 } else { | 79 } else { |
| 80 CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); | 80 CHECK_EQ(0, WebRtcOpus_DisableFec(inst_)); |
| 81 } | 81 } |
| 82 CHECK_EQ(0, | 82 CHECK_EQ(0, |
| 83 WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz)); | 83 WebRtcOpus_SetMaxPlaybackRate(inst_, config.max_playback_rate_hz)); |
| 84 CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, config.complexity)); | 84 CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, config.complexity)); |
| 85 if (config.dtx_enabled) { | 85 if (config.dtx_enabled) { |
| 86 CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); | 86 CHECK_EQ(0, WebRtcOpus_EnableDtx(inst_)); |
| 87 } else { | 87 } else { |
| 88 CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); | 88 CHECK_EQ(0, WebRtcOpus_DisableDtx(inst_)); |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 AudioEncoderOpus::~AudioEncoderOpus() { | 92 AudioEncoderOpus::~AudioEncoderOpus() { |
| 93 CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); | 93 CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 int AudioEncoderOpus::SampleRateHz() const { | 96 int AudioEncoderOpus::SampleRateHz() const { |
| 97 return kSampleRateHz; | 97 return kSampleRateHz; |
| 98 } | 98 } |
| 99 | 99 |
| 100 int AudioEncoderOpus::NumChannels() const { | 100 size_t AudioEncoderOpus::NumChannels() const { |
| 101 return num_channels_; | 101 return num_channels_; |
| 102 } | 102 } |
| 103 | 103 |
| 104 size_t AudioEncoderOpus::MaxEncodedBytes() const { | 104 size_t AudioEncoderOpus::MaxEncodedBytes() const { |
| 105 // Calculate the number of bytes we expect the encoder to produce, | 105 // Calculate the number of bytes we expect the encoder to produce, |
| 106 // then multiply by two to give a wide margin for error. | 106 // then multiply by two to give a wide margin for error. |
| 107 size_t bytes_per_millisecond = | 107 size_t bytes_per_millisecond = |
| 108 static_cast<size_t>(bitrate_bps_ / (1000 * 8) + 1); | 108 static_cast<size_t>(bitrate_bps_ / (1000 * 8) + 1); |
| 109 size_t approx_encoded_bytes = | 109 size_t approx_encoded_bytes = |
| 110 num_10ms_frames_per_packet_ * 10 * bytes_per_millisecond; | 110 num_10ms_frames_per_packet_ * 10 * bytes_per_millisecond; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 input_buffer_.insert(input_buffer_.end(), audio, | 186 input_buffer_.insert(input_buffer_.end(), audio, |
| 187 audio + samples_per_10ms_frame_); | 187 audio + samples_per_10ms_frame_); |
| 188 if (input_buffer_.size() < | 188 if (input_buffer_.size() < |
| 189 (num_10ms_frames_per_packet_ * samples_per_10ms_frame_)) { | 189 (num_10ms_frames_per_packet_ * samples_per_10ms_frame_)) { |
| 190 return EncodedInfo(); | 190 return EncodedInfo(); |
| 191 } | 191 } |
| 192 CHECK_EQ(input_buffer_.size(), | 192 CHECK_EQ(input_buffer_.size(), |
| 193 num_10ms_frames_per_packet_ * samples_per_10ms_frame_); | 193 num_10ms_frames_per_packet_ * samples_per_10ms_frame_); |
| 194 int status = WebRtcOpus_Encode( | 194 int status = WebRtcOpus_Encode( |
| 195 inst_, &input_buffer_[0], | 195 inst_, &input_buffer_[0], |
| 196 rtc::CheckedDivExact(input_buffer_.size(), | 196 rtc::CheckedDivExact(input_buffer_.size(), num_channels_), |
| 197 static_cast<size_t>(num_channels_)), | |
| 198 max_encoded_bytes, encoded); | 197 max_encoded_bytes, encoded); |
| 199 CHECK_GE(status, 0); // Fails only if fed invalid data. | 198 CHECK_GE(status, 0); // Fails only if fed invalid data. |
| 200 input_buffer_.clear(); | 199 input_buffer_.clear(); |
| 201 EncodedInfo info; | 200 EncodedInfo info; |
| 202 info.encoded_bytes = static_cast<size_t>(status); | 201 info.encoded_bytes = static_cast<size_t>(status); |
| 203 info.encoded_timestamp = first_timestamp_in_buffer_; | 202 info.encoded_timestamp = first_timestamp_in_buffer_; |
| 204 info.payload_type = payload_type_; | 203 info.payload_type = payload_type_; |
| 205 info.send_even_if_empty = true; // Allows Opus to send empty packets. | 204 info.send_even_if_empty = true; // Allows Opus to send empty packets. |
| 206 info.speech = (status > 0); | 205 info.speech = (status > 0); |
| 207 return info; | 206 return info; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 return Reconstruct(conf); | 248 return Reconstruct(conf); |
| 250 } | 249 } |
| 251 | 250 |
| 252 bool AudioEncoderMutableOpus::SetMaxPlaybackRate(int frequency_hz) { | 251 bool AudioEncoderMutableOpus::SetMaxPlaybackRate(int frequency_hz) { |
| 253 auto conf = config(); | 252 auto conf = config(); |
| 254 conf.max_playback_rate_hz = frequency_hz; | 253 conf.max_playback_rate_hz = frequency_hz; |
| 255 return Reconstruct(conf); | 254 return Reconstruct(conf); |
| 256 } | 255 } |
| 257 | 256 |
| 258 } // namespace webrtc | 257 } // namespace webrtc |
| OLD | NEW |