blob: 25f1cd9b24744f311a3b9a00688b91051ce6c711 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/optional.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020020#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "audio/audio_receive_stream.h"
22#include "audio/audio_send_stream.h"
23#include "audio/audio_state.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "audio/time_interval.h"
25#include "call/bitrate_allocator.h"
26#include "call/call.h"
27#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010028#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "call/rtp_stream_receiver_controller.h"
30#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020031#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
32#include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h"
33#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/bitrate_controller/include/bitrate_controller.h"
40#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
41#include "modules/rtp_rtcp/include/flexfec_receiver.h"
42#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
43#include "modules/rtp_rtcp/include/rtp_header_parser.h"
44#include "modules/rtp_rtcp/source/byte_io.h"
45#include "modules/rtp_rtcp/source/rtp_packet_received.h"
46#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010047#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "rtc_base/basictypes.h"
49#include "rtc_base/checks.h"
50#include "rtc_base/constructormagic.h"
51#include "rtc_base/location.h"
52#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 15:59:12 +010053#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/ptr_util.h"
Sebastian Jansson45087cd2018-03-01 15:56:57 +010055#include "rtc_base/rate_limiter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "rtc_base/sequenced_task_checker.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020057#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020058#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/task_queue.h"
60#include "rtc_base/thread_annotations.h"
61#include "rtc_base/trace_event.h"
62#include "system_wrappers/include/clock.h"
63#include "system_wrappers/include/cpu_info.h"
64#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "video/call_stats.h"
66#include "video/send_delay_stats.h"
67#include "video/stats_counter.h"
68#include "video/video_receive_stream.h"
69#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000070
71namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000072
nisse4709e892017-02-07 01:18:43 -080073namespace {
Sebastian Jansson45087cd2018-03-01 15:56:57 +010074static const int64_t kRetransmitWindowSizeMs = 500;
nisse4709e892017-02-07 01:18:43 -080075
76// TODO(nisse): This really begs for a shared context struct.
77bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
78 bool transport_cc) {
79 if (!transport_cc)
80 return false;
81 for (const auto& extension : extensions) {
82 if (extension.uri == RtpExtension::kTransportSequenceNumberUri)
83 return true;
84 }
85 return false;
86}
87
88bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
89 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
90}
91
92bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
93 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
94}
95
96bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
97 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
98}
99
nisse26e3abb2017-08-25 04:44:25 -0700100const int* FindKeyByValue(const std::map<int, int>& m, int v) {
101 for (const auto& kv : m) {
102 if (kv.second == v)
103 return &kv.first;
104 }
105 return nullptr;
106}
107
eladalon8ec568a2017-09-08 06:15:52 -0700108std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700109 const VideoReceiveStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700110 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
111 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
112 rtclog_config->local_ssrc = config.rtp.local_ssrc;
113 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
114 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
115 rtclog_config->remb = config.rtp.remb;
116 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700117
118 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700119 const int* search =
120 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
eladalon8ec568a2017-09-08 06:15:52 -0700121 rtclog_config->codecs.emplace_back(d.payload_name, d.payload_type,
nisse26e3abb2017-08-25 04:44:25 -0700122 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700123 }
124 return rtclog_config;
125}
126
eladalon8ec568a2017-09-08 06:15:52 -0700127std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700128 const VideoSendStream::Config& config,
129 size_t ssrc_index) {
eladalon8ec568a2017-09-08 06:15:52 -0700130 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
131 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700132 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700133 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700134 }
eladalon8ec568a2017-09-08 06:15:52 -0700135 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
136 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700137
Niels Möller259a4972018-04-05 15:36:51 +0200138 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
139 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700140 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700141 return rtclog_config;
142}
143
eladalon8ec568a2017-09-08 06:15:52 -0700144std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700145 const AudioReceiveStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700146 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
147 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
148 rtclog_config->local_ssrc = config.rtp.local_ssrc;
149 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700150 return rtclog_config;
151}
152
eladalon8ec568a2017-09-08 06:15:52 -0700153std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjf4726992017-05-22 10:12:26 -0700154 const AudioSendStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700155 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
156 rtclog_config->local_ssrc = config.rtp.ssrc;
157 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjf4726992017-05-22 10:12:26 -0700158 if (config.send_codec_spec) {
eladalon8ec568a2017-09-08 06:15:52 -0700159 rtclog_config->codecs.emplace_back(config.send_codec_spec->format.name,
160 config.send_codec_spec->payload_type, 0);
perkjf4726992017-05-22 10:12:26 -0700161 }
162 return rtclog_config;
163}
164
nisse4709e892017-02-07 01:18:43 -0800165} // namespace
166
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000167namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000168
Sebastian Janssone6256052018-05-04 14:08:15 +0200169class Call final : public webrtc::Call,
170 public PacketReceiver,
171 public RecoveredPacketReceiver,
172 public TargetTransferRateObserver,
173 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000174 public:
nisseb8f9a322017-03-27 05:36:15 -0700175 Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700176 std::unique_ptr<RtpTransportControllerSendInterface> transport_send);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000177 virtual ~Call();
178
brandtr25445d32016-10-23 23:37:14 -0700179 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000180 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000181
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200182 webrtc::AudioSendStream* CreateAudioSendStream(
183 const webrtc::AudioSendStream::Config& config) override;
184 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
185
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200186 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
187 const webrtc::AudioReceiveStream::Config& config) override;
188 void DestroyAudioReceiveStream(
189 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000190
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200191 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700192 webrtc::VideoSendStream::Config config,
193 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100194 webrtc::VideoSendStream* CreateVideoSendStream(
195 webrtc::VideoSendStream::Config config,
196 VideoEncoderConfig encoder_config,
197 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000198 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000199
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200200 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200201 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000202 void DestroyVideoReceiveStream(
203 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000204
brandtr7250b392016-12-19 01:13:46 -0800205 FlexfecReceiveStream* CreateFlexfecReceiveStream(
206 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700207 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800208 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700209
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100210 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
211
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000212 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000213
brandtr25445d32016-10-23 23:37:14 -0700214 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700215 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100216 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -0700217 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000218
brandtr4e523862016-10-18 23:50:45 -0700219 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700220 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700221
Alex Narest78609d52017-10-20 10:37:47 +0200222 void SetBitrateAllocationStrategy(
223 std::unique_ptr<rtc::BitrateAllocationStrategy>
224 bitrate_allocation_strategy) override;
225
skvlad7a43d252016-03-22 15:32:27 -0700226 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000227
michaelt79e05882016-11-08 02:50:09 -0800228 void OnTransportOverheadChanged(MediaType media,
229 int transport_overhead_per_packet) override;
230
stefanc1aeaf02015-10-15 07:26:07 -0700231 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
232
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100233 // Implements TargetTransferRateObserver,
234 void OnTargetTransferRate(TargetTransferRate msg) override;
mflodman0e7e2592015-11-12 21:02:42 -0800235
perkj71ee44c2016-06-15 00:47:53 -0700236 // Implements BitrateAllocator::LimitObserver.
237 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +0100238 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +0100239 uint32_t total_bitrate_bps,
240 bool has_packet_feedback) override;
perkj71ee44c2016-06-15 00:47:53 -0700241
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000242 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200243 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
244 size_t length);
stefan68786d22015-09-08 05:36:15 -0700245 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100246 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -0700247 const PacketTime& packet_time);
pbos8fc7fa72015-07-15 08:02:58 -0700248 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700249 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700250
nissed44ce052017-02-06 02:23:00 -0800251 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
252 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700253 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800254
asaperssonfc5e81c2017-04-19 23:28:53 -0700255 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700256 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800257 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700258 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700259 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800260
Peter Boströmd3c94472015-12-09 11:20:58 +0100261 Clock* const clock_;
stefan91d92602015-11-11 10:13:02 -0800262
Peter Boström45553ae2015-05-08 13:54:38 +0200263 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800264 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800265 const std::unique_ptr<CallStats> call_stats_;
266 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000267 Call::Config config_;
eladalonf3f5c0e2017-08-18 02:47:08 -0700268 rtc::SequencedTaskChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000269
skvlad7a43d252016-03-22 15:32:27 -0700270 NetworkState audio_network_state_;
271 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100272 rtc::CriticalSection aggregate_network_up_crit_;
273 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000274
kwibergb25345e2016-03-12 06:10:44 -0800275 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700276 // Audio, Video, and FlexFEC receive streams are owned by the client that
277 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700278 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700279 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200280 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700281 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700282
pbos8fc7fa72015-07-15 08:02:58 -0700283 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700284 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000285
nisse0f15f922017-06-21 01:05:22 -0700286 // TODO(nisse): Should eventually be injected at creation,
287 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700288 RtpStreamReceiverController audio_receiver_controller_;
289 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700290
nissed44ce052017-02-06 02:23:00 -0800291 // This extra map is used for receive processing which is
292 // independent of media type.
293
294 // TODO(nisse): In the RTP transport refactoring, we should have a
295 // single mapping from ssrc to a more abstract receive stream, with
296 // accessor methods for all configuration we need at this level.
297 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100298 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
299 : extensions(config.rtp.extensions),
300 use_send_side_bwe(UseSendSideBwe(config)) {}
301 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
302 : extensions(config.rtp.extensions),
303 use_send_side_bwe(UseSendSideBwe(config)) {}
304 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
305 : extensions(config.rtp_header_extensions),
306 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800307
308 // Registered RTP header extensions for each stream. Note that RTP header
309 // extensions are negotiated per track ("m= line") in the SDP, but we have
310 // no notion of tracks at the Call level. We therefore store the RTP header
311 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100312 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800313 // Set if both RTP extension the RTCP feedback message needed for
314 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100315 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800316 };
317 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700318 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800319
kwibergb25345e2016-03-12 06:10:44 -0800320 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700321 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700322 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
323 RTC_GUARDED_BY(send_crit_);
324 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
325 RTC_GUARDED_BY(send_crit_);
326 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000327
ossuc3d4b482017-05-23 06:07:11 -0700328 using RtpStateMap = std::map<uint32_t, RtpState>;
329 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700330 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700331 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700332 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700333
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200334 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
335 RtpPayloadStateMap suspended_video_payload_states_
336 RTC_GUARDED_BY(configuration_sequence_checker_);
337
skvlad11a9cbf2016-10-07 11:53:05 -0700338 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700339
stefan18adf0a2015-11-17 06:24:56 -0800340 // The following members are only accessed (exclusively) from one thread and
341 // from the destructor, and therefore doesn't need any explicit
342 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700343 RateCounter received_bytes_per_second_counter_;
344 RateCounter received_audio_bytes_per_second_counter_;
345 RateCounter received_video_bytes_per_second_counter_;
346 RateCounter received_rtcp_bytes_per_second_counter_;
saza0d7f04d2017-07-04 04:05:06 -0700347 rtc::Optional<int64_t> first_received_rtp_audio_ms_;
348 rtc::Optional<int64_t> last_received_rtp_audio_ms_;
349 rtc::Optional<int64_t> first_received_rtp_video_ms_;
350 rtc::Optional<int64_t> last_received_rtp_video_ms_;
sazac58f8c02017-07-19 00:39:19 -0700351 TimeInterval sent_rtp_audio_timer_ms_;
stefan91d92602015-11-11 10:13:02 -0800352
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100353 rtc::CriticalSection last_bandwidth_bps_crit_;
354 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800355 // TODO(holmer): Remove this lock once BitrateController no longer calls
356 // OnNetworkChanged from multiple threads.
357 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700358 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
359 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
360 AvgCounter estimated_send_bitrate_kbps_counter_
361 RTC_GUARDED_BY(&bitrate_crit_);
362 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800363
Sebastian Jansson45087cd2018-03-01 15:56:57 +0100364 RateLimiter retransmission_rate_limiter_;
nisse559af382017-03-21 06:41:12 -0700365 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100366
367 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
368
asapersson35151f32016-05-02 23:44:01 -0700369 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700370 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800371
Sebastian Janssone6256052018-05-04 14:08:15 +0200372 // Caches transport_send_.get(), to avoid racing with destructor.
373 // Note that this is declared before transport_send_ to ensure that it is not
374 // invalidated until no more tasks can be running on the transport_send_ task
375 // queue.
376 RtpTransportControllerSendInterface* transport_send_ptr_;
377 // Declared last since it will issue callbacks from a task queue. Declaring it
378 // last ensures that it is destroyed first and any running tasks are finished.
379 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
henrikg3c089d72015-09-16 05:37:44 -0700380 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000381};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000382} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000383
asapersson2e5cfcd2016-08-11 08:41:18 -0700384std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200385 char buf[1024];
386 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700387 ss << "Call stats: " << time_ms << ", {";
388 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
389 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
390 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
391 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
392 ss << "rtt_ms: " << rtt_ms;
393 ss << '}';
394 return ss.str();
395}
396
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000397Call* Call::Create(const Call::Config& config) {
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100398 return new internal::Call(
Sebastian Janssondfce03a2018-05-18 18:05:10 +0200399 config, rtc::MakeUnique<RtpTransportControllerSend>(
400 Clock::GetRealTimeClock(), config.event_log,
401 config.network_controller_factory, config.bitrate_config));
zstein7cb69d52017-05-08 11:52:38 -0700402}
403
404Call* Call::Create(
405 const Call::Config& config,
406 std::unique_ptr<RtpTransportControllerSendInterface> transport_send) {
407 return new internal::Call(config, std::move(transport_send));
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000408}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000409
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100410// This method here to avoid subclasses has to implement this method.
411// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
412// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100413VideoSendStream* Call::CreateVideoSendStream(
414 VideoSendStream::Config config,
415 VideoEncoderConfig encoder_config,
416 std::unique_ptr<FecController> fec_controller) {
417 return nullptr;
418}
419
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000420namespace internal {
421
nisseb8f9a322017-03-27 05:36:15 -0700422Call::Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700423 std::unique_ptr<RtpTransportControllerSendInterface> transport_send)
stefan91d92602015-11-11 10:13:02 -0800424 : clock_(Clock::GetRealTimeClock()),
425 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
kwiberg1c7fdd82016-04-26 08:18:04 -0700426 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Tommi38c5d932018-03-27 23:11:09 +0200427 call_stats_(new CallStats(clock_, module_process_thread_.get())),
perkj71ee44c2016-06-15 00:47:53 -0700428 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200429 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800430 audio_network_state_(kNetworkDown),
431 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100432 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000433 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800434 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700435 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700436 received_bytes_per_second_counter_(clock_, nullptr, true),
437 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
438 received_video_bytes_per_second_counter_(clock_, nullptr, true),
439 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100440 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700441 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700442 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700443 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
444 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
Sebastian Jansson45087cd2018-03-01 15:56:57 +0100445 retransmission_rate_limiter_(clock_, kRetransmitWindowSizeMs),
nisse05843312017-04-18 23:38:35 -0700446 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100447 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700448 video_send_delay_stats_(new SendDelayStats(clock_)),
Sebastian Janssone6256052018-05-04 14:08:15 +0200449 start_ms_(clock_->TimeInMilliseconds()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700450 RTC_DCHECK(config.event_log != nullptr);
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100451 transport_send->RegisterTargetTransferRateObserver(this);
nisse6167b262017-04-06 06:34:25 -0700452 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 14:08:15 +0200453 transport_send_ptr_ = transport_send_.get();
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100454
nissebcbaf742017-03-28 01:16:25 -0700455 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100456 call_stats_->RegisterStatsObserver(transport_send_->GetCallStatsObserver());
Stefan Holmerc379fcb2016-02-24 16:02:55 +0100457
Sebastian Janssonc33c0fc2018-02-22 11:10:18 +0100458 module_process_thread_->RegisterModule(
stefan64136af2017-08-14 08:03:17 -0700459 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
stefan9e117c5e12017-08-16 08:16:25 -0700460 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
461 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
stefan9e117c5e12017-08-16 08:16:25 -0700462 module_process_thread_->Start();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000463}
464
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000465Call::~Call() {
eladalonf3f5c0e2017-08-18 02:47:08 -0700466 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700467
solenbergc7a8b082015-10-16 14:35:07 -0700468 RTC_CHECK(audio_send_ssrcs_.empty());
469 RTC_CHECK(video_send_ssrcs_.empty());
470 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700471 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700472 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000473
Sebastian Janssonc33c0fc2018-02-22 11:10:18 +0100474 module_process_thread_->DeRegisterModule(
nisse559af382017-03-21 06:41:12 -0700475 receive_side_cc_.GetRemoteBitrateEstimator(true));
nisse559af382017-03-21 06:41:12 -0700476 module_process_thread_->DeRegisterModule(&receive_side_cc_);
mflodmane3787022015-10-21 13:24:28 +0200477 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200478 module_process_thread_->Stop();
nissebcbaf742017-03-28 01:16:25 -0700479 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100480 call_stats_->DeregisterStatsObserver(transport_send_->GetCallStatsObserver());
sprang6d6122b2016-07-13 06:37:09 -0700481
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100482 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700483 // Only update histograms after process threads have been shut down, so that
484 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700485 {
486 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700487 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700488 }
sprang6d6122b2016-07-13 06:37:09 -0700489 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700490 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000491}
492
asapersson4374a092016-07-27 00:39:09 -0700493void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700494 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700495 "WebRTC.Call.LifetimeInSeconds",
496 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
497}
498
asaperssonfc5e81c2017-04-19 23:28:53 -0700499void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
500 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800501 return;
sazac58f8c02017-07-19 00:39:19 -0700502 if (!sent_rtp_audio_timer_ms_.Empty()) {
503 RTC_HISTOGRAM_COUNTS_100000(
504 "WebRTC.Call.TimeSendingAudioRtpPacketsInSeconds",
505 sent_rtp_audio_timer_ms_.Length() / 1000);
506 }
stefan18adf0a2015-11-17 06:24:56 -0800507 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700508 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800509 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
510 return;
asaperssonce2e1362016-09-09 00:13:35 -0700511 const int kMinRequiredPeriodicSamples = 5;
512 AggregatedStats send_bitrate_stats =
513 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
514 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700515 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
516 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100517 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
518 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800519 }
asaperssonce2e1362016-09-09 00:13:35 -0700520 AggregatedStats pacer_bitrate_stats =
521 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
522 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700523 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
524 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100525 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
526 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800527 }
528}
529
530void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700531 if (first_received_rtp_audio_ms_) {
532 RTC_HISTOGRAM_COUNTS_100000(
533 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
534 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
535 }
536 if (first_received_rtp_video_ms_) {
537 RTC_HISTOGRAM_COUNTS_100000(
538 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
539 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
540 }
asapersson250fd972016-09-08 00:07:21 -0700541 const int kMinRequiredPeriodicSamples = 5;
542 AggregatedStats video_bytes_per_sec =
543 received_video_bytes_per_second_counter_.GetStats();
544 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700545 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
546 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100547 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
548 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800549 }
asapersson250fd972016-09-08 00:07:21 -0700550 AggregatedStats audio_bytes_per_sec =
551 received_audio_bytes_per_second_counter_.GetStats();
552 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700553 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
554 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100555 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
556 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800557 }
asapersson250fd972016-09-08 00:07:21 -0700558 AggregatedStats rtcp_bytes_per_sec =
559 received_rtcp_bytes_per_second_counter_.GetStats();
560 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700561 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
562 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100563 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
564 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800565 }
asapersson250fd972016-09-08 00:07:21 -0700566 AggregatedStats recv_bytes_per_sec =
567 received_bytes_per_second_counter_.GetStats();
568 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700569 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
570 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100571 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
572 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700573 }
stefan91d92602015-11-11 10:13:02 -0800574}
575
solenberg5a289392015-10-19 03:39:20 -0700576PacketReceiver* Call::Receiver() {
eladalond1dd2f72017-08-25 02:55:57 -0700577 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700578 return this;
579}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000580
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200581webrtc::AudioSendStream* Call::CreateAudioSendStream(
582 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700583 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700584 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200585 event_log_->Log(rtc::MakeUnique<RtcEventAudioSendStreamConfig>(
586 CreateRtcLogStreamConfig(config)));
ossuc3d4b482017-05-23 06:07:11 -0700587
588 rtc::Optional<RtpState> suspended_rtp_state;
589 {
590 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
591 if (iter != suspended_audio_send_ssrcs_.end()) {
592 suspended_rtp_state.emplace(iter->second);
593 }
594 }
595
Sebastian Janssone6256052018-05-04 14:08:15 +0200596 // TODO(srte): AudioSendStream should call GetWorkerQueue directly rather than
597 // having it injected.
598
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100599 AudioSendStream* send_stream = new AudioSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200600 config, config_.audio_state, transport_send_ptr_->GetWorkerQueue(),
601 module_process_thread_.get(), transport_send_ptr_,
602 bitrate_allocator_.get(), event_log_, call_stats_.get(),
603 suspended_rtp_state, &sent_rtp_audio_timer_ms_);
solenbergc7a8b082015-10-16 14:35:07 -0700604 {
solenbergc7a8b082015-10-16 14:35:07 -0700605 WriteLockScoped write_lock(*send_crit_);
606 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
607 audio_send_ssrcs_.end());
608 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700609 }
solenberg7602aab2016-11-14 11:30:07 -0800610 {
611 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700612 for (AudioReceiveStream* stream : audio_receive_streams_) {
613 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
614 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800615 }
616 }
617 }
skvlad7a43d252016-03-22 15:32:27 -0700618 send_stream->SignalNetworkState(audio_network_state_);
619 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700620 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200621}
622
623void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700624 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700625 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700626 RTC_DCHECK(send_stream != nullptr);
627
628 send_stream->Stop();
629
eladalonabbc4302017-07-26 02:09:44 -0700630 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700631 webrtc::internal::AudioSendStream* audio_send_stream =
632 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700633 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700634 {
635 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800636 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
637 RTC_DCHECK_EQ(1, num_deleted);
638 }
639 {
640 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700641 for (AudioReceiveStream* stream : audio_receive_streams_) {
642 if (stream->config().rtp.local_ssrc == ssrc) {
643 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800644 }
645 }
solenbergc7a8b082015-10-16 14:35:07 -0700646 }
skvlad7a43d252016-03-22 15:32:27 -0700647 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700648 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200649}
650
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200651webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
652 const webrtc::AudioReceiveStream::Config& config) {
653 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700654 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200655 event_log_->Log(rtc::MakeUnique<RtcEventAudioReceiveStreamConfig>(
656 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700657 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200658 &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100659 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200660 {
661 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100662 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
663 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700664 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800665
pbos8fc7fa72015-07-15 08:02:58 -0700666 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200667 }
solenberg7602aab2016-11-14 11:30:07 -0800668 {
669 ReadLockScoped read_lock(*send_crit_);
670 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
671 if (it != audio_send_ssrcs_.end()) {
672 receive_stream->AssociateSendStream(it->second);
673 }
674 }
skvlad7a43d252016-03-22 15:32:27 -0700675 receive_stream->SignalNetworkState(audio_network_state_);
676 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200677 return receive_stream;
678}
679
680void Call::DestroyAudioReceiveStream(
681 webrtc::AudioReceiveStream* receive_stream) {
682 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700683 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700684 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700685 webrtc::internal::AudioReceiveStream* audio_receive_stream =
686 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200687 {
688 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800689 const AudioReceiveStream::Config& config = audio_receive_stream->config();
690 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700691 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800692 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700693 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700694 const std::string& sync_group = audio_receive_stream->config().sync_group;
695 const auto it = sync_stream_mapping_.find(sync_group);
696 if (it != sync_stream_mapping_.end() &&
697 it->second == audio_receive_stream) {
698 sync_stream_mapping_.erase(it);
699 ConfigureSync(sync_group);
700 }
nissed44ce052017-02-06 02:23:00 -0800701 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200702 }
skvlad7a43d252016-03-22 15:32:27 -0700703 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200704 delete audio_receive_stream;
705}
706
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100707// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100708webrtc::VideoSendStream* Call::CreateVideoSendStream(
709 webrtc::VideoSendStream::Config config,
710 VideoEncoderConfig encoder_config,
711 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000712 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700713 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000714
asapersson35151f32016-05-02 23:44:01 -0700715 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700716 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
717 ++ssrc_index) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200718 event_log_->Log(rtc::MakeUnique<RtcEventVideoSendStreamConfig>(
719 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700720 }
perkj26091b12016-09-01 01:17:40 -0700721
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000722 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
723 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700724 // Copy ssrcs from |config| since |config| is moved.
725 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100726
Sebastian Janssone6256052018-05-04 14:08:15 +0200727 // TODO(srte): VideoSendStream should call GetWorkerQueue directly rather than
728 // having it injected.
mflodman0c478b32015-10-21 15:52:16 +0200729 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200730 num_cpu_cores_, module_process_thread_.get(),
731 transport_send_ptr_->GetWorkerQueue(), call_stats_.get(),
732 transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700733 video_send_delay_stats_.get(), event_log_, std::move(config),
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200734 std::move(encoder_config), suspended_video_send_ssrcs_,
Sebastian Jansson25e51102018-03-01 15:56:47 +0100735 suspended_video_payload_states_, std::move(fec_controller),
Sebastian Jansson45087cd2018-03-01 15:56:57 +0100736 &retransmission_rate_limiter_);
perkj26091b12016-09-01 01:17:40 -0700737
skvlad7a43d252016-03-22 15:32:27 -0700738 {
739 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700740 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700741 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
742 video_send_ssrcs_[ssrc] = send_stream;
743 }
744 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000745 }
skvlad7a43d252016-03-22 15:32:27 -0700746 send_stream->SignalNetworkState(video_network_state_);
747 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700748
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000749 return send_stream;
750}
751
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100752webrtc::VideoSendStream* Call::CreateVideoSendStream(
753 webrtc::VideoSendStream::Config config,
754 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100755 if (config_.fec_controller_factory) {
756 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
757 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100758 std::unique_ptr<FecController> fec_controller =
759 config_.fec_controller_factory
760 ? config_.fec_controller_factory->CreateFecController()
761 : rtc::MakeUnique<FecControllerDefault>(Clock::GetRealTimeClock());
762 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
763 std::move(fec_controller));
764}
765
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000766void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000767 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700768 RTC_DCHECK(send_stream != nullptr);
eladalonf3f5c0e2017-08-18 02:47:08 -0700769 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000770
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000771 send_stream->Stop();
772
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000773 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000774 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000775 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200776 auto it = video_send_ssrcs_.begin();
777 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000778 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
779 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200780 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000781 } else {
782 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000783 }
784 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200785 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000786 }
henrikg91d6ede2015-09-17 00:24:34 -0700787 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000788
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200789 VideoSendStream::RtpStateMap rtp_states;
790 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
791 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
792 &rtp_payload_states);
793 for (const auto& kv : rtp_states) {
794 suspended_video_send_ssrcs_[kv.first] = kv.second;
795 }
796 for (const auto& kv : rtp_payload_states) {
797 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000798 }
799
skvlad7a43d252016-03-22 15:32:27 -0700800 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000801 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000802}
803
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200804webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200805 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000806 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700807 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800808
nisse0f15f922017-06-21 01:05:22 -0700809 VideoReceiveStream* receive_stream = new VideoReceiveStream(
eladalon2a2b2972017-07-03 09:25:27 -0700810 &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200811 transport_send_ptr_->packet_router(), std::move(configuration),
nisse0f15f922017-06-21 01:05:22 -0700812 module_process_thread_.get(), call_stats_.get());
Tommi733b5472016-06-10 17:58:01 +0200813
814 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700815 {
816 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800817 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800818 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700819 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800820 // type, we may get an incorrect value for the rtx stream, but
821 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100822 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
823 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800824 }
Erik Språng09708512018-03-14 15:16:50 +0100825 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
826 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700827 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700828 ConfigureSync(config.sync_group);
829 }
830 receive_stream->SignalNetworkState(video_network_state_);
831 UpdateAggregateNetworkState();
Elad Alon4a87e1c2017-10-03 16:11:34 +0200832 event_log_->Log(rtc::MakeUnique<RtcEventVideoReceiveStreamConfig>(
833 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000834 return receive_stream;
835}
836
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000837void Call::DestroyVideoReceiveStream(
838 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000839 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700840 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700841 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700842 VideoReceiveStream* receive_stream_impl =
843 static_cast<VideoReceiveStream*>(receive_stream);
844 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000845 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000846 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000847 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
848 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700849 receive_rtp_config_.erase(config.rtp.remote_ssrc);
850 if (config.rtp.rtx_ssrc) {
851 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000852 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200853 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700854 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000855 }
nisse4709e892017-02-07 01:18:43 -0800856
nisse559af382017-03-21 06:41:12 -0700857 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800858 ->RemoveStream(config.rtp.remote_ssrc);
859
skvlad7a43d252016-03-22 15:32:27 -0700860 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000861 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000862}
863
brandtr7250b392016-12-19 01:13:46 -0800864FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
865 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700866 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700867 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800868
869 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700870
nisse0f15f922017-06-21 01:05:22 -0700871 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700872 {
873 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -0700874 // Unlike the video and audio receive streams,
875 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
876 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -0700877 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -0700878 // constructor while holding |receive_crit_| ensures that we don't
879 // call OnRtpPacket until the constructor is finished and the
880 // object is in a valid state.
881 // TODO(nisse): Fix constructor so that it can be moved outside of
882 // this locked scope.
883 receive_stream = new FlexfecReceiveStreamImpl(
eladalon2a2b2972017-07-03 09:25:27 -0700884 &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +0200885 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -0800886
nissed44ce052017-02-06 02:23:00 -0800887 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
888 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +0100889 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -0700890 }
brandtrb29e6522016-12-21 06:37:18 -0800891
brandtr25445d32016-10-23 23:37:14 -0700892 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -0800893
brandtr25445d32016-10-23 23:37:14 -0700894 return receive_stream;
895}
896
brandtr7250b392016-12-19 01:13:46 -0800897void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -0700898 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700899 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800900
brandtr25445d32016-10-23 23:37:14 -0700901 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -0700902 {
903 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800904
eladalon42f44f92017-07-25 06:40:06 -0700905 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -0800906 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -0800907 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -0800908
brandtr7250b392016-12-19 01:13:46 -0800909 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
910 // destroyed.
nisse559af382017-03-21 06:41:12 -0700911 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800912 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -0700913 }
brandtrb29e6522016-12-21 06:37:18 -0800914
eladalon42f44f92017-07-25 06:40:06 -0700915 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700916}
917
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100918RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +0200919 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100920}
921
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000922Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700923 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
924 // thread. Re-enable once that is fixed.
eladalonf3f5c0e2017-08-18 02:47:08 -0700925 // RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000926 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200927 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +0200928 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000929 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -0700930 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700931 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100932
933 {
934 rtc::CritScope cs(&last_bandwidth_bps_crit_);
935 stats.send_bandwidth_bps = last_bandwidth_bps_;
936 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000937 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100938 // TODO(srte): It is unclear if we only want to report queues if network is
939 // available.
940 {
941 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 14:08:15 +0200942 stats.pacer_delay_ms = aggregate_network_up_
943 ? transport_send_ptr_->GetPacerQueuingDelayMs()
944 : 0;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100945 }
946
Tommi38c5d932018-03-27 23:11:09 +0200947 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -0700948 {
949 rtc::CritScope cs(&bitrate_crit_);
950 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
951 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000952 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000953}
954
Alex Narest78609d52017-10-20 10:37:47 +0200955void Call::SetBitrateAllocationStrategy(
956 std::unique_ptr<rtc::BitrateAllocationStrategy>
957 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 14:08:15 +0200958 // TODO(srte): This function should be moved to RtpTransportControllerSend
959 // when BitrateAllocator is moved there.
960 struct Functor {
961 void operator()() {
962 bitrate_allocator_->SetBitrateAllocationStrategy(
963 std::move(bitrate_allocation_strategy_));
964 }
965 BitrateAllocator* bitrate_allocator_;
966 std::unique_ptr<rtc::BitrateAllocationStrategy>
967 bitrate_allocation_strategy_;
968 };
969 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
970 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 10:37:47 +0200971}
972
skvlad7a43d252016-03-22 15:32:27 -0700973void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
eladalonf3f5c0e2017-08-18 02:47:08 -0700974 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -0700975 switch (media) {
976 case MediaType::AUDIO:
977 audio_network_state_ = state;
978 break;
979 case MediaType::VIDEO:
980 video_network_state_ = state;
981 break;
982 case MediaType::ANY:
983 case MediaType::DATA:
984 RTC_NOTREACHED();
985 break;
986 }
987
988 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000989 {
skvlad7a43d252016-03-22 15:32:27 -0700990 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700991 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -0700992 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -0700993 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200994 for (auto& kv : video_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -0700995 kv.second->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000996 }
997 }
998 {
skvlad7a43d252016-03-22 15:32:27 -0700999 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001000 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1001 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001002 }
nissee4bcd6d2017-05-16 04:47:04 -07001003 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1004 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001005 }
1006 }
1007}
1008
michaelt79e05882016-11-08 02:50:09 -08001009void Call::OnTransportOverheadChanged(MediaType media,
1010 int transport_overhead_per_packet) {
1011 switch (media) {
1012 case MediaType::AUDIO: {
1013 ReadLockScoped read_lock(*send_crit_);
1014 for (auto& kv : audio_send_ssrcs_) {
1015 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1016 }
1017 break;
1018 }
1019 case MediaType::VIDEO: {
1020 ReadLockScoped read_lock(*send_crit_);
1021 for (auto& kv : video_send_ssrcs_) {
1022 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1023 }
1024 break;
1025 }
1026 case MediaType::ANY:
1027 case MediaType::DATA:
1028 RTC_NOTREACHED();
1029 break;
1030 }
1031}
1032
skvlad7a43d252016-03-22 15:32:27 -07001033void Call::UpdateAggregateNetworkState() {
eladalonf3f5c0e2017-08-18 02:47:08 -07001034 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001035
1036 bool have_audio = false;
1037 bool have_video = false;
1038 {
1039 ReadLockScoped read_lock(*send_crit_);
1040 if (audio_send_ssrcs_.size() > 0)
1041 have_audio = true;
1042 if (video_send_ssrcs_.size() > 0)
1043 have_video = true;
1044 }
1045 {
1046 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001047 if (audio_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001048 have_audio = true;
nissee4bcd6d2017-05-16 04:47:04 -07001049 if (video_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001050 have_video = true;
1051 }
1052
Sebastian Janssona06e9192018-03-07 18:49:55 +01001053 bool aggregate_network_up =
1054 ((have_video && video_network_state_ == kNetworkUp) ||
1055 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001056
Mirko Bonadei675513b2017-11-09 11:09:25 +01001057 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001058 << (aggregate_network_up ? "up" : "down");
1059 {
1060 rtc::CritScope cs(&aggregate_network_up_crit_);
1061 aggregate_network_up_ = aggregate_network_up;
1062 }
Sebastian Janssone6256052018-05-04 14:08:15 +02001063 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001064}
1065
stefanc1aeaf02015-10-15 07:26:07 -07001066void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001067 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1068 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001069 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001070}
1071
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001072void Call::OnTargetTransferRate(TargetTransferRate msg) {
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001073 uint32_t target_bitrate_bps = msg.target_rate.bps();
1074 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1075 uint8_t fraction_loss =
1076 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1077 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1078 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1079 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1080 {
1081 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1082 last_bandwidth_bps_ = bandwidth_bps;
1083 }
1084 retransmission_rate_limiter_.SetMaxRate(bandwidth_bps);
nisse559af382017-03-21 06:41:12 -07001085 // For controlling the rate of feedback messages.
1086 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
perkj71ee44c2016-06-15 00:47:53 -07001087 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss,
minyue78b4d562016-11-30 04:47:39 -08001088 rtt_ms, probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001089
asaperssonce2e1362016-09-09 00:13:35 -07001090 // Ignore updates if bitrate is zero (the aggregate network state is down).
1091 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001092 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001093 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1094 pacer_bitrate_kbps_counter_.ProcessAndPause();
1095 return;
stefan18adf0a2015-11-17 06:24:56 -08001096 }
asaperssonce2e1362016-09-09 00:13:35 -07001097
1098 bool sending_video;
1099 {
1100 ReadLockScoped read_lock(*send_crit_);
1101 sending_video = !video_send_streams_.empty();
1102 }
1103
1104 rtc::CritScope lock(&bitrate_crit_);
1105 if (!sending_video) {
1106 // Do not update the stats if we are not sending video.
1107 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1108 pacer_bitrate_kbps_counter_.ProcessAndPause();
1109 return;
1110 }
1111 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1112 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1113 uint32_t pacer_bitrate_bps =
1114 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1115 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001116}
mflodman101f2502016-06-09 17:21:19 +02001117
perkj71ee44c2016-06-15 00:47:53 -07001118void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001119 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +01001120 uint32_t total_bitrate_bps,
1121 bool has_packet_feedback) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001122 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001123 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Janssone6256052018-05-04 14:08:15 +02001124 transport_send_ptr_->SetPerPacketFeedbackAvailable(has_packet_feedback);
perkj71ee44c2016-06-15 00:47:53 -07001125 rtc::CritScope lock(&bitrate_crit_);
1126 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001127 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001128}
1129
pbos8fc7fa72015-07-15 08:02:58 -07001130void Call::ConfigureSync(const std::string& sync_group) {
1131 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001132 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001133 return;
1134
1135 AudioReceiveStream* sync_audio_stream = nullptr;
1136 // Find existing audio stream.
1137 const auto it = sync_stream_mapping_.find(sync_group);
1138 if (it != sync_stream_mapping_.end()) {
1139 sync_audio_stream = it->second;
1140 } else {
1141 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001142 for (AudioReceiveStream* stream : audio_receive_streams_) {
1143 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001144 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001145 RTC_LOG(LS_WARNING)
1146 << "Attempting to sync more than one audio stream "
1147 "within the same sync group. This is not "
1148 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001149 break;
1150 }
nissee4bcd6d2017-05-16 04:47:04 -07001151 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001152 }
1153 }
1154 }
1155 if (sync_audio_stream)
1156 sync_stream_mapping_[sync_group] = sync_audio_stream;
1157 size_t num_synced_streams = 0;
1158 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1159 if (video_stream->config().sync_group != sync_group)
1160 continue;
1161 ++num_synced_streams;
1162 if (num_synced_streams > 1) {
1163 // TODO(pbos): Support synchronizing more than one A/V pair.
1164 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001165 RTC_LOG(LS_WARNING)
1166 << "Attempting to sync more than one audio/video pair "
1167 "within the same sync group. This is not supported in "
1168 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001169 }
1170 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001171 if (num_synced_streams == 1) {
1172 // sync_audio_stream may be null and that's ok.
1173 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001174 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001175 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001176 }
1177 }
1178}
1179
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001180PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1181 const uint8_t* packet,
1182 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001183 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001184 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001185 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1186 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001187 if (received_bytes_per_second_counter_.HasSample()) {
1188 // First RTP packet has been received.
1189 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1190 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1191 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001192 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001193 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001194 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001195 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001196 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001197 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001198 }
1199 }
1200 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1201 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001202 for (AudioReceiveStream* stream : audio_receive_streams_) {
1203 if (stream->DeliverRtcp(packet, length))
mflodman3d7db262016-04-29 00:57:13 -07001204 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001205 }
1206 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001207 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001208 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001209 for (VideoSendStream* stream : video_send_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001210 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001211 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001212 }
1213 }
mflodman3d7db262016-04-29 00:57:13 -07001214 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1215 ReadLockScoped read_lock(*send_crit_);
1216 for (auto& kv : audio_send_ssrcs_) {
1217 if (kv.second->DeliverRtcp(packet, length))
1218 rtcp_delivered = true;
1219 }
1220 }
1221
Elad Alon4a87e1c2017-10-03 16:11:34 +02001222 if (rtcp_delivered) {
1223 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketIncoming>(
1224 rtc::MakeArrayView(packet, length)));
1225 }
mflodman3d7db262016-04-29 00:57:13 -07001226
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001227 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001228}
1229
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001230PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001231 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -07001232 const PacketTime& packet_time) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001233 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001234
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001235 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001236 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001237 return DELIVERY_PACKET_ERROR;
1238
1239 if (packet_time.timestamp != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001240 int64_t timestamp_us = packet_time.timestamp;
1241 if (receive_time_calculator_) {
1242 timestamp_us = receive_time_calculator_->ReconcileReceiveTimes(
1243 packet_time.timestamp, clock_->TimeInMicroseconds());
1244 }
1245 parsed_packet.set_arrival_time_ms((timestamp_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001246 } else {
1247 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1248 }
nissed44ce052017-02-06 02:23:00 -08001249
sprangc1abde72017-07-11 03:56:21 -07001250 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1251 // These are empty (zero length payload) RTP packets with an unsignaled
1252 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001253 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001254
1255 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1256 is_keep_alive_packet);
1257
sprangc1abde72017-07-11 03:56:21 -07001258 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001259 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001260 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001261 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1262 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001263 // Destruction of the receive stream, including deregistering from the
1264 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1265 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1266 // So by not passing the packet on to demuxing in this case, we prevent
1267 // incoming packets to be passed on via the demuxer to a receive stream
1268 // which is being torned down.
1269 return DELIVERY_UNKNOWN_SSRC;
1270 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001271 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001272
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001273 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001274
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001275 // RateCounters expect input parameter as int, save it as int,
1276 // instead of converting each time it is passed to RateCounter::Add below.
1277 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001278 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001279 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001280 received_bytes_per_second_counter_.Add(length);
1281 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001282 event_log_->Log(
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001283 rtc::MakeUnique<RtcEventRtpPacketIncoming>(parsed_packet));
1284 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001285 if (!first_received_rtp_audio_ms_) {
1286 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1287 }
1288 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001289 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001290 }
nissee4bcd6d2017-05-16 04:47:04 -07001291 } else if (media_type == MediaType::VIDEO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001292 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001293 received_bytes_per_second_counter_.Add(length);
1294 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001295 event_log_->Log(
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001296 rtc::MakeUnique<RtcEventRtpPacketIncoming>(parsed_packet));
1297 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001298 if (!first_received_rtp_video_ms_) {
1299 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1300 }
1301 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001302 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001303 }
1304 }
1305 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001306}
1307
stefan68786d22015-09-08 05:36:15 -07001308PacketReceiver::DeliveryStatus Call::DeliverPacket(
1309 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001310 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -07001311 const PacketTime& packet_time) {
eladalond1dd2f72017-08-25 02:55:57 -07001312 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001313 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1314 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001315
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001316 return DeliverRtp(media_type, std::move(packet), packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001317}
1318
nissed2ef3142017-05-11 08:00:58 -07001319void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001320 RtpPacketReceived parsed_packet;
1321 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001322 return;
1323
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001324 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001325
brandtrcaea68f2017-08-23 00:55:17 -07001326 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001327 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001328 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001329 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1330 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001331 // Destruction of the receive stream, including deregistering from the
1332 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1333 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1334 // So by not passing the packet on to demuxing in this case, we prevent
1335 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001336 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001337 return;
1338 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001339 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001340
1341 // TODO(brandtr): Update here when we support protecting audio packets too.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001342 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001343}
1344
nissed44ce052017-02-06 02:23:00 -08001345void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1346 MediaType media_type) {
1347 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001348 bool use_send_side_bwe =
1349 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001350
brandtrb29e6522016-12-21 06:37:18 -08001351 RTPHeader header;
1352 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001353
nisse4709e892017-02-07 01:18:43 -08001354 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001355 // Inconsistent configuration of send side BWE. Do nothing.
1356 // TODO(nisse): Without this check, we may produce RTCP feedback
1357 // packets even when not negotiated. But it would be cleaner to
1358 // move the check down to RTCPSender::SendFeedbackPacket, which
1359 // would also help the PacketRouter to select an appropriate rtp
1360 // module in the case that some, but not all, have RTCP feedback
1361 // enabled.
1362 return;
1363 }
1364 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001365 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001366 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001367 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001368 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1369 header);
1370 }
brandtrb29e6522016-12-21 06:37:18 -08001371}
1372
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001373} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001374
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001375} // namespace webrtc