• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "call/call.h"
12 
13 #include <string.h>
14 
15 #include <algorithm>
16 #include <atomic>
17 #include <map>
18 #include <memory>
19 #include <set>
20 #include <utility>
21 #include <vector>
22 
23 #include "absl/functional/bind_front.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/types/optional.h"
26 #include "api/rtc_event_log/rtc_event_log.h"
27 #include "api/sequence_checker.h"
28 #include "api/task_queue/pending_task_safety_flag.h"
29 #include "api/transport/network_control.h"
30 #include "audio/audio_receive_stream.h"
31 #include "audio/audio_send_stream.h"
32 #include "audio/audio_state.h"
33 #include "call/adaptation/broadcast_resource_listener.h"
34 #include "call/bitrate_allocator.h"
35 #include "call/flexfec_receive_stream_impl.h"
36 #include "call/receive_time_calculator.h"
37 #include "call/rtp_stream_receiver_controller.h"
38 #include "call/rtp_transport_controller_send.h"
39 #include "call/rtp_transport_controller_send_factory.h"
40 #include "call/version.h"
41 #include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
42 #include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
43 #include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
44 #include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
45 #include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
46 #include "logging/rtc_event_log/rtc_stream_config.h"
47 #include "modules/congestion_controller/include/receive_side_congestion_controller.h"
48 #include "modules/rtp_rtcp/include/flexfec_receiver.h"
49 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
50 #include "modules/rtp_rtcp/source/byte_io.h"
51 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
52 #include "modules/rtp_rtcp/source/rtp_util.h"
53 #include "modules/video_coding/fec_controller_default.h"
54 #include "rtc_base/checks.h"
55 #include "rtc_base/logging.h"
56 #include "rtc_base/strings/string_builder.h"
57 #include "rtc_base/system/no_unique_address.h"
58 #include "rtc_base/task_utils/repeating_task.h"
59 #include "rtc_base/thread_annotations.h"
60 #include "rtc_base/time_utils.h"
61 #include "rtc_base/trace_event.h"
62 #include "system_wrappers/include/clock.h"
63 #include "system_wrappers/include/cpu_info.h"
64 #include "system_wrappers/include/metrics.h"
65 #include "video/call_stats2.h"
66 #include "video/send_delay_stats.h"
67 #include "video/stats_counter.h"
68 #include "video/video_receive_stream2.h"
69 #include "video/video_send_stream.h"
70 
71 namespace webrtc {
72 
73 namespace {
SendPeriodicFeedback(const std::vector<RtpExtension> & extensions)74 bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) {
75   for (const auto& extension : extensions) {
76     if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
77       return false;
78   }
79   return true;
80 }
81 
HasTransportSequenceNumber(const RtpHeaderExtensionMap & map)82 bool HasTransportSequenceNumber(const RtpHeaderExtensionMap& map) {
83   return map.IsRegistered(kRtpExtensionTransportSequenceNumber) ||
84          map.IsRegistered(kRtpExtensionTransportSequenceNumber02);
85 }
86 
UseSendSideBwe(const ReceiveStreamInterface * stream)87 bool UseSendSideBwe(const ReceiveStreamInterface* stream) {
88   return stream->transport_cc() &&
89          HasTransportSequenceNumber(stream->GetRtpExtensionMap());
90 }
91 
FindKeyByValue(const std::map<int,int> & m,int v)92 const int* FindKeyByValue(const std::map<int, int>& m, int v) {
93   for (const auto& kv : m) {
94     if (kv.second == v)
95       return &kv.first;
96   }
97   return nullptr;
98 }
99 
CreateRtcLogStreamConfig(const VideoReceiveStreamInterface::Config & config)100 std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
101     const VideoReceiveStreamInterface::Config& config) {
102   auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
103   rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
104   rtclog_config->local_ssrc = config.rtp.local_ssrc;
105   rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
106   rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
107   rtclog_config->rtp_extensions = config.rtp.extensions;
108 
109   for (const auto& d : config.decoders) {
110     const int* search =
111         FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
112     rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
113                                        search ? *search : 0);
114   }
115   return rtclog_config;
116 }
117 
CreateRtcLogStreamConfig(const VideoSendStream::Config & config,size_t ssrc_index)118 std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
119     const VideoSendStream::Config& config,
120     size_t ssrc_index) {
121   auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
122   rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
123   if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
124     rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
125   }
126   rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
127   rtclog_config->rtp_extensions = config.rtp.extensions;
128 
129   rtclog_config->codecs.emplace_back(config.rtp.payload_name,
130                                      config.rtp.payload_type,
131                                      config.rtp.rtx.payload_type);
132   return rtclog_config;
133 }
134 
CreateRtcLogStreamConfig(const AudioReceiveStreamInterface::Config & config)135 std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
136     const AudioReceiveStreamInterface::Config& config) {
137   auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
138   rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
139   rtclog_config->local_ssrc = config.rtp.local_ssrc;
140   rtclog_config->rtp_extensions = config.rtp.extensions;
141   return rtclog_config;
142 }
143 
GetCurrentTaskQueueOrThread()144 TaskQueueBase* GetCurrentTaskQueueOrThread() {
145   TaskQueueBase* current = TaskQueueBase::Current();
146   if (!current)
147     current = rtc::ThreadManager::Instance()->CurrentThread();
148   return current;
149 }
150 
151 }  // namespace
152 
153 namespace internal {
154 
155 // Wraps an injected resource in a BroadcastResourceListener and handles adding
156 // and removing adapter resources to individual VideoSendStreams.
157 class ResourceVideoSendStreamForwarder {
158  public:
ResourceVideoSendStreamForwarder(rtc::scoped_refptr<webrtc::Resource> resource)159   ResourceVideoSendStreamForwarder(
160       rtc::scoped_refptr<webrtc::Resource> resource)
161       : broadcast_resource_listener_(resource) {
162     broadcast_resource_listener_.StartListening();
163   }
~ResourceVideoSendStreamForwarder()164   ~ResourceVideoSendStreamForwarder() {
165     RTC_DCHECK(adapter_resources_.empty());
166     broadcast_resource_listener_.StopListening();
167   }
168 
Resource() const169   rtc::scoped_refptr<webrtc::Resource> Resource() const {
170     return broadcast_resource_listener_.SourceResource();
171   }
172 
OnCreateVideoSendStream(VideoSendStream * video_send_stream)173   void OnCreateVideoSendStream(VideoSendStream* video_send_stream) {
174     RTC_DCHECK(adapter_resources_.find(video_send_stream) ==
175                adapter_resources_.end());
176     auto adapter_resource =
177         broadcast_resource_listener_.CreateAdapterResource();
178     video_send_stream->AddAdaptationResource(adapter_resource);
179     adapter_resources_.insert(
180         std::make_pair(video_send_stream, adapter_resource));
181   }
182 
OnDestroyVideoSendStream(VideoSendStream * video_send_stream)183   void OnDestroyVideoSendStream(VideoSendStream* video_send_stream) {
184     auto it = adapter_resources_.find(video_send_stream);
185     RTC_DCHECK(it != adapter_resources_.end());
186     broadcast_resource_listener_.RemoveAdapterResource(it->second);
187     adapter_resources_.erase(it);
188   }
189 
190  private:
191   BroadcastResourceListener broadcast_resource_listener_;
192   std::map<VideoSendStream*, rtc::scoped_refptr<webrtc::Resource>>
193       adapter_resources_;
194 };
195 
196 class Call final : public webrtc::Call,
197                    public PacketReceiver,
198                    public RecoveredPacketReceiver,
199                    public TargetTransferRateObserver,
200                    public BitrateAllocator::LimitObserver {
201  public:
202   Call(Clock* clock,
203        const Call::Config& config,
204        std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
205        TaskQueueFactory* task_queue_factory);
206   ~Call() override;
207 
208   Call(const Call&) = delete;
209   Call& operator=(const Call&) = delete;
210 
211   // Implements webrtc::Call.
212   PacketReceiver* Receiver() override;
213 
214   webrtc::AudioSendStream* CreateAudioSendStream(
215       const webrtc::AudioSendStream::Config& config) override;
216   void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
217 
218   webrtc::AudioReceiveStreamInterface* CreateAudioReceiveStream(
219       const webrtc::AudioReceiveStreamInterface::Config& config) override;
220   void DestroyAudioReceiveStream(
221       webrtc::AudioReceiveStreamInterface* receive_stream) override;
222 
223   webrtc::VideoSendStream* CreateVideoSendStream(
224       webrtc::VideoSendStream::Config config,
225       VideoEncoderConfig encoder_config) override;
226   webrtc::VideoSendStream* CreateVideoSendStream(
227       webrtc::VideoSendStream::Config config,
228       VideoEncoderConfig encoder_config,
229       std::unique_ptr<FecController> fec_controller) override;
230   void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
231 
232   webrtc::VideoReceiveStreamInterface* CreateVideoReceiveStream(
233       webrtc::VideoReceiveStreamInterface::Config configuration) override;
234   void DestroyVideoReceiveStream(
235       webrtc::VideoReceiveStreamInterface* receive_stream) override;
236 
237   FlexfecReceiveStream* CreateFlexfecReceiveStream(
238       const FlexfecReceiveStream::Config config) override;
239   void DestroyFlexfecReceiveStream(
240       FlexfecReceiveStream* receive_stream) override;
241 
242   void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
243 
244   RtpTransportControllerSendInterface* GetTransportControllerSend() override;
245 
246   Stats GetStats() const override;
247 
248   const FieldTrialsView& trials() const override;
249 
250   TaskQueueBase* network_thread() const override;
251   TaskQueueBase* worker_thread() const override;
252 
253   // Implements PacketReceiver.
254   DeliveryStatus DeliverPacket(MediaType media_type,
255                                rtc::CopyOnWriteBuffer packet,
256                                int64_t packet_time_us) override;
257 
258   // Implements RecoveredPacketReceiver.
259   void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
260 
261   void SignalChannelNetworkState(MediaType media, NetworkState state) override;
262 
263   void OnAudioTransportOverheadChanged(
264       int transport_overhead_per_packet) override;
265 
266   void OnLocalSsrcUpdated(webrtc::AudioReceiveStreamInterface& stream,
267                           uint32_t local_ssrc) override;
268   void OnLocalSsrcUpdated(VideoReceiveStreamInterface& stream,
269                           uint32_t local_ssrc) override;
270   void OnLocalSsrcUpdated(FlexfecReceiveStream& stream,
271                           uint32_t local_ssrc) override;
272 
273   void OnUpdateSyncGroup(webrtc::AudioReceiveStreamInterface& stream,
274                          absl::string_view sync_group) override;
275 
276   void OnSentPacket(const rtc::SentPacket& sent_packet) override;
277 
278   // Implements TargetTransferRateObserver,
279   void OnTargetTransferRate(TargetTransferRate msg) override;
280   void OnStartRateUpdate(DataRate start_rate) override;
281 
282   // Implements BitrateAllocator::LimitObserver.
283   void OnAllocationLimitsChanged(BitrateAllocationLimits limits) override;
284 
285   void SetClientBitratePreferences(const BitrateSettings& preferences) override;
286 
287  private:
288   // Thread-compatible class that collects received packet stats and exposes
289   // them as UMA histograms on destruction.
290   class ReceiveStats {
291    public:
292     explicit ReceiveStats(Clock* clock);
293     ~ReceiveStats();
294 
295     void AddReceivedRtcpBytes(int bytes);
296     void AddReceivedAudioBytes(int bytes, webrtc::Timestamp arrival_time);
297     void AddReceivedVideoBytes(int bytes, webrtc::Timestamp arrival_time);
298 
299    private:
300     RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
301     RateCounter received_bytes_per_second_counter_
302         RTC_GUARDED_BY(sequence_checker_);
303     RateCounter received_audio_bytes_per_second_counter_
304         RTC_GUARDED_BY(sequence_checker_);
305     RateCounter received_video_bytes_per_second_counter_
306         RTC_GUARDED_BY(sequence_checker_);
307     RateCounter received_rtcp_bytes_per_second_counter_
308         RTC_GUARDED_BY(sequence_checker_);
309     absl::optional<Timestamp> first_received_rtp_audio_timestamp_
310         RTC_GUARDED_BY(sequence_checker_);
311     absl::optional<Timestamp> last_received_rtp_audio_timestamp_
312         RTC_GUARDED_BY(sequence_checker_);
313     absl::optional<Timestamp> first_received_rtp_video_timestamp_
314         RTC_GUARDED_BY(sequence_checker_);
315     absl::optional<Timestamp> last_received_rtp_video_timestamp_
316         RTC_GUARDED_BY(sequence_checker_);
317   };
318 
319   // Thread-compatible class that collects sent packet stats and exposes
320   // them as UMA histograms on destruction, provided SetFirstPacketTime was
321   // called with a non-empty packet timestamp before the destructor.
322   class SendStats {
323    public:
324     explicit SendStats(Clock* clock);
325     ~SendStats();
326 
327     void SetFirstPacketTime(absl::optional<Timestamp> first_sent_packet_time);
328     void PauseSendAndPacerBitrateCounters();
329     void AddTargetBitrateSample(uint32_t target_bitrate_bps);
330     void SetMinAllocatableRate(BitrateAllocationLimits limits);
331 
332    private:
333     RTC_NO_UNIQUE_ADDRESS SequenceChecker destructor_sequence_checker_;
334     RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
335     Clock* const clock_ RTC_GUARDED_BY(destructor_sequence_checker_);
336     AvgCounter estimated_send_bitrate_kbps_counter_
337         RTC_GUARDED_BY(sequence_checker_);
338     AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(sequence_checker_);
RTC_GUARDED_BY(sequence_checker_)339     uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(sequence_checker_){
340         0};
341     absl::optional<Timestamp> first_sent_packet_time_
342         RTC_GUARDED_BY(destructor_sequence_checker_);
343   };
344 
345   void DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet)
346       RTC_RUN_ON(network_thread_);
347   DeliveryStatus DeliverRtp(MediaType media_type,
348                             rtc::CopyOnWriteBuffer packet,
349                             int64_t packet_time_us) RTC_RUN_ON(worker_thread_);
350 
351   AudioReceiveStreamImpl* FindAudioStreamForSyncGroup(
352       absl::string_view sync_group) RTC_RUN_ON(worker_thread_);
353   void ConfigureSync(absl::string_view sync_group) RTC_RUN_ON(worker_thread_);
354 
355   void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
356                                  MediaType media_type,
357                                  bool use_send_side_bwe)
358       RTC_RUN_ON(worker_thread_);
359 
360   bool IdentifyReceivedPacket(RtpPacketReceived& packet,
361                               bool* use_send_side_bwe = nullptr);
362   bool RegisterReceiveStream(uint32_t ssrc, ReceiveStreamInterface* stream);
363   bool UnregisterReceiveStream(uint32_t ssrc);
364 
365   void UpdateAggregateNetworkState();
366 
367   // Ensure that necessary process threads are started, and any required
368   // callbacks have been registered.
369   void EnsureStarted() RTC_RUN_ON(worker_thread_);
370 
371   Clock* const clock_;
372   TaskQueueFactory* const task_queue_factory_;
373   TaskQueueBase* const worker_thread_;
374   TaskQueueBase* const network_thread_;
375   const std::unique_ptr<DecodeSynchronizer> decode_sync_;
376   RTC_NO_UNIQUE_ADDRESS SequenceChecker send_transport_sequence_checker_;
377 
378   const int num_cpu_cores_;
379   const std::unique_ptr<CallStats> call_stats_;
380   const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
381   const Call::Config config_ RTC_GUARDED_BY(worker_thread_);
382   // Maps to config_.trials, can be used from any thread via `trials()`.
383   const FieldTrialsView& trials_;
384 
385   NetworkState audio_network_state_ RTC_GUARDED_BY(worker_thread_);
386   NetworkState video_network_state_ RTC_GUARDED_BY(worker_thread_);
387   // TODO(bugs.webrtc.org/11993): Move aggregate_network_up_ over to the
388   // network thread.
389   bool aggregate_network_up_ RTC_GUARDED_BY(worker_thread_);
390 
391   // Schedules nack periodic processing on behalf of all streams.
392   NackPeriodicProcessor nack_periodic_processor_;
393 
394   // Audio, Video, and FlexFEC receive streams are owned by the client that
395   // creates them.
396   // TODO(bugs.webrtc.org/11993): Move audio_receive_streams_,
397   // video_receive_streams_ over to the network thread.
398   std::set<AudioReceiveStreamImpl*> audio_receive_streams_
399       RTC_GUARDED_BY(worker_thread_);
400   std::set<VideoReceiveStream2*> video_receive_streams_
401       RTC_GUARDED_BY(worker_thread_);
402   // TODO(bugs.webrtc.org/7135, bugs.webrtc.org/9719): Should eventually be
403   // injected at creation, with a single object in the bundled case.
404   RtpStreamReceiverController audio_receiver_controller_
405       RTC_GUARDED_BY(worker_thread_);
406   RtpStreamReceiverController video_receiver_controller_
407       RTC_GUARDED_BY(worker_thread_);
408 
409   // This extra map is used for receive processing which is
410   // independent of media type.
411 
412   RTC_NO_UNIQUE_ADDRESS SequenceChecker receive_11993_checker_;
413 
414   // TODO(bugs.webrtc.org/11993): Move receive_rtp_config_ over to the
415   // network thread.
416   std::map<uint32_t, ReceiveStreamInterface*> receive_rtp_config_
417       RTC_GUARDED_BY(&receive_11993_checker_);
418 
419   // Audio and Video send streams are owned by the client that creates them.
420   // TODO(bugs.webrtc.org/11993): `audio_send_ssrcs_` and `video_send_ssrcs_`
421   // should be accessed on the network thread.
422   std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
423       RTC_GUARDED_BY(worker_thread_);
424   std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
425       RTC_GUARDED_BY(worker_thread_);
426   std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(worker_thread_);
427   // True if `video_send_streams_` is empty, false if not. The atomic variable
428   // is used to decide UMA send statistics behavior and enables avoiding a
429   // PostTask().
430   std::atomic<bool> video_send_streams_empty_{true};
431 
432   // Each forwarder wraps an adaptation resource that was added to the call.
433   std::vector<std::unique_ptr<ResourceVideoSendStreamForwarder>>
434       adaptation_resource_forwarders_ RTC_GUARDED_BY(worker_thread_);
435 
436   using RtpStateMap = std::map<uint32_t, RtpState>;
437   RtpStateMap suspended_audio_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
438   RtpStateMap suspended_video_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
439 
440   using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
441   RtpPayloadStateMap suspended_video_payload_states_
442       RTC_GUARDED_BY(worker_thread_);
443 
444   webrtc::RtcEventLog* const event_log_;
445 
446   // TODO(bugs.webrtc.org/11993) ready to move stats access to the network
447   // thread.
448   ReceiveStats receive_stats_ RTC_GUARDED_BY(worker_thread_);
449   SendStats send_stats_ RTC_GUARDED_BY(send_transport_sequence_checker_);
450   // `last_bandwidth_bps_` and `configured_max_padding_bitrate_bps_` being
451   // atomic avoids a PostTask. The variables are used for stats gathering.
452   std::atomic<uint32_t> last_bandwidth_bps_{0};
453   std::atomic<uint32_t> configured_max_padding_bitrate_bps_{0};
454 
455   ReceiveSideCongestionController receive_side_cc_;
456   RepeatingTaskHandle receive_side_cc_periodic_task_;
457 
458   const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
459 
460   const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
461   const Timestamp start_of_call_;
462 
463   // Note that `task_safety_` needs to be at a greater scope than the task queue
464   // owned by `transport_send_` since calls might arrive on the network thread
465   // while Call is being deleted and the task queue is being torn down.
466   const ScopedTaskSafety task_safety_;
467 
468   // Caches transport_send_.get(), to avoid racing with destructor.
469   // Note that this is declared before transport_send_ to ensure that it is not
470   // invalidated until no more tasks can be running on the transport_send_ task
471   // queue.
472   // For more details on the background of this member variable, see:
473   // https://webrtc-review.googlesource.com/c/src/+/63023/9/call/call.cc
474   // https://bugs.chromium.org/p/chromium/issues/detail?id=992640
475   RtpTransportControllerSendInterface* const transport_send_ptr_
476       RTC_GUARDED_BY(send_transport_sequence_checker_);
477   // Declared last since it will issue callbacks from a task queue. Declaring it
478   // last ensures that it is destroyed first and any running tasks are finished.
479   const std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
480 
481   bool is_started_ RTC_GUARDED_BY(worker_thread_) = false;
482 
483   // Sequence checker for outgoing network traffic. Could be the network thread.
484   // Could also be a pacer owned thread or TQ such as the TaskQueuePacedSender.
485   RTC_NO_UNIQUE_ADDRESS SequenceChecker sent_packet_sequence_checker_;
486   absl::optional<rtc::SentPacket> last_sent_packet_
487       RTC_GUARDED_BY(sent_packet_sequence_checker_);
488 };
489 }  // namespace internal
490 
ToString(int64_t time_ms) const491 std::string Call::Stats::ToString(int64_t time_ms) const {
492   char buf[1024];
493   rtc::SimpleStringBuilder ss(buf);
494   ss << "Call stats: " << time_ms << ", {";
495   ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
496   ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
497   ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
498   ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
499   ss << "rtt_ms: " << rtt_ms;
500   ss << '}';
501   return ss.str();
502 }
503 
Create(const Call::Config & config)504 Call* Call::Create(const Call::Config& config) {
505   Clock* clock = Clock::GetRealTimeClock();
506   return Create(config, clock,
507                 RtpTransportControllerSendFactory().Create(
508                     config.ExtractTransportConfig(), clock));
509 }
510 
Create(const Call::Config & config,Clock * clock,std::unique_ptr<RtpTransportControllerSendInterface> transportControllerSend)511 Call* Call::Create(const Call::Config& config,
512                    Clock* clock,
513                    std::unique_ptr<RtpTransportControllerSendInterface>
514                        transportControllerSend) {
515   RTC_DCHECK(config.task_queue_factory);
516   return new internal::Call(clock, config, std::move(transportControllerSend),
517                             config.task_queue_factory);
518 }
519 
520 // This method here to avoid subclasses has to implement this method.
521 // Call perf test will use Internal::Call::CreateVideoSendStream() to inject
522 // FecController.
CreateVideoSendStream(VideoSendStream::Config config,VideoEncoderConfig encoder_config,std::unique_ptr<FecController> fec_controller)523 VideoSendStream* Call::CreateVideoSendStream(
524     VideoSendStream::Config config,
525     VideoEncoderConfig encoder_config,
526     std::unique_ptr<FecController> fec_controller) {
527   return nullptr;
528 }
529 
530 namespace internal {
531 
ReceiveStats(Clock * clock)532 Call::ReceiveStats::ReceiveStats(Clock* clock)
533     : received_bytes_per_second_counter_(clock, nullptr, false),
534       received_audio_bytes_per_second_counter_(clock, nullptr, false),
535       received_video_bytes_per_second_counter_(clock, nullptr, false),
536       received_rtcp_bytes_per_second_counter_(clock, nullptr, false) {
537   sequence_checker_.Detach();
538 }
539 
AddReceivedRtcpBytes(int bytes)540 void Call::ReceiveStats::AddReceivedRtcpBytes(int bytes) {
541   RTC_DCHECK_RUN_ON(&sequence_checker_);
542   if (received_bytes_per_second_counter_.HasSample()) {
543     // First RTP packet has been received.
544     received_bytes_per_second_counter_.Add(static_cast<int>(bytes));
545     received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(bytes));
546   }
547 }
548 
AddReceivedAudioBytes(int bytes,webrtc::Timestamp arrival_time)549 void Call::ReceiveStats::AddReceivedAudioBytes(int bytes,
550                                                webrtc::Timestamp arrival_time) {
551   RTC_DCHECK_RUN_ON(&sequence_checker_);
552   received_bytes_per_second_counter_.Add(bytes);
553   received_audio_bytes_per_second_counter_.Add(bytes);
554   if (!first_received_rtp_audio_timestamp_)
555     first_received_rtp_audio_timestamp_ = arrival_time;
556   last_received_rtp_audio_timestamp_ = arrival_time;
557 }
558 
AddReceivedVideoBytes(int bytes,webrtc::Timestamp arrival_time)559 void Call::ReceiveStats::AddReceivedVideoBytes(int bytes,
560                                                webrtc::Timestamp arrival_time) {
561   RTC_DCHECK_RUN_ON(&sequence_checker_);
562   received_bytes_per_second_counter_.Add(bytes);
563   received_video_bytes_per_second_counter_.Add(bytes);
564   if (!first_received_rtp_video_timestamp_)
565     first_received_rtp_video_timestamp_ = arrival_time;
566   last_received_rtp_video_timestamp_ = arrival_time;
567 }
568 
~ReceiveStats()569 Call::ReceiveStats::~ReceiveStats() {
570   RTC_DCHECK_RUN_ON(&sequence_checker_);
571   if (first_received_rtp_audio_timestamp_) {
572     RTC_HISTOGRAM_COUNTS_100000(
573         "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
574         (*last_received_rtp_audio_timestamp_ -
575          *first_received_rtp_audio_timestamp_)
576             .seconds());
577   }
578   if (first_received_rtp_video_timestamp_) {
579     RTC_HISTOGRAM_COUNTS_100000(
580         "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
581         (*last_received_rtp_video_timestamp_ -
582          *first_received_rtp_video_timestamp_)
583             .seconds());
584   }
585   const int kMinRequiredPeriodicSamples = 5;
586   AggregatedStats video_bytes_per_sec =
587       received_video_bytes_per_second_counter_.GetStats();
588   if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
589     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
590                                 video_bytes_per_sec.average * 8 / 1000);
591     RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
592                      << video_bytes_per_sec.ToStringWithMultiplier(8);
593   }
594   AggregatedStats audio_bytes_per_sec =
595       received_audio_bytes_per_second_counter_.GetStats();
596   if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
597     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
598                                 audio_bytes_per_sec.average * 8 / 1000);
599     RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
600                      << audio_bytes_per_sec.ToStringWithMultiplier(8);
601   }
602   AggregatedStats rtcp_bytes_per_sec =
603       received_rtcp_bytes_per_second_counter_.GetStats();
604   if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
605     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
606                                 rtcp_bytes_per_sec.average * 8);
607     RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
608                      << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
609   }
610   AggregatedStats recv_bytes_per_sec =
611       received_bytes_per_second_counter_.GetStats();
612   if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
613     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
614                                 recv_bytes_per_sec.average * 8 / 1000);
615     RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
616                      << recv_bytes_per_sec.ToStringWithMultiplier(8);
617   }
618 }
619 
SendStats(Clock * clock)620 Call::SendStats::SendStats(Clock* clock)
621     : clock_(clock),
622       estimated_send_bitrate_kbps_counter_(clock, nullptr, true),
623       pacer_bitrate_kbps_counter_(clock, nullptr, true) {
624   destructor_sequence_checker_.Detach();
625   sequence_checker_.Detach();
626 }
627 
~SendStats()628 Call::SendStats::~SendStats() {
629   RTC_DCHECK_RUN_ON(&destructor_sequence_checker_);
630   if (!first_sent_packet_time_)
631     return;
632 
633   TimeDelta elapsed = clock_->CurrentTime() - *first_sent_packet_time_;
634   if (elapsed.seconds() < metrics::kMinRunTimeInSeconds)
635     return;
636 
637   const int kMinRequiredPeriodicSamples = 5;
638   AggregatedStats send_bitrate_stats =
639       estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
640   if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
641     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
642                                 send_bitrate_stats.average);
643     RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
644                      << send_bitrate_stats.ToString();
645   }
646   AggregatedStats pacer_bitrate_stats =
647       pacer_bitrate_kbps_counter_.ProcessAndGetStats();
648   if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
649     RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
650                                 pacer_bitrate_stats.average);
651     RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
652                      << pacer_bitrate_stats.ToString();
653   }
654 }
655 
SetFirstPacketTime(absl::optional<Timestamp> first_sent_packet_time)656 void Call::SendStats::SetFirstPacketTime(
657     absl::optional<Timestamp> first_sent_packet_time) {
658   RTC_DCHECK_RUN_ON(&destructor_sequence_checker_);
659   first_sent_packet_time_ = first_sent_packet_time;
660 }
661 
PauseSendAndPacerBitrateCounters()662 void Call::SendStats::PauseSendAndPacerBitrateCounters() {
663   RTC_DCHECK_RUN_ON(&sequence_checker_);
664   estimated_send_bitrate_kbps_counter_.ProcessAndPause();
665   pacer_bitrate_kbps_counter_.ProcessAndPause();
666 }
667 
AddTargetBitrateSample(uint32_t target_bitrate_bps)668 void Call::SendStats::AddTargetBitrateSample(uint32_t target_bitrate_bps) {
669   RTC_DCHECK_RUN_ON(&sequence_checker_);
670   estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
671   // Pacer bitrate may be higher than bitrate estimate if enforcing min
672   // bitrate.
673   uint32_t pacer_bitrate_bps =
674       std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
675   pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
676 }
677 
SetMinAllocatableRate(BitrateAllocationLimits limits)678 void Call::SendStats::SetMinAllocatableRate(BitrateAllocationLimits limits) {
679   RTC_DCHECK_RUN_ON(&sequence_checker_);
680   min_allocated_send_bitrate_bps_ = limits.min_allocatable_rate.bps();
681 }
682 
Call(Clock * clock,const Call::Config & config,std::unique_ptr<RtpTransportControllerSendInterface> transport_send,TaskQueueFactory * task_queue_factory)683 Call::Call(Clock* clock,
684            const Call::Config& config,
685            std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
686            TaskQueueFactory* task_queue_factory)
687     : clock_(clock),
688       task_queue_factory_(task_queue_factory),
689       worker_thread_(GetCurrentTaskQueueOrThread()),
690       // If `network_task_queue_` was set to nullptr, network related calls
691       // must be made on `worker_thread_` (i.e. they're one and the same).
692       network_thread_(config.network_task_queue_ ? config.network_task_queue_
693                                                  : worker_thread_),
694       decode_sync_(config.metronome
695                        ? std::make_unique<DecodeSynchronizer>(clock_,
696                                                               config.metronome,
697                                                               worker_thread_)
698                        : nullptr),
699       num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
700       call_stats_(new CallStats(clock_, worker_thread_)),
701       bitrate_allocator_(new BitrateAllocator(this)),
702       config_(config),
703       trials_(*config.trials),
704       audio_network_state_(kNetworkDown),
705       video_network_state_(kNetworkDown),
706       aggregate_network_up_(false),
707       event_log_(config.event_log),
708       receive_stats_(clock_),
709       send_stats_(clock_),
710       receive_side_cc_(clock,
711                        absl::bind_front(&PacketRouter::SendCombinedRtcpPacket,
712                                         transport_send->packet_router()),
713                        absl::bind_front(&PacketRouter::SendRemb,
714                                         transport_send->packet_router()),
715                        /*network_state_estimator=*/nullptr),
716       receive_time_calculator_(
717           ReceiveTimeCalculator::CreateFromFieldTrial(*config.trials)),
718       video_send_delay_stats_(new SendDelayStats(clock_)),
719       start_of_call_(clock_->CurrentTime()),
720       transport_send_ptr_(transport_send.get()),
721       transport_send_(std::move(transport_send)) {
722   RTC_DCHECK(config.event_log != nullptr);
723   RTC_DCHECK(config.trials != nullptr);
724   RTC_DCHECK(network_thread_);
725   RTC_DCHECK(worker_thread_->IsCurrent());
726 
727   receive_11993_checker_.Detach();
728   send_transport_sequence_checker_.Detach();
729   sent_packet_sequence_checker_.Detach();
730 
731   // Do not remove this call; it is here to convince the compiler that the
732   // WebRTC source timestamp string needs to be in the final binary.
733   LoadWebRTCVersionInRegister();
734 
735   call_stats_->RegisterStatsObserver(&receive_side_cc_);
736 
737   ReceiveSideCongestionController* receive_side_cc = &receive_side_cc_;
738   receive_side_cc_periodic_task_ = RepeatingTaskHandle::Start(
739       worker_thread_,
740       [receive_side_cc] { return receive_side_cc->MaybeProcess(); },
741       TaskQueueBase::DelayPrecision::kLow, clock_);
742 }
743 
~Call()744 Call::~Call() {
745   RTC_DCHECK_RUN_ON(worker_thread_);
746 
747   RTC_CHECK(audio_send_ssrcs_.empty());
748   RTC_CHECK(video_send_ssrcs_.empty());
749   RTC_CHECK(video_send_streams_.empty());
750   RTC_CHECK(audio_receive_streams_.empty());
751   RTC_CHECK(video_receive_streams_.empty());
752 
753   receive_side_cc_periodic_task_.Stop();
754   call_stats_->DeregisterStatsObserver(&receive_side_cc_);
755   send_stats_.SetFirstPacketTime(transport_send_->GetFirstPacketTime());
756 
757   RTC_HISTOGRAM_COUNTS_100000(
758       "WebRTC.Call.LifetimeInSeconds",
759       (clock_->CurrentTime() - start_of_call_).seconds());
760 }
761 
EnsureStarted()762 void Call::EnsureStarted() {
763   if (is_started_) {
764     return;
765   }
766   is_started_ = true;
767 
768   call_stats_->EnsureStarted();
769 
770   // This call seems to kick off a number of things, so probably better left
771   // off being kicked off on request rather than in the ctor.
772   transport_send_->RegisterTargetTransferRateObserver(this);
773 
774   transport_send_->EnsureStarted();
775 }
776 
SetClientBitratePreferences(const BitrateSettings & preferences)777 void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
778   RTC_DCHECK_RUN_ON(worker_thread_);
779   GetTransportControllerSend()->SetClientBitratePreferences(preferences);
780 }
781 
Receiver()782 PacketReceiver* Call::Receiver() {
783   return this;
784 }
785 
CreateAudioSendStream(const webrtc::AudioSendStream::Config & config)786 webrtc::AudioSendStream* Call::CreateAudioSendStream(
787     const webrtc::AudioSendStream::Config& config) {
788   TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
789   RTC_DCHECK_RUN_ON(worker_thread_);
790 
791   EnsureStarted();
792 
793   // Stream config is logged in AudioSendStream::ConfigureStream, as it may
794   // change during the stream's lifetime.
795   absl::optional<RtpState> suspended_rtp_state;
796   {
797     const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
798     if (iter != suspended_audio_send_ssrcs_.end()) {
799       suspended_rtp_state.emplace(iter->second);
800     }
801   }
802 
803   AudioSendStream* send_stream = new AudioSendStream(
804       clock_, config, config_.audio_state, task_queue_factory_,
805       transport_send_.get(), bitrate_allocator_.get(), event_log_,
806       call_stats_->AsRtcpRttStats(), suspended_rtp_state, trials());
807   RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
808              audio_send_ssrcs_.end());
809   audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
810 
811   // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
812   // UpdateAggregateNetworkState asynchronously on the network thread.
813   for (AudioReceiveStreamImpl* stream : audio_receive_streams_) {
814     if (stream->local_ssrc() == config.rtp.ssrc) {
815       stream->AssociateSendStream(send_stream);
816     }
817   }
818 
819   UpdateAggregateNetworkState();
820 
821   return send_stream;
822 }
823 
DestroyAudioSendStream(webrtc::AudioSendStream * send_stream)824 void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
825   TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
826   RTC_DCHECK_RUN_ON(worker_thread_);
827   RTC_DCHECK(send_stream != nullptr);
828 
829   send_stream->Stop();
830 
831   const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
832   webrtc::internal::AudioSendStream* audio_send_stream =
833       static_cast<webrtc::internal::AudioSendStream*>(send_stream);
834   suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
835 
836   size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
837   RTC_DCHECK_EQ(1, num_deleted);
838 
839   // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
840   // UpdateAggregateNetworkState asynchronously on the network thread.
841   for (AudioReceiveStreamImpl* stream : audio_receive_streams_) {
842     if (stream->local_ssrc() == ssrc) {
843       stream->AssociateSendStream(nullptr);
844     }
845   }
846 
847   UpdateAggregateNetworkState();
848 
849   delete send_stream;
850 }
851 
CreateAudioReceiveStream(const webrtc::AudioReceiveStreamInterface::Config & config)852 webrtc::AudioReceiveStreamInterface* Call::CreateAudioReceiveStream(
853     const webrtc::AudioReceiveStreamInterface::Config& config) {
854   TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
855   RTC_DCHECK_RUN_ON(worker_thread_);
856   EnsureStarted();
857   event_log_->Log(std::make_unique<RtcEventAudioReceiveStreamConfig>(
858       CreateRtcLogStreamConfig(config)));
859 
860   AudioReceiveStreamImpl* receive_stream = new AudioReceiveStreamImpl(
861       clock_, transport_send_->packet_router(), config_.neteq_factory, config,
862       config_.audio_state, event_log_);
863   audio_receive_streams_.insert(receive_stream);
864 
865   // TODO(bugs.webrtc.org/11993): Make the registration on the network thread
866   // (asynchronously). The registration and `audio_receiver_controller_` need
867   // to live on the network thread.
868   receive_stream->RegisterWithTransport(&audio_receiver_controller_);
869 
870   // TODO(bugs.webrtc.org/11993): Update the below on the network thread.
871   // We could possibly set up the audio_receiver_controller_ association up
872   // as part of the async setup.
873   RegisterReceiveStream(config.rtp.remote_ssrc, receive_stream);
874 
875   ConfigureSync(config.sync_group);
876 
877   auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
878   if (it != audio_send_ssrcs_.end()) {
879     receive_stream->AssociateSendStream(it->second);
880   }
881 
882   UpdateAggregateNetworkState();
883   return receive_stream;
884 }
885 
DestroyAudioReceiveStream(webrtc::AudioReceiveStreamInterface * receive_stream)886 void Call::DestroyAudioReceiveStream(
887     webrtc::AudioReceiveStreamInterface* receive_stream) {
888   TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
889   RTC_DCHECK_RUN_ON(worker_thread_);
890   RTC_DCHECK(receive_stream != nullptr);
891   webrtc::AudioReceiveStreamImpl* audio_receive_stream =
892       static_cast<webrtc::AudioReceiveStreamImpl*>(receive_stream);
893 
894   // TODO(bugs.webrtc.org/11993): Access the map, rtp config, call ConfigureSync
895   // and UpdateAggregateNetworkState on the network thread. The call to
896   // `UnregisterFromTransport` should also happen on the network thread.
897   audio_receive_stream->UnregisterFromTransport();
898 
899   uint32_t ssrc = audio_receive_stream->remote_ssrc();
900   receive_side_cc_.RemoveStream(ssrc);
901 
902   audio_receive_streams_.erase(audio_receive_stream);
903 
904   // After calling erase(), call ConfigureSync. This will clear associated
905   // video streams or associate them with a different audio stream if one exists
906   // for this sync_group.
907   ConfigureSync(audio_receive_stream->sync_group());
908 
909   UnregisterReceiveStream(ssrc);
910 
911   UpdateAggregateNetworkState();
912   // TODO(bugs.webrtc.org/11993): Consider if deleting `audio_receive_stream`
913   // on the network thread would be better or if we'd need to tear down the
914   // state in two phases.
915   delete audio_receive_stream;
916 }
917 
918 // This method can be used for Call tests with external fec controller factory.
CreateVideoSendStream(webrtc::VideoSendStream::Config config,VideoEncoderConfig encoder_config,std::unique_ptr<FecController> fec_controller)919 webrtc::VideoSendStream* Call::CreateVideoSendStream(
920     webrtc::VideoSendStream::Config config,
921     VideoEncoderConfig encoder_config,
922     std::unique_ptr<FecController> fec_controller) {
923   TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
924   RTC_DCHECK_RUN_ON(worker_thread_);
925 
926   EnsureStarted();
927 
928   video_send_delay_stats_->AddSsrcs(config);
929   for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
930        ++ssrc_index) {
931     event_log_->Log(std::make_unique<RtcEventVideoSendStreamConfig>(
932         CreateRtcLogStreamConfig(config, ssrc_index)));
933   }
934 
935   // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
936   // the call has already started.
937   // Copy ssrcs from `config` since `config` is moved.
938   std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
939 
940   VideoSendStream* send_stream = new VideoSendStream(
941       clock_, num_cpu_cores_, task_queue_factory_, network_thread_,
942       call_stats_->AsRtcpRttStats(), transport_send_.get(),
943       bitrate_allocator_.get(), video_send_delay_stats_.get(), event_log_,
944       std::move(config), std::move(encoder_config), suspended_video_send_ssrcs_,
945       suspended_video_payload_states_, std::move(fec_controller),
946       *config_.trials);
947 
948   for (uint32_t ssrc : ssrcs) {
949     RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
950     video_send_ssrcs_[ssrc] = send_stream;
951   }
952   video_send_streams_.insert(send_stream);
953   video_send_streams_empty_.store(false, std::memory_order_relaxed);
954 
955   // Forward resources that were previously added to the call to the new stream.
956   for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
957     resource_forwarder->OnCreateVideoSendStream(send_stream);
958   }
959 
960   UpdateAggregateNetworkState();
961 
962   return send_stream;
963 }
964 
CreateVideoSendStream(webrtc::VideoSendStream::Config config,VideoEncoderConfig encoder_config)965 webrtc::VideoSendStream* Call::CreateVideoSendStream(
966     webrtc::VideoSendStream::Config config,
967     VideoEncoderConfig encoder_config) {
968   RTC_DCHECK_RUN_ON(worker_thread_);
969   if (config_.fec_controller_factory) {
970     RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
971   }
972   std::unique_ptr<FecController> fec_controller =
973       config_.fec_controller_factory
974           ? config_.fec_controller_factory->CreateFecController()
975           : std::make_unique<FecControllerDefault>(clock_);
976   return CreateVideoSendStream(std::move(config), std::move(encoder_config),
977                                std::move(fec_controller));
978 }
979 
DestroyVideoSendStream(webrtc::VideoSendStream * send_stream)980 void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
981   TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
982   RTC_DCHECK(send_stream != nullptr);
983   RTC_DCHECK_RUN_ON(worker_thread_);
984 
985   VideoSendStream* send_stream_impl =
986       static_cast<VideoSendStream*>(send_stream);
987 
988   auto it = video_send_ssrcs_.begin();
989   while (it != video_send_ssrcs_.end()) {
990     if (it->second == static_cast<VideoSendStream*>(send_stream)) {
991       send_stream_impl = it->second;
992       video_send_ssrcs_.erase(it++);
993     } else {
994       ++it;
995     }
996   }
997 
998   // Stop forwarding resources to the stream being destroyed.
999   for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
1000     resource_forwarder->OnDestroyVideoSendStream(send_stream_impl);
1001   }
1002   video_send_streams_.erase(send_stream_impl);
1003   if (video_send_streams_.empty())
1004     video_send_streams_empty_.store(true, std::memory_order_relaxed);
1005 
1006   VideoSendStream::RtpStateMap rtp_states;
1007   VideoSendStream::RtpPayloadStateMap rtp_payload_states;
1008   send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
1009                                                    &rtp_payload_states);
1010   for (const auto& kv : rtp_states) {
1011     suspended_video_send_ssrcs_[kv.first] = kv.second;
1012   }
1013   for (const auto& kv : rtp_payload_states) {
1014     suspended_video_payload_states_[kv.first] = kv.second;
1015   }
1016 
1017   UpdateAggregateNetworkState();
1018   // TODO(tommi): consider deleting on the same thread as runs
1019   // StopPermanentlyAndGetRtpStates.
1020   delete send_stream_impl;
1021 }
1022 
CreateVideoReceiveStream(webrtc::VideoReceiveStreamInterface::Config configuration)1023 webrtc::VideoReceiveStreamInterface* Call::CreateVideoReceiveStream(
1024     webrtc::VideoReceiveStreamInterface::Config configuration) {
1025   TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
1026   RTC_DCHECK_RUN_ON(worker_thread_);
1027 
1028   receive_side_cc_.SetSendPeriodicFeedback(
1029       SendPeriodicFeedback(configuration.rtp.extensions));
1030 
1031   EnsureStarted();
1032 
1033   event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>(
1034       CreateRtcLogStreamConfig(configuration)));
1035 
1036   // TODO(bugs.webrtc.org/11993): Move the registration between `receive_stream`
1037   // and `video_receiver_controller_` out of VideoReceiveStream2 construction
1038   // and set it up asynchronously on the network thread (the registration and
1039   // `video_receiver_controller_` need to live on the network thread).
1040   // TODO(crbug.com/1381982): Re-enable decode synchronizer once the Chromium
1041   // API has adapted to the new Metronome interface.
1042   VideoReceiveStream2* receive_stream = new VideoReceiveStream2(
1043       task_queue_factory_, this, num_cpu_cores_,
1044       transport_send_->packet_router(), std::move(configuration),
1045       call_stats_.get(), clock_, std::make_unique<VCMTiming>(clock_, trials()),
1046       &nack_periodic_processor_, decode_sync_.get(), event_log_);
1047   // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network
1048   // thread.
1049   receive_stream->RegisterWithTransport(&video_receiver_controller_);
1050 
1051   if (receive_stream->rtx_ssrc()) {
1052     // We record identical config for the rtx stream as for the main
1053     // stream. Since the transport_send_cc negotiation is per payload
1054     // type, we may get an incorrect value for the rtx stream, but
1055     // that is unlikely to matter in practice.
1056     RegisterReceiveStream(receive_stream->rtx_ssrc(), receive_stream);
1057   }
1058   RegisterReceiveStream(receive_stream->remote_ssrc(), receive_stream);
1059   video_receive_streams_.insert(receive_stream);
1060 
1061   ConfigureSync(receive_stream->sync_group());
1062 
1063   receive_stream->SignalNetworkState(video_network_state_);
1064   UpdateAggregateNetworkState();
1065   return receive_stream;
1066 }
1067 
DestroyVideoReceiveStream(webrtc::VideoReceiveStreamInterface * receive_stream)1068 void Call::DestroyVideoReceiveStream(
1069     webrtc::VideoReceiveStreamInterface* receive_stream) {
1070   TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
1071   RTC_DCHECK_RUN_ON(worker_thread_);
1072   RTC_DCHECK(receive_stream != nullptr);
1073   VideoReceiveStream2* receive_stream_impl =
1074       static_cast<VideoReceiveStream2*>(receive_stream);
1075   // TODO(bugs.webrtc.org/11993): Unregister on the network thread.
1076   receive_stream_impl->UnregisterFromTransport();
1077 
1078   // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
1079   // separate SSRC there can be either one or two.
1080   UnregisterReceiveStream(receive_stream_impl->remote_ssrc());
1081 
1082   if (receive_stream_impl->rtx_ssrc()) {
1083     UnregisterReceiveStream(receive_stream_impl->rtx_ssrc());
1084   }
1085   video_receive_streams_.erase(receive_stream_impl);
1086   ConfigureSync(receive_stream_impl->sync_group());
1087 
1088   receive_side_cc_.RemoveStream(receive_stream_impl->remote_ssrc());
1089 
1090   UpdateAggregateNetworkState();
1091   delete receive_stream_impl;
1092 }
1093 
CreateFlexfecReceiveStream(const FlexfecReceiveStream::Config config)1094 FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
1095     const FlexfecReceiveStream::Config config) {
1096   TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
1097   RTC_DCHECK_RUN_ON(worker_thread_);
1098 
1099   // Unlike the video and audio receive streams, FlexfecReceiveStream implements
1100   // RtpPacketSinkInterface itself, and hence its constructor passes its `this`
1101   // pointer to video_receiver_controller_->CreateStream(). Calling the
1102   // constructor while on the worker thread ensures that we don't call
1103   // OnRtpPacket until the constructor is finished and the object is
1104   // in a valid state, since OnRtpPacket runs on the same thread.
1105   FlexfecReceiveStreamImpl* receive_stream = new FlexfecReceiveStreamImpl(
1106       clock_, std::move(config), this, call_stats_->AsRtcpRttStats());
1107 
1108   // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network
1109   // thread.
1110   receive_stream->RegisterWithTransport(&video_receiver_controller_);
1111   RegisterReceiveStream(receive_stream->remote_ssrc(), receive_stream);
1112 
1113   // TODO(brandtr): Store config in RtcEventLog here.
1114 
1115   return receive_stream;
1116 }
1117 
DestroyFlexfecReceiveStream(FlexfecReceiveStream * receive_stream)1118 void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
1119   TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
1120   RTC_DCHECK_RUN_ON(worker_thread_);
1121 
1122   FlexfecReceiveStreamImpl* receive_stream_impl =
1123       static_cast<FlexfecReceiveStreamImpl*>(receive_stream);
1124   // TODO(bugs.webrtc.org/11993): Unregister on the network thread.
1125   receive_stream_impl->UnregisterFromTransport();
1126 
1127   auto ssrc = receive_stream_impl->remote_ssrc();
1128   UnregisterReceiveStream(ssrc);
1129 
1130   // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1131   // destroyed.
1132   receive_side_cc_.RemoveStream(ssrc);
1133 
1134   delete receive_stream_impl;
1135 }
1136 
AddAdaptationResource(rtc::scoped_refptr<Resource> resource)1137 void Call::AddAdaptationResource(rtc::scoped_refptr<Resource> resource) {
1138   RTC_DCHECK_RUN_ON(worker_thread_);
1139   adaptation_resource_forwarders_.push_back(
1140       std::make_unique<ResourceVideoSendStreamForwarder>(resource));
1141   const auto& resource_forwarder = adaptation_resource_forwarders_.back();
1142   for (VideoSendStream* send_stream : video_send_streams_) {
1143     resource_forwarder->OnCreateVideoSendStream(send_stream);
1144   }
1145 }
1146 
GetTransportControllerSend()1147 RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
1148   return transport_send_.get();
1149 }
1150 
GetStats() const1151 Call::Stats Call::GetStats() const {
1152   RTC_DCHECK_RUN_ON(worker_thread_);
1153 
1154   Stats stats;
1155   // TODO(srte): It is unclear if we only want to report queues if network is
1156   // available.
1157   stats.pacer_delay_ms =
1158       aggregate_network_up_ ? transport_send_->GetPacerQueuingDelayMs() : 0;
1159 
1160   stats.rtt_ms = call_stats_->LastProcessedRtt();
1161 
1162   // Fetch available send/receive bitrates.
1163   stats.recv_bandwidth_bps = receive_side_cc_.LatestReceiveSideEstimate().bps();
1164   stats.send_bandwidth_bps =
1165       last_bandwidth_bps_.load(std::memory_order_relaxed);
1166   stats.max_padding_bitrate_bps =
1167       configured_max_padding_bitrate_bps_.load(std::memory_order_relaxed);
1168 
1169   return stats;
1170 }
1171 
trials() const1172 const FieldTrialsView& Call::trials() const {
1173   return trials_;
1174 }
1175 
network_thread() const1176 TaskQueueBase* Call::network_thread() const {
1177   return network_thread_;
1178 }
1179 
worker_thread() const1180 TaskQueueBase* Call::worker_thread() const {
1181   return worker_thread_;
1182 }
1183 
SignalChannelNetworkState(MediaType media,NetworkState state)1184 void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
1185   RTC_DCHECK_RUN_ON(network_thread_);
1186   RTC_DCHECK(media == MediaType::AUDIO || media == MediaType::VIDEO);
1187 
1188   auto closure = [this, media, state]() {
1189     // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1190     RTC_DCHECK_RUN_ON(worker_thread_);
1191     if (media == MediaType::AUDIO) {
1192       audio_network_state_ = state;
1193     } else {
1194       RTC_DCHECK_EQ(media, MediaType::VIDEO);
1195       video_network_state_ = state;
1196     }
1197 
1198     // TODO(tommi): Is it necessary to always do this, including if there
1199     // was no change in state?
1200     UpdateAggregateNetworkState();
1201 
1202     // TODO(tommi): Is it right to do this if media == AUDIO?
1203     for (VideoReceiveStream2* video_receive_stream : video_receive_streams_) {
1204       video_receive_stream->SignalNetworkState(video_network_state_);
1205     }
1206   };
1207 
1208   if (network_thread_ == worker_thread_) {
1209     closure();
1210   } else {
1211     // TODO(bugs.webrtc.org/11993): Remove workaround when we no longer need to
1212     // post to the worker thread.
1213     worker_thread_->PostTask(SafeTask(task_safety_.flag(), std::move(closure)));
1214   }
1215 }
1216 
OnAudioTransportOverheadChanged(int transport_overhead_per_packet)1217 void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1218   RTC_DCHECK_RUN_ON(network_thread_);
1219   worker_thread_->PostTask(
1220       SafeTask(task_safety_.flag(), [this, transport_overhead_per_packet]() {
1221         // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1222         RTC_DCHECK_RUN_ON(worker_thread_);
1223         for (auto& kv : audio_send_ssrcs_) {
1224           kv.second->SetTransportOverhead(transport_overhead_per_packet);
1225         }
1226       }));
1227 }
1228 
UpdateAggregateNetworkState()1229 void Call::UpdateAggregateNetworkState() {
1230   // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1231   // RTC_DCHECK_RUN_ON(network_thread_);
1232 
1233   RTC_DCHECK_RUN_ON(worker_thread_);
1234 
1235   bool have_audio =
1236       !audio_send_ssrcs_.empty() || !audio_receive_streams_.empty();
1237   bool have_video =
1238       !video_send_ssrcs_.empty() || !video_receive_streams_.empty();
1239 
1240   bool aggregate_network_up =
1241       ((have_video && video_network_state_ == kNetworkUp) ||
1242        (have_audio && audio_network_state_ == kNetworkUp));
1243 
1244   if (aggregate_network_up != aggregate_network_up_) {
1245     RTC_LOG(LS_INFO)
1246         << "UpdateAggregateNetworkState: aggregate_state change to "
1247         << (aggregate_network_up ? "up" : "down");
1248   } else {
1249     RTC_LOG(LS_VERBOSE)
1250         << "UpdateAggregateNetworkState: aggregate_state remains at "
1251         << (aggregate_network_up ? "up" : "down");
1252   }
1253   aggregate_network_up_ = aggregate_network_up;
1254 
1255   transport_send_->OnNetworkAvailability(aggregate_network_up);
1256 }
1257 
OnLocalSsrcUpdated(webrtc::AudioReceiveStreamInterface & stream,uint32_t local_ssrc)1258 void Call::OnLocalSsrcUpdated(webrtc::AudioReceiveStreamInterface& stream,
1259                               uint32_t local_ssrc) {
1260   RTC_DCHECK_RUN_ON(worker_thread_);
1261   webrtc::AudioReceiveStreamImpl& receive_stream =
1262       static_cast<webrtc::AudioReceiveStreamImpl&>(stream);
1263 
1264   receive_stream.SetLocalSsrc(local_ssrc);
1265   auto it = audio_send_ssrcs_.find(local_ssrc);
1266   receive_stream.AssociateSendStream(it != audio_send_ssrcs_.end() ? it->second
1267                                                                    : nullptr);
1268 }
1269 
OnLocalSsrcUpdated(VideoReceiveStreamInterface & stream,uint32_t local_ssrc)1270 void Call::OnLocalSsrcUpdated(VideoReceiveStreamInterface& stream,
1271                               uint32_t local_ssrc) {
1272   RTC_DCHECK_RUN_ON(worker_thread_);
1273   static_cast<VideoReceiveStream2&>(stream).SetLocalSsrc(local_ssrc);
1274 }
1275 
OnLocalSsrcUpdated(FlexfecReceiveStream & stream,uint32_t local_ssrc)1276 void Call::OnLocalSsrcUpdated(FlexfecReceiveStream& stream,
1277                               uint32_t local_ssrc) {
1278   RTC_DCHECK_RUN_ON(worker_thread_);
1279   static_cast<FlexfecReceiveStreamImpl&>(stream).SetLocalSsrc(local_ssrc);
1280 }
1281 
OnUpdateSyncGroup(webrtc::AudioReceiveStreamInterface & stream,absl::string_view sync_group)1282 void Call::OnUpdateSyncGroup(webrtc::AudioReceiveStreamInterface& stream,
1283                              absl::string_view sync_group) {
1284   RTC_DCHECK_RUN_ON(worker_thread_);
1285   webrtc::AudioReceiveStreamImpl& receive_stream =
1286       static_cast<webrtc::AudioReceiveStreamImpl&>(stream);
1287   receive_stream.SetSyncGroup(sync_group);
1288   ConfigureSync(sync_group);
1289 }
1290 
OnSentPacket(const rtc::SentPacket & sent_packet)1291 void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
1292   RTC_DCHECK_RUN_ON(&sent_packet_sequence_checker_);
1293   // When bundling is in effect, multiple senders may be sharing the same
1294   // transport. It means every |sent_packet| will be multiply notified from
1295   // different channels, WebRtcVoiceMediaChannel or WebRtcVideoChannel. Record
1296   // |last_sent_packet_| to deduplicate redundant notifications to downstream.
1297   // (https://crbug.com/webrtc/13437): Pass all packets without a |packet_id| to
1298   // downstream.
1299   if (last_sent_packet_.has_value() && last_sent_packet_->packet_id != -1 &&
1300       last_sent_packet_->packet_id == sent_packet.packet_id &&
1301       last_sent_packet_->send_time_ms == sent_packet.send_time_ms) {
1302     return;
1303   }
1304   last_sent_packet_ = sent_packet;
1305 
1306   // In production and with most tests, this method will be called on the
1307   // network thread. However some test classes such as DirectTransport don't
1308   // incorporate a network thread. This means that tests for RtpSenderEgress
1309   // and ModuleRtpRtcpImpl2 that use DirectTransport, will call this method
1310   // on a ProcessThread. This is alright as is since we forward the call to
1311   // implementations that either just do a PostTask or use locking.
1312   video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1313                                         clock_->TimeInMilliseconds());
1314   transport_send_->OnSentPacket(sent_packet);
1315 }
1316 
OnStartRateUpdate(DataRate start_rate)1317 void Call::OnStartRateUpdate(DataRate start_rate) {
1318   RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_);
1319   bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1320 }
1321 
OnTargetTransferRate(TargetTransferRate msg)1322 void Call::OnTargetTransferRate(TargetTransferRate msg) {
1323   RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_);
1324 
1325   uint32_t target_bitrate_bps = msg.target_rate.bps();
1326   // For controlling the rate of feedback messages.
1327   receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
1328   bitrate_allocator_->OnNetworkEstimateChanged(msg);
1329 
1330   last_bandwidth_bps_.store(target_bitrate_bps, std::memory_order_relaxed);
1331 
1332   // Ignore updates if bitrate is zero (the aggregate network state is
1333   // down) or if we're not sending video.
1334   // Using `video_send_streams_empty_` is racy but as the caller can't
1335   // reasonably expect synchronize with changes in `video_send_streams_` (being
1336   // on `send_transport_sequence_checker`), we can avoid a PostTask this way.
1337   if (target_bitrate_bps == 0 ||
1338       video_send_streams_empty_.load(std::memory_order_relaxed)) {
1339     send_stats_.PauseSendAndPacerBitrateCounters();
1340   } else {
1341     send_stats_.AddTargetBitrateSample(target_bitrate_bps);
1342   }
1343 }
1344 
OnAllocationLimitsChanged(BitrateAllocationLimits limits)1345 void Call::OnAllocationLimitsChanged(BitrateAllocationLimits limits) {
1346   RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_);
1347 
1348   transport_send_ptr_->SetAllocatedSendBitrateLimits(limits);
1349   send_stats_.SetMinAllocatableRate(limits);
1350   configured_max_padding_bitrate_bps_.store(limits.max_padding_rate.bps(),
1351                                             std::memory_order_relaxed);
1352 }
1353 
FindAudioStreamForSyncGroup(absl::string_view sync_group)1354 AudioReceiveStreamImpl* Call::FindAudioStreamForSyncGroup(
1355     absl::string_view sync_group) {
1356   RTC_DCHECK_RUN_ON(worker_thread_);
1357   RTC_DCHECK_RUN_ON(&receive_11993_checker_);
1358   if (!sync_group.empty()) {
1359     for (AudioReceiveStreamImpl* stream : audio_receive_streams_) {
1360       if (stream->sync_group() == sync_group)
1361         return stream;
1362     }
1363   }
1364 
1365   return nullptr;
1366 }
1367 
ConfigureSync(absl::string_view sync_group)1368 void Call::ConfigureSync(absl::string_view sync_group) {
1369   // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
1370   RTC_DCHECK_RUN_ON(worker_thread_);
1371   // `audio_stream` may be nullptr when clearing the audio stream for a group.
1372   AudioReceiveStreamImpl* audio_stream =
1373       FindAudioStreamForSyncGroup(sync_group);
1374 
1375   size_t num_synced_streams = 0;
1376   for (VideoReceiveStream2* video_stream : video_receive_streams_) {
1377     if (video_stream->sync_group() != sync_group)
1378       continue;
1379     ++num_synced_streams;
1380     // TODO(bugs.webrtc.org/4762): Support synchronizing more than one A/V pair.
1381     // Attempting to sync more than one audio/video pair within the same sync
1382     // group is not supported in the current implementation.
1383     // Only sync the first A/V pair within this sync group.
1384     if (num_synced_streams == 1) {
1385       // sync_audio_stream may be null and that's ok.
1386       video_stream->SetSync(audio_stream);
1387     } else {
1388       video_stream->SetSync(nullptr);
1389     }
1390   }
1391 }
1392 
DeliverRtcp(MediaType media_type,rtc::CopyOnWriteBuffer packet)1393 void Call::DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet) {
1394   RTC_DCHECK_RUN_ON(network_thread_);
1395   TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
1396 
1397   // TODO(bugs.webrtc.org/11993): This DCHECK is here just to maintain the
1398   // invariant that currently the only call path to this function is via
1399   // `PeerConnection::InitializeRtcpCallback()`. DeliverRtp on the other hand
1400   // gets called via the channel classes and
1401   // WebRtc[Audio|Video]Channel's `OnPacketReceived`. We'll remove the
1402   // PeerConnection involvement as well as
1403   // `JsepTransportController::OnRtcpPacketReceived_n` and `rtcp_handler`
1404   // and make sure that the flow of packets is consistent from the
1405   // `RtpTransport` class, via the *Channel and *Engine classes and into Call.
1406   // This way we'll also know more about the context of the packet.
1407   RTC_DCHECK_EQ(media_type, MediaType::ANY);
1408 
1409   // TODO(bugs.webrtc.org/11993): This should execute directly on the network
1410   // thread.
1411   worker_thread_->PostTask(
1412       SafeTask(task_safety_.flag(), [this, packet = std::move(packet)]() {
1413         RTC_DCHECK_RUN_ON(worker_thread_);
1414 
1415         receive_stats_.AddReceivedRtcpBytes(static_cast<int>(packet.size()));
1416         bool rtcp_delivered = false;
1417         for (VideoReceiveStream2* stream : video_receive_streams_) {
1418           if (stream->DeliverRtcp(packet.cdata(), packet.size()))
1419             rtcp_delivered = true;
1420         }
1421 
1422         for (AudioReceiveStreamImpl* stream : audio_receive_streams_) {
1423           stream->DeliverRtcp(packet.cdata(), packet.size());
1424           rtcp_delivered = true;
1425         }
1426 
1427         for (VideoSendStream* stream : video_send_streams_) {
1428           stream->DeliverRtcp(packet.cdata(), packet.size());
1429           rtcp_delivered = true;
1430         }
1431 
1432         for (auto& kv : audio_send_ssrcs_) {
1433           kv.second->DeliverRtcp(packet.cdata(), packet.size());
1434           rtcp_delivered = true;
1435         }
1436 
1437         if (rtcp_delivered) {
1438           event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>(
1439               rtc::MakeArrayView(packet.cdata(), packet.size())));
1440         }
1441       }));
1442 }
1443 
DeliverRtp(MediaType media_type,rtc::CopyOnWriteBuffer packet,int64_t packet_time_us)1444 PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
1445                                                 rtc::CopyOnWriteBuffer packet,
1446                                                 int64_t packet_time_us) {
1447   TRACE_EVENT0("webrtc", "Call::DeliverRtp");
1448   RTC_DCHECK_NE(media_type, MediaType::ANY);
1449 
1450   RtpPacketReceived parsed_packet;
1451   if (!parsed_packet.Parse(std::move(packet)))
1452     return DELIVERY_PACKET_ERROR;
1453 
1454   if (packet_time_us != -1) {
1455     if (receive_time_calculator_) {
1456       // Repair packet_time_us for clock resets by comparing a new read of
1457       // the same clock (TimeUTCMicros) to a monotonic clock reading.
1458       packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
1459           packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
1460     }
1461     parsed_packet.set_arrival_time(Timestamp::Micros(packet_time_us));
1462   } else {
1463     parsed_packet.set_arrival_time(clock_->CurrentTime());
1464   }
1465 
1466   // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1467   // These are empty (zero length payload) RTP packets with an unsignaled
1468   // payload type.
1469   const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
1470 
1471   RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1472              is_keep_alive_packet);
1473 
1474   bool use_send_side_bwe = false;
1475   if (!IdentifyReceivedPacket(parsed_packet, &use_send_side_bwe))
1476     return DELIVERY_UNKNOWN_SSRC;
1477 
1478   NotifyBweOfReceivedPacket(parsed_packet, media_type, use_send_side_bwe);
1479 
1480   // RateCounters expect input parameter as int, save it as int,
1481   // instead of converting each time it is passed to RateCounter::Add below.
1482   int length = static_cast<int>(parsed_packet.size());
1483   if (media_type == MediaType::AUDIO) {
1484     if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
1485       receive_stats_.AddReceivedAudioBytes(length,
1486                                            parsed_packet.arrival_time());
1487       event_log_->Log(
1488           std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
1489       return DELIVERY_OK;
1490     }
1491   } else if (media_type == MediaType::VIDEO) {
1492     parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
1493     if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
1494       receive_stats_.AddReceivedVideoBytes(length,
1495                                            parsed_packet.arrival_time());
1496       event_log_->Log(
1497           std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
1498       return DELIVERY_OK;
1499     }
1500   }
1501   return DELIVERY_UNKNOWN_SSRC;
1502 }
1503 
DeliverPacket(MediaType media_type,rtc::CopyOnWriteBuffer packet,int64_t packet_time_us)1504 PacketReceiver::DeliveryStatus Call::DeliverPacket(
1505     MediaType media_type,
1506     rtc::CopyOnWriteBuffer packet,
1507     int64_t packet_time_us) {
1508   if (IsRtcpPacket(packet)) {
1509     RTC_DCHECK_RUN_ON(network_thread_);
1510     DeliverRtcp(media_type, std::move(packet));
1511     return DELIVERY_OK;
1512   }
1513 
1514   RTC_DCHECK_RUN_ON(worker_thread_);
1515   return DeliverRtp(media_type, std::move(packet), packet_time_us);
1516 }
1517 
OnRecoveredPacket(const uint8_t * packet,size_t length)1518 void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
1519   // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
1520   // This method is called synchronously via `OnRtpPacket()` (see DeliverRtp)
1521   // on the same thread.
1522   RTC_DCHECK_RUN_ON(worker_thread_);
1523   RtpPacketReceived parsed_packet;
1524   if (!parsed_packet.Parse(packet, length))
1525     return;
1526 
1527   parsed_packet.set_recovered(true);
1528 
1529   if (!IdentifyReceivedPacket(parsed_packet))
1530     return;
1531 
1532   // TODO(brandtr): Update here when we support protecting audio packets too.
1533   parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
1534   video_receiver_controller_.OnRtpPacket(parsed_packet);
1535 }
1536 
NotifyBweOfReceivedPacket(const RtpPacketReceived & packet,MediaType media_type,bool use_send_side_bwe)1537 void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1538                                      MediaType media_type,
1539                                      bool use_send_side_bwe) {
1540   RTC_DCHECK_RUN_ON(worker_thread_);
1541   RTPHeader header;
1542   packet.GetHeader(&header);
1543 
1544   ReceivedPacket packet_msg;
1545   packet_msg.size = DataSize::Bytes(packet.payload_size());
1546   packet_msg.receive_time = packet.arrival_time();
1547   if (header.extension.hasAbsoluteSendTime) {
1548     packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1549   }
1550   transport_send_->OnReceivedPacket(packet_msg);
1551 
1552   if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
1553     // Inconsistent configuration of send side BWE. Do nothing.
1554     return;
1555   }
1556   // For audio, we only support send side BWE.
1557   if (media_type == MediaType::VIDEO ||
1558       (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
1559     receive_side_cc_.OnReceivedPacket(
1560         packet.arrival_time().ms(),
1561         packet.payload_size() + packet.padding_size(), header);
1562   }
1563 }
1564 
IdentifyReceivedPacket(RtpPacketReceived & packet,bool * use_send_side_bwe)1565 bool Call::IdentifyReceivedPacket(RtpPacketReceived& packet,
1566                                   bool* use_send_side_bwe /*= nullptr*/) {
1567   RTC_DCHECK_RUN_ON(&receive_11993_checker_);
1568   auto it = receive_rtp_config_.find(packet.Ssrc());
1569   if (it == receive_rtp_config_.end()) {
1570     RTC_DLOG(LS_WARNING) << "receive_rtp_config_ lookup failed for ssrc "
1571                          << packet.Ssrc();
1572     return false;
1573   }
1574 
1575   packet.IdentifyExtensions(it->second->GetRtpExtensionMap());
1576 
1577   if (use_send_side_bwe) {
1578     *use_send_side_bwe = UseSendSideBwe(it->second);
1579   }
1580 
1581   return true;
1582 }
1583 
RegisterReceiveStream(uint32_t ssrc,ReceiveStreamInterface * stream)1584 bool Call::RegisterReceiveStream(uint32_t ssrc,
1585                                  ReceiveStreamInterface* stream) {
1586   RTC_DCHECK_RUN_ON(&receive_11993_checker_);
1587   RTC_DCHECK(stream);
1588   auto inserted = receive_rtp_config_.emplace(ssrc, stream);
1589   if (!inserted.second) {
1590     RTC_DLOG(LS_WARNING) << "ssrc already registered: " << ssrc;
1591   }
1592   return inserted.second;
1593 }
1594 
UnregisterReceiveStream(uint32_t ssrc)1595 bool Call::UnregisterReceiveStream(uint32_t ssrc) {
1596   RTC_DCHECK_RUN_ON(&receive_11993_checker_);
1597   size_t erased = receive_rtp_config_.erase(ssrc);
1598   if (!erased) {
1599     RTC_DLOG(LS_WARNING) << "ssrc wasn't registered: " << ssrc;
1600   }
1601   return erased != 0u;
1602 }
1603 
1604 }  // namespace internal
1605 
1606 }  // namespace webrtc
1607