LCOV - code coverage report
Current view: top level - media/webrtc/trunk/webrtc/call - call.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 603 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 37 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
       3             :  *
       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
       6             :  *  tree. An additional intellectual property rights grant can be found
       7             :  *  in the file PATENTS.  All contributing project authors may
       8             :  *  be found in the AUTHORS file in the root of the source tree.
       9             :  */
      10             : 
      11             : #include <string.h>
      12             : #include <algorithm>
      13             : #include <map>
      14             : #include <memory>
      15             : #include <set>
      16             : #include <utility>
      17             : #include <vector>
      18             : 
      19             : #include "webrtc/audio/audio_receive_stream.h"
      20             : #include "webrtc/audio/audio_send_stream.h"
      21             : #include "webrtc/audio/audio_state.h"
      22             : #include "webrtc/audio/scoped_voe_interface.h"
      23             : #include "webrtc/base/basictypes.h"
      24             : #include "webrtc/base/checks.h"
      25             : #include "webrtc/base/constructormagic.h"
      26             : #include "webrtc/base/logging.h"
      27             : #include "webrtc/base/optional.h"
      28             : #include "webrtc/base/task_queue.h"
      29             : #include "webrtc/base/thread_annotations.h"
      30             : #include "webrtc/base/thread_checker.h"
      31             : #include "webrtc/base/trace_event.h"
      32             : #include "webrtc/call/bitrate_allocator.h"
      33             : #include "webrtc/call/call.h"
      34             : #include "webrtc/call/flexfec_receive_stream_impl.h"
      35             : #include "webrtc/config.h"
      36             : #include "webrtc/logging/rtc_event_log/rtc_event_log.h"
      37             : #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
      38             : #include "webrtc/modules/congestion_controller/include/congestion_controller.h"
      39             : #include "webrtc/modules/pacing/paced_sender.h"
      40             : #include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h"
      41             : #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
      42             : #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
      43             : #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
      44             : #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
      45             : #include "webrtc/modules/utility/include/process_thread.h"
      46             : #include "webrtc/system_wrappers/include/clock.h"
      47             : #include "webrtc/system_wrappers/include/cpu_info.h"
      48             : #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
      49             : #include "webrtc/system_wrappers/include/metrics.h"
      50             : #include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
      51             : #include "webrtc/system_wrappers/include/trace.h"
      52             : #include "webrtc/video/call_stats.h"
      53             : #include "webrtc/video/send_delay_stats.h"
      54             : #include "webrtc/video/stats_counter.h"
      55             : #include "webrtc/video/video_receive_stream.h"
      56             : #include "webrtc/video/video_send_stream.h"
      57             : #include "webrtc/video/vie_remb.h"
      58             : #include "webrtc/voice_engine/include/voe_codec.h"
      59             : 
      60             : namespace webrtc {
      61             : 
      62             : const int Call::Config::kDefaultStartBitrateBps = 300000;
      63             : 
      64             : namespace internal {
      65             : 
      66             : class Call : public webrtc::Call,
      67             :              public PacketReceiver,
      68             :              public RecoveredPacketReceiver,
      69             :              public CongestionController::Observer,
      70             :              public BitrateAllocator::LimitObserver {
      71             :  public:
      72             :   explicit Call(const Call::Config& config);
      73             :   virtual ~Call();
      74             : 
      75             :   // Implements webrtc::Call.
      76             :   PacketReceiver* Receiver() override;
      77             : 
      78             :   webrtc::AudioSendStream* CreateAudioSendStream(
      79             :       const webrtc::AudioSendStream::Config& config) override;
      80             :   void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
      81             : 
      82             :   webrtc::AudioReceiveStream* CreateAudioReceiveStream(
      83             :       const webrtc::AudioReceiveStream::Config& config) override;
      84             :   void DestroyAudioReceiveStream(
      85             :       webrtc::AudioReceiveStream* receive_stream) override;
      86             : 
      87             :   webrtc::VideoSendStream* CreateVideoSendStream(
      88             :       webrtc::VideoSendStream::Config config,
      89             :       VideoEncoderConfig encoder_config) override;
      90             :   void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
      91             : 
      92             :   webrtc::VideoReceiveStream* CreateVideoReceiveStream(
      93             :       webrtc::VideoReceiveStream::Config configuration) override;
      94             :   void DestroyVideoReceiveStream(
      95             :       webrtc::VideoReceiveStream* receive_stream) override;
      96             : 
      97             :   FlexfecReceiveStream* CreateFlexfecReceiveStream(
      98             :       const FlexfecReceiveStream::Config& config) override;
      99             :   void DestroyFlexfecReceiveStream(
     100             :       FlexfecReceiveStream* receive_stream) override;
     101             : 
     102             :   Stats GetStats() const override;
     103             : 
     104             :   // Implements PacketReceiver.
     105             :   DeliveryStatus DeliverPacket(MediaType media_type,
     106             :                                const uint8_t* packet,
     107             :                                size_t length,
     108             :                                const PacketTime& packet_time) override;
     109             : 
     110             :   // Implements RecoveredPacketReceiver.
     111             :   bool OnRecoveredPacket(const uint8_t* packet, size_t length) override;
     112             : 
     113             :   void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet);
     114             : 
     115             :   void SetBitrateConfig(
     116             :       const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
     117             : 
     118             :   void SignalChannelNetworkState(MediaType media, NetworkState state) override;
     119             : 
     120             :   void OnTransportOverheadChanged(MediaType media,
     121             :                                   int transport_overhead_per_packet) override;
     122             : 
     123             :   void OnNetworkRouteChanged(const std::string& transport_name,
     124             :                              const rtc::NetworkRoute& network_route) override;
     125             : 
     126             :   void OnSentPacket(const rtc::SentPacket& sent_packet) override;
     127             : 
     128             : 
     129             :   // TODO(minyue): remove this when old OnNetworkChanged is deprecated. See
     130             :   // https://bugs.chromium.org/p/webrtc/issues/detail?id=6796
     131             :   using CongestionController::Observer::OnNetworkChanged;
     132             : 
     133             :   // Implements BitrateObserver.
     134             :   void OnNetworkChanged(uint32_t bitrate_bps,
     135             :                         uint8_t fraction_loss,
     136             :                         int64_t rtt_ms,
     137             :                         int64_t probing_interval_ms) override;
     138             : 
     139             :   // Implements BitrateAllocator::LimitObserver.
     140             :   void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
     141             :                                  uint32_t max_padding_bitrate_bps) override;
     142             : 
     143           0 :   VoiceEngine* voice_engine() override {
     144             :     internal::AudioState* audio_state =
     145           0 :         static_cast<internal::AudioState*>(config_.audio_state.get());
     146           0 :     if (audio_state)
     147           0 :       return audio_state->voice_engine();
     148             :     else
     149           0 :       return nullptr;
     150             :   }
     151             : 
     152             :  private:
     153             :   DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
     154             :                              size_t length);
     155             :   DeliveryStatus DeliverRtp(MediaType media_type,
     156             :                             const uint8_t* packet,
     157             :                             size_t length,
     158             :                             const PacketTime& packet_time);
     159             :   void ConfigureSync(const std::string& sync_group)
     160             :       EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
     161             : 
     162             :   rtc::Optional<RtpPacketReceived> ParseRtpPacket(const uint8_t* packet,
     163             :                                                   size_t length,
     164             :                                                   const PacketTime& packet_time)
     165             :       SHARED_LOCKS_REQUIRED(receive_crit_);
     166             : 
     167             :   void UpdateSendHistograms() EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
     168             :   void UpdateReceiveHistograms();
     169             :   void UpdateHistograms();
     170             :   void UpdateAggregateNetworkState();
     171             : 
     172             :   Clock* const clock_;
     173             : 
     174             :   const int num_cpu_cores_;
     175             :   const std::unique_ptr<ProcessThread> module_process_thread_;
     176             :   const std::unique_ptr<ProcessThread> pacer_thread_;
     177             :   const std::unique_ptr<CallStats> call_stats_;
     178             :   const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
     179             :   Call::Config config_;
     180             :   rtc::ThreadChecker configuration_thread_checker_;
     181             : 
     182             :   NetworkState audio_network_state_;
     183             :   NetworkState video_network_state_;
     184             : 
     185             :   std::unique_ptr<RWLockWrapper> receive_crit_;
     186             :   // Audio, Video, and FlexFEC receive streams are owned by the client that
     187             :   // creates them.
     188             :   std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
     189             :       GUARDED_BY(receive_crit_);
     190             :   std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
     191             :       GUARDED_BY(receive_crit_);
     192             :   std::set<VideoReceiveStream*> video_receive_streams_
     193             :       GUARDED_BY(receive_crit_);
     194             :   // Each media stream could conceivably be protected by multiple FlexFEC
     195             :   // streams.
     196             :   std::multimap<uint32_t, FlexfecReceiveStreamImpl*>
     197             :       flexfec_receive_ssrcs_media_ GUARDED_BY(receive_crit_);
     198             :   std::map<uint32_t, FlexfecReceiveStreamImpl*>
     199             :       flexfec_receive_ssrcs_protection_ GUARDED_BY(receive_crit_);
     200             :   std::set<FlexfecReceiveStreamImpl*> flexfec_receive_streams_
     201             :       GUARDED_BY(receive_crit_);
     202             :   std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
     203             :       GUARDED_BY(receive_crit_);
     204             : 
     205             :   // Registered RTP header extensions for each stream.
     206             :   // Note that RTP header extensions are negotiated per track ("m= line") in the
     207             :   // SDP, but we have no notion of tracks at the Call level. We therefore store
     208             :   // the RTP header extensions per SSRC instead, which leads to some storage
     209             :   // overhead.
     210             :   std::map<uint32_t, RtpHeaderExtensionMap> received_rtp_header_extensions_
     211             :       GUARDED_BY(receive_crit_);
     212             : 
     213             :   std::unique_ptr<RWLockWrapper> send_crit_;
     214             :   // Audio and Video send streams are owned by the client that creates them.
     215             :   std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
     216             :   std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
     217             :   std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
     218             : 
     219             :   VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
     220             :   webrtc::RtcEventLog* event_log_;
     221             : 
     222             :   // The following members are only accessed (exclusively) from one thread and
     223             :   // from the destructor, and therefore doesn't need any explicit
     224             :   // synchronization.
     225             :   int64_t first_packet_sent_ms_;
     226             :   RateCounter received_bytes_per_second_counter_;
     227             :   RateCounter received_audio_bytes_per_second_counter_;
     228             :   RateCounter received_video_bytes_per_second_counter_;
     229             :   RateCounter received_rtcp_bytes_per_second_counter_;
     230             : 
     231             :   // TODO(holmer): Remove this lock once BitrateController no longer calls
     232             :   // OnNetworkChanged from multiple threads.
     233             :   rtc::CriticalSection bitrate_crit_;
     234             :   uint32_t min_allocated_send_bitrate_bps_ GUARDED_BY(&bitrate_crit_);
     235             :   uint32_t configured_max_padding_bitrate_bps_ GUARDED_BY(&bitrate_crit_);
     236             :   AvgCounter estimated_send_bitrate_kbps_counter_ GUARDED_BY(&bitrate_crit_);
     237             :   AvgCounter pacer_bitrate_kbps_counter_ GUARDED_BY(&bitrate_crit_);
     238             : 
     239             :   std::map<std::string, rtc::NetworkRoute> network_routes_;
     240             : 
     241             :   VieRemb remb_;
     242             :   PacketRouter packet_router_;
     243             :   // TODO(nisse): Could be a direct member, except for constness
     244             :   // issues with GetRemoteBitrateEstimator (and maybe others).
     245             :   const std::unique_ptr<CongestionController> congestion_controller_;
     246             :   const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
     247             :   const int64_t start_ms_;
     248             :   // TODO(perkj): |worker_queue_| is supposed to replace
     249             :   // |module_process_thread_|.
     250             :   // |worker_queue| is defined last to ensure all pending tasks are cancelled
     251             :   // and deleted before any other members.
     252             :   rtc::TaskQueue worker_queue_;
     253             : 
     254             :   RTC_DISALLOW_COPY_AND_ASSIGN(Call);
     255             : };
     256             : }  // namespace internal
     257             : 
     258           0 : std::string Call::Stats::ToString(int64_t time_ms) const {
     259           0 :   std::stringstream ss;
     260           0 :   ss << "Call stats: " << time_ms << ", {";
     261           0 :   ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
     262           0 :   ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
     263           0 :   ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
     264           0 :   ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
     265           0 :   ss << "rtt_ms: " << rtt_ms;
     266           0 :   ss << '}';
     267           0 :   return ss.str();
     268             : }
     269             : 
     270           0 : Call* Call::Create(const Call::Config& config) {
     271           0 :   return new internal::Call(config);
     272             : }
     273             : 
     274             : namespace internal {
     275             : 
     276           0 : Call::Call(const Call::Config& config)
     277           0 :     : clock_(Clock::GetRealTimeClock()),
     278           0 :       num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
     279             :       module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
     280             :       pacer_thread_(ProcessThread::Create("PacerThread")),
     281           0 :       call_stats_(new CallStats(clock_)),
     282           0 :       bitrate_allocator_(new BitrateAllocator(this)),
     283             :       config_(config),
     284             :       audio_network_state_(kNetworkDown),
     285             :       video_network_state_(kNetworkDown),
     286             :       receive_crit_(RWLockWrapper::CreateRWLock()),
     287             :       send_crit_(RWLockWrapper::CreateRWLock()),
     288           0 :       event_log_(config.event_log),
     289             :       first_packet_sent_ms_(-1),
     290           0 :       received_bytes_per_second_counter_(clock_, nullptr, true),
     291           0 :       received_audio_bytes_per_second_counter_(clock_, nullptr, true),
     292           0 :       received_video_bytes_per_second_counter_(clock_, nullptr, true),
     293           0 :       received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
     294             :       min_allocated_send_bitrate_bps_(0),
     295             :       configured_max_padding_bitrate_bps_(0),
     296           0 :       estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
     297           0 :       pacer_bitrate_kbps_counter_(clock_, nullptr, true),
     298           0 :       remb_(clock_),
     299           0 :       congestion_controller_(new CongestionController(clock_,
     300             :                                                       this,
     301             :                                                       &remb_,
     302             :                                                       event_log_,
     303           0 :                                                       &packet_router_)),
     304           0 :       video_send_delay_stats_(new SendDelayStats(clock_)),
     305           0 :       start_ms_(clock_->TimeInMilliseconds()),
     306           0 :       worker_queue_("call_worker_queue") {
     307           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     308           0 :   RTC_DCHECK(config.event_log != nullptr);
     309           0 :   RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
     310           0 :   RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
     311           0 :                 config.bitrate_config.min_bitrate_bps);
     312           0 :   if (config.bitrate_config.max_bitrate_bps != -1) {
     313           0 :     RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
     314           0 :                   config.bitrate_config.start_bitrate_bps);
     315             :   }
     316           0 :   Trace::CreateTrace();
     317           0 :   call_stats_->RegisterStatsObserver(congestion_controller_.get());
     318             : 
     319           0 :   congestion_controller_->SignalNetworkState(kNetworkDown);
     320           0 :   congestion_controller_->SetBweBitrates(
     321             :       config_.bitrate_config.min_bitrate_bps,
     322             :       config_.bitrate_config.start_bitrate_bps,
     323           0 :       config_.bitrate_config.max_bitrate_bps);
     324             : 
     325           0 :   module_process_thread_->Start();
     326           0 :   module_process_thread_->RegisterModule(call_stats_.get());
     327           0 :   module_process_thread_->RegisterModule(congestion_controller_.get());
     328           0 :   pacer_thread_->RegisterModule(congestion_controller_->pacer());
     329           0 :   pacer_thread_->RegisterModule(
     330           0 :       congestion_controller_->GetRemoteBitrateEstimator(true));
     331           0 :   pacer_thread_->Start();
     332           0 : }
     333             : 
     334           0 : Call::~Call() {
     335           0 :   RTC_DCHECK(!remb_.InUse());
     336           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     337             : 
     338           0 :   RTC_CHECK(audio_send_ssrcs_.empty());
     339           0 :   RTC_CHECK(video_send_ssrcs_.empty());
     340           0 :   RTC_CHECK(video_send_streams_.empty());
     341           0 :   RTC_CHECK(audio_receive_ssrcs_.empty());
     342           0 :   RTC_CHECK(video_receive_ssrcs_.empty());
     343           0 :   RTC_CHECK(video_receive_streams_.empty());
     344             : 
     345           0 :   pacer_thread_->Stop();
     346           0 :   pacer_thread_->DeRegisterModule(congestion_controller_->pacer());
     347           0 :   pacer_thread_->DeRegisterModule(
     348           0 :       congestion_controller_->GetRemoteBitrateEstimator(true));
     349           0 :   module_process_thread_->DeRegisterModule(congestion_controller_.get());
     350           0 :   module_process_thread_->DeRegisterModule(call_stats_.get());
     351           0 :   module_process_thread_->Stop();
     352           0 :   call_stats_->DeregisterStatsObserver(congestion_controller_.get());
     353             : 
     354             :   // Only update histograms after process threads have been shut down, so that
     355             :   // they won't try to concurrently update stats.
     356             :   {
     357           0 :     rtc::CritScope lock(&bitrate_crit_);
     358           0 :     UpdateSendHistograms();
     359             :   }
     360           0 :   UpdateReceiveHistograms();
     361           0 :   UpdateHistograms();
     362             : 
     363           0 :   Trace::ReturnTrace();
     364           0 : }
     365             : 
     366           0 : rtc::Optional<RtpPacketReceived> Call::ParseRtpPacket(
     367             :     const uint8_t* packet,
     368             :     size_t length,
     369             :     const PacketTime& packet_time) {
     370           0 :   RtpPacketReceived parsed_packet;
     371           0 :   if (!parsed_packet.Parse(packet, length))
     372           0 :     return rtc::Optional<RtpPacketReceived>();
     373             : 
     374           0 :   auto it = received_rtp_header_extensions_.find(parsed_packet.Ssrc());
     375           0 :   if (it != received_rtp_header_extensions_.end())
     376           0 :     parsed_packet.IdentifyExtensions(it->second);
     377             : 
     378             :   int64_t arrival_time_ms;
     379           0 :   if (packet_time.timestamp != -1) {
     380           0 :     arrival_time_ms = (packet_time.timestamp + 500) / 1000;
     381             :   } else {
     382           0 :     arrival_time_ms = clock_->TimeInMilliseconds();
     383             :   }
     384           0 :   parsed_packet.set_arrival_time_ms(arrival_time_ms);
     385             : 
     386           0 :   return rtc::Optional<RtpPacketReceived>(std::move(parsed_packet));
     387             : }
     388             : 
     389           0 : void Call::UpdateHistograms() {
     390           0 :   RTC_HISTOGRAM_COUNTS_100000(
     391             :       "WebRTC.Call.LifetimeInSeconds",
     392             :       (clock_->TimeInMilliseconds() - start_ms_) / 1000);
     393           0 : }
     394             : 
     395           0 : void Call::UpdateSendHistograms() {
     396           0 :   if (first_packet_sent_ms_ == -1)
     397           0 :     return;
     398             :   int64_t elapsed_sec =
     399           0 :       (clock_->TimeInMilliseconds() - first_packet_sent_ms_) / 1000;
     400           0 :   if (elapsed_sec < metrics::kMinRunTimeInSeconds)
     401           0 :     return;
     402           0 :   const int kMinRequiredPeriodicSamples = 5;
     403             :   AggregatedStats send_bitrate_stats =
     404           0 :       estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
     405           0 :   if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
     406           0 :     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
     407             :                                 send_bitrate_stats.average);
     408           0 :     LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
     409           0 :                  << send_bitrate_stats.ToString();
     410             :   }
     411             :   AggregatedStats pacer_bitrate_stats =
     412           0 :       pacer_bitrate_kbps_counter_.ProcessAndGetStats();
     413           0 :   if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
     414           0 :     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
     415             :                                 pacer_bitrate_stats.average);
     416           0 :     LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
     417           0 :                  << pacer_bitrate_stats.ToString();
     418             :   }
     419             : }
     420             : 
     421           0 : void Call::UpdateReceiveHistograms() {
     422           0 :   const int kMinRequiredPeriodicSamples = 5;
     423             :   AggregatedStats video_bytes_per_sec =
     424           0 :       received_video_bytes_per_second_counter_.GetStats();
     425           0 :   if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
     426           0 :     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
     427             :                                 video_bytes_per_sec.average * 8 / 1000);
     428           0 :     LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
     429           0 :                  << video_bytes_per_sec.ToStringWithMultiplier(8);
     430             :   }
     431             :   AggregatedStats audio_bytes_per_sec =
     432           0 :       received_audio_bytes_per_second_counter_.GetStats();
     433           0 :   if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
     434           0 :     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
     435             :                                 audio_bytes_per_sec.average * 8 / 1000);
     436           0 :     LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
     437           0 :                  << audio_bytes_per_sec.ToStringWithMultiplier(8);
     438             :   }
     439             :   AggregatedStats rtcp_bytes_per_sec =
     440           0 :       received_rtcp_bytes_per_second_counter_.GetStats();
     441           0 :   if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
     442           0 :     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
     443             :                                 rtcp_bytes_per_sec.average * 8);
     444           0 :     LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
     445           0 :                  << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
     446             :   }
     447             :   AggregatedStats recv_bytes_per_sec =
     448           0 :       received_bytes_per_second_counter_.GetStats();
     449           0 :   if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
     450           0 :     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
     451             :                                 recv_bytes_per_sec.average * 8 / 1000);
     452           0 :     LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
     453           0 :                  << recv_bytes_per_sec.ToStringWithMultiplier(8);
     454             :   }
     455           0 : }
     456             : 
     457           0 : PacketReceiver* Call::Receiver() {
     458             :   // TODO(solenberg): Some test cases in EndToEndTest use this from a different
     459             :   // thread. Re-enable once that is fixed.
     460             :   // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     461           0 :   return this;
     462             : }
     463             : 
     464           0 : webrtc::AudioSendStream* Call::CreateAudioSendStream(
     465             :     const webrtc::AudioSendStream::Config& config) {
     466           0 :   TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
     467           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     468           0 :   event_log_->LogAudioSendStreamConfig(config);
     469             :   AudioSendStream* send_stream = new AudioSendStream(
     470             :       config, config_.audio_state, &worker_queue_, &packet_router_,
     471           0 :       congestion_controller_.get(), bitrate_allocator_.get(), event_log_,
     472           0 :       call_stats_->rtcp_rtt_stats());
     473             :   {
     474           0 :     WriteLockScoped write_lock(*send_crit_);
     475           0 :     RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
     476           0 :                audio_send_ssrcs_.end());
     477           0 :     audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
     478             :   }
     479             :   {
     480           0 :     ReadLockScoped read_lock(*receive_crit_);
     481           0 :     for (const auto& kv : audio_receive_ssrcs_) {
     482           0 :       if (kv.second->config().rtp.local_ssrc == config.rtp.ssrc) {
     483           0 :         kv.second->AssociateSendStream(send_stream);
     484             :       }
     485             :     }
     486             :   }
     487           0 :   send_stream->SignalNetworkState(audio_network_state_);
     488           0 :   UpdateAggregateNetworkState();
     489           0 :   return send_stream;
     490             : }
     491             : 
     492           0 : void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
     493           0 :   TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
     494           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     495           0 :   RTC_DCHECK(send_stream != nullptr);
     496             : 
     497           0 :   send_stream->Stop();
     498             : 
     499             :   webrtc::internal::AudioSendStream* audio_send_stream =
     500           0 :       static_cast<webrtc::internal::AudioSendStream*>(send_stream);
     501           0 :   uint32_t ssrc = audio_send_stream->config().rtp.ssrc;
     502             :   {
     503           0 :     WriteLockScoped write_lock(*send_crit_);
     504           0 :     size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
     505           0 :     RTC_DCHECK_EQ(1, num_deleted);
     506             :   }
     507             :   {
     508           0 :     ReadLockScoped read_lock(*receive_crit_);
     509           0 :     for (const auto& kv : audio_receive_ssrcs_) {
     510           0 :       if (kv.second->config().rtp.local_ssrc == ssrc) {
     511           0 :         kv.second->AssociateSendStream(nullptr);
     512             :       }
     513             :     }
     514             :   }
     515           0 :   UpdateAggregateNetworkState();
     516           0 :   delete audio_send_stream;
     517           0 : }
     518             : 
     519           0 : webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
     520             :     const webrtc::AudioReceiveStream::Config& config) {
     521           0 :   TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
     522           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     523           0 :   event_log_->LogAudioReceiveStreamConfig(config);
     524             :   AudioReceiveStream* receive_stream = new AudioReceiveStream(
     525             :       &packet_router_,
     526             :       // TODO(nisse): Used only when UseSendSideBwe(config) is true.
     527           0 :       congestion_controller_->GetRemoteBitrateEstimator(true), config,
     528           0 :       config_.audio_state, event_log_);
     529             :   {
     530           0 :     WriteLockScoped write_lock(*receive_crit_);
     531           0 :     RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
     532           0 :                audio_receive_ssrcs_.end());
     533           0 :     audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
     534           0 :     ConfigureSync(config.sync_group);
     535             :   }
     536             :   {
     537           0 :     ReadLockScoped read_lock(*send_crit_);
     538           0 :     auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
     539           0 :     if (it != audio_send_ssrcs_.end()) {
     540           0 :       receive_stream->AssociateSendStream(it->second);
     541             :     }
     542             :   }
     543           0 :   receive_stream->SignalNetworkState(audio_network_state_);
     544           0 :   UpdateAggregateNetworkState();
     545           0 :   return receive_stream;
     546             : }
     547             : 
     548           0 : void Call::DestroyAudioReceiveStream(
     549             :     webrtc::AudioReceiveStream* receive_stream) {
     550           0 :   TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
     551           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     552           0 :   RTC_DCHECK(receive_stream != nullptr);
     553             :   webrtc::internal::AudioReceiveStream* audio_receive_stream =
     554           0 :       static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
     555             :   {
     556           0 :     WriteLockScoped write_lock(*receive_crit_);
     557           0 :     size_t num_deleted = audio_receive_ssrcs_.erase(
     558           0 :         audio_receive_stream->config().rtp.remote_ssrc);
     559           0 :     RTC_DCHECK(num_deleted == 1);
     560           0 :     const std::string& sync_group = audio_receive_stream->config().sync_group;
     561           0 :     const auto it = sync_stream_mapping_.find(sync_group);
     562           0 :     if (it != sync_stream_mapping_.end() &&
     563           0 :         it->second == audio_receive_stream) {
     564           0 :       sync_stream_mapping_.erase(it);
     565           0 :       ConfigureSync(sync_group);
     566             :     }
     567             :   }
     568           0 :   UpdateAggregateNetworkState();
     569           0 :   delete audio_receive_stream;
     570           0 : }
     571             : 
     572           0 : webrtc::VideoSendStream* Call::CreateVideoSendStream(
     573             :     webrtc::VideoSendStream::Config config,
     574             :     VideoEncoderConfig encoder_config) {
     575           0 :   TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
     576           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     577             : 
     578           0 :   video_send_delay_stats_->AddSsrcs(config);
     579           0 :   event_log_->LogVideoSendStreamConfig(config);
     580             : 
     581             :   // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
     582             :   // the call has already started.
     583             :   // Copy ssrcs from |config| since |config| is moved.
     584           0 :   std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
     585             :   VideoSendStream* send_stream = new VideoSendStream(
     586           0 :       num_cpu_cores_, module_process_thread_.get(), &worker_queue_,
     587           0 :       call_stats_.get(), congestion_controller_.get(), &packet_router_,
     588           0 :       bitrate_allocator_.get(), video_send_delay_stats_.get(), &remb_,
     589           0 :       event_log_, std::move(config), std::move(encoder_config),
     590           0 :       suspended_video_send_ssrcs_);
     591             : 
     592             :   {
     593           0 :     WriteLockScoped write_lock(*send_crit_);
     594           0 :     for (uint32_t ssrc : ssrcs) {
     595           0 :       RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
     596           0 :       video_send_ssrcs_[ssrc] = send_stream;
     597             :     }
     598           0 :     video_send_streams_.insert(send_stream);
     599             :   }
     600           0 :   send_stream->SignalNetworkState(video_network_state_);
     601           0 :   UpdateAggregateNetworkState();
     602             : 
     603           0 :   return send_stream;
     604             : }
     605             : 
     606           0 : void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
     607           0 :   TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
     608           0 :   RTC_DCHECK(send_stream != nullptr);
     609           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     610             : 
     611           0 :   send_stream->Stop();
     612             : 
     613           0 :   VideoSendStream* send_stream_impl = nullptr;
     614             :   {
     615           0 :     WriteLockScoped write_lock(*send_crit_);
     616           0 :     auto it = video_send_ssrcs_.begin();
     617           0 :     while (it != video_send_ssrcs_.end()) {
     618           0 :       if (it->second == static_cast<VideoSendStream*>(send_stream)) {
     619           0 :         send_stream_impl = it->second;
     620           0 :         video_send_ssrcs_.erase(it++);
     621             :       } else {
     622           0 :         ++it;
     623             :       }
     624             :     }
     625           0 :     video_send_streams_.erase(send_stream_impl);
     626             :   }
     627           0 :   RTC_CHECK(send_stream_impl != nullptr);
     628             : 
     629             :   VideoSendStream::RtpStateMap rtp_state =
     630           0 :       send_stream_impl->StopPermanentlyAndGetRtpStates();
     631             : 
     632           0 :   for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
     633           0 :        it != rtp_state.end(); ++it) {
     634           0 :     suspended_video_send_ssrcs_[it->first] = it->second;
     635             :   }
     636             : 
     637           0 :   UpdateAggregateNetworkState();
     638           0 :   delete send_stream_impl;
     639           0 : }
     640             : 
     641           0 : webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
     642             :     webrtc::VideoReceiveStream::Config configuration) {
     643           0 :   TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
     644           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     645             :   VideoReceiveStream* receive_stream = new VideoReceiveStream(
     646           0 :       num_cpu_cores_, congestion_controller_.get(), &packet_router_,
     647           0 :       std::move(configuration), voice_engine(), module_process_thread_.get(),
     648           0 :       call_stats_.get(), &remb_);
     649             : 
     650           0 :   const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
     651             :   {
     652           0 :     WriteLockScoped write_lock(*receive_crit_);
     653           0 :     RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
     654           0 :                video_receive_ssrcs_.end());
     655           0 :     video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
     656             :     // TODO(pbos): Configure different RTX payloads per receive payload.
     657             :     VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
     658           0 :         config.rtp.rtx.begin();
     659           0 :     if (it != config.rtp.rtx.end())
     660           0 :       video_receive_ssrcs_[it->second.ssrc] = receive_stream;
     661           0 :     video_receive_streams_.insert(receive_stream);
     662           0 :     ConfigureSync(config.sync_group);
     663             :   }
     664           0 :   receive_stream->SignalNetworkState(video_network_state_);
     665           0 :   UpdateAggregateNetworkState();
     666           0 :   event_log_->LogVideoReceiveStreamConfig(config);
     667           0 :   return receive_stream;
     668             : }
     669             : 
     670           0 : void Call::DestroyVideoReceiveStream(
     671             :     webrtc::VideoReceiveStream* receive_stream) {
     672           0 :   TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
     673           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     674           0 :   RTC_DCHECK(receive_stream != nullptr);
     675           0 :   VideoReceiveStream* receive_stream_impl = nullptr;
     676             :   {
     677           0 :     WriteLockScoped write_lock(*receive_crit_);
     678             :     // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
     679             :     // separate SSRC there can be either one or two.
     680           0 :     auto it = video_receive_ssrcs_.begin();
     681           0 :     while (it != video_receive_ssrcs_.end()) {
     682           0 :       if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
     683           0 :         if (receive_stream_impl != nullptr)
     684           0 :           RTC_DCHECK(receive_stream_impl == it->second);
     685           0 :         receive_stream_impl = it->second;
     686           0 :         video_receive_ssrcs_.erase(it++);
     687             :       } else {
     688           0 :         ++it;
     689             :       }
     690             :     }
     691           0 :     video_receive_streams_.erase(receive_stream_impl);
     692           0 :     RTC_CHECK(receive_stream_impl != nullptr);
     693           0 :     ConfigureSync(receive_stream_impl->config().sync_group);
     694             :   }
     695           0 :   UpdateAggregateNetworkState();
     696           0 :   delete receive_stream_impl;
     697           0 : }
     698             : 
     699           0 : FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
     700             :     const FlexfecReceiveStream::Config& config) {
     701           0 :   TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
     702           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     703             : 
     704           0 :   RecoveredPacketReceiver* recovered_packet_receiver = this;
     705             :   FlexfecReceiveStreamImpl* receive_stream = new FlexfecReceiveStreamImpl(
     706           0 :       config, recovered_packet_receiver, call_stats_->rtcp_rtt_stats(),
     707           0 :       module_process_thread_.get());
     708             : 
     709             :   {
     710           0 :     WriteLockScoped write_lock(*receive_crit_);
     711             : 
     712           0 :     RTC_DCHECK(flexfec_receive_streams_.find(receive_stream) ==
     713           0 :                flexfec_receive_streams_.end());
     714           0 :     flexfec_receive_streams_.insert(receive_stream);
     715             : 
     716           0 :     for (auto ssrc : config.protected_media_ssrcs)
     717           0 :       flexfec_receive_ssrcs_media_.insert(std::make_pair(ssrc, receive_stream));
     718             : 
     719           0 :     RTC_DCHECK(flexfec_receive_ssrcs_protection_.find(config.remote_ssrc) ==
     720           0 :                flexfec_receive_ssrcs_protection_.end());
     721           0 :     flexfec_receive_ssrcs_protection_[config.remote_ssrc] = receive_stream;
     722             : 
     723           0 :     RTC_DCHECK(received_rtp_header_extensions_.find(config.remote_ssrc) ==
     724           0 :                received_rtp_header_extensions_.end());
     725           0 :     RtpHeaderExtensionMap rtp_header_extensions(config.rtp_header_extensions);
     726           0 :     received_rtp_header_extensions_[config.remote_ssrc] = rtp_header_extensions;
     727             :   }
     728             : 
     729             :   // TODO(brandtr): Store config in RtcEventLog here.
     730             : 
     731           0 :   return receive_stream;
     732             : }
     733             : 
     734           0 : void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
     735           0 :   TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
     736           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     737             : 
     738           0 :   RTC_DCHECK(receive_stream != nullptr);
     739             :   // There exist no other derived classes of FlexfecReceiveStream,
     740             :   // so this downcast is safe.
     741             :   FlexfecReceiveStreamImpl* receive_stream_impl =
     742           0 :       static_cast<FlexfecReceiveStreamImpl*>(receive_stream);
     743             :   {
     744           0 :     WriteLockScoped write_lock(*receive_crit_);
     745             : 
     746           0 :     uint32_t ssrc = receive_stream_impl->GetConfig().remote_ssrc;
     747           0 :     received_rtp_header_extensions_.erase(ssrc);
     748             : 
     749             :     // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
     750             :     // destroyed.
     751           0 :     auto prot_it = flexfec_receive_ssrcs_protection_.begin();
     752           0 :     while (prot_it != flexfec_receive_ssrcs_protection_.end()) {
     753           0 :       if (prot_it->second == receive_stream_impl)
     754           0 :         prot_it = flexfec_receive_ssrcs_protection_.erase(prot_it);
     755             :       else
     756           0 :         ++prot_it;
     757             :     }
     758           0 :     auto media_it = flexfec_receive_ssrcs_media_.begin();
     759           0 :     while (media_it != flexfec_receive_ssrcs_media_.end()) {
     760           0 :       if (media_it->second == receive_stream_impl)
     761           0 :         media_it = flexfec_receive_ssrcs_media_.erase(media_it);
     762             :       else
     763           0 :         ++media_it;
     764             :     }
     765             : 
     766           0 :     flexfec_receive_streams_.erase(receive_stream_impl);
     767             :   }
     768             : 
     769           0 :   delete receive_stream_impl;
     770           0 : }
     771             : 
     772           0 : Call::Stats Call::GetStats() const {
     773             :   // TODO(solenberg): Some test cases in EndToEndTest use this from a different
     774             :   // thread. Re-enable once that is fixed.
     775             :   // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     776           0 :   Stats stats;
     777             :   // Fetch available send/receive bitrates.
     778           0 :   uint32_t send_bandwidth = 0;
     779           0 :   congestion_controller_->GetBitrateController()->AvailableBandwidth(
     780           0 :       &send_bandwidth);
     781           0 :   std::vector<unsigned int> ssrcs;
     782           0 :   uint32_t recv_bandwidth = 0;
     783           0 :   congestion_controller_->GetRemoteBitrateEstimator(false)->LatestEstimate(
     784           0 :       &ssrcs, &recv_bandwidth);
     785           0 :   stats.send_bandwidth_bps = send_bandwidth;
     786           0 :   stats.recv_bandwidth_bps = recv_bandwidth;
     787           0 :   stats.pacer_delay_ms = congestion_controller_->GetPacerQueuingDelayMs();
     788           0 :   stats.rtt_ms = call_stats_->rtcp_rtt_stats()->LastProcessedRtt();
     789             :   {
     790           0 :     rtc::CritScope cs(&bitrate_crit_);
     791           0 :     stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
     792             :   }
     793           0 :   return stats;
     794             : }
     795             : 
     796           0 : void Call::SetBitrateConfig(
     797             :     const webrtc::Call::Config::BitrateConfig& bitrate_config) {
     798           0 :   TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
     799           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     800           0 :   RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
     801           0 :   if (bitrate_config.max_bitrate_bps != -1)
     802           0 :     RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
     803           0 :   if (config_.bitrate_config.min_bitrate_bps ==
     804           0 :           bitrate_config.min_bitrate_bps &&
     805           0 :       (bitrate_config.start_bitrate_bps <= 0 ||
     806           0 :        config_.bitrate_config.start_bitrate_bps ==
     807           0 :            bitrate_config.start_bitrate_bps) &&
     808           0 :       config_.bitrate_config.max_bitrate_bps ==
     809           0 :           bitrate_config.max_bitrate_bps) {
     810             :     // Nothing new to set, early abort to avoid encoder reconfigurations.
     811           0 :     return;
     812             :   }
     813           0 :   config_.bitrate_config.min_bitrate_bps = bitrate_config.min_bitrate_bps;
     814             :   // Start bitrate of -1 means we should keep the old bitrate, which there is
     815             :   // no point in remembering for the future.
     816           0 :   if (bitrate_config.start_bitrate_bps > 0)
     817           0 :     config_.bitrate_config.start_bitrate_bps = bitrate_config.start_bitrate_bps;
     818           0 :   config_.bitrate_config.max_bitrate_bps = bitrate_config.max_bitrate_bps;
     819           0 :   congestion_controller_->SetBweBitrates(bitrate_config.min_bitrate_bps,
     820           0 :                                          bitrate_config.start_bitrate_bps,
     821           0 :                                          bitrate_config.max_bitrate_bps);
     822             : }
     823             : 
     824           0 : void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
     825           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     826           0 :   switch (media) {
     827             :     case MediaType::AUDIO:
     828           0 :       audio_network_state_ = state;
     829           0 :       break;
     830             :     case MediaType::VIDEO:
     831           0 :       video_network_state_ = state;
     832           0 :       break;
     833             :     case MediaType::ANY:
     834             :     case MediaType::DATA:
     835           0 :       RTC_NOTREACHED();
     836           0 :       break;
     837             :   }
     838             : 
     839           0 :   UpdateAggregateNetworkState();
     840             :   {
     841           0 :     ReadLockScoped read_lock(*send_crit_);
     842           0 :     for (auto& kv : audio_send_ssrcs_) {
     843           0 :       kv.second->SignalNetworkState(audio_network_state_);
     844             :     }
     845           0 :     for (auto& kv : video_send_ssrcs_) {
     846           0 :       kv.second->SignalNetworkState(video_network_state_);
     847             :     }
     848             :   }
     849             :   {
     850           0 :     ReadLockScoped read_lock(*receive_crit_);
     851           0 :     for (auto& kv : audio_receive_ssrcs_) {
     852           0 :       kv.second->SignalNetworkState(audio_network_state_);
     853             :     }
     854           0 :     for (auto& kv : video_receive_ssrcs_) {
     855           0 :       kv.second->SignalNetworkState(video_network_state_);
     856             :     }
     857             :   }
     858           0 : }
     859             : 
     860           0 : void Call::OnTransportOverheadChanged(MediaType media,
     861             :                                       int transport_overhead_per_packet) {
     862           0 :   switch (media) {
     863             :     case MediaType::AUDIO: {
     864           0 :       ReadLockScoped read_lock(*send_crit_);
     865           0 :       for (auto& kv : audio_send_ssrcs_) {
     866           0 :         kv.second->SetTransportOverhead(transport_overhead_per_packet);
     867             :       }
     868           0 :       break;
     869             :     }
     870             :     case MediaType::VIDEO: {
     871           0 :       ReadLockScoped read_lock(*send_crit_);
     872           0 :       for (auto& kv : video_send_ssrcs_) {
     873           0 :         kv.second->SetTransportOverhead(transport_overhead_per_packet);
     874             :       }
     875           0 :       break;
     876             :     }
     877             :     case MediaType::ANY:
     878             :     case MediaType::DATA:
     879           0 :       RTC_NOTREACHED();
     880           0 :       break;
     881             :   }
     882           0 : }
     883             : 
     884             : // TODO(honghaiz): Add tests for this method.
     885           0 : void Call::OnNetworkRouteChanged(const std::string& transport_name,
     886             :                                  const rtc::NetworkRoute& network_route) {
     887           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     888             :   // Check if the network route is connected.
     889           0 :   if (!network_route.connected) {
     890           0 :     LOG(LS_INFO) << "Transport " << transport_name << " is disconnected";
     891             :     // TODO(honghaiz): Perhaps handle this in SignalChannelNetworkState and
     892             :     // consider merging these two methods.
     893           0 :     return;
     894             :   }
     895             : 
     896             :   // Check whether the network route has changed on each transport.
     897             :   auto result =
     898           0 :       network_routes_.insert(std::make_pair(transport_name, network_route));
     899           0 :   auto kv = result.first;
     900           0 :   bool inserted = result.second;
     901           0 :   if (inserted) {
     902             :     // No need to reset BWE if this is the first time the network connects.
     903           0 :     return;
     904             :   }
     905           0 :   if (kv->second != network_route) {
     906           0 :     kv->second = network_route;
     907           0 :     LOG(LS_INFO) << "Network route changed on transport " << transport_name
     908           0 :                  << ": new local network id " << network_route.local_network_id
     909           0 :                  << " new remote network id " << network_route.remote_network_id
     910           0 :                  << " Reset bitrates to min: "
     911             :                  << config_.bitrate_config.min_bitrate_bps
     912           0 :                  << " bps, start: " << config_.bitrate_config.start_bitrate_bps
     913           0 :                  << " bps,  max: " << config_.bitrate_config.start_bitrate_bps
     914           0 :                  << " bps.";
     915           0 :     congestion_controller_->ResetBweAndBitrates(
     916             :         config_.bitrate_config.start_bitrate_bps,
     917             :         config_.bitrate_config.min_bitrate_bps,
     918           0 :         config_.bitrate_config.max_bitrate_bps);
     919             :   }
     920             : }
     921             : 
     922           0 : void Call::UpdateAggregateNetworkState() {
     923           0 :   RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
     924             : 
     925           0 :   bool have_audio = false;
     926           0 :   bool have_video = false;
     927             :   {
     928           0 :     ReadLockScoped read_lock(*send_crit_);
     929           0 :     if (audio_send_ssrcs_.size() > 0)
     930           0 :       have_audio = true;
     931           0 :     if (video_send_ssrcs_.size() > 0)
     932           0 :       have_video = true;
     933             :   }
     934             :   {
     935           0 :     ReadLockScoped read_lock(*receive_crit_);
     936           0 :     if (audio_receive_ssrcs_.size() > 0)
     937           0 :       have_audio = true;
     938           0 :     if (video_receive_ssrcs_.size() > 0)
     939           0 :       have_video = true;
     940             :   }
     941             : 
     942           0 :   NetworkState aggregate_state = kNetworkDown;
     943           0 :   if ((have_video && video_network_state_ == kNetworkUp) ||
     944           0 :       (have_audio && audio_network_state_ == kNetworkUp)) {
     945           0 :     aggregate_state = kNetworkUp;
     946             :   }
     947             : 
     948           0 :   LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
     949           0 :                << (aggregate_state == kNetworkUp ? "up" : "down");
     950             : 
     951           0 :   congestion_controller_->SignalNetworkState(aggregate_state);
     952           0 : }
     953             : 
     954           0 : void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
     955           0 :   if (first_packet_sent_ms_ == -1)
     956           0 :     first_packet_sent_ms_ = clock_->TimeInMilliseconds();
     957           0 :   video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
     958           0 :                                         clock_->TimeInMilliseconds());
     959           0 :   congestion_controller_->OnSentPacket(sent_packet);
     960           0 : }
     961             : 
     962           0 : void Call::OnNetworkChanged(uint32_t target_bitrate_bps,
     963             :                             uint8_t fraction_loss,
     964             :                             int64_t rtt_ms,
     965             :                             int64_t probing_interval_ms) {
     966             :   // TODO(perkj): Consider making sure CongestionController operates on
     967             :   // |worker_queue_|.
     968           0 :   if (!worker_queue_.IsCurrent()) {
     969           0 :     worker_queue_.PostTask(
     970           0 :         [this, target_bitrate_bps, fraction_loss, rtt_ms, probing_interval_ms] {
     971           0 :           OnNetworkChanged(target_bitrate_bps, fraction_loss, rtt_ms,
     972           0 :                            probing_interval_ms);
     973           0 :         });
     974           0 :     return;
     975             :   }
     976           0 :   RTC_DCHECK_RUN_ON(&worker_queue_);
     977           0 :   bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss,
     978           0 :                                        rtt_ms, probing_interval_ms);
     979             : 
     980             :   // Ignore updates if bitrate is zero (the aggregate network state is down).
     981           0 :   if (target_bitrate_bps == 0) {
     982           0 :     rtc::CritScope lock(&bitrate_crit_);
     983           0 :     estimated_send_bitrate_kbps_counter_.ProcessAndPause();
     984           0 :     pacer_bitrate_kbps_counter_.ProcessAndPause();
     985           0 :     return;
     986             :   }
     987             : 
     988             :   bool sending_video;
     989             :   {
     990           0 :     ReadLockScoped read_lock(*send_crit_);
     991           0 :     sending_video = !video_send_streams_.empty();
     992             :   }
     993             : 
     994           0 :   rtc::CritScope lock(&bitrate_crit_);
     995           0 :   if (!sending_video) {
     996             :     // Do not update the stats if we are not sending video.
     997           0 :     estimated_send_bitrate_kbps_counter_.ProcessAndPause();
     998           0 :     pacer_bitrate_kbps_counter_.ProcessAndPause();
     999           0 :     return;
    1000             :   }
    1001           0 :   estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
    1002             :   // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
    1003             :   uint32_t pacer_bitrate_bps =
    1004           0 :       std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
    1005           0 :   pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
    1006             : }
    1007             : 
    1008           0 : void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
    1009             :                                      uint32_t max_padding_bitrate_bps) {
    1010           0 :   congestion_controller_->SetAllocatedSendBitrateLimits(
    1011           0 :       min_send_bitrate_bps, max_padding_bitrate_bps);
    1012           0 :   rtc::CritScope lock(&bitrate_crit_);
    1013           0 :   min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
    1014           0 :   configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
    1015           0 : }
    1016             : 
    1017           0 : void Call::ConfigureSync(const std::string& sync_group) {
    1018             :   // Set sync only if there was no previous one.
    1019           0 :   if (voice_engine() == nullptr || sync_group.empty())
    1020           0 :     return;
    1021             : 
    1022           0 :   AudioReceiveStream* sync_audio_stream = nullptr;
    1023             :   // Find existing audio stream.
    1024           0 :   const auto it = sync_stream_mapping_.find(sync_group);
    1025           0 :   if (it != sync_stream_mapping_.end()) {
    1026           0 :     sync_audio_stream = it->second;
    1027             :   } else {
    1028             :     // No configured audio stream, see if we can find one.
    1029           0 :     for (const auto& kv : audio_receive_ssrcs_) {
    1030           0 :       if (kv.second->config().sync_group == sync_group) {
    1031           0 :         if (sync_audio_stream != nullptr) {
    1032           0 :           LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
    1033             :                              "within the same sync group. This is not "
    1034           0 :                              "supported in the current implementation.";
    1035           0 :           break;
    1036             :         }
    1037           0 :         sync_audio_stream = kv.second;
    1038             :       }
    1039             :     }
    1040             :   }
    1041           0 :   if (sync_audio_stream)
    1042           0 :     sync_stream_mapping_[sync_group] = sync_audio_stream;
    1043           0 :   size_t num_synced_streams = 0;
    1044           0 :   for (VideoReceiveStream* video_stream : video_receive_streams_) {
    1045           0 :     if (video_stream->config().sync_group != sync_group)
    1046           0 :       continue;
    1047           0 :     ++num_synced_streams;
    1048           0 :     if (num_synced_streams > 1) {
    1049             :       // TODO(pbos): Support synchronizing more than one A/V pair.
    1050             :       // https://code.google.com/p/webrtc/issues/detail?id=4762
    1051           0 :       LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
    1052             :                          "within the same sync group. This is not supported in "
    1053           0 :                          "the current implementation.";
    1054             :     }
    1055             :     // Only sync the first A/V pair within this sync group.
    1056           0 :     if (sync_audio_stream != nullptr && num_synced_streams == 1) {
    1057           0 :       video_stream->SetSyncChannel(voice_engine(),
    1058           0 :                                    sync_audio_stream->config().voe_channel_id);
    1059             :     } else {
    1060           0 :       video_stream->SetSyncChannel(voice_engine(), -1);
    1061             :     }
    1062             :   }
    1063             : }
    1064             : 
    1065           0 : PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
    1066             :                                                  const uint8_t* packet,
    1067             :                                                  size_t length) {
    1068           0 :   TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
    1069             :   // TODO(pbos): Make sure it's a valid packet.
    1070             :   //             Return DELIVERY_UNKNOWN_SSRC if it can be determined that
    1071             :   //             there's no receiver of the packet.
    1072           0 :   if (received_bytes_per_second_counter_.HasSample()) {
    1073             :     // First RTP packet has been received.
    1074           0 :     received_bytes_per_second_counter_.Add(static_cast<int>(length));
    1075           0 :     received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
    1076             :   }
    1077           0 :   bool rtcp_delivered = false;
    1078           0 :   if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
    1079           0 :     ReadLockScoped read_lock(*receive_crit_);
    1080           0 :     for (VideoReceiveStream* stream : video_receive_streams_) {
    1081           0 :       if (stream->DeliverRtcp(packet, length))
    1082           0 :         rtcp_delivered = true;
    1083             :     }
    1084             :   }
    1085           0 :   if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
    1086           0 :     ReadLockScoped read_lock(*receive_crit_);
    1087           0 :     for (auto& kv : audio_receive_ssrcs_) {
    1088           0 :       if (kv.second->DeliverRtcp(packet, length))
    1089           0 :         rtcp_delivered = true;
    1090             :     }
    1091             :   }
    1092           0 :   if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
    1093           0 :     ReadLockScoped read_lock(*send_crit_);
    1094           0 :     for (VideoSendStream* stream : video_send_streams_) {
    1095           0 :       if (stream->DeliverRtcp(packet, length))
    1096           0 :         rtcp_delivered = true;
    1097             :     }
    1098             :   }
    1099           0 :   if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
    1100           0 :     ReadLockScoped read_lock(*send_crit_);
    1101           0 :     for (auto& kv : audio_send_ssrcs_) {
    1102           0 :       if (kv.second->DeliverRtcp(packet, length))
    1103           0 :         rtcp_delivered = true;
    1104             :     }
    1105             :   }
    1106             : 
    1107           0 :   if (rtcp_delivered)
    1108           0 :     event_log_->LogRtcpPacket(kIncomingPacket, media_type, packet, length);
    1109             : 
    1110           0 :   return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
    1111             : }
    1112             : 
    1113           0 : PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
    1114             :                                                 const uint8_t* packet,
    1115             :                                                 size_t length,
    1116             :                                                 const PacketTime& packet_time) {
    1117           0 :   TRACE_EVENT0("webrtc", "Call::DeliverRtp");
    1118             :   // Minimum RTP header size.
    1119           0 :   if (length < 12)
    1120           0 :     return DELIVERY_PACKET_ERROR;
    1121             : 
    1122           0 :   uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
    1123           0 :   ReadLockScoped read_lock(*receive_crit_);
    1124           0 :   if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
    1125           0 :     auto it = audio_receive_ssrcs_.find(ssrc);
    1126           0 :     if (it != audio_receive_ssrcs_.end()) {
    1127           0 :       received_bytes_per_second_counter_.Add(static_cast<int>(length));
    1128           0 :       received_audio_bytes_per_second_counter_.Add(static_cast<int>(length));
    1129           0 :       auto status = it->second->DeliverRtp(packet, length, packet_time)
    1130           0 :                         ? DELIVERY_OK
    1131           0 :                         : DELIVERY_PACKET_ERROR;
    1132           0 :       if (status == DELIVERY_OK)
    1133           0 :         event_log_->LogRtpHeader(kIncomingPacket, media_type, packet, length);
    1134           0 :       return status;
    1135             :     }
    1136             :   }
    1137           0 :   if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
    1138           0 :     auto it = video_receive_ssrcs_.find(ssrc);
    1139           0 :     if (it != video_receive_ssrcs_.end()) {
    1140           0 :       received_bytes_per_second_counter_.Add(static_cast<int>(length));
    1141           0 :       received_video_bytes_per_second_counter_.Add(static_cast<int>(length));
    1142             :       // TODO(brandtr): Notify the BWE of received media packets here.
    1143           0 :       auto status = it->second->DeliverRtp(packet, length, packet_time)
    1144           0 :                         ? DELIVERY_OK
    1145           0 :                         : DELIVERY_PACKET_ERROR;
    1146             :       // Deliver media packets to FlexFEC subsystem. RTP header extensions need
    1147             :       // not be parsed, as FlexFEC is oblivious to the semantic meaning of the
    1148             :       // packet contents beyond the 12 byte RTP base header. The BWE is fed
    1149             :       // information about these media packets from the regular media pipeline.
    1150             :       rtc::Optional<RtpPacketReceived> parsed_packet =
    1151           0 :           ParseRtpPacket(packet, length, packet_time);
    1152           0 :       if (parsed_packet) {
    1153           0 :         auto it_bounds = flexfec_receive_ssrcs_media_.equal_range(ssrc);
    1154           0 :         for (auto it = it_bounds.first; it != it_bounds.second; ++it)
    1155           0 :           it->second->AddAndProcessReceivedPacket(*parsed_packet);
    1156             :       }
    1157           0 :       if (status == DELIVERY_OK)
    1158           0 :         event_log_->LogRtpHeader(kIncomingPacket, media_type, packet, length);
    1159           0 :       return status;
    1160             :     }
    1161             :   }
    1162           0 :   if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
    1163           0 :     auto it = flexfec_receive_ssrcs_protection_.find(ssrc);
    1164           0 :     if (it != flexfec_receive_ssrcs_protection_.end()) {
    1165             :       rtc::Optional<RtpPacketReceived> parsed_packet =
    1166           0 :           ParseRtpPacket(packet, length, packet_time);
    1167           0 :       if (parsed_packet) {
    1168           0 :         NotifyBweOfReceivedPacket(*parsed_packet);
    1169           0 :         auto status = it->second->AddAndProcessReceivedPacket(*parsed_packet)
    1170           0 :                           ? DELIVERY_OK
    1171           0 :                           : DELIVERY_PACKET_ERROR;
    1172           0 :         if (status == DELIVERY_OK)
    1173           0 :           event_log_->LogRtpHeader(kIncomingPacket, media_type, packet, length);
    1174           0 :         return status;
    1175             :       }
    1176             :     }
    1177             :   }
    1178           0 :   LOG(LS_WARNING) << __FUNCTION__ <<": found unknown SSRC: " << ssrc;
    1179           0 :   return DELIVERY_UNKNOWN_SSRC;
    1180             : }
    1181             : 
    1182           0 : PacketReceiver::DeliveryStatus Call::DeliverPacket(
    1183             :     MediaType media_type,
    1184             :     const uint8_t* packet,
    1185             :     size_t length,
    1186             :     const PacketTime& packet_time) {
    1187             :   // TODO(solenberg): Tests call this function on a network thread, libjingle
    1188             :   // calls on the worker thread. We should move towards always using a network
    1189             :   // thread. Then this check can be enabled.
    1190             :   // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
    1191           0 :   if (RtpHeaderParser::IsRtcp(packet, length))
    1192           0 :     return DeliverRtcp(media_type, packet, length);
    1193             : 
    1194           0 :   return DeliverRtp(media_type, packet, length, packet_time);
    1195             : }
    1196             : 
    1197             : // TODO(brandtr): Update this member function when we support protecting
    1198             : // audio packets with FlexFEC.
    1199           0 : bool Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
    1200           0 :   uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
    1201           0 :   ReadLockScoped read_lock(*receive_crit_);
    1202           0 :   auto it = video_receive_ssrcs_.find(ssrc);
    1203           0 :   if (it == video_receive_ssrcs_.end())
    1204           0 :     return false;
    1205           0 :   return it->second->OnRecoveredPacket(packet, length);
    1206             : }
    1207             : 
    1208           0 : void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet) {
    1209           0 :   RTPHeader header;
    1210           0 :   packet.GetHeader(&header);
    1211           0 :   congestion_controller_->OnReceivedPacket(packet.arrival_time_ms(),
    1212           0 :                                            packet.payload_size(), header);
    1213           0 : }
    1214             : 
    1215             : }  // namespace internal
    1216             : }  // namespace webrtc

Generated by: LCOV version 1.13