blob: 00f58d30a069b4b8a4cfcb652322ee7cf4e41009 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "call/call.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
mflodman101f2502016-06-09 17:21:19 +020015#include <algorithm>
Markus Handelld9943042021-05-31 22:52:02 +020016#include <atomic>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000017#include <map>
kwibergb25345e2016-03-12 06:10:44 -080018#include <memory>
ossuf515ab82016-12-07 04:52:58 -080019#include <set>
brandtr25445d32016-10-23 23:37:14 -070020#include <utility>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000021#include <vector>
22
Per Kjellanderfe2063e2021-05-12 09:02:43 +020023#include "absl/functional/bind_front.h"
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020024#include "absl/types/optional.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020025#include "api/rtc_event_log/rtc_event_log.h"
Artem Titovd15a5752021-02-10 14:31:24 +010026#include "api/sequence_checker.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020027#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "audio/audio_receive_stream.h"
29#include "audio/audio_send_stream.h"
30#include "audio/audio_state.h"
Henrik Boström29444c62020-07-01 15:48:46 +020031#include "call/adaptation/broadcast_resource_listener.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "call/bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010034#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "call/rtp_stream_receiver_controller.h"
36#include "call/rtp_transport_controller_send.h"
Vojin Ilic504fc192021-05-31 14:02:28 +020037#include "call/rtp_transport_controller_send_factory.h"
Mirko Bonadeib9857482020-12-14 15:28:43 +010038#include "call/version.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020039#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020040#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
41#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
42#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
43#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Elad Alon99a81b62017-09-21 10:25:29 +020044#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
46#include "modules/rtp_rtcp/include/flexfec_receiver.h"
47#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "modules/rtp_rtcp/source/byte_io.h"
49#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Danil Chapovalov00ca0042021-07-05 19:06:17 +020050#include "modules/rtp_rtcp/source/rtp_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010052#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080054#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/location.h"
56#include "rtc_base/logging.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020057#include "rtc_base/strings/string_builder.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010058#include "rtc_base/system/no_unique_address.h"
Tommi0d4647d2020-05-26 19:35:16 +020059#include "rtc_base/task_utils/pending_task_safety_flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080061#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "rtc_base/trace_event.h"
63#include "system_wrappers/include/clock.h"
64#include "system_wrappers/include/cpu_info.h"
Jonas Oreland6d835922019-03-18 10:59:40 +010065#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020066#include "system_wrappers/include/metrics.h"
Tommi822a8742020-05-11 00:42:30 +020067#include "video/call_stats2.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020068#include "video/send_delay_stats.h"
69#include "video/stats_counter.h"
Tommi553c8692020-05-05 15:35:45 +020070#include "video/video_receive_stream2.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020071#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000072
73namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000074
nisse4709e892017-02-07 01:18:43 -080075namespace {
Johannes Kronf59666b2019-04-08 12:57:06 +020076bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010077 for (const auto& extension : extensions) {
78 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
Johannes Kronf59666b2019-04-08 12:57:06 +020079 return false;
Johannes Kron7ff164e2019-02-07 12:50:18 +010080 }
Johannes Kronf59666b2019-04-08 12:57:06 +020081 return true;
Johannes Kron7ff164e2019-02-07 12:50:18 +010082}
83
Tommid3500062021-06-14 19:39:45 +020084bool UseSendSideBwe(const ReceiveStream::RtpConfig& rtp) {
85 if (!rtp.transport_cc)
nisse4709e892017-02-07 01:18:43 -080086 return false;
Tommid3500062021-06-14 19:39:45 +020087 for (const auto& extension : rtp.extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010088 if (extension.uri == RtpExtension::kTransportSequenceNumberUri ||
89 extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
nisse4709e892017-02-07 01:18:43 -080090 return true;
91 }
92 return false;
93}
94
nisse26e3abb2017-08-25 04:44:25 -070095const int* FindKeyByValue(const std::map<int, int>& m, int v) {
96 for (const auto& kv : m) {
97 if (kv.second == v)
98 return &kv.first;
99 }
100 return nullptr;
101}
102
eladalon8ec568a2017-09-08 06:15:52 -0700103std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700104 const VideoReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200105 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700106 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
107 rtclog_config->local_ssrc = config.rtp.local_ssrc;
108 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
109 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
eladalon8ec568a2017-09-08 06:15:52 -0700110 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700111
112 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700113 const int* search =
114 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200115 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200116 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700117 }
118 return rtclog_config;
119}
120
eladalon8ec568a2017-09-08 06:15:52 -0700121std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700122 const VideoSendStream::Config& config,
123 size_t ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200124 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700125 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700126 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700127 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700128 }
eladalon8ec568a2017-09-08 06:15:52 -0700129 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
130 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700131
Niels Möller259a4972018-04-05 15:36:51 +0200132 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
133 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700134 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700135 return rtclog_config;
136}
137
eladalon8ec568a2017-09-08 06:15:52 -0700138std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700139 const AudioReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200140 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700141 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
142 rtclog_config->local_ssrc = config.rtp.local_ssrc;
143 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700144 return rtclog_config;
145}
146
Tommi822a8742020-05-11 00:42:30 +0200147TaskQueueBase* GetCurrentTaskQueueOrThread() {
148 TaskQueueBase* current = TaskQueueBase::Current();
149 if (!current)
150 current = rtc::ThreadManager::Instance()->CurrentThread();
151 return current;
152}
153
nisse4709e892017-02-07 01:18:43 -0800154} // namespace
155
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000156namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000157
Henrik Boström29444c62020-07-01 15:48:46 +0200158// Wraps an injected resource in a BroadcastResourceListener and handles adding
159// and removing adapter resources to individual VideoSendStreams.
160class ResourceVideoSendStreamForwarder {
161 public:
162 ResourceVideoSendStreamForwarder(
163 rtc::scoped_refptr<webrtc::Resource> resource)
164 : broadcast_resource_listener_(resource) {
165 broadcast_resource_listener_.StartListening();
166 }
167 ~ResourceVideoSendStreamForwarder() {
168 RTC_DCHECK(adapter_resources_.empty());
169 broadcast_resource_listener_.StopListening();
170 }
171
172 rtc::scoped_refptr<webrtc::Resource> Resource() const {
173 return broadcast_resource_listener_.SourceResource();
174 }
175
176 void OnCreateVideoSendStream(VideoSendStream* video_send_stream) {
177 RTC_DCHECK(adapter_resources_.find(video_send_stream) ==
178 adapter_resources_.end());
179 auto adapter_resource =
180 broadcast_resource_listener_.CreateAdapterResource();
181 video_send_stream->AddAdaptationResource(adapter_resource);
182 adapter_resources_.insert(
183 std::make_pair(video_send_stream, adapter_resource));
184 }
185
186 void OnDestroyVideoSendStream(VideoSendStream* video_send_stream) {
187 auto it = adapter_resources_.find(video_send_stream);
188 RTC_DCHECK(it != adapter_resources_.end());
189 broadcast_resource_listener_.RemoveAdapterResource(it->second);
190 adapter_resources_.erase(it);
191 }
192
193 private:
194 BroadcastResourceListener broadcast_resource_listener_;
195 std::map<VideoSendStream*, rtc::scoped_refptr<webrtc::Resource>>
196 adapter_resources_;
197};
198
Sebastian Janssone6256052018-05-04 14:08:15 +0200199class Call final : public webrtc::Call,
200 public PacketReceiver,
201 public RecoveredPacketReceiver,
202 public TargetTransferRateObserver,
203 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000204 public:
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100205 Call(Clock* clock,
206 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100207 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
Tommi25c77c12020-05-25 17:44:55 +0200208 rtc::scoped_refptr<SharedModuleThread> module_process_thread,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100209 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200210 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000211
brandtr25445d32016-10-23 23:37:14 -0700212 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000213 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000214
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200215 webrtc::AudioSendStream* CreateAudioSendStream(
216 const webrtc::AudioSendStream::Config& config) override;
217 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
218
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200219 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
220 const webrtc::AudioReceiveStream::Config& config) override;
221 void DestroyAudioReceiveStream(
222 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000223
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200224 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700225 webrtc::VideoSendStream::Config config,
226 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100227 webrtc::VideoSendStream* CreateVideoSendStream(
228 webrtc::VideoSendStream::Config config,
229 VideoEncoderConfig encoder_config,
230 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000231 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000232
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200233 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200234 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000235 void DestroyVideoReceiveStream(
236 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000237
brandtr7250b392016-12-19 01:13:46 -0800238 FlexfecReceiveStream* CreateFlexfecReceiveStream(
239 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700240 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800241 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700242
Henrik Boströmf4a99912020-06-11 12:07:14 +0200243 void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
244
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100245 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
246
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000247 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000248
Erik Språngceb44952020-09-22 11:36:35 +0200249 const WebRtcKeyValueConfig& trials() const override;
250
Tomas Gunnarssone984aa22021-04-19 09:21:06 +0200251 TaskQueueBase* network_thread() const override;
252 TaskQueueBase* worker_thread() const override;
253
brandtr25445d32016-10-23 23:37:14 -0700254 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700255 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100256 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200257 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000258
brandtr4e523862016-10-18 23:50:45 -0700259 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700260 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700261
skvlad7a43d252016-03-22 15:32:27 -0700262 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000263
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200264 void OnAudioTransportOverheadChanged(
265 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800266
Tommi08be9ba2021-06-15 23:01:57 +0200267 void OnLocalSsrcUpdated(webrtc::AudioReceiveStream& stream,
268 uint32_t local_ssrc) override;
269
Tommi55107c82021-06-16 16:31:18 +0200270 void OnUpdateSyncGroup(webrtc::AudioReceiveStream& stream,
271 const std::string& sync_group) override;
272
stefanc1aeaf02015-10-15 07:26:07 -0700273 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
274
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100275 // Implements TargetTransferRateObserver,
276 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100277 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800278
perkj71ee44c2016-06-15 00:47:53 -0700279 // Implements BitrateAllocator::LimitObserver.
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200280 void OnAllocationLimitsChanged(BitrateAllocationLimits limits) override;
perkj71ee44c2016-06-15 00:47:53 -0700281
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700282 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
283
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000284 private:
Markus Handellc81afe32021-05-31 09:02:01 +0200285 // Thread-compatible class that collects received packet stats and exposes
286 // them as UMA histograms on destruction.
287 class ReceiveStats {
288 public:
289 explicit ReceiveStats(Clock* clock);
290 ~ReceiveStats();
291
292 void AddReceivedRtcpBytes(int bytes);
293 void AddReceivedAudioBytes(int bytes, webrtc::Timestamp arrival_time);
294 void AddReceivedVideoBytes(int bytes, webrtc::Timestamp arrival_time);
295
296 private:
Markus Handelld9943042021-05-31 22:52:02 +0200297 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
Markus Handellc81afe32021-05-31 09:02:01 +0200298 RateCounter received_bytes_per_second_counter_
299 RTC_GUARDED_BY(sequence_checker_);
300 RateCounter received_audio_bytes_per_second_counter_
301 RTC_GUARDED_BY(sequence_checker_);
302 RateCounter received_video_bytes_per_second_counter_
303 RTC_GUARDED_BY(sequence_checker_);
304 RateCounter received_rtcp_bytes_per_second_counter_
305 RTC_GUARDED_BY(sequence_checker_);
306 absl::optional<Timestamp> first_received_rtp_audio_timestamp_
307 RTC_GUARDED_BY(sequence_checker_);
308 absl::optional<Timestamp> last_received_rtp_audio_timestamp_
309 RTC_GUARDED_BY(sequence_checker_);
310 absl::optional<Timestamp> first_received_rtp_video_timestamp_
311 RTC_GUARDED_BY(sequence_checker_);
312 absl::optional<Timestamp> last_received_rtp_video_timestamp_
313 RTC_GUARDED_BY(sequence_checker_);
314 };
315
Markus Handelld9943042021-05-31 22:52:02 +0200316 // Thread-compatible class that collects sent packet stats and exposes
317 // them as UMA histograms on destruction, provided SetFirstPacketTime was
318 // called with a non-empty packet timestamp before the destructor.
319 class SendStats {
320 public:
321 explicit SendStats(Clock* clock);
322 ~SendStats();
323
324 void SetFirstPacketTime(absl::optional<Timestamp> first_sent_packet_time);
325 void PauseSendAndPacerBitrateCounters();
326 void AddTargetBitrateSample(uint32_t target_bitrate_bps);
327 void SetMinAllocatableRate(BitrateAllocationLimits limits);
328
329 private:
330 RTC_NO_UNIQUE_ADDRESS SequenceChecker destructor_sequence_checker_;
331 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
332 Clock* const clock_ RTC_GUARDED_BY(destructor_sequence_checker_);
333 AvgCounter estimated_send_bitrate_kbps_counter_
334 RTC_GUARDED_BY(sequence_checker_);
335 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(sequence_checker_);
336 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(sequence_checker_){
337 0};
338 absl::optional<Timestamp> first_sent_packet_time_
339 RTC_GUARDED_BY(destructor_sequence_checker_);
340 };
341
Tommicae1f1d2021-05-31 10:51:09 +0200342 void DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet)
343 RTC_RUN_ON(network_thread_);
stefan68786d22015-09-08 05:36:15 -0700344 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100345 rtc::CopyOnWriteBuffer packet,
Tommicae1f1d2021-05-31 10:51:09 +0200346 int64_t packet_time_us) RTC_RUN_ON(worker_thread_);
347 void ConfigureSync(const std::string& sync_group) RTC_RUN_ON(worker_thread_);
pbos8fc7fa72015-07-15 08:02:58 -0700348
nissed44ce052017-02-06 02:23:00 -0800349 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
350 MediaType media_type)
Tommi948e40c2021-05-31 12:39:57 +0200351 RTC_RUN_ON(worker_thread_);
nissed44ce052017-02-06 02:23:00 -0800352
skvlad7a43d252016-03-22 15:32:27 -0700353 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800354
Erik Språng7703f232020-09-14 11:03:13 +0200355 // Ensure that necessary process threads are started, and any required
356 // callbacks have been registered.
Tommicae1f1d2021-05-31 10:51:09 +0200357 void EnsureStarted() RTC_RUN_ON(worker_thread_);
Niels Möller46879152019-01-07 15:54:47 +0100358
Peter Boströmd3c94472015-12-09 11:20:58 +0100359 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100360 TaskQueueFactory* const task_queue_factory_;
Tommi0d4647d2020-05-26 19:35:16 +0200361 TaskQueueBase* const worker_thread_;
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100362 TaskQueueBase* const network_thread_;
Markus Handelld9943042021-05-31 22:52:02 +0200363 RTC_NO_UNIQUE_ADDRESS SequenceChecker send_transport_sequence_checker_;
stefan91d92602015-11-11 10:13:02 -0800364
Peter Boström45553ae2015-05-08 13:54:38 +0200365 const int num_cpu_cores_;
Tommi25c77c12020-05-25 17:44:55 +0200366 const rtc::scoped_refptr<SharedModuleThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800367 const std::unique_ptr<CallStats> call_stats_;
368 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
Tommi948e40c2021-05-31 12:39:57 +0200369 const Call::Config config_ RTC_GUARDED_BY(worker_thread_);
370 // Maps to config_.trials, can be used from any thread via `trials()`.
371 const WebRtcKeyValueConfig& trials_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000372
Tommi948e40c2021-05-31 12:39:57 +0200373 NetworkState audio_network_state_ RTC_GUARDED_BY(worker_thread_);
374 NetworkState video_network_state_ RTC_GUARDED_BY(worker_thread_);
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100375 // TODO(bugs.webrtc.org/11993): Move aggregate_network_up_ over to the
376 // network thread.
Tommi0d4647d2020-05-26 19:35:16 +0200377 bool aggregate_network_up_ RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000378
Markus Handell0e62f7a2021-07-20 13:32:02 +0200379 // Schedules nack periodic processing on behalf of all streams.
380 NackPeriodicProcessor nack_periodic_processor_;
381
brandtr25445d32016-10-23 23:37:14 -0700382 // Audio, Video, and FlexFEC receive streams are owned by the client that
383 // creates them.
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100384 // TODO(bugs.webrtc.org/11993): Move audio_receive_streams_,
385 // video_receive_streams_ and sync_stream_mapping_ over to the network thread.
nissee4bcd6d2017-05-16 04:47:04 -0700386 std::set<AudioReceiveStream*> audio_receive_streams_
Tommi0d4647d2020-05-26 19:35:16 +0200387 RTC_GUARDED_BY(worker_thread_);
Tommi553c8692020-05-05 15:35:45 +0200388 std::set<VideoReceiveStream2*> video_receive_streams_
Tommi0d4647d2020-05-26 19:35:16 +0200389 RTC_GUARDED_BY(worker_thread_);
pbos8fc7fa72015-07-15 08:02:58 -0700390 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
Tommi0d4647d2020-05-26 19:35:16 +0200391 RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000392
nisse0f15f922017-06-21 01:05:22 -0700393 // TODO(nisse): Should eventually be injected at creation,
394 // with a single object in the bundled case.
Tommi948e40c2021-05-31 12:39:57 +0200395 RtpStreamReceiverController audio_receiver_controller_
396 RTC_GUARDED_BY(worker_thread_);
397 RtpStreamReceiverController video_receiver_controller_
398 RTC_GUARDED_BY(worker_thread_);
nissee4bcd6d2017-05-16 04:47:04 -0700399
nissed44ce052017-02-06 02:23:00 -0800400 // This extra map is used for receive processing which is
401 // independent of media type.
402
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100403 // TODO(bugs.webrtc.org/11993): Move receive_rtp_config_ over to the
404 // network thread.
Tommid3500062021-06-14 19:39:45 +0200405 std::map<uint32_t, ReceiveStream*> receive_rtp_config_
Tommi0d4647d2020-05-26 19:35:16 +0200406 RTC_GUARDED_BY(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -0800407
solenbergc7a8b082015-10-16 14:35:07 -0700408 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700409 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
Tommi0d4647d2020-05-26 19:35:16 +0200410 RTC_GUARDED_BY(worker_thread_);
danilchapa37de392017-09-09 04:17:22 -0700411 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
Tommi0d4647d2020-05-26 19:35:16 +0200412 RTC_GUARDED_BY(worker_thread_);
413 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(worker_thread_);
Artem Titovea240272021-07-26 12:40:21 +0200414 // True if `video_send_streams_` is empty, false if not. The atomic variable
Markus Handelld9943042021-05-31 22:52:02 +0200415 // is used to decide UMA send statistics behavior and enables avoiding a
416 // PostTask().
417 std::atomic<bool> video_send_streams_empty_{true};
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000418
Henrik Boström29444c62020-07-01 15:48:46 +0200419 // Each forwarder wraps an adaptation resource that was added to the call.
420 std::vector<std::unique_ptr<ResourceVideoSendStreamForwarder>>
421 adaptation_resource_forwarders_ RTC_GUARDED_BY(worker_thread_);
Henrik Boströmf4a99912020-06-11 12:07:14 +0200422
ossuc3d4b482017-05-23 06:07:11 -0700423 using RtpStateMap = std::map<uint32_t, RtpState>;
Tommi0d4647d2020-05-26 19:35:16 +0200424 RtpStateMap suspended_audio_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
425 RtpStateMap suspended_video_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
ossuc3d4b482017-05-23 06:07:11 -0700426
Åsa Persson4bece9a2017-10-06 10:04:04 +0200427 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
428 RtpPayloadStateMap suspended_video_payload_states_
Tommi0d4647d2020-05-26 19:35:16 +0200429 RTC_GUARDED_BY(worker_thread_);
Åsa Persson4bece9a2017-10-06 10:04:04 +0200430
Tommi948e40c2021-05-31 12:39:57 +0200431 webrtc::RtcEventLog* const event_log_;
ivocb04965c2015-09-09 00:09:43 -0700432
Markus Handelld9943042021-05-31 22:52:02 +0200433 // TODO(bugs.webrtc.org/11993) ready to move stats access to the network
434 // thread.
Markus Handellc81afe32021-05-31 09:02:01 +0200435 ReceiveStats receive_stats_ RTC_GUARDED_BY(worker_thread_);
Markus Handelld9943042021-05-31 22:52:02 +0200436 SendStats send_stats_ RTC_GUARDED_BY(send_transport_sequence_checker_);
Artem Titovea240272021-07-26 12:40:21 +0200437 // `last_bandwidth_bps_` and `configured_max_padding_bitrate_bps_` being
Markus Handelld9943042021-05-31 22:52:02 +0200438 // atomic avoids a PostTask. The variables are used for stats gathering.
439 std::atomic<uint32_t> last_bandwidth_bps_{0};
440 std::atomic<uint32_t> configured_max_padding_bitrate_bps_{0};
stefan18adf0a2015-11-17 06:24:56 -0800441
nisse559af382017-03-21 06:41:12 -0700442 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100443
444 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
445
asapersson35151f32016-05-02 23:44:01 -0700446 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
Markus Handelld9943042021-05-31 22:52:02 +0200447 const Timestamp start_of_call_;
mflodman0e7e2592015-11-12 21:02:42 -0800448
Artem Titovea240272021-07-26 12:40:21 +0200449 // Note that `task_safety_` needs to be at a greater scope than the task queue
450 // owned by `transport_send_` since calls might arrive on the network thread
Tommi0d4647d2020-05-26 19:35:16 +0200451 // while Call is being deleted and the task queue is being torn down.
Tommi948e40c2021-05-31 12:39:57 +0200452 const ScopedTaskSafety task_safety_;
Tommi0d4647d2020-05-26 19:35:16 +0200453
Sebastian Janssone6256052018-05-04 14:08:15 +0200454 // Caches transport_send_.get(), to avoid racing with destructor.
455 // Note that this is declared before transport_send_ to ensure that it is not
456 // invalidated until no more tasks can be running on the transport_send_ task
457 // queue.
Tommi948e40c2021-05-31 12:39:57 +0200458 // For more details on the background of this member variable, see:
459 // https://webrtc-review.googlesource.com/c/src/+/63023/9/call/call.cc
460 // https://bugs.chromium.org/p/chromium/issues/detail?id=992640
461 RtpTransportControllerSendInterface* const transport_send_ptr_
Markus Handelld9943042021-05-31 22:52:02 +0200462 RTC_GUARDED_BY(send_transport_sequence_checker_);
Sebastian Janssone6256052018-05-04 14:08:15 +0200463 // Declared last since it will issue callbacks from a task queue. Declaring it
464 // last ensures that it is destroyed first and any running tasks are finished.
Tommi948e40c2021-05-31 12:39:57 +0200465 const std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800466
Erik Språng7703f232020-09-14 11:03:13 +0200467 bool is_started_ RTC_GUARDED_BY(worker_thread_) = false;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800468
henrikg3c089d72015-09-16 05:37:44 -0700469 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000470};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000471} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000472
asapersson2e5cfcd2016-08-11 08:41:18 -0700473std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200474 char buf[1024];
475 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700476 ss << "Call stats: " << time_ms << ", {";
477 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
478 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
479 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
480 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
481 ss << "rtt_ms: " << rtt_ms;
482 ss << '}';
483 return ss.str();
484}
485
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000486Call* Call::Create(const Call::Config& config) {
Tommi25c77c12020-05-25 17:44:55 +0200487 rtc::scoped_refptr<SharedModuleThread> call_thread =
Per Kjellander4c50e702020-06-30 14:39:43 +0200488 SharedModuleThread::Create(ProcessThread::Create("ModuleProcessThread"),
489 nullptr);
Tommi25c77c12020-05-25 17:44:55 +0200490 return Create(config, Clock::GetRealTimeClock(), std::move(call_thread),
Erik Språng6950b302019-08-16 12:54:08 +0200491 ProcessThread::Create("PacerThread"));
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100492}
493
494Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100495 Clock* clock,
Tommi25c77c12020-05-25 17:44:55 +0200496 rtc::scoped_refptr<SharedModuleThread> call_thread,
Danil Chapovalov359fe332019-04-01 10:46:36 +0200497 std::unique_ptr<ProcessThread> pacer_thread) {
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200498 RTC_DCHECK(config.task_queue_factory);
Vojin Ilic504fc192021-05-31 14:02:28 +0200499
500 RtpTransportControllerSendFactory transport_controller_factory_;
501
502 RtpTransportConfig transportConfig = config.ExtractTransportConfig();
503
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100504 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100505 clock, config,
Vojin Ilic504fc192021-05-31 14:02:28 +0200506 transport_controller_factory_.Create(transportConfig, clock,
507 std::move(pacer_thread)),
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200508 std::move(call_thread), config.task_queue_factory);
zstein7cb69d52017-05-08 11:52:38 -0700509}
510
Vojin Ilic504fc192021-05-31 14:02:28 +0200511Call* Call::Create(const Call::Config& config,
512 Clock* clock,
513 rtc::scoped_refptr<SharedModuleThread> call_thread,
514 std::unique_ptr<RtpTransportControllerSendInterface>
515 transportControllerSend) {
516 RTC_DCHECK(config.task_queue_factory);
517 return new internal::Call(clock, config, std::move(transportControllerSend),
518 std::move(call_thread), config.task_queue_factory);
519}
520
Tommi25c77c12020-05-25 17:44:55 +0200521class SharedModuleThread::Impl {
522 public:
523 Impl(std::unique_ptr<ProcessThread> process_thread,
524 std::function<void()> on_one_ref_remaining)
525 : module_thread_(std::move(process_thread)),
526 on_one_ref_remaining_(std::move(on_one_ref_remaining)) {}
527
528 void EnsureStarted() {
529 RTC_DCHECK_RUN_ON(&sequence_checker_);
530 if (started_)
531 return;
532 started_ = true;
533 module_thread_->Start();
534 }
535
536 ProcessThread* process_thread() {
537 RTC_DCHECK_RUN_ON(&sequence_checker_);
538 return module_thread_.get();
539 }
540
541 void AddRef() const {
542 RTC_DCHECK_RUN_ON(&sequence_checker_);
543 ++ref_count_;
544 }
545
546 rtc::RefCountReleaseStatus Release() const {
547 RTC_DCHECK_RUN_ON(&sequence_checker_);
548 --ref_count_;
549
550 if (ref_count_ == 0) {
551 module_thread_->Stop();
552 return rtc::RefCountReleaseStatus::kDroppedLastRef;
553 }
554
555 if (ref_count_ == 1 && on_one_ref_remaining_) {
556 auto moved_fn = std::move(on_one_ref_remaining_);
Artem Titovea240272021-07-26 12:40:21 +0200557 // NOTE: after this function returns, chances are that `this` has been
Tommi25c77c12020-05-25 17:44:55 +0200558 // deleted - do not touch any member variables.
559 // If the owner of the last reference implements a lambda that releases
560 // that last reference inside of the callback (which is legal according
561 // to this implementation), we will recursively enter Release() above,
562 // call Stop() and release the last reference.
563 moved_fn();
564 }
565
566 return rtc::RefCountReleaseStatus::kOtherRefsRemained;
567 }
568
569 private:
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100570 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
Tommi25c77c12020-05-25 17:44:55 +0200571 mutable int ref_count_ RTC_GUARDED_BY(sequence_checker_) = 0;
572 std::unique_ptr<ProcessThread> const module_thread_;
573 std::function<void()> const on_one_ref_remaining_;
574 bool started_ = false;
575};
576
577SharedModuleThread::SharedModuleThread(
578 std::unique_ptr<ProcessThread> process_thread,
579 std::function<void()> on_one_ref_remaining)
580 : impl_(std::make_unique<Impl>(std::move(process_thread),
581 std::move(on_one_ref_remaining))) {}
582
583SharedModuleThread::~SharedModuleThread() = default;
584
585// static
Tommi25c77c12020-05-25 17:44:55 +0200586
587rtc::scoped_refptr<SharedModuleThread> SharedModuleThread::Create(
588 std::unique_ptr<ProcessThread> process_thread,
589 std::function<void()> on_one_ref_remaining) {
590 return new SharedModuleThread(std::move(process_thread),
591 std::move(on_one_ref_remaining));
592}
593
594void SharedModuleThread::EnsureStarted() {
595 impl_->EnsureStarted();
596}
597
598ProcessThread* SharedModuleThread::process_thread() {
599 return impl_->process_thread();
600}
601
602void SharedModuleThread::AddRef() const {
603 impl_->AddRef();
604}
605
606rtc::RefCountReleaseStatus SharedModuleThread::Release() const {
607 auto ret = impl_->Release();
608 if (ret == rtc::RefCountReleaseStatus::kDroppedLastRef)
609 delete this;
610 return ret;
611}
612
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100613// This method here to avoid subclasses has to implement this method.
614// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
615// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100616VideoSendStream* Call::CreateVideoSendStream(
617 VideoSendStream::Config config,
618 VideoEncoderConfig encoder_config,
619 std::unique_ptr<FecController> fec_controller) {
620 return nullptr;
621}
622
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000623namespace internal {
624
Markus Handellc81afe32021-05-31 09:02:01 +0200625Call::ReceiveStats::ReceiveStats(Clock* clock)
626 : received_bytes_per_second_counter_(clock, nullptr, false),
627 received_audio_bytes_per_second_counter_(clock, nullptr, false),
628 received_video_bytes_per_second_counter_(clock, nullptr, false),
629 received_rtcp_bytes_per_second_counter_(clock, nullptr, false) {
630 sequence_checker_.Detach();
631}
632
633void Call::ReceiveStats::AddReceivedRtcpBytes(int bytes) {
634 RTC_DCHECK_RUN_ON(&sequence_checker_);
635 if (received_bytes_per_second_counter_.HasSample()) {
636 // First RTP packet has been received.
637 received_bytes_per_second_counter_.Add(static_cast<int>(bytes));
638 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(bytes));
639 }
640}
641
642void Call::ReceiveStats::AddReceivedAudioBytes(int bytes,
643 webrtc::Timestamp arrival_time) {
644 RTC_DCHECK_RUN_ON(&sequence_checker_);
645 received_bytes_per_second_counter_.Add(bytes);
646 received_audio_bytes_per_second_counter_.Add(bytes);
647 if (!first_received_rtp_audio_timestamp_)
648 first_received_rtp_audio_timestamp_ = arrival_time;
649 last_received_rtp_audio_timestamp_ = arrival_time;
650}
651
652void Call::ReceiveStats::AddReceivedVideoBytes(int bytes,
653 webrtc::Timestamp arrival_time) {
654 RTC_DCHECK_RUN_ON(&sequence_checker_);
655 received_bytes_per_second_counter_.Add(bytes);
656 received_video_bytes_per_second_counter_.Add(bytes);
657 if (!first_received_rtp_video_timestamp_)
658 first_received_rtp_video_timestamp_ = arrival_time;
659 last_received_rtp_video_timestamp_ = arrival_time;
660}
661
662Call::ReceiveStats::~ReceiveStats() {
663 RTC_DCHECK_RUN_ON(&sequence_checker_);
664 if (first_received_rtp_audio_timestamp_) {
665 RTC_HISTOGRAM_COUNTS_100000(
666 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
667 (*last_received_rtp_audio_timestamp_ -
668 *first_received_rtp_audio_timestamp_)
669 .seconds());
670 }
671 if (first_received_rtp_video_timestamp_) {
672 RTC_HISTOGRAM_COUNTS_100000(
673 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
674 (*last_received_rtp_video_timestamp_ -
675 *first_received_rtp_video_timestamp_)
676 .seconds());
677 }
678 const int kMinRequiredPeriodicSamples = 5;
679 AggregatedStats video_bytes_per_sec =
680 received_video_bytes_per_second_counter_.GetStats();
681 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
682 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
683 video_bytes_per_sec.average * 8 / 1000);
684 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
685 << video_bytes_per_sec.ToStringWithMultiplier(8);
686 }
687 AggregatedStats audio_bytes_per_sec =
688 received_audio_bytes_per_second_counter_.GetStats();
689 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
690 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
691 audio_bytes_per_sec.average * 8 / 1000);
692 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
693 << audio_bytes_per_sec.ToStringWithMultiplier(8);
694 }
695 AggregatedStats rtcp_bytes_per_sec =
696 received_rtcp_bytes_per_second_counter_.GetStats();
697 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
698 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
699 rtcp_bytes_per_sec.average * 8);
700 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
701 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
702 }
703 AggregatedStats recv_bytes_per_sec =
704 received_bytes_per_second_counter_.GetStats();
705 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
706 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
707 recv_bytes_per_sec.average * 8 / 1000);
708 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
709 << recv_bytes_per_sec.ToStringWithMultiplier(8);
710 }
711}
712
Markus Handelld9943042021-05-31 22:52:02 +0200713Call::SendStats::SendStats(Clock* clock)
714 : clock_(clock),
715 estimated_send_bitrate_kbps_counter_(clock, nullptr, true),
716 pacer_bitrate_kbps_counter_(clock, nullptr, true) {
717 destructor_sequence_checker_.Detach();
718 sequence_checker_.Detach();
719}
720
721Call::SendStats::~SendStats() {
722 RTC_DCHECK_RUN_ON(&destructor_sequence_checker_);
723 if (!first_sent_packet_time_)
724 return;
725
726 TimeDelta elapsed = clock_->CurrentTime() - *first_sent_packet_time_;
727 if (elapsed.seconds() < metrics::kMinRunTimeInSeconds)
728 return;
729
730 const int kMinRequiredPeriodicSamples = 5;
731 AggregatedStats send_bitrate_stats =
732 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
733 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
734 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
735 send_bitrate_stats.average);
736 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
737 << send_bitrate_stats.ToString();
738 }
739 AggregatedStats pacer_bitrate_stats =
740 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
741 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
742 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
743 pacer_bitrate_stats.average);
744 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
745 << pacer_bitrate_stats.ToString();
746 }
747}
748
749void Call::SendStats::SetFirstPacketTime(
750 absl::optional<Timestamp> first_sent_packet_time) {
751 RTC_DCHECK_RUN_ON(&destructor_sequence_checker_);
752 first_sent_packet_time_ = first_sent_packet_time;
753}
754
755void Call::SendStats::PauseSendAndPacerBitrateCounters() {
756 RTC_DCHECK_RUN_ON(&sequence_checker_);
757 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
758 pacer_bitrate_kbps_counter_.ProcessAndPause();
759}
760
761void Call::SendStats::AddTargetBitrateSample(uint32_t target_bitrate_bps) {
762 RTC_DCHECK_RUN_ON(&sequence_checker_);
763 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
764 // Pacer bitrate may be higher than bitrate estimate if enforcing min
765 // bitrate.
766 uint32_t pacer_bitrate_bps =
767 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
768 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
769}
770
771void Call::SendStats::SetMinAllocatableRate(BitrateAllocationLimits limits) {
772 RTC_DCHECK_RUN_ON(&sequence_checker_);
773 min_allocated_send_bitrate_bps_ = limits.min_allocatable_rate.bps();
774}
775
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100776Call::Call(Clock* clock,
777 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100778 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
Tommi25c77c12020-05-25 17:44:55 +0200779 rtc::scoped_refptr<SharedModuleThread> module_process_thread,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100780 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100781 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100782 task_queue_factory_(task_queue_factory),
Tommi0d4647d2020-05-26 19:35:16 +0200783 worker_thread_(GetCurrentTaskQueueOrThread()),
Artem Titovea240272021-07-26 12:40:21 +0200784 // If `network_task_queue_` was set to nullptr, network related calls
785 // must be made on `worker_thread_` (i.e. they're one and the same).
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100786 network_thread_(config.network_task_queue_ ? config.network_task_queue_
787 : worker_thread_),
stefan91d92602015-11-11 10:13:02 -0800788 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100789 module_process_thread_(std::move(module_process_thread)),
Tommi0d4647d2020-05-26 19:35:16 +0200790 call_stats_(new CallStats(clock_, worker_thread_)),
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200791 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200792 config_(config),
Tommi948e40c2021-05-31 12:39:57 +0200793 trials_(*config.trials),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800794 audio_network_state_(kNetworkDown),
795 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100796 aggregate_network_up_(false),
skvlad11a9cbf2016-10-07 11:53:05 -0700797 event_log_(config.event_log),
Markus Handellc81afe32021-05-31 09:02:01 +0200798 receive_stats_(clock_),
Markus Handelld9943042021-05-31 22:52:02 +0200799 send_stats_(clock_),
Per Kjellanderfe2063e2021-05-12 09:02:43 +0200800 receive_side_cc_(clock,
801 absl::bind_front(&PacketRouter::SendCombinedRtcpPacket,
802 transport_send->packet_router()),
803 absl::bind_front(&PacketRouter::SendRemb,
804 transport_send->packet_router()),
805 /*network_state_estimator=*/nullptr),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100806 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700807 video_send_delay_stats_(new SendDelayStats(clock_)),
Markus Handelld9943042021-05-31 22:52:02 +0200808 start_of_call_(clock_->CurrentTime()),
Tommi78a71382019-08-08 12:27:53 +0200809 transport_send_ptr_(transport_send.get()),
Markus Handelld9943042021-05-31 22:52:02 +0200810 transport_send_(std::move(transport_send)) {
skvlad11a9cbf2016-10-07 11:53:05 -0700811 RTC_DCHECK(config.event_log != nullptr);
Erik Språng17f82cf2019-12-04 11:10:43 +0100812 RTC_DCHECK(config.trials != nullptr);
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100813 RTC_DCHECK(network_thread_);
Tommi0d4647d2020-05-26 19:35:16 +0200814 RTC_DCHECK(worker_thread_->IsCurrent());
Markus Handelld9943042021-05-31 22:52:02 +0200815
816 send_transport_sequence_checker_.Detach();
Tommi48b48e52019-08-09 11:42:32 +0200817
Mirko Bonadeib9857482020-12-14 15:28:43 +0100818 // Do not remove this call; it is here to convince the compiler that the
819 // WebRTC source timestamp string needs to be in the final binary.
820 LoadWebRTCVersionInRegister();
821
Tommi48b48e52019-08-09 11:42:32 +0200822 call_stats_->RegisterStatsObserver(&receive_side_cc_);
823
Tommi25c77c12020-05-25 17:44:55 +0200824 module_process_thread_->process_thread()->RegisterModule(
Tommi48b48e52019-08-09 11:42:32 +0200825 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
Tommi25c77c12020-05-25 17:44:55 +0200826 module_process_thread_->process_thread()->RegisterModule(&receive_side_cc_,
827 RTC_FROM_HERE);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000828}
829
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000830Call::~Call() {
Tommi0d4647d2020-05-26 19:35:16 +0200831 RTC_DCHECK_RUN_ON(worker_thread_);
perkj26091b12016-09-01 01:17:40 -0700832
solenbergc7a8b082015-10-16 14:35:07 -0700833 RTC_CHECK(audio_send_ssrcs_.empty());
834 RTC_CHECK(video_send_ssrcs_.empty());
835 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700836 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700837 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000838
Tommi25c77c12020-05-25 17:44:55 +0200839 module_process_thread_->process_thread()->DeRegisterModule(
Tommi78a71382019-08-08 12:27:53 +0200840 receive_side_cc_.GetRemoteBitrateEstimator(true));
Tommi25c77c12020-05-25 17:44:55 +0200841 module_process_thread_->process_thread()->DeRegisterModule(&receive_side_cc_);
Tommi78a71382019-08-08 12:27:53 +0200842 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Markus Handelld9943042021-05-31 22:52:02 +0200843 send_stats_.SetFirstPacketTime(transport_send_->GetFirstPacketTime());
sprang6d6122b2016-07-13 06:37:09 -0700844
Markus Handelld9943042021-05-31 22:52:02 +0200845 RTC_HISTOGRAM_COUNTS_100000(
846 "WebRTC.Call.LifetimeInSeconds",
847 (clock_->CurrentTime() - start_of_call_).seconds());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000848}
849
Erik Språng7703f232020-09-14 11:03:13 +0200850void Call::EnsureStarted() {
851 if (is_started_) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800852 return;
Erik Språng7703f232020-09-14 11:03:13 +0200853 }
854 is_started_ = true;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800855
Etienne Pierre-Doraycc474372021-02-10 15:51:36 -0500856 call_stats_->EnsureStarted();
857
Tommi48b48e52019-08-09 11:42:32 +0200858 // This call seems to kick off a number of things, so probably better left
859 // off being kicked off on request rather than in the ctor.
Tommi948e40c2021-05-31 12:39:57 +0200860 transport_send_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800861
Tommi25c77c12020-05-25 17:44:55 +0200862 module_process_thread_->EnsureStarted();
Tommi948e40c2021-05-31 12:39:57 +0200863 transport_send_->EnsureStarted();
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700864}
865
866void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
Tommi0d4647d2020-05-26 19:35:16 +0200867 RTC_DCHECK_RUN_ON(worker_thread_);
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700868 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800869}
870
solenberg5a289392015-10-19 03:39:20 -0700871PacketReceiver* Call::Receiver() {
solenberg5a289392015-10-19 03:39:20 -0700872 return this;
873}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000874
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200875webrtc::AudioSendStream* Call::CreateAudioSendStream(
876 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700877 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200878 RTC_DCHECK_RUN_ON(worker_thread_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800879
Erik Språng7703f232020-09-14 11:03:13 +0200880 EnsureStarted();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800881
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100882 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
883 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200884 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700885 {
886 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
887 if (iter != suspended_audio_send_ssrcs_.end()) {
888 suspended_rtp_state.emplace(iter->second);
889 }
890 }
891
Tommi822a8742020-05-11 00:42:30 +0200892 AudioSendStream* send_stream = new AudioSendStream(
893 clock_, config, config_.audio_state, task_queue_factory_,
Markus Handelleb61b7f2021-06-22 10:46:48 +0200894 transport_send_.get(), bitrate_allocator_.get(), event_log_,
895 call_stats_->AsRtcpRttStats(), suspended_rtp_state);
Tommi0d4647d2020-05-26 19:35:16 +0200896 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
897 audio_send_ssrcs_.end());
898 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
Tommi31001a62020-05-26 11:38:36 +0200899
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100900 // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
901 // UpdateAggregateNetworkState asynchronously on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200902 for (AudioReceiveStream* stream : audio_receive_streams_) {
Tommi6eda26c2021-06-09 13:46:28 +0200903 if (stream->local_ssrc() == config.rtp.ssrc) {
Tommi31001a62020-05-26 11:38:36 +0200904 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800905 }
906 }
Tommi31001a62020-05-26 11:38:36 +0200907
skvlad7a43d252016-03-22 15:32:27 -0700908 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100909
solenbergc7a8b082015-10-16 14:35:07 -0700910 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200911}
912
913void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700914 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200915 RTC_DCHECK_RUN_ON(worker_thread_);
solenbergc7a8b082015-10-16 14:35:07 -0700916 RTC_DCHECK(send_stream != nullptr);
917
918 send_stream->Stop();
919
eladalonabbc4302017-07-26 02:09:44 -0700920 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700921 webrtc::internal::AudioSendStream* audio_send_stream =
922 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700923 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
Tommi0d4647d2020-05-26 19:35:16 +0200924
925 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
926 RTC_DCHECK_EQ(1, num_deleted);
Tommi31001a62020-05-26 11:38:36 +0200927
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100928 // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
929 // UpdateAggregateNetworkState asynchronously on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200930 for (AudioReceiveStream* stream : audio_receive_streams_) {
Tommi6eda26c2021-06-09 13:46:28 +0200931 if (stream->local_ssrc() == ssrc) {
Tommi31001a62020-05-26 11:38:36 +0200932 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800933 }
solenbergc7a8b082015-10-16 14:35:07 -0700934 }
Tommi31001a62020-05-26 11:38:36 +0200935
skvlad7a43d252016-03-22 15:32:27 -0700936 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100937
eladalonabbc4302017-07-26 02:09:44 -0700938 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200939}
940
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200941webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
942 const webrtc::AudioReceiveStream::Config& config) {
943 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +0200944 RTC_DCHECK_RUN_ON(worker_thread_);
Erik Språng7703f232020-09-14 11:03:13 +0200945 EnsureStarted();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200946 event_log_->Log(std::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200947 CreateRtcLogStreamConfig(config)));
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100948
nisse0f15f922017-06-21 01:05:22 -0700949 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Markus Handelleb61b7f2021-06-22 10:46:48 +0200950 clock_, transport_send_->packet_router(), config_.neteq_factory, config,
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +0100951 config_.audio_state, event_log_);
Tommi6eda26c2021-06-09 13:46:28 +0200952 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800953
Tommi02df2eb2021-05-31 12:57:53 +0200954 // TODO(bugs.webrtc.org/11993): Make the registration on the network thread
955 // (asynchronously). The registration and `audio_receiver_controller_` need
956 // to live on the network thread.
957 receive_stream->RegisterWithTransport(&audio_receiver_controller_);
958
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100959 // TODO(bugs.webrtc.org/11993): Update the below on the network thread.
960 // We could possibly set up the audio_receiver_controller_ association up
961 // as part of the async setup.
Tommid3500062021-06-14 19:39:45 +0200962 receive_rtp_config_.emplace(config.rtp.remote_ssrc, receive_stream);
Tommi31001a62020-05-26 11:38:36 +0200963
964 ConfigureSync(config.sync_group);
965
Tommi0d4647d2020-05-26 19:35:16 +0200966 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
967 if (it != audio_send_ssrcs_.end()) {
968 receive_stream->AssociateSendStream(it->second);
solenberg7602aab2016-11-14 11:30:07 -0800969 }
Tommi0d4647d2020-05-26 19:35:16 +0200970
skvlad7a43d252016-03-22 15:32:27 -0700971 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200972 return receive_stream;
973}
974
975void Call::DestroyAudioReceiveStream(
976 webrtc::AudioReceiveStream* receive_stream) {
977 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +0200978 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -0700979 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700980 webrtc::internal::AudioReceiveStream* audio_receive_stream =
981 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Tommi31001a62020-05-26 11:38:36 +0200982
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100983 // TODO(bugs.webrtc.org/11993): Access the map, rtp config, call ConfigureSync
Tommi02df2eb2021-05-31 12:57:53 +0200984 // and UpdateAggregateNetworkState on the network thread. The call to
985 // `UnregisterFromTransport` should also happen on the network thread.
986 audio_receive_stream->UnregisterFromTransport();
Tommie2561e12021-06-08 16:55:47 +0200987
Tommi6eda26c2021-06-09 13:46:28 +0200988 uint32_t ssrc = audio_receive_stream->remote_ssrc();
989 const AudioReceiveStream::Config& config = audio_receive_stream->config();
Tommid3500062021-06-14 19:39:45 +0200990 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config.rtp))
Tommi6eda26c2021-06-09 13:46:28 +0200991 ->RemoveStream(ssrc);
992
993 audio_receive_streams_.erase(audio_receive_stream);
994
995 const auto it = sync_stream_mapping_.find(config.sync_group);
Tommi31001a62020-05-26 11:38:36 +0200996 if (it != sync_stream_mapping_.end() && it->second == audio_receive_stream) {
997 sync_stream_mapping_.erase(it);
Tommi6eda26c2021-06-09 13:46:28 +0200998 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200999 }
Tommi31001a62020-05-26 11:38:36 +02001000 receive_rtp_config_.erase(ssrc);
1001
skvlad7a43d252016-03-22 15:32:27 -07001002 UpdateAggregateNetworkState();
Artem Titovea240272021-07-26 12:40:21 +02001003 // TODO(bugs.webrtc.org/11993): Consider if deleting `audio_receive_stream`
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001004 // on the network thread would be better or if we'd need to tear down the
1005 // state in two phases.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001006 delete audio_receive_stream;
1007}
1008
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001009// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +01001010webrtc::VideoSendStream* Call::CreateVideoSendStream(
1011 webrtc::VideoSendStream::Config config,
1012 VideoEncoderConfig encoder_config,
1013 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001014 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Tommi0d4647d2020-05-26 19:35:16 +02001015 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +00001016
Erik Språng7703f232020-09-14 11:03:13 +02001017 EnsureStarted();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001018
asapersson35151f32016-05-02 23:44:01 -07001019 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -07001020 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
1021 ++ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001022 event_log_->Log(std::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001023 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -07001024 }
perkj26091b12016-09-01 01:17:40 -07001025
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +00001026 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
1027 // the call has already started.
Artem Titovea240272021-07-26 12:40:21 +02001028 // Copy ssrcs from `config` since `config` is moved.
perkj26091b12016-09-01 01:17:40 -07001029 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001030
mflodman0c478b32015-10-21 15:52:16 +02001031 VideoSendStream* send_stream = new VideoSendStream(
Markus Handelleb61b7f2021-06-22 10:46:48 +02001032 clock_, num_cpu_cores_, task_queue_factory_,
1033 call_stats_->AsRtcpRttStats(), transport_send_.get(),
Tommi822a8742020-05-11 00:42:30 +02001034 bitrate_allocator_.get(), video_send_delay_stats_.get(), event_log_,
1035 std::move(config), std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +02001036 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -07001037
Tommi0d4647d2020-05-26 19:35:16 +02001038 for (uint32_t ssrc : ssrcs) {
1039 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
1040 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001041 }
Tommi0d4647d2020-05-26 19:35:16 +02001042 video_send_streams_.insert(send_stream);
Markus Handelld9943042021-05-31 22:52:02 +02001043 video_send_streams_empty_.store(false, std::memory_order_relaxed);
1044
Henrik Boström29444c62020-07-01 15:48:46 +02001045 // Forward resources that were previously added to the call to the new stream.
1046 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
1047 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001048 }
Tommi0d4647d2020-05-26 19:35:16 +02001049
skvlad7a43d252016-03-22 15:32:27 -07001050 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -07001051
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001052 return send_stream;
1053}
1054
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001055webrtc::VideoSendStream* Call::CreateVideoSendStream(
1056 webrtc::VideoSendStream::Config config,
1057 VideoEncoderConfig encoder_config) {
Tommi948e40c2021-05-31 12:39:57 +02001058 RTC_DCHECK_RUN_ON(worker_thread_);
Ying Wang012b7e72018-03-05 15:44:23 +01001059 if (config_.fec_controller_factory) {
1060 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
1061 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001062 std::unique_ptr<FecController> fec_controller =
1063 config_.fec_controller_factory
1064 ? config_.fec_controller_factory->CreateFecController()
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001065 : std::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001066 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
1067 std::move(fec_controller));
1068}
1069
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001070void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001071 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -07001072 RTC_DCHECK(send_stream != nullptr);
Tommi0d4647d2020-05-26 19:35:16 +02001073 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001074
Tommi1050fbc2021-06-03 17:58:28 +02001075 VideoSendStream* send_stream_impl =
1076 static_cast<VideoSendStream*>(send_stream);
1077 VideoSendStream::RtpStateMap rtp_states;
1078 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
1079 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
1080 &rtp_payload_states);
Tommi0d4647d2020-05-26 19:35:16 +02001081
1082 auto it = video_send_ssrcs_.begin();
1083 while (it != video_send_ssrcs_.end()) {
1084 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
1085 send_stream_impl = it->second;
1086 video_send_ssrcs_.erase(it++);
1087 } else {
1088 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001089 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001090 }
Tommi1050fbc2021-06-03 17:58:28 +02001091
Henrik Boström29444c62020-07-01 15:48:46 +02001092 // Stop forwarding resources to the stream being destroyed.
1093 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
1094 resource_forwarder->OnDestroyVideoSendStream(send_stream_impl);
1095 }
Tommi0d4647d2020-05-26 19:35:16 +02001096 video_send_streams_.erase(send_stream_impl);
Markus Handelld9943042021-05-31 22:52:02 +02001097 if (video_send_streams_.empty())
1098 video_send_streams_empty_.store(true, std::memory_order_relaxed);
Tommi0d4647d2020-05-26 19:35:16 +02001099
Åsa Persson4bece9a2017-10-06 10:04:04 +02001100 for (const auto& kv : rtp_states) {
1101 suspended_video_send_ssrcs_[kv.first] = kv.second;
1102 }
1103 for (const auto& kv : rtp_payload_states) {
1104 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001105 }
1106
skvlad7a43d252016-03-22 15:32:27 -07001107 UpdateAggregateNetworkState();
Tommi1050fbc2021-06-03 17:58:28 +02001108 // TODO(tommi): consider deleting on the same thread as runs
1109 // StopPermanentlyAndGetRtpStates.
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001110 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001111}
1112
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001113webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +02001114 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001115 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001116 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrfb45c6c2017-01-27 06:47:55 -08001117
Johannes Kronf59666b2019-04-08 12:57:06 +02001118 receive_side_cc_.SetSendPeriodicFeedback(
1119 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +01001120
Erik Språng7703f232020-09-14 11:03:13 +02001121 EnsureStarted();
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -08001122
Artem Titovea240272021-07-26 12:40:21 +02001123 // TODO(bugs.webrtc.org/11993): Move the registration between `receive_stream`
1124 // and `video_receiver_controller_` out of VideoReceiveStream2 construction
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001125 // and set it up asynchronously on the network thread (the registration and
Artem Titovea240272021-07-26 12:40:21 +02001126 // `video_receiver_controller_` need to live on the network thread).
Tommi553c8692020-05-05 15:35:45 +02001127 VideoReceiveStream2* receive_stream = new VideoReceiveStream2(
Tommi90738dd2021-05-31 17:36:47 +02001128 task_queue_factory_, this, num_cpu_cores_,
1129 transport_send_->packet_router(), std::move(configuration),
Markus Handell0e62f7a2021-07-20 13:32:02 +02001130 call_stats_.get(), clock_, new VCMTiming(clock_),
1131 &nack_periodic_processor_);
Tommi90738dd2021-05-31 17:36:47 +02001132 // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network
1133 // thread.
1134 receive_stream->RegisterWithTransport(&video_receiver_controller_);
Tommi733b5472016-06-10 17:58:01 +02001135
1136 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
Tommi31001a62020-05-26 11:38:36 +02001137 if (config.rtp.rtx_ssrc) {
1138 // We record identical config for the rtx stream as for the main
1139 // stream. Since the transport_send_cc negotiation is per payload
1140 // type, we may get an incorrect value for the rtx stream, but
1141 // that is unlikely to matter in practice.
Tommid3500062021-06-14 19:39:45 +02001142 receive_rtp_config_.emplace(config.rtp.rtx_ssrc, receive_stream);
skvlad7a43d252016-03-22 15:32:27 -07001143 }
Tommid3500062021-06-14 19:39:45 +02001144 receive_rtp_config_.emplace(config.rtp.remote_ssrc, receive_stream);
Tommi31001a62020-05-26 11:38:36 +02001145 video_receive_streams_.insert(receive_stream);
1146 ConfigureSync(config.sync_group);
1147
skvlad7a43d252016-03-22 15:32:27 -07001148 receive_stream->SignalNetworkState(video_network_state_);
1149 UpdateAggregateNetworkState();
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001150 event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001151 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001152 return receive_stream;
1153}
1154
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001155void Call::DestroyVideoReceiveStream(
1156 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001157 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001158 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -07001159 RTC_DCHECK(receive_stream != nullptr);
Tommi553c8692020-05-05 15:35:45 +02001160 VideoReceiveStream2* receive_stream_impl =
1161 static_cast<VideoReceiveStream2*>(receive_stream);
Tommi90738dd2021-05-31 17:36:47 +02001162 // TODO(bugs.webrtc.org/11993): Unregister on the network thread.
1163 receive_stream_impl->UnregisterFromTransport();
1164
nissee4bcd6d2017-05-16 04:47:04 -07001165 const VideoReceiveStream::Config& config = receive_stream_impl->config();
Tommi31001a62020-05-26 11:38:36 +02001166
1167 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
1168 // separate SSRC there can be either one or two.
1169 receive_rtp_config_.erase(config.rtp.remote_ssrc);
1170 if (config.rtp.rtx_ssrc) {
1171 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001172 }
Tommi31001a62020-05-26 11:38:36 +02001173 video_receive_streams_.erase(receive_stream_impl);
1174 ConfigureSync(config.sync_group);
nisse4709e892017-02-07 01:18:43 -08001175
Tommid3500062021-06-14 19:39:45 +02001176 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config.rtp))
nisse4709e892017-02-07 01:18:43 -08001177 ->RemoveStream(config.rtp.remote_ssrc);
1178
skvlad7a43d252016-03-22 15:32:27 -07001179 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001180 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001181}
1182
brandtr7250b392016-12-19 01:13:46 -08001183FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
1184 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -07001185 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001186 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001187
1188 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -07001189
nisse0f15f922017-06-21 01:05:22 -07001190 FlexfecReceiveStreamImpl* receive_stream;
brandtrb29e6522016-12-21 06:37:18 -08001191
Tommi31001a62020-05-26 11:38:36 +02001192 // Unlike the video and audio receive streams, FlexfecReceiveStream implements
Artem Titovea240272021-07-26 12:40:21 +02001193 // RtpPacketSinkInterface itself, and hence its constructor passes its `this`
Tommi31001a62020-05-26 11:38:36 +02001194 // pointer to video_receiver_controller_->CreateStream(). Calling the
1195 // constructor while on the worker thread ensures that we don't call
1196 // OnRtpPacket until the constructor is finished and the object is
1197 // in a valid state, since OnRtpPacket runs on the same thread.
1198 receive_stream = new FlexfecReceiveStreamImpl(
Markus Handelleb61b7f2021-06-22 10:46:48 +02001199 clock_, config, recovered_packet_receiver, call_stats_->AsRtcpRttStats());
Tommi0377bab2021-05-31 14:26:05 +02001200
1201 // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network
1202 // thread.
1203 receive_stream->RegisterWithTransport(&video_receiver_controller_);
Tommi31001a62020-05-26 11:38:36 +02001204
Tommi1c1f5402021-06-14 10:54:20 +02001205 RTC_DCHECK(receive_rtp_config_.find(config.rtp.remote_ssrc) ==
Tommi31001a62020-05-26 11:38:36 +02001206 receive_rtp_config_.end());
Tommid3500062021-06-14 19:39:45 +02001207 receive_rtp_config_.emplace(config.rtp.remote_ssrc, receive_stream);
brandtrb29e6522016-12-21 06:37:18 -08001208
brandtr25445d32016-10-23 23:37:14 -07001209 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001210
brandtr25445d32016-10-23 23:37:14 -07001211 return receive_stream;
1212}
1213
brandtr7250b392016-12-19 01:13:46 -08001214void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001215 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001216 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001217
Tommi0377bab2021-05-31 14:26:05 +02001218 FlexfecReceiveStreamImpl* receive_stream_impl =
1219 static_cast<FlexfecReceiveStreamImpl*>(receive_stream);
1220 // TODO(bugs.webrtc.org/11993): Unregister on the network thread.
1221 receive_stream_impl->UnregisterFromTransport();
1222
brandtr25445d32016-10-23 23:37:14 -07001223 RTC_DCHECK(receive_stream != nullptr);
Tommid3500062021-06-14 19:39:45 +02001224 const FlexfecReceiveStream::RtpConfig& rtp = receive_stream->rtp_config();
1225 receive_rtp_config_.erase(rtp.remote_ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001226
Tommi31001a62020-05-26 11:38:36 +02001227 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1228 // destroyed.
Tommid3500062021-06-14 19:39:45 +02001229 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(rtp))
1230 ->RemoveStream(rtp.remote_ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001231
eladalon42f44f92017-07-25 06:40:06 -07001232 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001233}
1234
Henrik Boströmf4a99912020-06-11 12:07:14 +02001235void Call::AddAdaptationResource(rtc::scoped_refptr<Resource> resource) {
1236 RTC_DCHECK_RUN_ON(worker_thread_);
Henrik Boström29444c62020-07-01 15:48:46 +02001237 adaptation_resource_forwarders_.push_back(
1238 std::make_unique<ResourceVideoSendStreamForwarder>(resource));
1239 const auto& resource_forwarder = adaptation_resource_forwarders_.back();
1240 for (VideoSendStream* send_stream : video_send_streams_) {
1241 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001242 }
1243}
1244
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001245RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Tommi948e40c2021-05-31 12:39:57 +02001246 return transport_send_.get();
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001247}
1248
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001249Call::Stats Call::GetStats() const {
Tommi0d4647d2020-05-26 19:35:16 +02001250 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi48b48e52019-08-09 11:42:32 +02001251
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001252 Stats stats;
Tommi48b48e52019-08-09 11:42:32 +02001253 // TODO(srte): It is unclear if we only want to report queues if network is
1254 // available.
1255 stats.pacer_delay_ms =
Tommi948e40c2021-05-31 12:39:57 +02001256 aggregate_network_up_ ? transport_send_->GetPacerQueuingDelayMs() : 0;
Tommi48b48e52019-08-09 11:42:32 +02001257
1258 stats.rtt_ms = call_stats_->LastProcessedRtt();
1259
Peter Boström45553ae2015-05-08 13:54:38 +02001260 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001261 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001262 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001263 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001264 &ssrcs, &recv_bandwidth);
Tommi48b48e52019-08-09 11:42:32 +02001265 stats.recv_bandwidth_bps = recv_bandwidth;
Markus Handelld9943042021-05-31 22:52:02 +02001266 stats.send_bandwidth_bps =
1267 last_bandwidth_bps_.load(std::memory_order_relaxed);
1268 stats.max_padding_bitrate_bps =
1269 configured_max_padding_bitrate_bps_.load(std::memory_order_relaxed);
Tommi48b48e52019-08-09 11:42:32 +02001270
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001271 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001272}
1273
Erik Språngceb44952020-09-22 11:36:35 +02001274const WebRtcKeyValueConfig& Call::trials() const {
Tommi948e40c2021-05-31 12:39:57 +02001275 return trials_;
Erik Språngceb44952020-09-22 11:36:35 +02001276}
1277
Tomas Gunnarssone984aa22021-04-19 09:21:06 +02001278TaskQueueBase* Call::network_thread() const {
1279 return network_thread_;
1280}
1281
1282TaskQueueBase* Call::worker_thread() const {
1283 return worker_thread_;
1284}
1285
skvlad7a43d252016-03-22 15:32:27 -07001286void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001287 RTC_DCHECK_RUN_ON(network_thread_);
1288 RTC_DCHECK(media == MediaType::AUDIO || media == MediaType::VIDEO);
Tomas Gunnarssond48a2b12021-02-02 17:57:36 +01001289
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001290 auto closure = [this, media, state]() {
1291 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1292 RTC_DCHECK_RUN_ON(worker_thread_);
1293 if (media == MediaType::AUDIO) {
1294 audio_network_state_ = state;
1295 } else {
1296 RTC_DCHECK_EQ(media, MediaType::VIDEO);
1297 video_network_state_ = state;
1298 }
1299
1300 // TODO(tommi): Is it necessary to always do this, including if there
1301 // was no change in state?
1302 UpdateAggregateNetworkState();
1303
1304 // TODO(tommi): Is it right to do this if media == AUDIO?
1305 for (VideoReceiveStream2* video_receive_stream : video_receive_streams_) {
1306 video_receive_stream->SignalNetworkState(video_network_state_);
1307 }
1308 };
1309
1310 if (network_thread_ == worker_thread_) {
1311 closure();
1312 } else {
1313 // TODO(bugs.webrtc.org/11993): Remove workaround when we no longer need to
1314 // post to the worker thread.
1315 worker_thread_->PostTask(ToQueuedTask(task_safety_, std::move(closure)));
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001316 }
1317}
1318
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001319void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001320 RTC_DCHECK_RUN_ON(network_thread_);
1321 worker_thread_->PostTask(
1322 ToQueuedTask(task_safety_, [this, transport_overhead_per_packet]() {
1323 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1324 RTC_DCHECK_RUN_ON(worker_thread_);
1325 for (auto& kv : audio_send_ssrcs_) {
1326 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1327 }
1328 }));
michaelt79e05882016-11-08 02:50:09 -08001329}
1330
skvlad7a43d252016-03-22 15:32:27 -07001331void Call::UpdateAggregateNetworkState() {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001332 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1333 // RTC_DCHECK_RUN_ON(network_thread_);
1334
Tommi0d4647d2020-05-26 19:35:16 +02001335 RTC_DCHECK_RUN_ON(worker_thread_);
skvlad7a43d252016-03-22 15:32:27 -07001336
Tommi0d4647d2020-05-26 19:35:16 +02001337 bool have_audio =
1338 !audio_send_ssrcs_.empty() || !audio_receive_streams_.empty();
1339 bool have_video =
1340 !video_send_ssrcs_.empty() || !video_receive_streams_.empty();
skvlad7a43d252016-03-22 15:32:27 -07001341
Sebastian Janssona06e9192018-03-07 18:49:55 +01001342 bool aggregate_network_up =
1343 ((have_video && video_network_state_ == kNetworkUp) ||
1344 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001345
Harald Alvestrand977b2652019-12-12 13:40:50 +01001346 if (aggregate_network_up != aggregate_network_up_) {
1347 RTC_LOG(LS_INFO)
1348 << "UpdateAggregateNetworkState: aggregate_state change to "
1349 << (aggregate_network_up ? "up" : "down");
1350 } else {
1351 RTC_LOG(LS_VERBOSE)
1352 << "UpdateAggregateNetworkState: aggregate_state remains at "
1353 << (aggregate_network_up ? "up" : "down");
1354 }
Tommi48b48e52019-08-09 11:42:32 +02001355 aggregate_network_up_ = aggregate_network_up;
1356
Tommi948e40c2021-05-31 12:39:57 +02001357 transport_send_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001358}
1359
Tommi08be9ba2021-06-15 23:01:57 +02001360void Call::OnLocalSsrcUpdated(webrtc::AudioReceiveStream& stream,
1361 uint32_t local_ssrc) {
1362 RTC_DCHECK_RUN_ON(worker_thread_);
1363 webrtc::internal::AudioReceiveStream& receive_stream =
1364 static_cast<webrtc::internal::AudioReceiveStream&>(stream);
1365
1366 receive_stream.SetLocalSsrc(local_ssrc);
1367 auto it = audio_send_ssrcs_.find(local_ssrc);
1368 receive_stream.AssociateSendStream(it != audio_send_ssrcs_.end() ? it->second
1369 : nullptr);
1370}
1371
Tommi55107c82021-06-16 16:31:18 +02001372void Call::OnUpdateSyncGroup(webrtc::AudioReceiveStream& stream,
1373 const std::string& sync_group) {
1374 RTC_DCHECK_RUN_ON(worker_thread_);
1375 webrtc::internal::AudioReceiveStream& receive_stream =
1376 static_cast<webrtc::internal::AudioReceiveStream&>(stream);
1377 receive_stream.SetSyncGroup(sync_group);
1378 ConfigureSync(sync_group);
1379}
1380
stefanc1aeaf02015-10-15 07:26:07 -07001381void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
Tomas Gunnarssoneb9c3f22021-04-19 12:53:09 +02001382 // In production and with most tests, this method will be called on the
1383 // network thread. However some test classes such as DirectTransport don't
1384 // incorporate a network thread. This means that tests for RtpSenderEgress
1385 // and ModuleRtpRtcpImpl2 that use DirectTransport, will call this method
1386 // on a ProcessThread. This is alright as is since we forward the call to
1387 // implementations that either just do a PostTask or use locking.
asapersson35151f32016-05-02 23:44:01 -07001388 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1389 clock_->TimeInMilliseconds());
Tommi948e40c2021-05-31 12:39:57 +02001390 transport_send_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001391}
1392
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001393void Call::OnStartRateUpdate(DataRate start_rate) {
Markus Handelld9943042021-05-31 22:52:02 +02001394 RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001395 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1396}
1397
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001398void Call::OnTargetTransferRate(TargetTransferRate msg) {
Markus Handelld9943042021-05-31 22:52:02 +02001399 RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001400
1401 uint32_t target_bitrate_bps = msg.target_rate.bps();
nisse559af382017-03-21 06:41:12 -07001402 // For controlling the rate of feedback messages.
1403 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001404 bitrate_allocator_->OnNetworkEstimateChanged(msg);
mflodman0e7e2592015-11-12 21:02:42 -08001405
Markus Handelld9943042021-05-31 22:52:02 +02001406 last_bandwidth_bps_.store(target_bitrate_bps, std::memory_order_relaxed);
asaperssonce2e1362016-09-09 00:13:35 -07001407
Markus Handelld9943042021-05-31 22:52:02 +02001408 // Ignore updates if bitrate is zero (the aggregate network state is
1409 // down) or if we're not sending video.
Artem Titovea240272021-07-26 12:40:21 +02001410 // Using `video_send_streams_empty_` is racy but as the caller can't
1411 // reasonably expect synchronize with changes in `video_send_streams_` (being
1412 // on `send_transport_sequence_checker`), we can avoid a PostTask this way.
Markus Handelld9943042021-05-31 22:52:02 +02001413 if (target_bitrate_bps == 0 ||
1414 video_send_streams_empty_.load(std::memory_order_relaxed)) {
1415 send_stats_.PauseSendAndPacerBitrateCounters();
1416 } else {
1417 send_stats_.AddTargetBitrateSample(target_bitrate_bps);
1418 }
perkj71ee44c2016-06-15 00:47:53 -07001419}
mflodman101f2502016-06-09 17:21:19 +02001420
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001421void Call::OnAllocationLimitsChanged(BitrateAllocationLimits limits) {
Markus Handelld9943042021-05-31 22:52:02 +02001422 RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_);
Tommi48b48e52019-08-09 11:42:32 +02001423
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001424 transport_send_ptr_->SetAllocatedSendBitrateLimits(limits);
Markus Handelld9943042021-05-31 22:52:02 +02001425 send_stats_.SetMinAllocatableRate(limits);
1426 configured_max_padding_bitrate_bps_.store(limits.max_padding_rate.bps(),
1427 std::memory_order_relaxed);
mflodman0e7e2592015-11-12 21:02:42 -08001428}
1429
Tommi6eda26c2021-06-09 13:46:28 +02001430// RTC_RUN_ON(worker_thread_)
pbos8fc7fa72015-07-15 08:02:58 -07001431void Call::ConfigureSync(const std::string& sync_group) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001432 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
pbos8fc7fa72015-07-15 08:02:58 -07001433 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001434 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001435 return;
1436
1437 AudioReceiveStream* sync_audio_stream = nullptr;
1438 // Find existing audio stream.
1439 const auto it = sync_stream_mapping_.find(sync_group);
1440 if (it != sync_stream_mapping_.end()) {
1441 sync_audio_stream = it->second;
1442 } else {
1443 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001444 for (AudioReceiveStream* stream : audio_receive_streams_) {
1445 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001446 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001447 RTC_LOG(LS_WARNING)
1448 << "Attempting to sync more than one audio stream "
1449 "within the same sync group. This is not "
1450 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001451 break;
1452 }
nissee4bcd6d2017-05-16 04:47:04 -07001453 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001454 }
1455 }
1456 }
1457 if (sync_audio_stream)
1458 sync_stream_mapping_[sync_group] = sync_audio_stream;
1459 size_t num_synced_streams = 0;
Tommi553c8692020-05-05 15:35:45 +02001460 for (VideoReceiveStream2* video_stream : video_receive_streams_) {
pbos8fc7fa72015-07-15 08:02:58 -07001461 if (video_stream->config().sync_group != sync_group)
1462 continue;
1463 ++num_synced_streams;
1464 if (num_synced_streams > 1) {
1465 // TODO(pbos): Support synchronizing more than one A/V pair.
1466 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001467 RTC_LOG(LS_WARNING)
1468 << "Attempting to sync more than one audio/video pair "
1469 "within the same sync group. This is not supported in "
1470 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001471 }
1472 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001473 if (num_synced_streams == 1) {
1474 // sync_audio_stream may be null and that's ok.
1475 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001476 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001477 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001478 }
1479 }
1480}
1481
Tommicae1f1d2021-05-31 10:51:09 +02001482// RTC_RUN_ON(network_thread_)
1483void Call::DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001484 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
Tommi3f418cc2021-05-05 11:04:30 +02001485
1486 // TODO(bugs.webrtc.org/11993): This DCHECK is here just to maintain the
1487 // invariant that currently the only call path to this function is via
1488 // `PeerConnection::InitializeRtcpCallback()`. DeliverRtp on the other hand
1489 // gets called via the channel classes and
1490 // WebRtc[Audio|Video]Channel's `OnPacketReceived`. We'll remove the
1491 // PeerConnection involvement as well as
1492 // `JsepTransportController::OnRtcpPacketReceived_n` and `rtcp_handler`
1493 // and make sure that the flow of packets is consistent from the
1494 // `RtpTransport` class, via the *Channel and *Engine classes and into Call.
1495 // This way we'll also know more about the context of the packet.
1496 RTC_DCHECK_EQ(media_type, MediaType::ANY);
1497
Tommicae1f1d2021-05-31 10:51:09 +02001498 // TODO(bugs.webrtc.org/11993): This should execute directly on the network
1499 // thread.
1500 worker_thread_->PostTask(
1501 ToQueuedTask(task_safety_, [this, packet = std::move(packet)]() {
1502 RTC_DCHECK_RUN_ON(worker_thread_);
mflodman3d7db262016-04-29 00:57:13 -07001503
Tommicae1f1d2021-05-31 10:51:09 +02001504 receive_stats_.AddReceivedRtcpBytes(static_cast<int>(packet.size()));
1505 bool rtcp_delivered = false;
1506 for (VideoReceiveStream2* stream : video_receive_streams_) {
1507 if (stream->DeliverRtcp(packet.cdata(), packet.size()))
1508 rtcp_delivered = true;
1509 }
mflodman3d7db262016-04-29 00:57:13 -07001510
Tommicae1f1d2021-05-31 10:51:09 +02001511 for (AudioReceiveStream* stream : audio_receive_streams_) {
1512 stream->DeliverRtcp(packet.cdata(), packet.size());
1513 rtcp_delivered = true;
1514 }
1515
1516 for (VideoSendStream* stream : video_send_streams_) {
1517 stream->DeliverRtcp(packet.cdata(), packet.size());
1518 rtcp_delivered = true;
1519 }
1520
1521 for (auto& kv : audio_send_ssrcs_) {
1522 kv.second->DeliverRtcp(packet.cdata(), packet.size());
1523 rtcp_delivered = true;
1524 }
1525
1526 if (rtcp_delivered) {
1527 event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>(
1528 rtc::MakeArrayView(packet.cdata(), packet.size())));
1529 }
1530 }));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001531}
1532
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001533PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001534 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001535 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001536 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
Tommi3f418cc2021-05-05 11:04:30 +02001537 RTC_DCHECK_NE(media_type, MediaType::ANY);
nissed44ce052017-02-06 02:23:00 -08001538
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001539 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001540 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001541 return DELIVERY_PACKET_ERROR;
1542
Niels Möller70082872018-08-07 11:03:12 +02001543 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001544 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001545 // Repair packet_time_us for clock resets by comparing a new read of
1546 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001547 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001548 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001549 }
Tommi2497a272021-05-05 12:33:00 +02001550 parsed_packet.set_arrival_time(Timestamp::Micros(packet_time_us));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001551 } else {
Tommi2497a272021-05-05 12:33:00 +02001552 parsed_packet.set_arrival_time(clock_->CurrentTime());
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001553 }
nissed44ce052017-02-06 02:23:00 -08001554
sprangc1abde72017-07-11 03:56:21 -07001555 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1556 // These are empty (zero length payload) RTP packets with an unsignaled
1557 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001558 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001559
1560 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1561 is_keep_alive_packet);
1562
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001563 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001564 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001565 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1566 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001567 // Destruction of the receive stream, including deregistering from the
Artem Titovea240272021-07-26 12:40:21 +02001568 // RtpDemuxer, is not protected by the `worker_thread_`.
1569 // But deregistering in the `receive_rtp_config_` map is. So by not passing
Tommi31001a62020-05-26 11:38:36 +02001570 // the packet on to demuxing in this case, we prevent incoming packets to be
1571 // passed on via the demuxer to a receive stream which is being torned down.
nisse0f15f922017-06-21 01:05:22 -07001572 return DELIVERY_UNKNOWN_SSRC;
1573 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001574
Tommid3500062021-06-14 19:39:45 +02001575 parsed_packet.IdentifyExtensions(
1576 RtpHeaderExtensionMap(it->second->rtp_config().extensions));
nisse0f15f922017-06-21 01:05:22 -07001577
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001578 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001579
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001580 // RateCounters expect input parameter as int, save it as int,
1581 // instead of converting each time it is passed to RateCounter::Add below.
1582 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001583 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001584 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001585 receive_stats_.AddReceivedAudioBytes(length,
1586 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001587 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001588 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse657bab22017-02-21 06:28:10 -08001589 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001590 }
nissee4bcd6d2017-05-16 04:47:04 -07001591 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001592 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001593 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001594 receive_stats_.AddReceivedVideoBytes(length,
1595 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001596 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001597 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse5c29a7a2017-02-16 06:52:32 -08001598 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001599 }
1600 }
1601 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001602}
1603
stefan68786d22015-09-08 05:36:15 -07001604PacketReceiver::DeliveryStatus Call::DeliverPacket(
1605 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001606 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001607 int64_t packet_time_us) {
Danil Chapovalov00ca0042021-07-05 19:06:17 +02001608 if (IsRtcpPacket(packet)) {
Tommicae1f1d2021-05-31 10:51:09 +02001609 RTC_DCHECK_RUN_ON(network_thread_);
1610 DeliverRtcp(media_type, std::move(packet));
1611 return DELIVERY_OK;
1612 }
1613
Tommi0d4647d2020-05-26 19:35:16 +02001614 RTC_DCHECK_RUN_ON(worker_thread_);
Niels Möller70082872018-08-07 11:03:12 +02001615 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001616}
1617
nissed2ef3142017-05-11 08:00:58 -07001618void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001619 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
Artem Titovea240272021-07-26 12:40:21 +02001620 // This method is called synchronously via `OnRtpPacket()` (see DeliverRtp)
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001621 // on the same thread.
Tommi0d4647d2020-05-26 19:35:16 +02001622 RTC_DCHECK_RUN_ON(worker_thread_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001623 RtpPacketReceived parsed_packet;
1624 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001625 return;
1626
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001627 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001628
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001629 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001630 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001631 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1632 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001633 // Destruction of the receive stream, including deregistering from the
Artem Titovea240272021-07-26 12:40:21 +02001634 // RtpDemuxer, is not protected by the `worker_thread_`.
1635 // But deregistering in the `receive_rtp_config_` map is.
brandtrcaea68f2017-08-23 00:55:17 -07001636 // So by not passing the packet on to demuxing in this case, we prevent
1637 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001638 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001639 return;
1640 }
Tommid3500062021-06-14 19:39:45 +02001641 parsed_packet.IdentifyExtensions(
1642 RtpHeaderExtensionMap(it->second->rtp_config().extensions));
brandtrcaea68f2017-08-23 00:55:17 -07001643
1644 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001645 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001646 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001647}
1648
Tommi948e40c2021-05-31 12:39:57 +02001649// RTC_RUN_ON(worker_thread_)
nissed44ce052017-02-06 02:23:00 -08001650void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1651 MediaType media_type) {
1652 auto it = receive_rtp_config_.find(packet.Ssrc());
Tommid3500062021-06-14 19:39:45 +02001653 bool use_send_side_bwe = (it != receive_rtp_config_.end()) &&
1654 UseSendSideBwe(it->second->rtp_config());
nissed44ce052017-02-06 02:23:00 -08001655
brandtrb29e6522016-12-21 06:37:18 -08001656 RTPHeader header;
1657 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001658
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001659 ReceivedPacket packet_msg;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001660 packet_msg.size = DataSize::Bytes(packet.payload_size());
Tommi2497a272021-05-05 12:33:00 +02001661 packet_msg.receive_time = packet.arrival_time();
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001662 if (header.extension.hasAbsoluteSendTime) {
1663 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1664 }
Tommi948e40c2021-05-31 12:39:57 +02001665 transport_send_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001666
nisse4709e892017-02-07 01:18:43 -08001667 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001668 // Inconsistent configuration of send side BWE. Do nothing.
1669 // TODO(nisse): Without this check, we may produce RTCP feedback
1670 // packets even when not negotiated. But it would be cleaner to
1671 // move the check down to RTCPSender::SendFeedbackPacket, which
1672 // would also help the PacketRouter to select an appropriate rtp
1673 // module in the case that some, but not all, have RTCP feedback
1674 // enabled.
1675 return;
1676 }
1677 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001678 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001679 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001680 receive_side_cc_.OnReceivedPacket(
Tommi2497a272021-05-05 12:33:00 +02001681 packet.arrival_time().ms(),
1682 packet.payload_size() + packet.padding_size(), header);
nissed44ce052017-02-06 02:23:00 -08001683 }
brandtrb29e6522016-12-21 06:37:18 -08001684}
1685
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001686} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001687
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001688} // namespace webrtc