OLD | NEW |
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 Loading... |
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_(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 23 matching lines...) Expand all Loading... |
432 &stats_proxy_, | 432 &stats_proxy_, |
433 &overuse_detector_) { | 433 &overuse_detector_) { |
434 LOG(LS_INFO) << "VideoSendStream: " << config_.ToString(); | 434 LOG(LS_INFO) << "VideoSendStream: " << config_.ToString(); |
435 | 435 |
436 RTC_DCHECK(!config_.rtp.ssrcs.empty()); | 436 RTC_DCHECK(!config_.rtp.ssrcs.empty()); |
437 RTC_DCHECK(module_process_thread_); | 437 RTC_DCHECK(module_process_thread_); |
438 RTC_DCHECK(call_stats_); | 438 RTC_DCHECK(call_stats_); |
439 RTC_DCHECK(congestion_controller_); | 439 RTC_DCHECK(congestion_controller_); |
440 RTC_DCHECK(remb_); | 440 RTC_DCHECK(remb_); |
441 | 441 |
442 | |
443 // RTP/RTCP initialization. | 442 // RTP/RTCP initialization. |
444 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 443 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
445 module_process_thread_->RegisterModule(rtp_rtcp); | 444 module_process_thread_->RegisterModule(rtp_rtcp); |
446 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); | 445 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); |
447 } | 446 } |
448 | 447 |
449 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { | 448 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
450 const std::string& extension = config_.rtp.extensions[i].uri; | 449 const std::string& extension = config_.rtp.extensions[i].uri; |
451 int id = config_.rtp.extensions[i].id; | 450 int id = config_.rtp.extensions[i].id; |
452 // One-byte-extension local identifiers are in the range 1-14 inclusive. | 451 // One-byte-extension local identifiers are in the range 1-14 inclusive. |
(...skipping 28 matching lines...) Expand all Loading... |
481 config_.encoder_settings.payload_name.c_str()); | 480 config_.encoder_settings.payload_name.c_str()); |
482 } | 481 } |
483 | 482 |
484 RTC_DCHECK(config.encoder_settings.encoder); | 483 RTC_DCHECK(config.encoder_settings.encoder); |
485 RTC_DCHECK_GE(config.encoder_settings.payload_type, 0); | 484 RTC_DCHECK_GE(config.encoder_settings.payload_type, 0); |
486 RTC_DCHECK_LE(config.encoder_settings.payload_type, 127); | 485 RTC_DCHECK_LE(config.encoder_settings.payload_type, 127); |
487 ReconfigureVideoEncoder(encoder_config); | 486 ReconfigureVideoEncoder(encoder_config); |
488 | 487 |
489 module_process_thread_->RegisterModule(&overuse_detector_); | 488 module_process_thread_->RegisterModule(&overuse_detector_); |
490 | 489 |
| 490 encoder_thread_checker_.DetachFromThread(); |
491 encoder_thread_.Start(); | 491 encoder_thread_.Start(); |
492 encoder_thread_.SetPriority(rtc::kHighPriority); | 492 encoder_thread_.SetPriority(rtc::kHighPriority); |
493 } | 493 } |
494 | 494 |
495 VideoSendStream::~VideoSendStream() { | 495 VideoSendStream::~VideoSendStream() { |
496 LOG(LS_INFO) << "~VideoSendStream: " << config_.ToString(); | 496 LOG(LS_INFO) << "~VideoSendStream: " << config_.ToString(); |
497 | 497 |
498 Stop(); | 498 Stop(); |
499 | 499 |
500 // Stop the encoder thread permanently. | 500 // Stop the encoder thread permanently. |
(...skipping 21 matching lines...) Expand all Loading... |
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<State>(State::kStarted); |
| 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<State>(State::kStopped); |
| 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 RTC_DCHECK_RUN_ON(&encoder_thread_checker_); |
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<State> 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 == State::kStarted && |
618 // TODO(perkj): |pre_encode_callback| is only used by tests. Tests should | 620 state_ == State::kStopped) { |
619 // register as a sink to the VideoSource instead. | 621 bitrate_allocator_->AddObserver( |
620 if (config_.pre_encode_callback) { | 622 this, current_encoder_settings_->video_codec.minBitrate * 1000, |
621 config_.pre_encode_callback->OnFrame(frame); | 623 current_encoder_settings_->video_codec.maxBitrate * 1000, |
| 624 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, |
| 625 config_.suspend_below_min_bitrate), |
| 626 !config_.suspend_below_min_bitrate); |
| 627 vie_encoder_.SendKeyFrame(); |
| 628 state_ = State::kStarted; |
| 629 LOG_F(LS_INFO) << "Encoder started."; |
| 630 } else if (*pending_state_change == State::kStopped) { |
| 631 bitrate_allocator_->RemoveObserver(this); |
| 632 vie_encoder_.OnBitrateUpdated(0, 0, 0); |
| 633 state_ = State::kStopped; |
| 634 LOG_F(LS_INFO) << "Encoder stopped."; |
622 } | 635 } |
623 vie_encoder_.EncodeVideoFrame(frame); | 636 encoder_wakeup_event_.Set(); |
| 637 continue; |
624 } | 638 } |
625 | 639 |
626 // Check if the encoder has produced anything the last kEncoderTimeOutMs. | 640 // Check if the encoder has produced anything the last kEncoderTimeOutMs. |
627 // If not, deregister as BitrateAllocatorObserver. | 641 // If not, deregister as BitrateAllocatorObserver. |
628 if (send_stream_registered_as_observer_ && | 642 if (state_ == State::kStarted && |
629 vie_encoder_.time_of_last_frame_activity_ms() < | 643 vie_encoder_.time_of_last_frame_activity_ms() < |
630 rtc::TimeMillis() - kEncoderTimeOutMs) { | 644 rtc::TimeMillis() - kEncoderTimeOutMs) { |
631 // The encoder has timed out. | 645 // The encoder has timed out. |
632 LOG_F(LS_INFO) << "Encoder timed out."; | 646 LOG_F(LS_INFO) << "Encoder timed out."; |
633 bitrate_allocator_->RemoveObserver(this); | 647 bitrate_allocator_->RemoveObserver(this); |
634 send_stream_registered_as_observer_ = false; | 648 state_ = State::kEncoderTimedOut; |
635 } | 649 } |
636 if (!send_stream_registered_as_observer_ && | 650 if (state_ == State::kEncoderTimedOut && |
637 vie_encoder_.time_of_last_frame_activity_ms() > | 651 vie_encoder_.time_of_last_frame_activity_ms() > |
638 rtc::TimeMillis() - kEncoderTimeOutMs) { | 652 rtc::TimeMillis() - kEncoderTimeOutMs) { |
639 LOG_F(LS_INFO) << "Encoder is active."; | 653 LOG_F(LS_INFO) << "Encoder is active."; |
640 bitrate_allocator_->AddObserver( | 654 bitrate_allocator_->AddObserver( |
641 this, current_encoder_settings_->video_codec.minBitrate * 1000, | 655 this, current_encoder_settings_->video_codec.minBitrate * 1000, |
642 current_encoder_settings_->video_codec.maxBitrate * 1000, | 656 current_encoder_settings_->video_codec.maxBitrate * 1000, |
643 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, | 657 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, |
644 config_.suspend_below_min_bitrate), | 658 config_.suspend_below_min_bitrate), |
645 !config_.suspend_below_min_bitrate); | 659 !config_.suspend_below_min_bitrate); |
646 send_stream_registered_as_observer_ = true; | 660 state_ = State::kStarted; |
| 661 } |
| 662 |
| 663 VideoFrame frame; |
| 664 if (input_.GetVideoFrame(&frame)) { |
| 665 // TODO(perkj): |pre_encode_callback| is only used by tests. Tests should |
| 666 // register as a sink to the VideoSource instead. |
| 667 if (config_.pre_encode_callback) { |
| 668 config_.pre_encode_callback->OnFrame(frame); |
| 669 } |
| 670 vie_encoder_.EncodeVideoFrame(frame); |
647 } | 671 } |
648 } | 672 } |
649 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); | 673 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); |
650 } | 674 } |
651 | 675 |
652 void VideoSendStream::ReconfigureVideoEncoder( | 676 void VideoSendStream::ReconfigureVideoEncoder( |
653 const VideoEncoderConfig& config) { | 677 const VideoEncoderConfig& config) { |
654 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); | 678 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); |
655 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); | 679 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); |
656 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); | 680 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 } | 866 } |
843 | 867 |
844 void VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps, | 868 void VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
845 uint8_t fraction_loss, | 869 uint8_t fraction_loss, |
846 int64_t rtt) { | 870 int64_t rtt) { |
847 payload_router_.SetTargetSendBitrate(bitrate_bps); | 871 payload_router_.SetTargetSendBitrate(bitrate_bps); |
848 // Get the encoder target rate. It is the estimated network rate - | 872 // Get the encoder target rate. It is the estimated network rate - |
849 // protection overhead. | 873 // protection overhead. |
850 uint32_t encoder_target_rate = protection_bitrate_calculator_.SetTargetRates( | 874 uint32_t encoder_target_rate = protection_bitrate_calculator_.SetTargetRates( |
851 bitrate_bps, stats_proxy_.GetSendFrameRate(), fraction_loss, rtt); | 875 bitrate_bps, stats_proxy_.GetSendFrameRate(), fraction_loss, rtt); |
852 | |
853 vie_encoder_.OnBitrateUpdated(encoder_target_rate, fraction_loss, rtt); | 876 vie_encoder_.OnBitrateUpdated(encoder_target_rate, fraction_loss, rtt); |
854 } | 877 } |
855 | 878 |
856 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params, | 879 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params, |
857 const FecProtectionParams* key_params, | 880 const FecProtectionParams* key_params, |
858 uint32_t* sent_video_rate_bps, | 881 uint32_t* sent_video_rate_bps, |
859 uint32_t* sent_nack_rate_bps, | 882 uint32_t* sent_nack_rate_bps, |
860 uint32_t* sent_fec_rate_bps) { | 883 uint32_t* sent_fec_rate_bps) { |
861 *sent_video_rate_bps = 0; | 884 *sent_video_rate_bps = 0; |
862 *sent_nack_rate_bps = 0; | 885 *sent_nack_rate_bps = 0; |
863 *sent_fec_rate_bps = 0; | 886 *sent_fec_rate_bps = 0; |
864 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 887 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
865 uint32_t not_used = 0; | 888 uint32_t not_used = 0; |
866 uint32_t module_video_rate = 0; | 889 uint32_t module_video_rate = 0; |
867 uint32_t module_fec_rate = 0; | 890 uint32_t module_fec_rate = 0; |
868 uint32_t module_nack_rate = 0; | 891 uint32_t module_nack_rate = 0; |
869 rtp_rtcp->SetFecParameters(delta_params, key_params); | 892 rtp_rtcp->SetFecParameters(delta_params, key_params); |
870 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, | 893 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, |
871 &module_nack_rate); | 894 &module_nack_rate); |
872 *sent_video_rate_bps += module_video_rate; | 895 *sent_video_rate_bps += module_video_rate; |
873 *sent_nack_rate_bps += module_nack_rate; | 896 *sent_nack_rate_bps += module_nack_rate; |
874 *sent_fec_rate_bps += module_fec_rate; | 897 *sent_fec_rate_bps += module_fec_rate; |
875 } | 898 } |
876 return 0; | 899 return 0; |
877 } | 900 } |
878 | 901 |
879 } // namespace internal | 902 } // namespace internal |
880 } // namespace webrtc | 903 } // namespace webrtc |
OLD | NEW |