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 |
11 #include "webrtc/video/vie_encoder.h" | 11 #include "webrtc/video/vie_encoder.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
15 #include <utility> | 15 #include <utility> |
16 | 16 |
17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
19 #include "webrtc/base/trace_event.h" | 19 #include "webrtc/base/trace_event.h" |
20 #include "webrtc/base/timeutils.h" | 20 #include "webrtc/base/timeutils.h" |
21 #include "webrtc/modules/pacing/paced_sender.h" | 21 #include "webrtc/modules/pacing/paced_sender.h" |
22 #include "webrtc/modules/video_coding/codecs/vp8/screenshare_layers.h" | |
22 #include "webrtc/modules/video_coding/include/video_coding.h" | 23 #include "webrtc/modules/video_coding/include/video_coding.h" |
23 #include "webrtc/modules/video_coding/include/video_coding_defines.h" | 24 #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
25 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h" | |
26 #include "webrtc/modules/video_coding/utility/simulcast_rate_allocator.h" | |
24 #include "webrtc/video/overuse_frame_detector.h" | 27 #include "webrtc/video/overuse_frame_detector.h" |
25 #include "webrtc/video/send_statistics_proxy.h" | 28 #include "webrtc/video/send_statistics_proxy.h" |
26 #include "webrtc/video_frame.h" | 29 #include "webrtc/video_frame.h" |
27 | 30 |
28 namespace webrtc { | 31 namespace webrtc { |
29 | 32 |
30 namespace { | 33 namespace { |
31 // Time interval for logging frame counts. | 34 // Time interval for logging frame counts. |
32 const int64_t kFrameLogIntervalMs = 60000; | 35 const int64_t kFrameLogIntervalMs = 60000; |
33 | 36 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 // remotely cope with the load right now. | 182 // remotely cope with the load right now. |
180 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { | 183 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { |
181 CpuOveruseOptions options; | 184 CpuOveruseOptions options; |
182 if (full_overuse_time) { | 185 if (full_overuse_time) { |
183 options.low_encode_usage_threshold_percent = 150; | 186 options.low_encode_usage_threshold_percent = 150; |
184 options.high_encode_usage_threshold_percent = 200; | 187 options.high_encode_usage_threshold_percent = 200; |
185 } | 188 } |
186 return options; | 189 return options; |
187 } | 190 } |
188 | 191 |
192 struct ScreenshareTemporalLayersFactory : webrtc::TemporalLayersFactory { | |
193 ScreenshareTemporalLayersFactory() {} | |
194 virtual ~ScreenshareTemporalLayersFactory() {} | |
195 | |
196 virtual webrtc::TemporalLayers* Create(int simulcast_id, | |
197 int num_temporal_layers, | |
198 uint8_t initial_tl0_pic_idx) const { | |
199 webrtc::TemporalLayers* tl = new webrtc::ScreenshareLayers( | |
200 num_temporal_layers, rand(), webrtc::Clock::GetRealTimeClock()); | |
201 if (listener_) | |
202 listener_->OnTemporalLayersCreated(simulcast_id, tl); | |
203 return tl; | |
204 } | |
205 }; | |
206 | |
189 } // namespace | 207 } // namespace |
190 | 208 |
191 class ViEEncoder::ConfigureEncoderTask : public rtc::QueuedTask { | 209 class ViEEncoder::ConfigureEncoderTask : public rtc::QueuedTask { |
192 public: | 210 public: |
193 ConfigureEncoderTask(ViEEncoder* vie_encoder, | 211 ConfigureEncoderTask(ViEEncoder* vie_encoder, |
194 VideoEncoderConfig config, | 212 VideoEncoderConfig config, |
195 size_t max_data_payload_length) | 213 size_t max_data_payload_length) |
196 : vie_encoder_(vie_encoder), | 214 : vie_encoder_(vie_encoder), |
197 config_(std::move(config)), | 215 config_(std::move(config)), |
198 max_data_payload_length_(max_data_payload_length) {} | 216 max_data_payload_length_(max_data_payload_length) {} |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 RTC_DCHECK_RUN_ON(&thread_checker_); | 362 RTC_DCHECK_RUN_ON(&thread_checker_); |
345 RTC_DCHECK(shutdown_event_.Wait(0)) | 363 RTC_DCHECK(shutdown_event_.Wait(0)) |
346 << "Must call ::Stop() before destruction."; | 364 << "Must call ::Stop() before destruction."; |
347 } | 365 } |
348 | 366 |
349 void ViEEncoder::Stop() { | 367 void ViEEncoder::Stop() { |
350 RTC_DCHECK_RUN_ON(&thread_checker_); | 368 RTC_DCHECK_RUN_ON(&thread_checker_); |
351 source_proxy_->SetSource(nullptr); | 369 source_proxy_->SetSource(nullptr); |
352 encoder_queue_.PostTask([this] { | 370 encoder_queue_.PostTask([this] { |
353 RTC_DCHECK_RUN_ON(&encoder_queue_); | 371 RTC_DCHECK_RUN_ON(&encoder_queue_); |
372 rate_allocator_.reset(); | |
354 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 373 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
355 false); | 374 false); |
356 overuse_detector_.StopCheckForOveruse(); | 375 overuse_detector_.StopCheckForOveruse(); |
357 shutdown_event_.Set(); | 376 shutdown_event_.Set(); |
358 }); | 377 }); |
359 | 378 |
360 shutdown_event_.Wait(rtc::Event::kForever); | 379 shutdown_event_.Wait(rtc::Event::kForever); |
361 } | 380 } |
362 | 381 |
363 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 382 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 last_frame_info_->width, last_frame_info_->height, encoder_config_); | 450 last_frame_info_->width, last_frame_info_->height, encoder_config_); |
432 | 451 |
433 VideoCodec codec = VideoEncoderConfigToVideoCodec( | 452 VideoCodec codec = VideoEncoderConfigToVideoCodec( |
434 encoder_config_, streams, settings_.payload_name, settings_.payload_type); | 453 encoder_config_, streams, settings_.payload_name, settings_.payload_type); |
435 | 454 |
436 codec.startBitrate = | 455 codec.startBitrate = |
437 std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate); | 456 std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate); |
438 codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate); | 457 codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate); |
439 codec.expect_encode_from_texture = last_frame_info_->is_texture; | 458 codec.expect_encode_from_texture = last_frame_info_->is_texture; |
440 | 459 |
460 switch (codec.codecType) { | |
perkj_webrtc
2016/10/21 08:24:29
Can we try to keep ViEEncoder codec agnostic?
Ie-
sprang_webrtc
2016/10/25 10:44:25
I created a separate factory class for this, since
| |
461 case kVideoCodecVP8: { | |
462 SimulcastRateAllocator* rate_allocator = | |
463 new SimulcastRateAllocator(codec); | |
464 rate_allocator_.reset(rate_allocator); | |
perkj_webrtc
2016/10/21 08:24:29
rate_allocator_.reset(new SimulcastRateAllocator(c
sprang_webrtc
2016/10/25 10:44:25
Done. This was to avoid a few static_cast when acc
| |
465 | |
466 // Set up default VP8 temporal layer factory, if not provided. | |
467 if (!codec.codecSpecific.VP8.tl_factory) { | |
468 if (codec.mode == kScreensharing && | |
469 codec.numberOfSimulcastStreams == 1 && | |
470 codec.codecSpecific.VP8.numberOfTemporalLayers == 2) { | |
471 // Conference mode temporal layering for screen content. | |
472 vp8_tl_factory_.reset(new ScreenshareTemporalLayersFactory()); | |
473 } else { | |
474 // Standard video temporal layers. | |
475 vp8_tl_factory_.reset(new TemporalLayersFactory()); | |
476 } | |
477 codec.codecSpecific.VP8.tl_factory = vp8_tl_factory_.get(); | |
478 } | |
479 vp8_tl_factory_->SetListener(rate_allocator); | |
perkj_webrtc
2016/10/21 08:24:29
Can we hide this from ViEEncoder by hiding it in S
sprang_webrtc
2016/10/25 10:44:25
I can do it so it can optionally hold the tl facto
| |
480 } break; | |
481 default: | |
482 rate_allocator_.reset(new DefaultVideoBitrateAllocator(codec)); | |
483 } | |
484 | |
441 bool success = video_sender_.RegisterSendCodec( | 485 bool success = video_sender_.RegisterSendCodec( |
442 &codec, number_of_cores_, | 486 &codec, number_of_cores_, |
443 static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; | 487 static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; |
444 if (!success) { | 488 if (!success) { |
445 LOG(LS_ERROR) << "Failed to configure encoder."; | 489 LOG(LS_ERROR) << "Failed to configure encoder."; |
446 RTC_DCHECK(success); | 490 RTC_DCHECK(success); |
447 } | 491 } |
448 | 492 |
449 rate_allocator_.reset(new SimulcastRateAllocator(codec)); | |
450 if (stats_proxy_) { | 493 if (stats_proxy_) { |
451 stats_proxy_->OnEncoderReconfigured(encoder_config_, | 494 int frame_rate = stats_proxy_->GetSendFrameRate(); |
452 rate_allocator_->GetPreferedBitrate()); | 495 if (frame_rate == 0) |
496 frame_rate = codec.maxFramerate; | |
perkj_webrtc
2016/10/21 08:24:29
codec.maxFramerate does not have a real meaning. I
sprang_webrtc
2016/10/25 10:44:25
I know, this is just a placeholder until we have v
| |
497 stats_proxy_->OnEncoderReconfigured( | |
498 encoder_config_, rate_allocator_->GetPreferedBitrate(frame_rate)); | |
453 } | 499 } |
454 | 500 |
455 pending_encoder_reconfiguration_ = false; | 501 pending_encoder_reconfiguration_ = false; |
456 if (stats_proxy_) { | 502 |
457 stats_proxy_->OnEncoderReconfigured(encoder_config_, | |
458 rate_allocator_->GetPreferedBitrate()); | |
459 } | |
460 sink_->OnEncoderConfigurationChanged( | 503 sink_->OnEncoderConfigurationChanged( |
461 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 504 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
462 } | 505 } |
463 | 506 |
464 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 507 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
465 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 508 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
466 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | 509 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); |
467 | 510 |
468 VideoFrame incoming_frame = video_frame; | 511 VideoFrame incoming_frame = video_frame; |
469 | 512 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 return; | 715 return; |
673 } | 716 } |
674 RTC_DCHECK_RUN_ON(&encoder_queue_); | 717 RTC_DCHECK_RUN_ON(&encoder_queue_); |
675 RTC_DCHECK(sink_) << "sink_ must be set before the encoder is active."; | 718 RTC_DCHECK(sink_) << "sink_ must be set before the encoder is active."; |
676 | 719 |
677 LOG(LS_VERBOSE) << "OnBitrateUpdated, bitrate " << bitrate_bps | 720 LOG(LS_VERBOSE) << "OnBitrateUpdated, bitrate " << bitrate_bps |
678 << " packet loss " << static_cast<int>(fraction_lost) | 721 << " packet loss " << static_cast<int>(fraction_lost) |
679 << " rtt " << round_trip_time_ms; | 722 << " rtt " << round_trip_time_ms; |
680 | 723 |
681 video_sender_.SetChannelParameters(bitrate_bps, fraction_lost, | 724 video_sender_.SetChannelParameters(bitrate_bps, fraction_lost, |
682 round_trip_time_ms); | 725 round_trip_time_ms, rate_allocator_.get()); |
683 | 726 |
684 encoder_start_bitrate_bps_ = | 727 encoder_start_bitrate_bps_ = |
685 bitrate_bps != 0 ? bitrate_bps : encoder_start_bitrate_bps_; | 728 bitrate_bps != 0 ? bitrate_bps : encoder_start_bitrate_bps_; |
686 bool video_is_suspended = bitrate_bps == 0; | 729 bool video_is_suspended = bitrate_bps == 0; |
687 bool video_suspension_changed = | 730 bool video_suspension_changed = |
688 video_is_suspended != (last_observed_bitrate_bps_ == 0); | 731 video_is_suspended != (last_observed_bitrate_bps_ == 0); |
689 last_observed_bitrate_bps_ = bitrate_bps; | 732 last_observed_bitrate_bps_ = bitrate_bps; |
690 | 733 |
691 if (stats_proxy_ && video_suspension_changed) { | 734 if (stats_proxy_ && video_suspension_changed) { |
692 LOG(LS_INFO) << "Video suspend state changed to: " | 735 LOG(LS_INFO) << "Video suspend state changed to: " |
(...skipping 11 matching lines...) Expand all Loading... | |
704 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); | 747 load_observer_->OnLoadUpdate(LoadObserver::kOveruse); |
705 } | 748 } |
706 | 749 |
707 void ViEEncoder::NormalUsage() { | 750 void ViEEncoder::NormalUsage() { |
708 RTC_DCHECK_RUN_ON(&encoder_queue_); | 751 RTC_DCHECK_RUN_ON(&encoder_queue_); |
709 if (load_observer_) | 752 if (load_observer_) |
710 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); | 753 load_observer_->OnLoadUpdate(LoadObserver::kUnderuse); |
711 } | 754 } |
712 | 755 |
713 } // namespace webrtc | 756 } // namespace webrtc |
OLD | NEW |