blob: b8eafdc9234682392224abe2194ffa25d30d366e [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>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000016#include <map>
kwibergb25345e2016-03-12 06:10:44 -080017#include <memory>
ossuf515ab82016-12-07 04:52:58 -080018#include <set>
brandtr25445d32016-10-23 23:37:14 -070019#include <utility>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000020#include <vector>
21
Per Kjellanderfe2063e2021-05-12 09:02:43 +020022#include "absl/functional/bind_front.h"
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020023#include "absl/types/optional.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020024#include "api/rtc_event_log/rtc_event_log.h"
Artem Titovd15a5752021-02-10 14:31:24 +010025#include "api/sequence_checker.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020026#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "audio/audio_receive_stream.h"
28#include "audio/audio_send_stream.h"
29#include "audio/audio_state.h"
Henrik Boström29444c62020-07-01 15:48:46 +020030#include "call/adaptation/broadcast_resource_listener.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "call/bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010033#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "call/rtp_stream_receiver_controller.h"
35#include "call/rtp_transport_controller_send.h"
Mirko Bonadeib9857482020-12-14 15:28:43 +010036#include "call/version.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020037#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020038#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
39#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
40#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
41#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Elad Alon99a81b62017-09-21 10:25:29 +020042#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
44#include "modules/rtp_rtcp/include/flexfec_receiver.h"
45#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "modules/rtp_rtcp/source/byte_io.h"
47#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Tommi25eb47c2019-08-29 16:39:05 +020048#include "modules/rtp_rtcp/source/rtp_utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010050#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080052#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "rtc_base/location.h"
54#include "rtc_base/logging.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020055#include "rtc_base/strings/string_builder.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010056#include "rtc_base/system/no_unique_address.h"
Tommi0d4647d2020-05-26 19:35:16 +020057#include "rtc_base/task_utils/pending_task_safety_flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080059#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/trace_event.h"
61#include "system_wrappers/include/clock.h"
62#include "system_wrappers/include/cpu_info.h"
Jonas Oreland6d835922019-03-18 10:59:40 +010063#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#include "system_wrappers/include/metrics.h"
Tommi822a8742020-05-11 00:42:30 +020065#include "video/call_stats2.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020066#include "video/send_delay_stats.h"
67#include "video/stats_counter.h"
Tommi553c8692020-05-05 15:35:45 +020068#include "video/video_receive_stream2.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020069#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000070
71namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000072
nisse4709e892017-02-07 01:18:43 -080073namespace {
Johannes Kronf59666b2019-04-08 12:57:06 +020074bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010075 for (const auto& extension : extensions) {
76 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
Johannes Kronf59666b2019-04-08 12:57:06 +020077 return false;
Johannes Kron7ff164e2019-02-07 12:50:18 +010078 }
Johannes Kronf59666b2019-04-08 12:57:06 +020079 return true;
Johannes Kron7ff164e2019-02-07 12:50:18 +010080}
81
nisse4709e892017-02-07 01:18:43 -080082// TODO(nisse): This really begs for a shared context struct.
83bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
84 bool transport_cc) {
85 if (!transport_cc)
86 return false;
87 for (const auto& extension : 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
95bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
96 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
97}
98
99bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
100 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
101}
102
103bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
104 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
105}
106
nisse26e3abb2017-08-25 04:44:25 -0700107const int* FindKeyByValue(const std::map<int, int>& m, int v) {
108 for (const auto& kv : m) {
109 if (kv.second == v)
110 return &kv.first;
111 }
112 return nullptr;
113}
114
eladalon8ec568a2017-09-08 06:15:52 -0700115std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700116 const VideoReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200117 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700118 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
119 rtclog_config->local_ssrc = config.rtp.local_ssrc;
120 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
121 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
eladalon8ec568a2017-09-08 06:15:52 -0700122 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700123
124 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700125 const int* search =
126 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200127 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200128 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700129 }
130 return rtclog_config;
131}
132
eladalon8ec568a2017-09-08 06:15:52 -0700133std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700134 const VideoSendStream::Config& config,
135 size_t ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200136 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700137 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700138 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700139 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700140 }
eladalon8ec568a2017-09-08 06:15:52 -0700141 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
142 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700143
Niels Möller259a4972018-04-05 15:36:51 +0200144 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
145 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700146 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700147 return rtclog_config;
148}
149
eladalon8ec568a2017-09-08 06:15:52 -0700150std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700151 const AudioReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200152 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700153 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
154 rtclog_config->local_ssrc = config.rtp.local_ssrc;
155 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700156 return rtclog_config;
157}
158
Tommi25eb47c2019-08-29 16:39:05 +0200159bool IsRtcp(const uint8_t* packet, size_t length) {
160 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
161 return rtp_parser.RTCP();
162}
163
Tommi822a8742020-05-11 00:42:30 +0200164TaskQueueBase* GetCurrentTaskQueueOrThread() {
165 TaskQueueBase* current = TaskQueueBase::Current();
166 if (!current)
167 current = rtc::ThreadManager::Instance()->CurrentThread();
168 return current;
169}
170
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100171// Called from the destructor of Call to report the collected send histograms.
172void UpdateSendHistograms(Timestamp now,
173 Timestamp first_sent_packet,
174 AvgCounter& estimated_send_bitrate_kbps_counter,
175 AvgCounter& pacer_bitrate_kbps_counter) {
176 TimeDelta elapsed = now - first_sent_packet;
177 if (elapsed.seconds() < metrics::kMinRunTimeInSeconds)
178 return;
179
180 const int kMinRequiredPeriodicSamples = 5;
181 AggregatedStats send_bitrate_stats =
182 estimated_send_bitrate_kbps_counter.ProcessAndGetStats();
183 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
184 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
185 send_bitrate_stats.average);
186 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
187 << send_bitrate_stats.ToString();
188 }
189 AggregatedStats pacer_bitrate_stats =
190 pacer_bitrate_kbps_counter.ProcessAndGetStats();
191 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
192 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
193 pacer_bitrate_stats.average);
194 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
195 << pacer_bitrate_stats.ToString();
196 }
197}
198
nisse4709e892017-02-07 01:18:43 -0800199} // namespace
200
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000201namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000202
Henrik Boström29444c62020-07-01 15:48:46 +0200203// Wraps an injected resource in a BroadcastResourceListener and handles adding
204// and removing adapter resources to individual VideoSendStreams.
205class ResourceVideoSendStreamForwarder {
206 public:
207 ResourceVideoSendStreamForwarder(
208 rtc::scoped_refptr<webrtc::Resource> resource)
209 : broadcast_resource_listener_(resource) {
210 broadcast_resource_listener_.StartListening();
211 }
212 ~ResourceVideoSendStreamForwarder() {
213 RTC_DCHECK(adapter_resources_.empty());
214 broadcast_resource_listener_.StopListening();
215 }
216
217 rtc::scoped_refptr<webrtc::Resource> Resource() const {
218 return broadcast_resource_listener_.SourceResource();
219 }
220
221 void OnCreateVideoSendStream(VideoSendStream* video_send_stream) {
222 RTC_DCHECK(adapter_resources_.find(video_send_stream) ==
223 adapter_resources_.end());
224 auto adapter_resource =
225 broadcast_resource_listener_.CreateAdapterResource();
226 video_send_stream->AddAdaptationResource(adapter_resource);
227 adapter_resources_.insert(
228 std::make_pair(video_send_stream, adapter_resource));
229 }
230
231 void OnDestroyVideoSendStream(VideoSendStream* video_send_stream) {
232 auto it = adapter_resources_.find(video_send_stream);
233 RTC_DCHECK(it != adapter_resources_.end());
234 broadcast_resource_listener_.RemoveAdapterResource(it->second);
235 adapter_resources_.erase(it);
236 }
237
238 private:
239 BroadcastResourceListener broadcast_resource_listener_;
240 std::map<VideoSendStream*, rtc::scoped_refptr<webrtc::Resource>>
241 adapter_resources_;
242};
243
Sebastian Janssone6256052018-05-04 14:08:15 +0200244class Call final : public webrtc::Call,
245 public PacketReceiver,
246 public RecoveredPacketReceiver,
247 public TargetTransferRateObserver,
248 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000249 public:
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100250 Call(Clock* clock,
251 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100252 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
Tommi25c77c12020-05-25 17:44:55 +0200253 rtc::scoped_refptr<SharedModuleThread> module_process_thread,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100254 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200255 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000256
brandtr25445d32016-10-23 23:37:14 -0700257 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000258 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000259
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200260 webrtc::AudioSendStream* CreateAudioSendStream(
261 const webrtc::AudioSendStream::Config& config) override;
262 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
263
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200264 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
265 const webrtc::AudioReceiveStream::Config& config) override;
266 void DestroyAudioReceiveStream(
267 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000268
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200269 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700270 webrtc::VideoSendStream::Config config,
271 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100272 webrtc::VideoSendStream* CreateVideoSendStream(
273 webrtc::VideoSendStream::Config config,
274 VideoEncoderConfig encoder_config,
275 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000276 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000277
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200278 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200279 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000280 void DestroyVideoReceiveStream(
281 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000282
brandtr7250b392016-12-19 01:13:46 -0800283 FlexfecReceiveStream* CreateFlexfecReceiveStream(
284 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700285 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800286 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700287
Henrik Boströmf4a99912020-06-11 12:07:14 +0200288 void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
289
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100290 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
291
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000292 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000293
Erik Språngceb44952020-09-22 11:36:35 +0200294 const WebRtcKeyValueConfig& trials() const override;
295
Tomas Gunnarssone984aa22021-04-19 09:21:06 +0200296 TaskQueueBase* network_thread() const override;
297 TaskQueueBase* worker_thread() const override;
298
brandtr25445d32016-10-23 23:37:14 -0700299 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700300 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100301 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200302 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000303
brandtr4e523862016-10-18 23:50:45 -0700304 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700305 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700306
skvlad7a43d252016-03-22 15:32:27 -0700307 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000308
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200309 void OnAudioTransportOverheadChanged(
310 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800311
stefanc1aeaf02015-10-15 07:26:07 -0700312 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
313
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100314 // Implements TargetTransferRateObserver,
315 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100316 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800317
perkj71ee44c2016-06-15 00:47:53 -0700318 // Implements BitrateAllocator::LimitObserver.
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200319 void OnAllocationLimitsChanged(BitrateAllocationLimits limits) override;
perkj71ee44c2016-06-15 00:47:53 -0700320
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700321 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
322
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000323 private:
Markus Handellc81afe32021-05-31 09:02:01 +0200324 // Thread-compatible class that collects received packet stats and exposes
325 // them as UMA histograms on destruction.
326 class ReceiveStats {
327 public:
328 explicit ReceiveStats(Clock* clock);
329 ~ReceiveStats();
330
331 void AddReceivedRtcpBytes(int bytes);
332 void AddReceivedAudioBytes(int bytes, webrtc::Timestamp arrival_time);
333 void AddReceivedVideoBytes(int bytes, webrtc::Timestamp arrival_time);
334
335 private:
336 SequenceChecker sequence_checker_;
337 RateCounter received_bytes_per_second_counter_
338 RTC_GUARDED_BY(sequence_checker_);
339 RateCounter received_audio_bytes_per_second_counter_
340 RTC_GUARDED_BY(sequence_checker_);
341 RateCounter received_video_bytes_per_second_counter_
342 RTC_GUARDED_BY(sequence_checker_);
343 RateCounter received_rtcp_bytes_per_second_counter_
344 RTC_GUARDED_BY(sequence_checker_);
345 absl::optional<Timestamp> first_received_rtp_audio_timestamp_
346 RTC_GUARDED_BY(sequence_checker_);
347 absl::optional<Timestamp> last_received_rtp_audio_timestamp_
348 RTC_GUARDED_BY(sequence_checker_);
349 absl::optional<Timestamp> first_received_rtp_video_timestamp_
350 RTC_GUARDED_BY(sequence_checker_);
351 absl::optional<Timestamp> last_received_rtp_video_timestamp_
352 RTC_GUARDED_BY(sequence_checker_);
353 };
354
Tommicae1f1d2021-05-31 10:51:09 +0200355 void DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet)
356 RTC_RUN_ON(network_thread_);
stefan68786d22015-09-08 05:36:15 -0700357 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100358 rtc::CopyOnWriteBuffer packet,
Tommicae1f1d2021-05-31 10:51:09 +0200359 int64_t packet_time_us) RTC_RUN_ON(worker_thread_);
360 void ConfigureSync(const std::string& sync_group) RTC_RUN_ON(worker_thread_);
pbos8fc7fa72015-07-15 08:02:58 -0700361
nissed44ce052017-02-06 02:23:00 -0800362 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
363 MediaType media_type)
Tommi948e40c2021-05-31 12:39:57 +0200364 RTC_RUN_ON(worker_thread_);
nissed44ce052017-02-06 02:23:00 -0800365
skvlad7a43d252016-03-22 15:32:27 -0700366 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800367
Erik Språng7703f232020-09-14 11:03:13 +0200368 // Ensure that necessary process threads are started, and any required
369 // callbacks have been registered.
Tommicae1f1d2021-05-31 10:51:09 +0200370 void EnsureStarted() RTC_RUN_ON(worker_thread_);
Niels Möller46879152019-01-07 15:54:47 +0100371
Peter Boströmd3c94472015-12-09 11:20:58 +0100372 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100373 TaskQueueFactory* const task_queue_factory_;
Tommi0d4647d2020-05-26 19:35:16 +0200374 TaskQueueBase* const worker_thread_;
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100375 TaskQueueBase* const network_thread_;
stefan91d92602015-11-11 10:13:02 -0800376
Peter Boström45553ae2015-05-08 13:54:38 +0200377 const int num_cpu_cores_;
Tommi25c77c12020-05-25 17:44:55 +0200378 const rtc::scoped_refptr<SharedModuleThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800379 const std::unique_ptr<CallStats> call_stats_;
380 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
Tommi948e40c2021-05-31 12:39:57 +0200381 const Call::Config config_ RTC_GUARDED_BY(worker_thread_);
382 // Maps to config_.trials, can be used from any thread via `trials()`.
383 const WebRtcKeyValueConfig& trials_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000384
Tommi948e40c2021-05-31 12:39:57 +0200385 NetworkState audio_network_state_ RTC_GUARDED_BY(worker_thread_);
386 NetworkState video_network_state_ RTC_GUARDED_BY(worker_thread_);
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100387 // TODO(bugs.webrtc.org/11993): Move aggregate_network_up_ over to the
388 // network thread.
Tommi0d4647d2020-05-26 19:35:16 +0200389 bool aggregate_network_up_ RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000390
brandtr25445d32016-10-23 23:37:14 -0700391 // Audio, Video, and FlexFEC receive streams are owned by the client that
392 // creates them.
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100393 // TODO(bugs.webrtc.org/11993): Move audio_receive_streams_,
394 // video_receive_streams_ and sync_stream_mapping_ over to the network thread.
nissee4bcd6d2017-05-16 04:47:04 -0700395 std::set<AudioReceiveStream*> audio_receive_streams_
Tommi0d4647d2020-05-26 19:35:16 +0200396 RTC_GUARDED_BY(worker_thread_);
Tommi553c8692020-05-05 15:35:45 +0200397 std::set<VideoReceiveStream2*> video_receive_streams_
Tommi0d4647d2020-05-26 19:35:16 +0200398 RTC_GUARDED_BY(worker_thread_);
pbos8fc7fa72015-07-15 08:02:58 -0700399 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
Tommi0d4647d2020-05-26 19:35:16 +0200400 RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000401
nisse0f15f922017-06-21 01:05:22 -0700402 // TODO(nisse): Should eventually be injected at creation,
403 // with a single object in the bundled case.
Tommi948e40c2021-05-31 12:39:57 +0200404 RtpStreamReceiverController audio_receiver_controller_
405 RTC_GUARDED_BY(worker_thread_);
406 RtpStreamReceiverController video_receiver_controller_
407 RTC_GUARDED_BY(worker_thread_);
nissee4bcd6d2017-05-16 04:47:04 -0700408
nissed44ce052017-02-06 02:23:00 -0800409 // This extra map is used for receive processing which is
410 // independent of media type.
411
412 // TODO(nisse): In the RTP transport refactoring, we should have a
413 // single mapping from ssrc to a more abstract receive stream, with
414 // accessor methods for all configuration we need at this level.
415 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100416 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
417 : extensions(config.rtp.extensions),
418 use_send_side_bwe(UseSendSideBwe(config)) {}
419 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
420 : extensions(config.rtp.extensions),
421 use_send_side_bwe(UseSendSideBwe(config)) {}
422 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
423 : extensions(config.rtp_header_extensions),
424 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800425
426 // Registered RTP header extensions for each stream. Note that RTP header
427 // extensions are negotiated per track ("m= line") in the SDP, but we have
428 // no notion of tracks at the Call level. We therefore store the RTP header
429 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100430 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800431 // Set if both RTP extension the RTCP feedback message needed for
432 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100433 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800434 };
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100435
436 // TODO(bugs.webrtc.org/11993): Move receive_rtp_config_ over to the
437 // network thread.
nissed44ce052017-02-06 02:23:00 -0800438 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
Tommi0d4647d2020-05-26 19:35:16 +0200439 RTC_GUARDED_BY(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -0800440
solenbergc7a8b082015-10-16 14:35:07 -0700441 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700442 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
Tommi0d4647d2020-05-26 19:35:16 +0200443 RTC_GUARDED_BY(worker_thread_);
danilchapa37de392017-09-09 04:17:22 -0700444 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
Tommi0d4647d2020-05-26 19:35:16 +0200445 RTC_GUARDED_BY(worker_thread_);
446 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000447
Henrik Boström29444c62020-07-01 15:48:46 +0200448 // Each forwarder wraps an adaptation resource that was added to the call.
449 std::vector<std::unique_ptr<ResourceVideoSendStreamForwarder>>
450 adaptation_resource_forwarders_ RTC_GUARDED_BY(worker_thread_);
Henrik Boströmf4a99912020-06-11 12:07:14 +0200451
ossuc3d4b482017-05-23 06:07:11 -0700452 using RtpStateMap = std::map<uint32_t, RtpState>;
Tommi0d4647d2020-05-26 19:35:16 +0200453 RtpStateMap suspended_audio_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
454 RtpStateMap suspended_video_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
ossuc3d4b482017-05-23 06:07:11 -0700455
Åsa Persson4bece9a2017-10-06 10:04:04 +0200456 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
457 RtpPayloadStateMap suspended_video_payload_states_
Tommi0d4647d2020-05-26 19:35:16 +0200458 RTC_GUARDED_BY(worker_thread_);
Åsa Persson4bece9a2017-10-06 10:04:04 +0200459
Tommi948e40c2021-05-31 12:39:57 +0200460 webrtc::RtcEventLog* const event_log_;
ivocb04965c2015-09-09 00:09:43 -0700461
Markus Handellc81afe32021-05-31 09:02:01 +0200462 // TODO(bugs.webrtc.org/11993) ready to move receive stats access to the
463 // network thread.
464 ReceiveStats receive_stats_ RTC_GUARDED_BY(worker_thread_);
stefan91d92602015-11-11 10:13:02 -0800465
Tommi0d4647d2020-05-26 19:35:16 +0200466 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(worker_thread_);
Tommi0d4647d2020-05-26 19:35:16 +0200467 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(worker_thread_);
468 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(worker_thread_);
danilchapa37de392017-09-09 04:17:22 -0700469 AvgCounter estimated_send_bitrate_kbps_counter_
Tommi0d4647d2020-05-26 19:35:16 +0200470 RTC_GUARDED_BY(worker_thread_);
471 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(worker_thread_);
stefan18adf0a2015-11-17 06:24:56 -0800472
nisse559af382017-03-21 06:41:12 -0700473 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100474
475 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
476
asapersson35151f32016-05-02 23:44:01 -0700477 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700478 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800479
Tommi0d4647d2020-05-26 19:35:16 +0200480 // Note that |task_safety_| needs to be at a greater scope than the task queue
481 // owned by |transport_send_| since calls might arrive on the network thread
482 // while Call is being deleted and the task queue is being torn down.
Tommi948e40c2021-05-31 12:39:57 +0200483 const ScopedTaskSafety task_safety_;
Tommi0d4647d2020-05-26 19:35:16 +0200484
Sebastian Janssone6256052018-05-04 14:08:15 +0200485 // Caches transport_send_.get(), to avoid racing with destructor.
486 // Note that this is declared before transport_send_ to ensure that it is not
487 // invalidated until no more tasks can be running on the transport_send_ task
488 // queue.
Tommi948e40c2021-05-31 12:39:57 +0200489 // For more details on the background of this member variable, see:
490 // https://webrtc-review.googlesource.com/c/src/+/63023/9/call/call.cc
491 // https://bugs.chromium.org/p/chromium/issues/detail?id=992640
492 RtpTransportControllerSendInterface* const transport_send_ptr_
493 RTC_GUARDED_BY(send_transport_queue_);
Sebastian Janssone6256052018-05-04 14:08:15 +0200494 // Declared last since it will issue callbacks from a task queue. Declaring it
495 // last ensures that it is destroyed first and any running tasks are finished.
Tommi948e40c2021-05-31 12:39:57 +0200496 const std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
497 rtc::TaskQueue* const send_transport_queue_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800498
Erik Språng7703f232020-09-14 11:03:13 +0200499 bool is_started_ RTC_GUARDED_BY(worker_thread_) = false;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800500
henrikg3c089d72015-09-16 05:37:44 -0700501 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000502};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000503} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000504
asapersson2e5cfcd2016-08-11 08:41:18 -0700505std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200506 char buf[1024];
507 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700508 ss << "Call stats: " << time_ms << ", {";
509 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
510 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
511 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
512 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
513 ss << "rtt_ms: " << rtt_ms;
514 ss << '}';
515 return ss.str();
516}
517
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000518Call* Call::Create(const Call::Config& config) {
Tommi25c77c12020-05-25 17:44:55 +0200519 rtc::scoped_refptr<SharedModuleThread> call_thread =
Per Kjellander4c50e702020-06-30 14:39:43 +0200520 SharedModuleThread::Create(ProcessThread::Create("ModuleProcessThread"),
521 nullptr);
Tommi25c77c12020-05-25 17:44:55 +0200522 return Create(config, std::move(call_thread));
523}
524
525Call* Call::Create(const Call::Config& config,
526 rtc::scoped_refptr<SharedModuleThread> call_thread) {
527 return Create(config, Clock::GetRealTimeClock(), std::move(call_thread),
Erik Språng6950b302019-08-16 12:54:08 +0200528 ProcessThread::Create("PacerThread"));
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100529}
530
531Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100532 Clock* clock,
Tommi25c77c12020-05-25 17:44:55 +0200533 rtc::scoped_refptr<SharedModuleThread> call_thread,
Danil Chapovalov359fe332019-04-01 10:46:36 +0200534 std::unique_ptr<ProcessThread> pacer_thread) {
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200535 RTC_DCHECK(config.task_queue_factory);
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100536 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100537 clock, config,
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200538 std::make_unique<RtpTransportControllerSend>(
Ying Wang0810a7c2019-04-10 13:48:24 +0200539 clock, config.event_log, config.network_state_predictor_factory,
540 config.network_controller_factory, config.bitrate_config,
Erik Språng662678d2019-11-15 17:18:52 +0100541 std::move(pacer_thread), config.task_queue_factory, config.trials),
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200542 std::move(call_thread), config.task_queue_factory);
zstein7cb69d52017-05-08 11:52:38 -0700543}
544
Tommi25c77c12020-05-25 17:44:55 +0200545class SharedModuleThread::Impl {
546 public:
547 Impl(std::unique_ptr<ProcessThread> process_thread,
548 std::function<void()> on_one_ref_remaining)
549 : module_thread_(std::move(process_thread)),
550 on_one_ref_remaining_(std::move(on_one_ref_remaining)) {}
551
552 void EnsureStarted() {
553 RTC_DCHECK_RUN_ON(&sequence_checker_);
554 if (started_)
555 return;
556 started_ = true;
557 module_thread_->Start();
558 }
559
560 ProcessThread* process_thread() {
561 RTC_DCHECK_RUN_ON(&sequence_checker_);
562 return module_thread_.get();
563 }
564
565 void AddRef() const {
566 RTC_DCHECK_RUN_ON(&sequence_checker_);
567 ++ref_count_;
568 }
569
570 rtc::RefCountReleaseStatus Release() const {
571 RTC_DCHECK_RUN_ON(&sequence_checker_);
572 --ref_count_;
573
574 if (ref_count_ == 0) {
575 module_thread_->Stop();
576 return rtc::RefCountReleaseStatus::kDroppedLastRef;
577 }
578
579 if (ref_count_ == 1 && on_one_ref_remaining_) {
580 auto moved_fn = std::move(on_one_ref_remaining_);
581 // NOTE: after this function returns, chances are that |this| has been
582 // deleted - do not touch any member variables.
583 // If the owner of the last reference implements a lambda that releases
584 // that last reference inside of the callback (which is legal according
585 // to this implementation), we will recursively enter Release() above,
586 // call Stop() and release the last reference.
587 moved_fn();
588 }
589
590 return rtc::RefCountReleaseStatus::kOtherRefsRemained;
591 }
592
593 private:
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100594 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
Tommi25c77c12020-05-25 17:44:55 +0200595 mutable int ref_count_ RTC_GUARDED_BY(sequence_checker_) = 0;
596 std::unique_ptr<ProcessThread> const module_thread_;
597 std::function<void()> const on_one_ref_remaining_;
598 bool started_ = false;
599};
600
601SharedModuleThread::SharedModuleThread(
602 std::unique_ptr<ProcessThread> process_thread,
603 std::function<void()> on_one_ref_remaining)
604 : impl_(std::make_unique<Impl>(std::move(process_thread),
605 std::move(on_one_ref_remaining))) {}
606
607SharedModuleThread::~SharedModuleThread() = default;
608
609// static
Tommi25c77c12020-05-25 17:44:55 +0200610
611rtc::scoped_refptr<SharedModuleThread> SharedModuleThread::Create(
612 std::unique_ptr<ProcessThread> process_thread,
613 std::function<void()> on_one_ref_remaining) {
614 return new SharedModuleThread(std::move(process_thread),
615 std::move(on_one_ref_remaining));
616}
617
618void SharedModuleThread::EnsureStarted() {
619 impl_->EnsureStarted();
620}
621
622ProcessThread* SharedModuleThread::process_thread() {
623 return impl_->process_thread();
624}
625
626void SharedModuleThread::AddRef() const {
627 impl_->AddRef();
628}
629
630rtc::RefCountReleaseStatus SharedModuleThread::Release() const {
631 auto ret = impl_->Release();
632 if (ret == rtc::RefCountReleaseStatus::kDroppedLastRef)
633 delete this;
634 return ret;
635}
636
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100637// This method here to avoid subclasses has to implement this method.
638// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
639// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100640VideoSendStream* Call::CreateVideoSendStream(
641 VideoSendStream::Config config,
642 VideoEncoderConfig encoder_config,
643 std::unique_ptr<FecController> fec_controller) {
644 return nullptr;
645}
646
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000647namespace internal {
648
Markus Handellc81afe32021-05-31 09:02:01 +0200649Call::ReceiveStats::ReceiveStats(Clock* clock)
650 : received_bytes_per_second_counter_(clock, nullptr, false),
651 received_audio_bytes_per_second_counter_(clock, nullptr, false),
652 received_video_bytes_per_second_counter_(clock, nullptr, false),
653 received_rtcp_bytes_per_second_counter_(clock, nullptr, false) {
654 sequence_checker_.Detach();
655}
656
657void Call::ReceiveStats::AddReceivedRtcpBytes(int bytes) {
658 RTC_DCHECK_RUN_ON(&sequence_checker_);
659 if (received_bytes_per_second_counter_.HasSample()) {
660 // First RTP packet has been received.
661 received_bytes_per_second_counter_.Add(static_cast<int>(bytes));
662 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(bytes));
663 }
664}
665
666void Call::ReceiveStats::AddReceivedAudioBytes(int bytes,
667 webrtc::Timestamp arrival_time) {
668 RTC_DCHECK_RUN_ON(&sequence_checker_);
669 received_bytes_per_second_counter_.Add(bytes);
670 received_audio_bytes_per_second_counter_.Add(bytes);
671 if (!first_received_rtp_audio_timestamp_)
672 first_received_rtp_audio_timestamp_ = arrival_time;
673 last_received_rtp_audio_timestamp_ = arrival_time;
674}
675
676void Call::ReceiveStats::AddReceivedVideoBytes(int bytes,
677 webrtc::Timestamp arrival_time) {
678 RTC_DCHECK_RUN_ON(&sequence_checker_);
679 received_bytes_per_second_counter_.Add(bytes);
680 received_video_bytes_per_second_counter_.Add(bytes);
681 if (!first_received_rtp_video_timestamp_)
682 first_received_rtp_video_timestamp_ = arrival_time;
683 last_received_rtp_video_timestamp_ = arrival_time;
684}
685
686Call::ReceiveStats::~ReceiveStats() {
687 RTC_DCHECK_RUN_ON(&sequence_checker_);
688 if (first_received_rtp_audio_timestamp_) {
689 RTC_HISTOGRAM_COUNTS_100000(
690 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
691 (*last_received_rtp_audio_timestamp_ -
692 *first_received_rtp_audio_timestamp_)
693 .seconds());
694 }
695 if (first_received_rtp_video_timestamp_) {
696 RTC_HISTOGRAM_COUNTS_100000(
697 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
698 (*last_received_rtp_video_timestamp_ -
699 *first_received_rtp_video_timestamp_)
700 .seconds());
701 }
702 const int kMinRequiredPeriodicSamples = 5;
703 AggregatedStats video_bytes_per_sec =
704 received_video_bytes_per_second_counter_.GetStats();
705 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
706 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
707 video_bytes_per_sec.average * 8 / 1000);
708 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
709 << video_bytes_per_sec.ToStringWithMultiplier(8);
710 }
711 AggregatedStats audio_bytes_per_sec =
712 received_audio_bytes_per_second_counter_.GetStats();
713 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
714 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
715 audio_bytes_per_sec.average * 8 / 1000);
716 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
717 << audio_bytes_per_sec.ToStringWithMultiplier(8);
718 }
719 AggregatedStats rtcp_bytes_per_sec =
720 received_rtcp_bytes_per_second_counter_.GetStats();
721 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
722 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
723 rtcp_bytes_per_sec.average * 8);
724 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
725 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
726 }
727 AggregatedStats recv_bytes_per_sec =
728 received_bytes_per_second_counter_.GetStats();
729 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
730 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
731 recv_bytes_per_sec.average * 8 / 1000);
732 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
733 << recv_bytes_per_sec.ToStringWithMultiplier(8);
734 }
735}
736
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100737Call::Call(Clock* clock,
738 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100739 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
Tommi25c77c12020-05-25 17:44:55 +0200740 rtc::scoped_refptr<SharedModuleThread> module_process_thread,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100741 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100742 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100743 task_queue_factory_(task_queue_factory),
Tommi0d4647d2020-05-26 19:35:16 +0200744 worker_thread_(GetCurrentTaskQueueOrThread()),
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100745 // If |network_task_queue_| was set to nullptr, network related calls
746 // must be made on |worker_thread_| (i.e. they're one and the same).
747 network_thread_(config.network_task_queue_ ? config.network_task_queue_
748 : worker_thread_),
stefan91d92602015-11-11 10:13:02 -0800749 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100750 module_process_thread_(std::move(module_process_thread)),
Tommi0d4647d2020-05-26 19:35:16 +0200751 call_stats_(new CallStats(clock_, worker_thread_)),
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200752 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200753 config_(config),
Tommi948e40c2021-05-31 12:39:57 +0200754 trials_(*config.trials),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800755 audio_network_state_(kNetworkDown),
756 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100757 aggregate_network_up_(false),
skvlad11a9cbf2016-10-07 11:53:05 -0700758 event_log_(config.event_log),
Markus Handellc81afe32021-05-31 09:02:01 +0200759 receive_stats_(clock_),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100760 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700761 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700762 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700763 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
764 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
Per Kjellanderfe2063e2021-05-12 09:02:43 +0200765 receive_side_cc_(clock,
766 absl::bind_front(&PacketRouter::SendCombinedRtcpPacket,
767 transport_send->packet_router()),
768 absl::bind_front(&PacketRouter::SendRemb,
769 transport_send->packet_router()),
770 /*network_state_estimator=*/nullptr),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100771 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700772 video_send_delay_stats_(new SendDelayStats(clock_)),
Tommi78a71382019-08-08 12:27:53 +0200773 start_ms_(clock_->TimeInMilliseconds()),
774 transport_send_ptr_(transport_send.get()),
Tommi948e40c2021-05-31 12:39:57 +0200775 transport_send_(std::move(transport_send)),
776 send_transport_queue_(transport_send_->GetWorkerQueue()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700777 RTC_DCHECK(config.event_log != nullptr);
Erik Språng17f82cf2019-12-04 11:10:43 +0100778 RTC_DCHECK(config.trials != nullptr);
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100779 RTC_DCHECK(network_thread_);
Tommi0d4647d2020-05-26 19:35:16 +0200780 RTC_DCHECK(worker_thread_->IsCurrent());
Tommi948e40c2021-05-31 12:39:57 +0200781 RTC_DCHECK(send_transport_queue_);
Tommi48b48e52019-08-09 11:42:32 +0200782
Mirko Bonadeib9857482020-12-14 15:28:43 +0100783 // Do not remove this call; it is here to convince the compiler that the
784 // WebRTC source timestamp string needs to be in the final binary.
785 LoadWebRTCVersionInRegister();
786
Tommi48b48e52019-08-09 11:42:32 +0200787 call_stats_->RegisterStatsObserver(&receive_side_cc_);
788
Tommi25c77c12020-05-25 17:44:55 +0200789 module_process_thread_->process_thread()->RegisterModule(
Tommi48b48e52019-08-09 11:42:32 +0200790 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
Tommi25c77c12020-05-25 17:44:55 +0200791 module_process_thread_->process_thread()->RegisterModule(&receive_side_cc_,
792 RTC_FROM_HERE);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000793}
794
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000795Call::~Call() {
Tommi0d4647d2020-05-26 19:35:16 +0200796 RTC_DCHECK_RUN_ON(worker_thread_);
perkj26091b12016-09-01 01:17:40 -0700797
solenbergc7a8b082015-10-16 14:35:07 -0700798 RTC_CHECK(audio_send_ssrcs_.empty());
799 RTC_CHECK(video_send_ssrcs_.empty());
800 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700801 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700802 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000803
Tommi25c77c12020-05-25 17:44:55 +0200804 module_process_thread_->process_thread()->DeRegisterModule(
Tommi78a71382019-08-08 12:27:53 +0200805 receive_side_cc_.GetRemoteBitrateEstimator(true));
Tommi25c77c12020-05-25 17:44:55 +0200806 module_process_thread_->process_thread()->DeRegisterModule(&receive_side_cc_);
Tommi78a71382019-08-08 12:27:53 +0200807 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
sprang6d6122b2016-07-13 06:37:09 -0700808
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100809 absl::optional<Timestamp> first_sent_packet_time =
Erik Språng425d6aa2019-07-29 16:38:27 +0200810 transport_send_->GetFirstPacketTime();
Tommi48b48e52019-08-09 11:42:32 +0200811
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100812 Timestamp now = clock_->CurrentTime();
813
sprang6d6122b2016-07-13 06:37:09 -0700814 // Only update histograms after process threads have been shut down, so that
815 // they won't try to concurrently update stats.
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100816 if (first_sent_packet_time) {
817 UpdateSendHistograms(now, *first_sent_packet_time,
818 estimated_send_bitrate_kbps_counter_,
819 pacer_bitrate_kbps_counter_);
perkj26091b12016-09-01 01:17:40 -0700820 }
Tommi48b48e52019-08-09 11:42:32 +0200821
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100822 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.LifetimeInSeconds",
823 (now.ms() - start_ms_) / 1000);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000824}
825
Erik Språng7703f232020-09-14 11:03:13 +0200826void Call::EnsureStarted() {
827 if (is_started_) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800828 return;
Erik Språng7703f232020-09-14 11:03:13 +0200829 }
830 is_started_ = true;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800831
Etienne Pierre-Doraycc474372021-02-10 15:51:36 -0500832 call_stats_->EnsureStarted();
833
Tommi48b48e52019-08-09 11:42:32 +0200834 // This call seems to kick off a number of things, so probably better left
835 // off being kicked off on request rather than in the ctor.
Tommi948e40c2021-05-31 12:39:57 +0200836 transport_send_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800837
Tommi25c77c12020-05-25 17:44:55 +0200838 module_process_thread_->EnsureStarted();
Tommi948e40c2021-05-31 12:39:57 +0200839 transport_send_->EnsureStarted();
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700840}
841
842void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
Tommi0d4647d2020-05-26 19:35:16 +0200843 RTC_DCHECK_RUN_ON(worker_thread_);
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700844 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800845}
846
solenberg5a289392015-10-19 03:39:20 -0700847PacketReceiver* Call::Receiver() {
solenberg5a289392015-10-19 03:39:20 -0700848 return this;
849}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000850
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200851webrtc::AudioSendStream* Call::CreateAudioSendStream(
852 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700853 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200854 RTC_DCHECK_RUN_ON(worker_thread_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800855
Erik Språng7703f232020-09-14 11:03:13 +0200856 EnsureStarted();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800857
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100858 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
859 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200860 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700861 {
862 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
863 if (iter != suspended_audio_send_ssrcs_.end()) {
864 suspended_rtp_state.emplace(iter->second);
865 }
866 }
867
Tommi822a8742020-05-11 00:42:30 +0200868 AudioSendStream* send_stream = new AudioSendStream(
869 clock_, config, config_.audio_state, task_queue_factory_,
Tommi948e40c2021-05-31 12:39:57 +0200870 module_process_thread_->process_thread(), transport_send_.get(),
Tommi822a8742020-05-11 00:42:30 +0200871 bitrate_allocator_.get(), event_log_, call_stats_->AsRtcpRttStats(),
872 suspended_rtp_state);
Tommi0d4647d2020-05-26 19:35:16 +0200873 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
874 audio_send_ssrcs_.end());
875 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
Tommi31001a62020-05-26 11:38:36 +0200876
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100877 // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
878 // UpdateAggregateNetworkState asynchronously on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200879 for (AudioReceiveStream* stream : audio_receive_streams_) {
880 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
881 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800882 }
883 }
Tommi31001a62020-05-26 11:38:36 +0200884
skvlad7a43d252016-03-22 15:32:27 -0700885 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100886
solenbergc7a8b082015-10-16 14:35:07 -0700887 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200888}
889
890void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700891 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200892 RTC_DCHECK_RUN_ON(worker_thread_);
solenbergc7a8b082015-10-16 14:35:07 -0700893 RTC_DCHECK(send_stream != nullptr);
894
895 send_stream->Stop();
896
eladalonabbc4302017-07-26 02:09:44 -0700897 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700898 webrtc::internal::AudioSendStream* audio_send_stream =
899 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700900 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
Tommi0d4647d2020-05-26 19:35:16 +0200901
902 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
903 RTC_DCHECK_EQ(1, num_deleted);
Tommi31001a62020-05-26 11:38:36 +0200904
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100905 // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
906 // UpdateAggregateNetworkState asynchronously on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200907 for (AudioReceiveStream* stream : audio_receive_streams_) {
908 if (stream->config().rtp.local_ssrc == ssrc) {
909 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800910 }
solenbergc7a8b082015-10-16 14:35:07 -0700911 }
Tommi31001a62020-05-26 11:38:36 +0200912
skvlad7a43d252016-03-22 15:32:27 -0700913 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100914
eladalonabbc4302017-07-26 02:09:44 -0700915 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200916}
917
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200918webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
919 const webrtc::AudioReceiveStream::Config& config) {
920 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +0200921 RTC_DCHECK_RUN_ON(worker_thread_);
Erik Språng7703f232020-09-14 11:03:13 +0200922 EnsureStarted();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200923 event_log_->Log(std::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200924 CreateRtcLogStreamConfig(config)));
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100925
nisse0f15f922017-06-21 01:05:22 -0700926 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Tommi02df2eb2021-05-31 12:57:53 +0200927 clock_, transport_send_->packet_router(),
Tommi25c77c12020-05-25 17:44:55 +0200928 module_process_thread_->process_thread(), config_.neteq_factory, config,
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +0100929 config_.audio_state, event_log_);
nissed44ce052017-02-06 02:23:00 -0800930
Tommi02df2eb2021-05-31 12:57:53 +0200931 // TODO(bugs.webrtc.org/11993): Make the registration on the network thread
932 // (asynchronously). The registration and `audio_receiver_controller_` need
933 // to live on the network thread.
934 receive_stream->RegisterWithTransport(&audio_receiver_controller_);
935
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100936 // TODO(bugs.webrtc.org/11993): Update the below on the network thread.
937 // We could possibly set up the audio_receiver_controller_ association up
938 // as part of the async setup.
Tommi31001a62020-05-26 11:38:36 +0200939 receive_rtp_config_.emplace(config.rtp.remote_ssrc, ReceiveRtpConfig(config));
940 audio_receive_streams_.insert(receive_stream);
941
942 ConfigureSync(config.sync_group);
943
Tommi0d4647d2020-05-26 19:35:16 +0200944 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
945 if (it != audio_send_ssrcs_.end()) {
946 receive_stream->AssociateSendStream(it->second);
solenberg7602aab2016-11-14 11:30:07 -0800947 }
Tommi0d4647d2020-05-26 19:35:16 +0200948
skvlad7a43d252016-03-22 15:32:27 -0700949 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200950 return receive_stream;
951}
952
953void Call::DestroyAudioReceiveStream(
954 webrtc::AudioReceiveStream* receive_stream) {
955 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +0200956 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -0700957 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700958 webrtc::internal::AudioReceiveStream* audio_receive_stream =
959 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Tommi31001a62020-05-26 11:38:36 +0200960
961 const AudioReceiveStream::Config& config = audio_receive_stream->config();
962 uint32_t ssrc = config.rtp.remote_ssrc;
963 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
964 ->RemoveStream(ssrc);
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100965
966 // TODO(bugs.webrtc.org/11993): Access the map, rtp config, call ConfigureSync
Tommi02df2eb2021-05-31 12:57:53 +0200967 // and UpdateAggregateNetworkState on the network thread. The call to
968 // `UnregisterFromTransport` should also happen on the network thread.
969 audio_receive_stream->UnregisterFromTransport();
Tommi31001a62020-05-26 11:38:36 +0200970 audio_receive_streams_.erase(audio_receive_stream);
971 const std::string& sync_group = audio_receive_stream->config().sync_group;
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100972
Tommi31001a62020-05-26 11:38:36 +0200973 const auto it = sync_stream_mapping_.find(sync_group);
974 if (it != sync_stream_mapping_.end() && it->second == audio_receive_stream) {
975 sync_stream_mapping_.erase(it);
976 ConfigureSync(sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200977 }
Tommi31001a62020-05-26 11:38:36 +0200978 receive_rtp_config_.erase(ssrc);
979
skvlad7a43d252016-03-22 15:32:27 -0700980 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100981 // TODO(bugs.webrtc.org/11993): Consider if deleting |audio_receive_stream|
982 // on the network thread would be better or if we'd need to tear down the
983 // state in two phases.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200984 delete audio_receive_stream;
985}
986
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100987// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100988webrtc::VideoSendStream* Call::CreateVideoSendStream(
989 webrtc::VideoSendStream::Config config,
990 VideoEncoderConfig encoder_config,
991 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000992 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200993 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000994
Erik Språng7703f232020-09-14 11:03:13 +0200995 EnsureStarted();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800996
asapersson35151f32016-05-02 23:44:01 -0700997 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700998 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
999 ++ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001000 event_log_->Log(std::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001001 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -07001002 }
perkj26091b12016-09-01 01:17:40 -07001003
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +00001004 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
1005 // the call has already started.
perkj26091b12016-09-01 01:17:40 -07001006 // Copy ssrcs from |config| since |config| is moved.
1007 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001008
mflodman0c478b32015-10-21 15:52:16 +02001009 VideoSendStream* send_stream = new VideoSendStream(
Tommi25c77c12020-05-25 17:44:55 +02001010 clock_, num_cpu_cores_, module_process_thread_->process_thread(),
Tommi948e40c2021-05-31 12:39:57 +02001011 task_queue_factory_, call_stats_->AsRtcpRttStats(), transport_send_.get(),
Tommi822a8742020-05-11 00:42:30 +02001012 bitrate_allocator_.get(), video_send_delay_stats_.get(), event_log_,
1013 std::move(config), std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +02001014 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -07001015
Tommi0d4647d2020-05-26 19:35:16 +02001016 for (uint32_t ssrc : ssrcs) {
1017 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
1018 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001019 }
Tommi0d4647d2020-05-26 19:35:16 +02001020 video_send_streams_.insert(send_stream);
Henrik Boström29444c62020-07-01 15:48:46 +02001021 // Forward resources that were previously added to the call to the new stream.
1022 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
1023 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001024 }
Tommi0d4647d2020-05-26 19:35:16 +02001025
skvlad7a43d252016-03-22 15:32:27 -07001026 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -07001027
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001028 return send_stream;
1029}
1030
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001031webrtc::VideoSendStream* Call::CreateVideoSendStream(
1032 webrtc::VideoSendStream::Config config,
1033 VideoEncoderConfig encoder_config) {
Tommi948e40c2021-05-31 12:39:57 +02001034 RTC_DCHECK_RUN_ON(worker_thread_);
Ying Wang012b7e72018-03-05 15:44:23 +01001035 if (config_.fec_controller_factory) {
1036 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
1037 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001038 std::unique_ptr<FecController> fec_controller =
1039 config_.fec_controller_factory
1040 ? config_.fec_controller_factory->CreateFecController()
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001041 : std::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001042 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
1043 std::move(fec_controller));
1044}
1045
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001046void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001047 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -07001048 RTC_DCHECK(send_stream != nullptr);
Tommi0d4647d2020-05-26 19:35:16 +02001049 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001050
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001051 send_stream->Stop();
1052
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +00001053 VideoSendStream* send_stream_impl = nullptr;
Tommi0d4647d2020-05-26 19:35:16 +02001054
1055 auto it = video_send_ssrcs_.begin();
1056 while (it != video_send_ssrcs_.end()) {
1057 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
1058 send_stream_impl = it->second;
1059 video_send_ssrcs_.erase(it++);
1060 } else {
1061 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001062 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001063 }
Henrik Boström29444c62020-07-01 15:48:46 +02001064 // Stop forwarding resources to the stream being destroyed.
1065 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
1066 resource_forwarder->OnDestroyVideoSendStream(send_stream_impl);
1067 }
Tommi0d4647d2020-05-26 19:35:16 +02001068 video_send_streams_.erase(send_stream_impl);
1069
henrikg91d6ede2015-09-17 00:24:34 -07001070 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001071
Åsa Persson4bece9a2017-10-06 10:04:04 +02001072 VideoSendStream::RtpStateMap rtp_states;
1073 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
1074 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
1075 &rtp_payload_states);
1076 for (const auto& kv : rtp_states) {
1077 suspended_video_send_ssrcs_[kv.first] = kv.second;
1078 }
1079 for (const auto& kv : rtp_payload_states) {
1080 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001081 }
1082
skvlad7a43d252016-03-22 15:32:27 -07001083 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001084 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001085}
1086
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001087webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +02001088 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001089 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001090 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrfb45c6c2017-01-27 06:47:55 -08001091
Johannes Kronf59666b2019-04-08 12:57:06 +02001092 receive_side_cc_.SetSendPeriodicFeedback(
1093 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +01001094
Erik Språng7703f232020-09-14 11:03:13 +02001095 EnsureStarted();
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -08001096
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001097 // TODO(bugs.webrtc.org/11993): Move the registration between |receive_stream|
1098 // and |video_receiver_controller_| out of VideoReceiveStream2 construction
1099 // and set it up asynchronously on the network thread (the registration and
1100 // |video_receiver_controller_| need to live on the network thread).
Tommi553c8692020-05-05 15:35:45 +02001101 VideoReceiveStream2* receive_stream = new VideoReceiveStream2(
Tommi90738dd2021-05-31 17:36:47 +02001102 task_queue_factory_, this, num_cpu_cores_,
1103 transport_send_->packet_router(), std::move(configuration),
1104 module_process_thread_->process_thread(), call_stats_.get(), clock_,
1105 new VCMTiming(clock_));
1106 // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network
1107 // thread.
1108 receive_stream->RegisterWithTransport(&video_receiver_controller_);
Tommi733b5472016-06-10 17:58:01 +02001109
1110 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
Tommi31001a62020-05-26 11:38:36 +02001111 if (config.rtp.rtx_ssrc) {
1112 // We record identical config for the rtx stream as for the main
1113 // stream. Since the transport_send_cc negotiation is per payload
1114 // type, we may get an incorrect value for the rtx stream, but
1115 // that is unlikely to matter in practice.
1116 receive_rtp_config_.emplace(config.rtp.rtx_ssrc, ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -07001117 }
Tommi31001a62020-05-26 11:38:36 +02001118 receive_rtp_config_.emplace(config.rtp.remote_ssrc, ReceiveRtpConfig(config));
1119 video_receive_streams_.insert(receive_stream);
1120 ConfigureSync(config.sync_group);
1121
skvlad7a43d252016-03-22 15:32:27 -07001122 receive_stream->SignalNetworkState(video_network_state_);
1123 UpdateAggregateNetworkState();
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001124 event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001125 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001126 return receive_stream;
1127}
1128
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001129void Call::DestroyVideoReceiveStream(
1130 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001131 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001132 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -07001133 RTC_DCHECK(receive_stream != nullptr);
Tommi553c8692020-05-05 15:35:45 +02001134 VideoReceiveStream2* receive_stream_impl =
1135 static_cast<VideoReceiveStream2*>(receive_stream);
Tommi90738dd2021-05-31 17:36:47 +02001136 // TODO(bugs.webrtc.org/11993): Unregister on the network thread.
1137 receive_stream_impl->UnregisterFromTransport();
1138
nissee4bcd6d2017-05-16 04:47:04 -07001139 const VideoReceiveStream::Config& config = receive_stream_impl->config();
Tommi31001a62020-05-26 11:38:36 +02001140
1141 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
1142 // separate SSRC there can be either one or two.
1143 receive_rtp_config_.erase(config.rtp.remote_ssrc);
1144 if (config.rtp.rtx_ssrc) {
1145 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001146 }
Tommi31001a62020-05-26 11:38:36 +02001147 video_receive_streams_.erase(receive_stream_impl);
1148 ConfigureSync(config.sync_group);
nisse4709e892017-02-07 01:18:43 -08001149
nisse559af382017-03-21 06:41:12 -07001150 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -08001151 ->RemoveStream(config.rtp.remote_ssrc);
1152
skvlad7a43d252016-03-22 15:32:27 -07001153 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001154 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001155}
1156
brandtr7250b392016-12-19 01:13:46 -08001157FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
1158 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -07001159 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001160 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001161
1162 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -07001163
nisse0f15f922017-06-21 01:05:22 -07001164 FlexfecReceiveStreamImpl* receive_stream;
brandtrb29e6522016-12-21 06:37:18 -08001165
Tommi31001a62020-05-26 11:38:36 +02001166 // Unlike the video and audio receive streams, FlexfecReceiveStream implements
1167 // RtpPacketSinkInterface itself, and hence its constructor passes its |this|
1168 // pointer to video_receiver_controller_->CreateStream(). Calling the
1169 // constructor while on the worker thread ensures that we don't call
1170 // OnRtpPacket until the constructor is finished and the object is
1171 // in a valid state, since OnRtpPacket runs on the same thread.
1172 receive_stream = new FlexfecReceiveStreamImpl(
Tommi0377bab2021-05-31 14:26:05 +02001173 clock_, config, recovered_packet_receiver, call_stats_->AsRtcpRttStats(),
1174 module_process_thread_->process_thread());
1175
1176 // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network
1177 // thread.
1178 receive_stream->RegisterWithTransport(&video_receiver_controller_);
Tommi31001a62020-05-26 11:38:36 +02001179
1180 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1181 receive_rtp_config_.end());
1182 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtrb29e6522016-12-21 06:37:18 -08001183
brandtr25445d32016-10-23 23:37:14 -07001184 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001185
brandtr25445d32016-10-23 23:37:14 -07001186 return receive_stream;
1187}
1188
brandtr7250b392016-12-19 01:13:46 -08001189void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001190 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001191 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001192
Tommi0377bab2021-05-31 14:26:05 +02001193 FlexfecReceiveStreamImpl* receive_stream_impl =
1194 static_cast<FlexfecReceiveStreamImpl*>(receive_stream);
1195 // TODO(bugs.webrtc.org/11993): Unregister on the network thread.
1196 receive_stream_impl->UnregisterFromTransport();
1197
brandtr25445d32016-10-23 23:37:14 -07001198 RTC_DCHECK(receive_stream != nullptr);
Tommi31001a62020-05-26 11:38:36 +02001199 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
1200 uint32_t ssrc = config.remote_ssrc;
1201 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001202
Tommi31001a62020-05-26 11:38:36 +02001203 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1204 // destroyed.
1205 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
1206 ->RemoveStream(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001207
eladalon42f44f92017-07-25 06:40:06 -07001208 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001209}
1210
Henrik Boströmf4a99912020-06-11 12:07:14 +02001211void Call::AddAdaptationResource(rtc::scoped_refptr<Resource> resource) {
1212 RTC_DCHECK_RUN_ON(worker_thread_);
Henrik Boström29444c62020-07-01 15:48:46 +02001213 adaptation_resource_forwarders_.push_back(
1214 std::make_unique<ResourceVideoSendStreamForwarder>(resource));
1215 const auto& resource_forwarder = adaptation_resource_forwarders_.back();
1216 for (VideoSendStream* send_stream : video_send_streams_) {
1217 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001218 }
1219}
1220
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001221RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Tommi948e40c2021-05-31 12:39:57 +02001222 return transport_send_.get();
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001223}
1224
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001225Call::Stats Call::GetStats() const {
Tommi0d4647d2020-05-26 19:35:16 +02001226 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi48b48e52019-08-09 11:42:32 +02001227
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001228 Stats stats;
Tommi48b48e52019-08-09 11:42:32 +02001229 // TODO(srte): It is unclear if we only want to report queues if network is
1230 // available.
1231 stats.pacer_delay_ms =
Tommi948e40c2021-05-31 12:39:57 +02001232 aggregate_network_up_ ? transport_send_->GetPacerQueuingDelayMs() : 0;
Tommi48b48e52019-08-09 11:42:32 +02001233
1234 stats.rtt_ms = call_stats_->LastProcessedRtt();
1235
Peter Boström45553ae2015-05-08 13:54:38 +02001236 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001237 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001238 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001239 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001240 &ssrcs, &recv_bandwidth);
Tommi48b48e52019-08-09 11:42:32 +02001241 stats.recv_bandwidth_bps = recv_bandwidth;
Tommi0d4647d2020-05-26 19:35:16 +02001242 stats.send_bandwidth_bps = last_bandwidth_bps_;
1243 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
Tommi48b48e52019-08-09 11:42:32 +02001244
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001245 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001246}
1247
Erik Språngceb44952020-09-22 11:36:35 +02001248const WebRtcKeyValueConfig& Call::trials() const {
Tommi948e40c2021-05-31 12:39:57 +02001249 return trials_;
Erik Språngceb44952020-09-22 11:36:35 +02001250}
1251
Tomas Gunnarssone984aa22021-04-19 09:21:06 +02001252TaskQueueBase* Call::network_thread() const {
1253 return network_thread_;
1254}
1255
1256TaskQueueBase* Call::worker_thread() const {
1257 return worker_thread_;
1258}
1259
skvlad7a43d252016-03-22 15:32:27 -07001260void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001261 RTC_DCHECK_RUN_ON(network_thread_);
1262 RTC_DCHECK(media == MediaType::AUDIO || media == MediaType::VIDEO);
Tomas Gunnarssond48a2b12021-02-02 17:57:36 +01001263
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001264 auto closure = [this, media, state]() {
1265 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1266 RTC_DCHECK_RUN_ON(worker_thread_);
1267 if (media == MediaType::AUDIO) {
1268 audio_network_state_ = state;
1269 } else {
1270 RTC_DCHECK_EQ(media, MediaType::VIDEO);
1271 video_network_state_ = state;
1272 }
1273
1274 // TODO(tommi): Is it necessary to always do this, including if there
1275 // was no change in state?
1276 UpdateAggregateNetworkState();
1277
1278 // TODO(tommi): Is it right to do this if media == AUDIO?
1279 for (VideoReceiveStream2* video_receive_stream : video_receive_streams_) {
1280 video_receive_stream->SignalNetworkState(video_network_state_);
1281 }
1282 };
1283
1284 if (network_thread_ == worker_thread_) {
1285 closure();
1286 } else {
1287 // TODO(bugs.webrtc.org/11993): Remove workaround when we no longer need to
1288 // post to the worker thread.
1289 worker_thread_->PostTask(ToQueuedTask(task_safety_, std::move(closure)));
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001290 }
1291}
1292
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001293void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001294 RTC_DCHECK_RUN_ON(network_thread_);
1295 worker_thread_->PostTask(
1296 ToQueuedTask(task_safety_, [this, transport_overhead_per_packet]() {
1297 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1298 RTC_DCHECK_RUN_ON(worker_thread_);
1299 for (auto& kv : audio_send_ssrcs_) {
1300 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1301 }
1302 }));
michaelt79e05882016-11-08 02:50:09 -08001303}
1304
skvlad7a43d252016-03-22 15:32:27 -07001305void Call::UpdateAggregateNetworkState() {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001306 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1307 // RTC_DCHECK_RUN_ON(network_thread_);
1308
Tommi0d4647d2020-05-26 19:35:16 +02001309 RTC_DCHECK_RUN_ON(worker_thread_);
skvlad7a43d252016-03-22 15:32:27 -07001310
Tommi0d4647d2020-05-26 19:35:16 +02001311 bool have_audio =
1312 !audio_send_ssrcs_.empty() || !audio_receive_streams_.empty();
1313 bool have_video =
1314 !video_send_ssrcs_.empty() || !video_receive_streams_.empty();
skvlad7a43d252016-03-22 15:32:27 -07001315
Sebastian Janssona06e9192018-03-07 18:49:55 +01001316 bool aggregate_network_up =
1317 ((have_video && video_network_state_ == kNetworkUp) ||
1318 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001319
Harald Alvestrand977b2652019-12-12 13:40:50 +01001320 if (aggregate_network_up != aggregate_network_up_) {
1321 RTC_LOG(LS_INFO)
1322 << "UpdateAggregateNetworkState: aggregate_state change to "
1323 << (aggregate_network_up ? "up" : "down");
1324 } else {
1325 RTC_LOG(LS_VERBOSE)
1326 << "UpdateAggregateNetworkState: aggregate_state remains at "
1327 << (aggregate_network_up ? "up" : "down");
1328 }
Tommi48b48e52019-08-09 11:42:32 +02001329 aggregate_network_up_ = aggregate_network_up;
1330
Tommi948e40c2021-05-31 12:39:57 +02001331 transport_send_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001332}
1333
stefanc1aeaf02015-10-15 07:26:07 -07001334void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
Tomas Gunnarssoneb9c3f22021-04-19 12:53:09 +02001335 // In production and with most tests, this method will be called on the
1336 // network thread. However some test classes such as DirectTransport don't
1337 // incorporate a network thread. This means that tests for RtpSenderEgress
1338 // and ModuleRtpRtcpImpl2 that use DirectTransport, will call this method
1339 // on a ProcessThread. This is alright as is since we forward the call to
1340 // implementations that either just do a PostTask or use locking.
asapersson35151f32016-05-02 23:44:01 -07001341 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1342 clock_->TimeInMilliseconds());
Tommi948e40c2021-05-31 12:39:57 +02001343 transport_send_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001344}
1345
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001346void Call::OnStartRateUpdate(DataRate start_rate) {
Tommi948e40c2021-05-31 12:39:57 +02001347 RTC_DCHECK_RUN_ON(send_transport_queue_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001348 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1349}
1350
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001351void Call::OnTargetTransferRate(TargetTransferRate msg) {
Tommi948e40c2021-05-31 12:39:57 +02001352 RTC_DCHECK_RUN_ON(send_transport_queue_);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001353
1354 uint32_t target_bitrate_bps = msg.target_rate.bps();
nisse559af382017-03-21 06:41:12 -07001355 // For controlling the rate of feedback messages.
1356 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001357 bitrate_allocator_->OnNetworkEstimateChanged(msg);
mflodman0e7e2592015-11-12 21:02:42 -08001358
Tommi0d4647d2020-05-26 19:35:16 +02001359 worker_thread_->PostTask(
1360 ToQueuedTask(task_safety_, [this, target_bitrate_bps]() {
1361 RTC_DCHECK_RUN_ON(worker_thread_);
1362 last_bandwidth_bps_ = target_bitrate_bps;
asaperssonce2e1362016-09-09 00:13:35 -07001363
Tommi0d4647d2020-05-26 19:35:16 +02001364 // Ignore updates if bitrate is zero (the aggregate network state is
1365 // down) or if we're not sending video.
1366 if (target_bitrate_bps == 0 || video_send_streams_.empty()) {
1367 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1368 pacer_bitrate_kbps_counter_.ProcessAndPause();
1369 return;
1370 }
asaperssonce2e1362016-09-09 00:13:35 -07001371
Tommi0d4647d2020-05-26 19:35:16 +02001372 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1373 // Pacer bitrate may be higher than bitrate estimate if enforcing min
1374 // bitrate.
1375 uint32_t pacer_bitrate_bps =
1376 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1377 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
1378 }));
perkj71ee44c2016-06-15 00:47:53 -07001379}
mflodman101f2502016-06-09 17:21:19 +02001380
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001381void Call::OnAllocationLimitsChanged(BitrateAllocationLimits limits) {
Tommi948e40c2021-05-31 12:39:57 +02001382 RTC_DCHECK_RUN_ON(send_transport_queue_);
Tommi48b48e52019-08-09 11:42:32 +02001383
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001384 transport_send_ptr_->SetAllocatedSendBitrateLimits(limits);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001385
Tommi0d4647d2020-05-26 19:35:16 +02001386 worker_thread_->PostTask(ToQueuedTask(task_safety_, [this, limits]() {
1387 RTC_DCHECK_RUN_ON(worker_thread_);
1388 min_allocated_send_bitrate_bps_ = limits.min_allocatable_rate.bps();
1389 configured_max_padding_bitrate_bps_ = limits.max_padding_rate.bps();
1390 }));
mflodman0e7e2592015-11-12 21:02:42 -08001391}
1392
pbos8fc7fa72015-07-15 08:02:58 -07001393void Call::ConfigureSync(const std::string& sync_group) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001394 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
pbos8fc7fa72015-07-15 08:02:58 -07001395 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001396 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001397 return;
1398
1399 AudioReceiveStream* sync_audio_stream = nullptr;
1400 // Find existing audio stream.
1401 const auto it = sync_stream_mapping_.find(sync_group);
1402 if (it != sync_stream_mapping_.end()) {
1403 sync_audio_stream = it->second;
1404 } else {
1405 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001406 for (AudioReceiveStream* stream : audio_receive_streams_) {
1407 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001408 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001409 RTC_LOG(LS_WARNING)
1410 << "Attempting to sync more than one audio stream "
1411 "within the same sync group. This is not "
1412 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001413 break;
1414 }
nissee4bcd6d2017-05-16 04:47:04 -07001415 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001416 }
1417 }
1418 }
1419 if (sync_audio_stream)
1420 sync_stream_mapping_[sync_group] = sync_audio_stream;
1421 size_t num_synced_streams = 0;
Tommi553c8692020-05-05 15:35:45 +02001422 for (VideoReceiveStream2* video_stream : video_receive_streams_) {
pbos8fc7fa72015-07-15 08:02:58 -07001423 if (video_stream->config().sync_group != sync_group)
1424 continue;
1425 ++num_synced_streams;
1426 if (num_synced_streams > 1) {
1427 // TODO(pbos): Support synchronizing more than one A/V pair.
1428 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001429 RTC_LOG(LS_WARNING)
1430 << "Attempting to sync more than one audio/video pair "
1431 "within the same sync group. This is not supported in "
1432 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001433 }
1434 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001435 if (num_synced_streams == 1) {
1436 // sync_audio_stream may be null and that's ok.
1437 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001438 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001439 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001440 }
1441 }
1442}
1443
Tommicae1f1d2021-05-31 10:51:09 +02001444// RTC_RUN_ON(network_thread_)
1445void Call::DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001446 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
Tommi3f418cc2021-05-05 11:04:30 +02001447
1448 // TODO(bugs.webrtc.org/11993): This DCHECK is here just to maintain the
1449 // invariant that currently the only call path to this function is via
1450 // `PeerConnection::InitializeRtcpCallback()`. DeliverRtp on the other hand
1451 // gets called via the channel classes and
1452 // WebRtc[Audio|Video]Channel's `OnPacketReceived`. We'll remove the
1453 // PeerConnection involvement as well as
1454 // `JsepTransportController::OnRtcpPacketReceived_n` and `rtcp_handler`
1455 // and make sure that the flow of packets is consistent from the
1456 // `RtpTransport` class, via the *Channel and *Engine classes and into Call.
1457 // This way we'll also know more about the context of the packet.
1458 RTC_DCHECK_EQ(media_type, MediaType::ANY);
1459
Tommicae1f1d2021-05-31 10:51:09 +02001460 // TODO(bugs.webrtc.org/11993): This should execute directly on the network
1461 // thread.
1462 worker_thread_->PostTask(
1463 ToQueuedTask(task_safety_, [this, packet = std::move(packet)]() {
1464 RTC_DCHECK_RUN_ON(worker_thread_);
mflodman3d7db262016-04-29 00:57:13 -07001465
Tommicae1f1d2021-05-31 10:51:09 +02001466 receive_stats_.AddReceivedRtcpBytes(static_cast<int>(packet.size()));
1467 bool rtcp_delivered = false;
1468 for (VideoReceiveStream2* stream : video_receive_streams_) {
1469 if (stream->DeliverRtcp(packet.cdata(), packet.size()))
1470 rtcp_delivered = true;
1471 }
mflodman3d7db262016-04-29 00:57:13 -07001472
Tommicae1f1d2021-05-31 10:51:09 +02001473 for (AudioReceiveStream* stream : audio_receive_streams_) {
1474 stream->DeliverRtcp(packet.cdata(), packet.size());
1475 rtcp_delivered = true;
1476 }
1477
1478 for (VideoSendStream* stream : video_send_streams_) {
1479 stream->DeliverRtcp(packet.cdata(), packet.size());
1480 rtcp_delivered = true;
1481 }
1482
1483 for (auto& kv : audio_send_ssrcs_) {
1484 kv.second->DeliverRtcp(packet.cdata(), packet.size());
1485 rtcp_delivered = true;
1486 }
1487
1488 if (rtcp_delivered) {
1489 event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>(
1490 rtc::MakeArrayView(packet.cdata(), packet.size())));
1491 }
1492 }));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001493}
1494
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001495PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001496 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001497 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001498 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
Tommi3f418cc2021-05-05 11:04:30 +02001499 RTC_DCHECK_NE(media_type, MediaType::ANY);
nissed44ce052017-02-06 02:23:00 -08001500
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001501 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001502 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001503 return DELIVERY_PACKET_ERROR;
1504
Niels Möller70082872018-08-07 11:03:12 +02001505 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001506 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001507 // Repair packet_time_us for clock resets by comparing a new read of
1508 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001509 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001510 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001511 }
Tommi2497a272021-05-05 12:33:00 +02001512 parsed_packet.set_arrival_time(Timestamp::Micros(packet_time_us));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001513 } else {
Tommi2497a272021-05-05 12:33:00 +02001514 parsed_packet.set_arrival_time(clock_->CurrentTime());
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001515 }
nissed44ce052017-02-06 02:23:00 -08001516
sprangc1abde72017-07-11 03:56:21 -07001517 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1518 // These are empty (zero length payload) RTP packets with an unsignaled
1519 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001520 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001521
1522 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1523 is_keep_alive_packet);
1524
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001525 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001526 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001527 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1528 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001529 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001530 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001531 // But deregistering in the |receive_rtp_config_| map is. So by not passing
1532 // the packet on to demuxing in this case, we prevent incoming packets to be
1533 // passed on via the demuxer to a receive stream which is being torned down.
nisse0f15f922017-06-21 01:05:22 -07001534 return DELIVERY_UNKNOWN_SSRC;
1535 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001536
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001537 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001538
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001539 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001540
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001541 // RateCounters expect input parameter as int, save it as int,
1542 // instead of converting each time it is passed to RateCounter::Add below.
1543 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001544 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001545 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001546 receive_stats_.AddReceivedAudioBytes(length,
1547 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001548 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001549 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse657bab22017-02-21 06:28:10 -08001550 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001551 }
nissee4bcd6d2017-05-16 04:47:04 -07001552 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001553 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001554 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001555 receive_stats_.AddReceivedVideoBytes(length,
1556 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001557 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001558 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse5c29a7a2017-02-16 06:52:32 -08001559 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001560 }
1561 }
1562 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001563}
1564
stefan68786d22015-09-08 05:36:15 -07001565PacketReceiver::DeliveryStatus Call::DeliverPacket(
1566 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001567 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001568 int64_t packet_time_us) {
Tommicae1f1d2021-05-31 10:51:09 +02001569 if (IsRtcp(packet.cdata(), packet.size())) {
1570 RTC_DCHECK_RUN_ON(network_thread_);
1571 DeliverRtcp(media_type, std::move(packet));
1572 return DELIVERY_OK;
1573 }
1574
Tommi0d4647d2020-05-26 19:35:16 +02001575 RTC_DCHECK_RUN_ON(worker_thread_);
Niels Möller70082872018-08-07 11:03:12 +02001576 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001577}
1578
nissed2ef3142017-05-11 08:00:58 -07001579void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001580 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
1581 // This method is called synchronously via |OnRtpPacket()| (see DeliverRtp)
1582 // on the same thread.
Tommi0d4647d2020-05-26 19:35:16 +02001583 RTC_DCHECK_RUN_ON(worker_thread_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001584 RtpPacketReceived parsed_packet;
1585 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001586 return;
1587
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001588 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001589
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001590 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001591 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001592 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1593 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001594 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001595 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001596 // But deregistering in the |receive_rtp_config_| map is.
brandtrcaea68f2017-08-23 00:55:17 -07001597 // So by not passing the packet on to demuxing in this case, we prevent
1598 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001599 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001600 return;
1601 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001602 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001603
1604 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001605 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001606 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001607}
1608
Tommi948e40c2021-05-31 12:39:57 +02001609// RTC_RUN_ON(worker_thread_)
nissed44ce052017-02-06 02:23:00 -08001610void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1611 MediaType media_type) {
1612 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001613 bool use_send_side_bwe =
1614 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001615
brandtrb29e6522016-12-21 06:37:18 -08001616 RTPHeader header;
1617 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001618
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001619 ReceivedPacket packet_msg;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001620 packet_msg.size = DataSize::Bytes(packet.payload_size());
Tommi2497a272021-05-05 12:33:00 +02001621 packet_msg.receive_time = packet.arrival_time();
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001622 if (header.extension.hasAbsoluteSendTime) {
1623 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1624 }
Tommi948e40c2021-05-31 12:39:57 +02001625 transport_send_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001626
nisse4709e892017-02-07 01:18:43 -08001627 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001628 // Inconsistent configuration of send side BWE. Do nothing.
1629 // TODO(nisse): Without this check, we may produce RTCP feedback
1630 // packets even when not negotiated. But it would be cleaner to
1631 // move the check down to RTCPSender::SendFeedbackPacket, which
1632 // would also help the PacketRouter to select an appropriate rtp
1633 // module in the case that some, but not all, have RTCP feedback
1634 // enabled.
1635 return;
1636 }
1637 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001638 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001639 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001640 receive_side_cc_.OnReceivedPacket(
Tommi2497a272021-05-05 12:33:00 +02001641 packet.arrival_time().ms(),
1642 packet.payload_size() + packet.padding_size(), header);
nissed44ce052017-02-06 02:23:00 -08001643 }
brandtrb29e6522016-12-21 06:37:18 -08001644}
1645
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001646} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001647
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001648} // namespace webrtc