Chromium Code Reviews| 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( | |
| 230 frame_.width(), frame_.height(), | |
| 231 vie_encoder_->cpu_restricted_counter_ != 0); | |
| 229 ++vie_encoder_->captured_frame_count_; | 232 ++vie_encoder_->captured_frame_count_; |
| 230 if (--vie_encoder_->posted_frames_waiting_for_encode_ == 0) { | 233 if (--vie_encoder_->posted_frames_waiting_for_encode_ == 0) { |
| 231 vie_encoder_->EncodeVideoFrame(frame_, time_when_posted_ms_); | 234 vie_encoder_->EncodeVideoFrame(frame_, time_when_posted_ms_); |
| 232 } else { | 235 } else { |
| 233 // There is a newer frame in flight. Do not encode this frame. | 236 // There is a newer frame in flight. Do not encode this frame. |
| 234 LOG(LS_VERBOSE) | 237 LOG(LS_VERBOSE) |
| 235 << "Incoming frame dropped due to that the encoder is blocked."; | 238 << "Incoming frame dropped due to that the encoder is blocked."; |
| 236 ++vie_encoder_->dropped_frame_count_; | 239 ++vie_encoder_->dropped_frame_count_; |
| 237 } | 240 } |
| 238 if (log_stats_) { | 241 if (log_stats_) { |
| 239 LOG(LS_INFO) << "Number of frames: captured " | 242 LOG(LS_INFO) << "Number of frames: captured " |
| 240 << vie_encoder_->captured_frame_count_ | 243 << vie_encoder_->captured_frame_count_ |
| 241 << ", dropped (due to encoder blocked) " | 244 << ", dropped (due to encoder blocked) " |
| 242 << vie_encoder_->dropped_frame_count_ << ", interval_ms " | 245 << vie_encoder_->dropped_frame_count_ << ", interval_ms " |
| 243 << kFrameLogIntervalMs; | 246 << kFrameLogIntervalMs; |
| 244 vie_encoder_->captured_frame_count_ = 0; | 247 vie_encoder_->captured_frame_count_ = 0; |
| 245 vie_encoder_->dropped_frame_count_ = 0; | 248 vie_encoder_->dropped_frame_count_ = 0; |
| 246 } | 249 } |
| 247 return true; | 250 return true; |
| 248 } | 251 } |
| 249 VideoFrame frame_; | 252 VideoFrame frame_; |
| 250 ViEEncoder* const vie_encoder_; | 253 ViEEncoder* const vie_encoder_; |
| 251 const int64_t time_when_posted_ms_; | 254 const int64_t time_when_posted_ms_; |
| 252 const bool log_stats_; | 255 const bool log_stats_; |
| 253 }; | 256 }; |
| 254 | 257 |
| 255 // VideoSourceProxy is responsible ensuring thread safety between calls to | 258 // VideoSourceProxy is responsible ensuring thread safety between calls to |
| 256 // ViEEncoder::SetSource that will happen on libjingles worker thread when a | 259 // 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 | 260 // video capturer is connected to the encoder and the encoder task queue |
| 258 // (encoder_queue_) where the encoder reports its VideoSinkWants. | 261 // (encoder_queue_) where the encoder reports its VideoSinkWants. |
| 259 class ViEEncoder::VideoSourceProxy { | 262 class ViEEncoder::VideoSourceProxy { |
| 260 public: | 263 public: |
| 261 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | 264 explicit VideoSourceProxy(ViEEncoder* vie_encoder) |
| 262 : vie_encoder_(vie_encoder), source_(nullptr) {} | 265 : vie_encoder_(vie_encoder), |
| 266 resolution_scaling_disabled_(false), | |
| 267 source_(nullptr) {} | |
| 263 | 268 |
| 264 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | 269 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 270 bool disable_resolution_scaling) { | |
|
åsapersson
2016/10/24 07:54:06
change to enable_resolution_scaling?
perkj_webrtc
2016/10/26 16:40:17
now use preferences
| |
| 271 // Called on libjingle's worker thread. | |
| 265 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 272 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
| 266 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 273 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
| 274 rtc::VideoSinkWants wants; | |
| 267 { | 275 { |
| 268 rtc::CritScope lock(&crit_); | 276 rtc::CritScope lock(&crit_); |
| 269 old_source = source_; | 277 old_source = source_; |
| 270 source_ = source; | 278 source_ = source; |
| 279 resolution_scaling_disabled_ = disable_resolution_scaling; | |
| 280 wants = disable_resolution_scaling ? disabled_scaling_sink_wants_ | |
| 281 : sink_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_, resolution_scaling_disabled_ | |
| 301 ? disabled_scaling_sink_wants_ | |
| 302 : sink_wants_); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 void RequestResolutionLowerThan(int pixel_count) { | |
| 307 // Called on the encoder task queue. | |
| 308 rtc::CritScope lock(&crit_); | |
| 309 if (resolution_scaling_disabled_) { | |
| 310 // This can happen since |resolution_scaling_disabled_| is set on | |
| 311 // libjingle's worker thread but the adaptation is done on the encoder | |
| 312 // task | |
|
åsapersson
2016/10/24 07:54:06
move comment up one line
perkj_webrtc
2016/10/26 16:40:17
Done.
| |
| 313 // queue. | |
| 314 return; | |
| 315 } | |
| 316 // The input video frame size will have a resolution with less than or | |
| 317 // equal to |max_pixel_count| depending on how the source can scale the | |
| 318 // input frame size. | |
| 319 sink_wants_.max_pixel_count = rtc::Optional<int>((pixel_count * 3) / 5); | |
| 320 sink_wants_.max_pixel_count_step_up = rtc::Optional<int>(); | |
| 321 if (source_) | |
| 322 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | |
| 323 } | |
| 324 | |
| 325 void RequestHigherResolutionThan(int pixel_count) { | |
| 326 rtc::CritScope lock(&crit_); | |
| 327 if (resolution_scaling_disabled_) { | |
| 328 // This can happen since |resolution_scaling_disabled_| is set on | |
| 329 // libjingles worker thread but the adaptation is done on the encoder task | |
| 330 // queue. | |
| 331 return; | |
| 332 } | |
| 333 // The input video frame size will have a resolution with "one step up" | |
| 334 // pixels than |max_pixel_count_step_up| where "one step up" depends on | |
| 335 // how the source can scale the input frame size. | |
| 336 sink_wants_.max_pixel_count = rtc::Optional<int>(); | |
| 337 sink_wants_.max_pixel_count_step_up = rtc::Optional<int>(pixel_count); | |
| 338 if (source_) | |
| 339 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | |
| 340 } | |
| 341 | |
| 287 private: | 342 private: |
| 288 rtc::CriticalSection crit_; | 343 rtc::CriticalSection crit_; |
| 289 rtc::SequencedTaskChecker main_checker_; | 344 rtc::SequencedTaskChecker main_checker_; |
| 290 ViEEncoder* vie_encoder_; | 345 ViEEncoder* const vie_encoder_; |
| 346 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | |
| 347 rtc::VideoSinkWants disabled_scaling_sink_wants_ GUARDED_BY(&crit_); | |
| 348 bool resolution_scaling_disabled_ GUARDED_BY(&crit_); | |
|
åsapersson
2016/10/24 07:54:06
change to resolution_scaling_enabled_?
perkj_webrtc
2016/10/26 16:40:17
degradation_preference_
| |
| 291 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 349 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
| 292 | 350 |
| 293 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 351 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
| 294 }; | 352 }; |
| 295 | 353 |
| 296 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 354 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
| 297 SendStatisticsProxy* stats_proxy, | 355 SendStatisticsProxy* stats_proxy, |
| 298 const VideoSendStream::Config::EncoderSettings& settings, | 356 const VideoSendStream::Config::EncoderSettings& settings, |
| 299 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 357 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
| 300 LoadObserver* overuse_callback, | |
| 301 EncodedFrameObserver* encoder_timing) | 358 EncodedFrameObserver* encoder_timing) |
| 302 : shutdown_event_(true /* manual_reset */, false), | 359 : shutdown_event_(true /* manual_reset */, false), |
| 303 number_of_cores_(number_of_cores), | 360 number_of_cores_(number_of_cores), |
| 304 source_proxy_(new VideoSourceProxy(this)), | 361 source_proxy_(new VideoSourceProxy(this)), |
| 305 sink_(nullptr), | 362 sink_(nullptr), |
| 306 settings_(settings), | 363 settings_(settings), |
| 307 codec_type_(PayloadNameToCodecType(settings.payload_name)), | 364 codec_type_(PayloadNameToCodecType(settings.payload_name)), |
| 308 video_sender_(Clock::GetRealTimeClock(), this, this), | 365 video_sender_(Clock::GetRealTimeClock(), this, this), |
| 309 overuse_detector_(Clock::GetRealTimeClock(), | |
| 310 GetCpuOveruseOptions(settings.full_overuse_time), | |
| 311 this, | |
| 312 encoder_timing, | |
| 313 stats_proxy), | |
| 314 load_observer_(overuse_callback), | |
| 315 stats_proxy_(stats_proxy), | 366 stats_proxy_(stats_proxy), |
| 316 pre_encode_callback_(pre_encode_callback), | 367 pre_encode_callback_(pre_encode_callback), |
| 368 encoder_timing_(encoder_timing), | |
| 317 module_process_thread_(nullptr), | 369 module_process_thread_(nullptr), |
| 318 pending_encoder_reconfiguration_(false), | 370 pending_encoder_reconfiguration_(false), |
| 319 encoder_start_bitrate_bps_(0), | 371 encoder_start_bitrate_bps_(0), |
| 320 max_data_payload_length_(0), | 372 max_data_payload_length_(0), |
| 321 last_observed_bitrate_bps_(0), | 373 last_observed_bitrate_bps_(0), |
| 322 encoder_paused_and_dropped_frame_(false), | 374 encoder_paused_and_dropped_frame_(false), |
| 323 has_received_sli_(false), | 375 has_received_sli_(false), |
| 324 picture_id_sli_(0), | 376 picture_id_sli_(0), |
| 325 has_received_rpsi_(false), | 377 has_received_rpsi_(false), |
| 326 picture_id_rpsi_(0), | 378 picture_id_rpsi_(0), |
| 327 clock_(Clock::GetRealTimeClock()), | 379 clock_(Clock::GetRealTimeClock()), |
| 380 cpu_restricted_counter_(0), | |
| 381 last_frame_width_(0), | |
| 382 last_frame_height_(0), | |
| 328 last_captured_timestamp_(0), | 383 last_captured_timestamp_(0), |
| 329 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 384 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
| 330 clock_->TimeInMilliseconds()), | 385 clock_->TimeInMilliseconds()), |
| 331 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 386 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
| 332 captured_frame_count_(0), | 387 captured_frame_count_(0), |
| 333 dropped_frame_count_(0), | 388 dropped_frame_count_(0), |
| 334 encoder_queue_("EncoderQueue") { | 389 encoder_queue_("EncoderQueue") { |
| 335 encoder_queue_.PostTask([this, encoder_timing] { | 390 encoder_queue_.PostTask([this, encoder_timing] { |
| 336 RTC_DCHECK_RUN_ON(&encoder_queue_); | 391 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 337 video_sender_.RegisterExternalEncoder( | 392 video_sender_.RegisterExternalEncoder( |
| 338 settings_.encoder, settings_.payload_type, settings_.internal_source); | 393 settings_.encoder, settings_.payload_type, settings_.internal_source); |
| 339 overuse_detector_.StartCheckForOveruse(); | |
| 340 }); | 394 }); |
| 341 } | 395 } |
| 342 | 396 |
| 343 ViEEncoder::~ViEEncoder() { | 397 ViEEncoder::~ViEEncoder() { |
| 344 RTC_DCHECK_RUN_ON(&thread_checker_); | 398 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 345 RTC_DCHECK(shutdown_event_.Wait(0)) | 399 RTC_DCHECK(shutdown_event_.Wait(0)) |
| 346 << "Must call ::Stop() before destruction."; | 400 << "Must call ::Stop() before destruction."; |
| 347 } | 401 } |
| 348 | 402 |
| 349 void ViEEncoder::Stop() { | 403 void ViEEncoder::Stop() { |
| 350 RTC_DCHECK_RUN_ON(&thread_checker_); | 404 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 351 source_proxy_->SetSource(nullptr); | 405 source_proxy_->SetSource(nullptr, true /* disable_resolution_scaling*/); |
| 352 encoder_queue_.PostTask([this] { | 406 encoder_queue_.PostTask([this] { |
| 353 RTC_DCHECK_RUN_ON(&encoder_queue_); | 407 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 354 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 408 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
| 355 false); | 409 false); |
| 356 overuse_detector_.StopCheckForOveruse(); | 410 overuse_detector_.reset(); |
| 357 shutdown_event_.Set(); | 411 shutdown_event_.Set(); |
| 358 }); | 412 }); |
| 359 | 413 |
| 360 shutdown_event_.Wait(rtc::Event::kForever); | 414 shutdown_event_.Wait(rtc::Event::kForever); |
| 361 } | 415 } |
| 362 | 416 |
| 363 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 417 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
| 364 RTC_DCHECK_RUN_ON(&thread_checker_); | 418 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 365 RTC_DCHECK(!module_process_thread_); | 419 RTC_DCHECK(!module_process_thread_); |
| 366 module_process_thread_ = module_process_thread; | 420 module_process_thread_ = module_process_thread; |
| 367 module_process_thread_->RegisterModule(&video_sender_); | 421 module_process_thread_->RegisterModule(&video_sender_); |
| 368 module_process_thread_checker_.DetachFromThread(); | 422 module_process_thread_checker_.DetachFromThread(); |
| 369 } | 423 } |
| 370 | 424 |
| 371 void ViEEncoder::DeRegisterProcessThread() { | 425 void ViEEncoder::DeRegisterProcessThread() { |
| 372 RTC_DCHECK_RUN_ON(&thread_checker_); | 426 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 373 module_process_thread_->DeRegisterModule(&video_sender_); | 427 module_process_thread_->DeRegisterModule(&video_sender_); |
| 374 } | 428 } |
| 375 | 429 |
| 376 void ViEEncoder::SetSource(rtc::VideoSourceInterface<VideoFrame>* source) { | 430 void ViEEncoder::SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 431 bool disable_resolution_scaling) { | |
| 377 RTC_DCHECK_RUN_ON(&thread_checker_); | 432 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 378 source_proxy_->SetSource(source); | 433 source_proxy_->SetSource(source, disable_resolution_scaling); |
| 434 encoder_queue_.PostTask([this, disable_resolution_scaling] { | |
| 435 RTC_DCHECK_RUN_ON(&encoder_queue_); | |
| 436 if (disable_resolution_scaling) { | |
| 437 overuse_detector_.reset(nullptr); | |
|
åsapersson
2016/10/24 07:54:06
Note that the encode time is reported to SendStati
perkj_webrtc
2016/10/26 16:40:17
Thanks for pointing this out. I did this change si
| |
| 438 } else { | |
| 439 overuse_detector_.reset(new OveruseFrameDetector( | |
| 440 clock_, GetCpuOveruseOptions(settings_.full_overuse_time), this, | |
| 441 stats_proxy_)); | |
| 442 overuse_detector_->StartCheckForOveruse(); | |
| 443 } | |
| 444 stats_proxy_->SetCpuRestrictedResolution(!disable_resolution_scaling && | |
| 445 cpu_restricted_counter_ != 0); | |
|
åsapersson
2016/10/24 07:54:06
Is SetCpuRestrictedResolution needed? Looks like w
perkj_webrtc
2016/10/26 16:40:17
I think it is needed but it is really up to you. W
| |
| 446 }); | |
| 379 } | 447 } |
| 380 | 448 |
| 381 void ViEEncoder::SetSink(EncoderSink* sink) { | 449 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
| 450 source_proxy_->SetWantsRotationApplied(rotation_applied); | |
| 382 encoder_queue_.PostTask([this, sink] { | 451 encoder_queue_.PostTask([this, sink] { |
| 383 RTC_DCHECK_RUN_ON(&encoder_queue_); | 452 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 384 sink_ = sink; | 453 sink_ = sink; |
| 385 }); | 454 }); |
| 386 } | 455 } |
| 387 | 456 |
| 388 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { | 457 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { |
| 389 encoder_queue_.PostTask([this, start_bitrate_bps] { | 458 encoder_queue_.PostTask([this, start_bitrate_bps] { |
| 390 RTC_DCHECK_RUN_ON(&encoder_queue_); | 459 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 391 encoder_start_bitrate_bps_ = start_bitrate_bps; | 460 encoder_start_bitrate_bps_ = start_bitrate_bps; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 if (stats_proxy_) { | 525 if (stats_proxy_) { |
| 457 stats_proxy_->OnEncoderReconfigured(encoder_config_, | 526 stats_proxy_->OnEncoderReconfigured(encoder_config_, |
| 458 rate_allocator_->GetPreferedBitrate()); | 527 rate_allocator_->GetPreferedBitrate()); |
| 459 } | 528 } |
| 460 sink_->OnEncoderConfigurationChanged( | 529 sink_->OnEncoderConfigurationChanged( |
| 461 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 530 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
| 462 } | 531 } |
| 463 | 532 |
| 464 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 533 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
| 465 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 534 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
| 466 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | |
| 467 | |
| 468 VideoFrame incoming_frame = video_frame; | 535 VideoFrame incoming_frame = video_frame; |
| 469 | 536 |
| 470 // Local time in webrtc time base. | 537 // Local time in webrtc time base. |
| 471 int64_t current_time = clock_->TimeInMilliseconds(); | 538 int64_t current_time = clock_->TimeInMilliseconds(); |
| 472 incoming_frame.set_render_time_ms(current_time); | 539 incoming_frame.set_render_time_ms(current_time); |
| 473 | 540 |
| 474 // Capture time may come from clock with an offset and drift from clock_. | 541 // Capture time may come from clock with an offset and drift from clock_. |
| 475 int64_t capture_ntp_time_ms; | 542 int64_t capture_ntp_time_ms; |
| 476 if (video_frame.ntp_time_ms() != 0) { | 543 if (video_frame.ntp_time_ms() != 0) { |
| 477 capture_ntp_time_ms = video_frame.ntp_time_ms(); | 544 capture_ntp_time_ms = video_frame.ntp_time_ms(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 // End trace event on first frame after encoder resumes, if frame was dropped. | 598 // End trace event on first frame after encoder resumes, if frame was dropped. |
| 532 if (encoder_paused_and_dropped_frame_) { | 599 if (encoder_paused_and_dropped_frame_) { |
| 533 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); | 600 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); |
| 534 } | 601 } |
| 535 encoder_paused_and_dropped_frame_ = false; | 602 encoder_paused_and_dropped_frame_ = false; |
| 536 } | 603 } |
| 537 | 604 |
| 538 void ViEEncoder::EncodeVideoFrame(const VideoFrame& video_frame, | 605 void ViEEncoder::EncodeVideoFrame(const VideoFrame& video_frame, |
| 539 int64_t time_when_posted_in_ms) { | 606 int64_t time_when_posted_in_ms) { |
| 540 RTC_DCHECK_RUN_ON(&encoder_queue_); | 607 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 608 | |
| 541 if (pre_encode_callback_) | 609 if (pre_encode_callback_) |
| 542 pre_encode_callback_->OnFrame(video_frame); | 610 pre_encode_callback_->OnFrame(video_frame); |
| 543 | 611 |
| 544 if (video_frame.width() != last_frame_info_->width || | 612 if (video_frame.width() != last_frame_info_->width || |
| 545 video_frame.height() != last_frame_info_->height || | 613 video_frame.height() != last_frame_info_->height || |
| 546 video_frame.rotation() != last_frame_info_->rotation || | 614 video_frame.rotation() != last_frame_info_->rotation || |
| 547 video_frame.is_texture() != last_frame_info_->is_texture) { | 615 video_frame.is_texture() != last_frame_info_->is_texture) { |
| 548 pending_encoder_reconfiguration_ = true; | 616 pending_encoder_reconfiguration_ = true; |
| 549 last_frame_info_ = rtc::Optional<VideoFrameInfo>( | 617 last_frame_info_ = rtc::Optional<VideoFrameInfo>( |
| 550 VideoFrameInfo(video_frame.width(), video_frame.height(), | 618 VideoFrameInfo(video_frame.width(), video_frame.height(), |
| 551 video_frame.rotation(), video_frame.is_texture())); | 619 video_frame.rotation(), video_frame.is_texture())); |
| 552 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" | 620 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" |
| 553 << last_frame_info_->width << "x" << last_frame_info_->height | 621 << last_frame_info_->width << "x" << last_frame_info_->height |
| 554 << ", rotation=" << last_frame_info_->rotation | 622 << ", rotation=" << last_frame_info_->rotation |
| 555 << ", texture=" << last_frame_info_->is_texture; | 623 << ", texture=" << last_frame_info_->is_texture; |
| 556 } | 624 } |
| 557 | 625 |
| 558 if (pending_encoder_reconfiguration_) { | 626 if (pending_encoder_reconfiguration_) { |
| 559 ReconfigureEncoder(); | 627 ReconfigureEncoder(); |
| 560 } | 628 } |
| 561 | 629 |
| 562 if (EncoderPaused()) { | 630 if (EncoderPaused()) { |
| 563 TraceFrameDropStart(); | 631 TraceFrameDropStart(); |
| 564 return; | 632 return; |
| 565 } | 633 } |
| 566 TraceFrameDropEnd(); | 634 TraceFrameDropEnd(); |
| 567 | 635 |
| 636 last_frame_height_ = video_frame.height(); | |
| 637 last_frame_width_ = video_frame.width(); | |
| 638 | |
| 568 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), | 639 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), |
| 569 "Encode"); | 640 "Encode"); |
| 570 | 641 |
| 571 overuse_detector_.FrameCaptured(video_frame, time_when_posted_in_ms); | 642 if (overuse_detector_) { |
| 643 overuse_detector_->FrameCaptured(video_frame, time_when_posted_in_ms); | |
| 644 } | |
| 572 | 645 |
| 573 if (codec_type_ == webrtc::kVideoCodecVP8) { | 646 if (codec_type_ == webrtc::kVideoCodecVP8) { |
| 574 webrtc::CodecSpecificInfo codec_specific_info; | 647 webrtc::CodecSpecificInfo codec_specific_info; |
| 575 codec_specific_info.codecType = webrtc::kVideoCodecVP8; | 648 codec_specific_info.codecType = webrtc::kVideoCodecVP8; |
| 576 | 649 |
| 577 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI = | 650 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI = |
| 578 has_received_rpsi_; | 651 has_received_rpsi_; |
| 579 codec_specific_info.codecSpecific.VP8.hasReceivedSLI = | 652 codec_specific_info.codecSpecific.VP8.hasReceivedSLI = |
| 580 has_received_sli_; | 653 has_received_sli_; |
| 581 codec_specific_info.codecSpecific.VP8.pictureIdRPSI = | 654 codec_specific_info.codecSpecific.VP8.pictureIdRPSI = |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 604 const EncodedImage& encoded_image, | 677 const EncodedImage& encoded_image, |
| 605 const CodecSpecificInfo* codec_specific_info, | 678 const CodecSpecificInfo* codec_specific_info, |
| 606 const RTPFragmentationHeader* fragmentation) { | 679 const RTPFragmentationHeader* fragmentation) { |
| 607 // Encoded is called on whatever thread the real encoder implementation run | 680 // Encoded is called on whatever thread the real encoder implementation run |
| 608 // on. In the case of hardware encoders, there might be several encoders | 681 // on. In the case of hardware encoders, there might be several encoders |
| 609 // running in parallel on different threads. | 682 // running in parallel on different threads. |
| 610 if (stats_proxy_) { | 683 if (stats_proxy_) { |
| 611 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); | 684 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); |
| 612 } | 685 } |
| 613 | 686 |
| 687 if (encoder_timing_) { | |
| 688 encoder_timing_->OnEncodeTiming( | |
| 689 encoded_image.ntp_time_ms_, | |
| 690 clock_->TimeInMilliseconds() - encoded_image.capture_time_ms_); | |
| 691 } | |
| 692 | |
| 614 EncodedImageCallback::Result result = | 693 EncodedImageCallback::Result result = |
| 615 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); | 694 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
| 616 | 695 |
| 617 int64_t time_sent = clock_->TimeInMilliseconds(); | 696 int64_t time_sent = clock_->TimeInMilliseconds(); |
| 618 uint32_t timestamp = encoded_image._timeStamp; | 697 uint32_t timestamp = encoded_image._timeStamp; |
| 619 encoder_queue_.PostTask([this, timestamp, time_sent] { | 698 |
| 699 auto call_overuse_frame_sent = [this, timestamp, time_sent] { | |
| 620 RTC_DCHECK_RUN_ON(&encoder_queue_); | 700 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 621 overuse_detector_.FrameSent(timestamp, time_sent); | 701 if (overuse_detector_) { |
| 622 }); | 702 overuse_detector_->FrameSent(timestamp, time_sent); |
| 703 } | |
| 704 }; | |
| 705 if (encoder_queue_.IsCurrent()) { | |
| 706 call_overuse_frame_sent(); | |
|
kthelgason
2016/10/19 12:06:44
Is this contortion really saving us anything? Seem
perkj_webrtc
2016/10/26 16:40:17
Done.
| |
| 707 } else { | |
| 708 encoder_queue_.PostTask(call_overuse_frame_sent); | |
| 709 } | |
| 710 | |
| 623 return result; | 711 return result; |
| 624 } | 712 } |
| 625 | 713 |
| 626 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { | 714 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { |
| 627 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); | 715 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
| 628 if (stats_proxy_) | 716 if (stats_proxy_) |
| 629 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); | 717 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); |
| 630 } | 718 } |
| 631 | 719 |
| 632 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) { | 720 void ViEEncoder::OnReceivedSLI(uint8_t picture_id) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 690 | 778 |
| 691 if (stats_proxy_ && video_suspension_changed) { | 779 if (stats_proxy_ && video_suspension_changed) { |
| 692 LOG(LS_INFO) << "Video suspend state changed to: " | 780 LOG(LS_INFO) << "Video suspend state changed to: " |
| 693 << (video_is_suspended ? "suspended" : "not suspended"); | 781 << (video_is_suspended ? "suspended" : "not suspended"); |
| 694 stats_proxy_->OnSuspendChange(video_is_suspended); | 782 stats_proxy_->OnSuspendChange(video_is_suspended); |
| 695 } | 783 } |
| 696 } | 784 } |
| 697 | 785 |
| 698 void ViEEncoder::OveruseDetected() { | 786 void ViEEncoder::OveruseDetected() { |
| 699 RTC_DCHECK_RUN_ON(&encoder_queue_); | 787 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 700 // TODO(perkj): When ViEEncoder inherit rtc::VideoSink instead of | 788 LOG(LS_INFO) << "OveruseDetected usage"; |
|
kthelgason
2016/10/19 12:06:44
This is a weird log message! Perhaps change this t
perkj_webrtc
2016/10/26 16:40:17
Done.
| |
| 701 // VideoCaptureInput |load_observer_| should be removed and overuse be | 789 if (cpu_restricted_counter_ >= kMaxCpuDowngrades) { |
| 702 // expressed as rtc::VideoSinkWants instead. | 790 return; |
| 703 if (load_observer_) | 791 } |
| 704 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); | 792 // Request lower resolution if the current resolution is lower than last time |
| 793 // we asked for the resolution to be lowered. | |
| 794 // Update stats accordingly. | |
| 795 int current_pixel_count = last_frame_height_ * last_frame_width_; | |
| 796 if (current_pixel_count < | |
| 797 max_pixel_count_.value_or(current_pixel_count + 1)) { | |
|
kthelgason
2016/10/19 12:06:44
I think this would read better as (max_pixel_count
perkj_webrtc
2016/10/26 16:40:17
Done.
I prefer optional instead of a magic value
| |
| 798 max_pixel_count_ = rtc::Optional<int>(current_pixel_count); | |
| 799 max_pixel_count_step_up_ = rtc::Optional<int>(); | |
|
kthelgason
2016/10/19 12:06:44
Same here, this could just be an int initialized t
perkj_webrtc
2016/10/26 16:40:17
dito
| |
| 800 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | |
| 801 ++cpu_restricted_counter_; | |
| 802 source_proxy_->RequestResolutionLowerThan(current_pixel_count); | |
| 803 } | |
| 705 } | 804 } |
| 706 | 805 |
| 707 void ViEEncoder::NormalUsage() { | 806 void ViEEncoder::NormalUsage() { |
| 708 RTC_DCHECK_RUN_ON(&encoder_queue_); | 807 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 709 if (load_observer_) | 808 int current_pixel_count = last_frame_height_ * last_frame_width_; |
| 710 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); | 809 // Request higher resolution if we are cpu restricted and the the current |
| 810 // resolution is higher than last time we requested higher resolution. | |
| 811 // Update stats accordingly. | |
| 812 if (cpu_restricted_counter_ > 0 && | |
| 813 current_pixel_count > max_pixel_count_step_up_.value_or(0)) { | |
| 814 max_pixel_count_ = rtc::Optional<int>(); | |
| 815 max_pixel_count_step_up_ = rtc::Optional<int>(current_pixel_count); | |
| 816 --cpu_restricted_counter_; | |
| 817 stats_proxy_->OnCpuRestrictedResolutionChanged(cpu_restricted_counter_ > 0); | |
| 818 source_proxy_->RequestHigherResolutionThan(current_pixel_count); | |
| 819 } | |
| 820 } | |
| 821 | |
| 822 bool ViEEncoder::OveruseFrameDetectionEnabled() const { | |
| 823 RTC_DCHECK_RUN_ON(&encoder_queue_); | |
| 824 return overuse_detector_ != nullptr; | |
| 711 } | 825 } |
| 712 | 826 |
| 713 } // namespace webrtc | 827 } // namespace webrtc |
| OLD | NEW |