blob: 940c1a396479bbd2536da30966cfb5d88407d827 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "call/call.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
mflodman101f2502016-06-09 17:21:19 +020015#include <algorithm>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000016#include <map>
kwibergb25345e2016-03-12 06:10:44 -080017#include <memory>
ossuf515ab82016-12-07 04:52:58 -080018#include <set>
brandtr25445d32016-10-23 23:37:14 -070019#include <utility>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000020#include <vector>
21
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020022#include "absl/types/optional.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020023#include "api/rtc_event_log/rtc_event_log.h"
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"
Elad Alon99a81b62017-09-21 10:25:29 +020038#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
40#include "modules/rtp_rtcp/include/flexfec_receiver.h"
41#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "modules/rtp_rtcp/source/byte_io.h"
43#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Tommi25eb47c2019-08-29 16:39:05 +020044#include "modules/rtp_rtcp/source/rtp_utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010046#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "rtc_base/location.h"
50#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 15:59:12 +010051#include "rtc_base/numerics/safe_minmax.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020052#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020053#include "rtc_base/synchronization/rw_lock_wrapper.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020054#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080056#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "rtc_base/trace_event.h"
58#include "system_wrappers/include/clock.h"
59#include "system_wrappers/include/cpu_info.h"
Jonas Oreland6d835922019-03-18 10:59:40 +010060#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "video/call_stats.h"
63#include "video/send_delay_stats.h"
64#include "video/stats_counter.h"
65#include "video/video_receive_stream.h"
66#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000067
68namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000069
nisse4709e892017-02-07 01:18:43 -080070namespace {
Johannes Kronf59666b2019-04-08 12:57:06 +020071bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010072 for (const auto& extension : extensions) {
73 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
Johannes Kronf59666b2019-04-08 12:57:06 +020074 return false;
Johannes Kron7ff164e2019-02-07 12:50:18 +010075 }
Johannes Kronf59666b2019-04-08 12:57:06 +020076 return true;
Johannes Kron7ff164e2019-02-07 12:50:18 +010077}
78
nisse4709e892017-02-07 01:18:43 -080079// TODO(nisse): This really begs for a shared context struct.
80bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
81 bool transport_cc) {
82 if (!transport_cc)
83 return false;
84 for (const auto& extension : extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010085 if (extension.uri == RtpExtension::kTransportSequenceNumberUri ||
86 extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
nisse4709e892017-02-07 01:18:43 -080087 return true;
88 }
89 return false;
90}
91
92bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
93 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
94}
95
96bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
97 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
98}
99
100bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
101 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
102}
103
nisse26e3abb2017-08-25 04:44:25 -0700104const int* FindKeyByValue(const std::map<int, int>& m, int v) {
105 for (const auto& kv : m) {
106 if (kv.second == v)
107 return &kv.first;
108 }
109 return nullptr;
110}
111
eladalon8ec568a2017-09-08 06:15:52 -0700112std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700113 const VideoReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200114 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700115 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
116 rtclog_config->local_ssrc = config.rtp.local_ssrc;
117 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
118 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
eladalon8ec568a2017-09-08 06:15:52 -0700119 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700120
121 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700122 const int* search =
123 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200124 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200125 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700126 }
127 return rtclog_config;
128}
129
eladalon8ec568a2017-09-08 06:15:52 -0700130std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700131 const VideoSendStream::Config& config,
132 size_t ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200133 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700134 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700135 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700136 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700137 }
eladalon8ec568a2017-09-08 06:15:52 -0700138 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
139 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700140
Niels Möller259a4972018-04-05 15:36:51 +0200141 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
142 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700143 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700144 return rtclog_config;
145}
146
eladalon8ec568a2017-09-08 06:15:52 -0700147std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700148 const AudioReceiveStream::Config& config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200149 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700150 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
151 rtclog_config->local_ssrc = config.rtp.local_ssrc;
152 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700153 return rtclog_config;
154}
155
Tommi25eb47c2019-08-29 16:39:05 +0200156bool IsRtcp(const uint8_t* packet, size_t length) {
157 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
158 return rtp_parser.RTCP();
159}
160
nisse4709e892017-02-07 01:18:43 -0800161} // namespace
162
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000163namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000164
Sebastian Janssone6256052018-05-04 14:08:15 +0200165class Call final : public webrtc::Call,
166 public PacketReceiver,
167 public RecoveredPacketReceiver,
168 public TargetTransferRateObserver,
169 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000170 public:
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100171 Call(Clock* clock,
172 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100173 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
174 std::unique_ptr<ProcessThread> module_process_thread,
175 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200176 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000177
brandtr25445d32016-10-23 23:37:14 -0700178 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000179 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000180
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200181 webrtc::AudioSendStream* CreateAudioSendStream(
182 const webrtc::AudioSendStream::Config& config) override;
183 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
184
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200185 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
186 const webrtc::AudioReceiveStream::Config& config) override;
187 void DestroyAudioReceiveStream(
188 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000189
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200190 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700191 webrtc::VideoSendStream::Config config,
192 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100193 webrtc::VideoSendStream* CreateVideoSendStream(
194 webrtc::VideoSendStream::Config config,
195 VideoEncoderConfig encoder_config,
196 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000197 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000198
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200199 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200200 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000201 void DestroyVideoReceiveStream(
202 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000203
brandtr7250b392016-12-19 01:13:46 -0800204 FlexfecReceiveStream* CreateFlexfecReceiveStream(
205 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700206 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800207 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700208
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100209 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
210
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000211 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000212
brandtr25445d32016-10-23 23:37:14 -0700213 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700214 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100215 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200216 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000217
brandtr4e523862016-10-18 23:50:45 -0700218 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700219 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700220
skvlad7a43d252016-03-22 15:32:27 -0700221 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000222
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200223 void OnAudioTransportOverheadChanged(
224 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800225
stefanc1aeaf02015-10-15 07:26:07 -0700226 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
227
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100228 // Implements TargetTransferRateObserver,
229 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100230 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800231
perkj71ee44c2016-06-15 00:47:53 -0700232 // Implements BitrateAllocator::LimitObserver.
233 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +0100234 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100235 uint32_t total_bitrate_bps) override;
perkj71ee44c2016-06-15 00:47:53 -0700236
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700237 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
238
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000239 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200240 DeliveryStatus DeliverRtcp(MediaType media_type,
241 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200242 size_t length);
stefan68786d22015-09-08 05:36:15 -0700243 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100244 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200245 int64_t packet_time_us);
pbos8fc7fa72015-07-15 08:02:58 -0700246 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700247 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700248
nissed44ce052017-02-06 02:23:00 -0800249 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
250 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700251 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800252
Erik Språng425d6aa2019-07-29 16:38:27 +0200253 void UpdateSendHistograms(Timestamp first_sent_packet)
danilchapa37de392017-09-09 04:17:22 -0700254 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800255 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700256 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700257 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800258
Tommi78a71382019-08-08 12:27:53 +0200259 void RegisterRateObserver();
Niels Möller46879152019-01-07 15:54:47 +0100260
Tommi48b48e52019-08-09 11:42:32 +0200261 rtc::TaskQueue* network_queue() const {
262 return transport_send_ptr_->GetWorkerQueue();
263 }
264
Peter Boströmd3c94472015-12-09 11:20:58 +0100265 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100266 TaskQueueFactory* const task_queue_factory_;
stefan91d92602015-11-11 10:13:02 -0800267
Peter Boström45553ae2015-05-08 13:54:38 +0200268 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800269 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800270 const std::unique_ptr<CallStats> call_stats_;
271 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000272 Call::Config config_;
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200273 SequenceChecker configuration_sequence_checker_;
Tommi78a71382019-08-08 12:27:53 +0200274 SequenceChecker worker_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000275
skvlad7a43d252016-03-22 15:32:27 -0700276 NetworkState audio_network_state_;
277 NetworkState video_network_state_;
Tommi48b48e52019-08-09 11:42:32 +0200278 bool aggregate_network_up_ RTC_GUARDED_BY(configuration_sequence_checker_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000279
kwibergb25345e2016-03-12 06:10:44 -0800280 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700281 // Audio, Video, and FlexFEC receive streams are owned by the client that
282 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700283 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700284 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200285 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700286 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700287
pbos8fc7fa72015-07-15 08:02:58 -0700288 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700289 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000290
nisse0f15f922017-06-21 01:05:22 -0700291 // TODO(nisse): Should eventually be injected at creation,
292 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700293 RtpStreamReceiverController audio_receiver_controller_;
294 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700295
nissed44ce052017-02-06 02:23:00 -0800296 // This extra map is used for receive processing which is
297 // independent of media type.
298
299 // TODO(nisse): In the RTP transport refactoring, we should have a
300 // single mapping from ssrc to a more abstract receive stream, with
301 // accessor methods for all configuration we need at this level.
302 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100303 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
304 : extensions(config.rtp.extensions),
305 use_send_side_bwe(UseSendSideBwe(config)) {}
306 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
307 : extensions(config.rtp.extensions),
308 use_send_side_bwe(UseSendSideBwe(config)) {}
309 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
310 : extensions(config.rtp_header_extensions),
311 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800312
313 // Registered RTP header extensions for each stream. Note that RTP header
314 // extensions are negotiated per track ("m= line") in the SDP, but we have
315 // no notion of tracks at the Call level. We therefore store the RTP header
316 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100317 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800318 // Set if both RTP extension the RTCP feedback message needed for
319 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100320 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800321 };
322 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700323 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800324
kwibergb25345e2016-03-12 06:10:44 -0800325 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700326 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700327 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
328 RTC_GUARDED_BY(send_crit_);
329 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
330 RTC_GUARDED_BY(send_crit_);
331 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000332
ossuc3d4b482017-05-23 06:07:11 -0700333 using RtpStateMap = std::map<uint32_t, RtpState>;
334 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700335 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700336 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700337 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700338
Åsa Persson4bece9a2017-10-06 10:04:04 +0200339 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
340 RtpPayloadStateMap suspended_video_payload_states_
341 RTC_GUARDED_BY(configuration_sequence_checker_);
342
skvlad11a9cbf2016-10-07 11:53:05 -0700343 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700344
stefan18adf0a2015-11-17 06:24:56 -0800345 // The following members are only accessed (exclusively) from one thread and
346 // from the destructor, and therefore doesn't need any explicit
347 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700348 RateCounter received_bytes_per_second_counter_;
349 RateCounter received_audio_bytes_per_second_counter_;
350 RateCounter received_video_bytes_per_second_counter_;
351 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200352 absl::optional<int64_t> first_received_rtp_audio_ms_;
353 absl::optional<int64_t> last_received_rtp_audio_ms_;
354 absl::optional<int64_t> first_received_rtp_video_ms_;
355 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 10:13:02 -0800356
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100357 rtc::CriticalSection last_bandwidth_bps_crit_;
358 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800359 // TODO(holmer): Remove this lock once BitrateController no longer calls
360 // OnNetworkChanged from multiple threads.
361 rtc::CriticalSection bitrate_crit_;
Tommi78a71382019-08-08 12:27:53 +0200362 uint32_t min_allocated_send_bitrate_bps_
363 RTC_GUARDED_BY(&worker_sequence_checker_);
danilchapa37de392017-09-09 04:17:22 -0700364 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
365 AvgCounter estimated_send_bitrate_kbps_counter_
366 RTC_GUARDED_BY(&bitrate_crit_);
367 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800368
nisse559af382017-03-21 06:41:12 -0700369 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100370
371 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
372
asapersson35151f32016-05-02 23:44:01 -0700373 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700374 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800375
Sebastian Janssone6256052018-05-04 14:08:15 +0200376 // Caches transport_send_.get(), to avoid racing with destructor.
377 // Note that this is declared before transport_send_ to ensure that it is not
378 // invalidated until no more tasks can be running on the transport_send_ task
379 // queue.
Tommi78a71382019-08-08 12:27:53 +0200380 RtpTransportControllerSendInterface* const transport_send_ptr_;
Sebastian Janssone6256052018-05-04 14:08:15 +0200381 // Declared last since it will issue callbacks from a task queue. Declaring it
382 // last ensures that it is destroyed first and any running tasks are finished.
383 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800384
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800385 bool is_target_rate_observer_registered_
Tommi78a71382019-08-08 12:27:53 +0200386 RTC_GUARDED_BY(&configuration_sequence_checker_) = false;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800387
henrikg3c089d72015-09-16 05:37:44 -0700388 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000389};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000390} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000391
asapersson2e5cfcd2016-08-11 08:41:18 -0700392std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200393 char buf[1024];
394 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700395 ss << "Call stats: " << time_ms << ", {";
396 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
397 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
398 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
399 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
400 ss << "rtt_ms: " << rtt_ms;
401 ss << '}';
402 return ss.str();
403}
404
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000405Call* Call::Create(const Call::Config& config) {
Danil Chapovalov359fe332019-04-01 10:46:36 +0200406 return Create(config, Clock::GetRealTimeClock(),
Erik Språng6950b302019-08-16 12:54:08 +0200407 ProcessThread::Create("ModuleProcessThread"),
408 ProcessThread::Create("PacerThread"));
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100409}
410
411Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100412 Clock* clock,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100413 std::unique_ptr<ProcessThread> call_thread,
Danil Chapovalov359fe332019-04-01 10:46:36 +0200414 std::unique_ptr<ProcessThread> pacer_thread) {
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200415 RTC_DCHECK(config.task_queue_factory);
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100416 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100417 clock, config,
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200418 std::make_unique<RtpTransportControllerSend>(
Ying Wang0810a7c2019-04-10 13:48:24 +0200419 clock, config.event_log, config.network_state_predictor_factory,
420 config.network_controller_factory, config.bitrate_config,
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200421 std::move(pacer_thread), config.task_queue_factory),
422 std::move(call_thread), config.task_queue_factory);
zstein7cb69d52017-05-08 11:52:38 -0700423}
424
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100425// This method here to avoid subclasses has to implement this method.
426// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
427// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100428VideoSendStream* Call::CreateVideoSendStream(
429 VideoSendStream::Config config,
430 VideoEncoderConfig encoder_config,
431 std::unique_ptr<FecController> fec_controller) {
432 return nullptr;
433}
434
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000435namespace internal {
436
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100437Call::Call(Clock* clock,
438 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100439 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
440 std::unique_ptr<ProcessThread> module_process_thread,
441 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100442 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100443 task_queue_factory_(task_queue_factory),
stefan91d92602015-11-11 10:13:02 -0800444 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100445 module_process_thread_(std::move(module_process_thread)),
Tommi38c5d932018-03-27 23:11:09 +0200446 call_stats_(new CallStats(clock_, module_process_thread_.get())),
Sebastian Janssonda6806c2019-03-04 17:05:12 +0100447 bitrate_allocator_(new BitrateAllocator(clock_, this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200448 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800449 audio_network_state_(kNetworkDown),
450 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100451 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000452 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800453 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700454 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700455 received_bytes_per_second_counter_(clock_, nullptr, true),
456 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
457 received_video_bytes_per_second_counter_(clock_, nullptr, true),
458 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100459 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700460 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700461 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700462 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
463 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700464 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100465 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700466 video_send_delay_stats_(new SendDelayStats(clock_)),
Tommi78a71382019-08-08 12:27:53 +0200467 start_ms_(clock_->TimeInMilliseconds()),
468 transport_send_ptr_(transport_send.get()),
469 transport_send_(std::move(transport_send)) {
skvlad11a9cbf2016-10-07 11:53:05 -0700470 RTC_DCHECK(config.event_log != nullptr);
Tommi78a71382019-08-08 12:27:53 +0200471 worker_sequence_checker_.Detach();
Tommi48b48e52019-08-09 11:42:32 +0200472
473 call_stats_->RegisterStatsObserver(&receive_side_cc_);
474
475 module_process_thread_->RegisterModule(
476 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
477 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
478 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000479}
480
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000481Call::~Call() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200482 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700483
solenbergc7a8b082015-10-16 14:35:07 -0700484 RTC_CHECK(audio_send_ssrcs_.empty());
485 RTC_CHECK(video_send_ssrcs_.empty());
486 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700487 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700488 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000489
Tommi48b48e52019-08-09 11:42:32 +0200490 module_process_thread_->Stop();
Tommi78a71382019-08-08 12:27:53 +0200491 module_process_thread_->DeRegisterModule(
492 receive_side_cc_.GetRemoteBitrateEstimator(true));
493 module_process_thread_->DeRegisterModule(&receive_side_cc_);
494 module_process_thread_->DeRegisterModule(call_stats_.get());
Tommi78a71382019-08-08 12:27:53 +0200495 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
sprang6d6122b2016-07-13 06:37:09 -0700496
Erik Språng425d6aa2019-07-29 16:38:27 +0200497 absl::optional<Timestamp> first_sent_packet_ms =
498 transport_send_->GetFirstPacketTime();
Tommi48b48e52019-08-09 11:42:32 +0200499
sprang6d6122b2016-07-13 06:37:09 -0700500 // Only update histograms after process threads have been shut down, so that
501 // they won't try to concurrently update stats.
Erik Språngaa59eca2019-07-24 14:52:55 +0200502 if (first_sent_packet_ms) {
perkj26091b12016-09-01 01:17:40 -0700503 rtc::CritScope lock(&bitrate_crit_);
Erik Språngaa59eca2019-07-24 14:52:55 +0200504 UpdateSendHistograms(*first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700505 }
Tommi48b48e52019-08-09 11:42:32 +0200506
sprang6d6122b2016-07-13 06:37:09 -0700507 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700508 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000509}
510
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800511void Call::RegisterRateObserver() {
Tommi78a71382019-08-08 12:27:53 +0200512 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800513
Tommi78a71382019-08-08 12:27:53 +0200514 if (is_target_rate_observer_registered_)
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800515 return;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800516
517 is_target_rate_observer_registered_ = true;
518
Tommi48b48e52019-08-09 11:42:32 +0200519 // This call seems to kick off a number of things, so probably better left
520 // off being kicked off on request rather than in the ctor.
Tommi78a71382019-08-08 12:27:53 +0200521 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800522
Tommi78a71382019-08-08 12:27:53 +0200523 module_process_thread_->Start();
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700524}
525
526void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
Tommi78a71382019-08-08 12:27:53 +0200527 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700528 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800529}
530
asapersson4374a092016-07-27 00:39:09 -0700531void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700532 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700533 "WebRTC.Call.LifetimeInSeconds",
534 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
535}
536
Tommi48b48e52019-08-09 11:42:32 +0200537// Called from the dtor.
Erik Språng425d6aa2019-07-29 16:38:27 +0200538void Call::UpdateSendHistograms(Timestamp first_sent_packet) {
stefan18adf0a2015-11-17 06:24:56 -0800539 int64_t elapsed_sec =
Erik Språng425d6aa2019-07-29 16:38:27 +0200540 (clock_->TimeInMilliseconds() - first_sent_packet.ms()) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800541 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
542 return;
asaperssonce2e1362016-09-09 00:13:35 -0700543 const int kMinRequiredPeriodicSamples = 5;
544 AggregatedStats send_bitrate_stats =
545 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
546 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700547 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
548 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100549 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
550 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800551 }
asaperssonce2e1362016-09-09 00:13:35 -0700552 AggregatedStats pacer_bitrate_stats =
553 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
554 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700555 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
556 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100557 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
558 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800559 }
560}
561
562void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700563 if (first_received_rtp_audio_ms_) {
564 RTC_HISTOGRAM_COUNTS_100000(
565 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
566 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
567 }
568 if (first_received_rtp_video_ms_) {
569 RTC_HISTOGRAM_COUNTS_100000(
570 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
571 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
572 }
asapersson250fd972016-09-08 00:07:21 -0700573 const int kMinRequiredPeriodicSamples = 5;
574 AggregatedStats video_bytes_per_sec =
575 received_video_bytes_per_second_counter_.GetStats();
576 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700577 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
578 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100579 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
580 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800581 }
asapersson250fd972016-09-08 00:07:21 -0700582 AggregatedStats audio_bytes_per_sec =
583 received_audio_bytes_per_second_counter_.GetStats();
584 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700585 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
586 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100587 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
588 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800589 }
asapersson250fd972016-09-08 00:07:21 -0700590 AggregatedStats rtcp_bytes_per_sec =
591 received_rtcp_bytes_per_second_counter_.GetStats();
592 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700593 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
594 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100595 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
596 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800597 }
asapersson250fd972016-09-08 00:07:21 -0700598 AggregatedStats recv_bytes_per_sec =
599 received_bytes_per_second_counter_.GetStats();
600 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700601 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
602 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100603 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
604 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700605 }
stefan91d92602015-11-11 10:13:02 -0800606}
607
solenberg5a289392015-10-19 03:39:20 -0700608PacketReceiver* Call::Receiver() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200609 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700610 return this;
611}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000612
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200613webrtc::AudioSendStream* Call::CreateAudioSendStream(
614 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700615 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200616 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800617
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800618 RegisterRateObserver();
619
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100620 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
621 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200622 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700623 {
624 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
625 if (iter != suspended_audio_send_ssrcs_.end()) {
626 suspended_rtp_state.emplace(iter->second);
627 }
628 }
629
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100630 AudioSendStream* send_stream =
631 new AudioSendStream(clock_, config, config_.audio_state,
632 task_queue_factory_, module_process_thread_.get(),
633 transport_send_ptr_, bitrate_allocator_.get(),
634 event_log_, call_stats_.get(), suspended_rtp_state);
solenbergc7a8b082015-10-16 14:35:07 -0700635 {
solenbergc7a8b082015-10-16 14:35:07 -0700636 WriteLockScoped write_lock(*send_crit_);
637 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
638 audio_send_ssrcs_.end());
639 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700640 }
solenberg7602aab2016-11-14 11:30:07 -0800641 {
642 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700643 for (AudioReceiveStream* stream : audio_receive_streams_) {
644 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
645 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800646 }
647 }
648 }
skvlad7a43d252016-03-22 15:32:27 -0700649 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700650 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200651}
652
653void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700654 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200655 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700656 RTC_DCHECK(send_stream != nullptr);
657
658 send_stream->Stop();
659
eladalonabbc4302017-07-26 02:09:44 -0700660 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700661 webrtc::internal::AudioSendStream* audio_send_stream =
662 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700663 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700664 {
665 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800666 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
667 RTC_DCHECK_EQ(1, num_deleted);
668 }
669 {
670 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700671 for (AudioReceiveStream* stream : audio_receive_streams_) {
672 if (stream->config().rtp.local_ssrc == ssrc) {
673 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800674 }
675 }
solenbergc7a8b082015-10-16 14:35:07 -0700676 }
skvlad7a43d252016-03-22 15:32:27 -0700677 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700678 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200679}
680
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200681webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
682 const webrtc::AudioReceiveStream::Config& config) {
683 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200684 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800685 RegisterRateObserver();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200686 event_log_->Log(std::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200687 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700688 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100689 clock_, &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100690 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200691 {
692 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100693 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
694 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700695 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800696
pbos8fc7fa72015-07-15 08:02:58 -0700697 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200698 }
solenberg7602aab2016-11-14 11:30:07 -0800699 {
700 ReadLockScoped read_lock(*send_crit_);
701 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
702 if (it != audio_send_ssrcs_.end()) {
703 receive_stream->AssociateSendStream(it->second);
704 }
705 }
skvlad7a43d252016-03-22 15:32:27 -0700706 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200707 return receive_stream;
708}
709
710void Call::DestroyAudioReceiveStream(
711 webrtc::AudioReceiveStream* receive_stream) {
712 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200713 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700714 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700715 webrtc::internal::AudioReceiveStream* audio_receive_stream =
716 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200717 {
718 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800719 const AudioReceiveStream::Config& config = audio_receive_stream->config();
720 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700721 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800722 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700723 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700724 const std::string& sync_group = audio_receive_stream->config().sync_group;
725 const auto it = sync_stream_mapping_.find(sync_group);
726 if (it != sync_stream_mapping_.end() &&
727 it->second == audio_receive_stream) {
728 sync_stream_mapping_.erase(it);
729 ConfigureSync(sync_group);
730 }
nissed44ce052017-02-06 02:23:00 -0800731 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200732 }
skvlad7a43d252016-03-22 15:32:27 -0700733 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200734 delete audio_receive_stream;
735}
736
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100737// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100738webrtc::VideoSendStream* Call::CreateVideoSendStream(
739 webrtc::VideoSendStream::Config config,
740 VideoEncoderConfig encoder_config,
741 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000742 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200743 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000744
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800745 RegisterRateObserver();
746
asapersson35151f32016-05-02 23:44:01 -0700747 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700748 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
749 ++ssrc_index) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200750 event_log_->Log(std::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200751 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700752 }
perkj26091b12016-09-01 01:17:40 -0700753
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000754 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
755 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700756 // Copy ssrcs from |config| since |config| is moved.
757 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100758
mflodman0c478b32015-10-21 15:52:16 +0200759 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Jansson0b698262019-03-07 09:17:19 +0100760 clock_, num_cpu_cores_, module_process_thread_.get(), task_queue_factory_,
Sebastian Jansson74682c12019-03-01 11:50:20 +0100761 call_stats_.get(), transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700762 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200763 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200764 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700765
skvlad7a43d252016-03-22 15:32:27 -0700766 {
767 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700768 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700769 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
770 video_send_ssrcs_[ssrc] = send_stream;
771 }
772 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000773 }
skvlad7a43d252016-03-22 15:32:27 -0700774 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700775
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000776 return send_stream;
777}
778
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100779webrtc::VideoSendStream* Call::CreateVideoSendStream(
780 webrtc::VideoSendStream::Config config,
781 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100782 if (config_.fec_controller_factory) {
783 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
784 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100785 std::unique_ptr<FecController> fec_controller =
786 config_.fec_controller_factory
787 ? config_.fec_controller_factory->CreateFecController()
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200788 : std::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100789 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
790 std::move(fec_controller));
791}
792
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000793void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000794 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700795 RTC_DCHECK(send_stream != nullptr);
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200796 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000797
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000798 send_stream->Stop();
799
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000800 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000801 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000802 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200803 auto it = video_send_ssrcs_.begin();
804 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000805 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
806 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200807 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000808 } else {
809 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000810 }
811 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200812 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000813 }
henrikg91d6ede2015-09-17 00:24:34 -0700814 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000815
Åsa Persson4bece9a2017-10-06 10:04:04 +0200816 VideoSendStream::RtpStateMap rtp_states;
817 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
818 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
819 &rtp_payload_states);
820 for (const auto& kv : rtp_states) {
821 suspended_video_send_ssrcs_[kv.first] = kv.second;
822 }
823 for (const auto& kv : rtp_payload_states) {
824 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000825 }
826
skvlad7a43d252016-03-22 15:32:27 -0700827 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000828 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000829}
830
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200831webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200832 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000833 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200834 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800835
Johannes Kronf59666b2019-04-08 12:57:06 +0200836 receive_side_cc_.SetSendPeriodicFeedback(
837 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +0100838
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800839 RegisterRateObserver();
840
nisse0f15f922017-06-21 01:05:22 -0700841 VideoReceiveStream* receive_stream = new VideoReceiveStream(
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100842 task_queue_factory_, &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200843 transport_send_ptr_->packet_router(), std::move(configuration),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100844 module_process_thread_.get(), call_stats_.get(), clock_);
Tommi733b5472016-06-10 17:58:01 +0200845
846 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700847 {
848 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800849 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800850 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700851 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800852 // type, we may get an incorrect value for the rtx stream, but
853 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100854 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
855 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800856 }
Erik Språng09708512018-03-14 15:16:50 +0100857 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
858 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700859 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700860 ConfigureSync(config.sync_group);
861 }
862 receive_stream->SignalNetworkState(video_network_state_);
863 UpdateAggregateNetworkState();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200864 event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200865 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000866 return receive_stream;
867}
868
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000869void Call::DestroyVideoReceiveStream(
870 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000871 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200872 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700873 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700874 VideoReceiveStream* receive_stream_impl =
875 static_cast<VideoReceiveStream*>(receive_stream);
876 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000877 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000878 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000879 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
880 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700881 receive_rtp_config_.erase(config.rtp.remote_ssrc);
882 if (config.rtp.rtx_ssrc) {
883 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000884 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200885 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700886 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000887 }
nisse4709e892017-02-07 01:18:43 -0800888
nisse559af382017-03-21 06:41:12 -0700889 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800890 ->RemoveStream(config.rtp.remote_ssrc);
891
skvlad7a43d252016-03-22 15:32:27 -0700892 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000893 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000894}
895
brandtr7250b392016-12-19 01:13:46 -0800896FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
897 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700898 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200899 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800900
901 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700902
nisse0f15f922017-06-21 01:05:22 -0700903 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700904 {
905 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -0700906 // Unlike the video and audio receive streams,
907 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
908 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -0700909 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -0700910 // constructor while holding |receive_crit_| ensures that we don't
911 // call OnRtpPacket until the constructor is finished and the
912 // object is in a valid state.
913 // TODO(nisse): Fix constructor so that it can be moved outside of
914 // this locked scope.
915 receive_stream = new FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100916 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +0200917 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -0800918
nissed44ce052017-02-06 02:23:00 -0800919 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
920 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +0100921 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -0700922 }
brandtrb29e6522016-12-21 06:37:18 -0800923
brandtr25445d32016-10-23 23:37:14 -0700924 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -0800925
brandtr25445d32016-10-23 23:37:14 -0700926 return receive_stream;
927}
928
brandtr7250b392016-12-19 01:13:46 -0800929void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -0700930 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200931 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800932
brandtr25445d32016-10-23 23:37:14 -0700933 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -0700934 {
935 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800936
eladalon42f44f92017-07-25 06:40:06 -0700937 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -0800938 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -0800939 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -0800940
brandtr7250b392016-12-19 01:13:46 -0800941 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
942 // destroyed.
nisse559af382017-03-21 06:41:12 -0700943 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800944 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -0700945 }
brandtrb29e6522016-12-21 06:37:18 -0800946
eladalon42f44f92017-07-25 06:40:06 -0700947 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700948}
949
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100950RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +0200951 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100952}
953
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000954Call::Stats Call::GetStats() const {
Tommi48b48e52019-08-09 11:42:32 +0200955 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
956
957 // TODO(tommi): The following stats are managed on the process thread:
958 // - pacer_delay_ms (PacedSender::Process)
959 // - rtt_ms
960 // - recv_bandwidth_bps
961 // These are delivered on the network TQ:
962 // - send_bandwidth_bps (see OnTargetTransferRate)
963 // - max_padding_bitrate_bps (see OnAllocationLimitsChanged)
964
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000965 Stats stats;
Tommi48b48e52019-08-09 11:42:32 +0200966 // TODO(srte): It is unclear if we only want to report queues if network is
967 // available.
968 stats.pacer_delay_ms =
969 aggregate_network_up_ ? transport_send_ptr_->GetPacerQueuingDelayMs() : 0;
970
971 stats.rtt_ms = call_stats_->LastProcessedRtt();
972
Peter Boström45553ae2015-05-08 13:54:38 +0200973 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +0200974 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000975 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -0700976 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700977 &ssrcs, &recv_bandwidth);
Tommi48b48e52019-08-09 11:42:32 +0200978 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100979
980 {
981 rtc::CritScope cs(&last_bandwidth_bps_crit_);
982 stats.send_bandwidth_bps = last_bandwidth_bps_;
983 }
Sebastian Janssona06e9192018-03-07 18:49:55 +0100984
sprang9c0b5512016-07-06 00:54:28 -0700985 {
986 rtc::CritScope cs(&bitrate_crit_);
987 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
988 }
Tommi48b48e52019-08-09 11:42:32 +0200989
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000990 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000991}
992
skvlad7a43d252016-03-22 15:32:27 -0700993void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200994 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -0700995 switch (media) {
996 case MediaType::AUDIO:
997 audio_network_state_ = state;
998 break;
999 case MediaType::VIDEO:
1000 video_network_state_ = state;
1001 break;
1002 case MediaType::ANY:
1003 case MediaType::DATA:
1004 RTC_NOTREACHED();
1005 break;
1006 }
1007
1008 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001009 {
skvlad7a43d252016-03-22 15:32:27 -07001010 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001011 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1012 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001013 }
1014 }
1015}
1016
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001017void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1018 ReadLockScoped read_lock(*send_crit_);
1019 for (auto& kv : audio_send_ssrcs_) {
1020 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -08001021 }
1022}
1023
skvlad7a43d252016-03-22 15:32:27 -07001024void Call::UpdateAggregateNetworkState() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001025 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001026
1027 bool have_audio = false;
1028 bool have_video = false;
1029 {
1030 ReadLockScoped read_lock(*send_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001031 if (!audio_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001032 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001033 if (!video_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001034 have_video = true;
1035 }
1036 {
1037 ReadLockScoped read_lock(*receive_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001038 if (!audio_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001039 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001040 if (!video_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001041 have_video = true;
1042 }
1043
Sebastian Janssona06e9192018-03-07 18:49:55 +01001044 bool aggregate_network_up =
1045 ((have_video && video_network_state_ == kNetworkUp) ||
1046 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001047
Mirko Bonadei675513b2017-11-09 11:09:25 +01001048 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001049 << (aggregate_network_up ? "up" : "down");
Tommi48b48e52019-08-09 11:42:32 +02001050 aggregate_network_up_ = aggregate_network_up;
1051
Sebastian Janssone6256052018-05-04 14:08:15 +02001052 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001053}
1054
stefanc1aeaf02015-10-15 07:26:07 -07001055void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001056 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1057 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001058 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001059}
1060
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001061void Call::OnStartRateUpdate(DataRate start_rate) {
Tommi48b48e52019-08-09 11:42:32 +02001062 RTC_DCHECK(network_queue()->IsCurrent());
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001063 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1064}
1065
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001066void Call::OnTargetTransferRate(TargetTransferRate msg) {
Tommi48b48e52019-08-09 11:42:32 +02001067 RTC_DCHECK(network_queue()->IsCurrent());
Tommi78a71382019-08-08 12:27:53 +02001068 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001069
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001070 uint32_t target_bitrate_bps = msg.target_rate.bps();
1071 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1072 uint8_t fraction_loss =
1073 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1074 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1075 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1076 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
Florent Castelli4e615d52019-08-22 16:09:06 +02001077 uint32_t stable_target_rate_bps = msg.stable_target_rate.bps();
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001078 {
1079 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1080 last_bandwidth_bps_ = bandwidth_bps;
1081 }
nisse559af382017-03-21 06:41:12 -07001082 // For controlling the rate of feedback messages.
1083 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Florent Castelli4e615d52019-08-22 16:09:06 +02001084 bitrate_allocator_->OnNetworkChanged(
1085 target_bitrate_bps, stable_target_rate_bps, bandwidth_bps, fraction_loss,
1086 rtt_ms, probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001087
asaperssonce2e1362016-09-09 00:13:35 -07001088 // Ignore updates if bitrate is zero (the aggregate network state is down).
1089 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001090 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001091 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1092 pacer_bitrate_kbps_counter_.ProcessAndPause();
1093 return;
stefan18adf0a2015-11-17 06:24:56 -08001094 }
asaperssonce2e1362016-09-09 00:13:35 -07001095
1096 bool sending_video;
1097 {
1098 ReadLockScoped read_lock(*send_crit_);
1099 sending_video = !video_send_streams_.empty();
1100 }
1101
1102 rtc::CritScope lock(&bitrate_crit_);
1103 if (!sending_video) {
1104 // Do not update the stats if we are not sending video.
1105 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1106 pacer_bitrate_kbps_counter_.ProcessAndPause();
1107 return;
1108 }
1109 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1110 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1111 uint32_t pacer_bitrate_bps =
1112 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1113 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001114}
mflodman101f2502016-06-09 17:21:19 +02001115
perkj71ee44c2016-06-15 00:47:53 -07001116void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001117 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +01001118 uint32_t total_bitrate_bps) {
Tommi48b48e52019-08-09 11:42:32 +02001119 RTC_DCHECK(network_queue()->IsCurrent());
Tommi78a71382019-08-08 12:27:53 +02001120 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
Tommi48b48e52019-08-09 11:42:32 +02001121
Sebastian Janssone6256052018-05-04 14:08:15 +02001122 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001123 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001124
Tommi78a71382019-08-08 12:27:53 +02001125 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -08001126
perkj71ee44c2016-06-15 00:47:53 -07001127 rtc::CritScope lock(&bitrate_crit_);
sprang9c0b5512016-07-06 00:54:28 -07001128 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001129}
1130
pbos8fc7fa72015-07-15 08:02:58 -07001131void Call::ConfigureSync(const std::string& sync_group) {
1132 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001133 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001134 return;
1135
1136 AudioReceiveStream* sync_audio_stream = nullptr;
1137 // Find existing audio stream.
1138 const auto it = sync_stream_mapping_.find(sync_group);
1139 if (it != sync_stream_mapping_.end()) {
1140 sync_audio_stream = it->second;
1141 } else {
1142 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001143 for (AudioReceiveStream* stream : audio_receive_streams_) {
1144 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001145 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001146 RTC_LOG(LS_WARNING)
1147 << "Attempting to sync more than one audio stream "
1148 "within the same sync group. This is not "
1149 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001150 break;
1151 }
nissee4bcd6d2017-05-16 04:47:04 -07001152 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001153 }
1154 }
1155 }
1156 if (sync_audio_stream)
1157 sync_stream_mapping_[sync_group] = sync_audio_stream;
1158 size_t num_synced_streams = 0;
1159 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1160 if (video_stream->config().sync_group != sync_group)
1161 continue;
1162 ++num_synced_streams;
1163 if (num_synced_streams > 1) {
1164 // TODO(pbos): Support synchronizing more than one A/V pair.
1165 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001166 RTC_LOG(LS_WARNING)
1167 << "Attempting to sync more than one audio/video pair "
1168 "within the same sync group. This is not supported in "
1169 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001170 }
1171 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001172 if (num_synced_streams == 1) {
1173 // sync_audio_stream may be null and that's ok.
1174 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001175 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001176 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001177 }
1178 }
1179}
1180
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001181PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1182 const uint8_t* packet,
1183 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001184 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001185 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001186 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1187 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001188 if (received_bytes_per_second_counter_.HasSample()) {
1189 // First RTP packet has been received.
1190 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1191 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1192 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001193 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001194 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001195 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001196 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001197 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001198 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001199 }
1200 }
1201 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1202 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001203 for (AudioReceiveStream* stream : audio_receive_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001204 stream->DeliverRtcp(packet, length);
1205 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001206 }
1207 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001208 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001209 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001210 for (VideoSendStream* stream : video_send_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001211 stream->DeliverRtcp(packet, length);
1212 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001213 }
1214 }
mflodman3d7db262016-04-29 00:57:13 -07001215 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1216 ReadLockScoped read_lock(*send_crit_);
1217 for (auto& kv : audio_send_ssrcs_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001218 kv.second->DeliverRtcp(packet, length);
1219 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001220 }
1221 }
1222
Elad Alon4a87e1c2017-10-03 16:11:34 +02001223 if (rtcp_delivered) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001224 event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001225 rtc::MakeArrayView(packet, length)));
1226 }
mflodman3d7db262016-04-29 00:57:13 -07001227
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001228 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001229}
1230
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001231PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001232 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001233 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001234 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001235
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001236 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001237 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001238 return DELIVERY_PACKET_ERROR;
1239
Niels Möller70082872018-08-07 11:03:12 +02001240 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001241 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001242 // Repair packet_time_us for clock resets by comparing a new read of
1243 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001244 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001245 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001246 }
Niels Möller70082872018-08-07 11:03:12 +02001247 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001248 } else {
1249 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1250 }
nissed44ce052017-02-06 02:23:00 -08001251
sprangc1abde72017-07-11 03:56:21 -07001252 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1253 // These are empty (zero length payload) RTP packets with an unsignaled
1254 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001255 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001256
1257 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1258 is_keep_alive_packet);
1259
sprangc1abde72017-07-11 03:56:21 -07001260 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001261 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001262 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001263 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1264 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001265 // Destruction of the receive stream, including deregistering from the
1266 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1267 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1268 // So by not passing the packet on to demuxing in this case, we prevent
1269 // incoming packets to be passed on via the demuxer to a receive stream
1270 // which is being torned down.
1271 return DELIVERY_UNKNOWN_SSRC;
1272 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001273
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001274 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001275
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001276 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001277
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001278 // RateCounters expect input parameter as int, save it as int,
1279 // instead of converting each time it is passed to RateCounter::Add below.
1280 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001281 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001282 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001283 received_bytes_per_second_counter_.Add(length);
1284 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001285 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001286 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001287 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001288 if (!first_received_rtp_audio_ms_) {
1289 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1290 }
1291 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001292 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001293 }
nissee4bcd6d2017-05-16 04:47:04 -07001294 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001295 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001296 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001297 received_bytes_per_second_counter_.Add(length);
1298 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001299 event_log_->Log(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001300 std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001301 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001302 if (!first_received_rtp_video_ms_) {
1303 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1304 }
1305 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001306 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001307 }
1308 }
1309 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001310}
1311
stefan68786d22015-09-08 05:36:15 -07001312PacketReceiver::DeliveryStatus Call::DeliverPacket(
1313 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001314 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001315 int64_t packet_time_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001316 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Tommi25eb47c2019-08-29 16:39:05 +02001317 if (IsRtcp(packet.cdata(), packet.size()))
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001318 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001319
Niels Möller70082872018-08-07 11:03:12 +02001320 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001321}
1322
nissed2ef3142017-05-11 08:00:58 -07001323void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001324 RtpPacketReceived parsed_packet;
1325 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001326 return;
1327
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001328 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001329
brandtrcaea68f2017-08-23 00:55:17 -07001330 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001331 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001332 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001333 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1334 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001335 // Destruction of the receive stream, including deregistering from the
1336 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1337 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1338 // So by not passing the packet on to demuxing in this case, we prevent
1339 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001340 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001341 return;
1342 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001343 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001344
1345 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001346 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001347 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001348}
1349
nissed44ce052017-02-06 02:23:00 -08001350void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1351 MediaType media_type) {
1352 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001353 bool use_send_side_bwe =
1354 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001355
brandtrb29e6522016-12-21 06:37:18 -08001356 RTPHeader header;
1357 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001358
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001359 ReceivedPacket packet_msg;
1360 packet_msg.size = DataSize::bytes(packet.payload_size());
1361 packet_msg.receive_time = Timestamp::ms(packet.arrival_time_ms());
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001362 if (header.extension.hasAbsoluteSendTime) {
1363 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1364 }
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001365 transport_send_ptr_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001366
nisse4709e892017-02-07 01:18:43 -08001367 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001368 // Inconsistent configuration of send side BWE. Do nothing.
1369 // TODO(nisse): Without this check, we may produce RTCP feedback
1370 // packets even when not negotiated. But it would be cleaner to
1371 // move the check down to RTCPSender::SendFeedbackPacket, which
1372 // would also help the PacketRouter to select an appropriate rtp
1373 // module in the case that some, but not all, have RTCP feedback
1374 // enabled.
1375 return;
1376 }
1377 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001378 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001379 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001380 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001381 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1382 header);
1383 }
brandtrb29e6522016-12-21 06:37:18 -08001384}
1385
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001386} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001387
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001388} // namespace webrtc