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