blob: 293afa977941e3a76fd3fe4113020ff1d28669ca [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
Karl Wiberg918f50c2018-07-05 11:40:33 +020022#include "absl/memory/memory.h"
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020023#include "absl/types/optional.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020024#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "audio/audio_receive_stream.h"
26#include "audio/audio_send_stream.h"
27#include "audio/audio_state.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "call/bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010030#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "call/rtp_stream_receiver_controller.h"
32#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020033#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020034#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
35#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
36#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
37#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon99a81b62017-09-21 10:25:29 +020039#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
41#include "modules/rtp_rtcp/include/flexfec_receiver.h"
42#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
43#include "modules/rtp_rtcp/include/rtp_header_parser.h"
44#include "modules/rtp_rtcp/source/byte_io.h"
45#include "modules/rtp_rtcp/source/rtp_packet_received.h"
46#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010047#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/location.h"
51#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 15:59:12 +010052#include "rtc_base/numerics/safe_minmax.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020053#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020054#include "rtc_base/synchronization/rw_lock_wrapper.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020055#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080057#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "rtc_base/trace_event.h"
59#include "system_wrappers/include/clock.h"
60#include "system_wrappers/include/cpu_info.h"
Jonas Oreland6d835922019-03-18 10:59:40 +010061#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "video/call_stats.h"
64#include "video/send_delay_stats.h"
65#include "video/stats_counter.h"
66#include "video/video_receive_stream.h"
67#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000068
69namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000070
nisse4709e892017-02-07 01:18:43 -080071namespace {
Johannes Kronf59666b2019-04-08 12:57:06 +020072bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010073 for (const auto& extension : extensions) {
74 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
Johannes Kronf59666b2019-04-08 12:57:06 +020075 return false;
Johannes Kron7ff164e2019-02-07 12:50:18 +010076 }
Johannes Kronf59666b2019-04-08 12:57:06 +020077 return true;
Johannes Kron7ff164e2019-02-07 12:50:18 +010078}
79
nisse4709e892017-02-07 01:18:43 -080080// TODO(nisse): This really begs for a shared context struct.
81bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
82 bool transport_cc) {
83 if (!transport_cc)
84 return false;
85 for (const auto& extension : extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010086 if (extension.uri == RtpExtension::kTransportSequenceNumberUri ||
87 extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
nisse4709e892017-02-07 01:18:43 -080088 return true;
89 }
90 return false;
91}
92
93bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
94 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
95}
96
97bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
98 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
99}
100
101bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
102 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
103}
104
nisse26e3abb2017-08-25 04:44:25 -0700105const int* FindKeyByValue(const std::map<int, int>& m, int v) {
106 for (const auto& kv : m) {
107 if (kv.second == v)
108 return &kv.first;
109 }
110 return nullptr;
111}
112
eladalon8ec568a2017-09-08 06:15:52 -0700113std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700114 const VideoReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200115 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700116 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
117 rtclog_config->local_ssrc = config.rtp.local_ssrc;
118 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
119 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
120 rtclog_config->remb = config.rtp.remb;
121 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) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200135 auto rtclog_config = absl::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) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200151 auto rtclog_config = absl::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
nisse4709e892017-02-07 01:18:43 -0800158} // namespace
159
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000160namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000161
Sebastian Janssone6256052018-05-04 14:08:15 +0200162class Call final : public webrtc::Call,
163 public PacketReceiver,
164 public RecoveredPacketReceiver,
165 public TargetTransferRateObserver,
166 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000167 public:
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100168 Call(Clock* clock,
169 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100170 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
171 std::unique_ptr<ProcessThread> module_process_thread,
172 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200173 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000174
brandtr25445d32016-10-23 23:37:14 -0700175 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000176 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000177
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200178 webrtc::AudioSendStream* CreateAudioSendStream(
179 const webrtc::AudioSendStream::Config& config) override;
180 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
181
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200182 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
183 const webrtc::AudioReceiveStream::Config& config) override;
184 void DestroyAudioReceiveStream(
185 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000186
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200187 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700188 webrtc::VideoSendStream::Config config,
189 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100190 webrtc::VideoSendStream* CreateVideoSendStream(
191 webrtc::VideoSendStream::Config config,
192 VideoEncoderConfig encoder_config,
193 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000194 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000195
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200196 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200197 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000198 void DestroyVideoReceiveStream(
199 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000200
brandtr7250b392016-12-19 01:13:46 -0800201 FlexfecReceiveStream* CreateFlexfecReceiveStream(
202 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700203 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800204 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700205
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100206 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
207
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000208 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000209
brandtr25445d32016-10-23 23:37:14 -0700210 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700211 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100212 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200213 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000214
brandtr4e523862016-10-18 23:50:45 -0700215 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700216 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700217
Alex Narest78609d52017-10-20 10:37:47 +0200218 void SetBitrateAllocationStrategy(
219 std::unique_ptr<rtc::BitrateAllocationStrategy>
220 bitrate_allocation_strategy) override;
221
skvlad7a43d252016-03-22 15:32:27 -0700222 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000223
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200224 void OnAudioTransportOverheadChanged(
225 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800226
stefanc1aeaf02015-10-15 07:26:07 -0700227 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
228
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100229 // Implements TargetTransferRateObserver,
230 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100231 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800232
perkj71ee44c2016-06-15 00:47:53 -0700233 // Implements BitrateAllocator::LimitObserver.
234 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +0100235 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100236 uint32_t total_bitrate_bps) override;
perkj71ee44c2016-06-15 00:47:53 -0700237
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800238 // This method is invoked when the media transport is created and when the
239 // media transport is being destructed.
240 // We only allow one media transport per connection.
241 //
242 // It should be called with non-null argument at most once, and if it was
243 // called with non-null argument, it has to be called with a null argument
244 // at least once after that.
245 void MediaTransportChange(MediaTransportInterface* media_transport) override;
246
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700247 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
248
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000249 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200250 DeliveryStatus DeliverRtcp(MediaType media_type,
251 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200252 size_t length);
stefan68786d22015-09-08 05:36:15 -0700253 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100254 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200255 int64_t packet_time_us);
pbos8fc7fa72015-07-15 08:02:58 -0700256 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700257 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700258
nissed44ce052017-02-06 02:23:00 -0800259 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
260 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700261 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800262
asaperssonfc5e81c2017-04-19 23:28:53 -0700263 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700264 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800265 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700266 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700267 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800268
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800269 // If |media_transport| is not null, it registers the rate observer for the
270 // media transport.
271 void RegisterRateObserver() RTC_LOCKS_EXCLUDED(target_observer_crit_);
272
Niels Möller46879152019-01-07 15:54:47 +0100273 // Intended for DCHECKs, to avoid locking in production builds.
274 MediaTransportInterface* media_transport()
275 RTC_LOCKS_EXCLUDED(target_observer_crit_);
276
Peter Boströmd3c94472015-12-09 11:20:58 +0100277 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100278 TaskQueueFactory* const task_queue_factory_;
stefan91d92602015-11-11 10:13:02 -0800279
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700280 // Caching the last SetBitrate for media transport.
281 absl::optional<MediaTransportTargetRateConstraints> last_set_bitrate_
282 RTC_GUARDED_BY(&target_observer_crit_);
Peter Boström45553ae2015-05-08 13:54:38 +0200283 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800284 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800285 const std::unique_ptr<CallStats> call_stats_;
286 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000287 Call::Config config_;
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200288 SequenceChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000289
skvlad7a43d252016-03-22 15:32:27 -0700290 NetworkState audio_network_state_;
291 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100292 rtc::CriticalSection aggregate_network_up_crit_;
293 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000294
kwibergb25345e2016-03-12 06:10:44 -0800295 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700296 // Audio, Video, and FlexFEC receive streams are owned by the client that
297 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700298 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700299 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200300 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700301 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700302
pbos8fc7fa72015-07-15 08:02:58 -0700303 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700304 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000305
nisse0f15f922017-06-21 01:05:22 -0700306 // TODO(nisse): Should eventually be injected at creation,
307 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700308 RtpStreamReceiverController audio_receiver_controller_;
309 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700310
nissed44ce052017-02-06 02:23:00 -0800311 // This extra map is used for receive processing which is
312 // independent of media type.
313
314 // TODO(nisse): In the RTP transport refactoring, we should have a
315 // single mapping from ssrc to a more abstract receive stream, with
316 // accessor methods for all configuration we need at this level.
317 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100318 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
319 : extensions(config.rtp.extensions),
320 use_send_side_bwe(UseSendSideBwe(config)) {}
321 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
322 : extensions(config.rtp.extensions),
323 use_send_side_bwe(UseSendSideBwe(config)) {}
324 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
325 : extensions(config.rtp_header_extensions),
326 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800327
328 // Registered RTP header extensions for each stream. Note that RTP header
329 // extensions are negotiated per track ("m= line") in the SDP, but we have
330 // no notion of tracks at the Call level. We therefore store the RTP header
331 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100332 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800333 // Set if both RTP extension the RTCP feedback message needed for
334 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100335 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800336 };
337 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700338 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800339
kwibergb25345e2016-03-12 06:10:44 -0800340 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700341 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700342 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
343 RTC_GUARDED_BY(send_crit_);
344 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
345 RTC_GUARDED_BY(send_crit_);
346 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000347
ossuc3d4b482017-05-23 06:07:11 -0700348 using RtpStateMap = std::map<uint32_t, RtpState>;
349 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700350 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700351 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700352 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700353
Åsa Persson4bece9a2017-10-06 10:04:04 +0200354 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
355 RtpPayloadStateMap suspended_video_payload_states_
356 RTC_GUARDED_BY(configuration_sequence_checker_);
357
skvlad11a9cbf2016-10-07 11:53:05 -0700358 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700359
stefan18adf0a2015-11-17 06:24:56 -0800360 // The following members are only accessed (exclusively) from one thread and
361 // from the destructor, and therefore doesn't need any explicit
362 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700363 RateCounter received_bytes_per_second_counter_;
364 RateCounter received_audio_bytes_per_second_counter_;
365 RateCounter received_video_bytes_per_second_counter_;
366 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200367 absl::optional<int64_t> first_received_rtp_audio_ms_;
368 absl::optional<int64_t> last_received_rtp_audio_ms_;
369 absl::optional<int64_t> first_received_rtp_video_ms_;
370 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 10:13:02 -0800371
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100372 rtc::CriticalSection last_bandwidth_bps_crit_;
373 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800374 // TODO(holmer): Remove this lock once BitrateController no longer calls
375 // OnNetworkChanged from multiple threads.
376 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700377 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
378 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
379 AvgCounter estimated_send_bitrate_kbps_counter_
380 RTC_GUARDED_BY(&bitrate_crit_);
381 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800382
nisse559af382017-03-21 06:41:12 -0700383 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100384
385 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
386
asapersson35151f32016-05-02 23:44:01 -0700387 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700388 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800389
Sebastian Janssone6256052018-05-04 14:08:15 +0200390 // Caches transport_send_.get(), to avoid racing with destructor.
391 // Note that this is declared before transport_send_ to ensure that it is not
392 // invalidated until no more tasks can be running on the transport_send_ task
393 // queue.
394 RtpTransportControllerSendInterface* transport_send_ptr_;
395 // Declared last since it will issue callbacks from a task queue. Declaring it
396 // last ensures that it is destroyed first and any running tasks are finished.
397 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800398
399 // This is a precaution, since |MediaTransportChange| is not guaranteed to be
400 // invoked on a particular thread.
401 rtc::CriticalSection target_observer_crit_;
402 bool is_target_rate_observer_registered_
403 RTC_GUARDED_BY(&target_observer_crit_) = false;
404 MediaTransportInterface* media_transport_
405 RTC_GUARDED_BY(&target_observer_crit_) = nullptr;
406
henrikg3c089d72015-09-16 05:37:44 -0700407 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000408};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000409} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000410
asapersson2e5cfcd2016-08-11 08:41:18 -0700411std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200412 char buf[1024];
413 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700414 ss << "Call stats: " << time_ms << ", {";
415 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
416 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
417 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
418 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
419 ss << "rtt_ms: " << rtt_ms;
420 ss << '}';
421 return ss.str();
422}
423
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000424Call* Call::Create(const Call::Config& config) {
Danil Chapovalov359fe332019-04-01 10:46:36 +0200425 return Create(config, Clock::GetRealTimeClock(),
426 ProcessThread::Create("PacerThread"),
427 ProcessThread::Create("ModuleProcessThread"));
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100428}
429
430Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100431 Clock* clock,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100432 std::unique_ptr<ProcessThread> call_thread,
Danil Chapovalov359fe332019-04-01 10:46:36 +0200433 std::unique_ptr<ProcessThread> pacer_thread) {
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200434 RTC_DCHECK(config.task_queue_factory);
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100435 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100436 clock, config,
Sebastian Janssoned50e6c2019-03-01 14:45:21 +0100437 absl::make_unique<RtpTransportControllerSend>(
Ying Wang0810a7c2019-04-10 13:48:24 +0200438 clock, config.event_log, config.network_state_predictor_factory,
439 config.network_controller_factory, config.bitrate_config,
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200440 std::move(pacer_thread), config.task_queue_factory),
441 std::move(call_thread), config.task_queue_factory);
zstein7cb69d52017-05-08 11:52:38 -0700442}
443
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100444// This method here to avoid subclasses has to implement this method.
445// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
446// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100447VideoSendStream* Call::CreateVideoSendStream(
448 VideoSendStream::Config config,
449 VideoEncoderConfig encoder_config,
450 std::unique_ptr<FecController> fec_controller) {
451 return nullptr;
452}
453
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000454namespace internal {
455
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100456Call::Call(Clock* clock,
457 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100458 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
459 std::unique_ptr<ProcessThread> module_process_thread,
460 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100461 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100462 task_queue_factory_(task_queue_factory),
stefan91d92602015-11-11 10:13:02 -0800463 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100464 module_process_thread_(std::move(module_process_thread)),
Tommi38c5d932018-03-27 23:11:09 +0200465 call_stats_(new CallStats(clock_, module_process_thread_.get())),
Sebastian Janssonda6806c2019-03-04 17:05:12 +0100466 bitrate_allocator_(new BitrateAllocator(clock_, this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200467 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800468 audio_network_state_(kNetworkDown),
469 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100470 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000471 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800472 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700473 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700474 received_bytes_per_second_counter_(clock_, nullptr, true),
475 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
476 received_video_bytes_per_second_counter_(clock_, nullptr, true),
477 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100478 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700479 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700480 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700481 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
482 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700483 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100484 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700485 video_send_delay_stats_(new SendDelayStats(clock_)),
Benjamin Wrighta5564482019-04-03 10:44:18 -0700486 start_ms_(clock_->TimeInMilliseconds()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700487 RTC_DCHECK(config.event_log != nullptr);
nisse6167b262017-04-06 06:34:25 -0700488 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 14:08:15 +0200489 transport_send_ptr_ = transport_send_.get();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000490}
491
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000492Call::~Call() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200493 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700494
solenbergc7a8b082015-10-16 14:35:07 -0700495 RTC_CHECK(audio_send_ssrcs_.empty());
496 RTC_CHECK(video_send_ssrcs_.empty());
497 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700498 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700499 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000500
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800501 if (!media_transport_) {
502 module_process_thread_->DeRegisterModule(
503 receive_side_cc_.GetRemoteBitrateEstimator(true));
504 module_process_thread_->DeRegisterModule(&receive_side_cc_);
505 module_process_thread_->DeRegisterModule(call_stats_.get());
506 module_process_thread_->Stop();
507 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800508 }
sprang6d6122b2016-07-13 06:37:09 -0700509
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100510 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700511 // Only update histograms after process threads have been shut down, so that
512 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700513 {
514 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700515 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700516 }
sprang6d6122b2016-07-13 06:37:09 -0700517 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700518 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000519}
520
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800521void Call::RegisterRateObserver() {
522 rtc::CritScope lock(&target_observer_crit_);
523
524 if (is_target_rate_observer_registered_) {
525 return;
526 }
527
528 is_target_rate_observer_registered_ = true;
529
530 if (media_transport_) {
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800531 // TODO(bugs.webrtc.org/9719): We should report call_stats_ from
532 // media transport (at least Rtt). We should extend media transport
533 // interface to include "receive_side bwe" if needed.
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800534 media_transport_->AddTargetTransferRateObserver(this);
535 } else {
536 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800537
538 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800539
540 module_process_thread_->RegisterModule(
541 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
542 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
543 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
544 module_process_thread_->Start();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800545 }
546}
547
Niels Möller46879152019-01-07 15:54:47 +0100548MediaTransportInterface* Call::media_transport() {
549 rtc::CritScope lock(&target_observer_crit_);
550 return media_transport_;
551}
552
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800553void Call::MediaTransportChange(MediaTransportInterface* media_transport) {
554 rtc::CritScope lock(&target_observer_crit_);
555
556 if (is_target_rate_observer_registered_) {
557 // Only used to unregister rate observer from media transport. Registration
558 // happens when the stream is created.
559 if (!media_transport && media_transport_) {
560 media_transport_->RemoveTargetTransferRateObserver(this);
561 media_transport_ = nullptr;
562 is_target_rate_observer_registered_ = false;
563 }
564 } else if (media_transport) {
565 RTC_DCHECK(media_transport_ == nullptr ||
566 media_transport_ == media_transport)
567 << "media_transport_=" << (media_transport_ != nullptr)
568 << ", (media_transport_==media_transport)="
569 << (media_transport_ == media_transport);
570 media_transport_ = media_transport;
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -0700571 MediaTransportTargetRateConstraints constraints;
572 if (config_.bitrate_config.start_bitrate_bps > 0) {
573 constraints.starting_bitrate =
574 DataRate::bps(config_.bitrate_config.start_bitrate_bps);
575 }
576 if (config_.bitrate_config.max_bitrate_bps > 0) {
577 constraints.max_bitrate =
578 DataRate::bps(config_.bitrate_config.max_bitrate_bps);
579 }
580 if (config_.bitrate_config.min_bitrate_bps > 0) {
581 constraints.min_bitrate =
582 DataRate::bps(config_.bitrate_config.min_bitrate_bps);
583 }
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700584
585 // User called ::SetBitrate on peer connection before
586 // media transport was created.
587 if (last_set_bitrate_) {
588 media_transport_->SetTargetBitrateLimits(*last_set_bitrate_);
589 } else {
590 media_transport_->SetTargetBitrateLimits(constraints);
591 }
592 }
593}
594
595void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
596 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
597 // Can the client code invoke 'SetBitrate' before media transport is created?
598 // It's probably possible :/
599 MediaTransportTargetRateConstraints constraints;
600 if (preferences.start_bitrate_bps.has_value()) {
601 constraints.starting_bitrate =
602 webrtc::DataRate::bps(*preferences.start_bitrate_bps);
603 }
604 if (preferences.max_bitrate_bps.has_value()) {
605 constraints.max_bitrate =
606 webrtc::DataRate::bps(*preferences.max_bitrate_bps);
607 }
608 if (preferences.min_bitrate_bps.has_value()) {
609 constraints.min_bitrate =
610 webrtc::DataRate::bps(*preferences.min_bitrate_bps);
611 }
612 rtc::CritScope lock(&target_observer_crit_);
613 last_set_bitrate_ = constraints;
614 if (media_transport_) {
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -0700615 media_transport_->SetTargetBitrateLimits(constraints);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800616 }
617}
618
asapersson4374a092016-07-27 00:39:09 -0700619void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700620 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700621 "WebRTC.Call.LifetimeInSeconds",
622 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
623}
624
asaperssonfc5e81c2017-04-19 23:28:53 -0700625void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
626 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800627 return;
628 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700629 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800630 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
631 return;
asaperssonce2e1362016-09-09 00:13:35 -0700632 const int kMinRequiredPeriodicSamples = 5;
633 AggregatedStats send_bitrate_stats =
634 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
635 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700636 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
637 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100638 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
639 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800640 }
asaperssonce2e1362016-09-09 00:13:35 -0700641 AggregatedStats pacer_bitrate_stats =
642 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
643 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700644 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
645 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100646 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
647 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800648 }
649}
650
651void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700652 if (first_received_rtp_audio_ms_) {
653 RTC_HISTOGRAM_COUNTS_100000(
654 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
655 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
656 }
657 if (first_received_rtp_video_ms_) {
658 RTC_HISTOGRAM_COUNTS_100000(
659 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
660 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
661 }
asapersson250fd972016-09-08 00:07:21 -0700662 const int kMinRequiredPeriodicSamples = 5;
663 AggregatedStats video_bytes_per_sec =
664 received_video_bytes_per_second_counter_.GetStats();
665 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700666 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
667 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100668 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
669 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800670 }
asapersson250fd972016-09-08 00:07:21 -0700671 AggregatedStats audio_bytes_per_sec =
672 received_audio_bytes_per_second_counter_.GetStats();
673 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700674 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
675 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100676 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
677 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800678 }
asapersson250fd972016-09-08 00:07:21 -0700679 AggregatedStats rtcp_bytes_per_sec =
680 received_rtcp_bytes_per_second_counter_.GetStats();
681 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700682 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
683 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100684 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
685 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800686 }
asapersson250fd972016-09-08 00:07:21 -0700687 AggregatedStats recv_bytes_per_sec =
688 received_bytes_per_second_counter_.GetStats();
689 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700690 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
691 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100692 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
693 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700694 }
stefan91d92602015-11-11 10:13:02 -0800695}
696
solenberg5a289392015-10-19 03:39:20 -0700697PacketReceiver* Call::Receiver() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200698 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700699 return this;
700}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000701
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200702webrtc::AudioSendStream* Call::CreateAudioSendStream(
703 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700704 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200705 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800706
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700707 RTC_DCHECK_EQ(media_transport(),
708 config.media_transport_config.media_transport);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800709
710 RegisterRateObserver();
711
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100712 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
713 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200714 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700715 {
716 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
717 if (iter != suspended_audio_send_ssrcs_.end()) {
718 suspended_rtp_state.emplace(iter->second);
719 }
720 }
721
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100722 AudioSendStream* send_stream =
723 new AudioSendStream(clock_, config, config_.audio_state,
724 task_queue_factory_, module_process_thread_.get(),
725 transport_send_ptr_, bitrate_allocator_.get(),
726 event_log_, call_stats_.get(), suspended_rtp_state);
solenbergc7a8b082015-10-16 14:35:07 -0700727 {
solenbergc7a8b082015-10-16 14:35:07 -0700728 WriteLockScoped write_lock(*send_crit_);
729 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
730 audio_send_ssrcs_.end());
731 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700732 }
solenberg7602aab2016-11-14 11:30:07 -0800733 {
734 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700735 for (AudioReceiveStream* stream : audio_receive_streams_) {
736 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
737 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800738 }
739 }
740 }
skvlad7a43d252016-03-22 15:32:27 -0700741 send_stream->SignalNetworkState(audio_network_state_);
742 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700743 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200744}
745
746void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700747 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200748 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700749 RTC_DCHECK(send_stream != nullptr);
750
751 send_stream->Stop();
752
eladalonabbc4302017-07-26 02:09:44 -0700753 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700754 webrtc::internal::AudioSendStream* audio_send_stream =
755 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700756 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700757 {
758 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800759 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
760 RTC_DCHECK_EQ(1, num_deleted);
761 }
762 {
763 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700764 for (AudioReceiveStream* stream : audio_receive_streams_) {
765 if (stream->config().rtp.local_ssrc == ssrc) {
766 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800767 }
768 }
solenbergc7a8b082015-10-16 14:35:07 -0700769 }
skvlad7a43d252016-03-22 15:32:27 -0700770 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700771 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200772}
773
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200774webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
775 const webrtc::AudioReceiveStream::Config& config) {
776 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200777 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800778 RegisterRateObserver();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200779 event_log_->Log(absl::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200780 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700781 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100782 clock_, &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100783 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200784 {
785 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100786 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
787 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700788 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800789
pbos8fc7fa72015-07-15 08:02:58 -0700790 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200791 }
solenberg7602aab2016-11-14 11:30:07 -0800792 {
793 ReadLockScoped read_lock(*send_crit_);
794 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
795 if (it != audio_send_ssrcs_.end()) {
796 receive_stream->AssociateSendStream(it->second);
797 }
798 }
skvlad7a43d252016-03-22 15:32:27 -0700799 receive_stream->SignalNetworkState(audio_network_state_);
800 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200801 return receive_stream;
802}
803
804void Call::DestroyAudioReceiveStream(
805 webrtc::AudioReceiveStream* receive_stream) {
806 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200807 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700808 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700809 webrtc::internal::AudioReceiveStream* audio_receive_stream =
810 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200811 {
812 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800813 const AudioReceiveStream::Config& config = audio_receive_stream->config();
814 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700815 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800816 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700817 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700818 const std::string& sync_group = audio_receive_stream->config().sync_group;
819 const auto it = sync_stream_mapping_.find(sync_group);
820 if (it != sync_stream_mapping_.end() &&
821 it->second == audio_receive_stream) {
822 sync_stream_mapping_.erase(it);
823 ConfigureSync(sync_group);
824 }
nissed44ce052017-02-06 02:23:00 -0800825 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200826 }
skvlad7a43d252016-03-22 15:32:27 -0700827 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200828 delete audio_receive_stream;
829}
830
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100831// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100832webrtc::VideoSendStream* Call::CreateVideoSendStream(
833 webrtc::VideoSendStream::Config config,
834 VideoEncoderConfig encoder_config,
835 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000836 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200837 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000838
Niels Möller46879152019-01-07 15:54:47 +0100839 RTC_DCHECK(media_transport() == config.media_transport);
840
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800841 RegisterRateObserver();
842
asapersson35151f32016-05-02 23:44:01 -0700843 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700844 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
845 ++ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200846 event_log_->Log(absl::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200847 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700848 }
perkj26091b12016-09-01 01:17:40 -0700849
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000850 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
851 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700852 // Copy ssrcs from |config| since |config| is moved.
853 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100854
mflodman0c478b32015-10-21 15:52:16 +0200855 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Jansson0b698262019-03-07 09:17:19 +0100856 clock_, num_cpu_cores_, module_process_thread_.get(), task_queue_factory_,
Sebastian Jansson74682c12019-03-01 11:50:20 +0100857 call_stats_.get(), transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700858 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200859 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200860 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700861
skvlad7a43d252016-03-22 15:32:27 -0700862 {
863 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700864 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700865 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
866 video_send_ssrcs_[ssrc] = send_stream;
867 }
868 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000869 }
skvlad7a43d252016-03-22 15:32:27 -0700870 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700871
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000872 return send_stream;
873}
874
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100875webrtc::VideoSendStream* Call::CreateVideoSendStream(
876 webrtc::VideoSendStream::Config config,
877 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100878 if (config_.fec_controller_factory) {
879 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
880 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100881 std::unique_ptr<FecController> fec_controller =
882 config_.fec_controller_factory
883 ? config_.fec_controller_factory->CreateFecController()
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100884 : absl::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100885 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
886 std::move(fec_controller));
887}
888
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000889void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000890 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700891 RTC_DCHECK(send_stream != nullptr);
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200892 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000893
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000894 send_stream->Stop();
895
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000896 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000897 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000898 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200899 auto it = video_send_ssrcs_.begin();
900 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000901 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
902 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200903 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000904 } else {
905 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000906 }
907 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200908 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000909 }
henrikg91d6ede2015-09-17 00:24:34 -0700910 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000911
Åsa Persson4bece9a2017-10-06 10:04:04 +0200912 VideoSendStream::RtpStateMap rtp_states;
913 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
914 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
915 &rtp_payload_states);
916 for (const auto& kv : rtp_states) {
917 suspended_video_send_ssrcs_[kv.first] = kv.second;
918 }
919 for (const auto& kv : rtp_payload_states) {
920 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000921 }
922
skvlad7a43d252016-03-22 15:32:27 -0700923 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000924 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000925}
926
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200927webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200928 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000929 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200930 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800931
Johannes Kronf59666b2019-04-08 12:57:06 +0200932 receive_side_cc_.SetSendPeriodicFeedback(
933 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +0100934
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800935 RegisterRateObserver();
936
nisse0f15f922017-06-21 01:05:22 -0700937 VideoReceiveStream* receive_stream = new VideoReceiveStream(
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100938 task_queue_factory_, &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200939 transport_send_ptr_->packet_router(), std::move(configuration),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100940 module_process_thread_.get(), call_stats_.get(), clock_);
Tommi733b5472016-06-10 17:58:01 +0200941
942 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700943 {
944 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800945 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800946 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700947 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800948 // type, we may get an incorrect value for the rtx stream, but
949 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100950 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
951 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800952 }
Erik Språng09708512018-03-14 15:16:50 +0100953 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
954 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700955 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700956 ConfigureSync(config.sync_group);
957 }
958 receive_stream->SignalNetworkState(video_network_state_);
959 UpdateAggregateNetworkState();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200960 event_log_->Log(absl::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200961 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000962 return receive_stream;
963}
964
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000965void Call::DestroyVideoReceiveStream(
966 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000967 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200968 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700969 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700970 VideoReceiveStream* receive_stream_impl =
971 static_cast<VideoReceiveStream*>(receive_stream);
972 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000973 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000974 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000975 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
976 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700977 receive_rtp_config_.erase(config.rtp.remote_ssrc);
978 if (config.rtp.rtx_ssrc) {
979 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000980 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200981 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700982 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000983 }
nisse4709e892017-02-07 01:18:43 -0800984
nisse559af382017-03-21 06:41:12 -0700985 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800986 ->RemoveStream(config.rtp.remote_ssrc);
987
skvlad7a43d252016-03-22 15:32:27 -0700988 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000989 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000990}
991
brandtr7250b392016-12-19 01:13:46 -0800992FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
993 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700994 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200995 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800996
997 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700998
nisse0f15f922017-06-21 01:05:22 -0700999 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001000 {
1001 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -07001002 // Unlike the video and audio receive streams,
1003 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
1004 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -07001005 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -07001006 // constructor while holding |receive_crit_| ensures that we don't
1007 // call OnRtpPacket until the constructor is finished and the
1008 // object is in a valid state.
1009 // TODO(nisse): Fix constructor so that it can be moved outside of
1010 // this locked scope.
1011 receive_stream = new FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 19:39:01 +01001012 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +02001013 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -08001014
nissed44ce052017-02-06 02:23:00 -08001015 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1016 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +01001017 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -07001018 }
brandtrb29e6522016-12-21 06:37:18 -08001019
brandtr25445d32016-10-23 23:37:14 -07001020 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001021
brandtr25445d32016-10-23 23:37:14 -07001022 return receive_stream;
1023}
1024
brandtr7250b392016-12-19 01:13:46 -08001025void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001026 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001027 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -08001028
brandtr25445d32016-10-23 23:37:14 -07001029 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -07001030 {
1031 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -08001032
eladalon42f44f92017-07-25 06:40:06 -07001033 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -08001034 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -08001035 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001036
brandtr7250b392016-12-19 01:13:46 -08001037 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1038 // destroyed.
nisse559af382017-03-21 06:41:12 -07001039 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -08001040 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -07001041 }
brandtrb29e6522016-12-21 06:37:18 -08001042
eladalon42f44f92017-07-25 06:40:06 -07001043 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001044}
1045
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001046RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +02001047 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001048}
1049
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001050Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -07001051 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
1052 // thread. Re-enable once that is fixed.
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001053 // RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001054 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +02001055 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001056 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001057 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001058 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001059 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001060
1061 {
1062 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1063 stats.send_bandwidth_bps = last_bandwidth_bps_;
1064 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001065 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001066 // TODO(srte): It is unclear if we only want to report queues if network is
1067 // available.
1068 {
1069 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 14:08:15 +02001070 stats.pacer_delay_ms = aggregate_network_up_
1071 ? transport_send_ptr_->GetPacerQueuingDelayMs()
1072 : 0;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001073 }
1074
Tommi38c5d932018-03-27 23:11:09 +02001075 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -07001076 {
1077 rtc::CritScope cs(&bitrate_crit_);
1078 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
1079 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001080 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001081}
1082
Alex Narest78609d52017-10-20 10:37:47 +02001083void Call::SetBitrateAllocationStrategy(
1084 std::unique_ptr<rtc::BitrateAllocationStrategy>
1085 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001086 // TODO(srte): This function should be moved to RtpTransportControllerSend
1087 // when BitrateAllocator is moved there.
1088 struct Functor {
1089 void operator()() {
1090 bitrate_allocator_->SetBitrateAllocationStrategy(
1091 std::move(bitrate_allocation_strategy_));
1092 }
1093 BitrateAllocator* bitrate_allocator_;
1094 std::unique_ptr<rtc::BitrateAllocationStrategy>
1095 bitrate_allocation_strategy_;
1096 };
1097 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
1098 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 10:37:47 +02001099}
1100
skvlad7a43d252016-03-22 15:32:27 -07001101void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001102 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001103 switch (media) {
1104 case MediaType::AUDIO:
1105 audio_network_state_ = state;
1106 break;
1107 case MediaType::VIDEO:
1108 video_network_state_ = state;
1109 break;
1110 case MediaType::ANY:
1111 case MediaType::DATA:
1112 RTC_NOTREACHED();
1113 break;
1114 }
1115
1116 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001117 {
skvlad7a43d252016-03-22 15:32:27 -07001118 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -07001119 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001120 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -07001121 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001122 }
1123 {
skvlad7a43d252016-03-22 15:32:27 -07001124 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001125 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1126 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001127 }
nissee4bcd6d2017-05-16 04:47:04 -07001128 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1129 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001130 }
1131 }
1132}
1133
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001134void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1135 ReadLockScoped read_lock(*send_crit_);
1136 for (auto& kv : audio_send_ssrcs_) {
1137 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -08001138 }
1139}
1140
skvlad7a43d252016-03-22 15:32:27 -07001141void Call::UpdateAggregateNetworkState() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001142 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001143
1144 bool have_audio = false;
1145 bool have_video = false;
1146 {
1147 ReadLockScoped read_lock(*send_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001148 if (!audio_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001149 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001150 if (!video_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001151 have_video = true;
1152 }
1153 {
1154 ReadLockScoped read_lock(*receive_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001155 if (!audio_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001156 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001157 if (!video_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001158 have_video = true;
1159 }
1160
Sebastian Janssona06e9192018-03-07 18:49:55 +01001161 bool aggregate_network_up =
1162 ((have_video && video_network_state_ == kNetworkUp) ||
1163 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001164
Mirko Bonadei675513b2017-11-09 11:09:25 +01001165 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001166 << (aggregate_network_up ? "up" : "down");
1167 {
1168 rtc::CritScope cs(&aggregate_network_up_crit_);
1169 aggregate_network_up_ = aggregate_network_up;
1170 }
Sebastian Janssone6256052018-05-04 14:08:15 +02001171 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001172}
1173
stefanc1aeaf02015-10-15 07:26:07 -07001174void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001175 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1176 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001177 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001178}
1179
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001180void Call::OnStartRateUpdate(DataRate start_rate) {
1181 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1182 transport_send_ptr_->GetWorkerQueue()->PostTask(
1183 [this, start_rate] { this->OnStartRateUpdate(start_rate); });
1184 return;
1185 }
1186 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1187}
1188
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001189void Call::OnTargetTransferRate(TargetTransferRate msg) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001190 // TODO(bugs.webrtc.org/9719)
1191 // Call::OnTargetTransferRate requires that on target transfer rate is invoked
1192 // from the worker queue (because bitrate_allocator_ requires it). Media
1193 // transport does not guarantee the callback on the worker queue.
1194 // When the threading model for MediaTransportInterface is update, reconsider
1195 // changing this implementation.
1196 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1197 transport_send_ptr_->GetWorkerQueue()->PostTask(
1198 [this, msg] { this->OnTargetTransferRate(msg); });
1199 return;
1200 }
1201
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001202 uint32_t target_bitrate_bps = msg.target_rate.bps();
1203 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1204 uint8_t fraction_loss =
1205 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1206 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1207 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1208 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1209 {
1210 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1211 last_bandwidth_bps_ = bandwidth_bps;
1212 }
nisse559af382017-03-21 06:41:12 -07001213 // For controlling the rate of feedback messages.
1214 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +01001215 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, bandwidth_bps,
1216 fraction_loss, rtt_ms,
1217 probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001218
asaperssonce2e1362016-09-09 00:13:35 -07001219 // Ignore updates if bitrate is zero (the aggregate network state is down).
1220 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001221 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001222 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1223 pacer_bitrate_kbps_counter_.ProcessAndPause();
1224 return;
stefan18adf0a2015-11-17 06:24:56 -08001225 }
asaperssonce2e1362016-09-09 00:13:35 -07001226
1227 bool sending_video;
1228 {
1229 ReadLockScoped read_lock(*send_crit_);
1230 sending_video = !video_send_streams_.empty();
1231 }
1232
1233 rtc::CritScope lock(&bitrate_crit_);
1234 if (!sending_video) {
1235 // Do not update the stats if we are not sending video.
1236 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1237 pacer_bitrate_kbps_counter_.ProcessAndPause();
1238 return;
1239 }
1240 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1241 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1242 uint32_t pacer_bitrate_bps =
1243 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1244 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001245}
mflodman101f2502016-06-09 17:21:19 +02001246
perkj71ee44c2016-06-15 00:47:53 -07001247void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001248 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +01001249 uint32_t total_bitrate_bps) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001250 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001251 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001252
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -08001253 {
1254 rtc::CritScope lock(&target_observer_crit_);
1255 if (media_transport_) {
1256 MediaTransportAllocatedBitrateLimits limits;
1257 limits.min_pacing_rate = DataRate::bps(min_send_bitrate_bps);
1258 limits.max_padding_bitrate = DataRate::bps(max_padding_bitrate_bps);
1259 limits.max_total_allocated_bitrate = DataRate::bps(total_bitrate_bps);
1260 media_transport_->SetAllocatedBitrateLimits(limits);
1261 }
1262 }
1263
perkj71ee44c2016-06-15 00:47:53 -07001264 rtc::CritScope lock(&bitrate_crit_);
1265 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001266 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001267}
1268
pbos8fc7fa72015-07-15 08:02:58 -07001269void Call::ConfigureSync(const std::string& sync_group) {
1270 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001271 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001272 return;
1273
1274 AudioReceiveStream* sync_audio_stream = nullptr;
1275 // Find existing audio stream.
1276 const auto it = sync_stream_mapping_.find(sync_group);
1277 if (it != sync_stream_mapping_.end()) {
1278 sync_audio_stream = it->second;
1279 } else {
1280 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001281 for (AudioReceiveStream* stream : audio_receive_streams_) {
1282 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001283 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001284 RTC_LOG(LS_WARNING)
1285 << "Attempting to sync more than one audio stream "
1286 "within the same sync group. This is not "
1287 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001288 break;
1289 }
nissee4bcd6d2017-05-16 04:47:04 -07001290 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001291 }
1292 }
1293 }
1294 if (sync_audio_stream)
1295 sync_stream_mapping_[sync_group] = sync_audio_stream;
1296 size_t num_synced_streams = 0;
1297 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1298 if (video_stream->config().sync_group != sync_group)
1299 continue;
1300 ++num_synced_streams;
1301 if (num_synced_streams > 1) {
1302 // TODO(pbos): Support synchronizing more than one A/V pair.
1303 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001304 RTC_LOG(LS_WARNING)
1305 << "Attempting to sync more than one audio/video pair "
1306 "within the same sync group. This is not supported in "
1307 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001308 }
1309 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001310 if (num_synced_streams == 1) {
1311 // sync_audio_stream may be null and that's ok.
1312 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001313 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001314 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001315 }
1316 }
1317}
1318
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001319PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1320 const uint8_t* packet,
1321 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001322 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001323 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001324 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1325 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001326 if (received_bytes_per_second_counter_.HasSample()) {
1327 // First RTP packet has been received.
1328 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1329 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1330 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001331 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001332 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001333 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001334 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001335 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001336 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001337 }
1338 }
1339 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1340 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001341 for (AudioReceiveStream* stream : audio_receive_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001342 stream->DeliverRtcp(packet, length);
1343 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001344 }
1345 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001346 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001347 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001348 for (VideoSendStream* stream : video_send_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001349 stream->DeliverRtcp(packet, length);
1350 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001351 }
1352 }
mflodman3d7db262016-04-29 00:57:13 -07001353 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1354 ReadLockScoped read_lock(*send_crit_);
1355 for (auto& kv : audio_send_ssrcs_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001356 kv.second->DeliverRtcp(packet, length);
1357 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001358 }
1359 }
1360
Elad Alon4a87e1c2017-10-03 16:11:34 +02001361 if (rtcp_delivered) {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001362 event_log_->Log(absl::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001363 rtc::MakeArrayView(packet, length)));
1364 }
mflodman3d7db262016-04-29 00:57:13 -07001365
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001366 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001367}
1368
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001369PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001370 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001371 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001372 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001373
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001374 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001375 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001376 return DELIVERY_PACKET_ERROR;
1377
Niels Möller70082872018-08-07 11:03:12 +02001378 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001379 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001380 // Repair packet_time_us for clock resets by comparing a new read of
1381 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001382 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001383 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001384 }
Niels Möller70082872018-08-07 11:03:12 +02001385 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001386 } else {
1387 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1388 }
nissed44ce052017-02-06 02:23:00 -08001389
sprangc1abde72017-07-11 03:56:21 -07001390 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1391 // These are empty (zero length payload) RTP packets with an unsignaled
1392 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001393 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001394
1395 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1396 is_keep_alive_packet);
1397
sprangc1abde72017-07-11 03:56:21 -07001398 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001399 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001400 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001401 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1402 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001403 // Destruction of the receive stream, including deregistering from the
1404 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1405 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1406 // So by not passing the packet on to demuxing in this case, we prevent
1407 // incoming packets to be passed on via the demuxer to a receive stream
1408 // which is being torned down.
1409 return DELIVERY_UNKNOWN_SSRC;
1410 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001411
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001412 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001413
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001414 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001415
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001416 // RateCounters expect input parameter as int, save it as int,
1417 // instead of converting each time it is passed to RateCounter::Add below.
1418 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001419 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001420 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001421 received_bytes_per_second_counter_.Add(length);
1422 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001423 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001424 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001425 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001426 if (!first_received_rtp_audio_ms_) {
1427 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1428 }
1429 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001430 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001431 }
nissee4bcd6d2017-05-16 04:47:04 -07001432 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001433 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001434 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001435 received_bytes_per_second_counter_.Add(length);
1436 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001437 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001438 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001439 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001440 if (!first_received_rtp_video_ms_) {
1441 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1442 }
1443 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001444 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001445 }
1446 }
1447 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001448}
1449
stefan68786d22015-09-08 05:36:15 -07001450PacketReceiver::DeliveryStatus Call::DeliverPacket(
1451 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001452 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001453 int64_t packet_time_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001454 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001455 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1456 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001457
Niels Möller70082872018-08-07 11:03:12 +02001458 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001459}
1460
nissed2ef3142017-05-11 08:00:58 -07001461void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001462 RtpPacketReceived parsed_packet;
1463 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001464 return;
1465
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001466 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001467
brandtrcaea68f2017-08-23 00:55:17 -07001468 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001469 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001470 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001471 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1472 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001473 // Destruction of the receive stream, including deregistering from the
1474 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1475 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1476 // So by not passing the packet on to demuxing in this case, we prevent
1477 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001478 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001479 return;
1480 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001481 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001482
1483 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001484 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001485 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001486}
1487
nissed44ce052017-02-06 02:23:00 -08001488void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1489 MediaType media_type) {
1490 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001491 bool use_send_side_bwe =
1492 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001493
brandtrb29e6522016-12-21 06:37:18 -08001494 RTPHeader header;
1495 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001496
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001497 ReceivedPacket packet_msg;
1498 packet_msg.size = DataSize::bytes(packet.payload_size());
1499 packet_msg.receive_time = Timestamp::ms(packet.arrival_time_ms());
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001500 if (header.extension.hasAbsoluteSendTime) {
1501 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1502 }
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001503 transport_send_ptr_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001504
nisse4709e892017-02-07 01:18:43 -08001505 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001506 // Inconsistent configuration of send side BWE. Do nothing.
1507 // TODO(nisse): Without this check, we may produce RTCP feedback
1508 // packets even when not negotiated. But it would be cleaner to
1509 // move the check down to RTCPSender::SendFeedbackPacket, which
1510 // would also help the PacketRouter to select an appropriate rtp
1511 // module in the case that some, but not all, have RTCP feedback
1512 // enabled.
1513 return;
1514 }
1515 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001516 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001517 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001518 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001519 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1520 header);
1521 }
brandtrb29e6522016-12-21 06:37:18 -08001522}
1523
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001524} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001525
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001526} // namespace webrtc