Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: webrtc/video/video_send_stream.cc

Issue 2070343002: Remove ViEncoder::Pause / Start (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 config_(config), 388 config_(config),
389 suspended_ssrcs_(suspended_ssrcs), 389 suspended_ssrcs_(suspended_ssrcs),
390 module_process_thread_(module_process_thread), 390 module_process_thread_(module_process_thread),
391 call_stats_(call_stats), 391 call_stats_(call_stats),
392 congestion_controller_(congestion_controller), 392 congestion_controller_(congestion_controller),
393 bitrate_allocator_(bitrate_allocator), 393 bitrate_allocator_(bitrate_allocator),
394 remb_(remb), 394 remb_(remb),
395 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), 395 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"),
396 encoder_wakeup_event_(false, false), 396 encoder_wakeup_event_(false, false),
397 stop_encoder_thread_(0), 397 stop_encoder_thread_(0),
398 send_stream_registered_as_observer_(false), 398 state_(kStopped),
399 overuse_detector_( 399 overuse_detector_(
400 Clock::GetRealTimeClock(), 400 Clock::GetRealTimeClock(),
401 GetCpuOveruseOptions(config.encoder_settings.full_overuse_time), 401 GetCpuOveruseOptions(config.encoder_settings.full_overuse_time),
402 this, 402 this,
403 config.post_encode_callback, 403 config.post_encode_callback,
404 &stats_proxy_), 404 &stats_proxy_),
405 vie_encoder_(num_cpu_cores, 405 vie_encoder_(num_cpu_cores,
406 module_process_thread_, 406 module_process_thread_,
407 &stats_proxy_, 407 &stats_proxy_,
408 &overuse_detector_, 408 &overuse_detector_,
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) 522 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
523 rtp_rtcp->IncomingRtcpPacket(packet, length); 523 rtp_rtcp->IncomingRtcpPacket(packet, length);
524 return true; 524 return true;
525 } 525 }
526 526
527 void VideoSendStream::Start() { 527 void VideoSendStream::Start() {
528 if (payload_router_.active()) 528 if (payload_router_.active())
529 return; 529 return;
530 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); 530 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start");
531 payload_router_.set_active(true); 531 payload_router_.set_active(true);
532 // Was not already started, trigger a keyframe. 532 {
533 vie_encoder_.SendKeyFrame(); 533 rtc::CritScope lock(&encoder_settings_crit_);
534 vie_encoder_.Start(); 534 pending_state_change_ = rtc::Optional<bool>(true);
535 }
536 encoder_wakeup_event_.Set();
535 } 537 }
536 538
537 void VideoSendStream::Stop() { 539 void VideoSendStream::Stop() {
538 if (!payload_router_.active()) 540 if (!payload_router_.active())
539 return; 541 return;
540 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); 542 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop");
541 vie_encoder_.Pause();
542 payload_router_.set_active(false); 543 payload_router_.set_active(false);
544 {
545 rtc::CritScope lock(&encoder_settings_crit_);
546 pending_state_change_ = rtc::Optional<bool>(false);
547 }
548 encoder_wakeup_event_.Set();
543 } 549 }
544 550
545 VideoCaptureInput* VideoSendStream::Input() { 551 VideoCaptureInput* VideoSendStream::Input() {
546 return &input_; 552 return &input_;
547 } 553 }
548 554
549 bool VideoSendStream::EncoderThreadFunction(void* obj) { 555 bool VideoSendStream::EncoderThreadFunction(void* obj) {
550 static_cast<VideoSendStream*>(obj)->EncoderProcess(); 556 static_cast<VideoSendStream*>(obj)->EncoderProcess();
551 // We're done, return false to abort. 557 // We're done, return false to abort.
552 return false; 558 return false;
553 } 559 }
554 560
555 void VideoSendStream::EncoderProcess() { 561 void VideoSendStream::EncoderProcess() {
556 RTC_CHECK_EQ(0, vie_encoder_.RegisterExternalEncoder( 562 RTC_CHECK_EQ(0, vie_encoder_.RegisterExternalEncoder(
557 config_.encoder_settings.encoder, 563 config_.encoder_settings.encoder,
558 config_.encoder_settings.payload_type, 564 config_.encoder_settings.payload_type,
559 config_.encoder_settings.internal_source)); 565 config_.encoder_settings.internal_source));
560 566
561 while (true) { 567 while (true) {
562 // Wake up every kEncodeCheckForActivityPeriodMs to check if the encoder is 568 // Wake up every kEncodeCheckForActivityPeriodMs to check if the encoder is
563 // active. If not, deregister as BitrateAllocatorObserver. 569 // active. If not, deregister as BitrateAllocatorObserver.
564 const int kEncodeCheckForActivityPeriodMs = 1000; 570 const int kEncodeCheckForActivityPeriodMs = 1000;
565 encoder_wakeup_event_.Wait(kEncodeCheckForActivityPeriodMs); 571 encoder_wakeup_event_.Wait(kEncodeCheckForActivityPeriodMs);
566 if (rtc::AtomicOps::AcquireLoad(&stop_encoder_thread_)) 572 if (rtc::AtomicOps::AcquireLoad(&stop_encoder_thread_))
567 break; 573 break;
568 bool change_settings = false; 574 bool change_settings = false;
575 rtc::Optional<bool> pending_state_change;
569 { 576 {
570 rtc::CritScope lock(&encoder_settings_crit_); 577 rtc::CritScope lock(&encoder_settings_crit_);
571 if (pending_encoder_settings_) { 578 if (pending_encoder_settings_) {
572 std::swap(current_encoder_settings_, pending_encoder_settings_); 579 std::swap(current_encoder_settings_, pending_encoder_settings_);
573 pending_encoder_settings_.reset(); 580 pending_encoder_settings_.reset();
574 change_settings = true; 581 change_settings = true;
582 } else if (pending_state_change_) {
583 swap(pending_state_change, pending_state_change_);
575 } 584 }
576 } 585 }
577 if (change_settings) { 586 if (change_settings) {
578 current_encoder_settings_->video_codec.startBitrate = 587 current_encoder_settings_->video_codec.startBitrate = std::max(
579 bitrate_allocator_->AddObserver( 588 bitrate_allocator_->GetStartBitrate(this) / 1000,
580 this, current_encoder_settings_->video_codec.minBitrate * 1000, 589 static_cast<int>(current_encoder_settings_->video_codec.minBitrate));
581 current_encoder_settings_->video_codec.maxBitrate * 1000,
582 CalulcateMaxPadBitrateBps(current_encoder_settings_->config,
583 config_.suspend_below_min_bitrate),
584 !config_.suspend_below_min_bitrate) /
585 1000;
586 send_stream_registered_as_observer_ = true;
587
588 payload_router_.SetSendStreams(current_encoder_settings_->config.streams); 590 payload_router_.SetSendStreams(current_encoder_settings_->config.streams);
589 vie_encoder_.SetEncoder(current_encoder_settings_->video_codec, 591 vie_encoder_.SetEncoder(current_encoder_settings_->video_codec,
590 payload_router_.MaxPayloadLength()); 592 payload_router_.MaxPayloadLength());
591 593
592 // Clear stats for disabled layers. 594 // Clear stats for disabled layers.
593 for (size_t i = current_encoder_settings_->config.streams.size(); 595 for (size_t i = current_encoder_settings_->config.streams.size();
594 i < config_.rtp.ssrcs.size(); ++i) { 596 i < config_.rtp.ssrcs.size(); ++i) {
595 stats_proxy_.OnInactiveSsrc(config_.rtp.ssrcs[i]); 597 stats_proxy_.OnInactiveSsrc(config_.rtp.ssrcs[i]);
596 } 598 }
597 599
598 size_t number_of_temporal_layers = 600 size_t number_of_temporal_layers =
599 current_encoder_settings_->config.streams.back() 601 current_encoder_settings_->config.streams.back()
600 .temporal_layer_thresholds_bps.size() + 602 .temporal_layer_thresholds_bps.size() +
601 1; 603 1;
602 protection_bitrate_calculator_.SetEncodingData( 604 protection_bitrate_calculator_.SetEncodingData(
603 current_encoder_settings_->video_codec.startBitrate * 1000, 605 current_encoder_settings_->video_codec.startBitrate * 1000,
604 current_encoder_settings_->video_codec.width, 606 current_encoder_settings_->video_codec.width,
605 current_encoder_settings_->video_codec.height, 607 current_encoder_settings_->video_codec.height,
606 current_encoder_settings_->video_codec.maxFramerate, 608 current_encoder_settings_->video_codec.maxFramerate,
607 number_of_temporal_layers, payload_router_.MaxPayloadLength()); 609 number_of_temporal_layers, payload_router_.MaxPayloadLength());
608 610
609 // We might've gotten new settings while configuring the encoder settings, 611 // We might've gotten new settings while configuring the encoder settings,
610 // restart from the top to see if that's the case before trying to encode 612 // restart from the top to see if that's the case before trying to encode
611 // a frame (which might correspond to the last frame size). 613 // a frame (which might correspond to the last frame size).
612 encoder_wakeup_event_.Set(); 614 encoder_wakeup_event_.Set();
613 continue; 615 continue;
614 } 616 }
615 617
616 VideoFrame frame; 618 if (pending_state_change) {
617 if (input_.GetVideoFrame(&frame)) { 619 if (*pending_state_change == true && state_ == kStopped) {
pbos-webrtc 2016/06/17 12:38:21 no == true
perkj_webrtc 2016/06/17 13:06:32 Done.
618 // TODO(perkj): |pre_encode_callback| is only used by tests. Tests should 620 bitrate_allocator_->AddObserver(
619 // register as a sink to the VideoSource instead. 621 this, current_encoder_settings_->video_codec.minBitrate * 1000,
620 if (config_.pre_encode_callback) { 622 current_encoder_settings_->video_codec.maxBitrate * 1000,
621 config_.pre_encode_callback->OnFrame(frame); 623 CalulcateMaxPadBitrateBps(current_encoder_settings_->config,
624 config_.suspend_below_min_bitrate),
625 !config_.suspend_below_min_bitrate);
626 vie_encoder_.SendKeyFrame();
627 state_ = kStarted;
628 LOG_F(LS_INFO) << "Encoder started.";
629 } else if (*pending_state_change == false) {
pbos-webrtc 2016/06/17 12:38:21 !(*pending_state_change)
perkj_webrtc 2016/06/17 13:06:32 Done.
630 bitrate_allocator_->RemoveObserver(this);
631 vie_encoder_.OnBitrateUpdated(0, 0, 0);
632 state_ = kStopped;
633 LOG_F(LS_INFO) << "Encoder stopped.";
622 } 634 }
623 vie_encoder_.EncodeVideoFrame(frame); 635 encoder_wakeup_event_.Set();
636 continue;
624 } 637 }
625 638
626 // Check if the encoder has produced anything the last kEncoderTimeOutMs. 639 // Check if the encoder has produced anything the last kEncoderTimeOutMs.
627 // If not, deregister as BitrateAllocatorObserver. 640 // If not, deregister as BitrateAllocatorObserver.
628 if (send_stream_registered_as_observer_ && 641 if (state_ == kStarted &&
629 vie_encoder_.time_of_last_frame_activity_ms() < 642 vie_encoder_.time_of_last_frame_activity_ms() <
630 rtc::TimeMillis() - kEncoderTimeOutMs) { 643 rtc::TimeMillis() - kEncoderTimeOutMs) {
631 // The encoder has timed out. 644 // The encoder has timed out.
632 LOG_F(LS_INFO) << "Encoder timed out."; 645 LOG_F(LS_INFO) << "Encoder timed out.";
633 bitrate_allocator_->RemoveObserver(this); 646 bitrate_allocator_->RemoveObserver(this);
634 send_stream_registered_as_observer_ = false; 647 state_ = kEncoderTimedOut;
635 } 648 }
636 if (!send_stream_registered_as_observer_ && 649 if (state_ == kEncoderTimedOut &&
637 vie_encoder_.time_of_last_frame_activity_ms() > 650 vie_encoder_.time_of_last_frame_activity_ms() >
638 rtc::TimeMillis() - kEncoderTimeOutMs) { 651 rtc::TimeMillis() - kEncoderTimeOutMs) {
639 LOG_F(LS_INFO) << "Encoder is active."; 652 LOG_F(LS_INFO) << "Encoder is active.";
640 bitrate_allocator_->AddObserver( 653 bitrate_allocator_->AddObserver(
641 this, current_encoder_settings_->video_codec.minBitrate * 1000, 654 this, current_encoder_settings_->video_codec.minBitrate * 1000,
642 current_encoder_settings_->video_codec.maxBitrate * 1000, 655 current_encoder_settings_->video_codec.maxBitrate * 1000,
643 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, 656 CalulcateMaxPadBitrateBps(current_encoder_settings_->config,
644 config_.suspend_below_min_bitrate), 657 config_.suspend_below_min_bitrate),
645 !config_.suspend_below_min_bitrate); 658 !config_.suspend_below_min_bitrate);
646 send_stream_registered_as_observer_ = true; 659 state_ = kStarted;
660 }
661
662 VideoFrame frame;
663 if (input_.GetVideoFrame(&frame)) {
664 // TODO(perkj): |pre_encode_callback| is only used by tests. Tests should
665 // register as a sink to the VideoSource instead.
666 if (config_.pre_encode_callback) {
667 config_.pre_encode_callback->OnFrame(frame);
668 }
669 vie_encoder_.EncodeVideoFrame(frame);
647 } 670 }
648 } 671 }
649 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); 672 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type);
650 } 673 }
651 674
652 void VideoSendStream::ReconfigureVideoEncoder( 675 void VideoSendStream::ReconfigureVideoEncoder(
653 const VideoEncoderConfig& config) { 676 const VideoEncoderConfig& config) {
654 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); 677 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder");
655 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); 678 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString();
656 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); 679 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size());
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 } 865 }
843 866
844 void VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps, 867 void VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps,
845 uint8_t fraction_loss, 868 uint8_t fraction_loss,
846 int64_t rtt) { 869 int64_t rtt) {
847 payload_router_.SetTargetSendBitrate(bitrate_bps); 870 payload_router_.SetTargetSendBitrate(bitrate_bps);
848 // Get the encoder target rate. It is the estimated network rate - 871 // Get the encoder target rate. It is the estimated network rate -
849 // protection overhead. 872 // protection overhead.
850 uint32_t encoder_target_rate = protection_bitrate_calculator_.SetTargetRates( 873 uint32_t encoder_target_rate = protection_bitrate_calculator_.SetTargetRates(
851 bitrate_bps, stats_proxy_.GetSendFrameRate(), fraction_loss, rtt); 874 bitrate_bps, stats_proxy_.GetSendFrameRate(), fraction_loss, rtt);
852
853 vie_encoder_.OnBitrateUpdated(encoder_target_rate, fraction_loss, rtt); 875 vie_encoder_.OnBitrateUpdated(encoder_target_rate, fraction_loss, rtt);
854 } 876 }
855 877
856 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params, 878 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params,
857 const FecProtectionParams* key_params, 879 const FecProtectionParams* key_params,
858 uint32_t* sent_video_rate_bps, 880 uint32_t* sent_video_rate_bps,
859 uint32_t* sent_nack_rate_bps, 881 uint32_t* sent_nack_rate_bps,
860 uint32_t* sent_fec_rate_bps) { 882 uint32_t* sent_fec_rate_bps) {
861 *sent_video_rate_bps = 0; 883 *sent_video_rate_bps = 0;
862 *sent_nack_rate_bps = 0; 884 *sent_nack_rate_bps = 0;
863 *sent_fec_rate_bps = 0; 885 *sent_fec_rate_bps = 0;
864 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { 886 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
865 uint32_t not_used = 0; 887 uint32_t not_used = 0;
866 uint32_t module_video_rate = 0; 888 uint32_t module_video_rate = 0;
867 uint32_t module_fec_rate = 0; 889 uint32_t module_fec_rate = 0;
868 uint32_t module_nack_rate = 0; 890 uint32_t module_nack_rate = 0;
869 rtp_rtcp->SetFecParameters(delta_params, key_params); 891 rtp_rtcp->SetFecParameters(delta_params, key_params);
870 rtp_rtcp->BitrateSent(&not_used, &module_video_rate, &module_fec_rate, 892 rtp_rtcp->BitrateSent(&not_used, &module_video_rate, &module_fec_rate,
871 &module_nack_rate); 893 &module_nack_rate);
872 *sent_video_rate_bps += module_video_rate; 894 *sent_video_rate_bps += module_video_rate;
873 *sent_nack_rate_bps += module_nack_rate; 895 *sent_nack_rate_bps += module_nack_rate;
874 *sent_fec_rate_bps += module_fec_rate; 896 *sent_fec_rate_bps += module_fec_rate;
875 } 897 }
876 return 0; 898 return 0;
877 } 899 }
878 900
879 } // namespace internal 901 } // namespace internal
880 } // namespace webrtc 902 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698