blob: de30d6540f7e2d2fb4e9f8203ee1940f88d7d7b0 [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
926 // TODO(bugs.webrtc.org/11993): Move the registration between |receive_stream|
927 // and |audio_receiver_controller_| out of AudioReceiveStream construction and
928 // set it up asynchronously on the network thread (the registration and
929 // |audio_receiver_controller_| need to live on the network thread).
nisse0f15f922017-06-21 01:05:22 -0700930 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Tommi948e40c2021-05-31 12:39:57 +0200931 clock_, &audio_receiver_controller_, transport_send_->packet_router(),
Tommi25c77c12020-05-25 17:44:55 +0200932 module_process_thread_->process_thread(), config_.neteq_factory, config,
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +0100933 config_.audio_state, event_log_);
nissed44ce052017-02-06 02:23:00 -0800934
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100935 // TODO(bugs.webrtc.org/11993): Update the below on the network thread.
936 // We could possibly set up the audio_receiver_controller_ association up
937 // as part of the async setup.
Tommi31001a62020-05-26 11:38:36 +0200938 receive_rtp_config_.emplace(config.rtp.remote_ssrc, ReceiveRtpConfig(config));
939 audio_receive_streams_.insert(receive_stream);
940
941 ConfigureSync(config.sync_group);
942
Tommi0d4647d2020-05-26 19:35:16 +0200943 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
944 if (it != audio_send_ssrcs_.end()) {
945 receive_stream->AssociateSendStream(it->second);
solenberg7602aab2016-11-14 11:30:07 -0800946 }
Tommi0d4647d2020-05-26 19:35:16 +0200947
skvlad7a43d252016-03-22 15:32:27 -0700948 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200949 return receive_stream;
950}
951
952void Call::DestroyAudioReceiveStream(
953 webrtc::AudioReceiveStream* receive_stream) {
954 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +0200955 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -0700956 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700957 webrtc::internal::AudioReceiveStream* audio_receive_stream =
958 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Tommi31001a62020-05-26 11:38:36 +0200959
960 const AudioReceiveStream::Config& config = audio_receive_stream->config();
961 uint32_t ssrc = config.rtp.remote_ssrc;
962 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
963 ->RemoveStream(ssrc);
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100964
965 // TODO(bugs.webrtc.org/11993): Access the map, rtp config, call ConfigureSync
966 // and UpdateAggregateNetworkState on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200967 audio_receive_streams_.erase(audio_receive_stream);
968 const std::string& sync_group = audio_receive_stream->config().sync_group;
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100969
Tommi31001a62020-05-26 11:38:36 +0200970 const auto it = sync_stream_mapping_.find(sync_group);
971 if (it != sync_stream_mapping_.end() && it->second == audio_receive_stream) {
972 sync_stream_mapping_.erase(it);
973 ConfigureSync(sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200974 }
Tommi31001a62020-05-26 11:38:36 +0200975 receive_rtp_config_.erase(ssrc);
976
skvlad7a43d252016-03-22 15:32:27 -0700977 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100978 // TODO(bugs.webrtc.org/11993): Consider if deleting |audio_receive_stream|
979 // on the network thread would be better or if we'd need to tear down the
980 // state in two phases.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200981 delete audio_receive_stream;
982}
983
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100984// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100985webrtc::VideoSendStream* Call::CreateVideoSendStream(
986 webrtc::VideoSendStream::Config config,
987 VideoEncoderConfig encoder_config,
988 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000989 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200990 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000991
Erik Språng7703f232020-09-14 11:03:13 +0200992 EnsureStarted();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800993
asapersson35151f32016-05-02 23:44:01 -0700994 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700995 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
996 ++ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200997 event_log_->Log(std::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200998 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700999 }
perkj26091b12016-09-01 01:17:40 -07001000
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +00001001 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
1002 // the call has already started.
perkj26091b12016-09-01 01:17:40 -07001003 // Copy ssrcs from |config| since |config| is moved.
1004 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001005
mflodman0c478b32015-10-21 15:52:16 +02001006 VideoSendStream* send_stream = new VideoSendStream(
Tommi25c77c12020-05-25 17:44:55 +02001007 clock_, num_cpu_cores_, module_process_thread_->process_thread(),
Tommi948e40c2021-05-31 12:39:57 +02001008 task_queue_factory_, call_stats_->AsRtcpRttStats(), transport_send_.get(),
Tommi822a8742020-05-11 00:42:30 +02001009 bitrate_allocator_.get(), video_send_delay_stats_.get(), event_log_,
1010 std::move(config), std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +02001011 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -07001012
Tommi0d4647d2020-05-26 19:35:16 +02001013 for (uint32_t ssrc : ssrcs) {
1014 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
1015 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001016 }
Tommi0d4647d2020-05-26 19:35:16 +02001017 video_send_streams_.insert(send_stream);
Henrik Boström29444c62020-07-01 15:48:46 +02001018 // Forward resources that were previously added to the call to the new stream.
1019 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
1020 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001021 }
Tommi0d4647d2020-05-26 19:35:16 +02001022
skvlad7a43d252016-03-22 15:32:27 -07001023 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -07001024
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001025 return send_stream;
1026}
1027
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001028webrtc::VideoSendStream* Call::CreateVideoSendStream(
1029 webrtc::VideoSendStream::Config config,
1030 VideoEncoderConfig encoder_config) {
Tommi948e40c2021-05-31 12:39:57 +02001031 RTC_DCHECK_RUN_ON(worker_thread_);
Ying Wang012b7e72018-03-05 15:44:23 +01001032 if (config_.fec_controller_factory) {
1033 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
1034 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001035 std::unique_ptr<FecController> fec_controller =
1036 config_.fec_controller_factory
1037 ? config_.fec_controller_factory->CreateFecController()
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001038 : std::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +01001039 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
1040 std::move(fec_controller));
1041}
1042
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001043void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001044 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -07001045 RTC_DCHECK(send_stream != nullptr);
Tommi0d4647d2020-05-26 19:35:16 +02001046 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001047
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001048 send_stream->Stop();
1049
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +00001050 VideoSendStream* send_stream_impl = nullptr;
Tommi0d4647d2020-05-26 19:35:16 +02001051
1052 auto it = video_send_ssrcs_.begin();
1053 while (it != video_send_ssrcs_.end()) {
1054 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
1055 send_stream_impl = it->second;
1056 video_send_ssrcs_.erase(it++);
1057 } else {
1058 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001059 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001060 }
Henrik Boström29444c62020-07-01 15:48:46 +02001061 // Stop forwarding resources to the stream being destroyed.
1062 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
1063 resource_forwarder->OnDestroyVideoSendStream(send_stream_impl);
1064 }
Tommi0d4647d2020-05-26 19:35:16 +02001065 video_send_streams_.erase(send_stream_impl);
1066
henrikg91d6ede2015-09-17 00:24:34 -07001067 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001068
Åsa Persson4bece9a2017-10-06 10:04:04 +02001069 VideoSendStream::RtpStateMap rtp_states;
1070 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
1071 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
1072 &rtp_payload_states);
1073 for (const auto& kv : rtp_states) {
1074 suspended_video_send_ssrcs_[kv.first] = kv.second;
1075 }
1076 for (const auto& kv : rtp_payload_states) {
1077 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001078 }
1079
skvlad7a43d252016-03-22 15:32:27 -07001080 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001081 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001082}
1083
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001084webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +02001085 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001086 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001087 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrfb45c6c2017-01-27 06:47:55 -08001088
Johannes Kronf59666b2019-04-08 12:57:06 +02001089 receive_side_cc_.SetSendPeriodicFeedback(
1090 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +01001091
Erik Språng7703f232020-09-14 11:03:13 +02001092 EnsureStarted();
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -08001093
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001094 // TODO(bugs.webrtc.org/11993): Move the registration between |receive_stream|
1095 // and |video_receiver_controller_| out of VideoReceiveStream2 construction
1096 // and set it up asynchronously on the network thread (the registration and
1097 // |video_receiver_controller_| need to live on the network thread).
Tommi553c8692020-05-05 15:35:45 +02001098 VideoReceiveStream2* receive_stream = new VideoReceiveStream2(
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001099 task_queue_factory_, worker_thread_, &video_receiver_controller_,
Tommi948e40c2021-05-31 12:39:57 +02001100 num_cpu_cores_, transport_send_->packet_router(),
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001101 std::move(configuration), module_process_thread_->process_thread(),
1102 call_stats_.get(), clock_, new VCMTiming(clock_));
Tommi733b5472016-06-10 17:58:01 +02001103
1104 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
Tommi31001a62020-05-26 11:38:36 +02001105 if (config.rtp.rtx_ssrc) {
1106 // We record identical config for the rtx stream as for the main
1107 // stream. Since the transport_send_cc negotiation is per payload
1108 // type, we may get an incorrect value for the rtx stream, but
1109 // that is unlikely to matter in practice.
1110 receive_rtp_config_.emplace(config.rtp.rtx_ssrc, ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -07001111 }
Tommi31001a62020-05-26 11:38:36 +02001112 receive_rtp_config_.emplace(config.rtp.remote_ssrc, ReceiveRtpConfig(config));
1113 video_receive_streams_.insert(receive_stream);
1114 ConfigureSync(config.sync_group);
1115
skvlad7a43d252016-03-22 15:32:27 -07001116 receive_stream->SignalNetworkState(video_network_state_);
1117 UpdateAggregateNetworkState();
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001118 event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001119 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001120 return receive_stream;
1121}
1122
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001123void Call::DestroyVideoReceiveStream(
1124 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001125 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001126 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -07001127 RTC_DCHECK(receive_stream != nullptr);
Tommi553c8692020-05-05 15:35:45 +02001128 VideoReceiveStream2* receive_stream_impl =
1129 static_cast<VideoReceiveStream2*>(receive_stream);
nissee4bcd6d2017-05-16 04:47:04 -07001130 const VideoReceiveStream::Config& config = receive_stream_impl->config();
Tommi31001a62020-05-26 11:38:36 +02001131
1132 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
1133 // separate SSRC there can be either one or two.
1134 receive_rtp_config_.erase(config.rtp.remote_ssrc);
1135 if (config.rtp.rtx_ssrc) {
1136 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001137 }
Tommi31001a62020-05-26 11:38:36 +02001138 video_receive_streams_.erase(receive_stream_impl);
1139 ConfigureSync(config.sync_group);
nisse4709e892017-02-07 01:18:43 -08001140
nisse559af382017-03-21 06:41:12 -07001141 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -08001142 ->RemoveStream(config.rtp.remote_ssrc);
1143
skvlad7a43d252016-03-22 15:32:27 -07001144 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001145 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001146}
1147
brandtr7250b392016-12-19 01:13:46 -08001148FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
1149 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -07001150 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001151 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001152
1153 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -07001154
nisse0f15f922017-06-21 01:05:22 -07001155 FlexfecReceiveStreamImpl* receive_stream;
brandtrb29e6522016-12-21 06:37:18 -08001156
Tommi31001a62020-05-26 11:38:36 +02001157 // Unlike the video and audio receive streams, FlexfecReceiveStream implements
1158 // RtpPacketSinkInterface itself, and hence its constructor passes its |this|
1159 // pointer to video_receiver_controller_->CreateStream(). Calling the
1160 // constructor while on the worker thread ensures that we don't call
1161 // OnRtpPacket until the constructor is finished and the object is
1162 // in a valid state, since OnRtpPacket runs on the same thread.
1163 receive_stream = new FlexfecReceiveStreamImpl(
1164 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
1165 call_stats_->AsRtcpRttStats(), module_process_thread_->process_thread());
1166
1167 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1168 receive_rtp_config_.end());
1169 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtrb29e6522016-12-21 06:37:18 -08001170
brandtr25445d32016-10-23 23:37:14 -07001171 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001172
brandtr25445d32016-10-23 23:37:14 -07001173 return receive_stream;
1174}
1175
brandtr7250b392016-12-19 01:13:46 -08001176void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001177 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001178 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001179
brandtr25445d32016-10-23 23:37:14 -07001180 RTC_DCHECK(receive_stream != nullptr);
Tommi31001a62020-05-26 11:38:36 +02001181 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
1182 uint32_t ssrc = config.remote_ssrc;
1183 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001184
Tommi31001a62020-05-26 11:38:36 +02001185 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1186 // destroyed.
1187 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
1188 ->RemoveStream(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001189
eladalon42f44f92017-07-25 06:40:06 -07001190 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001191}
1192
Henrik Boströmf4a99912020-06-11 12:07:14 +02001193void Call::AddAdaptationResource(rtc::scoped_refptr<Resource> resource) {
1194 RTC_DCHECK_RUN_ON(worker_thread_);
Henrik Boström29444c62020-07-01 15:48:46 +02001195 adaptation_resource_forwarders_.push_back(
1196 std::make_unique<ResourceVideoSendStreamForwarder>(resource));
1197 const auto& resource_forwarder = adaptation_resource_forwarders_.back();
1198 for (VideoSendStream* send_stream : video_send_streams_) {
1199 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001200 }
1201}
1202
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001203RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Tommi948e40c2021-05-31 12:39:57 +02001204 return transport_send_.get();
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001205}
1206
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001207Call::Stats Call::GetStats() const {
Tommi0d4647d2020-05-26 19:35:16 +02001208 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi48b48e52019-08-09 11:42:32 +02001209
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001210 Stats stats;
Tommi48b48e52019-08-09 11:42:32 +02001211 // TODO(srte): It is unclear if we only want to report queues if network is
1212 // available.
1213 stats.pacer_delay_ms =
Tommi948e40c2021-05-31 12:39:57 +02001214 aggregate_network_up_ ? transport_send_->GetPacerQueuingDelayMs() : 0;
Tommi48b48e52019-08-09 11:42:32 +02001215
1216 stats.rtt_ms = call_stats_->LastProcessedRtt();
1217
Peter Boström45553ae2015-05-08 13:54:38 +02001218 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001219 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001220 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001221 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001222 &ssrcs, &recv_bandwidth);
Tommi48b48e52019-08-09 11:42:32 +02001223 stats.recv_bandwidth_bps = recv_bandwidth;
Tommi0d4647d2020-05-26 19:35:16 +02001224 stats.send_bandwidth_bps = last_bandwidth_bps_;
1225 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
Tommi48b48e52019-08-09 11:42:32 +02001226
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001227 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001228}
1229
Erik Språngceb44952020-09-22 11:36:35 +02001230const WebRtcKeyValueConfig& Call::trials() const {
Tommi948e40c2021-05-31 12:39:57 +02001231 return trials_;
Erik Språngceb44952020-09-22 11:36:35 +02001232}
1233
Tomas Gunnarssone984aa22021-04-19 09:21:06 +02001234TaskQueueBase* Call::network_thread() const {
1235 return network_thread_;
1236}
1237
1238TaskQueueBase* Call::worker_thread() const {
1239 return worker_thread_;
1240}
1241
skvlad7a43d252016-03-22 15:32:27 -07001242void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001243 RTC_DCHECK_RUN_ON(network_thread_);
1244 RTC_DCHECK(media == MediaType::AUDIO || media == MediaType::VIDEO);
Tomas Gunnarssond48a2b12021-02-02 17:57:36 +01001245
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001246 auto closure = [this, media, state]() {
1247 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1248 RTC_DCHECK_RUN_ON(worker_thread_);
1249 if (media == MediaType::AUDIO) {
1250 audio_network_state_ = state;
1251 } else {
1252 RTC_DCHECK_EQ(media, MediaType::VIDEO);
1253 video_network_state_ = state;
1254 }
1255
1256 // TODO(tommi): Is it necessary to always do this, including if there
1257 // was no change in state?
1258 UpdateAggregateNetworkState();
1259
1260 // TODO(tommi): Is it right to do this if media == AUDIO?
1261 for (VideoReceiveStream2* video_receive_stream : video_receive_streams_) {
1262 video_receive_stream->SignalNetworkState(video_network_state_);
1263 }
1264 };
1265
1266 if (network_thread_ == worker_thread_) {
1267 closure();
1268 } else {
1269 // TODO(bugs.webrtc.org/11993): Remove workaround when we no longer need to
1270 // post to the worker thread.
1271 worker_thread_->PostTask(ToQueuedTask(task_safety_, std::move(closure)));
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001272 }
1273}
1274
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001275void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001276 RTC_DCHECK_RUN_ON(network_thread_);
1277 worker_thread_->PostTask(
1278 ToQueuedTask(task_safety_, [this, transport_overhead_per_packet]() {
1279 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1280 RTC_DCHECK_RUN_ON(worker_thread_);
1281 for (auto& kv : audio_send_ssrcs_) {
1282 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1283 }
1284 }));
michaelt79e05882016-11-08 02:50:09 -08001285}
1286
skvlad7a43d252016-03-22 15:32:27 -07001287void Call::UpdateAggregateNetworkState() {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001288 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1289 // RTC_DCHECK_RUN_ON(network_thread_);
1290
Tommi0d4647d2020-05-26 19:35:16 +02001291 RTC_DCHECK_RUN_ON(worker_thread_);
skvlad7a43d252016-03-22 15:32:27 -07001292
Tommi0d4647d2020-05-26 19:35:16 +02001293 bool have_audio =
1294 !audio_send_ssrcs_.empty() || !audio_receive_streams_.empty();
1295 bool have_video =
1296 !video_send_ssrcs_.empty() || !video_receive_streams_.empty();
skvlad7a43d252016-03-22 15:32:27 -07001297
Sebastian Janssona06e9192018-03-07 18:49:55 +01001298 bool aggregate_network_up =
1299 ((have_video && video_network_state_ == kNetworkUp) ||
1300 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001301
Harald Alvestrand977b2652019-12-12 13:40:50 +01001302 if (aggregate_network_up != aggregate_network_up_) {
1303 RTC_LOG(LS_INFO)
1304 << "UpdateAggregateNetworkState: aggregate_state change to "
1305 << (aggregate_network_up ? "up" : "down");
1306 } else {
1307 RTC_LOG(LS_VERBOSE)
1308 << "UpdateAggregateNetworkState: aggregate_state remains at "
1309 << (aggregate_network_up ? "up" : "down");
1310 }
Tommi48b48e52019-08-09 11:42:32 +02001311 aggregate_network_up_ = aggregate_network_up;
1312
Tommi948e40c2021-05-31 12:39:57 +02001313 transport_send_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001314}
1315
stefanc1aeaf02015-10-15 07:26:07 -07001316void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
Tomas Gunnarssoneb9c3f22021-04-19 12:53:09 +02001317 // In production and with most tests, this method will be called on the
1318 // network thread. However some test classes such as DirectTransport don't
1319 // incorporate a network thread. This means that tests for RtpSenderEgress
1320 // and ModuleRtpRtcpImpl2 that use DirectTransport, will call this method
1321 // on a ProcessThread. This is alright as is since we forward the call to
1322 // implementations that either just do a PostTask or use locking.
asapersson35151f32016-05-02 23:44:01 -07001323 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1324 clock_->TimeInMilliseconds());
Tommi948e40c2021-05-31 12:39:57 +02001325 transport_send_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001326}
1327
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001328void Call::OnStartRateUpdate(DataRate start_rate) {
Tommi948e40c2021-05-31 12:39:57 +02001329 RTC_DCHECK_RUN_ON(send_transport_queue_);
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001330 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1331}
1332
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001333void Call::OnTargetTransferRate(TargetTransferRate msg) {
Tommi948e40c2021-05-31 12:39:57 +02001334 RTC_DCHECK_RUN_ON(send_transport_queue_);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001335
1336 uint32_t target_bitrate_bps = msg.target_rate.bps();
nisse559af382017-03-21 06:41:12 -07001337 // For controlling the rate of feedback messages.
1338 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001339 bitrate_allocator_->OnNetworkEstimateChanged(msg);
mflodman0e7e2592015-11-12 21:02:42 -08001340
Tommi0d4647d2020-05-26 19:35:16 +02001341 worker_thread_->PostTask(
1342 ToQueuedTask(task_safety_, [this, target_bitrate_bps]() {
1343 RTC_DCHECK_RUN_ON(worker_thread_);
1344 last_bandwidth_bps_ = target_bitrate_bps;
asaperssonce2e1362016-09-09 00:13:35 -07001345
Tommi0d4647d2020-05-26 19:35:16 +02001346 // Ignore updates if bitrate is zero (the aggregate network state is
1347 // down) or if we're not sending video.
1348 if (target_bitrate_bps == 0 || video_send_streams_.empty()) {
1349 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1350 pacer_bitrate_kbps_counter_.ProcessAndPause();
1351 return;
1352 }
asaperssonce2e1362016-09-09 00:13:35 -07001353
Tommi0d4647d2020-05-26 19:35:16 +02001354 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1355 // Pacer bitrate may be higher than bitrate estimate if enforcing min
1356 // bitrate.
1357 uint32_t pacer_bitrate_bps =
1358 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1359 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
1360 }));
perkj71ee44c2016-06-15 00:47:53 -07001361}
mflodman101f2502016-06-09 17:21:19 +02001362
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001363void Call::OnAllocationLimitsChanged(BitrateAllocationLimits limits) {
Tommi948e40c2021-05-31 12:39:57 +02001364 RTC_DCHECK_RUN_ON(send_transport_queue_);
Tommi48b48e52019-08-09 11:42:32 +02001365
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001366 transport_send_ptr_->SetAllocatedSendBitrateLimits(limits);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001367
Tommi0d4647d2020-05-26 19:35:16 +02001368 worker_thread_->PostTask(ToQueuedTask(task_safety_, [this, limits]() {
1369 RTC_DCHECK_RUN_ON(worker_thread_);
1370 min_allocated_send_bitrate_bps_ = limits.min_allocatable_rate.bps();
1371 configured_max_padding_bitrate_bps_ = limits.max_padding_rate.bps();
1372 }));
mflodman0e7e2592015-11-12 21:02:42 -08001373}
1374
pbos8fc7fa72015-07-15 08:02:58 -07001375void Call::ConfigureSync(const std::string& sync_group) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001376 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
pbos8fc7fa72015-07-15 08:02:58 -07001377 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001378 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001379 return;
1380
1381 AudioReceiveStream* sync_audio_stream = nullptr;
1382 // Find existing audio stream.
1383 const auto it = sync_stream_mapping_.find(sync_group);
1384 if (it != sync_stream_mapping_.end()) {
1385 sync_audio_stream = it->second;
1386 } else {
1387 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001388 for (AudioReceiveStream* stream : audio_receive_streams_) {
1389 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001390 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001391 RTC_LOG(LS_WARNING)
1392 << "Attempting to sync more than one audio stream "
1393 "within the same sync group. This is not "
1394 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001395 break;
1396 }
nissee4bcd6d2017-05-16 04:47:04 -07001397 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001398 }
1399 }
1400 }
1401 if (sync_audio_stream)
1402 sync_stream_mapping_[sync_group] = sync_audio_stream;
1403 size_t num_synced_streams = 0;
Tommi553c8692020-05-05 15:35:45 +02001404 for (VideoReceiveStream2* video_stream : video_receive_streams_) {
pbos8fc7fa72015-07-15 08:02:58 -07001405 if (video_stream->config().sync_group != sync_group)
1406 continue;
1407 ++num_synced_streams;
1408 if (num_synced_streams > 1) {
1409 // TODO(pbos): Support synchronizing more than one A/V pair.
1410 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001411 RTC_LOG(LS_WARNING)
1412 << "Attempting to sync more than one audio/video pair "
1413 "within the same sync group. This is not supported in "
1414 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001415 }
1416 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001417 if (num_synced_streams == 1) {
1418 // sync_audio_stream may be null and that's ok.
1419 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001420 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001421 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001422 }
1423 }
1424}
1425
Tommicae1f1d2021-05-31 10:51:09 +02001426// RTC_RUN_ON(network_thread_)
1427void Call::DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001428 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
Tommi3f418cc2021-05-05 11:04:30 +02001429
1430 // TODO(bugs.webrtc.org/11993): This DCHECK is here just to maintain the
1431 // invariant that currently the only call path to this function is via
1432 // `PeerConnection::InitializeRtcpCallback()`. DeliverRtp on the other hand
1433 // gets called via the channel classes and
1434 // WebRtc[Audio|Video]Channel's `OnPacketReceived`. We'll remove the
1435 // PeerConnection involvement as well as
1436 // `JsepTransportController::OnRtcpPacketReceived_n` and `rtcp_handler`
1437 // and make sure that the flow of packets is consistent from the
1438 // `RtpTransport` class, via the *Channel and *Engine classes and into Call.
1439 // This way we'll also know more about the context of the packet.
1440 RTC_DCHECK_EQ(media_type, MediaType::ANY);
1441
Tommicae1f1d2021-05-31 10:51:09 +02001442 // TODO(bugs.webrtc.org/11993): This should execute directly on the network
1443 // thread.
1444 worker_thread_->PostTask(
1445 ToQueuedTask(task_safety_, [this, packet = std::move(packet)]() {
1446 RTC_DCHECK_RUN_ON(worker_thread_);
mflodman3d7db262016-04-29 00:57:13 -07001447
Tommicae1f1d2021-05-31 10:51:09 +02001448 receive_stats_.AddReceivedRtcpBytes(static_cast<int>(packet.size()));
1449 bool rtcp_delivered = false;
1450 for (VideoReceiveStream2* stream : video_receive_streams_) {
1451 if (stream->DeliverRtcp(packet.cdata(), packet.size()))
1452 rtcp_delivered = true;
1453 }
mflodman3d7db262016-04-29 00:57:13 -07001454
Tommicae1f1d2021-05-31 10:51:09 +02001455 for (AudioReceiveStream* stream : audio_receive_streams_) {
1456 stream->DeliverRtcp(packet.cdata(), packet.size());
1457 rtcp_delivered = true;
1458 }
1459
1460 for (VideoSendStream* stream : video_send_streams_) {
1461 stream->DeliverRtcp(packet.cdata(), packet.size());
1462 rtcp_delivered = true;
1463 }
1464
1465 for (auto& kv : audio_send_ssrcs_) {
1466 kv.second->DeliverRtcp(packet.cdata(), packet.size());
1467 rtcp_delivered = true;
1468 }
1469
1470 if (rtcp_delivered) {
1471 event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>(
1472 rtc::MakeArrayView(packet.cdata(), packet.size())));
1473 }
1474 }));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001475}
1476
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001477PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001478 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001479 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001480 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
Tommi3f418cc2021-05-05 11:04:30 +02001481 RTC_DCHECK_NE(media_type, MediaType::ANY);
nissed44ce052017-02-06 02:23:00 -08001482
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001483 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001484 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001485 return DELIVERY_PACKET_ERROR;
1486
Niels Möller70082872018-08-07 11:03:12 +02001487 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001488 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001489 // Repair packet_time_us for clock resets by comparing a new read of
1490 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001491 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001492 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001493 }
Tommi2497a272021-05-05 12:33:00 +02001494 parsed_packet.set_arrival_time(Timestamp::Micros(packet_time_us));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001495 } else {
Tommi2497a272021-05-05 12:33:00 +02001496 parsed_packet.set_arrival_time(clock_->CurrentTime());
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001497 }
nissed44ce052017-02-06 02:23:00 -08001498
sprangc1abde72017-07-11 03:56:21 -07001499 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1500 // These are empty (zero length payload) RTP packets with an unsignaled
1501 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001502 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001503
1504 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1505 is_keep_alive_packet);
1506
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001507 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001508 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001509 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1510 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001511 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001512 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001513 // But deregistering in the |receive_rtp_config_| map is. So by not passing
1514 // the packet on to demuxing in this case, we prevent incoming packets to be
1515 // passed on via the demuxer to a receive stream which is being torned down.
nisse0f15f922017-06-21 01:05:22 -07001516 return DELIVERY_UNKNOWN_SSRC;
1517 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001518
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001519 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001520
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001521 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001522
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001523 // RateCounters expect input parameter as int, save it as int,
1524 // instead of converting each time it is passed to RateCounter::Add below.
1525 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001526 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001527 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001528 receive_stats_.AddReceivedAudioBytes(length,
1529 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001530 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001531 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse657bab22017-02-21 06:28:10 -08001532 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001533 }
nissee4bcd6d2017-05-16 04:47:04 -07001534 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001535 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001536 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Markus Handellc81afe32021-05-31 09:02:01 +02001537 receive_stats_.AddReceivedVideoBytes(length,
1538 parsed_packet.arrival_time());
Elad Alon4a87e1c2017-10-03 16:11:34 +02001539 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001540 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
nisse5c29a7a2017-02-16 06:52:32 -08001541 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001542 }
1543 }
1544 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001545}
1546
stefan68786d22015-09-08 05:36:15 -07001547PacketReceiver::DeliveryStatus Call::DeliverPacket(
1548 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001549 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001550 int64_t packet_time_us) {
Tommicae1f1d2021-05-31 10:51:09 +02001551 if (IsRtcp(packet.cdata(), packet.size())) {
1552 RTC_DCHECK_RUN_ON(network_thread_);
1553 DeliverRtcp(media_type, std::move(packet));
1554 return DELIVERY_OK;
1555 }
1556
Tommi0d4647d2020-05-26 19:35:16 +02001557 RTC_DCHECK_RUN_ON(worker_thread_);
Niels Möller70082872018-08-07 11:03:12 +02001558 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001559}
1560
nissed2ef3142017-05-11 08:00:58 -07001561void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001562 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
1563 // This method is called synchronously via |OnRtpPacket()| (see DeliverRtp)
1564 // on the same thread.
Tommi0d4647d2020-05-26 19:35:16 +02001565 RTC_DCHECK_RUN_ON(worker_thread_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001566 RtpPacketReceived parsed_packet;
1567 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001568 return;
1569
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001570 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001571
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001572 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001573 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001574 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1575 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001576 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001577 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001578 // But deregistering in the |receive_rtp_config_| map is.
brandtrcaea68f2017-08-23 00:55:17 -07001579 // So by not passing the packet on to demuxing in this case, we prevent
1580 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001581 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001582 return;
1583 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001584 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001585
1586 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001587 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001588 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001589}
1590
Tommi948e40c2021-05-31 12:39:57 +02001591// RTC_RUN_ON(worker_thread_)
nissed44ce052017-02-06 02:23:00 -08001592void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1593 MediaType media_type) {
1594 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001595 bool use_send_side_bwe =
1596 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001597
brandtrb29e6522016-12-21 06:37:18 -08001598 RTPHeader header;
1599 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001600
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001601 ReceivedPacket packet_msg;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001602 packet_msg.size = DataSize::Bytes(packet.payload_size());
Tommi2497a272021-05-05 12:33:00 +02001603 packet_msg.receive_time = packet.arrival_time();
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001604 if (header.extension.hasAbsoluteSendTime) {
1605 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1606 }
Tommi948e40c2021-05-31 12:39:57 +02001607 transport_send_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001608
nisse4709e892017-02-07 01:18:43 -08001609 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001610 // Inconsistent configuration of send side BWE. Do nothing.
1611 // TODO(nisse): Without this check, we may produce RTCP feedback
1612 // packets even when not negotiated. But it would be cleaner to
1613 // move the check down to RTCPSender::SendFeedbackPacket, which
1614 // would also help the PacketRouter to select an appropriate rtp
1615 // module in the case that some, but not all, have RTCP feedback
1616 // enabled.
1617 return;
1618 }
1619 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001620 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001621 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001622 receive_side_cc_.OnReceivedPacket(
Tommi2497a272021-05-05 12:33:00 +02001623 packet.arrival_time().ms(),
1624 packet.payload_size() + packet.padding_size(), header);
nissed44ce052017-02-06 02:23:00 -08001625 }
brandtrb29e6522016-12-21 06:37:18 -08001626}
1627
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001628} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001629
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001630} // namespace webrtc