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 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 remb_(remb), | 365 remb_(remb), |
366 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), | 366 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), |
367 encoder_wakeup_event_(false, false), | 367 encoder_wakeup_event_(false, false), |
368 stop_encoder_thread_(0), | 368 stop_encoder_thread_(0), |
369 overuse_detector_( | 369 overuse_detector_( |
370 Clock::GetRealTimeClock(), | 370 Clock::GetRealTimeClock(), |
371 GetCpuOveruseOptions(config.encoder_settings.full_overuse_time), | 371 GetCpuOveruseOptions(config.encoder_settings.full_overuse_time), |
372 this, | 372 this, |
373 config.post_encode_callback, | 373 config.post_encode_callback, |
374 &stats_proxy_), | 374 &stats_proxy_), |
375 vie_encoder_( | 375 vie_encoder_(num_cpu_cores, |
376 num_cpu_cores, | 376 config_.rtp.ssrcs, |
377 config_.rtp.ssrcs, | 377 module_process_thread_, |
378 module_process_thread_, | 378 &stats_proxy_, |
379 &stats_proxy_, | 379 config.pre_encode_callback, |
380 config.pre_encode_callback, | 380 &overuse_detector_, |
381 &overuse_detector_, | 381 congestion_controller_->pacer()), |
382 congestion_controller_->pacer(), | |
383 &payload_router_, | |
384 config.post_encode_callback ? &encoded_frame_proxy_ : nullptr), | |
385 video_sender_(vie_encoder_.video_sender()), | 382 video_sender_(vie_encoder_.video_sender()), |
386 bandwidth_observer_(congestion_controller_->GetBitrateController() | 383 bandwidth_observer_(congestion_controller_->GetBitrateController() |
387 ->CreateRtcpBandwidthObserver()), | 384 ->CreateRtcpBandwidthObserver()), |
388 rtp_rtcp_modules_(CreateRtpRtcpModules( | 385 rtp_rtcp_modules_(CreateRtpRtcpModules( |
389 config.send_transport, | 386 config.send_transport, |
390 &encoder_feedback_, | 387 &encoder_feedback_, |
391 bandwidth_observer_.get(), | 388 bandwidth_observer_.get(), |
392 congestion_controller_->GetTransportFeedbackObserver(), | 389 congestion_controller_->GetTransportFeedbackObserver(), |
393 call_stats_->rtcp_rtt_stats(), | 390 call_stats_->rtcp_rtt_stats(), |
394 congestion_controller_->pacer(), | 391 congestion_controller_->pacer(), |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { | 506 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
510 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) | 507 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) |
511 rtp_rtcp->IncomingRtcpPacket(packet, length); | 508 rtp_rtcp->IncomingRtcpPacket(packet, length); |
512 return true; | 509 return true; |
513 } | 510 } |
514 | 511 |
515 void VideoSendStream::Start() { | 512 void VideoSendStream::Start() { |
516 if (payload_router_.active()) | 513 if (payload_router_.active()) |
517 return; | 514 return; |
518 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); | 515 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); |
519 vie_encoder_.Pause(); | |
520 payload_router_.set_active(true); | 516 payload_router_.set_active(true); |
521 // Was not already started, trigger a keyframe. | 517 // Was not already started, trigger a keyframe. |
522 vie_encoder_.SendKeyFrame(); | 518 vie_encoder_.SendKeyFrame(); |
523 vie_encoder_.Restart(); | 519 vie_encoder_.Start(); |
524 } | 520 } |
525 | 521 |
526 void VideoSendStream::Stop() { | 522 void VideoSendStream::Stop() { |
527 if (!payload_router_.active()) | 523 if (!payload_router_.active()) |
528 return; | 524 return; |
529 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); | 525 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); |
530 // TODO(pbos): Make sure the encoder stops here. | 526 vie_encoder_.Pause(); |
531 payload_router_.set_active(false); | 527 payload_router_.set_active(false); |
532 } | 528 } |
533 | 529 |
534 VideoCaptureInput* VideoSendStream::Input() { | 530 VideoCaptureInput* VideoSendStream::Input() { |
535 return &input_; | 531 return &input_; |
536 } | 532 } |
537 | 533 |
538 bool VideoSendStream::EncoderThreadFunction(void* obj) { | 534 bool VideoSendStream::EncoderThreadFunction(void* obj) { |
539 static_cast<VideoSendStream*>(obj)->EncoderProcess(); | 535 static_cast<VideoSendStream*>(obj)->EncoderProcess(); |
540 // We're done, return false to abort. | 536 // We're done, return false to abort. |
(...skipping 17 matching lines...) Expand all Loading... |
558 encoder_settings = pending_encoder_settings_; | 554 encoder_settings = pending_encoder_settings_; |
559 pending_encoder_settings_ = rtc::Optional<EncoderSettings>(); | 555 pending_encoder_settings_ = rtc::Optional<EncoderSettings>(); |
560 } | 556 } |
561 } | 557 } |
562 if (encoder_settings) { | 558 if (encoder_settings) { |
563 encoder_settings->video_codec.startBitrate = | 559 encoder_settings->video_codec.startBitrate = |
564 bitrate_allocator_->AddObserver( | 560 bitrate_allocator_->AddObserver( |
565 this, encoder_settings->video_codec.minBitrate * 1000, | 561 this, encoder_settings->video_codec.minBitrate * 1000, |
566 encoder_settings->video_codec.maxBitrate * 1000) / | 562 encoder_settings->video_codec.maxBitrate * 1000) / |
567 1000; | 563 1000; |
| 564 |
| 565 payload_router_.SetSendStreams(encoder_settings->streams); |
568 vie_encoder_.SetEncoder(encoder_settings->video_codec, | 566 vie_encoder_.SetEncoder(encoder_settings->video_codec, |
569 encoder_settings->min_transmit_bitrate_bps); | 567 encoder_settings->min_transmit_bitrate_bps, |
| 568 payload_router_.MaxPayloadLength(), this); |
570 if (config_.suspend_below_min_bitrate) { | 569 if (config_.suspend_below_min_bitrate) { |
571 video_sender_->SuspendBelowMinBitrate(); | 570 video_sender_->SuspendBelowMinBitrate(); |
572 bitrate_allocator_->EnforceMinBitrate(false); | 571 bitrate_allocator_->EnforceMinBitrate(false); |
573 } | 572 } |
574 // We might've gotten new settings while configuring the encoder settings, | 573 // We might've gotten new settings while configuring the encoder settings, |
575 // restart from the top to see if that's the case before trying to encode | 574 // restart from the top to see if that's the case before trying to encode |
576 // a frame (which might correspond to the last frame size). | 575 // a frame (which might correspond to the last frame size). |
577 encoder_wakeup_event_.Set(); | 576 encoder_wakeup_event_.Set(); |
578 continue; | 577 continue; |
579 } | 578 } |
580 | 579 |
581 VideoFrame frame; | 580 VideoFrame frame; |
582 if (input_.GetVideoFrame(&frame)) | 581 if (input_.GetVideoFrame(&frame)) |
583 vie_encoder_.EncodeVideoFrame(frame); | 582 vie_encoder_.EncodeVideoFrame(frame); |
584 } | 583 } |
585 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); | 584 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); |
586 } | 585 } |
587 | 586 |
588 void VideoSendStream::ReconfigureVideoEncoder( | 587 void VideoSendStream::ReconfigureVideoEncoder( |
589 const VideoEncoderConfig& config) { | 588 const VideoEncoderConfig& config) { |
590 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); | 589 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); |
591 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); | 590 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); |
592 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); | 591 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); |
593 VideoCodec video_codec = VideoEncoderConfigToVideoCodec( | 592 VideoCodec video_codec = VideoEncoderConfigToVideoCodec( |
594 config, config_.encoder_settings.payload_name, | 593 config, config_.encoder_settings.payload_name, |
595 config_.encoder_settings.payload_type); | 594 config_.encoder_settings.payload_type); |
596 { | 595 { |
597 rtc::CritScope lock(&encoder_settings_crit_); | 596 rtc::CritScope lock(&encoder_settings_crit_); |
598 pending_encoder_settings_ = rtc::Optional<EncoderSettings>( | 597 pending_encoder_settings_ = rtc::Optional<EncoderSettings>( |
599 {video_codec, config.min_transmit_bitrate_bps}); | 598 {video_codec, config.min_transmit_bitrate_bps, config.streams}); |
600 } | 599 } |
601 encoder_wakeup_event_.Set(); | 600 encoder_wakeup_event_.Set(); |
602 } | 601 } |
603 | 602 |
604 VideoSendStream::Stats VideoSendStream::GetStats() { | 603 VideoSendStream::Stats VideoSendStream::GetStats() { |
605 return stats_proxy_.GetStats(); | 604 return stats_proxy_.GetStats(); |
606 } | 605 } |
607 | 606 |
608 void VideoSendStream::OveruseDetected() { | 607 void VideoSendStream::OveruseDetected() { |
609 if (config_.overuse_callback) | 608 if (config_.overuse_callback) |
610 config_.overuse_callback->OnLoadUpdate(LoadObserver::kOveruse); | 609 config_.overuse_callback->OnLoadUpdate(LoadObserver::kOveruse); |
611 } | 610 } |
612 | 611 |
613 void VideoSendStream::NormalUsage() { | 612 void VideoSendStream::NormalUsage() { |
614 if (config_.overuse_callback) | 613 if (config_.overuse_callback) |
615 config_.overuse_callback->OnLoadUpdate(LoadObserver::kUnderuse); | 614 config_.overuse_callback->OnLoadUpdate(LoadObserver::kUnderuse); |
616 } | 615 } |
617 | 616 |
| 617 int32_t VideoSendStream::Encoded(const EncodedImage& encoded_image, |
| 618 const CodecSpecificInfo* codec_specific_info, |
| 619 const RTPFragmentationHeader* fragmentation) { |
| 620 // |encoded_frame_proxy_| forwards frames to |config_.post_encode_callback|; |
| 621 encoded_frame_proxy_.Encoded(encoded_image, codec_specific_info, |
| 622 fragmentation); |
| 623 return payload_router_.Encoded(encoded_image, codec_specific_info, |
| 624 fragmentation); |
| 625 } |
| 626 |
618 void VideoSendStream::ConfigureProtection() { | 627 void VideoSendStream::ConfigureProtection() { |
619 // Enable NACK, FEC or both. | 628 // Enable NACK, FEC or both. |
620 const bool enable_protection_nack = config_.rtp.nack.rtp_history_ms > 0; | 629 const bool enable_protection_nack = config_.rtp.nack.rtp_history_ms > 0; |
621 bool enable_protection_fec = config_.rtp.fec.red_payload_type != -1; | 630 bool enable_protection_fec = config_.rtp.fec.red_payload_type != -1; |
622 // Payload types without picture ID cannot determine that a stream is complete | 631 // Payload types without picture ID cannot determine that a stream is complete |
623 // without retransmitting FEC, so using FEC + NACK for H.264 (for instance) is | 632 // without retransmitting FEC, so using FEC + NACK for H.264 (for instance) is |
624 // a waste of bandwidth since FEC packets still have to be transmitted. Note | 633 // a waste of bandwidth since FEC packets still have to be transmitted. Note |
625 // that this is not the case with FLEXFEC. | 634 // that this is not the case with FLEXFEC. |
626 if (enable_protection_nack && | 635 if (enable_protection_nack && |
627 !PayloadTypeSupportsSkippingFecPackets( | 636 !PayloadTypeSupportsSkippingFecPackets( |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 return rtp_states; | 738 return rtp_states; |
730 } | 739 } |
731 | 740 |
732 int VideoSendStream::GetPaddingNeededBps() const { | 741 int VideoSendStream::GetPaddingNeededBps() const { |
733 return vie_encoder_.GetPaddingNeededBps(); | 742 return vie_encoder_.GetPaddingNeededBps(); |
734 } | 743 } |
735 | 744 |
736 void VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps, | 745 void VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
737 uint8_t fraction_loss, | 746 uint8_t fraction_loss, |
738 int64_t rtt) { | 747 int64_t rtt) { |
| 748 payload_router_.SetTargetSendBitrate(bitrate_bps); |
739 vie_encoder_.OnBitrateUpdated(bitrate_bps, fraction_loss, rtt); | 749 vie_encoder_.OnBitrateUpdated(bitrate_bps, fraction_loss, rtt); |
740 } | 750 } |
741 | 751 |
742 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params, | 752 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params, |
743 const FecProtectionParams* key_params, | 753 const FecProtectionParams* key_params, |
744 uint32_t* sent_video_rate_bps, | 754 uint32_t* sent_video_rate_bps, |
745 uint32_t* sent_nack_rate_bps, | 755 uint32_t* sent_nack_rate_bps, |
746 uint32_t* sent_fec_rate_bps) { | 756 uint32_t* sent_fec_rate_bps) { |
747 *sent_video_rate_bps = 0; | 757 *sent_video_rate_bps = 0; |
748 *sent_nack_rate_bps = 0; | 758 *sent_nack_rate_bps = 0; |
749 *sent_fec_rate_bps = 0; | 759 *sent_fec_rate_bps = 0; |
750 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 760 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
751 uint32_t not_used = 0; | 761 uint32_t not_used = 0; |
752 uint32_t module_video_rate = 0; | 762 uint32_t module_video_rate = 0; |
753 uint32_t module_fec_rate = 0; | 763 uint32_t module_fec_rate = 0; |
754 uint32_t module_nack_rate = 0; | 764 uint32_t module_nack_rate = 0; |
755 rtp_rtcp->SetFecParameters(delta_params, key_params); | 765 rtp_rtcp->SetFecParameters(delta_params, key_params); |
756 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, | 766 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, |
757 &module_nack_rate); | 767 &module_nack_rate); |
758 *sent_video_rate_bps += module_video_rate; | 768 *sent_video_rate_bps += module_video_rate; |
759 *sent_nack_rate_bps += module_nack_rate; | 769 *sent_nack_rate_bps += module_nack_rate; |
760 *sent_fec_rate_bps += module_fec_rate; | 770 *sent_fec_rate_bps += module_fec_rate; |
761 } | 771 } |
762 return 0; | 772 return 0; |
763 } | 773 } |
764 | 774 |
765 } // namespace internal | 775 } // namespace internal |
766 } // namespace webrtc | 776 } // namespace webrtc |
OLD | NEW |