OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 time_when_posted_ms_(time_when_posted_in_ms), | 219 time_when_posted_ms_(time_when_posted_in_ms), |
220 log_stats_(log_stats) { | 220 log_stats_(log_stats) { |
221 frame_.ShallowCopy(frame); | 221 frame_.ShallowCopy(frame); |
222 ++vie_encoder_->posted_frames_waiting_for_encode_; | 222 ++vie_encoder_->posted_frames_waiting_for_encode_; |
223 } | 223 } |
224 | 224 |
225 private: | 225 private: |
226 bool Run() override { | 226 bool Run() override { |
227 RTC_DCHECK_RUN_ON(&vie_encoder_->encoder_queue_); | 227 RTC_DCHECK_RUN_ON(&vie_encoder_->encoder_queue_); |
228 RTC_DCHECK_GT(vie_encoder_->posted_frames_waiting_for_encode_.Value(), 0); | 228 RTC_DCHECK_GT(vie_encoder_->posted_frames_waiting_for_encode_.Value(), 0); |
229 vie_encoder_->stats_proxy_->OnIncomingFrame(frame_.width(), | |
230 frame_.height()); | |
229 ++vie_encoder_->captured_frame_count_; | 231 ++vie_encoder_->captured_frame_count_; |
230 if (--vie_encoder_->posted_frames_waiting_for_encode_ == 0) { | 232 if (--vie_encoder_->posted_frames_waiting_for_encode_ == 0) { |
231 vie_encoder_->EncodeVideoFrame(frame_, time_when_posted_ms_); | 233 vie_encoder_->EncodeVideoFrame(frame_, time_when_posted_ms_); |
232 } else { | 234 } else { |
233 // There is a newer frame in flight. Do not encode this frame. | 235 // There is a newer frame in flight. Do not encode this frame. |
234 LOG(LS_VERBOSE) | 236 LOG(LS_VERBOSE) |
235 << "Incoming frame dropped due to that the encoder is blocked."; | 237 << "Incoming frame dropped due to that the encoder is blocked."; |
236 ++vie_encoder_->dropped_frame_count_; | 238 ++vie_encoder_->dropped_frame_count_; |
237 } | 239 } |
238 if (log_stats_) { | 240 if (log_stats_) { |
239 LOG(LS_INFO) << "Number of frames: captured " | 241 LOG(LS_INFO) << "Number of frames: captured " |
240 << vie_encoder_->captured_frame_count_ | 242 << vie_encoder_->captured_frame_count_ |
241 << ", dropped (due to encoder blocked) " | 243 << ", dropped (due to encoder blocked) " |
242 << vie_encoder_->dropped_frame_count_ << ", interval_ms " | 244 << vie_encoder_->dropped_frame_count_ << ", interval_ms " |
243 << kFrameLogIntervalMs; | 245 << kFrameLogIntervalMs; |
244 vie_encoder_->captured_frame_count_ = 0; | 246 vie_encoder_->captured_frame_count_ = 0; |
245 vie_encoder_->dropped_frame_count_ = 0; | 247 vie_encoder_->dropped_frame_count_ = 0; |
246 } | 248 } |
247 return true; | 249 return true; |
248 } | 250 } |
249 VideoFrame frame_; | 251 VideoFrame frame_; |
250 ViEEncoder* const vie_encoder_; | 252 ViEEncoder* const vie_encoder_; |
251 const int64_t time_when_posted_ms_; | 253 const int64_t time_when_posted_ms_; |
252 const bool log_stats_; | 254 const bool log_stats_; |
253 }; | 255 }; |
254 | 256 |
255 // VideoSourceProxy is responsible ensuring thread safety between calls to | 257 // VideoSourceProxy is responsible ensuring thread safety between calls to |
256 // ViEEncoder::SetSource that will happen on libjingles worker thread when a | 258 // ViEEncoder::SetSource that will happen on libjingle's worker thread when a |
257 // video capturer is connected to the encoder and the encoder task queue | 259 // video capturer is connected to the encoder and the encoder task queue |
258 // (encoder_queue_) where the encoder reports its VideoSinkWants. | 260 // (encoder_queue_) where the encoder reports its VideoSinkWants. |
259 class ViEEncoder::VideoSourceProxy { | 261 class ViEEncoder::VideoSourceProxy { |
260 public: | 262 public: |
261 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | 263 explicit VideoSourceProxy(ViEEncoder* vie_encoder) |
262 : vie_encoder_(vie_encoder), source_(nullptr) {} | 264 : vie_encoder_(vie_encoder), |
265 degradation_preference_( | |
266 VideoSendStream::DegradationPreference::kMaintainResolution), | |
267 source_(nullptr) {} | |
263 | 268 |
264 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | 269 void SetSource( |
270 rtc::VideoSourceInterface<VideoFrame>* source, | |
271 const VideoSendStream::DegradationPreference& degradation_preference) { | |
272 // Called on libjingle's worker thread. | |
265 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 273 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
266 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 274 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
275 rtc::VideoSinkWants wants; | |
267 { | 276 { |
268 rtc::CritScope lock(&crit_); | 277 rtc::CritScope lock(&crit_); |
269 old_source = source_; | 278 old_source = source_; |
270 source_ = source; | 279 source_ = source; |
280 degradation_preference_ = degradation_preference; | |
281 wants = current_wants(); | |
271 } | 282 } |
272 | 283 |
273 if (old_source != source && old_source != nullptr) { | 284 if (old_source != source && old_source != nullptr) { |
274 old_source->RemoveSink(vie_encoder_); | 285 old_source->RemoveSink(vie_encoder_); |
275 } | 286 } |
276 | 287 |
277 if (!source) { | 288 if (!source) { |
278 return; | 289 return; |
279 } | 290 } |
280 | 291 |
281 // TODO(perkj): Let VideoSourceProxy implement LoadObserver and truly send | |
282 // CPU load as sink wants. | |
283 rtc::VideoSinkWants wants; | |
284 source->AddOrUpdateSink(vie_encoder_, wants); | 292 source->AddOrUpdateSink(vie_encoder_, wants); |
285 } | 293 } |
286 | 294 |
295 void SetWantsRotationApplied(bool rotation_applied) { | |
296 rtc::CritScope lock(&crit_); | |
297 sink_wants_.rotation_applied = rotation_applied; | |
298 disabled_scaling_sink_wants_.rotation_applied = rotation_applied; | |
299 if (source_) { | |
300 source_->AddOrUpdateSink(vie_encoder_, current_wants()); | |
301 } | |
302 } | |
303 | |
304 void RequestResolutionLowerThan(int pixel_count) { | |
305 // Called on the encoder task queue. | |
306 rtc::CritScope lock(&crit_); | |
307 if (!IsResutionScalingEnabledLocked()) { | |
308 // This can happen since |degradation_preference_| is set on | |
309 // libjingle's worker thread but the adaptation is done on the encoder | |
310 // task queue. | |
311 return; | |
312 } | |
313 // The input video frame size will have a resolution with less than or | |
314 // equal to |max_pixel_count| depending on how the source can scale the | |
315 // input frame size. | |
316 sink_wants_.max_pixel_count = rtc::Optional<int>((pixel_count * 3) / 5); | |
317 sink_wants_.max_pixel_count_step_up = rtc::Optional<int>(); | |
318 if (source_) | |
319 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | |
320 } | |
321 | |
322 void RequestHigherResolutionThan(int pixel_count) { | |
323 rtc::CritScope lock(&crit_); | |
324 if (!IsResutionScalingEnabledLocked()) { | |
325 // This can happen since |degradation_preference_| is set on | |
326 // libjingle's worker thread but the adaptation is done on the encoder | |
327 // task | |
328 // queue. | |
329 return; | |
330 } | |
331 // The input video frame size will have a resolution with "one step up" | |
332 // pixels than |max_pixel_count_step_up| where "one step up" depends on | |
333 // how the source can scale the input frame size. | |
334 sink_wants_.max_pixel_count = rtc::Optional<int>(); | |
335 sink_wants_.max_pixel_count_step_up = rtc::Optional<int>(pixel_count); | |
336 if (source_) | |
337 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | |
338 } | |
339 | |
287 private: | 340 private: |
341 bool IsResutionScalingEnabledLocked() const EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | |
nisse-webrtc
2016/10/28 10:53:44
Spelling.
perkj_webrtc
2016/10/28 14:05:34
Done.
| |
342 return degradation_preference_ != | |
343 VideoSendStream::DegradationPreference::kMaintainResolution; | |
344 } | |
345 | |
346 const rtc::VideoSinkWants& current_wants() const | |
347 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | |
348 return IsResutionScalingEnabledLocked() ? sink_wants_ | |
349 : disabled_scaling_sink_wants_; | |
350 } | |
351 | |
288 rtc::CriticalSection crit_; | 352 rtc::CriticalSection crit_; |
289 rtc::SequencedTaskChecker main_checker_; | 353 rtc::SequencedTaskChecker main_checker_; |
290 ViEEncoder* vie_encoder_; | 354 ViEEncoder* const vie_encoder_; |
355 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | |
356 rtc::VideoSinkWants disabled_scaling_sink_wants_ GUARDED_BY(&crit_); | |
357 VideoSendStream::DegradationPreference degradation_preference_ | |
358 GUARDED_BY(&crit_); | |
291 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 359 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
292 | 360 |
293 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 361 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
294 }; | 362 }; |
295 | 363 |
296 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 364 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
297 SendStatisticsProxy* stats_proxy, | 365 SendStatisticsProxy* stats_proxy, |
298 const VideoSendStream::Config::EncoderSettings& settings, | 366 const VideoSendStream::Config::EncoderSettings& settings, |
299 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 367 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
300 LoadObserver* overuse_callback, | |
301 EncodedFrameObserver* encoder_timing) | 368 EncodedFrameObserver* encoder_timing) |
302 : shutdown_event_(true /* manual_reset */, false), | 369 : shutdown_event_(true /* manual_reset */, false), |
303 number_of_cores_(number_of_cores), | 370 number_of_cores_(number_of_cores), |
304 source_proxy_(new VideoSourceProxy(this)), | 371 source_proxy_(new VideoSourceProxy(this)), |
305 sink_(nullptr), | 372 sink_(nullptr), |
306 settings_(settings), | 373 settings_(settings), |
307 codec_type_(PayloadNameToCodecType(settings.payload_name)), | 374 codec_type_(PayloadNameToCodecType(settings.payload_name)), |
308 video_sender_(Clock::GetRealTimeClock(), this, this), | 375 video_sender_(Clock::GetRealTimeClock(), this, this), |
309 overuse_detector_(Clock::GetRealTimeClock(), | 376 overuse_detector_(Clock::GetRealTimeClock(), |
310 GetCpuOveruseOptions(settings.full_overuse_time), | 377 GetCpuOveruseOptions(settings.full_overuse_time), |
311 this, | 378 this, |
312 encoder_timing, | 379 encoder_timing, |
313 stats_proxy), | 380 stats_proxy), |
314 load_observer_(overuse_callback), | |
315 stats_proxy_(stats_proxy), | 381 stats_proxy_(stats_proxy), |
316 pre_encode_callback_(pre_encode_callback), | 382 pre_encode_callback_(pre_encode_callback), |
317 module_process_thread_(nullptr), | 383 module_process_thread_(nullptr), |
318 pending_encoder_reconfiguration_(false), | 384 pending_encoder_reconfiguration_(false), |
319 encoder_start_bitrate_bps_(0), | 385 encoder_start_bitrate_bps_(0), |
320 max_data_payload_length_(0), | 386 max_data_payload_length_(0), |
321 last_observed_bitrate_bps_(0), | 387 last_observed_bitrate_bps_(0), |
322 encoder_paused_and_dropped_frame_(false), | 388 encoder_paused_and_dropped_frame_(false), |
323 has_received_sli_(false), | 389 has_received_sli_(false), |
324 picture_id_sli_(0), | 390 picture_id_sli_(0), |
325 has_received_rpsi_(false), | 391 has_received_rpsi_(false), |
326 picture_id_rpsi_(0), | 392 picture_id_rpsi_(0), |
327 clock_(Clock::GetRealTimeClock()), | 393 clock_(Clock::GetRealTimeClock()), |
394 degradation_preference_( | |
395 VideoSendStream::DegradationPreference::kBalanced), | |
396 cpu_restricted_counter_(0), | |
397 last_frame_width_(0), | |
398 last_frame_height_(0), | |
328 last_captured_timestamp_(0), | 399 last_captured_timestamp_(0), |
329 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 400 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
330 clock_->TimeInMilliseconds()), | 401 clock_->TimeInMilliseconds()), |
331 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 402 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
332 captured_frame_count_(0), | 403 captured_frame_count_(0), |
333 dropped_frame_count_(0), | 404 dropped_frame_count_(0), |
334 encoder_queue_("EncoderQueue") { | 405 encoder_queue_("EncoderQueue") { |
335 encoder_queue_.PostTask([this, encoder_timing] { | 406 encoder_queue_.PostTask([this] { |
336 RTC_DCHECK_RUN_ON(&encoder_queue_); | 407 RTC_DCHECK_RUN_ON(&encoder_queue_); |
408 overuse_detector_.StartCheckForOveruse(); | |
337 video_sender_.RegisterExternalEncoder( | 409 video_sender_.RegisterExternalEncoder( |
338 settings_.encoder, settings_.payload_type, settings_.internal_source); | 410 settings_.encoder, settings_.payload_type, settings_.internal_source); |
339 overuse_detector_.StartCheckForOveruse(); | |
340 }); | 411 }); |
341 } | 412 } |
342 | 413 |
343 ViEEncoder::~ViEEncoder() { | 414 ViEEncoder::~ViEEncoder() { |
344 RTC_DCHECK_RUN_ON(&thread_checker_); | 415 RTC_DCHECK_RUN_ON(&thread_checker_); |
345 RTC_DCHECK(shutdown_event_.Wait(0)) | 416 RTC_DCHECK(shutdown_event_.Wait(0)) |
346 << "Must call ::Stop() before destruction."; | 417 << "Must call ::Stop() before destruction."; |
347 } | 418 } |
348 | 419 |
349 void ViEEncoder::Stop() { | 420 void ViEEncoder::Stop() { |
350 RTC_DCHECK_RUN_ON(&thread_checker_); | 421 RTC_DCHECK_RUN_ON(&thread_checker_); |
351 source_proxy_->SetSource(nullptr); | 422 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); |
352 encoder_queue_.PostTask([this] { | 423 encoder_queue_.PostTask([this] { |
353 RTC_DCHECK_RUN_ON(&encoder_queue_); | 424 RTC_DCHECK_RUN_ON(&encoder_queue_); |
425 overuse_detector_.StopCheckForOveruse(); | |
354 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 426 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
355 false); | 427 false); |
356 overuse_detector_.StopCheckForOveruse(); | |
357 shutdown_event_.Set(); | 428 shutdown_event_.Set(); |
358 }); | 429 }); |
359 | 430 |
360 shutdown_event_.Wait(rtc::Event::kForever); | 431 shutdown_event_.Wait(rtc::Event::kForever); |
361 } | 432 } |
362 | 433 |
363 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 434 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
364 RTC_DCHECK_RUN_ON(&thread_checker_); | 435 RTC_DCHECK_RUN_ON(&thread_checker_); |
365 RTC_DCHECK(!module_process_thread_); | 436 RTC_DCHECK(!module_process_thread_); |
366 module_process_thread_ = module_process_thread; | 437 module_process_thread_ = module_process_thread; |
367 module_process_thread_->RegisterModule(&video_sender_); | 438 module_process_thread_->RegisterModule(&video_sender_); |
368 module_process_thread_checker_.DetachFromThread(); | 439 module_process_thread_checker_.DetachFromThread(); |
369 } | 440 } |
370 | 441 |
371 void ViEEncoder::DeRegisterProcessThread() { | 442 void ViEEncoder::DeRegisterProcessThread() { |
372 RTC_DCHECK_RUN_ON(&thread_checker_); | 443 RTC_DCHECK_RUN_ON(&thread_checker_); |
373 module_process_thread_->DeRegisterModule(&video_sender_); | 444 module_process_thread_->DeRegisterModule(&video_sender_); |
374 } | 445 } |
375 | 446 |
376 void ViEEncoder::SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | 447 void ViEEncoder::SetSource( |
448 rtc::VideoSourceInterface<VideoFrame>* source, | |
449 const VideoSendStream::DegradationPreference& degradation_preference) { | |
377 RTC_DCHECK_RUN_ON(&thread_checker_); | 450 RTC_DCHECK_RUN_ON(&thread_checker_); |
378 source_proxy_->SetSource(source); | 451 source_proxy_->SetSource(source, degradation_preference); |
452 encoder_queue_.PostTask([this, degradation_preference] { | |
453 RTC_DCHECK_RUN_ON(&encoder_queue_); | |
454 degradation_preference_ = degradation_preference; | |
455 // Set the stats for if we are currently CPU restricted. We are CPU | |
456 // restricted depending on degradation preference and | |
457 // if the overusedetector has currently detected overuse which is counted in | |
458 // |cpu_restricted_counter_| | |
459 // We do this on the encoder task queue to avoid a race with the stats set | |
460 // in | |
461 // ViEEncoder::NormalUsage and ViEEncoder::Overusedetected. | |
åsapersson
2016/10/31 08:43:02
OveruseDetected and see spacing
perkj_webrtc
2016/10/31 19:45:18
Done.
| |
462 stats_proxy_->SetCpuRestrictedResolution( | |
463 degradation_preference_ != | |
464 VideoSendStream::DegradationPreference::kMaintainResolution && | |
465 cpu_restricted_counter_ != 0); | |
466 }); | |
379 } | 467 } |
380 | 468 |
381 void ViEEncoder::SetSink(EncoderSink* sink) { | 469 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
470 source_proxy_->SetWantsRotationApplied(rotation_applied); | |
382 encoder_queue_.PostTask([this, sink] { | 471 encoder_queue_.PostTask([this, sink] { |
383 RTC_DCHECK_RUN_ON(&encoder_queue_); | 472 RTC_DCHECK_RUN_ON(&encoder_queue_); |
384 sink_ = sink; | 473 sink_ = sink; |
385 }); | 474 }); |
386 } | 475 } |
387 | 476 |
388 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { | 477 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { |
389 encoder_queue_.PostTask([this, start_bitrate_bps] { | 478 encoder_queue_.PostTask([this, start_bitrate_bps] { |
390 RTC_DCHECK_RUN_ON(&encoder_queue_); | 479 RTC_DCHECK_RUN_ON(&encoder_queue_); |
391 encoder_start_bitrate_bps_ = start_bitrate_bps; | 480 encoder_start_bitrate_bps_ = start_bitrate_bps; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
456 if (stats_proxy_) { | 545 if (stats_proxy_) { |
457 stats_proxy_->OnEncoderReconfigured(encoder_config_, | 546 stats_proxy_->OnEncoderReconfigured(encoder_config_, |
458 rate_allocator_->GetPreferedBitrate()); | 547 rate_allocator_->GetPreferedBitrate()); |
459 } | 548 } |
460 sink_->OnEncoderConfigurationChanged( | 549 sink_->OnEncoderConfigurationChanged( |
461 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 550 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
462 } | 551 } |
463 | 552 |
464 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 553 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
465 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 554 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
466 if (stats_proxy_) { | |
467 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | |
468 } | |
469 VideoFrame incoming_frame = video_frame; | 555 VideoFrame incoming_frame = video_frame; |
470 | 556 |
471 // Local time in webrtc time base. | 557 // Local time in webrtc time base. |
472 int64_t current_time = clock_->TimeInMilliseconds(); | 558 int64_t current_time = clock_->TimeInMilliseconds(); |
473 incoming_frame.set_render_time_ms(current_time); | 559 incoming_frame.set_render_time_ms(current_time); |
474 | 560 |
475 // Capture time may come from clock with an offset and drift from clock_. | 561 // Capture time may come from clock with an offset and drift from clock_. |
476 int64_t capture_ntp_time_ms; | 562 int64_t capture_ntp_time_ms; |
477 if (video_frame.ntp_time_ms() != 0) { | 563 if (video_frame.ntp_time_ms() != 0) { |
478 capture_ntp_time_ms = video_frame.ntp_time_ms(); | 564 capture_ntp_time_ms = video_frame.ntp_time_ms(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
559 if (pending_encoder_reconfiguration_) { | 645 if (pending_encoder_reconfiguration_) { |
560 ReconfigureEncoder(); | 646 ReconfigureEncoder(); |
561 } | 647 } |
562 | 648 |
563 if (EncoderPaused()) { | 649 if (EncoderPaused()) { |
564 TraceFrameDropStart(); | 650 TraceFrameDropStart(); |
565 return; | 651 return; |
566 } | 652 } |
567 TraceFrameDropEnd(); | 653 TraceFrameDropEnd(); |
568 | 654 |
655 last_frame_height_ = video_frame.height(); | |
656 last_frame_width_ = video_frame.width(); | |
657 | |
569 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), | 658 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), |
570 "Encode"); | 659 "Encode"); |
571 | 660 |
572 overuse_detector_.FrameCaptured(video_frame, time_when_posted_in_ms); | 661 overuse_detector_.FrameCaptured(video_frame, time_when_posted_in_ms); |
573 | 662 |
574 if (codec_type_ == webrtc::kVideoCodecVP8) { | 663 if (codec_type_ == webrtc::kVideoCodecVP8) { |
575 webrtc::CodecSpecificInfo codec_specific_info; | 664 webrtc::CodecSpecificInfo codec_specific_info; |
576 codec_specific_info.codecType = webrtc::kVideoCodecVP8; | 665 codec_specific_info.codecType = webrtc::kVideoCodecVP8; |
577 | 666 |
578 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI = | 667 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
610 // running in parallel on different threads. | 699 // running in parallel on different threads. |
611 if (stats_proxy_) { | 700 if (stats_proxy_) { |
612 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); | 701 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); |
613 } | 702 } |
614 | 703 |
615 EncodedImageCallback::Result result = | 704 EncodedImageCallback::Result result = |
616 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); | 705 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
617 | 706 |
618 int64_t time_sent = clock_->TimeInMilliseconds(); | 707 int64_t time_sent = clock_->TimeInMilliseconds(); |
619 uint32_t timestamp = encoded_image._timeStamp; | 708 uint32_t timestamp = encoded_image._timeStamp; |
709 | |
620 encoder_queue_.PostTask([this, timestamp, time_sent] { | 710 encoder_queue_.PostTask([this, timestamp, time_sent] { |
621 RTC_DCHECK_RUN_ON(&encoder_queue_); | 711 RTC_DCHECK_RUN_ON(&encoder_queue_); |
622 overuse_detector_.FrameSent(timestamp, time_sent); | 712 overuse_detector_.FrameSent(timestamp, time_sent); |
623 }); | 713 }); |
714 | |
624 return result; | 715 return result; |
625 } | 716 } |
626 | 717 |
627 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { | 718 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { |
628 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); | 719 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
629 if (stats_proxy_) | 720 if (stats_proxy_) |
630 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); | 721 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); |
631 } | 722 } |
632 | 723 |
633 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) { | 724 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
691 | 782 |
692 if (stats_proxy_ && video_suspension_changed) { | 783 if (stats_proxy_ && video_suspension_changed) { |
693 LOG(LS_INFO) << "Video suspend state changed to: " | 784 LOG(LS_INFO) << "Video suspend state changed to: " |
694 << (video_is_suspended ? "suspended" : "not suspended"); | 785 << (video_is_suspended ? "suspended" : "not suspended"); |
695 stats_proxy_->OnSuspendChange(video_is_suspended); | 786 stats_proxy_->OnSuspendChange(video_is_suspended); |
696 } | 787 } |
697 } | 788 } |
698 | 789 |
699 void ViEEncoder::OveruseDetected() { | 790 void ViEEncoder::OveruseDetected() { |
700 RTC_DCHECK_RUN_ON(&encoder_queue_); | 791 RTC_DCHECK_RUN_ON(&encoder_queue_); |
701 // TODO(perkj): When ViEEncoder inherit rtc::VideoSink instead of | 792 if (degradation_preference_ == |
702 // VideoCaptureInput |load_observer_| should be removed and overuse be | 793 VideoSendStream::DegradationPreference::kMaintainResolution || |
703 // expressed as rtc::VideoSinkWants instead. | 794 cpu_restricted_counter_ >= kMaxCpuDowngrades) { |
704 if (load_observer_) | 795 return; |
705 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); | 796 } |
797 LOG(LS_INFO) << "CPU overuse detected. Requesting lower resolution."; | |
798 // Request lower resolution if the current resolution is lower than last time | |
799 // we asked for the resolution to be lowered. | |
800 // Update stats accordingly. | |
801 int current_pixel_count = last_frame_height_ * last_frame_width_; | |
802 if (!max_pixel_count_ || current_pixel_count < *max_pixel_count_) { | |
803 max_pixel_count_ = rtc::Optional<int>(current_pixel_count); | |
804 max_pixel_count_step_up_ = rtc::Optional<int>(); | |
805 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | |
806 ++cpu_restricted_counter_; | |
807 source_proxy_->RequestResolutionLowerThan(current_pixel_count); | |
808 } | |
706 } | 809 } |
707 | 810 |
708 void ViEEncoder::NormalUsage() { | 811 void ViEEncoder::NormalUsage() { |
709 RTC_DCHECK_RUN_ON(&encoder_queue_); | 812 RTC_DCHECK_RUN_ON(&encoder_queue_); |
710 if (load_observer_) | 813 if (degradation_preference_ == |
711 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); | 814 VideoSendStream::DegradationPreference::kMaintainResolution || |
815 cpu_restricted_counter_ == 0) { | |
816 return; | |
817 } | |
818 | |
819 LOG(LS_INFO) << "CPU underuse detected. Requesting higher resolution."; | |
820 int current_pixel_count = last_frame_height_ * last_frame_width_; | |
821 // Request higher resolution if we are CPU restricted and the the current | |
822 // resolution is higher than last time we requested higher resolution. | |
823 // Update stats accordingly. | |
824 if (!max_pixel_count_step_up_ || | |
825 current_pixel_count > *max_pixel_count_step_up_) { | |
826 max_pixel_count_ = rtc::Optional<int>(); | |
827 max_pixel_count_step_up_ = rtc::Optional<int>(current_pixel_count); | |
828 --cpu_restricted_counter_; | |
829 stats_proxy_->OnCpuRestrictedResolutionChanged(cpu_restricted_counter_ > 0); | |
830 source_proxy_->RequestHigherResolutionThan(current_pixel_count); | |
831 } | |
712 } | 832 } |
713 | 833 |
714 } // namespace webrtc | 834 } // namespace webrtc |
OLD | NEW |