blob: e6cf1c31dd00fa026602c889a4f4089d4facacb8 [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
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020022#include "absl/types/optional.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020023#include "api/rtc_event_log/rtc_event_log.h"
Artem Titovd15a5752021-02-10 14:31:24 +010024#include "api/sequence_checker.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020025#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "audio/audio_receive_stream.h"
27#include "audio/audio_send_stream.h"
28#include "audio/audio_state.h"
Henrik Boström29444c62020-07-01 15:48:46 +020029#include "call/adaptation/broadcast_resource_listener.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "call/bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010032#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "call/rtp_stream_receiver_controller.h"
34#include "call/rtp_transport_controller_send.h"
Mirko Bonadeib9857482020-12-14 15:28:43 +010035#include "call/version.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020036#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020037#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
38#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
39#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
40#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Elad Alon99a81b62017-09-21 10:25:29 +020041#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
43#include "modules/rtp_rtcp/include/flexfec_receiver.h"
44#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "modules/rtp_rtcp/source/byte_io.h"
46#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Tommi25eb47c2019-08-29 16:39:05 +020047#include "modules/rtp_rtcp/source/rtp_utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010049#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080051#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/location.h"
53#include "rtc_base/logging.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020054#include "rtc_base/strings/string_builder.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010055#include "rtc_base/system/no_unique_address.h"
Tommi0d4647d2020-05-26 19:35:16 +020056#include "rtc_base/task_utils/pending_task_safety_flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080058#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/trace_event.h"
60#include "system_wrappers/include/clock.h"
61#include "system_wrappers/include/cpu_info.h"
Jonas Oreland6d835922019-03-18 10:59:40 +010062#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "system_wrappers/include/metrics.h"
Tommi822a8742020-05-11 00:42:30 +020064#include "video/call_stats2.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "video/send_delay_stats.h"
66#include "video/stats_counter.h"
Tommi553c8692020-05-05 15:35:45 +020067#include "video/video_receive_stream2.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020068#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000069
70namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000071
nisse4709e892017-02-07 01:18:43 -080072namespace {
Johannes Kronf59666b2019-04-08 12:57:06 +020073bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010074 for (const auto& extension : extensions) {
75 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
Johannes Kronf59666b2019-04-08 12:57:06 +020076 return false;
Johannes Kron7ff164e2019-02-07 12:50:18 +010077 }
Johannes Kronf59666b2019-04-08 12:57:06 +020078 return true;
Johannes Kron7ff164e2019-02-07 12:50:18 +010079}
80
nisse4709e892017-02-07 01:18:43 -080081// TODO(nisse): This really begs for a shared context struct.
82bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
83 bool transport_cc) {
84 if (!transport_cc)
85 return false;
86 for (const auto& extension : extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010087 if (extension.uri == RtpExtension::kTransportSequenceNumberUri ||
88 extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
nisse4709e892017-02-07 01:18:43 -080089 return true;
90 }
91 return false;
92}
93
94bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
95 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
96}
97
98bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
99 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
100}
101
102bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
103 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
104}
105
nisse26e3abb2017-08-25 04:44:25 -0700106const int* FindKeyByValue(const std::map<int, int>& m, int v) {
107 for (const auto& kv : m) {
108 if (kv.second == v)
109 return &kv.first;
110 }
111 return nullptr;
112}
113
eladalon8ec568a2017-09-08 06:15:52 -0700114std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700115 const VideoReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200116 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700117 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
118 rtclog_config->local_ssrc = config.rtp.local_ssrc;
119 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
120 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
eladalon8ec568a2017-09-08 06:15:52 -0700121 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700122
123 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700124 const int* search =
125 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200126 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200127 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700128 }
129 return rtclog_config;
130}
131
eladalon8ec568a2017-09-08 06:15:52 -0700132std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700133 const VideoSendStream::Config& config,
134 size_t ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200135 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700136 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700137 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700138 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700139 }
eladalon8ec568a2017-09-08 06:15:52 -0700140 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
141 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700142
Niels Möller259a4972018-04-05 15:36:51 +0200143 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
144 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700145 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700146 return rtclog_config;
147}
148
eladalon8ec568a2017-09-08 06:15:52 -0700149std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700150 const AudioReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200151 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700152 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
153 rtclog_config->local_ssrc = config.rtp.local_ssrc;
154 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700155 return rtclog_config;
156}
157
Tommi25eb47c2019-08-29 16:39:05 +0200158bool IsRtcp(const uint8_t* packet, size_t length) {
159 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
160 return rtp_parser.RTCP();
161}
162
Tommi822a8742020-05-11 00:42:30 +0200163TaskQueueBase* GetCurrentTaskQueueOrThread() {
164 TaskQueueBase* current = TaskQueueBase::Current();
165 if (!current)
166 current = rtc::ThreadManager::Instance()->CurrentThread();
167 return current;
168}
169
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100170// Called from the destructor of Call to report the collected send histograms.
171void UpdateSendHistograms(Timestamp now,
172 Timestamp first_sent_packet,
173 AvgCounter& estimated_send_bitrate_kbps_counter,
174 AvgCounter& pacer_bitrate_kbps_counter) {
175 TimeDelta elapsed = now - first_sent_packet;
176 if (elapsed.seconds() < metrics::kMinRunTimeInSeconds)
177 return;
178
179 const int kMinRequiredPeriodicSamples = 5;
180 AggregatedStats send_bitrate_stats =
181 estimated_send_bitrate_kbps_counter.ProcessAndGetStats();
182 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
183 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
184 send_bitrate_stats.average);
185 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
186 << send_bitrate_stats.ToString();
187 }
188 AggregatedStats pacer_bitrate_stats =
189 pacer_bitrate_kbps_counter.ProcessAndGetStats();
190 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
191 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
192 pacer_bitrate_stats.average);
193 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
194 << pacer_bitrate_stats.ToString();
195 }
196}
197
nisse4709e892017-02-07 01:18:43 -0800198} // namespace
199
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000200namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000201
Henrik Boström29444c62020-07-01 15:48:46 +0200202// Wraps an injected resource in a BroadcastResourceListener and handles adding
203// and removing adapter resources to individual VideoSendStreams.
204class ResourceVideoSendStreamForwarder {
205 public:
206 ResourceVideoSendStreamForwarder(
207 rtc::scoped_refptr<webrtc::Resource> resource)
208 : broadcast_resource_listener_(resource) {
209 broadcast_resource_listener_.StartListening();
210 }
211 ~ResourceVideoSendStreamForwarder() {
212 RTC_DCHECK(adapter_resources_.empty());
213 broadcast_resource_listener_.StopListening();
214 }
215
216 rtc::scoped_refptr<webrtc::Resource> Resource() const {
217 return broadcast_resource_listener_.SourceResource();
218 }
219
220 void OnCreateVideoSendStream(VideoSendStream* video_send_stream) {
221 RTC_DCHECK(adapter_resources_.find(video_send_stream) ==
222 adapter_resources_.end());
223 auto adapter_resource =
224 broadcast_resource_listener_.CreateAdapterResource();
225 video_send_stream->AddAdaptationResource(adapter_resource);
226 adapter_resources_.insert(
227 std::make_pair(video_send_stream, adapter_resource));
228 }
229
230 void OnDestroyVideoSendStream(VideoSendStream* video_send_stream) {
231 auto it = adapter_resources_.find(video_send_stream);
232 RTC_DCHECK(it != adapter_resources_.end());
233 broadcast_resource_listener_.RemoveAdapterResource(it->second);
234 adapter_resources_.erase(it);
235 }
236
237 private:
238 BroadcastResourceListener broadcast_resource_listener_;
239 std::map<VideoSendStream*, rtc::scoped_refptr<webrtc::Resource>>
240 adapter_resources_;
241};
242
Sebastian Janssone6256052018-05-04 14:08:15 +0200243class Call final : public webrtc::Call,
244 public PacketReceiver,
245 public RecoveredPacketReceiver,
246 public TargetTransferRateObserver,
247 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000248 public:
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100249 Call(Clock* clock,
250 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100251 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
Tommi25c77c12020-05-25 17:44:55 +0200252 rtc::scoped_refptr<SharedModuleThread> module_process_thread,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100253 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200254 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000255
brandtr25445d32016-10-23 23:37:14 -0700256 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000257 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000258
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200259 webrtc::AudioSendStream* CreateAudioSendStream(
260 const webrtc::AudioSendStream::Config& config) override;
261 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
262
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200263 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
264 const webrtc::AudioReceiveStream::Config& config) override;
265 void DestroyAudioReceiveStream(
266 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000267
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200268 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700269 webrtc::VideoSendStream::Config config,
270 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100271 webrtc::VideoSendStream* CreateVideoSendStream(
272 webrtc::VideoSendStream::Config config,
273 VideoEncoderConfig encoder_config,
274 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000275 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000276
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200277 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200278 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000279 void DestroyVideoReceiveStream(
280 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000281
brandtr7250b392016-12-19 01:13:46 -0800282 FlexfecReceiveStream* CreateFlexfecReceiveStream(
283 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700284 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800285 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700286
Henrik Boströmf4a99912020-06-11 12:07:14 +0200287 void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
288
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100289 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
290
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000291 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000292
Erik Språngceb44952020-09-22 11:36:35 +0200293 const WebRtcKeyValueConfig& trials() const override;
294
Tomas Gunnarssone984aa22021-04-19 09:21:06 +0200295 TaskQueueBase* network_thread() const override;
296 TaskQueueBase* worker_thread() const override;
297
brandtr25445d32016-10-23 23:37:14 -0700298 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700299 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100300 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200301 int64_t packet_time_us) override;
Tomas Gunnarssona722d2a2021-01-19 09:00:18 +0100302 void DeliverPacketAsync(MediaType media_type,
303 rtc::CopyOnWriteBuffer packet,
304 int64_t packet_time_us,
305 PacketCallback callback) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000306
brandtr4e523862016-10-18 23:50:45 -0700307 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700308 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700309
skvlad7a43d252016-03-22 15:32:27 -0700310 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000311
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200312 void OnAudioTransportOverheadChanged(
313 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800314
stefanc1aeaf02015-10-15 07:26:07 -0700315 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
316
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100317 // Implements TargetTransferRateObserver,
318 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100319 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800320
perkj71ee44c2016-06-15 00:47:53 -0700321 // Implements BitrateAllocator::LimitObserver.
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200322 void OnAllocationLimitsChanged(BitrateAllocationLimits limits) override;
perkj71ee44c2016-06-15 00:47:53 -0700323
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700324 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
325
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000326 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200327 DeliveryStatus DeliverRtcp(MediaType media_type,
328 const uint8_t* packet,
Tommi31001a62020-05-26 11:38:36 +0200329 size_t length)
Tommi0d4647d2020-05-26 19:35:16 +0200330 RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
stefan68786d22015-09-08 05:36:15 -0700331 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100332 rtc::CopyOnWriteBuffer packet,
Tommi31001a62020-05-26 11:38:36 +0200333 int64_t packet_time_us)
Tommi0d4647d2020-05-26 19:35:16 +0200334 RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
pbos8fc7fa72015-07-15 08:02:58 -0700335 void ConfigureSync(const std::string& sync_group)
Tommi0d4647d2020-05-26 19:35:16 +0200336 RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
pbos8fc7fa72015-07-15 08:02:58 -0700337
nissed44ce052017-02-06 02:23:00 -0800338 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
339 MediaType media_type)
Tommi0d4647d2020-05-26 19:35:16 +0200340 RTC_SHARED_LOCKS_REQUIRED(worker_thread_);
nissed44ce052017-02-06 02:23:00 -0800341
stefan18adf0a2015-11-17 06:24:56 -0800342 void UpdateReceiveHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700343 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800344
Erik Språng7703f232020-09-14 11:03:13 +0200345 // Ensure that necessary process threads are started, and any required
346 // callbacks have been registered.
347 void EnsureStarted() RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
Niels Möller46879152019-01-07 15:54:47 +0100348
Tommi8edfe6e2020-05-28 09:01:41 +0200349 rtc::TaskQueue* send_transport_queue() const {
Tommi48b48e52019-08-09 11:42:32 +0200350 return transport_send_ptr_->GetWorkerQueue();
351 }
352
Peter Boströmd3c94472015-12-09 11:20:58 +0100353 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100354 TaskQueueFactory* const task_queue_factory_;
Tommi0d4647d2020-05-26 19:35:16 +0200355 TaskQueueBase* const worker_thread_;
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100356 TaskQueueBase* const network_thread_;
stefan91d92602015-11-11 10:13:02 -0800357
Peter Boström45553ae2015-05-08 13:54:38 +0200358 const int num_cpu_cores_;
Tommi25c77c12020-05-25 17:44:55 +0200359 const rtc::scoped_refptr<SharedModuleThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800360 const std::unique_ptr<CallStats> call_stats_;
361 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000362 Call::Config config_;
363
skvlad7a43d252016-03-22 15:32:27 -0700364 NetworkState audio_network_state_;
365 NetworkState video_network_state_;
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100366 // TODO(bugs.webrtc.org/11993): Move aggregate_network_up_ over to the
367 // network thread.
Tommi0d4647d2020-05-26 19:35:16 +0200368 bool aggregate_network_up_ RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000369
brandtr25445d32016-10-23 23:37:14 -0700370 // Audio, Video, and FlexFEC receive streams are owned by the client that
371 // creates them.
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100372 // TODO(bugs.webrtc.org/11993): Move audio_receive_streams_,
373 // video_receive_streams_ and sync_stream_mapping_ over to the network thread.
nissee4bcd6d2017-05-16 04:47:04 -0700374 std::set<AudioReceiveStream*> audio_receive_streams_
Tommi0d4647d2020-05-26 19:35:16 +0200375 RTC_GUARDED_BY(worker_thread_);
Tommi553c8692020-05-05 15:35:45 +0200376 std::set<VideoReceiveStream2*> video_receive_streams_
Tommi0d4647d2020-05-26 19:35:16 +0200377 RTC_GUARDED_BY(worker_thread_);
pbos8fc7fa72015-07-15 08:02:58 -0700378 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
Tommi0d4647d2020-05-26 19:35:16 +0200379 RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000380
nisse0f15f922017-06-21 01:05:22 -0700381 // TODO(nisse): Should eventually be injected at creation,
382 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700383 RtpStreamReceiverController audio_receiver_controller_;
384 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700385
nissed44ce052017-02-06 02:23:00 -0800386 // This extra map is used for receive processing which is
387 // independent of media type.
388
389 // TODO(nisse): In the RTP transport refactoring, we should have a
390 // single mapping from ssrc to a more abstract receive stream, with
391 // accessor methods for all configuration we need at this level.
392 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100393 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
394 : extensions(config.rtp.extensions),
395 use_send_side_bwe(UseSendSideBwe(config)) {}
396 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
397 : extensions(config.rtp.extensions),
398 use_send_side_bwe(UseSendSideBwe(config)) {}
399 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
400 : extensions(config.rtp_header_extensions),
401 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800402
403 // Registered RTP header extensions for each stream. Note that RTP header
404 // extensions are negotiated per track ("m= line") in the SDP, but we have
405 // no notion of tracks at the Call level. We therefore store the RTP header
406 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100407 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800408 // Set if both RTP extension the RTCP feedback message needed for
409 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100410 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800411 };
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100412
413 // TODO(bugs.webrtc.org/11993): Move receive_rtp_config_ over to the
414 // network thread.
nissed44ce052017-02-06 02:23:00 -0800415 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
Tommi0d4647d2020-05-26 19:35:16 +0200416 RTC_GUARDED_BY(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -0800417
solenbergc7a8b082015-10-16 14:35:07 -0700418 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700419 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
Tommi0d4647d2020-05-26 19:35:16 +0200420 RTC_GUARDED_BY(worker_thread_);
danilchapa37de392017-09-09 04:17:22 -0700421 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
Tommi0d4647d2020-05-26 19:35:16 +0200422 RTC_GUARDED_BY(worker_thread_);
423 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(worker_thread_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000424
Henrik Boström29444c62020-07-01 15:48:46 +0200425 // Each forwarder wraps an adaptation resource that was added to the call.
426 std::vector<std::unique_ptr<ResourceVideoSendStreamForwarder>>
427 adaptation_resource_forwarders_ RTC_GUARDED_BY(worker_thread_);
Henrik Boströmf4a99912020-06-11 12:07:14 +0200428
ossuc3d4b482017-05-23 06:07:11 -0700429 using RtpStateMap = std::map<uint32_t, RtpState>;
Tommi0d4647d2020-05-26 19:35:16 +0200430 RtpStateMap suspended_audio_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
431 RtpStateMap suspended_video_send_ssrcs_ RTC_GUARDED_BY(worker_thread_);
ossuc3d4b482017-05-23 06:07:11 -0700432
Åsa Persson4bece9a2017-10-06 10:04:04 +0200433 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
434 RtpPayloadStateMap suspended_video_payload_states_
Tommi0d4647d2020-05-26 19:35:16 +0200435 RTC_GUARDED_BY(worker_thread_);
Åsa Persson4bece9a2017-10-06 10:04:04 +0200436
skvlad11a9cbf2016-10-07 11:53:05 -0700437 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700438
stefan18adf0a2015-11-17 06:24:56 -0800439 // The following members are only accessed (exclusively) from one thread and
440 // from the destructor, and therefore doesn't need any explicit
441 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700442 RateCounter received_bytes_per_second_counter_;
443 RateCounter received_audio_bytes_per_second_counter_;
444 RateCounter received_video_bytes_per_second_counter_;
445 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200446 absl::optional<int64_t> first_received_rtp_audio_ms_;
447 absl::optional<int64_t> last_received_rtp_audio_ms_;
448 absl::optional<int64_t> first_received_rtp_video_ms_;
449 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 10:13:02 -0800450
Tommi0d4647d2020-05-26 19:35:16 +0200451 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(worker_thread_);
stefan18adf0a2015-11-17 06:24:56 -0800452 // TODO(holmer): Remove this lock once BitrateController no longer calls
453 // OnNetworkChanged from multiple threads.
Tommi0d4647d2020-05-26 19:35:16 +0200454 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(worker_thread_);
455 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(worker_thread_);
danilchapa37de392017-09-09 04:17:22 -0700456 AvgCounter estimated_send_bitrate_kbps_counter_
Tommi0d4647d2020-05-26 19:35:16 +0200457 RTC_GUARDED_BY(worker_thread_);
458 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(worker_thread_);
stefan18adf0a2015-11-17 06:24:56 -0800459
nisse559af382017-03-21 06:41:12 -0700460 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100461
462 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
463
asapersson35151f32016-05-02 23:44:01 -0700464 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700465 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800466
Tommi0d4647d2020-05-26 19:35:16 +0200467 // Note that |task_safety_| needs to be at a greater scope than the task queue
468 // owned by |transport_send_| since calls might arrive on the network thread
469 // while Call is being deleted and the task queue is being torn down.
470 ScopedTaskSafety task_safety_;
471
Sebastian Janssone6256052018-05-04 14:08:15 +0200472 // Caches transport_send_.get(), to avoid racing with destructor.
473 // Note that this is declared before transport_send_ to ensure that it is not
474 // invalidated until no more tasks can be running on the transport_send_ task
475 // queue.
Tommi78a71382019-08-08 12:27:53 +0200476 RtpTransportControllerSendInterface* const transport_send_ptr_;
Sebastian Janssone6256052018-05-04 14:08:15 +0200477 // Declared last since it will issue callbacks from a task queue. Declaring it
478 // last ensures that it is destroyed first and any running tasks are finished.
479 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800480
Erik Språng7703f232020-09-14 11:03:13 +0200481 bool is_started_ RTC_GUARDED_BY(worker_thread_) = false;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800482
henrikg3c089d72015-09-16 05:37:44 -0700483 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000484};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000485} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000486
asapersson2e5cfcd2016-08-11 08:41:18 -0700487std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200488 char buf[1024];
489 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700490 ss << "Call stats: " << time_ms << ", {";
491 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
492 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
493 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
494 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
495 ss << "rtt_ms: " << rtt_ms;
496 ss << '}';
497 return ss.str();
498}
499
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000500Call* Call::Create(const Call::Config& config) {
Tommi25c77c12020-05-25 17:44:55 +0200501 rtc::scoped_refptr<SharedModuleThread> call_thread =
Per Kjellander4c50e702020-06-30 14:39:43 +0200502 SharedModuleThread::Create(ProcessThread::Create("ModuleProcessThread"),
503 nullptr);
Tommi25c77c12020-05-25 17:44:55 +0200504 return Create(config, std::move(call_thread));
505}
506
507Call* Call::Create(const Call::Config& config,
508 rtc::scoped_refptr<SharedModuleThread> call_thread) {
509 return Create(config, Clock::GetRealTimeClock(), std::move(call_thread),
Erik Språng6950b302019-08-16 12:54:08 +0200510 ProcessThread::Create("PacerThread"));
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100511}
512
513Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100514 Clock* clock,
Tommi25c77c12020-05-25 17:44:55 +0200515 rtc::scoped_refptr<SharedModuleThread> call_thread,
Danil Chapovalov359fe332019-04-01 10:46:36 +0200516 std::unique_ptr<ProcessThread> pacer_thread) {
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200517 RTC_DCHECK(config.task_queue_factory);
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100518 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100519 clock, config,
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200520 std::make_unique<RtpTransportControllerSend>(
Ying Wang0810a7c2019-04-10 13:48:24 +0200521 clock, config.event_log, config.network_state_predictor_factory,
522 config.network_controller_factory, config.bitrate_config,
Erik Språng662678d2019-11-15 17:18:52 +0100523 std::move(pacer_thread), config.task_queue_factory, config.trials),
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200524 std::move(call_thread), config.task_queue_factory);
zstein7cb69d52017-05-08 11:52:38 -0700525}
526
Tommi25c77c12020-05-25 17:44:55 +0200527class SharedModuleThread::Impl {
528 public:
529 Impl(std::unique_ptr<ProcessThread> process_thread,
530 std::function<void()> on_one_ref_remaining)
531 : module_thread_(std::move(process_thread)),
532 on_one_ref_remaining_(std::move(on_one_ref_remaining)) {}
533
534 void EnsureStarted() {
535 RTC_DCHECK_RUN_ON(&sequence_checker_);
536 if (started_)
537 return;
538 started_ = true;
539 module_thread_->Start();
540 }
541
542 ProcessThread* process_thread() {
543 RTC_DCHECK_RUN_ON(&sequence_checker_);
544 return module_thread_.get();
545 }
546
547 void AddRef() const {
548 RTC_DCHECK_RUN_ON(&sequence_checker_);
549 ++ref_count_;
550 }
551
552 rtc::RefCountReleaseStatus Release() const {
553 RTC_DCHECK_RUN_ON(&sequence_checker_);
554 --ref_count_;
555
556 if (ref_count_ == 0) {
557 module_thread_->Stop();
558 return rtc::RefCountReleaseStatus::kDroppedLastRef;
559 }
560
561 if (ref_count_ == 1 && on_one_ref_remaining_) {
562 auto moved_fn = std::move(on_one_ref_remaining_);
563 // NOTE: after this function returns, chances are that |this| has been
564 // deleted - do not touch any member variables.
565 // If the owner of the last reference implements a lambda that releases
566 // that last reference inside of the callback (which is legal according
567 // to this implementation), we will recursively enter Release() above,
568 // call Stop() and release the last reference.
569 moved_fn();
570 }
571
572 return rtc::RefCountReleaseStatus::kOtherRefsRemained;
573 }
574
575 private:
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100576 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
Tommi25c77c12020-05-25 17:44:55 +0200577 mutable int ref_count_ RTC_GUARDED_BY(sequence_checker_) = 0;
578 std::unique_ptr<ProcessThread> const module_thread_;
579 std::function<void()> const on_one_ref_remaining_;
580 bool started_ = false;
581};
582
583SharedModuleThread::SharedModuleThread(
584 std::unique_ptr<ProcessThread> process_thread,
585 std::function<void()> on_one_ref_remaining)
586 : impl_(std::make_unique<Impl>(std::move(process_thread),
587 std::move(on_one_ref_remaining))) {}
588
589SharedModuleThread::~SharedModuleThread() = default;
590
591// static
Tommi25c77c12020-05-25 17:44:55 +0200592
593rtc::scoped_refptr<SharedModuleThread> SharedModuleThread::Create(
594 std::unique_ptr<ProcessThread> process_thread,
595 std::function<void()> on_one_ref_remaining) {
596 return new SharedModuleThread(std::move(process_thread),
597 std::move(on_one_ref_remaining));
598}
599
600void SharedModuleThread::EnsureStarted() {
601 impl_->EnsureStarted();
602}
603
604ProcessThread* SharedModuleThread::process_thread() {
605 return impl_->process_thread();
606}
607
608void SharedModuleThread::AddRef() const {
609 impl_->AddRef();
610}
611
612rtc::RefCountReleaseStatus SharedModuleThread::Release() const {
613 auto ret = impl_->Release();
614 if (ret == rtc::RefCountReleaseStatus::kDroppedLastRef)
615 delete this;
616 return ret;
617}
618
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100619// This method here to avoid subclasses has to implement this method.
620// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
621// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100622VideoSendStream* Call::CreateVideoSendStream(
623 VideoSendStream::Config config,
624 VideoEncoderConfig encoder_config,
625 std::unique_ptr<FecController> fec_controller) {
626 return nullptr;
627}
628
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000629namespace internal {
630
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100631Call::Call(Clock* clock,
632 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100633 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
Tommi25c77c12020-05-25 17:44:55 +0200634 rtc::scoped_refptr<SharedModuleThread> module_process_thread,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100635 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100636 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100637 task_queue_factory_(task_queue_factory),
Tommi0d4647d2020-05-26 19:35:16 +0200638 worker_thread_(GetCurrentTaskQueueOrThread()),
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100639 // If |network_task_queue_| was set to nullptr, network related calls
640 // must be made on |worker_thread_| (i.e. they're one and the same).
641 network_thread_(config.network_task_queue_ ? config.network_task_queue_
642 : worker_thread_),
stefan91d92602015-11-11 10:13:02 -0800643 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100644 module_process_thread_(std::move(module_process_thread)),
Tommi0d4647d2020-05-26 19:35:16 +0200645 call_stats_(new CallStats(clock_, worker_thread_)),
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200646 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200647 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800648 audio_network_state_(kNetworkDown),
649 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100650 aggregate_network_up_(false),
skvlad11a9cbf2016-10-07 11:53:05 -0700651 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700652 received_bytes_per_second_counter_(clock_, nullptr, true),
653 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
654 received_video_bytes_per_second_counter_(clock_, nullptr, true),
655 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100656 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700657 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700658 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700659 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
660 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700661 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100662 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700663 video_send_delay_stats_(new SendDelayStats(clock_)),
Tommi78a71382019-08-08 12:27:53 +0200664 start_ms_(clock_->TimeInMilliseconds()),
665 transport_send_ptr_(transport_send.get()),
666 transport_send_(std::move(transport_send)) {
skvlad11a9cbf2016-10-07 11:53:05 -0700667 RTC_DCHECK(config.event_log != nullptr);
Erik Språng17f82cf2019-12-04 11:10:43 +0100668 RTC_DCHECK(config.trials != nullptr);
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +0100669 RTC_DCHECK(network_thread_);
Tommi0d4647d2020-05-26 19:35:16 +0200670 RTC_DCHECK(worker_thread_->IsCurrent());
Tommi48b48e52019-08-09 11:42:32 +0200671
Mirko Bonadeib9857482020-12-14 15:28:43 +0100672 // Do not remove this call; it is here to convince the compiler that the
673 // WebRTC source timestamp string needs to be in the final binary.
674 LoadWebRTCVersionInRegister();
675
Tommi48b48e52019-08-09 11:42:32 +0200676 call_stats_->RegisterStatsObserver(&receive_side_cc_);
677
Tommi25c77c12020-05-25 17:44:55 +0200678 module_process_thread_->process_thread()->RegisterModule(
Tommi48b48e52019-08-09 11:42:32 +0200679 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
Tommi25c77c12020-05-25 17:44:55 +0200680 module_process_thread_->process_thread()->RegisterModule(&receive_side_cc_,
681 RTC_FROM_HERE);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000682}
683
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000684Call::~Call() {
Tommi0d4647d2020-05-26 19:35:16 +0200685 RTC_DCHECK_RUN_ON(worker_thread_);
perkj26091b12016-09-01 01:17:40 -0700686
solenbergc7a8b082015-10-16 14:35:07 -0700687 RTC_CHECK(audio_send_ssrcs_.empty());
688 RTC_CHECK(video_send_ssrcs_.empty());
689 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700690 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700691 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000692
Tommi25c77c12020-05-25 17:44:55 +0200693 module_process_thread_->process_thread()->DeRegisterModule(
Tommi78a71382019-08-08 12:27:53 +0200694 receive_side_cc_.GetRemoteBitrateEstimator(true));
Tommi25c77c12020-05-25 17:44:55 +0200695 module_process_thread_->process_thread()->DeRegisterModule(&receive_side_cc_);
Tommi78a71382019-08-08 12:27:53 +0200696 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
sprang6d6122b2016-07-13 06:37:09 -0700697
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100698 absl::optional<Timestamp> first_sent_packet_time =
Erik Språng425d6aa2019-07-29 16:38:27 +0200699 transport_send_->GetFirstPacketTime();
Tommi48b48e52019-08-09 11:42:32 +0200700
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100701 Timestamp now = clock_->CurrentTime();
702
sprang6d6122b2016-07-13 06:37:09 -0700703 // Only update histograms after process threads have been shut down, so that
704 // they won't try to concurrently update stats.
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100705 if (first_sent_packet_time) {
706 UpdateSendHistograms(now, *first_sent_packet_time,
707 estimated_send_bitrate_kbps_counter_,
708 pacer_bitrate_kbps_counter_);
perkj26091b12016-09-01 01:17:40 -0700709 }
Tommi48b48e52019-08-09 11:42:32 +0200710
sprang6d6122b2016-07-13 06:37:09 -0700711 UpdateReceiveHistograms();
Tomas Gunnarsson9915db32021-02-18 08:35:44 +0100712
713 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.LifetimeInSeconds",
714 (now.ms() - start_ms_) / 1000);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000715}
716
Erik Språng7703f232020-09-14 11:03:13 +0200717void Call::EnsureStarted() {
718 if (is_started_) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800719 return;
Erik Språng7703f232020-09-14 11:03:13 +0200720 }
721 is_started_ = true;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800722
Etienne Pierre-Doraycc474372021-02-10 15:51:36 -0500723 call_stats_->EnsureStarted();
724
Tommi48b48e52019-08-09 11:42:32 +0200725 // This call seems to kick off a number of things, so probably better left
726 // off being kicked off on request rather than in the ctor.
Tommi78a71382019-08-08 12:27:53 +0200727 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800728
Tommi25c77c12020-05-25 17:44:55 +0200729 module_process_thread_->EnsureStarted();
Erik Språng7703f232020-09-14 11:03:13 +0200730 transport_send_ptr_->EnsureStarted();
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700731}
732
733void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
Tommi0d4647d2020-05-26 19:35:16 +0200734 RTC_DCHECK_RUN_ON(worker_thread_);
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700735 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800736}
737
stefan18adf0a2015-11-17 06:24:56 -0800738void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700739 if (first_received_rtp_audio_ms_) {
740 RTC_HISTOGRAM_COUNTS_100000(
741 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
742 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
743 }
744 if (first_received_rtp_video_ms_) {
745 RTC_HISTOGRAM_COUNTS_100000(
746 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
747 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
748 }
asapersson250fd972016-09-08 00:07:21 -0700749 const int kMinRequiredPeriodicSamples = 5;
750 AggregatedStats video_bytes_per_sec =
751 received_video_bytes_per_second_counter_.GetStats();
752 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700753 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
754 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100755 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
756 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800757 }
asapersson250fd972016-09-08 00:07:21 -0700758 AggregatedStats audio_bytes_per_sec =
759 received_audio_bytes_per_second_counter_.GetStats();
760 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700761 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
762 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100763 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
764 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800765 }
asapersson250fd972016-09-08 00:07:21 -0700766 AggregatedStats rtcp_bytes_per_sec =
767 received_rtcp_bytes_per_second_counter_.GetStats();
768 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700769 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
770 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100771 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
772 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800773 }
asapersson250fd972016-09-08 00:07:21 -0700774 AggregatedStats recv_bytes_per_sec =
775 received_bytes_per_second_counter_.GetStats();
776 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700777 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
778 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100779 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
780 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700781 }
stefan91d92602015-11-11 10:13:02 -0800782}
783
solenberg5a289392015-10-19 03:39:20 -0700784PacketReceiver* Call::Receiver() {
solenberg5a289392015-10-19 03:39:20 -0700785 return this;
786}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000787
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200788webrtc::AudioSendStream* Call::CreateAudioSendStream(
789 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700790 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200791 RTC_DCHECK_RUN_ON(worker_thread_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800792
Erik Språng7703f232020-09-14 11:03:13 +0200793 EnsureStarted();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800794
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100795 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
796 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200797 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700798 {
799 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
800 if (iter != suspended_audio_send_ssrcs_.end()) {
801 suspended_rtp_state.emplace(iter->second);
802 }
803 }
804
Tommi822a8742020-05-11 00:42:30 +0200805 AudioSendStream* send_stream = new AudioSendStream(
806 clock_, config, config_.audio_state, task_queue_factory_,
Tommi25c77c12020-05-25 17:44:55 +0200807 module_process_thread_->process_thread(), transport_send_ptr_,
Tommi822a8742020-05-11 00:42:30 +0200808 bitrate_allocator_.get(), event_log_, call_stats_->AsRtcpRttStats(),
809 suspended_rtp_state);
Tommi0d4647d2020-05-26 19:35:16 +0200810 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
811 audio_send_ssrcs_.end());
812 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
Tommi31001a62020-05-26 11:38:36 +0200813
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100814 // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
815 // UpdateAggregateNetworkState asynchronously on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200816 for (AudioReceiveStream* stream : audio_receive_streams_) {
817 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
818 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800819 }
820 }
Tommi31001a62020-05-26 11:38:36 +0200821
skvlad7a43d252016-03-22 15:32:27 -0700822 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100823
solenbergc7a8b082015-10-16 14:35:07 -0700824 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200825}
826
827void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700828 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200829 RTC_DCHECK_RUN_ON(worker_thread_);
solenbergc7a8b082015-10-16 14:35:07 -0700830 RTC_DCHECK(send_stream != nullptr);
831
832 send_stream->Stop();
833
eladalonabbc4302017-07-26 02:09:44 -0700834 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700835 webrtc::internal::AudioSendStream* audio_send_stream =
836 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700837 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
Tommi0d4647d2020-05-26 19:35:16 +0200838
839 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
840 RTC_DCHECK_EQ(1, num_deleted);
Tommi31001a62020-05-26 11:38:36 +0200841
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100842 // TODO(bugs.webrtc.org/11993): call AssociateSendStream and
843 // UpdateAggregateNetworkState asynchronously on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200844 for (AudioReceiveStream* stream : audio_receive_streams_) {
845 if (stream->config().rtp.local_ssrc == ssrc) {
846 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800847 }
solenbergc7a8b082015-10-16 14:35:07 -0700848 }
Tommi31001a62020-05-26 11:38:36 +0200849
skvlad7a43d252016-03-22 15:32:27 -0700850 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100851
eladalonabbc4302017-07-26 02:09:44 -0700852 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200853}
854
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200855webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
856 const webrtc::AudioReceiveStream::Config& config) {
857 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +0200858 RTC_DCHECK_RUN_ON(worker_thread_);
Erik Språng7703f232020-09-14 11:03:13 +0200859 EnsureStarted();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200860 event_log_->Log(std::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200861 CreateRtcLogStreamConfig(config)));
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100862
863 // TODO(bugs.webrtc.org/11993): Move the registration between |receive_stream|
864 // and |audio_receiver_controller_| out of AudioReceiveStream construction and
865 // set it up asynchronously on the network thread (the registration and
866 // |audio_receiver_controller_| need to live on the network thread).
nisse0f15f922017-06-21 01:05:22 -0700867 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100868 clock_, &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Tommi25c77c12020-05-25 17:44:55 +0200869 module_process_thread_->process_thread(), config_.neteq_factory, config,
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +0100870 config_.audio_state, event_log_);
nissed44ce052017-02-06 02:23:00 -0800871
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100872 // TODO(bugs.webrtc.org/11993): Update the below on the network thread.
873 // We could possibly set up the audio_receiver_controller_ association up
874 // as part of the async setup.
Tommi31001a62020-05-26 11:38:36 +0200875 receive_rtp_config_.emplace(config.rtp.remote_ssrc, ReceiveRtpConfig(config));
876 audio_receive_streams_.insert(receive_stream);
877
878 ConfigureSync(config.sync_group);
879
Tommi0d4647d2020-05-26 19:35:16 +0200880 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
881 if (it != audio_send_ssrcs_.end()) {
882 receive_stream->AssociateSendStream(it->second);
solenberg7602aab2016-11-14 11:30:07 -0800883 }
Tommi0d4647d2020-05-26 19:35:16 +0200884
skvlad7a43d252016-03-22 15:32:27 -0700885 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200886 return receive_stream;
887}
888
889void Call::DestroyAudioReceiveStream(
890 webrtc::AudioReceiveStream* receive_stream) {
891 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +0200892 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -0700893 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700894 webrtc::internal::AudioReceiveStream* audio_receive_stream =
895 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Tommi31001a62020-05-26 11:38:36 +0200896
897 const AudioReceiveStream::Config& config = audio_receive_stream->config();
898 uint32_t ssrc = config.rtp.remote_ssrc;
899 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
900 ->RemoveStream(ssrc);
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100901
902 // TODO(bugs.webrtc.org/11993): Access the map, rtp config, call ConfigureSync
903 // and UpdateAggregateNetworkState on the network thread.
Tommi31001a62020-05-26 11:38:36 +0200904 audio_receive_streams_.erase(audio_receive_stream);
905 const std::string& sync_group = audio_receive_stream->config().sync_group;
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100906
Tommi31001a62020-05-26 11:38:36 +0200907 const auto it = sync_stream_mapping_.find(sync_group);
908 if (it != sync_stream_mapping_.end() && it->second == audio_receive_stream) {
909 sync_stream_mapping_.erase(it);
910 ConfigureSync(sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200911 }
Tommi31001a62020-05-26 11:38:36 +0200912 receive_rtp_config_.erase(ssrc);
913
skvlad7a43d252016-03-22 15:32:27 -0700914 UpdateAggregateNetworkState();
Tomas Gunnarssonad325862021-02-03 16:23:40 +0100915 // TODO(bugs.webrtc.org/11993): Consider if deleting |audio_receive_stream|
916 // on the network thread would be better or if we'd need to tear down the
917 // state in two phases.
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200918 delete audio_receive_stream;
919}
920
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100921// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100922webrtc::VideoSendStream* Call::CreateVideoSendStream(
923 webrtc::VideoSendStream::Config config,
924 VideoEncoderConfig encoder_config,
925 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000926 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Tommi0d4647d2020-05-26 19:35:16 +0200927 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000928
Erik Språng7703f232020-09-14 11:03:13 +0200929 EnsureStarted();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800930
asapersson35151f32016-05-02 23:44:01 -0700931 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700932 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
933 ++ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200934 event_log_->Log(std::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200935 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700936 }
perkj26091b12016-09-01 01:17:40 -0700937
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000938 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
939 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700940 // Copy ssrcs from |config| since |config| is moved.
941 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100942
mflodman0c478b32015-10-21 15:52:16 +0200943 VideoSendStream* send_stream = new VideoSendStream(
Tommi25c77c12020-05-25 17:44:55 +0200944 clock_, num_cpu_cores_, module_process_thread_->process_thread(),
945 task_queue_factory_, call_stats_->AsRtcpRttStats(), transport_send_ptr_,
Tommi822a8742020-05-11 00:42:30 +0200946 bitrate_allocator_.get(), video_send_delay_stats_.get(), event_log_,
947 std::move(config), std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200948 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700949
Tommi0d4647d2020-05-26 19:35:16 +0200950 for (uint32_t ssrc : ssrcs) {
951 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
952 video_send_ssrcs_[ssrc] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000953 }
Tommi0d4647d2020-05-26 19:35:16 +0200954 video_send_streams_.insert(send_stream);
Henrik Boström29444c62020-07-01 15:48:46 +0200955 // Forward resources that were previously added to the call to the new stream.
956 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
957 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +0200958 }
Tommi0d4647d2020-05-26 19:35:16 +0200959
skvlad7a43d252016-03-22 15:32:27 -0700960 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700961
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000962 return send_stream;
963}
964
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100965webrtc::VideoSendStream* Call::CreateVideoSendStream(
966 webrtc::VideoSendStream::Config config,
967 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100968 if (config_.fec_controller_factory) {
969 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
970 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100971 std::unique_ptr<FecController> fec_controller =
972 config_.fec_controller_factory
973 ? config_.fec_controller_factory->CreateFecController()
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200974 : std::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100975 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
976 std::move(fec_controller));
977}
978
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000979void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000980 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700981 RTC_DCHECK(send_stream != nullptr);
Tommi0d4647d2020-05-26 19:35:16 +0200982 RTC_DCHECK_RUN_ON(worker_thread_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000983
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000984 send_stream->Stop();
985
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000986 VideoSendStream* send_stream_impl = nullptr;
Tommi0d4647d2020-05-26 19:35:16 +0200987
988 auto it = video_send_ssrcs_.begin();
989 while (it != video_send_ssrcs_.end()) {
990 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
991 send_stream_impl = it->second;
992 video_send_ssrcs_.erase(it++);
993 } else {
994 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000995 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000996 }
Henrik Boström29444c62020-07-01 15:48:46 +0200997 // Stop forwarding resources to the stream being destroyed.
998 for (const auto& resource_forwarder : adaptation_resource_forwarders_) {
999 resource_forwarder->OnDestroyVideoSendStream(send_stream_impl);
1000 }
Tommi0d4647d2020-05-26 19:35:16 +02001001 video_send_streams_.erase(send_stream_impl);
1002
henrikg91d6ede2015-09-17 00:24:34 -07001003 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001004
Åsa Persson4bece9a2017-10-06 10:04:04 +02001005 VideoSendStream::RtpStateMap rtp_states;
1006 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
1007 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
1008 &rtp_payload_states);
1009 for (const auto& kv : rtp_states) {
1010 suspended_video_send_ssrcs_[kv.first] = kv.second;
1011 }
1012 for (const auto& kv : rtp_payload_states) {
1013 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001014 }
1015
skvlad7a43d252016-03-22 15:32:27 -07001016 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001017 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001018}
1019
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001020webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +02001021 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001022 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001023 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrfb45c6c2017-01-27 06:47:55 -08001024
Johannes Kronf59666b2019-04-08 12:57:06 +02001025 receive_side_cc_.SetSendPeriodicFeedback(
1026 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +01001027
Erik Språng7703f232020-09-14 11:03:13 +02001028 EnsureStarted();
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -08001029
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001030 // TODO(bugs.webrtc.org/11993): Move the registration between |receive_stream|
1031 // and |video_receiver_controller_| out of VideoReceiveStream2 construction
1032 // and set it up asynchronously on the network thread (the registration and
1033 // |video_receiver_controller_| need to live on the network thread).
Tommi553c8692020-05-05 15:35:45 +02001034 VideoReceiveStream2* receive_stream = new VideoReceiveStream2(
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001035 task_queue_factory_, worker_thread_, &video_receiver_controller_,
1036 num_cpu_cores_, transport_send_ptr_->packet_router(),
1037 std::move(configuration), module_process_thread_->process_thread(),
1038 call_stats_.get(), clock_, new VCMTiming(clock_));
Tommi733b5472016-06-10 17:58:01 +02001039
1040 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
Tommi31001a62020-05-26 11:38:36 +02001041 if (config.rtp.rtx_ssrc) {
1042 // We record identical config for the rtx stream as for the main
1043 // stream. Since the transport_send_cc negotiation is per payload
1044 // type, we may get an incorrect value for the rtx stream, but
1045 // that is unlikely to matter in practice.
1046 receive_rtp_config_.emplace(config.rtp.rtx_ssrc, ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -07001047 }
Tommi31001a62020-05-26 11:38:36 +02001048 receive_rtp_config_.emplace(config.rtp.remote_ssrc, ReceiveRtpConfig(config));
1049 video_receive_streams_.insert(receive_stream);
1050 ConfigureSync(config.sync_group);
1051
skvlad7a43d252016-03-22 15:32:27 -07001052 receive_stream->SignalNetworkState(video_network_state_);
1053 UpdateAggregateNetworkState();
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001054 event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001055 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001056 return receive_stream;
1057}
1058
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +00001059void Call::DestroyVideoReceiveStream(
1060 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001061 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001062 RTC_DCHECK_RUN_ON(worker_thread_);
henrikg91d6ede2015-09-17 00:24:34 -07001063 RTC_DCHECK(receive_stream != nullptr);
Tommi553c8692020-05-05 15:35:45 +02001064 VideoReceiveStream2* receive_stream_impl =
1065 static_cast<VideoReceiveStream2*>(receive_stream);
nissee4bcd6d2017-05-16 04:47:04 -07001066 const VideoReceiveStream::Config& config = receive_stream_impl->config();
Tommi31001a62020-05-26 11:38:36 +02001067
1068 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
1069 // separate SSRC there can be either one or two.
1070 receive_rtp_config_.erase(config.rtp.remote_ssrc);
1071 if (config.rtp.rtx_ssrc) {
1072 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001073 }
Tommi31001a62020-05-26 11:38:36 +02001074 video_receive_streams_.erase(receive_stream_impl);
1075 ConfigureSync(config.sync_group);
nisse4709e892017-02-07 01:18:43 -08001076
nisse559af382017-03-21 06:41:12 -07001077 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -08001078 ->RemoveStream(config.rtp.remote_ssrc);
1079
skvlad7a43d252016-03-22 15:32:27 -07001080 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +00001081 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001082}
1083
brandtr7250b392016-12-19 01:13:46 -08001084FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
1085 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -07001086 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001087 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001088
1089 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -07001090
nisse0f15f922017-06-21 01:05:22 -07001091 FlexfecReceiveStreamImpl* receive_stream;
brandtrb29e6522016-12-21 06:37:18 -08001092
Tommi31001a62020-05-26 11:38:36 +02001093 // Unlike the video and audio receive streams, FlexfecReceiveStream implements
1094 // RtpPacketSinkInterface itself, and hence its constructor passes its |this|
1095 // pointer to video_receiver_controller_->CreateStream(). Calling the
1096 // constructor while on the worker thread ensures that we don't call
1097 // OnRtpPacket until the constructor is finished and the object is
1098 // in a valid state, since OnRtpPacket runs on the same thread.
1099 receive_stream = new FlexfecReceiveStreamImpl(
1100 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
1101 call_stats_->AsRtcpRttStats(), module_process_thread_->process_thread());
1102
1103 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1104 receive_rtp_config_.end());
1105 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtrb29e6522016-12-21 06:37:18 -08001106
brandtr25445d32016-10-23 23:37:14 -07001107 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001108
brandtr25445d32016-10-23 23:37:14 -07001109 return receive_stream;
1110}
1111
brandtr7250b392016-12-19 01:13:46 -08001112void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001113 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Tommi0d4647d2020-05-26 19:35:16 +02001114 RTC_DCHECK_RUN_ON(worker_thread_);
brandtrb29e6522016-12-21 06:37:18 -08001115
brandtr25445d32016-10-23 23:37:14 -07001116 RTC_DCHECK(receive_stream != nullptr);
Tommi31001a62020-05-26 11:38:36 +02001117 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
1118 uint32_t ssrc = config.remote_ssrc;
1119 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001120
Tommi31001a62020-05-26 11:38:36 +02001121 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1122 // destroyed.
1123 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
1124 ->RemoveStream(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001125
eladalon42f44f92017-07-25 06:40:06 -07001126 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001127}
1128
Henrik Boströmf4a99912020-06-11 12:07:14 +02001129void Call::AddAdaptationResource(rtc::scoped_refptr<Resource> resource) {
1130 RTC_DCHECK_RUN_ON(worker_thread_);
Henrik Boström29444c62020-07-01 15:48:46 +02001131 adaptation_resource_forwarders_.push_back(
1132 std::make_unique<ResourceVideoSendStreamForwarder>(resource));
1133 const auto& resource_forwarder = adaptation_resource_forwarders_.back();
1134 for (VideoSendStream* send_stream : video_send_streams_) {
1135 resource_forwarder->OnCreateVideoSendStream(send_stream);
Henrik Boströmf4a99912020-06-11 12:07:14 +02001136 }
1137}
1138
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001139RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +02001140 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001141}
1142
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001143Call::Stats Call::GetStats() const {
Tommi0d4647d2020-05-26 19:35:16 +02001144 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi48b48e52019-08-09 11:42:32 +02001145
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001146 Stats stats;
Tommi48b48e52019-08-09 11:42:32 +02001147 // TODO(srte): It is unclear if we only want to report queues if network is
1148 // available.
1149 stats.pacer_delay_ms =
1150 aggregate_network_up_ ? transport_send_ptr_->GetPacerQueuingDelayMs() : 0;
1151
1152 stats.rtt_ms = call_stats_->LastProcessedRtt();
1153
Peter Boström45553ae2015-05-08 13:54:38 +02001154 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001155 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001156 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001157 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001158 &ssrcs, &recv_bandwidth);
Tommi48b48e52019-08-09 11:42:32 +02001159 stats.recv_bandwidth_bps = recv_bandwidth;
Tommi0d4647d2020-05-26 19:35:16 +02001160 stats.send_bandwidth_bps = last_bandwidth_bps_;
1161 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
Tommi48b48e52019-08-09 11:42:32 +02001162
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001163 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001164}
1165
Erik Språngceb44952020-09-22 11:36:35 +02001166const WebRtcKeyValueConfig& Call::trials() const {
1167 return *config_.trials;
1168}
1169
Tomas Gunnarssone984aa22021-04-19 09:21:06 +02001170TaskQueueBase* Call::network_thread() const {
1171 return network_thread_;
1172}
1173
1174TaskQueueBase* Call::worker_thread() const {
1175 return worker_thread_;
1176}
1177
skvlad7a43d252016-03-22 15:32:27 -07001178void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001179 RTC_DCHECK_RUN_ON(network_thread_);
1180 RTC_DCHECK(media == MediaType::AUDIO || media == MediaType::VIDEO);
Tomas Gunnarssond48a2b12021-02-02 17:57:36 +01001181
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001182 auto closure = [this, media, state]() {
1183 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1184 RTC_DCHECK_RUN_ON(worker_thread_);
1185 if (media == MediaType::AUDIO) {
1186 audio_network_state_ = state;
1187 } else {
1188 RTC_DCHECK_EQ(media, MediaType::VIDEO);
1189 video_network_state_ = state;
1190 }
1191
1192 // TODO(tommi): Is it necessary to always do this, including if there
1193 // was no change in state?
1194 UpdateAggregateNetworkState();
1195
1196 // TODO(tommi): Is it right to do this if media == AUDIO?
1197 for (VideoReceiveStream2* video_receive_stream : video_receive_streams_) {
1198 video_receive_stream->SignalNetworkState(video_network_state_);
1199 }
1200 };
1201
1202 if (network_thread_ == worker_thread_) {
1203 closure();
1204 } else {
1205 // TODO(bugs.webrtc.org/11993): Remove workaround when we no longer need to
1206 // post to the worker thread.
1207 worker_thread_->PostTask(ToQueuedTask(task_safety_, std::move(closure)));
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001208 }
1209}
1210
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001211void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001212 RTC_DCHECK_RUN_ON(network_thread_);
1213 worker_thread_->PostTask(
1214 ToQueuedTask(task_safety_, [this, transport_overhead_per_packet]() {
1215 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1216 RTC_DCHECK_RUN_ON(worker_thread_);
1217 for (auto& kv : audio_send_ssrcs_) {
1218 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1219 }
1220 }));
michaelt79e05882016-11-08 02:50:09 -08001221}
1222
skvlad7a43d252016-03-22 15:32:27 -07001223void Call::UpdateAggregateNetworkState() {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001224 // TODO(bugs.webrtc.org/11993): Move this over to the network thread.
1225 // RTC_DCHECK_RUN_ON(network_thread_);
1226
Tommi0d4647d2020-05-26 19:35:16 +02001227 RTC_DCHECK_RUN_ON(worker_thread_);
skvlad7a43d252016-03-22 15:32:27 -07001228
Tommi0d4647d2020-05-26 19:35:16 +02001229 bool have_audio =
1230 !audio_send_ssrcs_.empty() || !audio_receive_streams_.empty();
1231 bool have_video =
1232 !video_send_ssrcs_.empty() || !video_receive_streams_.empty();
skvlad7a43d252016-03-22 15:32:27 -07001233
Sebastian Janssona06e9192018-03-07 18:49:55 +01001234 bool aggregate_network_up =
1235 ((have_video && video_network_state_ == kNetworkUp) ||
1236 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001237
Harald Alvestrand977b2652019-12-12 13:40:50 +01001238 if (aggregate_network_up != aggregate_network_up_) {
1239 RTC_LOG(LS_INFO)
1240 << "UpdateAggregateNetworkState: aggregate_state change to "
1241 << (aggregate_network_up ? "up" : "down");
1242 } else {
1243 RTC_LOG(LS_VERBOSE)
1244 << "UpdateAggregateNetworkState: aggregate_state remains at "
1245 << (aggregate_network_up ? "up" : "down");
1246 }
Tommi48b48e52019-08-09 11:42:32 +02001247 aggregate_network_up_ = aggregate_network_up;
1248
Sebastian Janssone6256052018-05-04 14:08:15 +02001249 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001250}
1251
stefanc1aeaf02015-10-15 07:26:07 -07001252void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
Tomas Gunnarssoneb9c3f22021-04-19 12:53:09 +02001253 // In production and with most tests, this method will be called on the
1254 // network thread. However some test classes such as DirectTransport don't
1255 // incorporate a network thread. This means that tests for RtpSenderEgress
1256 // and ModuleRtpRtcpImpl2 that use DirectTransport, will call this method
1257 // on a ProcessThread. This is alright as is since we forward the call to
1258 // implementations that either just do a PostTask or use locking.
asapersson35151f32016-05-02 23:44:01 -07001259 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1260 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001261 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001262}
1263
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001264void Call::OnStartRateUpdate(DataRate start_rate) {
Tommi8edfe6e2020-05-28 09:01:41 +02001265 RTC_DCHECK_RUN_ON(send_transport_queue());
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001266 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1267}
1268
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001269void Call::OnTargetTransferRate(TargetTransferRate msg) {
Tommi8edfe6e2020-05-28 09:01:41 +02001270 RTC_DCHECK_RUN_ON(send_transport_queue());
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001271
1272 uint32_t target_bitrate_bps = msg.target_rate.bps();
nisse559af382017-03-21 06:41:12 -07001273 // For controlling the rate of feedback messages.
1274 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +02001275 bitrate_allocator_->OnNetworkEstimateChanged(msg);
mflodman0e7e2592015-11-12 21:02:42 -08001276
Tommi0d4647d2020-05-26 19:35:16 +02001277 worker_thread_->PostTask(
1278 ToQueuedTask(task_safety_, [this, target_bitrate_bps]() {
1279 RTC_DCHECK_RUN_ON(worker_thread_);
1280 last_bandwidth_bps_ = target_bitrate_bps;
asaperssonce2e1362016-09-09 00:13:35 -07001281
Tommi0d4647d2020-05-26 19:35:16 +02001282 // Ignore updates if bitrate is zero (the aggregate network state is
1283 // down) or if we're not sending video.
1284 if (target_bitrate_bps == 0 || video_send_streams_.empty()) {
1285 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1286 pacer_bitrate_kbps_counter_.ProcessAndPause();
1287 return;
1288 }
asaperssonce2e1362016-09-09 00:13:35 -07001289
Tommi0d4647d2020-05-26 19:35:16 +02001290 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1291 // Pacer bitrate may be higher than bitrate estimate if enforcing min
1292 // bitrate.
1293 uint32_t pacer_bitrate_bps =
1294 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1295 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
1296 }));
perkj71ee44c2016-06-15 00:47:53 -07001297}
mflodman101f2502016-06-09 17:21:19 +02001298
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001299void Call::OnAllocationLimitsChanged(BitrateAllocationLimits limits) {
Tommi8edfe6e2020-05-28 09:01:41 +02001300 RTC_DCHECK_RUN_ON(send_transport_queue());
Tommi48b48e52019-08-09 11:42:32 +02001301
Sebastian Jansson93b1ea22019-09-18 18:31:52 +02001302 transport_send_ptr_->SetAllocatedSendBitrateLimits(limits);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001303
Tommi0d4647d2020-05-26 19:35:16 +02001304 worker_thread_->PostTask(ToQueuedTask(task_safety_, [this, limits]() {
1305 RTC_DCHECK_RUN_ON(worker_thread_);
1306 min_allocated_send_bitrate_bps_ = limits.min_allocatable_rate.bps();
1307 configured_max_padding_bitrate_bps_ = limits.max_padding_rate.bps();
1308 }));
mflodman0e7e2592015-11-12 21:02:42 -08001309}
1310
pbos8fc7fa72015-07-15 08:02:58 -07001311void Call::ConfigureSync(const std::string& sync_group) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001312 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
pbos8fc7fa72015-07-15 08:02:58 -07001313 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001314 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001315 return;
1316
1317 AudioReceiveStream* sync_audio_stream = nullptr;
1318 // Find existing audio stream.
1319 const auto it = sync_stream_mapping_.find(sync_group);
1320 if (it != sync_stream_mapping_.end()) {
1321 sync_audio_stream = it->second;
1322 } else {
1323 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001324 for (AudioReceiveStream* stream : audio_receive_streams_) {
1325 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001326 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001327 RTC_LOG(LS_WARNING)
1328 << "Attempting to sync more than one audio stream "
1329 "within the same sync group. This is not "
1330 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001331 break;
1332 }
nissee4bcd6d2017-05-16 04:47:04 -07001333 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001334 }
1335 }
1336 }
1337 if (sync_audio_stream)
1338 sync_stream_mapping_[sync_group] = sync_audio_stream;
1339 size_t num_synced_streams = 0;
Tommi553c8692020-05-05 15:35:45 +02001340 for (VideoReceiveStream2* video_stream : video_receive_streams_) {
pbos8fc7fa72015-07-15 08:02:58 -07001341 if (video_stream->config().sync_group != sync_group)
1342 continue;
1343 ++num_synced_streams;
1344 if (num_synced_streams > 1) {
1345 // TODO(pbos): Support synchronizing more than one A/V pair.
1346 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001347 RTC_LOG(LS_WARNING)
1348 << "Attempting to sync more than one audio/video pair "
1349 "within the same sync group. This is not supported in "
1350 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001351 }
1352 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001353 if (num_synced_streams == 1) {
1354 // sync_audio_stream may be null and that's ok.
1355 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001356 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001357 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001358 }
1359 }
1360}
1361
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001362PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1363 const uint8_t* packet,
1364 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001365 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
Tommi3f418cc2021-05-05 11:04:30 +02001366
1367 // TODO(bugs.webrtc.org/11993): This DCHECK is here just to maintain the
1368 // invariant that currently the only call path to this function is via
1369 // `PeerConnection::InitializeRtcpCallback()`. DeliverRtp on the other hand
1370 // gets called via the channel classes and
1371 // WebRtc[Audio|Video]Channel's `OnPacketReceived`. We'll remove the
1372 // PeerConnection involvement as well as
1373 // `JsepTransportController::OnRtcpPacketReceived_n` and `rtcp_handler`
1374 // and make sure that the flow of packets is consistent from the
1375 // `RtpTransport` class, via the *Channel and *Engine classes and into Call.
1376 // This way we'll also know more about the context of the packet.
1377 RTC_DCHECK_EQ(media_type, MediaType::ANY);
1378
mflodman3d7db262016-04-29 00:57:13 -07001379 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001380 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1381 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001382 if (received_bytes_per_second_counter_.HasSample()) {
1383 // First RTP packet has been received.
1384 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1385 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1386 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001387 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001388 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
Tommi553c8692020-05-05 15:35:45 +02001389 for (VideoReceiveStream2* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001390 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001391 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001392 }
1393 }
1394 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
nissee4bcd6d2017-05-16 04:47:04 -07001395 for (AudioReceiveStream* stream : audio_receive_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001396 stream->DeliverRtcp(packet, length);
1397 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001398 }
1399 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001400 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001401 for (VideoSendStream* stream : video_send_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001402 stream->DeliverRtcp(packet, length);
1403 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001404 }
1405 }
mflodman3d7db262016-04-29 00:57:13 -07001406 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
mflodman3d7db262016-04-29 00:57:13 -07001407 for (auto& kv : audio_send_ssrcs_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001408 kv.second->DeliverRtcp(packet, length);
1409 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001410 }
1411 }
1412
Elad Alon4a87e1c2017-10-03 16:11:34 +02001413 if (rtcp_delivered) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001414 event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001415 rtc::MakeArrayView(packet, length)));
1416 }
mflodman3d7db262016-04-29 00:57:13 -07001417
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001418 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001419}
1420
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001421PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001422 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001423 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001424 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
Tommi3f418cc2021-05-05 11:04:30 +02001425 RTC_DCHECK_NE(media_type, MediaType::ANY);
nissed44ce052017-02-06 02:23:00 -08001426
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001427 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001428 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001429 return DELIVERY_PACKET_ERROR;
1430
Niels Möller70082872018-08-07 11:03:12 +02001431 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001432 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001433 // Repair packet_time_us for clock resets by comparing a new read of
1434 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001435 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001436 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001437 }
Tommi2497a272021-05-05 12:33:00 +02001438 parsed_packet.set_arrival_time(Timestamp::Micros(packet_time_us));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001439 } else {
Tommi2497a272021-05-05 12:33:00 +02001440 parsed_packet.set_arrival_time(clock_->CurrentTime());
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001441 }
nissed44ce052017-02-06 02:23:00 -08001442
sprangc1abde72017-07-11 03:56:21 -07001443 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1444 // These are empty (zero length payload) RTP packets with an unsignaled
1445 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001446 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001447
1448 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1449 is_keep_alive_packet);
1450
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001451 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001452 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001453 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1454 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001455 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001456 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001457 // But deregistering in the |receive_rtp_config_| map is. So by not passing
1458 // the packet on to demuxing in this case, we prevent incoming packets to be
1459 // passed on via the demuxer to a receive stream which is being torned down.
nisse0f15f922017-06-21 01:05:22 -07001460 return DELIVERY_UNKNOWN_SSRC;
1461 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001462
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001463 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001464
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001465 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001466
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001467 // RateCounters expect input parameter as int, save it as int,
1468 // instead of converting each time it is passed to RateCounter::Add below.
1469 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001470 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001471 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001472 received_bytes_per_second_counter_.Add(length);
1473 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001474 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001475 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Tommi2497a272021-05-05 12:33:00 +02001476 const int64_t arrival_time_ms = parsed_packet.arrival_time().ms();
saza0d7f04d2017-07-04 04:05:06 -07001477 if (!first_received_rtp_audio_ms_) {
1478 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1479 }
1480 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001481 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001482 }
nissee4bcd6d2017-05-16 04:47:04 -07001483 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001484 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001485 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001486 received_bytes_per_second_counter_.Add(length);
1487 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001488 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001489 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Tommi2497a272021-05-05 12:33:00 +02001490 const int64_t arrival_time_ms = parsed_packet.arrival_time().ms();
saza0d7f04d2017-07-04 04:05:06 -07001491 if (!first_received_rtp_video_ms_) {
1492 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1493 }
1494 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001495 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001496 }
1497 }
1498 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001499}
1500
stefan68786d22015-09-08 05:36:15 -07001501PacketReceiver::DeliveryStatus Call::DeliverPacket(
1502 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001503 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001504 int64_t packet_time_us) {
Tommi0d4647d2020-05-26 19:35:16 +02001505 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi8edfe6e2020-05-28 09:01:41 +02001506
Tommi25eb47c2019-08-29 16:39:05 +02001507 if (IsRtcp(packet.cdata(), packet.size()))
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001508 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001509
Niels Möller70082872018-08-07 11:03:12 +02001510 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001511}
1512
Tomas Gunnarssona722d2a2021-01-19 09:00:18 +01001513void Call::DeliverPacketAsync(MediaType media_type,
1514 rtc::CopyOnWriteBuffer packet,
1515 int64_t packet_time_us,
1516 PacketCallback callback) {
Tomas Gunnarsson41bfcf42021-01-30 16:15:21 +01001517 RTC_DCHECK_RUN_ON(network_thread_);
Tomas Gunnarssona722d2a2021-01-19 09:00:18 +01001518
1519 TaskQueueBase* network_thread = rtc::Thread::Current();
1520 RTC_DCHECK(network_thread);
1521
1522 worker_thread_->PostTask(ToQueuedTask(
1523 task_safety_, [this, network_thread, media_type, p = std::move(packet),
1524 packet_time_us, cb = std::move(callback)] {
1525 RTC_DCHECK_RUN_ON(worker_thread_);
1526 DeliveryStatus status = DeliverPacket(media_type, p, packet_time_us);
1527 if (cb) {
1528 network_thread->PostTask(
1529 ToQueuedTask([cb = std::move(cb), status, media_type,
1530 p = std::move(p), packet_time_us]() {
1531 cb(status, media_type, std::move(p), packet_time_us);
1532 }));
1533 }
1534 }));
1535}
1536
nissed2ef3142017-05-11 08:00:58 -07001537void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Tomas Gunnarssonad325862021-02-03 16:23:40 +01001538 // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread.
1539 // This method is called synchronously via |OnRtpPacket()| (see DeliverRtp)
1540 // on the same thread.
Tommi0d4647d2020-05-26 19:35:16 +02001541 RTC_DCHECK_RUN_ON(worker_thread_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001542 RtpPacketReceived parsed_packet;
1543 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001544 return;
1545
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001546 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001547
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001548 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001549 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001550 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1551 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001552 // Destruction of the receive stream, including deregistering from the
Tommi0d4647d2020-05-26 19:35:16 +02001553 // RtpDemuxer, is not protected by the |worker_thread_|.
Tommi31001a62020-05-26 11:38:36 +02001554 // But deregistering in the |receive_rtp_config_| map is.
brandtrcaea68f2017-08-23 00:55:17 -07001555 // So by not passing the packet on to demuxing in this case, we prevent
1556 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001557 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001558 return;
1559 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001560 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001561
1562 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001563 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001564 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001565}
1566
nissed44ce052017-02-06 02:23:00 -08001567void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1568 MediaType media_type) {
1569 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001570 bool use_send_side_bwe =
1571 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001572
brandtrb29e6522016-12-21 06:37:18 -08001573 RTPHeader header;
1574 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001575
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001576 ReceivedPacket packet_msg;
Danil Chapovalovcad3e0e2020-02-17 18:46:07 +01001577 packet_msg.size = DataSize::Bytes(packet.payload_size());
Tommi2497a272021-05-05 12:33:00 +02001578 packet_msg.receive_time = packet.arrival_time();
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001579 if (header.extension.hasAbsoluteSendTime) {
1580 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1581 }
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001582 transport_send_ptr_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001583
nisse4709e892017-02-07 01:18:43 -08001584 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001585 // Inconsistent configuration of send side BWE. Do nothing.
1586 // TODO(nisse): Without this check, we may produce RTCP feedback
1587 // packets even when not negotiated. But it would be cleaner to
1588 // move the check down to RTCPSender::SendFeedbackPacket, which
1589 // would also help the PacketRouter to select an appropriate rtp
1590 // module in the case that some, but not all, have RTCP feedback
1591 // enabled.
1592 return;
1593 }
1594 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001595 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001596 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001597 receive_side_cc_.OnReceivedPacket(
Tommi2497a272021-05-05 12:33:00 +02001598 packet.arrival_time().ms(),
1599 packet.payload_size() + packet.padding_size(), header);
nissed44ce052017-02-06 02:23:00 -08001600 }
brandtrb29e6522016-12-21 06:37:18 -08001601}
1602
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001603} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001604
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001605} // namespace webrtc