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_ = frame; | 221 frame_ = 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 (!IsResolutionScalingEnabledLocked()) { |
| 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 (!IsResolutionScalingEnabledLocked()) { |
| 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 IsResolutionScalingEnabledLocked() const |
| 342 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
| 343 return degradation_preference_ != |
| 344 VideoSendStream::DegradationPreference::kMaintainResolution; |
| 345 } |
| 346 |
| 347 const rtc::VideoSinkWants& current_wants() const |
| 348 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
| 349 return IsResolutionScalingEnabledLocked() ? sink_wants_ |
| 350 : disabled_scaling_sink_wants_; |
| 351 } |
| 352 |
288 rtc::CriticalSection crit_; | 353 rtc::CriticalSection crit_; |
289 rtc::SequencedTaskChecker main_checker_; | 354 rtc::SequencedTaskChecker main_checker_; |
290 ViEEncoder* vie_encoder_; | 355 ViEEncoder* const vie_encoder_; |
| 356 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); |
| 357 rtc::VideoSinkWants disabled_scaling_sink_wants_ GUARDED_BY(&crit_); |
| 358 VideoSendStream::DegradationPreference degradation_preference_ |
| 359 GUARDED_BY(&crit_); |
291 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 360 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
292 | 361 |
293 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 362 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
294 }; | 363 }; |
295 | 364 |
296 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 365 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
297 SendStatisticsProxy* stats_proxy, | 366 SendStatisticsProxy* stats_proxy, |
298 const VideoSendStream::Config::EncoderSettings& settings, | 367 const VideoSendStream::Config::EncoderSettings& settings, |
299 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 368 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
300 LoadObserver* overuse_callback, | |
301 EncodedFrameObserver* encoder_timing) | 369 EncodedFrameObserver* encoder_timing) |
302 : shutdown_event_(true /* manual_reset */, false), | 370 : shutdown_event_(true /* manual_reset */, false), |
303 number_of_cores_(number_of_cores), | 371 number_of_cores_(number_of_cores), |
304 source_proxy_(new VideoSourceProxy(this)), | 372 source_proxy_(new VideoSourceProxy(this)), |
305 sink_(nullptr), | 373 sink_(nullptr), |
306 settings_(settings), | 374 settings_(settings), |
307 codec_type_(PayloadNameToCodecType(settings.payload_name)), | 375 codec_type_(PayloadNameToCodecType(settings.payload_name)), |
308 video_sender_(Clock::GetRealTimeClock(), this, this), | 376 video_sender_(Clock::GetRealTimeClock(), this, this), |
309 overuse_detector_(Clock::GetRealTimeClock(), | 377 overuse_detector_(Clock::GetRealTimeClock(), |
310 GetCpuOveruseOptions(settings.full_overuse_time), | 378 GetCpuOveruseOptions(settings.full_overuse_time), |
311 this, | 379 this, |
312 encoder_timing, | 380 encoder_timing, |
313 stats_proxy), | 381 stats_proxy), |
314 load_observer_(overuse_callback), | |
315 stats_proxy_(stats_proxy), | 382 stats_proxy_(stats_proxy), |
316 pre_encode_callback_(pre_encode_callback), | 383 pre_encode_callback_(pre_encode_callback), |
317 module_process_thread_(nullptr), | 384 module_process_thread_(nullptr), |
318 pending_encoder_reconfiguration_(false), | 385 pending_encoder_reconfiguration_(false), |
319 encoder_start_bitrate_bps_(0), | 386 encoder_start_bitrate_bps_(0), |
320 max_data_payload_length_(0), | 387 max_data_payload_length_(0), |
321 last_observed_bitrate_bps_(0), | 388 last_observed_bitrate_bps_(0), |
322 encoder_paused_and_dropped_frame_(false), | 389 encoder_paused_and_dropped_frame_(false), |
323 has_received_sli_(false), | 390 has_received_sli_(false), |
324 picture_id_sli_(0), | 391 picture_id_sli_(0), |
325 has_received_rpsi_(false), | 392 has_received_rpsi_(false), |
326 picture_id_rpsi_(0), | 393 picture_id_rpsi_(0), |
327 clock_(Clock::GetRealTimeClock()), | 394 clock_(Clock::GetRealTimeClock()), |
| 395 degradation_preference_( |
| 396 VideoSendStream::DegradationPreference::kBalanced), |
| 397 cpu_restricted_counter_(0), |
| 398 last_frame_width_(0), |
| 399 last_frame_height_(0), |
328 last_captured_timestamp_(0), | 400 last_captured_timestamp_(0), |
329 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 401 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
330 clock_->TimeInMilliseconds()), | 402 clock_->TimeInMilliseconds()), |
331 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 403 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
332 captured_frame_count_(0), | 404 captured_frame_count_(0), |
333 dropped_frame_count_(0), | 405 dropped_frame_count_(0), |
334 encoder_queue_("EncoderQueue") { | 406 encoder_queue_("EncoderQueue") { |
335 encoder_queue_.PostTask([this, encoder_timing] { | 407 encoder_queue_.PostTask([this] { |
336 RTC_DCHECK_RUN_ON(&encoder_queue_); | 408 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 409 overuse_detector_.StartCheckForOveruse(); |
337 video_sender_.RegisterExternalEncoder( | 410 video_sender_.RegisterExternalEncoder( |
338 settings_.encoder, settings_.payload_type, settings_.internal_source); | 411 settings_.encoder, settings_.payload_type, settings_.internal_source); |
339 overuse_detector_.StartCheckForOveruse(); | |
340 }); | 412 }); |
341 } | 413 } |
342 | 414 |
343 ViEEncoder::~ViEEncoder() { | 415 ViEEncoder::~ViEEncoder() { |
344 RTC_DCHECK_RUN_ON(&thread_checker_); | 416 RTC_DCHECK_RUN_ON(&thread_checker_); |
345 RTC_DCHECK(shutdown_event_.Wait(0)) | 417 RTC_DCHECK(shutdown_event_.Wait(0)) |
346 << "Must call ::Stop() before destruction."; | 418 << "Must call ::Stop() before destruction."; |
347 } | 419 } |
348 | 420 |
349 void ViEEncoder::Stop() { | 421 void ViEEncoder::Stop() { |
350 RTC_DCHECK_RUN_ON(&thread_checker_); | 422 RTC_DCHECK_RUN_ON(&thread_checker_); |
351 source_proxy_->SetSource(nullptr); | 423 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); |
352 encoder_queue_.PostTask([this] { | 424 encoder_queue_.PostTask([this] { |
353 RTC_DCHECK_RUN_ON(&encoder_queue_); | 425 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 426 overuse_detector_.StopCheckForOveruse(); |
354 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 427 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
355 false); | 428 false); |
356 overuse_detector_.StopCheckForOveruse(); | |
357 shutdown_event_.Set(); | 429 shutdown_event_.Set(); |
358 }); | 430 }); |
359 | 431 |
360 shutdown_event_.Wait(rtc::Event::kForever); | 432 shutdown_event_.Wait(rtc::Event::kForever); |
361 } | 433 } |
362 | 434 |
363 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 435 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
364 RTC_DCHECK_RUN_ON(&thread_checker_); | 436 RTC_DCHECK_RUN_ON(&thread_checker_); |
365 RTC_DCHECK(!module_process_thread_); | 437 RTC_DCHECK(!module_process_thread_); |
366 module_process_thread_ = module_process_thread; | 438 module_process_thread_ = module_process_thread; |
367 module_process_thread_->RegisterModule(&video_sender_); | 439 module_process_thread_->RegisterModule(&video_sender_); |
368 module_process_thread_checker_.DetachFromThread(); | 440 module_process_thread_checker_.DetachFromThread(); |
369 } | 441 } |
370 | 442 |
371 void ViEEncoder::DeRegisterProcessThread() { | 443 void ViEEncoder::DeRegisterProcessThread() { |
372 RTC_DCHECK_RUN_ON(&thread_checker_); | 444 RTC_DCHECK_RUN_ON(&thread_checker_); |
373 module_process_thread_->DeRegisterModule(&video_sender_); | 445 module_process_thread_->DeRegisterModule(&video_sender_); |
374 } | 446 } |
375 | 447 |
376 void ViEEncoder::SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | 448 void ViEEncoder::SetSource( |
| 449 rtc::VideoSourceInterface<VideoFrame>* source, |
| 450 const VideoSendStream::DegradationPreference& degradation_preference) { |
377 RTC_DCHECK_RUN_ON(&thread_checker_); | 451 RTC_DCHECK_RUN_ON(&thread_checker_); |
378 source_proxy_->SetSource(source); | 452 source_proxy_->SetSource(source, degradation_preference); |
| 453 encoder_queue_.PostTask([this, degradation_preference] { |
| 454 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 455 degradation_preference_ = degradation_preference; |
| 456 // Set the stats for if we are currently CPU restricted. We are CPU |
| 457 // restricted depending on degradation preference and |
| 458 // if the overusedetector has currently detected overuse which is counted in |
| 459 // |cpu_restricted_counter_| |
| 460 // We do this on the encoder task queue to avoid a race with the stats set |
| 461 // in ViEEncoder::NormalUsage and ViEEncoder::OveruseDetected. |
| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 if (stats_proxy_) { | 544 if (stats_proxy_) { |
456 stats_proxy_->OnEncoderReconfigured(encoder_config_, | 545 stats_proxy_->OnEncoderReconfigured(encoder_config_, |
457 rate_allocator_->GetPreferedBitrate()); | 546 rate_allocator_->GetPreferedBitrate()); |
458 } | 547 } |
459 sink_->OnEncoderConfigurationChanged( | 548 sink_->OnEncoderConfigurationChanged( |
460 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 549 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
461 } | 550 } |
462 | 551 |
463 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 552 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
464 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 553 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
465 if (stats_proxy_) { | |
466 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | |
467 } | |
468 VideoFrame incoming_frame = video_frame; | 554 VideoFrame incoming_frame = video_frame; |
469 | 555 |
470 // Local time in webrtc time base. | 556 // Local time in webrtc time base. |
471 int64_t current_time = clock_->TimeInMilliseconds(); | 557 int64_t current_time = clock_->TimeInMilliseconds(); |
472 incoming_frame.set_render_time_ms(current_time); | 558 incoming_frame.set_render_time_ms(current_time); |
473 | 559 |
474 // Capture time may come from clock with an offset and drift from clock_. | 560 // Capture time may come from clock with an offset and drift from clock_. |
475 int64_t capture_ntp_time_ms; | 561 int64_t capture_ntp_time_ms; |
476 if (video_frame.ntp_time_ms() != 0) { | 562 if (video_frame.ntp_time_ms() != 0) { |
477 capture_ntp_time_ms = video_frame.ntp_time_ms(); | 563 capture_ntp_time_ms = video_frame.ntp_time_ms(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 if (pending_encoder_reconfiguration_) { | 644 if (pending_encoder_reconfiguration_) { |
559 ReconfigureEncoder(); | 645 ReconfigureEncoder(); |
560 } | 646 } |
561 | 647 |
562 if (EncoderPaused()) { | 648 if (EncoderPaused()) { |
563 TraceFrameDropStart(); | 649 TraceFrameDropStart(); |
564 return; | 650 return; |
565 } | 651 } |
566 TraceFrameDropEnd(); | 652 TraceFrameDropEnd(); |
567 | 653 |
| 654 last_frame_height_ = video_frame.height(); |
| 655 last_frame_width_ = video_frame.width(); |
| 656 |
568 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), | 657 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), |
569 "Encode"); | 658 "Encode"); |
570 | 659 |
571 overuse_detector_.FrameCaptured(video_frame, time_when_posted_in_ms); | 660 overuse_detector_.FrameCaptured(video_frame, time_when_posted_in_ms); |
572 | 661 |
573 if (codec_type_ == webrtc::kVideoCodecVP8) { | 662 if (codec_type_ == webrtc::kVideoCodecVP8) { |
574 webrtc::CodecSpecificInfo codec_specific_info; | 663 webrtc::CodecSpecificInfo codec_specific_info; |
575 codec_specific_info.codecType = webrtc::kVideoCodecVP8; | 664 codec_specific_info.codecType = webrtc::kVideoCodecVP8; |
576 | 665 |
577 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI = | 666 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 // running in parallel on different threads. | 698 // running in parallel on different threads. |
610 if (stats_proxy_) { | 699 if (stats_proxy_) { |
611 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); | 700 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); |
612 } | 701 } |
613 | 702 |
614 EncodedImageCallback::Result result = | 703 EncodedImageCallback::Result result = |
615 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); | 704 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
616 | 705 |
617 int64_t time_sent = clock_->TimeInMilliseconds(); | 706 int64_t time_sent = clock_->TimeInMilliseconds(); |
618 uint32_t timestamp = encoded_image._timeStamp; | 707 uint32_t timestamp = encoded_image._timeStamp; |
| 708 |
619 encoder_queue_.PostTask([this, timestamp, time_sent] { | 709 encoder_queue_.PostTask([this, timestamp, time_sent] { |
620 RTC_DCHECK_RUN_ON(&encoder_queue_); | 710 RTC_DCHECK_RUN_ON(&encoder_queue_); |
621 overuse_detector_.FrameSent(timestamp, time_sent); | 711 overuse_detector_.FrameSent(timestamp, time_sent); |
622 }); | 712 }); |
| 713 |
623 return result; | 714 return result; |
624 } | 715 } |
625 | 716 |
626 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { | 717 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { |
627 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); | 718 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
628 if (stats_proxy_) | 719 if (stats_proxy_) |
629 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); | 720 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); |
630 } | 721 } |
631 | 722 |
632 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) { | 723 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 | 781 |
691 if (stats_proxy_ && video_suspension_changed) { | 782 if (stats_proxy_ && video_suspension_changed) { |
692 LOG(LS_INFO) << "Video suspend state changed to: " | 783 LOG(LS_INFO) << "Video suspend state changed to: " |
693 << (video_is_suspended ? "suspended" : "not suspended"); | 784 << (video_is_suspended ? "suspended" : "not suspended"); |
694 stats_proxy_->OnSuspendChange(video_is_suspended); | 785 stats_proxy_->OnSuspendChange(video_is_suspended); |
695 } | 786 } |
696 } | 787 } |
697 | 788 |
698 void ViEEncoder::OveruseDetected() { | 789 void ViEEncoder::OveruseDetected() { |
699 RTC_DCHECK_RUN_ON(&encoder_queue_); | 790 RTC_DCHECK_RUN_ON(&encoder_queue_); |
700 // TODO(perkj): When ViEEncoder inherit rtc::VideoSink instead of | 791 if (degradation_preference_ == |
701 // VideoCaptureInput |load_observer_| should be removed and overuse be | 792 VideoSendStream::DegradationPreference::kMaintainResolution || |
702 // expressed as rtc::VideoSinkWants instead. | 793 cpu_restricted_counter_ >= kMaxCpuDowngrades) { |
703 if (load_observer_) | 794 return; |
704 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); | 795 } |
| 796 LOG(LS_INFO) << "CPU overuse detected. Requesting lower resolution."; |
| 797 // Request lower resolution if the current resolution is lower than last time |
| 798 // we asked for the resolution to be lowered. |
| 799 // Update stats accordingly. |
| 800 int current_pixel_count = last_frame_height_ * last_frame_width_; |
| 801 if (!max_pixel_count_ || current_pixel_count < *max_pixel_count_) { |
| 802 max_pixel_count_ = rtc::Optional<int>(current_pixel_count); |
| 803 max_pixel_count_step_up_ = rtc::Optional<int>(); |
| 804 stats_proxy_->OnCpuRestrictedResolutionChanged(true); |
| 805 ++cpu_restricted_counter_; |
| 806 source_proxy_->RequestResolutionLowerThan(current_pixel_count); |
| 807 } |
705 } | 808 } |
706 | 809 |
707 void ViEEncoder::NormalUsage() { | 810 void ViEEncoder::NormalUsage() { |
708 RTC_DCHECK_RUN_ON(&encoder_queue_); | 811 RTC_DCHECK_RUN_ON(&encoder_queue_); |
709 if (load_observer_) | 812 if (degradation_preference_ == |
710 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); | 813 VideoSendStream::DegradationPreference::kMaintainResolution || |
| 814 cpu_restricted_counter_ == 0) { |
| 815 return; |
| 816 } |
| 817 |
| 818 LOG(LS_INFO) << "CPU underuse detected. Requesting higher resolution."; |
| 819 int current_pixel_count = last_frame_height_ * last_frame_width_; |
| 820 // Request higher resolution if we are CPU restricted and the the current |
| 821 // resolution is higher than last time we requested higher resolution. |
| 822 // Update stats accordingly. |
| 823 if (!max_pixel_count_step_up_ || |
| 824 current_pixel_count > *max_pixel_count_step_up_) { |
| 825 max_pixel_count_ = rtc::Optional<int>(); |
| 826 max_pixel_count_step_up_ = rtc::Optional<int>(current_pixel_count); |
| 827 --cpu_restricted_counter_; |
| 828 stats_proxy_->OnCpuRestrictedResolutionChanged(cpu_restricted_counter_ > 0); |
| 829 source_proxy_->RequestHigherResolutionThan(current_pixel_count); |
| 830 } |
711 } | 831 } |
712 | 832 |
713 } // namespace webrtc | 833 } // namespace webrtc |
OLD | NEW |