blob: fe922e40b64957c9d7e53838c8790984dbc34eed [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <string.h>
mflodman101f2502016-06-09 17:21:19 +020012#include <algorithm>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013#include <map>
kwibergb25345e2016-03-12 06:10:44 -080014#include <memory>
ossuf515ab82016-12-07 04:52:58 -080015#include <set>
brandtr25445d32016-10-23 23:37:14 -070016#include <utility>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000017#include <vector>
18
Karl Wiberg918f50c2018-07-05 11:40:33 +020019#include "absl/memory/memory.h"
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020020#include "absl/types/optional.h"
Sebastian Jansson74682c12019-03-01 11:50:20 +010021#include "api/task_queue/global_task_queue_factory.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020022#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "audio/audio_receive_stream.h"
24#include "audio/audio_send_stream.h"
25#include "audio/audio_state.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "call/bitrate_allocator.h"
27#include "call/call.h"
28#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010029#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "call/rtp_stream_receiver_controller.h"
31#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020032#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020033#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
34#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
35#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
36#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "logging/rtc_event_log/rtc_event_log.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"
42#include "modules/rtp_rtcp/include/rtp_header_parser.h"
43#include "modules/rtp_rtcp/source/byte_io.h"
44#include "modules/rtp_rtcp/source/rtp_packet_received.h"
45#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) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200114 auto rtclog_config = absl::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;
119 rtclog_config->remb = config.rtp.remb;
120 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700121
122 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700123 const int* search =
124 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200125 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200126 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700127 }
128 return rtclog_config;
129}
130
eladalon8ec568a2017-09-08 06:15:52 -0700131std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700132 const VideoSendStream::Config& config,
133 size_t ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200134 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700135 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700136 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700137 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700138 }
eladalon8ec568a2017-09-08 06:15:52 -0700139 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
140 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700141
Niels Möller259a4972018-04-05 15:36:51 +0200142 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
143 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700144 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700145 return rtclog_config;
146}
147
eladalon8ec568a2017-09-08 06:15:52 -0700148std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700149 const AudioReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200150 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700151 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
152 rtclog_config->local_ssrc = config.rtp.local_ssrc;
153 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700154 return rtclog_config;
155}
156
nisse4709e892017-02-07 01:18:43 -0800157} // namespace
158
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000159namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000160
Sebastian Janssone6256052018-05-04 14:08:15 +0200161class Call final : public webrtc::Call,
162 public PacketReceiver,
163 public RecoveredPacketReceiver,
164 public TargetTransferRateObserver,
165 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000166 public:
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100167 Call(Clock* clock,
168 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100169 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
170 std::unique_ptr<ProcessThread> module_process_thread,
171 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200172 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000173
brandtr25445d32016-10-23 23:37:14 -0700174 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000175 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000176
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200177 webrtc::AudioSendStream* CreateAudioSendStream(
178 const webrtc::AudioSendStream::Config& config) override;
179 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
180
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200181 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
182 const webrtc::AudioReceiveStream::Config& config) override;
183 void DestroyAudioReceiveStream(
184 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000185
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200186 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700187 webrtc::VideoSendStream::Config config,
188 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100189 webrtc::VideoSendStream* CreateVideoSendStream(
190 webrtc::VideoSendStream::Config config,
191 VideoEncoderConfig encoder_config,
192 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000193 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000194
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200195 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200196 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000197 void DestroyVideoReceiveStream(
198 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000199
brandtr7250b392016-12-19 01:13:46 -0800200 FlexfecReceiveStream* CreateFlexfecReceiveStream(
201 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700202 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800203 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700204
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100205 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
206
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000207 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000208
brandtr25445d32016-10-23 23:37:14 -0700209 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700210 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100211 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200212 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000213
brandtr4e523862016-10-18 23:50:45 -0700214 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700215 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700216
Alex Narest78609d52017-10-20 10:37:47 +0200217 void SetBitrateAllocationStrategy(
218 std::unique_ptr<rtc::BitrateAllocationStrategy>
219 bitrate_allocation_strategy) override;
220
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) Slatalacc8e8bb2018-11-15 08:26:19 -0800237 // This method is invoked when the media transport is created and when the
238 // media transport is being destructed.
239 // We only allow one media transport per connection.
240 //
241 // It should be called with non-null argument at most once, and if it was
242 // called with non-null argument, it has to be called with a null argument
243 // at least once after that.
244 void MediaTransportChange(MediaTransportInterface* media_transport) override;
245
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700246 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
247
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000248 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200249 DeliveryStatus DeliverRtcp(MediaType media_type,
250 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200251 size_t length);
stefan68786d22015-09-08 05:36:15 -0700252 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100253 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200254 int64_t packet_time_us);
pbos8fc7fa72015-07-15 08:02:58 -0700255 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700256 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700257
nissed44ce052017-02-06 02:23:00 -0800258 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
259 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700260 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800261
asaperssonfc5e81c2017-04-19 23:28:53 -0700262 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700263 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800264 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700265 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700266 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800267
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800268 // If |media_transport| is not null, it registers the rate observer for the
269 // media transport.
270 void RegisterRateObserver() RTC_LOCKS_EXCLUDED(target_observer_crit_);
271
Niels Möller46879152019-01-07 15:54:47 +0100272 // Intended for DCHECKs, to avoid locking in production builds.
273 MediaTransportInterface* media_transport()
274 RTC_LOCKS_EXCLUDED(target_observer_crit_);
275
Peter Boströmd3c94472015-12-09 11:20:58 +0100276 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100277 TaskQueueFactory* const task_queue_factory_;
stefan91d92602015-11-11 10:13:02 -0800278
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700279 // Caching the last SetBitrate for media transport.
280 absl::optional<MediaTransportTargetRateConstraints> last_set_bitrate_
281 RTC_GUARDED_BY(&target_observer_crit_);
Peter Boström45553ae2015-05-08 13:54:38 +0200282 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800283 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800284 const std::unique_ptr<CallStats> call_stats_;
285 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000286 Call::Config config_;
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200287 SequenceChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000288
skvlad7a43d252016-03-22 15:32:27 -0700289 NetworkState audio_network_state_;
290 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100291 rtc::CriticalSection aggregate_network_up_crit_;
292 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000293
kwibergb25345e2016-03-12 06:10:44 -0800294 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700295 // Audio, Video, and FlexFEC receive streams are owned by the client that
296 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700297 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700298 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200299 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700300 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700301
pbos8fc7fa72015-07-15 08:02:58 -0700302 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700303 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000304
nisse0f15f922017-06-21 01:05:22 -0700305 // TODO(nisse): Should eventually be injected at creation,
306 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700307 RtpStreamReceiverController audio_receiver_controller_;
308 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700309
nissed44ce052017-02-06 02:23:00 -0800310 // This extra map is used for receive processing which is
311 // independent of media type.
312
313 // TODO(nisse): In the RTP transport refactoring, we should have a
314 // single mapping from ssrc to a more abstract receive stream, with
315 // accessor methods for all configuration we need at this level.
316 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100317 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
318 : extensions(config.rtp.extensions),
319 use_send_side_bwe(UseSendSideBwe(config)) {}
320 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
321 : extensions(config.rtp.extensions),
322 use_send_side_bwe(UseSendSideBwe(config)) {}
323 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
324 : extensions(config.rtp_header_extensions),
325 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800326
327 // Registered RTP header extensions for each stream. Note that RTP header
328 // extensions are negotiated per track ("m= line") in the SDP, but we have
329 // no notion of tracks at the Call level. We therefore store the RTP header
330 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100331 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800332 // Set if both RTP extension the RTCP feedback message needed for
333 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100334 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800335 };
336 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700337 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800338
kwibergb25345e2016-03-12 06:10:44 -0800339 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700340 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700341 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
342 RTC_GUARDED_BY(send_crit_);
343 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
344 RTC_GUARDED_BY(send_crit_);
345 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000346
ossuc3d4b482017-05-23 06:07:11 -0700347 using RtpStateMap = std::map<uint32_t, RtpState>;
348 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700349 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700350 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700351 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700352
Åsa Persson4bece9a2017-10-06 10:04:04 +0200353 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
354 RtpPayloadStateMap suspended_video_payload_states_
355 RTC_GUARDED_BY(configuration_sequence_checker_);
356
skvlad11a9cbf2016-10-07 11:53:05 -0700357 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700358
stefan18adf0a2015-11-17 06:24:56 -0800359 // The following members are only accessed (exclusively) from one thread and
360 // from the destructor, and therefore doesn't need any explicit
361 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700362 RateCounter received_bytes_per_second_counter_;
363 RateCounter received_audio_bytes_per_second_counter_;
364 RateCounter received_video_bytes_per_second_counter_;
365 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200366 absl::optional<int64_t> first_received_rtp_audio_ms_;
367 absl::optional<int64_t> last_received_rtp_audio_ms_;
368 absl::optional<int64_t> first_received_rtp_video_ms_;
369 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 10:13:02 -0800370
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100371 rtc::CriticalSection last_bandwidth_bps_crit_;
372 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800373 // TODO(holmer): Remove this lock once BitrateController no longer calls
374 // OnNetworkChanged from multiple threads.
375 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700376 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
377 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
378 AvgCounter estimated_send_bitrate_kbps_counter_
379 RTC_GUARDED_BY(&bitrate_crit_);
380 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800381
nisse559af382017-03-21 06:41:12 -0700382 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100383
384 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
385
asapersson35151f32016-05-02 23:44:01 -0700386 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700387 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800388
Sebastian Janssone6256052018-05-04 14:08:15 +0200389 // Caches transport_send_.get(), to avoid racing with destructor.
390 // Note that this is declared before transport_send_ to ensure that it is not
391 // invalidated until no more tasks can be running on the transport_send_ task
392 // queue.
393 RtpTransportControllerSendInterface* transport_send_ptr_;
394 // Declared last since it will issue callbacks from a task queue. Declaring it
395 // last ensures that it is destroyed first and any running tasks are finished.
396 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800397
398 // This is a precaution, since |MediaTransportChange| is not guaranteed to be
399 // invoked on a particular thread.
400 rtc::CriticalSection target_observer_crit_;
401 bool is_target_rate_observer_registered_
402 RTC_GUARDED_BY(&target_observer_crit_) = false;
403 MediaTransportInterface* media_transport_
404 RTC_GUARDED_BY(&target_observer_crit_) = nullptr;
405
henrikg3c089d72015-09-16 05:37:44 -0700406 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000407};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000408} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000409
asapersson2e5cfcd2016-08-11 08:41:18 -0700410std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200411 char buf[1024];
412 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700413 ss << "Call stats: " << time_ms << ", {";
414 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
415 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
416 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
417 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
418 ss << "rtt_ms: " << rtt_ms;
419 ss << '}';
420 return ss.str();
421}
422
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000423Call* Call::Create(const Call::Config& config) {
Danil Chapovalov359fe332019-04-01 10:46:36 +0200424 return Create(config, Clock::GetRealTimeClock(),
425 ProcessThread::Create("PacerThread"),
426 ProcessThread::Create("ModuleProcessThread"));
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100427}
428
429Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100430 Clock* clock,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100431 std::unique_ptr<ProcessThread> call_thread,
Danil Chapovalov359fe332019-04-01 10:46:36 +0200432 std::unique_ptr<ProcessThread> pacer_thread) {
433 // TODO(bugs.webrtc.org/10284): DCHECK task_queue_factory dependency is
434 // always provided in the config.
435 TaskQueueFactory* task_queue_factory = config.task_queue_factory
436 ? config.task_queue_factory
437 : &GlobalTaskQueueFactory();
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100438 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100439 clock, config,
Sebastian Janssoned50e6c2019-03-01 14:45:21 +0100440 absl::make_unique<RtpTransportControllerSend>(
Ying Wang0810a7c2019-04-10 13:48:24 +0200441 clock, config.event_log, config.network_state_predictor_factory,
442 config.network_controller_factory, config.bitrate_config,
443 std::move(pacer_thread), task_queue_factory),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100444 std::move(call_thread), task_queue_factory);
zstein7cb69d52017-05-08 11:52:38 -0700445}
446
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100447// This method here to avoid subclasses has to implement this method.
448// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
449// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100450VideoSendStream* Call::CreateVideoSendStream(
451 VideoSendStream::Config config,
452 VideoEncoderConfig encoder_config,
453 std::unique_ptr<FecController> fec_controller) {
454 return nullptr;
455}
456
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000457namespace internal {
458
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100459Call::Call(Clock* clock,
460 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100461 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
462 std::unique_ptr<ProcessThread> module_process_thread,
463 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100464 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100465 task_queue_factory_(task_queue_factory),
stefan91d92602015-11-11 10:13:02 -0800466 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100467 module_process_thread_(std::move(module_process_thread)),
Tommi38c5d932018-03-27 23:11:09 +0200468 call_stats_(new CallStats(clock_, module_process_thread_.get())),
Sebastian Janssonda6806c2019-03-04 17:05:12 +0100469 bitrate_allocator_(new BitrateAllocator(clock_, this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200470 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800471 audio_network_state_(kNetworkDown),
472 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100473 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000474 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800475 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700476 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700477 received_bytes_per_second_counter_(clock_, nullptr, true),
478 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
479 received_video_bytes_per_second_counter_(clock_, nullptr, true),
480 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100481 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700482 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700483 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700484 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
485 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700486 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100487 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700488 video_send_delay_stats_(new SendDelayStats(clock_)),
Benjamin Wrighta5564482019-04-03 10:44:18 -0700489 start_ms_(clock_->TimeInMilliseconds()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700490 RTC_DCHECK(config.event_log != nullptr);
nisse6167b262017-04-06 06:34:25 -0700491 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 14:08:15 +0200492 transport_send_ptr_ = transport_send_.get();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000493}
494
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000495Call::~Call() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200496 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700497
solenbergc7a8b082015-10-16 14:35:07 -0700498 RTC_CHECK(audio_send_ssrcs_.empty());
499 RTC_CHECK(video_send_ssrcs_.empty());
500 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700501 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700502 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000503
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800504 if (!media_transport_) {
505 module_process_thread_->DeRegisterModule(
506 receive_side_cc_.GetRemoteBitrateEstimator(true));
507 module_process_thread_->DeRegisterModule(&receive_side_cc_);
508 module_process_thread_->DeRegisterModule(call_stats_.get());
509 module_process_thread_->Stop();
510 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800511 }
sprang6d6122b2016-07-13 06:37:09 -0700512
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100513 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700514 // Only update histograms after process threads have been shut down, so that
515 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700516 {
517 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700518 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700519 }
sprang6d6122b2016-07-13 06:37:09 -0700520 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700521 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000522}
523
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800524void Call::RegisterRateObserver() {
525 rtc::CritScope lock(&target_observer_crit_);
526
527 if (is_target_rate_observer_registered_) {
528 return;
529 }
530
531 is_target_rate_observer_registered_ = true;
532
533 if (media_transport_) {
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800534 // TODO(bugs.webrtc.org/9719): We should report call_stats_ from
535 // media transport (at least Rtt). We should extend media transport
536 // interface to include "receive_side bwe" if needed.
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800537 media_transport_->AddTargetTransferRateObserver(this);
538 } else {
539 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800540
541 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800542
543 module_process_thread_->RegisterModule(
544 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
545 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
546 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
547 module_process_thread_->Start();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800548 }
549}
550
Niels Möller46879152019-01-07 15:54:47 +0100551MediaTransportInterface* Call::media_transport() {
552 rtc::CritScope lock(&target_observer_crit_);
553 return media_transport_;
554}
555
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800556void Call::MediaTransportChange(MediaTransportInterface* media_transport) {
557 rtc::CritScope lock(&target_observer_crit_);
558
559 if (is_target_rate_observer_registered_) {
560 // Only used to unregister rate observer from media transport. Registration
561 // happens when the stream is created.
562 if (!media_transport && media_transport_) {
563 media_transport_->RemoveTargetTransferRateObserver(this);
564 media_transport_ = nullptr;
565 is_target_rate_observer_registered_ = false;
566 }
567 } else if (media_transport) {
568 RTC_DCHECK(media_transport_ == nullptr ||
569 media_transport_ == media_transport)
570 << "media_transport_=" << (media_transport_ != nullptr)
571 << ", (media_transport_==media_transport)="
572 << (media_transport_ == media_transport);
573 media_transport_ = media_transport;
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -0700574 MediaTransportTargetRateConstraints constraints;
575 if (config_.bitrate_config.start_bitrate_bps > 0) {
576 constraints.starting_bitrate =
577 DataRate::bps(config_.bitrate_config.start_bitrate_bps);
578 }
579 if (config_.bitrate_config.max_bitrate_bps > 0) {
580 constraints.max_bitrate =
581 DataRate::bps(config_.bitrate_config.max_bitrate_bps);
582 }
583 if (config_.bitrate_config.min_bitrate_bps > 0) {
584 constraints.min_bitrate =
585 DataRate::bps(config_.bitrate_config.min_bitrate_bps);
586 }
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700587
588 // User called ::SetBitrate on peer connection before
589 // media transport was created.
590 if (last_set_bitrate_) {
591 media_transport_->SetTargetBitrateLimits(*last_set_bitrate_);
592 } else {
593 media_transport_->SetTargetBitrateLimits(constraints);
594 }
595 }
596}
597
598void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
599 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
600 // Can the client code invoke 'SetBitrate' before media transport is created?
601 // It's probably possible :/
602 MediaTransportTargetRateConstraints constraints;
603 if (preferences.start_bitrate_bps.has_value()) {
604 constraints.starting_bitrate =
605 webrtc::DataRate::bps(*preferences.start_bitrate_bps);
606 }
607 if (preferences.max_bitrate_bps.has_value()) {
608 constraints.max_bitrate =
609 webrtc::DataRate::bps(*preferences.max_bitrate_bps);
610 }
611 if (preferences.min_bitrate_bps.has_value()) {
612 constraints.min_bitrate =
613 webrtc::DataRate::bps(*preferences.min_bitrate_bps);
614 }
615 rtc::CritScope lock(&target_observer_crit_);
616 last_set_bitrate_ = constraints;
617 if (media_transport_) {
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -0700618 media_transport_->SetTargetBitrateLimits(constraints);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800619 }
620}
621
asapersson4374a092016-07-27 00:39:09 -0700622void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700623 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700624 "WebRTC.Call.LifetimeInSeconds",
625 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
626}
627
asaperssonfc5e81c2017-04-19 23:28:53 -0700628void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
629 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800630 return;
631 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700632 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800633 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
634 return;
asaperssonce2e1362016-09-09 00:13:35 -0700635 const int kMinRequiredPeriodicSamples = 5;
636 AggregatedStats send_bitrate_stats =
637 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
638 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700639 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
640 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100641 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
642 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800643 }
asaperssonce2e1362016-09-09 00:13:35 -0700644 AggregatedStats pacer_bitrate_stats =
645 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
646 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700647 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
648 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100649 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
650 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800651 }
652}
653
654void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700655 if (first_received_rtp_audio_ms_) {
656 RTC_HISTOGRAM_COUNTS_100000(
657 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
658 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
659 }
660 if (first_received_rtp_video_ms_) {
661 RTC_HISTOGRAM_COUNTS_100000(
662 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
663 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
664 }
asapersson250fd972016-09-08 00:07:21 -0700665 const int kMinRequiredPeriodicSamples = 5;
666 AggregatedStats video_bytes_per_sec =
667 received_video_bytes_per_second_counter_.GetStats();
668 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700669 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
670 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100671 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
672 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800673 }
asapersson250fd972016-09-08 00:07:21 -0700674 AggregatedStats audio_bytes_per_sec =
675 received_audio_bytes_per_second_counter_.GetStats();
676 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700677 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
678 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100679 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
680 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800681 }
asapersson250fd972016-09-08 00:07:21 -0700682 AggregatedStats rtcp_bytes_per_sec =
683 received_rtcp_bytes_per_second_counter_.GetStats();
684 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700685 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
686 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100687 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
688 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800689 }
asapersson250fd972016-09-08 00:07:21 -0700690 AggregatedStats recv_bytes_per_sec =
691 received_bytes_per_second_counter_.GetStats();
692 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700693 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
694 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100695 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
696 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700697 }
stefan91d92602015-11-11 10:13:02 -0800698}
699
solenberg5a289392015-10-19 03:39:20 -0700700PacketReceiver* Call::Receiver() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200701 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700702 return this;
703}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000704
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200705webrtc::AudioSendStream* Call::CreateAudioSendStream(
706 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700707 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200708 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800709
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700710 RTC_DCHECK_EQ(media_transport(),
711 config.media_transport_config.media_transport);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800712
713 RegisterRateObserver();
714
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100715 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
716 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200717 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700718 {
719 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
720 if (iter != suspended_audio_send_ssrcs_.end()) {
721 suspended_rtp_state.emplace(iter->second);
722 }
723 }
724
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100725 AudioSendStream* send_stream =
726 new AudioSendStream(clock_, config, config_.audio_state,
727 task_queue_factory_, module_process_thread_.get(),
728 transport_send_ptr_, bitrate_allocator_.get(),
729 event_log_, call_stats_.get(), suspended_rtp_state);
solenbergc7a8b082015-10-16 14:35:07 -0700730 {
solenbergc7a8b082015-10-16 14:35:07 -0700731 WriteLockScoped write_lock(*send_crit_);
732 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
733 audio_send_ssrcs_.end());
734 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700735 }
solenberg7602aab2016-11-14 11:30:07 -0800736 {
737 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700738 for (AudioReceiveStream* stream : audio_receive_streams_) {
739 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
740 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800741 }
742 }
743 }
skvlad7a43d252016-03-22 15:32:27 -0700744 send_stream->SignalNetworkState(audio_network_state_);
745 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700746 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200747}
748
749void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700750 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200751 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700752 RTC_DCHECK(send_stream != nullptr);
753
754 send_stream->Stop();
755
eladalonabbc4302017-07-26 02:09:44 -0700756 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700757 webrtc::internal::AudioSendStream* audio_send_stream =
758 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700759 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700760 {
761 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800762 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
763 RTC_DCHECK_EQ(1, num_deleted);
764 }
765 {
766 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700767 for (AudioReceiveStream* stream : audio_receive_streams_) {
768 if (stream->config().rtp.local_ssrc == ssrc) {
769 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800770 }
771 }
solenbergc7a8b082015-10-16 14:35:07 -0700772 }
skvlad7a43d252016-03-22 15:32:27 -0700773 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700774 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200775}
776
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200777webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
778 const webrtc::AudioReceiveStream::Config& config) {
779 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200780 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800781 RegisterRateObserver();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200782 event_log_->Log(absl::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200783 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700784 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100785 clock_, &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100786 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200787 {
788 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100789 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
790 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700791 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800792
pbos8fc7fa72015-07-15 08:02:58 -0700793 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200794 }
solenberg7602aab2016-11-14 11:30:07 -0800795 {
796 ReadLockScoped read_lock(*send_crit_);
797 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
798 if (it != audio_send_ssrcs_.end()) {
799 receive_stream->AssociateSendStream(it->second);
800 }
801 }
skvlad7a43d252016-03-22 15:32:27 -0700802 receive_stream->SignalNetworkState(audio_network_state_);
803 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200804 return receive_stream;
805}
806
807void Call::DestroyAudioReceiveStream(
808 webrtc::AudioReceiveStream* receive_stream) {
809 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200810 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700811 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700812 webrtc::internal::AudioReceiveStream* audio_receive_stream =
813 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200814 {
815 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800816 const AudioReceiveStream::Config& config = audio_receive_stream->config();
817 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700818 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800819 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700820 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700821 const std::string& sync_group = audio_receive_stream->config().sync_group;
822 const auto it = sync_stream_mapping_.find(sync_group);
823 if (it != sync_stream_mapping_.end() &&
824 it->second == audio_receive_stream) {
825 sync_stream_mapping_.erase(it);
826 ConfigureSync(sync_group);
827 }
nissed44ce052017-02-06 02:23:00 -0800828 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200829 }
skvlad7a43d252016-03-22 15:32:27 -0700830 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200831 delete audio_receive_stream;
832}
833
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100834// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100835webrtc::VideoSendStream* Call::CreateVideoSendStream(
836 webrtc::VideoSendStream::Config config,
837 VideoEncoderConfig encoder_config,
838 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000839 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200840 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000841
Niels Möller46879152019-01-07 15:54:47 +0100842 RTC_DCHECK(media_transport() == config.media_transport);
843
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800844 RegisterRateObserver();
845
asapersson35151f32016-05-02 23:44:01 -0700846 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700847 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
848 ++ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200849 event_log_->Log(absl::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200850 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700851 }
perkj26091b12016-09-01 01:17:40 -0700852
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000853 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
854 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700855 // Copy ssrcs from |config| since |config| is moved.
856 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100857
mflodman0c478b32015-10-21 15:52:16 +0200858 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Jansson0b698262019-03-07 09:17:19 +0100859 clock_, num_cpu_cores_, module_process_thread_.get(), task_queue_factory_,
Sebastian Jansson74682c12019-03-01 11:50:20 +0100860 call_stats_.get(), transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700861 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200862 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200863 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700864
skvlad7a43d252016-03-22 15:32:27 -0700865 {
866 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700867 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700868 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
869 video_send_ssrcs_[ssrc] = send_stream;
870 }
871 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000872 }
skvlad7a43d252016-03-22 15:32:27 -0700873 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700874
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000875 return send_stream;
876}
877
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100878webrtc::VideoSendStream* Call::CreateVideoSendStream(
879 webrtc::VideoSendStream::Config config,
880 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100881 if (config_.fec_controller_factory) {
882 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
883 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100884 std::unique_ptr<FecController> fec_controller =
885 config_.fec_controller_factory
886 ? config_.fec_controller_factory->CreateFecController()
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100887 : absl::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100888 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
889 std::move(fec_controller));
890}
891
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000892void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000893 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700894 RTC_DCHECK(send_stream != nullptr);
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200895 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000896
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000897 send_stream->Stop();
898
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000899 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000900 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000901 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200902 auto it = video_send_ssrcs_.begin();
903 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000904 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
905 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200906 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000907 } else {
908 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000909 }
910 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200911 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000912 }
henrikg91d6ede2015-09-17 00:24:34 -0700913 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000914
Åsa Persson4bece9a2017-10-06 10:04:04 +0200915 VideoSendStream::RtpStateMap rtp_states;
916 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
917 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
918 &rtp_payload_states);
919 for (const auto& kv : rtp_states) {
920 suspended_video_send_ssrcs_[kv.first] = kv.second;
921 }
922 for (const auto& kv : rtp_payload_states) {
923 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000924 }
925
skvlad7a43d252016-03-22 15:32:27 -0700926 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000927 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000928}
929
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200930webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200931 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000932 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200933 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800934
Johannes Kronf59666b2019-04-08 12:57:06 +0200935 receive_side_cc_.SetSendPeriodicFeedback(
936 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +0100937
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800938 RegisterRateObserver();
939
nisse0f15f922017-06-21 01:05:22 -0700940 VideoReceiveStream* receive_stream = new VideoReceiveStream(
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100941 task_queue_factory_, &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200942 transport_send_ptr_->packet_router(), std::move(configuration),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100943 module_process_thread_.get(), call_stats_.get(), clock_);
Tommi733b5472016-06-10 17:58:01 +0200944
945 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700946 {
947 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800948 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800949 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700950 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800951 // type, we may get an incorrect value for the rtx stream, but
952 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100953 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
954 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800955 }
Erik Språng09708512018-03-14 15:16:50 +0100956 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
957 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700958 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700959 ConfigureSync(config.sync_group);
960 }
961 receive_stream->SignalNetworkState(video_network_state_);
962 UpdateAggregateNetworkState();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200963 event_log_->Log(absl::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200964 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000965 return receive_stream;
966}
967
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000968void Call::DestroyVideoReceiveStream(
969 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000970 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200971 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700972 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700973 VideoReceiveStream* receive_stream_impl =
974 static_cast<VideoReceiveStream*>(receive_stream);
975 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000976 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000977 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000978 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
979 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700980 receive_rtp_config_.erase(config.rtp.remote_ssrc);
981 if (config.rtp.rtx_ssrc) {
982 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000983 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200984 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700985 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000986 }
nisse4709e892017-02-07 01:18:43 -0800987
nisse559af382017-03-21 06:41:12 -0700988 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800989 ->RemoveStream(config.rtp.remote_ssrc);
990
skvlad7a43d252016-03-22 15:32:27 -0700991 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000992 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000993}
994
brandtr7250b392016-12-19 01:13:46 -0800995FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
996 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700997 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200998 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800999
1000 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -07001001
nisse0f15f922017-06-21 01:05:22 -07001002 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001003 {
1004 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -07001005 // Unlike the video and audio receive streams,
1006 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
1007 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -07001008 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -07001009 // constructor while holding |receive_crit_| ensures that we don't
1010 // call OnRtpPacket until the constructor is finished and the
1011 // object is in a valid state.
1012 // TODO(nisse): Fix constructor so that it can be moved outside of
1013 // this locked scope.
1014 receive_stream = new FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 19:39:01 +01001015 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +02001016 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -08001017
nissed44ce052017-02-06 02:23:00 -08001018 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1019 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +01001020 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -07001021 }
brandtrb29e6522016-12-21 06:37:18 -08001022
brandtr25445d32016-10-23 23:37:14 -07001023 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001024
brandtr25445d32016-10-23 23:37:14 -07001025 return receive_stream;
1026}
1027
brandtr7250b392016-12-19 01:13:46 -08001028void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001029 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001030 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -08001031
brandtr25445d32016-10-23 23:37:14 -07001032 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -07001033 {
1034 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -08001035
eladalon42f44f92017-07-25 06:40:06 -07001036 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -08001037 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -08001038 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001039
brandtr7250b392016-12-19 01:13:46 -08001040 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1041 // destroyed.
nisse559af382017-03-21 06:41:12 -07001042 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -08001043 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -07001044 }
brandtrb29e6522016-12-21 06:37:18 -08001045
eladalon42f44f92017-07-25 06:40:06 -07001046 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001047}
1048
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001049RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +02001050 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001051}
1052
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001053Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -07001054 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
1055 // thread. Re-enable once that is fixed.
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001056 // RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001057 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +02001058 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001059 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001060 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001061 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001062 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001063
1064 {
1065 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1066 stats.send_bandwidth_bps = last_bandwidth_bps_;
1067 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001068 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001069 // TODO(srte): It is unclear if we only want to report queues if network is
1070 // available.
1071 {
1072 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 14:08:15 +02001073 stats.pacer_delay_ms = aggregate_network_up_
1074 ? transport_send_ptr_->GetPacerQueuingDelayMs()
1075 : 0;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001076 }
1077
Tommi38c5d932018-03-27 23:11:09 +02001078 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -07001079 {
1080 rtc::CritScope cs(&bitrate_crit_);
1081 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
1082 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001083 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001084}
1085
Alex Narest78609d52017-10-20 10:37:47 +02001086void Call::SetBitrateAllocationStrategy(
1087 std::unique_ptr<rtc::BitrateAllocationStrategy>
1088 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001089 // TODO(srte): This function should be moved to RtpTransportControllerSend
1090 // when BitrateAllocator is moved there.
1091 struct Functor {
1092 void operator()() {
1093 bitrate_allocator_->SetBitrateAllocationStrategy(
1094 std::move(bitrate_allocation_strategy_));
1095 }
1096 BitrateAllocator* bitrate_allocator_;
1097 std::unique_ptr<rtc::BitrateAllocationStrategy>
1098 bitrate_allocation_strategy_;
1099 };
1100 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
1101 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 10:37:47 +02001102}
1103
skvlad7a43d252016-03-22 15:32:27 -07001104void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001105 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001106 switch (media) {
1107 case MediaType::AUDIO:
1108 audio_network_state_ = state;
1109 break;
1110 case MediaType::VIDEO:
1111 video_network_state_ = state;
1112 break;
1113 case MediaType::ANY:
1114 case MediaType::DATA:
1115 RTC_NOTREACHED();
1116 break;
1117 }
1118
1119 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001120 {
skvlad7a43d252016-03-22 15:32:27 -07001121 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -07001122 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001123 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -07001124 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001125 }
1126 {
skvlad7a43d252016-03-22 15:32:27 -07001127 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001128 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1129 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001130 }
nissee4bcd6d2017-05-16 04:47:04 -07001131 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1132 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001133 }
1134 }
1135}
1136
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001137void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1138 ReadLockScoped read_lock(*send_crit_);
1139 for (auto& kv : audio_send_ssrcs_) {
1140 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -08001141 }
1142}
1143
skvlad7a43d252016-03-22 15:32:27 -07001144void Call::UpdateAggregateNetworkState() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001145 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001146
1147 bool have_audio = false;
1148 bool have_video = false;
1149 {
1150 ReadLockScoped read_lock(*send_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001151 if (!audio_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001152 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001153 if (!video_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001154 have_video = true;
1155 }
1156 {
1157 ReadLockScoped read_lock(*receive_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001158 if (!audio_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001159 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001160 if (!video_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001161 have_video = true;
1162 }
1163
Sebastian Janssona06e9192018-03-07 18:49:55 +01001164 bool aggregate_network_up =
1165 ((have_video && video_network_state_ == kNetworkUp) ||
1166 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001167
Mirko Bonadei675513b2017-11-09 11:09:25 +01001168 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001169 << (aggregate_network_up ? "up" : "down");
1170 {
1171 rtc::CritScope cs(&aggregate_network_up_crit_);
1172 aggregate_network_up_ = aggregate_network_up;
1173 }
Sebastian Janssone6256052018-05-04 14:08:15 +02001174 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001175}
1176
stefanc1aeaf02015-10-15 07:26:07 -07001177void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001178 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1179 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001180 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001181}
1182
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001183void Call::OnStartRateUpdate(DataRate start_rate) {
1184 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1185 transport_send_ptr_->GetWorkerQueue()->PostTask(
1186 [this, start_rate] { this->OnStartRateUpdate(start_rate); });
1187 return;
1188 }
1189 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1190}
1191
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001192void Call::OnTargetTransferRate(TargetTransferRate msg) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001193 // TODO(bugs.webrtc.org/9719)
1194 // Call::OnTargetTransferRate requires that on target transfer rate is invoked
1195 // from the worker queue (because bitrate_allocator_ requires it). Media
1196 // transport does not guarantee the callback on the worker queue.
1197 // When the threading model for MediaTransportInterface is update, reconsider
1198 // changing this implementation.
1199 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1200 transport_send_ptr_->GetWorkerQueue()->PostTask(
1201 [this, msg] { this->OnTargetTransferRate(msg); });
1202 return;
1203 }
1204
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001205 uint32_t target_bitrate_bps = msg.target_rate.bps();
1206 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1207 uint8_t fraction_loss =
1208 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1209 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1210 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1211 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1212 {
1213 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1214 last_bandwidth_bps_ = bandwidth_bps;
1215 }
nisse559af382017-03-21 06:41:12 -07001216 // For controlling the rate of feedback messages.
1217 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +01001218 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, bandwidth_bps,
1219 fraction_loss, rtt_ms,
1220 probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001221
asaperssonce2e1362016-09-09 00:13:35 -07001222 // Ignore updates if bitrate is zero (the aggregate network state is down).
1223 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001224 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001225 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1226 pacer_bitrate_kbps_counter_.ProcessAndPause();
1227 return;
stefan18adf0a2015-11-17 06:24:56 -08001228 }
asaperssonce2e1362016-09-09 00:13:35 -07001229
1230 bool sending_video;
1231 {
1232 ReadLockScoped read_lock(*send_crit_);
1233 sending_video = !video_send_streams_.empty();
1234 }
1235
1236 rtc::CritScope lock(&bitrate_crit_);
1237 if (!sending_video) {
1238 // Do not update the stats if we are not sending video.
1239 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1240 pacer_bitrate_kbps_counter_.ProcessAndPause();
1241 return;
1242 }
1243 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1244 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1245 uint32_t pacer_bitrate_bps =
1246 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1247 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001248}
mflodman101f2502016-06-09 17:21:19 +02001249
perkj71ee44c2016-06-15 00:47:53 -07001250void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001251 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +01001252 uint32_t total_bitrate_bps) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001253 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001254 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001255
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -08001256 {
1257 rtc::CritScope lock(&target_observer_crit_);
1258 if (media_transport_) {
1259 MediaTransportAllocatedBitrateLimits limits;
1260 limits.min_pacing_rate = DataRate::bps(min_send_bitrate_bps);
1261 limits.max_padding_bitrate = DataRate::bps(max_padding_bitrate_bps);
1262 limits.max_total_allocated_bitrate = DataRate::bps(total_bitrate_bps);
1263 media_transport_->SetAllocatedBitrateLimits(limits);
1264 }
1265 }
1266
perkj71ee44c2016-06-15 00:47:53 -07001267 rtc::CritScope lock(&bitrate_crit_);
1268 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001269 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001270}
1271
pbos8fc7fa72015-07-15 08:02:58 -07001272void Call::ConfigureSync(const std::string& sync_group) {
1273 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001274 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001275 return;
1276
1277 AudioReceiveStream* sync_audio_stream = nullptr;
1278 // Find existing audio stream.
1279 const auto it = sync_stream_mapping_.find(sync_group);
1280 if (it != sync_stream_mapping_.end()) {
1281 sync_audio_stream = it->second;
1282 } else {
1283 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001284 for (AudioReceiveStream* stream : audio_receive_streams_) {
1285 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001286 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001287 RTC_LOG(LS_WARNING)
1288 << "Attempting to sync more than one audio stream "
1289 "within the same sync group. This is not "
1290 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001291 break;
1292 }
nissee4bcd6d2017-05-16 04:47:04 -07001293 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001294 }
1295 }
1296 }
1297 if (sync_audio_stream)
1298 sync_stream_mapping_[sync_group] = sync_audio_stream;
1299 size_t num_synced_streams = 0;
1300 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1301 if (video_stream->config().sync_group != sync_group)
1302 continue;
1303 ++num_synced_streams;
1304 if (num_synced_streams > 1) {
1305 // TODO(pbos): Support synchronizing more than one A/V pair.
1306 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001307 RTC_LOG(LS_WARNING)
1308 << "Attempting to sync more than one audio/video pair "
1309 "within the same sync group. This is not supported in "
1310 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001311 }
1312 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001313 if (num_synced_streams == 1) {
1314 // sync_audio_stream may be null and that's ok.
1315 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001316 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001317 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001318 }
1319 }
1320}
1321
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001322PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1323 const uint8_t* packet,
1324 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001325 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001326 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001327 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1328 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001329 if (received_bytes_per_second_counter_.HasSample()) {
1330 // First RTP packet has been received.
1331 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1332 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1333 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001334 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001335 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001336 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001337 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001338 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001339 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001340 }
1341 }
1342 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1343 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001344 for (AudioReceiveStream* stream : audio_receive_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001345 stream->DeliverRtcp(packet, length);
1346 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001347 }
1348 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001349 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001350 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001351 for (VideoSendStream* stream : video_send_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001352 stream->DeliverRtcp(packet, length);
1353 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001354 }
1355 }
mflodman3d7db262016-04-29 00:57:13 -07001356 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1357 ReadLockScoped read_lock(*send_crit_);
1358 for (auto& kv : audio_send_ssrcs_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001359 kv.second->DeliverRtcp(packet, length);
1360 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001361 }
1362 }
1363
Elad Alon4a87e1c2017-10-03 16:11:34 +02001364 if (rtcp_delivered) {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001365 event_log_->Log(absl::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001366 rtc::MakeArrayView(packet, length)));
1367 }
mflodman3d7db262016-04-29 00:57:13 -07001368
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001369 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001370}
1371
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001372PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001373 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001374 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001375 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001376
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001377 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001378 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001379 return DELIVERY_PACKET_ERROR;
1380
Niels Möller70082872018-08-07 11:03:12 +02001381 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001382 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001383 // Repair packet_time_us for clock resets by comparing a new read of
1384 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001385 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001386 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001387 }
Niels Möller70082872018-08-07 11:03:12 +02001388 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001389 } else {
1390 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1391 }
nissed44ce052017-02-06 02:23:00 -08001392
sprangc1abde72017-07-11 03:56:21 -07001393 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1394 // These are empty (zero length payload) RTP packets with an unsignaled
1395 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001396 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001397
1398 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1399 is_keep_alive_packet);
1400
sprangc1abde72017-07-11 03:56:21 -07001401 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001402 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001403 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001404 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1405 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001406 // Destruction of the receive stream, including deregistering from the
1407 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1408 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1409 // So by not passing the packet on to demuxing in this case, we prevent
1410 // incoming packets to be passed on via the demuxer to a receive stream
1411 // which is being torned down.
1412 return DELIVERY_UNKNOWN_SSRC;
1413 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001414
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001415 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001416
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001417 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001418
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001419 // RateCounters expect input parameter as int, save it as int,
1420 // instead of converting each time it is passed to RateCounter::Add below.
1421 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001422 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001423 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001424 received_bytes_per_second_counter_.Add(length);
1425 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001426 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001427 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001428 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001429 if (!first_received_rtp_audio_ms_) {
1430 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1431 }
1432 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001433 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001434 }
nissee4bcd6d2017-05-16 04:47:04 -07001435 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001436 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001437 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001438 received_bytes_per_second_counter_.Add(length);
1439 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001440 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001441 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001442 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001443 if (!first_received_rtp_video_ms_) {
1444 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1445 }
1446 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001447 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001448 }
1449 }
1450 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001451}
1452
stefan68786d22015-09-08 05:36:15 -07001453PacketReceiver::DeliveryStatus Call::DeliverPacket(
1454 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001455 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001456 int64_t packet_time_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001457 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001458 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1459 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001460
Niels Möller70082872018-08-07 11:03:12 +02001461 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001462}
1463
nissed2ef3142017-05-11 08:00:58 -07001464void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001465 RtpPacketReceived parsed_packet;
1466 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001467 return;
1468
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001469 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001470
brandtrcaea68f2017-08-23 00:55:17 -07001471 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001472 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001473 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001474 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1475 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001476 // Destruction of the receive stream, including deregistering from the
1477 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1478 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1479 // So by not passing the packet on to demuxing in this case, we prevent
1480 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001481 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001482 return;
1483 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001484 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001485
1486 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001487 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001488 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001489}
1490
nissed44ce052017-02-06 02:23:00 -08001491void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1492 MediaType media_type) {
1493 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001494 bool use_send_side_bwe =
1495 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001496
brandtrb29e6522016-12-21 06:37:18 -08001497 RTPHeader header;
1498 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001499
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001500 ReceivedPacket packet_msg;
1501 packet_msg.size = DataSize::bytes(packet.payload_size());
1502 packet_msg.receive_time = Timestamp::ms(packet.arrival_time_ms());
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001503 if (header.extension.hasAbsoluteSendTime) {
1504 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1505 }
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001506 transport_send_ptr_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001507
nisse4709e892017-02-07 01:18:43 -08001508 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001509 // Inconsistent configuration of send side BWE. Do nothing.
1510 // TODO(nisse): Without this check, we may produce RTCP feedback
1511 // packets even when not negotiated. But it would be cleaner to
1512 // move the check down to RTCPSender::SendFeedbackPacket, which
1513 // would also help the PacketRouter to select an appropriate rtp
1514 // module in the case that some, but not all, have RTCP feedback
1515 // enabled.
1516 return;
1517 }
1518 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001519 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001520 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001521 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001522 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1523 header);
1524 }
brandtrb29e6522016-12-21 06:37:18 -08001525}
1526
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001527} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001528
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001529} // namespace webrtc