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 | |
11 #include "webrtc/video/video_send_stream.h" | 10 #include "webrtc/video/video_send_stream.h" |
12 | 11 |
13 #include <algorithm> | 12 #include <algorithm> |
14 #include <sstream> | 13 #include <sstream> |
15 #include <string> | 14 #include <string> |
16 #include <utility> | 15 #include <utility> |
17 #include <vector> | 16 #include <vector> |
18 | 17 |
19 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
20 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/trace_event.h" | 20 #include "webrtc/base/trace_event.h" |
22 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 21 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
23 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 22 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
24 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 23 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
25 #include "webrtc/modules/pacing/packet_router.h" | 24 #include "webrtc/modules/pacing/packet_router.h" |
26 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 25 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
27 #include "webrtc/modules/utility/include/process_thread.h" | 26 #include "webrtc/modules/utility/include/process_thread.h" |
28 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" | 27 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" |
29 #include "webrtc/video/call_stats.h" | 28 #include "webrtc/video/call_stats.h" |
30 #include "webrtc/video/video_capture_input.h" | 29 #include "webrtc/video/video_capture_input.h" |
31 #include "webrtc/video/vie_remb.h" | 30 #include "webrtc/video/vie_remb.h" |
32 #include "webrtc/video_send_stream.h" | 31 #include "webrtc/video_send_stream.h" |
33 | 32 |
34 namespace webrtc { | 33 namespace webrtc { |
35 | 34 |
36 class RtcpIntraFrameObserver; | 35 class RtcpIntraFrameObserver; |
37 class TransportFeedbackObserver; | 36 class TransportFeedbackObserver; |
38 | 37 |
39 static const int kMinSendSidePacketHistorySize = 600; | 38 static const int kMinSendSidePacketHistorySize = 600; |
40 static const int kEncoderTimeOutMs = 2000; | |
41 | |
42 namespace { | 39 namespace { |
43 | 40 |
44 std::vector<RtpRtcp*> CreateRtpRtcpModules( | 41 std::vector<RtpRtcp*> CreateRtpRtcpModules( |
45 Transport* outgoing_transport, | 42 Transport* outgoing_transport, |
46 RtcpIntraFrameObserver* intra_frame_callback, | 43 RtcpIntraFrameObserver* intra_frame_callback, |
47 RtcpBandwidthObserver* bandwidth_callback, | 44 RtcpBandwidthObserver* bandwidth_callback, |
48 TransportFeedbackObserver* transport_feedback_callback, | 45 TransportFeedbackObserver* transport_feedback_callback, |
49 RtcpRttStats* rtt_stats, | 46 RtcpRttStats* rtt_stats, |
50 RtpPacketSender* paced_sender, | 47 RtpPacketSender* paced_sender, |
51 TransportSequenceNumberAllocator* transport_sequence_number_allocator, | 48 TransportSequenceNumberAllocator* transport_sequence_number_allocator, |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 361 |
365 pad_up_to_bitrate_bps = | 362 pad_up_to_bitrate_bps = |
366 std::max(pad_up_to_bitrate_bps, config.min_transmit_bitrate_bps); | 363 std::max(pad_up_to_bitrate_bps, config.min_transmit_bitrate_bps); |
367 | 364 |
368 return pad_up_to_bitrate_bps; | 365 return pad_up_to_bitrate_bps; |
369 } | 366 } |
370 | 367 |
371 } // namespace | 368 } // namespace |
372 | 369 |
373 namespace internal { | 370 namespace internal { |
| 371 |
| 372 class VideoSendStream::ConstructionTask : public rtc::QueuedTask { |
| 373 public: |
| 374 ConstructionTask(std::unique_ptr<VideoSendStreamInternal>* send_stream, |
| 375 rtc::Event* done_event, |
| 376 int num_cpu_cores, |
| 377 ProcessThread* module_process_thread, |
| 378 CallStats* call_stats, |
| 379 CongestionController* congestion_controller, |
| 380 BitrateAllocator* bitrate_allocator, |
| 381 SendDelayStats* send_delay_stats, |
| 382 VieRemb* remb, |
| 383 RtcEventLog* event_log, |
| 384 const VideoSendStream::Config& config, |
| 385 const VideoEncoderConfig& encoder_config, |
| 386 const std::map<uint32_t, RtpState>& suspended_ssrcs) |
| 387 : send_stream_(send_stream), |
| 388 done_event_(done_event), |
| 389 num_cpu_cores_(num_cpu_cores), |
| 390 call_stats_(call_stats), |
| 391 congestion_controller_(congestion_controller), |
| 392 bitrate_allocator_(bitrate_allocator), |
| 393 send_delay_stats_(send_delay_stats), |
| 394 remb_(remb), |
| 395 event_log_(event_log), |
| 396 config_(config), |
| 397 encoder_config_(encoder_config), |
| 398 suspended_ssrcs_(suspended_ssrcs) {} |
| 399 ~ConstructionTask() { done_event_->Set(); } |
| 400 |
| 401 bool Run() override { |
| 402 send_stream_->reset(new VideoSendStreamInternal( |
| 403 num_cpu_cores_, rtc::TaskQueue::Current(), call_stats_, |
| 404 congestion_controller_, bitrate_allocator_, send_delay_stats_, remb_, |
| 405 event_log_, config_, encoder_config_, suspended_ssrcs_)); |
| 406 return true; |
| 407 } |
| 408 |
| 409 private: |
| 410 std::unique_ptr<VideoSendStreamInternal>* send_stream_; |
| 411 rtc::Event* done_event_; |
| 412 const int num_cpu_cores_; |
| 413 CallStats* const call_stats_; |
| 414 CongestionController* const congestion_controller_; |
| 415 BitrateAllocator* const bitrate_allocator_; |
| 416 SendDelayStats* const send_delay_stats_; |
| 417 VieRemb* const remb_; |
| 418 RtcEventLog* const event_log_; |
| 419 const VideoSendStream::Config config_; |
| 420 const VideoEncoderConfig encoder_config_; |
| 421 const std::map<uint32_t, RtpState> suspended_ssrcs_; |
| 422 }; |
| 423 |
| 424 class VideoSendStream::DestructAndGetRTPStateTask : public rtc::QueuedTask { |
| 425 public: |
| 426 DestructAndGetRTPStateTask( |
| 427 VideoSendStream::RtpStateMap* state_map, |
| 428 std::unique_ptr<VideoSendStreamInternal> send_stream, |
| 429 rtc::Event* done_event) |
| 430 : state_map_(state_map), |
| 431 send_stream_(std::move(send_stream)), |
| 432 done_event_(done_event) {} |
| 433 ~DestructAndGetRTPStateTask() { |
| 434 send_stream_.reset(); |
| 435 done_event_->Set(); |
| 436 } |
| 437 |
| 438 bool Run() override { |
| 439 send_stream_->Stop(); |
| 440 *state_map_ = send_stream_->GetRtpStates(); |
| 441 send_stream_.reset(); |
| 442 return true; |
| 443 } |
| 444 |
| 445 private: |
| 446 VideoSendStream::RtpStateMap* state_map_; |
| 447 std::unique_ptr<VideoSendStreamInternal> send_stream_; |
| 448 rtc::Event* done_event_; |
| 449 }; |
| 450 |
| 451 // CheckEncoderActivityTask is used for tracking when the encoder last produced |
| 452 // and encoded video frame. This is used to |
| 453 // track when video is stopped long enough that we also want to stop sending |
| 454 // padding. |
| 455 class VideoSendStreamInternal::CheckEncoderActivityTask |
| 456 : public rtc::QueuedTask { |
| 457 public: |
| 458 static const int kEncoderTimeOutMs = 2000; |
| 459 explicit CheckEncoderActivityTask(VideoSendStreamInternal* send_stream) |
| 460 : activity_(0), send_stream_(send_stream), timed_out_(false) { |
| 461 encoder_thread_checker_.DetachFromThread(); |
| 462 } |
| 463 |
| 464 void Stop() { |
| 465 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 466 send_stream_ = nullptr; |
| 467 } |
| 468 |
| 469 void UpdateEncoderActivity() { |
| 470 RTC_DCHECK_RUN_ON(&encoder_thread_checker_); |
| 471 rtc::AtomicOps::ReleaseStore(&activity_, 1); |
| 472 } |
| 473 |
| 474 private: |
| 475 bool Run() override { |
| 476 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 477 if (!send_stream_) |
| 478 return true; |
| 479 if (!rtc::AtomicOps::AcquireLoad(&activity_)) { |
| 480 if (!timed_out_) { |
| 481 send_stream_->EncoderTimedOut(); |
| 482 } |
| 483 timed_out_ = true; |
| 484 } else if (timed_out_) { |
| 485 send_stream_->EncoderIsActive(); |
| 486 timed_out_ = false; |
| 487 } |
| 488 rtc::AtomicOps::ReleaseStore(&activity_, 0); |
| 489 |
| 490 rtc::TaskQueue::Current()->PostDelayedTask( |
| 491 std::unique_ptr<rtc::QueuedTask>(this), kEncoderTimeOutMs); |
| 492 // Return false to prevent this task from being deleted. Ownership has been |
| 493 // transferred to the task queue when PostDelayedTask was called. |
| 494 return false; |
| 495 } |
| 496 |
| 497 rtc::ThreadChecker encoder_thread_checker_; |
| 498 volatile int activity_; |
| 499 |
| 500 rtc::ThreadChecker thread_checker_; |
| 501 VideoSendStreamInternal* send_stream_; |
| 502 bool timed_out_; |
| 503 }; |
| 504 |
374 VideoSendStream::VideoSendStream( | 505 VideoSendStream::VideoSendStream( |
375 int num_cpu_cores, | 506 int num_cpu_cores, |
376 ProcessThread* module_process_thread, | 507 ProcessThread* module_process_thread, |
| 508 rtc::TaskQueue* worker_queu, |
377 CallStats* call_stats, | 509 CallStats* call_stats, |
378 CongestionController* congestion_controller, | 510 CongestionController* congestion_controller, |
379 BitrateAllocator* bitrate_allocator, | 511 BitrateAllocator* bitrate_allocator, |
| 512 SendDelayStats* send_delay_stats, |
| 513 VieRemb* remb, |
| 514 RtcEventLog* event_log, |
| 515 const VideoSendStream::Config& config, |
| 516 const VideoEncoderConfig& encoder_config, |
| 517 const std::map<uint32_t, RtpState>& suspended_ssrcs) |
| 518 : worker_queu_(worker_queu), |
| 519 thread_sync_event_(false /* manual_reset */, false) { |
| 520 worker_queu_->PostTask(std::unique_ptr<rtc::QueuedTask>(new ConstructionTask( |
| 521 &send_stream_, &thread_sync_event_, num_cpu_cores, module_process_thread, |
| 522 call_stats, congestion_controller, bitrate_allocator, send_delay_stats, |
| 523 remb, event_log, config, encoder_config, suspended_ssrcs))); |
| 524 |
| 525 // Wait for |construction_task| to complete so that |module_process_thread| |
| 526 // can be registered. |
| 527 thread_sync_event_.Wait(rtc::Event::kForever); |
| 528 // |send_stream_| can be null if |worker_queu_| is destroyed before the |
| 529 // ConstructionTask runs. |
| 530 if (send_stream_) |
| 531 send_stream_->RegisterProcessThread(module_process_thread); |
| 532 } |
| 533 |
| 534 VideoSendStream::~VideoSendStream() { |
| 535 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 536 LOG(LS_INFO) << "~VideoSendStream: "; |
| 537 RTC_DCHECK(!send_stream_); |
| 538 } |
| 539 |
| 540 void VideoSendStream::Start() { |
| 541 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 542 VideoSendStreamInternal* send_stream = send_stream_.get(); |
| 543 worker_queu_->PostTask([send_stream] { send_stream->Start(); }); |
| 544 } |
| 545 |
| 546 void VideoSendStream::Stop() { |
| 547 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 548 VideoSendStreamInternal* send_stream = send_stream_.get(); |
| 549 worker_queu_->PostTask([send_stream] { send_stream->Stop(); }); |
| 550 } |
| 551 |
| 552 VideoCaptureInput* VideoSendStream::Input() { |
| 553 return send_stream_->Input(); |
| 554 } |
| 555 |
| 556 void VideoSendStream::ReconfigureVideoEncoder( |
| 557 const VideoEncoderConfig& config) { |
| 558 // ReconfigureVideoEncoder can be called from multiple threads at the moment |
| 559 // in libjingle. |
| 560 VideoSendStreamInternal* send_stream = send_stream_.get(); |
| 561 worker_queu_->PostTask( |
| 562 [send_stream, config] { send_stream->ReconfigureVideoEncoder(config); }); |
| 563 } |
| 564 |
| 565 VideoSendStream::Stats VideoSendStream::GetStats() { |
| 566 return send_stream_->GetStats(); |
| 567 } |
| 568 |
| 569 void VideoSendStream::SignalNetworkState(NetworkState state) { |
| 570 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 571 VideoSendStreamInternal* send_stream = send_stream_.get(); |
| 572 worker_queu_->PostTask( |
| 573 [send_stream, state] { send_stream->SignalNetworkState(state); }); |
| 574 } |
| 575 |
| 576 VideoSendStream::RtpStateMap VideoSendStream::StopPermanentlyAndGetRtpStates() { |
| 577 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 578 VideoSendStream::RtpStateMap state_map; |
| 579 send_stream_->DeRegisterProcessThread(); |
| 580 worker_queu_->PostTask( |
| 581 std::unique_ptr<rtc::QueuedTask>(new DestructAndGetRTPStateTask( |
| 582 &state_map, std::move(send_stream_), &thread_sync_event_))); |
| 583 thread_sync_event_.Wait(rtc::Event::kForever); |
| 584 return state_map; |
| 585 } |
| 586 |
| 587 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 588 return send_stream_->DeliverRtcp(packet, length); |
| 589 } |
| 590 |
| 591 VideoSendStreamInternal::VideoSendStreamInternal( |
| 592 int num_cpu_cores, |
| 593 rtc::TaskQueue* worker_queu, |
| 594 CallStats* call_stats, |
| 595 CongestionController* congestion_controller, |
| 596 BitrateAllocator* bitrate_allocator, |
380 SendDelayStats* send_delay_stats, | 597 SendDelayStats* send_delay_stats, |
381 VieRemb* remb, | 598 VieRemb* remb, |
382 RtcEventLog* event_log, | 599 RtcEventLog* event_log, |
383 const VideoSendStream::Config& config, | 600 const VideoSendStream::Config& config, |
384 const VideoEncoderConfig& encoder_config, | 601 const VideoEncoderConfig& encoder_config, |
385 const std::map<uint32_t, RtpState>& suspended_ssrcs) | 602 const std::map<uint32_t, RtpState>& suspended_ssrcs) |
386 : stats_proxy_(Clock::GetRealTimeClock(), | 603 : stats_proxy_(Clock::GetRealTimeClock(), |
387 config, | 604 config, |
388 encoder_config.content_type), | 605 encoder_config.content_type), |
389 config_(config), | 606 config_(config), |
390 suspended_ssrcs_(suspended_ssrcs), | 607 suspended_ssrcs_(suspended_ssrcs), |
391 module_process_thread_(module_process_thread), | 608 module_process_thread_(nullptr), |
| 609 worker_queu_(worker_queu), |
| 610 check_encoder_activity_task_(nullptr), |
392 call_stats_(call_stats), | 611 call_stats_(call_stats), |
393 congestion_controller_(congestion_controller), | 612 congestion_controller_(congestion_controller), |
394 bitrate_allocator_(bitrate_allocator), | 613 bitrate_allocator_(bitrate_allocator), |
395 remb_(remb), | 614 remb_(remb), |
396 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), | 615 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), |
397 encoder_wakeup_event_(false, false), | 616 encoder_wakeup_event_(false, false), |
398 stop_encoder_thread_(0), | 617 stop_encoder_thread_(0), |
399 state_(State::kStopped), | 618 max_padding_bitrate_(0), |
| 619 encoder_target_rate_(0), |
400 overuse_detector_( | 620 overuse_detector_( |
401 Clock::GetRealTimeClock(), | 621 Clock::GetRealTimeClock(), |
402 GetCpuOveruseOptions(config.encoder_settings.full_overuse_time), | 622 GetCpuOveruseOptions(config.encoder_settings.full_overuse_time), |
403 this, | 623 this, |
404 config.post_encode_callback, | 624 config.post_encode_callback, |
405 &stats_proxy_), | 625 &stats_proxy_), |
406 vie_encoder_(num_cpu_cores, | 626 vie_encoder_(num_cpu_cores, &stats_proxy_, &overuse_detector_, this), |
407 module_process_thread_, | |
408 &stats_proxy_, | |
409 &overuse_detector_, | |
410 this), | |
411 encoder_feedback_(Clock::GetRealTimeClock(), | 627 encoder_feedback_(Clock::GetRealTimeClock(), |
412 config.rtp.ssrcs, | 628 config.rtp.ssrcs, |
413 &vie_encoder_), | 629 &vie_encoder_), |
414 protection_bitrate_calculator_(Clock::GetRealTimeClock(), this), | 630 protection_bitrate_calculator_(Clock::GetRealTimeClock(), this), |
415 video_sender_(vie_encoder_.video_sender()), | 631 video_sender_(vie_encoder_.video_sender()), |
416 bandwidth_observer_(congestion_controller_->GetBitrateController() | 632 bandwidth_observer_(congestion_controller_->GetBitrateController() |
417 ->CreateRtcpBandwidthObserver()), | 633 ->CreateRtcpBandwidthObserver()), |
418 rtp_rtcp_modules_(CreateRtpRtcpModules( | 634 rtp_rtcp_modules_(CreateRtpRtcpModules( |
419 config.send_transport, | 635 config.send_transport, |
420 &encoder_feedback_, | 636 &encoder_feedback_, |
421 bandwidth_observer_.get(), | 637 bandwidth_observer_.get(), |
422 congestion_controller_->GetTransportFeedbackObserver(), | 638 congestion_controller_->GetTransportFeedbackObserver(), |
423 call_stats_->rtcp_rtt_stats(), | 639 call_stats_->rtcp_rtt_stats(), |
424 congestion_controller_->pacer(), | 640 congestion_controller_->pacer(), |
425 congestion_controller_->packet_router(), | 641 congestion_controller_->packet_router(), |
426 &stats_proxy_, | 642 &stats_proxy_, |
427 send_delay_stats, | 643 send_delay_stats, |
428 event_log, | 644 event_log, |
429 config_.rtp.ssrcs.size())), | 645 config_.rtp.ssrcs.size())), |
430 payload_router_(rtp_rtcp_modules_, config.encoder_settings.payload_type), | 646 payload_router_(rtp_rtcp_modules_, config.encoder_settings.payload_type), |
431 input_(&encoder_wakeup_event_, | 647 input_(&encoder_wakeup_event_, |
432 config_.local_renderer, | 648 config_.local_renderer, |
433 &stats_proxy_, | 649 &stats_proxy_, |
434 &overuse_detector_) { | 650 &overuse_detector_) { |
435 LOG(LS_INFO) << "VideoSendStream: " << config_.ToString(); | 651 RTC_DCHECK_RUN_ON(worker_queu_); |
| 652 LOG(LS_INFO) << "VideoSendStreamInternal: " << config_.ToString(); |
| 653 module_process_thread_checker_.DetachFromThread(); |
436 | 654 |
437 RTC_DCHECK(!config_.rtp.ssrcs.empty()); | 655 RTC_DCHECK(!config_.rtp.ssrcs.empty()); |
438 RTC_DCHECK(module_process_thread_); | |
439 RTC_DCHECK(call_stats_); | 656 RTC_DCHECK(call_stats_); |
440 RTC_DCHECK(congestion_controller_); | 657 RTC_DCHECK(congestion_controller_); |
441 RTC_DCHECK(remb_); | 658 RTC_DCHECK(remb_); |
442 | 659 |
443 // RTP/RTCP initialization. | 660 // RTP/RTCP initialization. |
444 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 661 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
445 module_process_thread_->RegisterModule(rtp_rtcp); | |
446 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); | 662 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); |
447 } | 663 } |
448 | 664 |
449 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { | 665 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
450 const std::string& extension = config_.rtp.extensions[i].uri; | 666 const std::string& extension = config_.rtp.extensions[i].uri; |
451 int id = config_.rtp.extensions[i].id; | 667 int id = config_.rtp.extensions[i].id; |
452 // One-byte-extension local identifiers are in the range 1-14 inclusive. | 668 // One-byte-extension local identifiers are in the range 1-14 inclusive. |
453 RTC_DCHECK_GE(id, 1); | 669 RTC_DCHECK_GE(id, 1); |
454 RTC_DCHECK_LE(id, 14); | 670 RTC_DCHECK_LE(id, 14); |
455 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); | 671 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); |
(...skipping 23 matching lines...) Expand all Loading... |
479 rtp_rtcp->RegisterVideoSendPayload( | 695 rtp_rtcp->RegisterVideoSendPayload( |
480 config_.encoder_settings.payload_type, | 696 config_.encoder_settings.payload_type, |
481 config_.encoder_settings.payload_name.c_str()); | 697 config_.encoder_settings.payload_name.c_str()); |
482 } | 698 } |
483 | 699 |
484 RTC_DCHECK(config.encoder_settings.encoder); | 700 RTC_DCHECK(config.encoder_settings.encoder); |
485 RTC_DCHECK_GE(config.encoder_settings.payload_type, 0); | 701 RTC_DCHECK_GE(config.encoder_settings.payload_type, 0); |
486 RTC_DCHECK_LE(config.encoder_settings.payload_type, 127); | 702 RTC_DCHECK_LE(config.encoder_settings.payload_type, 127); |
487 ReconfigureVideoEncoder(encoder_config); | 703 ReconfigureVideoEncoder(encoder_config); |
488 | 704 |
489 module_process_thread_->RegisterModule(&overuse_detector_); | |
490 | |
491 encoder_thread_checker_.DetachFromThread(); | 705 encoder_thread_checker_.DetachFromThread(); |
492 encoder_thread_.Start(); | 706 encoder_thread_.Start(); |
493 encoder_thread_.SetPriority(rtc::kHighPriority); | 707 encoder_thread_.SetPriority(rtc::kHighPriority); |
494 } | 708 } |
495 | 709 |
496 VideoSendStream::~VideoSendStream() { | 710 void VideoSendStreamInternal::RegisterProcessThread( |
497 LOG(LS_INFO) << "~VideoSendStream: " << config_.ToString(); | 711 ProcessThread* module_process_thread) { |
| 712 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 713 RTC_DCHECK(!module_process_thread_); |
| 714 module_process_thread_ = module_process_thread; |
498 | 715 |
499 Stop(); | 716 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 717 module_process_thread_->RegisterModule(rtp_rtcp); |
| 718 } |
| 719 module_process_thread_->RegisterModule(&overuse_detector_); |
| 720 vie_encoder_.RegisterProcessThread(module_process_thread); |
| 721 } |
| 722 |
| 723 void VideoSendStreamInternal::DeRegisterProcessThread() { |
| 724 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 725 module_process_thread_->DeRegisterModule(&overuse_detector_); |
| 726 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 727 module_process_thread_->DeRegisterModule(rtp_rtcp); |
| 728 } |
| 729 vie_encoder_.DeRegisterProcessThread(); |
| 730 } |
| 731 |
| 732 VideoSendStreamInternal::~VideoSendStreamInternal() { |
| 733 RTC_DCHECK_RUN_ON(worker_queu_); |
| 734 LOG(LS_INFO) << "~VideoSendStreamInternal: " << config_.ToString(); |
500 | 735 |
501 // Stop the encoder thread permanently. | 736 // Stop the encoder thread permanently. |
502 rtc::AtomicOps::ReleaseStore(&stop_encoder_thread_, 1); | 737 rtc::AtomicOps::ReleaseStore(&stop_encoder_thread_, 1); |
503 encoder_wakeup_event_.Set(); | 738 encoder_wakeup_event_.Set(); |
504 encoder_thread_.Stop(); | 739 encoder_thread_.Stop(); |
505 | 740 |
506 // This needs to happen after stopping the encoder thread, | |
507 // since the encoder thread calls AddObserver. | |
508 bitrate_allocator_->RemoveObserver(this); | 741 bitrate_allocator_->RemoveObserver(this); |
509 | 742 |
510 module_process_thread_->DeRegisterModule(&overuse_detector_); | |
511 | |
512 rtp_rtcp_modules_[0]->SetREMBStatus(false); | 743 rtp_rtcp_modules_[0]->SetREMBStatus(false); |
513 remb_->RemoveRembSender(rtp_rtcp_modules_[0]); | 744 remb_->RemoveRembSender(rtp_rtcp_modules_[0]); |
514 | 745 |
515 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 746 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
516 congestion_controller_->packet_router()->RemoveRtpModule(rtp_rtcp); | 747 congestion_controller_->packet_router()->RemoveRtpModule(rtp_rtcp); |
517 module_process_thread_->DeRegisterModule(rtp_rtcp); | |
518 delete rtp_rtcp; | 748 delete rtp_rtcp; |
519 } | 749 } |
| 750 LOG(LS_INFO) << "~VideoSendStreamInternal: done"; |
520 } | 751 } |
521 | 752 |
522 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { | 753 bool VideoSendStreamInternal::DeliverRtcp(const uint8_t* packet, |
| 754 size_t length) { |
523 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) | 755 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) |
524 rtp_rtcp->IncomingRtcpPacket(packet, length); | 756 rtp_rtcp->IncomingRtcpPacket(packet, length); |
525 return true; | 757 return true; |
526 } | 758 } |
527 | 759 |
528 void VideoSendStream::Start() { | 760 void VideoSendStreamInternal::Start() { |
| 761 RTC_DCHECK_RUN_ON(worker_queu_); |
| 762 LOG_F(LS_INFO) << "Start"; |
529 if (payload_router_.active()) | 763 if (payload_router_.active()) |
530 return; | 764 return; |
531 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); | 765 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); |
532 payload_router_.set_active(true); | 766 payload_router_.set_active(true); |
| 767 |
| 768 // Add our self as bitrate observer. |
| 769 bitrate_allocator_->AddObserver(this, current_video_codec_.minBitrate * 1000, |
| 770 current_video_codec_.maxBitrate * 1000, |
| 771 max_padding_bitrate_, |
| 772 !config_.suspend_below_min_bitrate); |
| 773 |
| 774 // Start monitoring encoder activity. |
533 { | 775 { |
534 rtc::CritScope lock(&encoder_settings_crit_); | 776 rtc::CritScope lock(&encoder_settings_crit_); |
535 pending_state_change_ = rtc::Optional<State>(State::kStarted); | 777 RTC_DCHECK(!check_encoder_activity_task_); |
| 778 check_encoder_activity_task_ = new CheckEncoderActivityTask(this); |
| 779 worker_queu_->PostDelayedTask( |
| 780 std::unique_ptr<rtc::QueuedTask>(check_encoder_activity_task_), |
| 781 CheckEncoderActivityTask::kEncoderTimeOutMs); |
536 } | 782 } |
537 encoder_wakeup_event_.Set(); | 783 |
| 784 vie_encoder_.SendKeyFrame(); |
538 } | 785 } |
539 | 786 |
540 void VideoSendStream::Stop() { | 787 void VideoSendStreamInternal::Stop() { |
| 788 RTC_DCHECK_RUN_ON(worker_queu_); |
541 if (!payload_router_.active()) | 789 if (!payload_router_.active()) |
542 return; | 790 return; |
543 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); | 791 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); |
544 payload_router_.set_active(false); | 792 payload_router_.set_active(false); |
| 793 bitrate_allocator_->RemoveObserver(this); |
545 { | 794 { |
546 rtc::CritScope lock(&encoder_settings_crit_); | 795 rtc::CritScope lock(&encoder_settings_crit_); |
547 pending_state_change_ = rtc::Optional<State>(State::kStopped); | 796 check_encoder_activity_task_->Stop(); |
| 797 check_encoder_activity_task_ = nullptr; |
548 } | 798 } |
549 encoder_wakeup_event_.Set(); | 799 vie_encoder_.OnBitrateUpdated(0, 0, 0); |
550 } | 800 } |
551 | 801 |
552 VideoCaptureInput* VideoSendStream::Input() { | 802 VideoCaptureInput* VideoSendStreamInternal::Input() { |
553 return &input_; | 803 return &input_; |
554 } | 804 } |
555 | 805 |
556 bool VideoSendStream::EncoderThreadFunction(void* obj) { | 806 bool VideoSendStreamInternal::EncoderThreadFunction(void* obj) { |
557 static_cast<VideoSendStream*>(obj)->EncoderProcess(); | 807 static_cast<VideoSendStreamInternal*>(obj)->EncoderProcess(); |
558 // We're done, return false to abort. | 808 // We're done, return false to abort. |
559 return false; | 809 return false; |
560 } | 810 } |
561 | 811 |
562 void VideoSendStream::EncoderProcess() { | 812 void VideoSendStreamInternal::EncoderProcess() { |
563 RTC_CHECK_EQ(0, vie_encoder_.RegisterExternalEncoder( | 813 RTC_CHECK_EQ(0, vie_encoder_.RegisterExternalEncoder( |
564 config_.encoder_settings.encoder, | 814 config_.encoder_settings.encoder, |
565 config_.encoder_settings.payload_type, | 815 config_.encoder_settings.payload_type, |
566 config_.encoder_settings.internal_source)); | 816 config_.encoder_settings.internal_source)); |
567 RTC_DCHECK_RUN_ON(&encoder_thread_checker_); | 817 RTC_DCHECK_RUN_ON(&encoder_thread_checker_); |
568 while (true) { | 818 while (true) { |
569 // Wake up every kEncodeCheckForActivityPeriodMs to check if the encoder is | 819 encoder_wakeup_event_.Wait(rtc::Event::kForever); |
570 // active. If not, deregister as BitrateAllocatorObserver. | |
571 const int kEncodeCheckForActivityPeriodMs = 1000; | |
572 encoder_wakeup_event_.Wait(kEncodeCheckForActivityPeriodMs); | |
573 if (rtc::AtomicOps::AcquireLoad(&stop_encoder_thread_)) | 820 if (rtc::AtomicOps::AcquireLoad(&stop_encoder_thread_)) |
574 break; | 821 break; |
575 bool change_settings = false; | 822 std::unique_ptr<VideoCodec> pending_encoder_settings; |
576 rtc::Optional<State> pending_state_change; | |
577 { | 823 { |
578 rtc::CritScope lock(&encoder_settings_crit_); | 824 rtc::CritScope lock(&encoder_settings_crit_); |
579 if (pending_encoder_settings_) { | 825 std::swap(pending_encoder_settings_, pending_encoder_settings); |
580 std::swap(current_encoder_settings_, pending_encoder_settings_); | |
581 pending_encoder_settings_.reset(); | |
582 change_settings = true; | |
583 } else if (pending_state_change_) { | |
584 swap(pending_state_change, pending_state_change_); | |
585 } | |
586 } | 826 } |
587 if (change_settings) { | 827 if (pending_encoder_settings) { |
588 current_encoder_settings_->video_codec.startBitrate = std::max( | 828 vie_encoder_.SetEncoder(*pending_encoder_settings, |
589 bitrate_allocator_->GetStartBitrate(this) / 1000, | |
590 static_cast<int>(current_encoder_settings_->video_codec.minBitrate)); | |
591 payload_router_.SetSendStreams(current_encoder_settings_->config.streams); | |
592 vie_encoder_.SetEncoder(current_encoder_settings_->video_codec, | |
593 payload_router_.MaxPayloadLength()); | 829 payload_router_.MaxPayloadLength()); |
594 | 830 |
595 // Clear stats for disabled layers. | |
596 for (size_t i = current_encoder_settings_->config.streams.size(); | |
597 i < config_.rtp.ssrcs.size(); ++i) { | |
598 stats_proxy_.OnInactiveSsrc(config_.rtp.ssrcs[i]); | |
599 } | |
600 | |
601 size_t number_of_temporal_layers = | |
602 current_encoder_settings_->config.streams.back() | |
603 .temporal_layer_thresholds_bps.size() + | |
604 1; | |
605 protection_bitrate_calculator_.SetEncodingData( | |
606 current_encoder_settings_->video_codec.startBitrate * 1000, | |
607 current_encoder_settings_->video_codec.width, | |
608 current_encoder_settings_->video_codec.height, | |
609 current_encoder_settings_->video_codec.maxFramerate, | |
610 number_of_temporal_layers, payload_router_.MaxPayloadLength()); | |
611 | |
612 // We might've gotten new settings while configuring the encoder settings, | 831 // We might've gotten new settings while configuring the encoder settings, |
613 // restart from the top to see if that's the case before trying to encode | 832 // restart from the top to see if that's the case before trying to encode |
614 // a frame (which might correspond to the last frame size). | 833 // a frame (which might correspond to the last frame size). |
615 encoder_wakeup_event_.Set(); | 834 encoder_wakeup_event_.Set(); |
616 continue; | 835 continue; |
617 } | 836 } |
618 | 837 |
619 if (pending_state_change) { | |
620 if (*pending_state_change == State::kStarted && | |
621 state_ == State::kStopped) { | |
622 bitrate_allocator_->AddObserver( | |
623 this, current_encoder_settings_->video_codec.minBitrate * 1000, | |
624 current_encoder_settings_->video_codec.maxBitrate * 1000, | |
625 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, | |
626 config_.suspend_below_min_bitrate), | |
627 !config_.suspend_below_min_bitrate); | |
628 vie_encoder_.SendKeyFrame(); | |
629 state_ = State::kStarted; | |
630 LOG_F(LS_INFO) << "Encoder started."; | |
631 } else if (*pending_state_change == State::kStopped) { | |
632 bitrate_allocator_->RemoveObserver(this); | |
633 vie_encoder_.OnBitrateUpdated(0, 0, 0); | |
634 state_ = State::kStopped; | |
635 LOG_F(LS_INFO) << "Encoder stopped."; | |
636 } | |
637 encoder_wakeup_event_.Set(); | |
638 continue; | |
639 } | |
640 | |
641 // Check if the encoder has produced anything the last kEncoderTimeOutMs. | |
642 // If not, deregister as BitrateAllocatorObserver. | |
643 if (state_ == State::kStarted && | |
644 vie_encoder_.time_of_last_frame_activity_ms() < | |
645 rtc::TimeMillis() - kEncoderTimeOutMs) { | |
646 // The encoder has timed out. | |
647 LOG_F(LS_INFO) << "Encoder timed out."; | |
648 bitrate_allocator_->RemoveObserver(this); | |
649 state_ = State::kEncoderTimedOut; | |
650 } | |
651 if (state_ == State::kEncoderTimedOut && | |
652 vie_encoder_.time_of_last_frame_activity_ms() > | |
653 rtc::TimeMillis() - kEncoderTimeOutMs) { | |
654 LOG_F(LS_INFO) << "Encoder is active."; | |
655 bitrate_allocator_->AddObserver( | |
656 this, current_encoder_settings_->video_codec.minBitrate * 1000, | |
657 current_encoder_settings_->video_codec.maxBitrate * 1000, | |
658 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, | |
659 config_.suspend_below_min_bitrate), | |
660 !config_.suspend_below_min_bitrate); | |
661 state_ = State::kStarted; | |
662 } | |
663 | |
664 VideoFrame frame; | 838 VideoFrame frame; |
665 if (input_.GetVideoFrame(&frame)) { | 839 if (input_.GetVideoFrame(&frame)) { |
666 // TODO(perkj): |pre_encode_callback| is only used by tests. Tests should | 840 // TODO(perkj): |pre_encode_callback| is only used by tests. Tests should |
667 // register as a sink to the VideoSource instead. | 841 // register as a sink to the VideoSource instead. |
668 if (config_.pre_encode_callback) { | 842 if (config_.pre_encode_callback) { |
669 config_.pre_encode_callback->OnFrame(frame); | 843 config_.pre_encode_callback->OnFrame(frame); |
670 } | 844 } |
671 vie_encoder_.EncodeVideoFrame(frame); | 845 vie_encoder_.EncodeVideoFrame(frame); |
672 } | 846 } |
673 } | 847 } |
674 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); | 848 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); |
675 } | 849 } |
676 | 850 |
677 void VideoSendStream::ReconfigureVideoEncoder( | 851 void VideoSendStreamInternal::EncoderTimedOut() { |
| 852 RTC_DCHECK_RUN_ON(worker_queu_); |
| 853 // If the encoder has not produced anything the last kEncoderTimeOutMs and it |
| 854 // is supposed to, deregister as BitrateAllocatorObserver. This can happen |
| 855 // if a camera stop producing frames, temporary or permanently during a call. |
| 856 if (encoder_target_rate_ > 0) { |
| 857 LOG_F(LS_INFO) << "Encoder timed out."; |
| 858 bitrate_allocator_->RemoveObserver(this); |
| 859 } |
| 860 } |
| 861 |
| 862 void VideoSendStreamInternal::EncoderIsActive() { |
| 863 RTC_DCHECK_RUN_ON(worker_queu_); |
| 864 LOG_F(LS_INFO) << "Encoder is active."; |
| 865 bitrate_allocator_->AddObserver(this, current_video_codec_.minBitrate * 1000, |
| 866 current_video_codec_.maxBitrate * 1000, |
| 867 max_padding_bitrate_, |
| 868 !config_.suspend_below_min_bitrate); |
| 869 } |
| 870 |
| 871 void VideoSendStreamInternal::ReconfigureVideoEncoder( |
678 const VideoEncoderConfig& config) { | 872 const VideoEncoderConfig& config) { |
| 873 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); |
679 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); | 874 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); |
680 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); | 875 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); |
681 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); | 876 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); |
682 VideoCodec video_codec = VideoEncoderConfigToVideoCodec( | 877 RTC_DCHECK_RUN_ON(worker_queu_); |
| 878 |
| 879 current_video_codec_ = VideoEncoderConfigToVideoCodec( |
683 config, config_.encoder_settings.payload_name, | 880 config, config_.encoder_settings.payload_name, |
684 config_.encoder_settings.payload_type); | 881 config_.encoder_settings.payload_type); |
| 882 |
| 883 max_padding_bitrate_ = |
| 884 CalulcateMaxPadBitrateBps(config, config_.suspend_below_min_bitrate); |
| 885 current_video_codec_.startBitrate = |
| 886 bitrate_allocator_->GetStartBitrate(this) / 1000; |
| 887 |
| 888 payload_router_.SetSendStreams(config.streams); |
| 889 |
| 890 // Clear stats for disabled layers. |
| 891 for (size_t i = config.streams.size(); i < config_.rtp.ssrcs.size(); ++i) { |
| 892 stats_proxy_.OnInactiveSsrc(config_.rtp.ssrcs[i]); |
| 893 } |
| 894 |
| 895 size_t number_of_temporal_layers = |
| 896 config.streams.back().temporal_layer_thresholds_bps.size() + 1; |
| 897 protection_bitrate_calculator_.SetEncodingData( |
| 898 current_video_codec_.startBitrate * 1000, current_video_codec_.width, |
| 899 current_video_codec_.height, current_video_codec_.maxFramerate, |
| 900 number_of_temporal_layers, payload_router_.MaxPayloadLength()); |
| 901 |
685 { | 902 { |
686 rtc::CritScope lock(&encoder_settings_crit_); | 903 rtc::CritScope lock(&encoder_settings_crit_); |
687 pending_encoder_settings_.reset(new EncoderSettings({video_codec, config})); | 904 pending_encoder_settings_.reset(new VideoCodec(current_video_codec_)); |
688 } | 905 } |
689 encoder_wakeup_event_.Set(); | 906 encoder_wakeup_event_.Set(); |
690 } | 907 } |
691 | 908 |
692 VideoSendStream::Stats VideoSendStream::GetStats() { | 909 VideoSendStream::Stats VideoSendStreamInternal::GetStats() { |
693 return stats_proxy_.GetStats(); | 910 return stats_proxy_.GetStats(); |
694 } | 911 } |
695 | 912 |
696 void VideoSendStream::OveruseDetected() { | 913 void VideoSendStreamInternal::OveruseDetected() { |
697 if (config_.overuse_callback) | 914 if (config_.overuse_callback) |
698 config_.overuse_callback->OnLoadUpdate(LoadObserver::kOveruse); | 915 config_.overuse_callback->OnLoadUpdate(LoadObserver::kOveruse); |
699 } | 916 } |
700 | 917 |
701 void VideoSendStream::NormalUsage() { | 918 void VideoSendStreamInternal::NormalUsage() { |
702 if (config_.overuse_callback) | 919 if (config_.overuse_callback) |
703 config_.overuse_callback->OnLoadUpdate(LoadObserver::kUnderuse); | 920 config_.overuse_callback->OnLoadUpdate(LoadObserver::kUnderuse); |
704 } | 921 } |
705 | 922 |
706 int32_t VideoSendStream::Encoded(const EncodedImage& encoded_image, | 923 int32_t VideoSendStreamInternal::Encoded( |
707 const CodecSpecificInfo* codec_specific_info, | 924 const EncodedImage& encoded_image, |
708 const RTPFragmentationHeader* fragmentation) { | 925 const CodecSpecificInfo* codec_specific_info, |
| 926 const RTPFragmentationHeader* fragmentation) { |
| 927 { |
| 928 rtc::CritScope lock(&encoder_settings_crit_); |
| 929 if (check_encoder_activity_task_) |
| 930 check_encoder_activity_task_->UpdateEncoderActivity(); |
| 931 } |
709 if (config_.post_encode_callback) { | 932 if (config_.post_encode_callback) { |
710 config_.post_encode_callback->EncodedFrameCallback( | 933 config_.post_encode_callback->EncodedFrameCallback( |
711 EncodedFrame(encoded_image._buffer, encoded_image._length, | 934 EncodedFrame(encoded_image._buffer, encoded_image._length, |
712 encoded_image._frameType)); | 935 encoded_image._frameType)); |
713 } | 936 } |
714 | 937 |
715 protection_bitrate_calculator_.UpdateWithEncodedData(encoded_image); | 938 protection_bitrate_calculator_.UpdateWithEncodedData(encoded_image); |
716 int32_t return_value = payload_router_.Encoded( | 939 int32_t return_value = payload_router_.Encoded( |
717 encoded_image, codec_specific_info, fragmentation); | 940 encoded_image, codec_specific_info, fragmentation); |
718 | 941 |
(...skipping 16 matching lines...) Expand all Loading... |
735 } | 958 } |
736 if (file_writer) { | 959 if (file_writer) { |
737 bool ok = file_writer->WriteFrame(encoded_image); | 960 bool ok = file_writer->WriteFrame(encoded_image); |
738 RTC_DCHECK(ok); | 961 RTC_DCHECK(ok); |
739 } | 962 } |
740 } | 963 } |
741 | 964 |
742 return return_value; | 965 return return_value; |
743 } | 966 } |
744 | 967 |
745 void VideoSendStream::ConfigureProtection() { | 968 void VideoSendStreamInternal::ConfigureProtection() { |
746 // Enable NACK, FEC or both. | 969 // Enable NACK, FEC or both. |
747 const bool enable_protection_nack = config_.rtp.nack.rtp_history_ms > 0; | 970 const bool enable_protection_nack = config_.rtp.nack.rtp_history_ms > 0; |
748 bool enable_protection_fec = config_.rtp.fec.ulpfec_payload_type != -1; | 971 bool enable_protection_fec = config_.rtp.fec.ulpfec_payload_type != -1; |
749 // Payload types without picture ID cannot determine that a stream is complete | 972 // Payload types without picture ID cannot determine that a stream is complete |
750 // without retransmitting FEC, so using FEC + NACK for H.264 (for instance) is | 973 // without retransmitting FEC, so using FEC + NACK for H.264 (for instance) is |
751 // a waste of bandwidth since FEC packets still have to be transmitted. Note | 974 // a waste of bandwidth since FEC packets still have to be transmitted. Note |
752 // that this is not the case with FLEXFEC. | 975 // that this is not the case with FLEXFEC. |
753 if (enable_protection_nack && | 976 if (enable_protection_nack && |
754 !PayloadTypeSupportsSkippingFecPackets( | 977 !PayloadTypeSupportsSkippingFecPackets( |
755 config_.encoder_settings.payload_name)) { | 978 config_.encoder_settings.payload_name)) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 1015 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
793 rtp_rtcp->SetGenericFECStatus(enable_protection_fec, payload_type_red, | 1016 rtp_rtcp->SetGenericFECStatus(enable_protection_fec, payload_type_red, |
794 payload_type_fec); | 1017 payload_type_fec); |
795 } | 1018 } |
796 } | 1019 } |
797 | 1020 |
798 protection_bitrate_calculator_.SetProtectionMethod(enable_protection_fec, | 1021 protection_bitrate_calculator_.SetProtectionMethod(enable_protection_fec, |
799 enable_protection_nack); | 1022 enable_protection_nack); |
800 } | 1023 } |
801 | 1024 |
802 void VideoSendStream::ConfigureSsrcs() { | 1025 void VideoSendStreamInternal::ConfigureSsrcs() { |
803 // Configure regular SSRCs. | 1026 // Configure regular SSRCs. |
804 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { | 1027 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
805 uint32_t ssrc = config_.rtp.ssrcs[i]; | 1028 uint32_t ssrc = config_.rtp.ssrcs[i]; |
806 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; | 1029 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; |
807 rtp_rtcp->SetSSRC(ssrc); | 1030 rtp_rtcp->SetSSRC(ssrc); |
808 | 1031 |
809 // Restore RTP state if previous existed. | 1032 // Restore RTP state if previous existed. |
810 RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); | 1033 VideoSendStream::RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
811 if (it != suspended_ssrcs_.end()) | 1034 if (it != suspended_ssrcs_.end()) |
812 rtp_rtcp->SetRtpState(it->second); | 1035 rtp_rtcp->SetRtpState(it->second); |
813 } | 1036 } |
814 | 1037 |
815 // Set up RTX if available. | 1038 // Set up RTX if available. |
816 if (config_.rtp.rtx.ssrcs.empty()) | 1039 if (config_.rtp.rtx.ssrcs.empty()) |
817 return; | 1040 return; |
818 | 1041 |
819 // Configure RTX SSRCs. | 1042 // Configure RTX SSRCs. |
820 RTC_DCHECK_EQ(config_.rtp.rtx.ssrcs.size(), config_.rtp.ssrcs.size()); | 1043 RTC_DCHECK_EQ(config_.rtp.rtx.ssrcs.size(), config_.rtp.ssrcs.size()); |
821 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { | 1044 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { |
822 uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; | 1045 uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; |
823 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; | 1046 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; |
824 rtp_rtcp->SetRtxSsrc(ssrc); | 1047 rtp_rtcp->SetRtxSsrc(ssrc); |
825 RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); | 1048 VideoSendStream::RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
826 if (it != suspended_ssrcs_.end()) | 1049 if (it != suspended_ssrcs_.end()) |
827 rtp_rtcp->SetRtxState(it->second); | 1050 rtp_rtcp->SetRtxState(it->second); |
828 } | 1051 } |
829 | 1052 |
830 // Configure RTX payload types. | 1053 // Configure RTX payload types. |
831 RTC_DCHECK_GE(config_.rtp.rtx.payload_type, 0); | 1054 RTC_DCHECK_GE(config_.rtp.rtx.payload_type, 0); |
832 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 1055 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
833 rtp_rtcp->SetRtxSendPayloadType(config_.rtp.rtx.payload_type, | 1056 rtp_rtcp->SetRtxSendPayloadType(config_.rtp.rtx.payload_type, |
834 config_.encoder_settings.payload_type); | 1057 config_.encoder_settings.payload_type); |
835 rtp_rtcp->SetRtxSendStatus(kRtxRetransmitted | kRtxRedundantPayloads); | 1058 rtp_rtcp->SetRtxSendStatus(kRtxRetransmitted | kRtxRedundantPayloads); |
836 } | 1059 } |
837 if (config_.rtp.fec.red_payload_type != -1 && | 1060 if (config_.rtp.fec.red_payload_type != -1 && |
838 config_.rtp.fec.red_rtx_payload_type != -1) { | 1061 config_.rtp.fec.red_rtx_payload_type != -1) { |
839 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 1062 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
840 rtp_rtcp->SetRtxSendPayloadType(config_.rtp.fec.red_rtx_payload_type, | 1063 rtp_rtcp->SetRtxSendPayloadType(config_.rtp.fec.red_rtx_payload_type, |
841 config_.rtp.fec.red_payload_type); | 1064 config_.rtp.fec.red_payload_type); |
842 } | 1065 } |
843 } | 1066 } |
844 } | 1067 } |
845 | 1068 |
846 std::map<uint32_t, RtpState> VideoSendStream::GetRtpStates() const { | 1069 std::map<uint32_t, RtpState> VideoSendStreamInternal::GetRtpStates() const { |
847 std::map<uint32_t, RtpState> rtp_states; | 1070 std::map<uint32_t, RtpState> rtp_states; |
848 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { | 1071 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
849 uint32_t ssrc = config_.rtp.ssrcs[i]; | 1072 uint32_t ssrc = config_.rtp.ssrcs[i]; |
850 RTC_DCHECK_EQ(ssrc, rtp_rtcp_modules_[i]->SSRC()); | 1073 RTC_DCHECK_EQ(ssrc, rtp_rtcp_modules_[i]->SSRC()); |
851 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtpState(); | 1074 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtpState(); |
852 } | 1075 } |
853 | 1076 |
854 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { | 1077 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { |
855 uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; | 1078 uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; |
856 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtxState(); | 1079 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtxState(); |
857 } | 1080 } |
858 | 1081 |
859 return rtp_states; | 1082 return rtp_states; |
860 } | 1083 } |
861 | 1084 |
862 void VideoSendStream::SignalNetworkState(NetworkState state) { | 1085 void VideoSendStreamInternal::SignalNetworkState(NetworkState state) { |
863 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 1086 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
864 rtp_rtcp->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode | 1087 rtp_rtcp->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode |
865 : RtcpMode::kOff); | 1088 : RtcpMode::kOff); |
866 } | 1089 } |
867 } | 1090 } |
868 | 1091 |
869 void VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps, | 1092 void VideoSendStreamInternal::OnBitrateUpdated(uint32_t bitrate_bps, |
870 uint8_t fraction_loss, | 1093 uint8_t fraction_loss, |
871 int64_t rtt) { | 1094 int64_t rtt) { |
| 1095 RTC_DCHECK_RUN_ON(worker_queu_); |
| 1096 LOG(LS_VERBOSE) << "OnBitrateUpdated, bitrate_bps " << bitrate_bps |
| 1097 << " fraction_loss " << static_cast<int>(fraction_loss) |
| 1098 << " rtt " << rtt; |
872 payload_router_.SetTargetSendBitrate(bitrate_bps); | 1099 payload_router_.SetTargetSendBitrate(bitrate_bps); |
873 // Get the encoder target rate. It is the estimated network rate - | 1100 // Get the encoder target rate. It is the estimated network rate - |
874 // protection overhead. | 1101 // protection overhead. |
875 uint32_t encoder_target_rate = protection_bitrate_calculator_.SetTargetRates( | 1102 encoder_target_rate_ = protection_bitrate_calculator_.SetTargetRates( |
876 bitrate_bps, stats_proxy_.GetSendFrameRate(), fraction_loss, rtt); | 1103 bitrate_bps, stats_proxy_.GetSendFrameRate(), fraction_loss, rtt); |
877 vie_encoder_.OnBitrateUpdated(encoder_target_rate, fraction_loss, rtt); | 1104 if (!payload_router_.active()) |
| 1105 return; // The send stream is currently not started. |
| 1106 vie_encoder_.OnBitrateUpdated(encoder_target_rate_, fraction_loss, rtt); |
878 } | 1107 } |
879 | 1108 |
880 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params, | 1109 int VideoSendStreamInternal::ProtectionRequest( |
881 const FecProtectionParams* key_params, | 1110 const FecProtectionParams* delta_params, |
882 uint32_t* sent_video_rate_bps, | 1111 const FecProtectionParams* key_params, |
883 uint32_t* sent_nack_rate_bps, | 1112 uint32_t* sent_video_rate_bps, |
884 uint32_t* sent_fec_rate_bps) { | 1113 uint32_t* sent_nack_rate_bps, |
| 1114 uint32_t* sent_fec_rate_bps) { |
885 *sent_video_rate_bps = 0; | 1115 *sent_video_rate_bps = 0; |
886 *sent_nack_rate_bps = 0; | 1116 *sent_nack_rate_bps = 0; |
887 *sent_fec_rate_bps = 0; | 1117 *sent_fec_rate_bps = 0; |
888 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 1118 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
889 uint32_t not_used = 0; | 1119 uint32_t not_used = 0; |
890 uint32_t module_video_rate = 0; | 1120 uint32_t module_video_rate = 0; |
891 uint32_t module_fec_rate = 0; | 1121 uint32_t module_fec_rate = 0; |
892 uint32_t module_nack_rate = 0; | 1122 uint32_t module_nack_rate = 0; |
893 rtp_rtcp->SetFecParameters(delta_params, key_params); | 1123 rtp_rtcp->SetFecParameters(delta_params, key_params); |
894 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, | 1124 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, |
895 &module_nack_rate); | 1125 &module_nack_rate); |
896 *sent_video_rate_bps += module_video_rate; | 1126 *sent_video_rate_bps += module_video_rate; |
897 *sent_nack_rate_bps += module_nack_rate; | 1127 *sent_nack_rate_bps += module_nack_rate; |
898 *sent_fec_rate_bps += module_fec_rate; | 1128 *sent_fec_rate_bps += module_fec_rate; |
899 } | 1129 } |
900 return 0; | 1130 return 0; |
901 } | 1131 } |
902 | 1132 |
903 } // namespace internal | 1133 } // namespace internal |
904 } // namespace webrtc | 1134 } // namespace webrtc |
OLD | NEW |