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