blob: 5835412f339480b7773a75ee14e0abef8b198a20 [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(
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001102 task_queue_factory_, worker_thread_, &video_receiver_controller_,
Tommi948e40c2021-05-31 12:39:57 +02001103 num_cpu_cores_, transport_send_->packet_router(),
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001104 std::move(configuration), module_process_thread_->process_thread(),
1105 call_stats_.get(), clock_, new VCMTiming(clock_));
Tommi733b5472016-06-10 17:58:01 +02001106
1107 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
Tommi31001a62020-05-26 11:38:36 +02001108 if (config.rtp.rtx_ssrc) {
1109 // We record identical config for the rtx stream as for the main
1110 // stream. Since the transport_send_cc negotiation is per payload
1111 // type, we may get an incorrect value for the rtx stream, but
1112 // that is unlikely to matter in practice.
1113 receive_rtp_config_.emplace(config.rtp.rtx_ssrc, ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -07001114 }
Tommi31001a62020-05-26 11:38:36 +02001115 receive_rtp_config_.emplace(config.rtp.remote_ssrc, ReceiveRtpConfig(config));
1116 video_receive_streams_.insert(receive_stream);
1117 ConfigureSync(config.sync_group);
1118
skvlad7a43d252016-03-22 15:32:27 -07001119 receive_stream->SignalNetworkState(video_network_state_);
1120 UpdateAggregateNetworkState();
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001121 event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001122 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001123 return receive_stream;
1124}
1125
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001126void Call::DestroyVideoReceiveStream(
1127 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001128 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001129 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -07001130 RTC_DCHECK(receive_stream != nullptr);
Tommi553c8692020-05-05 15:35:45 +02001131 VideoReceiveStream2* receive_stream_impl =
1132 static_cast<VideoReceiveStream2*>(receive_stream);
nissee4bcd6d2017-05-16 04:47:04 -07001133 const VideoReceiveStream::Config& config = receive_stream_impl->config();
Tommi31001a62020-05-26 11:38:36 +02001134
1135 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
1136 // separate SSRC there can be either one or two.
1137 receive_rtp_config_.erase(config.rtp.remote_ssrc);
1138 if (config.rtp.rtx_ssrc) {
1139 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001140 }
Tommi31001a62020-05-26 11:38:36 +02001141 video_receive_streams_.erase(receive_stream_impl);
1142 ConfigureSync(config.sync_group);
nisse4709e892017-02-07 01:18:43 -08001143
nisse559af382017-03-21 06:41:12 -07001144 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -08001145 ->RemoveStream(config.rtp.remote_ssrc);
1146
skvlad7a43d252016-03-22 15:32:27 -07001147 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001148 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001149}
1150
brandtr7250b392016-12-19 01:13:46 -08001151FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
1152 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -07001153 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001154 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001155
1156 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -07001157
nisse0f15f922017-06-21 01:05:22 -07001158 FlexfecReceiveStreamImpl* receive_stream;
brandtrb29e6522016-12-21 06:37:18 -08001159
Tommi31001a62020-05-26 11:38:36 +02001160 // Unlike the video and audio receive streams, FlexfecReceiveStream implements
1161 // RtpPacketSinkInterface itself, and hence its constructor passes its |this|
1162 // pointer to video_receiver_controller_->CreateStream(). Calling the
1163 // constructor while on the worker thread ensures that we don't call
1164 // OnRtpPacket until the constructor is finished and the object is
1165 // in a valid state, since OnRtpPacket runs on the same thread.
1166 receive_stream = new FlexfecReceiveStreamImpl(
1167 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
1168 call_stats_->AsRtcpRttStats(), module_process_thread_->process_thread());
1169
1170 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1171 receive_rtp_config_.end());
1172 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtrb29e6522016-12-21 06:37:18 -08001173
brandtr25445d32016-10-23 23:37:14 -07001174 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001175
brandtr25445d32016-10-23 23:37:14 -07001176 return receive_stream;
1177}
1178
brandtr7250b392016-12-19 01:13:46 -08001179void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001180 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001181 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001182
brandtr25445d32016-10-23 23:37:14 -07001183 RTC_DCHECK(receive_stream != nullptr);
Tommi31001a62020-05-26 11:38:36 +02001184 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
1185 uint32_t ssrc = config.remote_ssrc;
1186 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001187
Tommi31001a62020-05-26 11:38:36 +02001188 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1189 // destroyed.
1190 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
1191 ->RemoveStream(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001192
eladalon42f44f92017-07-25 06:40:06 -07001193 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001194}
1195
Henrik Boströmf4a99912020-06-11 12:07:14 +02001196void Call::AddAdaptationResource(rtc::scoped_refptr<Resource> resource) {
1197 RTC_DCHECK_RUN_ON(worker_thread_);
Henrik Boström29444c62020-07-01 15:48:46 +02001198 adaptation_resource_forwarders_.push_back(
1199 std::make_unique<ResourceVideoSendStreamForwarder>(resource));
1200 const auto& resource_forwarder = adaptation_resource_forwarders_.back();
1201 for (VideoSendStream* send_stream : video_send_streams_) {
1202 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001203 }
1204}
1205
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001206RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Tommi948e40c2021-05-31 12:39:57 +02001207 return transport_send_.get();
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001208}
1209
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001210Call::Stats Call::GetStats() const {
Tommi0d4647d2020-05-26 19:35:16 +02001211 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi48b48e52019-08-09 11:42:32 +02001212
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001213 Stats stats;
Tommi48b48e52019-08-09 11:42:32 +02001214 // TODO(srte): It is unclear if we only want to report queues if network is
1215 // available.
1216 stats.pacer_delay_ms =
Tommi948e40c2021-05-31 12:39:57 +02001217 aggregate_network_up_ ? transport_send_->GetPacerQueuingDelayMs() : 0;
Tommi48b48e52019-08-09 11:42:32 +02001218
1219 stats.rtt_ms = call_stats_->LastProcessedRtt();
1220
Peter Boström45553ae2015-05-08 13:54:38 +02001221 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001222 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001223 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001224 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001225 &ssrcs, &recv_bandwidth);
Tommi48b48e52019-08-09 11:42:32 +02001226 stats.recv_bandwidth_bps = recv_bandwidth;
Tommi0d4647d2020-05-26 19:35:16 +02001227 stats.send_bandwidth_bps = last_bandwidth_bps_;
1228 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
Tommi48b48e52019-08-09 11:42:32 +02001229
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001230 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001231}
1232
Erik Språngceb44952020-09-22 11:36:35 +02001233const WebRtcKeyValueConfig& Call::trials() const {
Tommi948e40c2021-05-31 12:39:57 +02001234 return trials_;
Erik Språngceb44952020-09-22 11:36:35 +02001235}
1236
Tomas Gunnarssone984aa22021-04-19 09:21:06 +02001237TaskQueueBase* Call::network_thread() const {
1238 return network_thread_;
1239}
1240
1241TaskQueueBase* Call::worker_thread() const {
1242 return worker_thread_;
1243}
1244
skvlad7a43d252016-03-22 15:32:27 -07001245void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001246 RTC_DCHECK_RUN_ON(network_thread_);
1247 RTC_DCHECK(media == MediaType::AUDIO || media == MediaType::VIDEO);
Tomas Gunnarssond48a2b12021-02-02 17:57:36 +01001248
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001249 auto closure = [this, media, state]() {
1250 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1251 RTC_DCHECK_RUN_ON(worker_thread_);
1252 if (media == MediaType::AUDIO) {
1253 audio_network_state_ = state;
1254 } else {
1255 RTC_DCHECK_EQ(media, MediaType::VIDEO);
1256 video_network_state_ = state;
1257 }
1258
1259 // TODO(tommi): Is it necessary to always do this, including if there
1260 // was no change in state?
1261 UpdateAggregateNetworkState();
1262
1263 // TODO(tommi): Is it right to do this if media == AUDIO?
1264 for (VideoReceiveStream2* video_receive_stream : video_receive_streams_) {
1265 video_receive_stream->SignalNetworkState(video_network_state_);
1266 }
1267 };
1268
1269 if (network_thread_ == worker_thread_) {
1270 closure();
1271 } else {
1272 // TODO(bugs.webrtc.org/11993): Remove workaround when we no longer need to
1273 // post to the worker thread.
1274 worker_thread_->PostTask(ToQueuedTask(task_safety_, std::move(closure)));
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001275 }
1276}
1277
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001278void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001279 RTC_DCHECK_RUN_ON(network_thread_);
1280 worker_thread_->PostTask(
1281 ToQueuedTask(task_safety_, [this, transport_overhead_per_packet]() {
1282 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1283 RTC_DCHECK_RUN_ON(worker_thread_);
1284 for (auto& kv : audio_send_ssrcs_) {
1285 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1286 }
1287 }));
michaelt79e05882016-11-08 02:50:09 -08001288}
1289
skvlad7a43d252016-03-22 15:32:27 -07001290void Call::UpdateAggregateNetworkState() {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001291 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1292 // RTC_DCHECK_RUN_ON(network_thread_);
1293
Tommi0d4647d2020-05-26 19:35:16 +02001294 RTC_DCHECK_RUN_ON(worker_thread_);
skvlad7a43d252016-03-22 15:32:27 -07001295
Tommi0d4647d2020-05-26 19:35:16 +02001296 bool have_audio =
1297 !audio_send_ssrcs_.empty() || !audio_receive_streams_.empty();
1298 bool have_video =
1299 !video_send_ssrcs_.empty() || !video_receive_streams_.empty();
skvlad7a43d252016-03-22 15:32:27 -07001300
Sebastian Janssona06e9192018-03-07 18:49:55 +01001301 bool aggregate_network_up =
1302 ((have_video && video_network_state_ == kNetworkUp) ||
1303 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001304
Harald Alvestrand977b2652019-12-12 13:40:50 +01001305 if (aggregate_network_up != aggregate_network_up_) {
1306 RTC_LOG(LS_INFO)
1307 << "UpdateAggregateNetworkState: aggregate_state change to "
1308 << (aggregate_network_up ? "up" : "down");
1309 } else {
1310 RTC_LOG(LS_VERBOSE)
1311 << "UpdateAggregateNetworkState: aggregate_state remains at "
1312 << (aggregate_network_up ? "up" : "down");
1313 }
Tommi48b48e52019-08-09 11:42:32 +02001314 aggregate_network_up_ = aggregate_network_up;
1315
Tommi948e40c2021-05-31 12:39:57 +02001316 transport_send_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001317}
1318
stefanc1aeaf02015-10-15 07:26:07 -07001319void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
Tomas Gunnarssoneb9c3f22021-04-19 12:53:09 +02001320 // In production and with most tests, this method will be called on the
1321 // network thread. However some test classes such as DirectTransport don't
1322 // incorporate a network thread. This means that tests for RtpSenderEgress
1323 // and ModuleRtpRtcpImpl2 that use DirectTransport, will call this method
1324 // on a ProcessThread. This is alright as is since we forward the call to
1325 // implementations that either just do a PostTask or use locking.
asapersson35151f32016-05-02 23:44:01 -07001326 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1327 clock_->TimeInMilliseconds());
Tommi948e40c2021-05-31 12:39:57 +02001328 transport_send_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001329}
1330
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001331void Call::OnStartRateUpdate(DataRate start_rate) {
Tommi948e40c2021-05-31 12:39:57 +02001332 RTC_DCHECK_RUN_ON(send_transport_queue_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001333 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1334}
1335
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001336void Call::OnTargetTransferRate(TargetTransferRate msg) {
Tommi948e40c2021-05-31 12:39:57 +02001337 RTC_DCHECK_RUN_ON(send_transport_queue_);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001338
1339 uint32_t target_bitrate_bps = msg.target_rate.bps();
nisse559af382017-03-21 06:41:12 -07001340 // For controlling the rate of feedback messages.
1341 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001342 bitrate_allocator_->OnNetworkEstimateChanged(msg);
mflodman0e7e2592015-11-12 21:02:42 -08001343
Tommi0d4647d2020-05-26 19:35:16 +02001344 worker_thread_->PostTask(
1345 ToQueuedTask(task_safety_, [this, target_bitrate_bps]() {
1346 RTC_DCHECK_RUN_ON(worker_thread_);
1347 last_bandwidth_bps_ = target_bitrate_bps;
asaperssonce2e1362016-09-09 00:13:35 -07001348
Tommi0d4647d2020-05-26 19:35:16 +02001349 // Ignore updates if bitrate is zero (the aggregate network state is
1350 // down) or if we're not sending video.
1351 if (target_bitrate_bps == 0 || video_send_streams_.empty()) {
1352 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1353 pacer_bitrate_kbps_counter_.ProcessAndPause();
1354 return;
1355 }
asaperssonce2e1362016-09-09 00:13:35 -07001356
Tommi0d4647d2020-05-26 19:35:16 +02001357 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1358 // Pacer bitrate may be higher than bitrate estimate if enforcing min
1359 // bitrate.
1360 uint32_t pacer_bitrate_bps =
1361 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1362 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
1363 }));
perkj71ee44c2016-06-15 00:47:53 -07001364}
mflodman101f2502016-06-09 17:21:19 +02001365
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001366void Call::OnAllocationLimitsChanged(BitrateAllocationLimits limits) {
Tommi948e40c2021-05-31 12:39:57 +02001367 RTC_DCHECK_RUN_ON(send_transport_queue_);
Tommi48b48e52019-08-09 11:42:32 +02001368
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001369 transport_send_ptr_->SetAllocatedSendBitrateLimits(limits);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001370
Tommi0d4647d2020-05-26 19:35:16 +02001371 worker_thread_->PostTask(ToQueuedTask(task_safety_, [this, limits]() {
1372 RTC_DCHECK_RUN_ON(worker_thread_);
1373 min_allocated_send_bitrate_bps_ = limits.min_allocatable_rate.bps();
1374 configured_max_padding_bitrate_bps_ = limits.max_padding_rate.bps();
1375 }));
mflodman0e7e2592015-11-12 21:02:42 -08001376}
1377
pbos8fc7fa72015-07-15 08:02:58 -07001378void Call::ConfigureSync(const std::string& sync_group) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001379 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
pbos8fc7fa72015-07-15 08:02:58 -07001380 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001381 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001382 return;
1383
1384 AudioReceiveStream* sync_audio_stream = nullptr;
1385 // Find existing audio stream.
1386 const auto it = sync_stream_mapping_.find(sync_group);
1387 if (it != sync_stream_mapping_.end()) {
1388 sync_audio_stream = it->second;
1389 } else {
1390 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001391 for (AudioReceiveStream* stream : audio_receive_streams_) {
1392 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001393 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001394 RTC_LOG(LS_WARNING)
1395 << "Attempting to sync more than one audio stream "
1396 "within the same sync group. This is not "
1397 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001398 break;
1399 }
nissee4bcd6d2017-05-16 04:47:04 -07001400 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001401 }
1402 }
1403 }
1404 if (sync_audio_stream)
1405 sync_stream_mapping_[sync_group] = sync_audio_stream;
1406 size_t num_synced_streams = 0;
Tommi553c8692020-05-05 15:35:45 +02001407 for (VideoReceiveStream2* video_stream : video_receive_streams_) {
pbos8fc7fa72015-07-15 08:02:58 -07001408 if (video_stream->config().sync_group != sync_group)
1409 continue;
1410 ++num_synced_streams;
1411 if (num_synced_streams > 1) {
1412 // TODO(pbos): Support synchronizing more than one A/V pair.
1413 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001414 RTC_LOG(LS_WARNING)
1415 << "Attempting to sync more than one audio/video pair "
1416 "within the same sync group. This is not supported in "
1417 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001418 }
1419 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001420 if (num_synced_streams == 1) {
1421 // sync_audio_stream may be null and that's ok.
1422 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001423 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001424 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001425 }
1426 }
1427}
1428
Tommicae1f1d2021-05-31 10:51:09 +02001429// RTC_RUN_ON(network_thread_)
1430void Call::DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001431 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
Tommi3f418cc2021-05-05 11:04:30 +02001432
1433 // TODO(bugs.webrtc.org/11993): This DCHECK is here just to maintain the
1434 // invariant that currently the only call path to this function is via
1435 // `PeerConnection::InitializeRtcpCallback()`. DeliverRtp on the other hand
1436 // gets called via the channel classes and
1437 // WebRtc[Audio|Video]Channel's `OnPacketReceived`. We'll remove the
1438 // PeerConnection involvement as well as
1439 // `JsepTransportController::OnRtcpPacketReceived_n` and `rtcp_handler`
1440 // and make sure that the flow of packets is consistent from the
1441 // `RtpTransport` class, via the *Channel and *Engine classes and into Call.
1442 // This way we'll also know more about the context of the packet.
1443 RTC_DCHECK_EQ(media_type, MediaType::ANY);
1444
Tommicae1f1d2021-05-31 10:51:09 +02001445 // TODO(bugs.webrtc.org/11993): This should execute directly on the network
1446 // thread.
1447 worker_thread_->PostTask(
1448 ToQueuedTask(task_safety_, [this, packet = std::move(packet)]() {
1449 RTC_DCHECK_RUN_ON(worker_thread_);
mflodman3d7db262016-04-29 00:57:13 -07001450
Tommicae1f1d2021-05-31 10:51:09 +02001451 receive_stats_.AddReceivedRtcpBytes(static_cast<int>(packet.size()));
1452 bool rtcp_delivered = false;
1453 for (VideoReceiveStream2* stream : video_receive_streams_) {
1454 if (stream->DeliverRtcp(packet.cdata(), packet.size()))
1455 rtcp_delivered = true;
1456 }
mflodman3d7db262016-04-29 00:57:13 -07001457
Tommicae1f1d2021-05-31 10:51:09 +02001458 for (AudioReceiveStream* stream : audio_receive_streams_) {
1459 stream->DeliverRtcp(packet.cdata(), packet.size());
1460 rtcp_delivered = true;
1461 }
1462
1463 for (VideoSendStream* stream : video_send_streams_) {
1464 stream->DeliverRtcp(packet.cdata(), packet.size());
1465 rtcp_delivered = true;
1466 }
1467
1468 for (auto& kv : audio_send_ssrcs_) {
1469 kv.second->DeliverRtcp(packet.cdata(), packet.size());
1470 rtcp_delivered = true;
1471 }
1472
1473 if (rtcp_delivered) {
1474 event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>(
1475 rtc::MakeArrayView(packet.cdata(), packet.size())));
1476 }
1477 }));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001478}
1479
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001480PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001481 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001482 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001483 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
Tommi3f418cc2021-05-05 11:04:30 +02001484 RTC_DCHECK_NE(media_type, MediaType::ANY);
nissed44ce052017-02-06 02:23:00 -08001485
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001486 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001487 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001488 return DELIVERY_PACKET_ERROR;
1489
Niels Möller70082872018-08-07 11:03:12 +02001490 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001491 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001492 // Repair packet_time_us for clock resets by comparing a new read of
1493 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001494 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001495 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001496 }
Tommi2497a272021-05-05 12:33:00 +02001497 parsed_packet.set_arrival_time(Timestamp::Micros(packet_time_us));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001498 } else {
Tommi2497a272021-05-05 12:33:00 +02001499 parsed_packet.set_arrival_time(clock_->CurrentTime());
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001500 }
nissed44ce052017-02-06 02:23:00 -08001501
sprangc1abde72017-07-11 03:56:21 -07001502 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1503 // These are empty (zero length payload) RTP packets with an unsignaled
1504 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001505 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001506
1507 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1508 is_keep_alive_packet);
1509
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001510 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001511 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001512 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1513 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001514 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001515 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001516 // But deregistering in the |receive_rtp_config_| map is. So by not passing
1517 // the packet on to demuxing in this case, we prevent incoming packets to be
1518 // passed on via the demuxer to a receive stream which is being torned down.
nisse0f15f922017-06-21 01:05:22 -07001519 return DELIVERY_UNKNOWN_SSRC;
1520 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001521
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001522 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001523
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001524 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001525
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001526 // RateCounters expect input parameter as int, save it as int,
1527 // instead of converting each time it is passed to RateCounter::Add below.
1528 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001529 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001530 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001531 receive_stats_.AddReceivedAudioBytes(length,
1532 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001533 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001534 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse657bab22017-02-21 06:28:10 -08001535 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001536 }
nissee4bcd6d2017-05-16 04:47:04 -07001537 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001538 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001539 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001540 receive_stats_.AddReceivedVideoBytes(length,
1541 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001542 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001543 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse5c29a7a2017-02-16 06:52:32 -08001544 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001545 }
1546 }
1547 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001548}
1549
stefan68786d22015-09-08 05:36:15 -07001550PacketReceiver::DeliveryStatus Call::DeliverPacket(
1551 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001552 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001553 int64_t packet_time_us) {
Tommicae1f1d2021-05-31 10:51:09 +02001554 if (IsRtcp(packet.cdata(), packet.size())) {
1555 RTC_DCHECK_RUN_ON(network_thread_);
1556 DeliverRtcp(media_type, std::move(packet));
1557 return DELIVERY_OK;
1558 }
1559
Tommi0d4647d2020-05-26 19:35:16 +02001560 RTC_DCHECK_RUN_ON(worker_thread_);
Niels Möller70082872018-08-07 11:03:12 +02001561 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001562}
1563
nissed2ef3142017-05-11 08:00:58 -07001564void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001565 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
1566 // This method is called synchronously via |OnRtpPacket()| (see DeliverRtp)
1567 // on the same thread.
Tommi0d4647d2020-05-26 19:35:16 +02001568 RTC_DCHECK_RUN_ON(worker_thread_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001569 RtpPacketReceived parsed_packet;
1570 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001571 return;
1572
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001573 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001574
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001575 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001576 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001577 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1578 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001579 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001580 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001581 // But deregistering in the |receive_rtp_config_| map is.
brandtrcaea68f2017-08-23 00:55:17 -07001582 // So by not passing the packet on to demuxing in this case, we prevent
1583 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001584 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001585 return;
1586 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001587 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001588
1589 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001590 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001591 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001592}
1593
Tommi948e40c2021-05-31 12:39:57 +02001594// RTC_RUN_ON(worker_thread_)
nissed44ce052017-02-06 02:23:00 -08001595void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1596 MediaType media_type) {
1597 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001598 bool use_send_side_bwe =
1599 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001600
brandtrb29e6522016-12-21 06:37:18 -08001601 RTPHeader header;
1602 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001603
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001604 ReceivedPacket packet_msg;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001605 packet_msg.size = DataSize::Bytes(packet.payload_size());
Tommi2497a272021-05-05 12:33:00 +02001606 packet_msg.receive_time = packet.arrival_time();
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001607 if (header.extension.hasAbsoluteSendTime) {
1608 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1609 }
Tommi948e40c2021-05-31 12:39:57 +02001610 transport_send_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001611
nisse4709e892017-02-07 01:18:43 -08001612 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001613 // Inconsistent configuration of send side BWE. Do nothing.
1614 // TODO(nisse): Without this check, we may produce RTCP feedback
1615 // packets even when not negotiated. But it would be cleaner to
1616 // move the check down to RTCPSender::SendFeedbackPacket, which
1617 // would also help the PacketRouter to select an appropriate rtp
1618 // module in the case that some, but not all, have RTCP feedback
1619 // enabled.
1620 return;
1621 }
1622 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001623 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001624 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001625 receive_side_cc_.OnReceivedPacket(
Tommi2497a272021-05-05 12:33:00 +02001626 packet.arrival_time().ms(),
1627 packet.payload_size() + packet.padding_size(), header);
nissed44ce052017-02-06 02:23:00 -08001628 }
brandtrb29e6522016-12-21 06:37:18 -08001629}
1630
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001631} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001632
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001633} // namespace webrtc