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/libyuv/include/webrtc_libyuv.h" | 17 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
18 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 18 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
19 #include "webrtc/modules/video_coding/encoded_frame.h" | 19 #include "webrtc/modules/video_coding/encoded_frame.h" |
20 #include "webrtc/modules/video_coding/utility/quality_scaler.h" | 20 #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
21 #include "webrtc/modules/video_coding/video_coding_impl.h" | 21 #include "webrtc/modules/video_coding/video_coding_impl.h" |
22 #include "webrtc/system_wrappers/include/clock.h" | 22 #include "webrtc/system_wrappers/include/clock.h" |
23 | 23 |
24 namespace webrtc { | 24 namespace webrtc { |
25 namespace vcm { | 25 namespace vcm { |
26 | 26 |
27 VideoSender::VideoSender(Clock* clock, | 27 VideoSender::VideoSender(Clock* clock, |
28 EncodedImageCallback* post_encode_callback, | 28 EncodedImageCallback* post_encode_callback, |
29 VideoEncoderRateObserver* encoder_rate_observer, | 29 VideoEncoderRateObserver* encoder_rate_observer, |
30 VCMQMSettingsCallback* qm_settings_callback) | 30 VCMQMSettingsCallback* qm_settings_callback, |
| 31 VCMSendStatisticsCallback* send_stats_callback) |
31 : clock_(clock), | 32 : clock_(clock), |
32 _encoder(nullptr), | 33 _encoder(nullptr), |
33 _encodedFrameCallback(post_encode_callback), | 34 _encodedFrameCallback(post_encode_callback), |
34 _mediaOpt(clock_), | 35 _mediaOpt(clock_), |
35 _sendStatsCallback(nullptr), | 36 send_stats_callback_(send_stats_callback), |
36 _codecDataBase(encoder_rate_observer, &_encodedFrameCallback), | 37 _codecDataBase(encoder_rate_observer, &_encodedFrameCallback), |
37 frame_dropper_enabled_(true), | 38 frame_dropper_enabled_(true), |
38 _sendStatsTimer(1000, clock_), | 39 _sendStatsTimer(1000, clock_), |
39 current_codec_(), | 40 current_codec_(), |
40 qm_settings_callback_(qm_settings_callback), | 41 qm_settings_callback_(qm_settings_callback), |
41 protection_callback_(nullptr), | 42 protection_callback_(nullptr), |
42 encoder_params_({0, 0, 0, 0}), | 43 encoder_params_({0, 0, 0, 0}), |
43 encoder_has_internal_source_(false), | 44 encoder_has_internal_source_(false), |
44 next_frame_types_(1, kVideoFrameDelta) { | 45 next_frame_types_(1, kVideoFrameDelta) { |
45 // 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 |
46 // 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 |
47 // one external project (diffractor). | 48 // one external project (diffractor). |
48 _mediaOpt.EnableQM(qm_settings_callback_ != nullptr); | 49 _mediaOpt.EnableQM(qm_settings_callback_ != nullptr); |
49 _mediaOpt.Reset(); | 50 _mediaOpt.Reset(); |
| 51 _encodedFrameCallback.SetMediaOpt(&_mediaOpt); |
50 main_thread_.DetachFromThread(); | 52 main_thread_.DetachFromThread(); |
51 } | 53 } |
52 | 54 |
53 VideoSender::~VideoSender() {} | 55 VideoSender::~VideoSender() {} |
54 | 56 |
55 void VideoSender::Process() { | 57 void VideoSender::Process() { |
56 if (_sendStatsTimer.TimeUntilProcess() == 0) { | 58 if (_sendStatsTimer.TimeUntilProcess() == 0) { |
| 59 // |_sendStatsTimer.Processed()| must be called. Otherwise |
| 60 // VideoSender::Process() will be called in an infinit loop. |
57 _sendStatsTimer.Processed(); | 61 _sendStatsTimer.Processed(); |
58 rtc::CritScope cs(&process_crit_); | 62 if (!send_stats_callback_) { |
59 if (_sendStatsCallback != nullptr) { | 63 return; |
60 uint32_t bitRate = _mediaOpt.SentBitRate(); | |
61 uint32_t frameRate = _mediaOpt.SentFrameRate(); | |
62 _sendStatsCallback->SendStatistics(bitRate, frameRate); | |
63 } | 64 } |
| 65 uint32_t bitRate = _mediaOpt.SentBitRate(); |
| 66 uint32_t frameRate = _mediaOpt.SentFrameRate(); |
| 67 std::string encoder_name; |
| 68 { |
| 69 rtc::CritScope cs(¶ms_crit_); |
| 70 // Copy the string here so that we don't hold |params_crit_| in the CB. |
| 71 encoder_name = encoder_name_; |
| 72 } |
| 73 send_stats_callback_->SendStatistics(bitRate, frameRate, |
| 74 std::move(encoder_name)); |
64 } | 75 } |
65 | 76 |
66 { | 77 { |
67 rtc::CritScope cs(¶ms_crit_); | 78 rtc::CritScope cs(¶ms_crit_); |
68 // Force an encoder parameters update, so that incoming frame rate is | 79 // Force an encoder parameters update, so that incoming frame rate is |
69 // updated even if bandwidth hasn't changed. | 80 // updated even if bandwidth hasn't changed. |
70 encoder_params_.input_frame_rate = _mediaOpt.InputFrameRate(); | 81 encoder_params_.input_frame_rate = _mediaOpt.InputFrameRate(); |
71 } | 82 } |
72 } | 83 } |
73 | 84 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 return; | 239 return; |
229 | 240 |
230 if (params.input_frame_rate == 0) { | 241 if (params.input_frame_rate == 0) { |
231 // No frame rate estimate available, use default. | 242 // No frame rate estimate available, use default. |
232 params.input_frame_rate = current_codec_.maxFramerate; | 243 params.input_frame_rate = current_codec_.maxFramerate; |
233 } | 244 } |
234 if (_encoder != nullptr) | 245 if (_encoder != nullptr) |
235 _encoder->SetEncoderParameters(params); | 246 _encoder->SetEncoderParameters(params); |
236 } | 247 } |
237 | 248 |
238 int32_t VideoSender::RegisterTransportCallback( | |
239 VCMPacketizationCallback* transport) { | |
240 rtc::CritScope lock(&encoder_crit_); | |
241 _encodedFrameCallback.SetMediaOpt(&_mediaOpt); | |
242 _encodedFrameCallback.SetTransportCallback(transport); | |
243 return VCM_OK; | |
244 } | |
245 | |
246 // Register video output information callback which will be called to deliver | |
247 // information about the video stream produced by the encoder, for instance the | |
248 // average frame rate and bit rate. | |
249 int32_t VideoSender::RegisterSendStatisticsCallback( | |
250 VCMSendStatisticsCallback* sendStats) { | |
251 rtc::CritScope cs(&process_crit_); | |
252 _sendStatsCallback = sendStats; | |
253 return VCM_OK; | |
254 } | |
255 | |
256 // Register a video protection callback which will be called to deliver the | 249 // Register a video protection callback which will be called to deliver the |
257 // requested FEC rate and NACK status (on/off). | 250 // requested FEC rate and NACK status (on/off). |
258 // Note: this callback is assumed to only be registered once and before it is | 251 // Note: this callback is assumed to only be registered once and before it is |
259 // used in this class. | 252 // used in this class. |
260 int32_t VideoSender::RegisterProtectionCallback( | 253 int32_t VideoSender::RegisterProtectionCallback( |
261 VCMProtectionCallback* protection_callback) { | 254 VCMProtectionCallback* protection_callback) { |
262 RTC_DCHECK(protection_callback == nullptr || protection_callback_ == nullptr); | 255 RTC_DCHECK(protection_callback == nullptr || protection_callback_ == nullptr); |
263 protection_callback_ = protection_callback; | 256 protection_callback_ = protection_callback; |
264 return VCM_OK; | 257 return VCM_OK; |
265 } | 258 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 converted_frame = converted_frame.ConvertNativeToI420Frame(); | 315 converted_frame = converted_frame.ConvertNativeToI420Frame(); |
323 RTC_CHECK(!converted_frame.IsZeroSize()) | 316 RTC_CHECK(!converted_frame.IsZeroSize()) |
324 << "Frame conversion failed, won't be able to encode frame."; | 317 << "Frame conversion failed, won't be able to encode frame."; |
325 } | 318 } |
326 int32_t ret = | 319 int32_t ret = |
327 _encoder->Encode(converted_frame, codecSpecificInfo, next_frame_types); | 320 _encoder->Encode(converted_frame, codecSpecificInfo, next_frame_types); |
328 if (ret < 0) { | 321 if (ret < 0) { |
329 LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret; | 322 LOG(LS_ERROR) << "Failed to encode frame. Error code: " << ret; |
330 return ret; | 323 return ret; |
331 } | 324 } |
| 325 |
332 { | 326 { |
| 327 rtc::CritScope lock(¶ms_crit_); |
| 328 encoder_name_ = _encoder->ImplementationName(); |
| 329 |
333 // Change all keyframe requests to encode delta frames the next time. | 330 // Change all keyframe requests to encode delta frames the next time. |
334 rtc::CritScope lock(¶ms_crit_); | |
335 for (size_t i = 0; i < next_frame_types_.size(); ++i) { | 331 for (size_t i = 0; i < next_frame_types_.size(); ++i) { |
336 // Check for equality (same requested as before encoding) to not | 332 // Check for equality (same requested as before encoding) to not |
337 // accidentally drop a keyframe request while encoding. | 333 // accidentally drop a keyframe request while encoding. |
338 if (next_frame_types[i] == next_frame_types_[i]) | 334 if (next_frame_types[i] == next_frame_types_[i]) |
339 next_frame_types_[i] = kVideoFrameDelta; | 335 next_frame_types_[i] = kVideoFrameDelta; |
340 } | 336 } |
341 } | 337 } |
342 if (qm_settings_callback_) | 338 if (qm_settings_callback_) |
343 qm_settings_callback_->SetTargetFramerate(_encoder->GetTargetFramerate()); | 339 qm_settings_callback_->SetTargetFramerate(_encoder->GetTargetFramerate()); |
344 return VCM_OK; | 340 return VCM_OK; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 // 10 kbps. | 390 // 10 kbps. |
395 int window_bps = std::max(threshold_bps / 10, 10000); | 391 int window_bps = std::max(threshold_bps / 10, 10000); |
396 _mediaOpt.SuspendBelowMinBitrate(threshold_bps, window_bps); | 392 _mediaOpt.SuspendBelowMinBitrate(threshold_bps, window_bps); |
397 } | 393 } |
398 | 394 |
399 bool VideoSender::VideoSuspended() const { | 395 bool VideoSender::VideoSuspended() const { |
400 return _mediaOpt.IsVideoSuspended(); | 396 return _mediaOpt.IsVideoSuspended(); |
401 } | 397 } |
402 } // namespace vcm | 398 } // namespace vcm |
403 } // namespace webrtc | 399 } // namespace webrtc |
OLD | NEW |