| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 | 11 |
| 12 #include <algorithm> // std::max | 12 #include <algorithm> // std::max |
| 13 | 13 |
| 14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
| 16 #include "webrtc/common_types.h" | 16 #include "webrtc/common_types.h" |
| 17 #include "webrtc/common_video/include/video_bitrate_allocator.h" | |
| 18 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 17 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 19 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" | |
| 20 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 18 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
| 21 #include "webrtc/modules/video_coding/encoded_frame.h" | 19 #include "webrtc/modules/video_coding/encoded_frame.h" |
| 22 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h" | |
| 23 #include "webrtc/modules/video_coding/utility/quality_scaler.h" | 20 #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
| 24 #include "webrtc/modules/video_coding/video_coding_impl.h" | 21 #include "webrtc/modules/video_coding/video_coding_impl.h" |
| 25 #include "webrtc/system_wrappers/include/clock.h" | 22 #include "webrtc/system_wrappers/include/clock.h" |
| 26 | 23 |
| 27 namespace webrtc { | 24 namespace webrtc { |
| 28 namespace vcm { | 25 namespace vcm { |
| 29 | 26 |
| 30 VideoSender::VideoSender(Clock* clock, | 27 VideoSender::VideoSender(Clock* clock, |
| 31 EncodedImageCallback* post_encode_callback, | 28 EncodedImageCallback* post_encode_callback, |
| 32 VCMSendStatisticsCallback* send_stats_callback) | 29 VCMSendStatisticsCallback* send_stats_callback) |
| 33 : clock_(clock), | 30 : clock_(clock), |
| 34 _encoder(nullptr), | 31 _encoder(nullptr), |
| 35 _mediaOpt(clock_), | 32 _mediaOpt(clock_), |
| 36 _encodedFrameCallback(post_encode_callback, &_mediaOpt), | 33 _encodedFrameCallback(post_encode_callback, &_mediaOpt), |
| 37 send_stats_callback_(send_stats_callback), | 34 send_stats_callback_(send_stats_callback), |
| 38 _codecDataBase(&_encodedFrameCallback), | 35 _codecDataBase(&_encodedFrameCallback), |
| 39 frame_dropper_enabled_(true), | 36 frame_dropper_enabled_(true), |
| 40 _sendStatsTimer(1000, clock_), | 37 _sendStatsTimer(1000, clock_), |
| 41 current_codec_(), | 38 current_codec_(), |
| 42 encoder_params_({BitrateAllocation(), 0, 0, 0}), | 39 encoder_params_({0, 0, 0, 0}), |
| 43 encoder_has_internal_source_(false), | 40 encoder_has_internal_source_(false), |
| 44 next_frame_types_(1, kVideoFrameDelta) { | 41 next_frame_types_(1, kVideoFrameDelta) { |
| 45 _mediaOpt.Reset(); | 42 _mediaOpt.Reset(); |
| 46 // Allow VideoSender to be created on one thread but used on another, post | 43 // Allow VideoSender to be created on one thread but used on another, post |
| 47 // construction. This is currently how this class is being used by at least | 44 // construction. This is currently how this class is being used by at least |
| 48 // one external project (diffractor). | 45 // one external project (diffractor). |
| 49 sequenced_checker_.Detach(); | 46 sequenced_checker_.Detach(); |
| 50 } | 47 } |
| 51 | 48 |
| 52 VideoSender::~VideoSender() {} | 49 VideoSender::~VideoSender() {} |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 165 } |
| 169 | 166 |
| 170 // Get encode bitrate | 167 // Get encode bitrate |
| 171 int VideoSender::Bitrate(unsigned int* bitrate) const { | 168 int VideoSender::Bitrate(unsigned int* bitrate) const { |
| 172 RTC_DCHECK(sequenced_checker_.CalledSequentially()); | 169 RTC_DCHECK(sequenced_checker_.CalledSequentially()); |
| 173 // Since we're running on the thread that's the only thread known to modify | 170 // Since we're running on the thread that's the only thread known to modify |
| 174 // the value of _encoder, we don't need to grab the lock here. | 171 // the value of _encoder, we don't need to grab the lock here. |
| 175 | 172 |
| 176 if (!_encoder) | 173 if (!_encoder) |
| 177 return VCM_UNINITIALIZED; | 174 return VCM_UNINITIALIZED; |
| 178 *bitrate = _encoder->GetEncoderParameters().target_bitrate.get_sum_bps(); | 175 *bitrate = _encoder->GetEncoderParameters().target_bitrate; |
| 179 return 0; | 176 return 0; |
| 180 } | 177 } |
| 181 | 178 |
| 182 // Get encode frame rate | 179 // Get encode frame rate |
| 183 int VideoSender::FrameRate(unsigned int* framerate) const { | 180 int VideoSender::FrameRate(unsigned int* framerate) const { |
| 184 RTC_DCHECK(sequenced_checker_.CalledSequentially()); | 181 RTC_DCHECK(sequenced_checker_.CalledSequentially()); |
| 185 // Since we're running on the thread that's the only thread known to modify | 182 // Since we're running on the thread that's the only thread known to modify |
| 186 // the value of _encoder, we don't need to grab the lock here. | 183 // the value of _encoder, we don't need to grab the lock here. |
| 187 | 184 |
| 188 if (!_encoder) | 185 if (!_encoder) |
| 189 return VCM_UNINITIALIZED; | 186 return VCM_UNINITIALIZED; |
| 190 | 187 |
| 191 *framerate = _encoder->GetEncoderParameters().input_frame_rate; | 188 *framerate = _encoder->GetEncoderParameters().input_frame_rate; |
| 192 return 0; | 189 return 0; |
| 193 } | 190 } |
| 194 | 191 |
| 195 int32_t VideoSender::UpdateChannelParemeters( | 192 int32_t VideoSender::SetChannelParameters(uint32_t target_bitrate, |
| 196 VideoBitrateAllocator* bitrate_allocator) { | 193 uint8_t lossRate, |
| 197 EncoderParameters encoder_params; | 194 int64_t rtt) { |
| 198 { | 195 uint32_t target_rate = |
| 199 rtc::CritScope cs(¶ms_crit_); | 196 _mediaOpt.SetTargetRates(target_bitrate, lossRate, rtt); |
| 200 encoder_params = encoder_params_; | |
| 201 } | |
| 202 | 197 |
| 203 return SetChannelParameters(encoder_params.target_bitrate.get_sum_bps(), | 198 uint32_t input_frame_rate = _mediaOpt.InputFrameRate(); |
| 204 encoder_params.loss_rate, encoder_params.rtt, | |
| 205 bitrate_allocator); | |
| 206 } | |
| 207 | 199 |
| 208 int32_t VideoSender::SetChannelParameters( | 200 EncoderParameters encoder_params = {target_rate, lossRate, rtt, |
| 209 uint32_t target_bitrate_bps, | |
| 210 uint8_t lossRate, | |
| 211 int64_t rtt, | |
| 212 VideoBitrateAllocator* bitrate_allocator) { | |
| 213 uint32_t video_target_rate_bps = | |
| 214 _mediaOpt.SetTargetRates(target_bitrate_bps, lossRate, rtt); | |
| 215 uint32_t input_frame_rate = _mediaOpt.InputFrameRate(); | |
| 216 BitrateAllocation bitrate_allocation; | |
| 217 if (bitrate_allocator) { | |
| 218 bitrate_allocation = bitrate_allocator->GetAllocation(video_target_rate_bps, | |
| 219 input_frame_rate); | |
| 220 } else { | |
| 221 DefaultVideoBitrateAllocator default_allocator(current_codec_); | |
| 222 bitrate_allocation = default_allocator.GetAllocation(video_target_rate_bps, | |
| 223 input_frame_rate); | |
| 224 } | |
| 225 | |
| 226 EncoderParameters encoder_params = {bitrate_allocation, lossRate, rtt, | |
| 227 input_frame_rate}; | 201 input_frame_rate}; |
| 228 bool encoder_has_internal_source; | 202 bool encoder_has_internal_source; |
| 229 { | 203 { |
| 230 rtc::CritScope cs(¶ms_crit_); | 204 rtc::CritScope cs(¶ms_crit_); |
| 231 encoder_params_ = encoder_params; | 205 encoder_params_ = encoder_params; |
| 232 encoder_has_internal_source = encoder_has_internal_source_; | 206 encoder_has_internal_source = encoder_has_internal_source_; |
| 233 } | 207 } |
| 234 | 208 |
| 235 // For encoders with internal sources, we need to tell the encoder directly, | 209 // For encoders with internal sources, we need to tell the encoder directly, |
| 236 // instead of waiting for an AddVideoFrame that will never come (internal | 210 // instead of waiting for an AddVideoFrame that will never come (internal |
| (...skipping 10 matching lines...) Expand all Loading... |
| 247 | 221 |
| 248 void VideoSender::SetEncoderParameters(EncoderParameters params, | 222 void VideoSender::SetEncoderParameters(EncoderParameters params, |
| 249 bool has_internal_source) { | 223 bool has_internal_source) { |
| 250 // |target_bitrate == 0 | means that the network is down or the send pacer is | 224 // |target_bitrate == 0 | means that the network is down or the send pacer is |
| 251 // full. We currently only report this if the encoder has an internal source. | 225 // full. We currently only report this if the encoder has an internal source. |
| 252 // If the encoder does not have an internal source, higher levels are expected | 226 // If the encoder does not have an internal source, higher levels are expected |
| 253 // to not call AddVideoFrame. We do this since its unclear how current | 227 // to not call AddVideoFrame. We do this since its unclear how current |
| 254 // encoder implementations behave when given a zero target bitrate. | 228 // encoder implementations behave when given a zero target bitrate. |
| 255 // TODO(perkj): Make sure all known encoder implementations handle zero | 229 // TODO(perkj): Make sure all known encoder implementations handle zero |
| 256 // target bitrate and remove this check. | 230 // target bitrate and remove this check. |
| 257 if (!has_internal_source && params.target_bitrate.get_sum_bps() == 0) | 231 if (!has_internal_source && params.target_bitrate == 0) |
| 258 return; | 232 return; |
| 259 | 233 |
| 260 if (params.input_frame_rate == 0) { | 234 if (params.input_frame_rate == 0) { |
| 261 // No frame rate estimate available, use default. | 235 // No frame rate estimate available, use default. |
| 262 params.input_frame_rate = current_codec_.maxFramerate; | 236 params.input_frame_rate = current_codec_.maxFramerate; |
| 263 } | 237 } |
| 264 if (_encoder != nullptr) | 238 if (_encoder != nullptr) |
| 265 _encoder->SetEncoderParameters(params); | 239 _encoder->SetEncoderParameters(params); |
| 266 } | 240 } |
| 267 | 241 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 287 encoder_params = encoder_params_; | 261 encoder_params = encoder_params_; |
| 288 next_frame_types = next_frame_types_; | 262 next_frame_types = next_frame_types_; |
| 289 encoder_has_internal_source = encoder_has_internal_source_; | 263 encoder_has_internal_source = encoder_has_internal_source_; |
| 290 } | 264 } |
| 291 rtc::CritScope lock(&encoder_crit_); | 265 rtc::CritScope lock(&encoder_crit_); |
| 292 if (_encoder == nullptr) | 266 if (_encoder == nullptr) |
| 293 return VCM_UNINITIALIZED; | 267 return VCM_UNINITIALIZED; |
| 294 SetEncoderParameters(encoder_params, encoder_has_internal_source); | 268 SetEncoderParameters(encoder_params, encoder_has_internal_source); |
| 295 if (_mediaOpt.DropFrame()) { | 269 if (_mediaOpt.DropFrame()) { |
| 296 LOG(LS_VERBOSE) << "Drop Frame " | 270 LOG(LS_VERBOSE) << "Drop Frame " |
| 297 << "target bitrate " | 271 << "target bitrate " << encoder_params.target_bitrate |
| 298 << encoder_params.target_bitrate.get_sum_bps() | |
| 299 << " loss rate " << encoder_params.loss_rate << " rtt " | 272 << " loss rate " << encoder_params.loss_rate << " rtt " |
| 300 << encoder_params.rtt << " input frame rate " | 273 << encoder_params.rtt << " input frame rate " |
| 301 << encoder_params.input_frame_rate; | 274 << encoder_params.input_frame_rate; |
| 302 _encoder->OnDroppedFrame(); | 275 _encoder->OnDroppedFrame(); |
| 303 return VCM_OK; | 276 return VCM_OK; |
| 304 } | 277 } |
| 305 // TODO(pbos): Make sure setting send codec is synchronized with video | 278 // TODO(pbos): Make sure setting send codec is synchronized with video |
| 306 // processing so frame size always matches. | 279 // processing so frame size always matches. |
| 307 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(), | 280 if (!_codecDataBase.MatchesCurrentResolution(videoFrame.width(), |
| 308 videoFrame.height())) { | 281 videoFrame.height())) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 } | 350 } |
| 378 | 351 |
| 379 int32_t VideoSender::EnableFrameDropper(bool enable) { | 352 int32_t VideoSender::EnableFrameDropper(bool enable) { |
| 380 rtc::CritScope lock(&encoder_crit_); | 353 rtc::CritScope lock(&encoder_crit_); |
| 381 frame_dropper_enabled_ = enable; | 354 frame_dropper_enabled_ = enable; |
| 382 _mediaOpt.EnableFrameDropper(enable); | 355 _mediaOpt.EnableFrameDropper(enable); |
| 383 return VCM_OK; | 356 return VCM_OK; |
| 384 } | 357 } |
| 385 } // namespace vcm | 358 } // namespace vcm |
| 386 } // namespace webrtc | 359 } // namespace webrtc |
| OLD | NEW |