blob: 440cd3b0c42aa5c7eb5e9dd2413287e5c4638369 [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"
20#include "audio/audio_receive_stream.h"
21#include "audio/audio_send_stream.h"
22#include "audio/audio_state.h"
23#include "audio/scoped_voe_interface.h"
24#include "audio/time_interval.h"
25#include "call/bitrate_allocator.h"
26#include "call/call.h"
27#include "call/flexfec_receive_stream_impl.h"
28#include "call/rtp_stream_receiver_controller.h"
29#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020030#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
31#include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h"
32#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
33#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
34#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
35#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon99a81b62017-09-21 10:25:29 +020037#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "modules/bitrate_controller/include/bitrate_controller.h"
39#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"
46#include "rtc_base/basictypes.h"
47#include "rtc_base/checks.h"
48#include "rtc_base/constructormagic.h"
49#include "rtc_base/location.h"
50#include "rtc_base/logging.h"
51#include "rtc_base/ptr_util.h"
52#include "rtc_base/sequenced_task_checker.h"
53#include "rtc_base/task_queue.h"
54#include "rtc_base/thread_annotations.h"
55#include "rtc_base/trace_event.h"
56#include "system_wrappers/include/clock.h"
57#include "system_wrappers/include/cpu_info.h"
58#include "system_wrappers/include/metrics.h"
59#include "system_wrappers/include/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "video/call_stats.h"
61#include "video/send_delay_stats.h"
62#include "video/stats_counter.h"
63#include "video/video_receive_stream.h"
64#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000065
66namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000067
nisse4709e892017-02-07 01:18:43 -080068namespace {
69
70// TODO(nisse): This really begs for a shared context struct.
71bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
72 bool transport_cc) {
73 if (!transport_cc)
74 return false;
75 for (const auto& extension : extensions) {
76 if (extension.uri == RtpExtension::kTransportSequenceNumberUri)
77 return true;
78 }
79 return false;
80}
81
82bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
83 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
84}
85
86bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
87 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
88}
89
90bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
91 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
92}
93
nisse26e3abb2017-08-25 04:44:25 -070094const int* FindKeyByValue(const std::map<int, int>& m, int v) {
95 for (const auto& kv : m) {
96 if (kv.second == v)
97 return &kv.first;
98 }
99 return nullptr;
100}
101
eladalon8ec568a2017-09-08 06:15:52 -0700102std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700103 const VideoReceiveStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700104 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
105 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
106 rtclog_config->local_ssrc = config.rtp.local_ssrc;
107 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
108 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
109 rtclog_config->remb = config.rtp.remb;
110 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700111
112 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700113 const int* search =
114 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
eladalon8ec568a2017-09-08 06:15:52 -0700115 rtclog_config->codecs.emplace_back(d.payload_name, d.payload_type,
nisse26e3abb2017-08-25 04:44:25 -0700116 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700117 }
118 return rtclog_config;
119}
120
eladalon8ec568a2017-09-08 06:15:52 -0700121std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700122 const VideoSendStream::Config& config,
123 size_t ssrc_index) {
eladalon8ec568a2017-09-08 06:15:52 -0700124 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
125 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700126 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700127 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700128 }
eladalon8ec568a2017-09-08 06:15:52 -0700129 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
130 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700131
eladalon8ec568a2017-09-08 06:15:52 -0700132 rtclog_config->codecs.emplace_back(config.encoder_settings.payload_name,
133 config.encoder_settings.payload_type,
134 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700135 return rtclog_config;
136}
137
eladalon8ec568a2017-09-08 06:15:52 -0700138std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700139 const AudioReceiveStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700140 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
141 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
142 rtclog_config->local_ssrc = config.rtp.local_ssrc;
143 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700144 return rtclog_config;
145}
146
eladalon8ec568a2017-09-08 06:15:52 -0700147std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjf4726992017-05-22 10:12:26 -0700148 const AudioSendStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700149 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
150 rtclog_config->local_ssrc = config.rtp.ssrc;
151 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjf4726992017-05-22 10:12:26 -0700152 if (config.send_codec_spec) {
eladalon8ec568a2017-09-08 06:15:52 -0700153 rtclog_config->codecs.emplace_back(config.send_codec_spec->format.name,
154 config.send_codec_spec->payload_type, 0);
perkjf4726992017-05-22 10:12:26 -0700155 }
156 return rtclog_config;
157}
158
nisse4709e892017-02-07 01:18:43 -0800159} // namespace
160
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000161namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000162
perkjec81bcd2016-05-11 06:01:13 -0700163class Call : public webrtc::Call,
164 public PacketReceiver,
brandtr4e523862016-10-18 23:50:45 -0700165 public RecoveredPacketReceiver,
nisse559af382017-03-21 06:41:12 -0700166 public SendSideCongestionController::Observer,
perkj71ee44c2016-06-15 00:47:53 -0700167 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000168 public:
nisseb8f9a322017-03-27 05:36:15 -0700169 Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700170 std::unique_ptr<RtpTransportControllerSendInterface> transport_send);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000171 virtual ~Call();
172
brandtr25445d32016-10-23 23:37:14 -0700173 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000174 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000175
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200176 webrtc::AudioSendStream* CreateAudioSendStream(
177 const webrtc::AudioSendStream::Config& config) override;
178 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
179
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200180 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
181 const webrtc::AudioReceiveStream::Config& config) override;
182 void DestroyAudioReceiveStream(
183 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000184
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200185 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700186 webrtc::VideoSendStream::Config config,
187 VideoEncoderConfig encoder_config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000188 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000189
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200190 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200191 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 void DestroyVideoReceiveStream(
193 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000194
brandtr7250b392016-12-19 01:13:46 -0800195 FlexfecReceiveStream* CreateFlexfecReceiveStream(
196 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700197 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800198 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700199
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000200 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000201
brandtr25445d32016-10-23 23:37:14 -0700202 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700203 DeliveryStatus DeliverPacket(MediaType media_type,
204 const uint8_t* packet,
205 size_t length,
206 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000207
brandtr4e523862016-10-18 23:50:45 -0700208 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700209 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700210
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000211 void SetBitrateConfig(
212 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
skvlad7a43d252016-03-22 15:32:27 -0700213
zstein4b979802017-06-02 14:37:37 -0700214 void SetBitrateConfigMask(
215 const webrtc::Call::Config::BitrateConfigMask& bitrate_config) override;
216
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
michaelt79e05882016-11-08 02:50:09 -0800223 void OnTransportOverheadChanged(MediaType media,
224 int transport_overhead_per_packet) override;
225
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700226 void OnNetworkRouteChanged(const std::string& transport_name,
227 const rtc::NetworkRoute& network_route) override;
228
stefanc1aeaf02015-10-15 07:26:07 -0700229 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
230
mflodman0e7e2592015-11-12 21:02:42 -0800231 // Implements BitrateObserver.
minyue78b4d562016-11-30 04:47:39 -0800232 void OnNetworkChanged(uint32_t bitrate_bps,
233 uint8_t fraction_loss,
234 int64_t rtt_ms,
235 int64_t probing_interval_ms) override;
mflodman0e7e2592015-11-12 21:02:42 -0800236
perkj71ee44c2016-06-15 00:47:53 -0700237 // Implements BitrateAllocator::LimitObserver.
238 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
239 uint32_t max_padding_bitrate_bps) override;
240
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000241 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200242 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
243 size_t length);
stefan68786d22015-09-08 05:36:15 -0700244 DeliveryStatus DeliverRtp(MediaType media_type,
245 const uint8_t* packet,
246 size_t length,
247 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
zstein4b979802017-06-02 14:37:37 -0700261 // Applies update to the BitrateConfig cached in |config_|, restarting
262 // bandwidth estimation from |new_start| if set.
263 void UpdateCurrentBitrateConfig(const rtc::Optional<int>& new_start);
264
Peter Boströmd3c94472015-12-09 11:20:58 +0100265 Clock* const clock_;
stefan91d92602015-11-11 10:13:02 -0800266
Peter Boström45553ae2015-05-08 13:54:38 +0200267 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800268 const std::unique_ptr<ProcessThread> module_process_thread_;
nisseb9359842017-01-19 05:41:25 -0800269 const std::unique_ptr<ProcessThread> pacer_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800270 const std::unique_ptr<CallStats> call_stats_;
271 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000272 Call::Config config_;
eladalonf3f5c0e2017-08-18 02:47:08 -0700273 rtc::SequencedTaskChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000274
skvlad7a43d252016-03-22 15:32:27 -0700275 NetworkState audio_network_state_;
276 NetworkState video_network_state_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000277
kwibergb25345e2016-03-12 06:10:44 -0800278 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700279 // Audio, Video, and FlexFEC receive streams are owned by the client that
280 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700281 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700282 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200283 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700284 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700285
pbos8fc7fa72015-07-15 08:02:58 -0700286 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700287 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000288
nisse0f15f922017-06-21 01:05:22 -0700289 // TODO(nisse): Should eventually be injected at creation,
290 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700291 RtpStreamReceiverController audio_receiver_controller_;
292 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700293
nissed44ce052017-02-06 02:23:00 -0800294 // This extra map is used for receive processing which is
295 // independent of media type.
296
297 // TODO(nisse): In the RTP transport refactoring, we should have a
298 // single mapping from ssrc to a more abstract receive stream, with
299 // accessor methods for all configuration we need at this level.
300 struct ReceiveRtpConfig {
301 ReceiveRtpConfig() = default; // Needed by std::map
302 ReceiveRtpConfig(const std::vector<RtpExtension>& extensions,
nisse4709e892017-02-07 01:18:43 -0800303 bool use_send_side_bwe)
304 : extensions(extensions), use_send_side_bwe(use_send_side_bwe) {}
nissed44ce052017-02-06 02:23:00 -0800305
306 // Registered RTP header extensions for each stream. Note that RTP header
307 // extensions are negotiated per track ("m= line") in the SDP, but we have
308 // no notion of tracks at the Call level. We therefore store the RTP header
309 // extensions per SSRC instead, which leads to some storage overhead.
310 RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800311 // Set if both RTP extension the RTCP feedback message needed for
312 // send side BWE are negotiated.
313 bool use_send_side_bwe = false;
nissed44ce052017-02-06 02:23:00 -0800314 };
315 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700316 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800317
kwibergb25345e2016-03-12 06:10:44 -0800318 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700319 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700320 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
321 RTC_GUARDED_BY(send_crit_);
322 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
323 RTC_GUARDED_BY(send_crit_);
324 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000325
ossuc3d4b482017-05-23 06:07:11 -0700326 using RtpStateMap = std::map<uint32_t, RtpState>;
327 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700328 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700329 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700330 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700331
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200332 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
333 RtpPayloadStateMap suspended_video_payload_states_
334 RTC_GUARDED_BY(configuration_sequence_checker_);
335
skvlad11a9cbf2016-10-07 11:53:05 -0700336 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700337
stefan18adf0a2015-11-17 06:24:56 -0800338 // The following members are only accessed (exclusively) from one thread and
339 // from the destructor, and therefore doesn't need any explicit
340 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700341 RateCounter received_bytes_per_second_counter_;
342 RateCounter received_audio_bytes_per_second_counter_;
343 RateCounter received_video_bytes_per_second_counter_;
344 RateCounter received_rtcp_bytes_per_second_counter_;
saza0d7f04d2017-07-04 04:05:06 -0700345 rtc::Optional<int64_t> first_received_rtp_audio_ms_;
346 rtc::Optional<int64_t> last_received_rtp_audio_ms_;
347 rtc::Optional<int64_t> first_received_rtp_video_ms_;
348 rtc::Optional<int64_t> last_received_rtp_video_ms_;
sazac58f8c02017-07-19 00:39:19 -0700349 TimeInterval sent_rtp_audio_timer_ms_;
stefan91d92602015-11-11 10:13:02 -0800350
stefan18adf0a2015-11-17 06:24:56 -0800351 // TODO(holmer): Remove this lock once BitrateController no longer calls
352 // OnNetworkChanged from multiple threads.
353 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700354 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
355 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
356 AvgCounter estimated_send_bitrate_kbps_counter_
357 RTC_GUARDED_BY(&bitrate_crit_);
358 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800359
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700360 std::map<std::string, rtc::NetworkRoute> network_routes_;
361
nisse6167b262017-04-06 06:34:25 -0700362 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
nisse559af382017-03-21 06:41:12 -0700363 ReceiveSideCongestionController receive_side_cc_;
asapersson35151f32016-05-02 23:44:01 -0700364 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700365 const int64_t start_ms_;
perkj26091b12016-09-01 01:17:40 -0700366 // TODO(perkj): |worker_queue_| is supposed to replace
367 // |module_process_thread_|.
368 // |worker_queue| is defined last to ensure all pending tasks are cancelled
369 // and deleted before any other members.
370 rtc::TaskQueue worker_queue_;
mflodman0e7e2592015-11-12 21:02:42 -0800371
zstein4b979802017-06-02 14:37:37 -0700372 // The config mask set by SetBitrateConfigMask.
373 // 0 <= min <= start <= max
374 Config::BitrateConfigMask bitrate_config_mask_;
375
376 // The config set by SetBitrateConfig.
377 // min >= 0, start != 0, max == -1 || max > 0
378 Config::BitrateConfig base_bitrate_config_;
379
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 {
385 std::stringstream ss;
386 ss << "Call stats: " << time_ms << ", {";
387 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
388 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
389 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
390 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
391 ss << "rtt_ms: " << rtt_ms;
392 ss << '}';
393 return ss.str();
394}
395
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000396Call* Call::Create(const Call::Config& config) {
zstein7cb69d52017-05-08 11:52:38 -0700397 return new internal::Call(config,
398 rtc::MakeUnique<RtpTransportControllerSend>(
399 Clock::GetRealTimeClock(), config.event_log));
400}
401
402Call* Call::Create(
403 const Call::Config& config,
404 std::unique_ptr<RtpTransportControllerSendInterface> transport_send) {
405 return new internal::Call(config, std::move(transport_send));
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000406}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000407
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000408namespace internal {
409
nisseb8f9a322017-03-27 05:36:15 -0700410Call::Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700411 std::unique_ptr<RtpTransportControllerSendInterface> transport_send)
stefan91d92602015-11-11 10:13:02 -0800412 : clock_(Clock::GetRealTimeClock()),
413 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
kwiberg1c7fdd82016-04-26 08:18:04 -0700414 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
nisseb9359842017-01-19 05:41:25 -0800415 pacer_thread_(ProcessThread::Create("PacerThread")),
Peter Boströmd3c94472015-12-09 11:20:58 +0100416 call_stats_(new CallStats(clock_)),
perkj71ee44c2016-06-15 00:47:53 -0700417 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200418 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800419 audio_network_state_(kNetworkDown),
420 video_network_state_(kNetworkDown),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000421 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800422 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700423 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700424 received_bytes_per_second_counter_(clock_, nullptr, true),
425 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
426 received_video_bytes_per_second_counter_(clock_, nullptr, true),
427 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
perkj71ee44c2016-06-15 00:47:53 -0700428 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700429 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700430 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
431 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700432 receive_side_cc_(clock_, transport_send->packet_router()),
asapersson4374a092016-07-27 00:39:09 -0700433 video_send_delay_stats_(new SendDelayStats(clock_)),
perkj26091b12016-09-01 01:17:40 -0700434 start_ms_(clock_->TimeInMilliseconds()),
zstein4b979802017-06-02 14:37:37 -0700435 worker_queue_("call_worker_queue"),
436 base_bitrate_config_(config.bitrate_config) {
skvlad11a9cbf2016-10-07 11:53:05 -0700437 RTC_DCHECK(config.event_log != nullptr);
henrikg91d6ede2015-09-17 00:24:34 -0700438 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
stefanfca900a2017-04-10 03:53:00 -0700439 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
henrikg91d6ede2015-09-17 00:24:34 -0700440 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100441 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700442 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
443 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000444 }
zstein7cb69d52017-05-08 11:52:38 -0700445 transport_send->send_side_cc()->RegisterNetworkObserver(this);
nisse6167b262017-04-06 06:34:25 -0700446 transport_send_ = std::move(transport_send);
nisseb8f9a322017-03-27 05:36:15 -0700447 transport_send_->send_side_cc()->SignalNetworkState(kNetworkDown);
448 transport_send_->send_side_cc()->SetBweBitrates(
449 config_.bitrate_config.min_bitrate_bps,
450 config_.bitrate_config.start_bitrate_bps,
451 config_.bitrate_config.max_bitrate_bps);
nissebcbaf742017-03-28 01:16:25 -0700452 call_stats_->RegisterStatsObserver(&receive_side_cc_);
nisseb8f9a322017-03-27 05:36:15 -0700453 call_stats_->RegisterStatsObserver(transport_send_->send_side_cc());
Stefan Holmerc379fcb2016-02-24 16:02:55 +0100454
stefan9e117c5e12017-08-16 08:16:25 -0700455 // We have to attach the pacer to the pacer thread before starting the
456 // module process thread to avoid a race accessing the process thread
457 // both from the process thread and the pacer thread.
Stefan Holmer5c8942a2017-08-22 16:16:44 +0200458 pacer_thread_->RegisterModule(transport_send_->pacer(), RTC_FROM_HERE);
stefan64136af2017-08-14 08:03:17 -0700459 pacer_thread_->RegisterModule(
460 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
stefan64136af2017-08-14 08:03:17 -0700461 pacer_thread_->Start();
stefan9e117c5e12017-08-16 08:16:25 -0700462
463 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
464 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
465 module_process_thread_->RegisterModule(transport_send_->send_side_cc(),
466 RTC_FROM_HERE);
467 module_process_thread_->Start();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000468}
469
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000470Call::~Call() {
eladalonf3f5c0e2017-08-18 02:47:08 -0700471 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700472
solenbergc7a8b082015-10-16 14:35:07 -0700473 RTC_CHECK(audio_send_ssrcs_.empty());
474 RTC_CHECK(video_send_ssrcs_.empty());
475 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700476 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700477 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000478
stefan9e117c5e12017-08-16 08:16:25 -0700479 // The send-side congestion controller must be de-registered prior to
480 // the pacer thread being stopped to avoid a race when accessing the
481 // pacer thread object on the module process thread at the same time as
482 // the pacer thread is stopped.
483 module_process_thread_->DeRegisterModule(transport_send_->send_side_cc());
nisseb9359842017-01-19 05:41:25 -0800484 pacer_thread_->Stop();
Stefan Holmer5c8942a2017-08-22 16:16:44 +0200485 pacer_thread_->DeRegisterModule(transport_send_->pacer());
nisseb9359842017-01-19 05:41:25 -0800486 pacer_thread_->DeRegisterModule(
nisse559af382017-03-21 06:41:12 -0700487 receive_side_cc_.GetRemoteBitrateEstimator(true));
nisse559af382017-03-21 06:41:12 -0700488 module_process_thread_->DeRegisterModule(&receive_side_cc_);
mflodmane3787022015-10-21 13:24:28 +0200489 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200490 module_process_thread_->Stop();
nissebcbaf742017-03-28 01:16:25 -0700491 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
nisseb8f9a322017-03-27 05:36:15 -0700492 call_stats_->DeregisterStatsObserver(transport_send_->send_side_cc());
sprang6d6122b2016-07-13 06:37:09 -0700493
asaperssonfc5e81c2017-04-19 23:28:53 -0700494 int64_t first_sent_packet_ms =
495 transport_send_->send_side_cc()->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700496 // Only update histograms after process threads have been shut down, so that
497 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700498 {
499 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700500 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700501 }
sprang6d6122b2016-07-13 06:37:09 -0700502 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700503 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000504}
505
asapersson4374a092016-07-27 00:39:09 -0700506void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700507 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700508 "WebRTC.Call.LifetimeInSeconds",
509 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
510}
511
asaperssonfc5e81c2017-04-19 23:28:53 -0700512void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
513 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800514 return;
sazac58f8c02017-07-19 00:39:19 -0700515 if (!sent_rtp_audio_timer_ms_.Empty()) {
516 RTC_HISTOGRAM_COUNTS_100000(
517 "WebRTC.Call.TimeSendingAudioRtpPacketsInSeconds",
518 sent_rtp_audio_timer_ms_.Length() / 1000);
519 }
stefan18adf0a2015-11-17 06:24:56 -0800520 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700521 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800522 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
523 return;
asaperssonce2e1362016-09-09 00:13:35 -0700524 const int kMinRequiredPeriodicSamples = 5;
525 AggregatedStats send_bitrate_stats =
526 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
527 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700528 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
529 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100530 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
531 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800532 }
asaperssonce2e1362016-09-09 00:13:35 -0700533 AggregatedStats pacer_bitrate_stats =
534 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
535 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700536 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
537 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100538 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
539 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800540 }
541}
542
543void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700544 if (first_received_rtp_audio_ms_) {
545 RTC_HISTOGRAM_COUNTS_100000(
546 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
547 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
548 }
549 if (first_received_rtp_video_ms_) {
550 RTC_HISTOGRAM_COUNTS_100000(
551 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
552 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
553 }
asapersson250fd972016-09-08 00:07:21 -0700554 const int kMinRequiredPeriodicSamples = 5;
555 AggregatedStats video_bytes_per_sec =
556 received_video_bytes_per_second_counter_.GetStats();
557 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700558 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
559 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100560 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
561 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800562 }
asapersson250fd972016-09-08 00:07:21 -0700563 AggregatedStats audio_bytes_per_sec =
564 received_audio_bytes_per_second_counter_.GetStats();
565 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700566 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
567 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100568 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
569 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800570 }
asapersson250fd972016-09-08 00:07:21 -0700571 AggregatedStats rtcp_bytes_per_sec =
572 received_rtcp_bytes_per_second_counter_.GetStats();
573 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700574 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
575 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100576 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
577 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800578 }
asapersson250fd972016-09-08 00:07:21 -0700579 AggregatedStats recv_bytes_per_sec =
580 received_bytes_per_second_counter_.GetStats();
581 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700582 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
583 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100584 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
585 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700586 }
stefan91d92602015-11-11 10:13:02 -0800587}
588
solenberg5a289392015-10-19 03:39:20 -0700589PacketReceiver* Call::Receiver() {
eladalond1dd2f72017-08-25 02:55:57 -0700590 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700591 return this;
592}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000593
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200594webrtc::AudioSendStream* Call::CreateAudioSendStream(
595 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700596 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700597 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200598 event_log_->Log(rtc::MakeUnique<RtcEventAudioSendStreamConfig>(
599 CreateRtcLogStreamConfig(config)));
ossuc3d4b482017-05-23 06:07:11 -0700600
601 rtc::Optional<RtpState> suspended_rtp_state;
602 {
603 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
604 if (iter != suspended_audio_send_ssrcs_.end()) {
605 suspended_rtp_state.emplace(iter->second);
606 }
607 }
608
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100609 AudioSendStream* send_stream = new AudioSendStream(
nisseb8f9a322017-03-27 05:36:15 -0700610 config, config_.audio_state, &worker_queue_, transport_send_.get(),
ossuc3d4b482017-05-23 06:07:11 -0700611 bitrate_allocator_.get(), event_log_, call_stats_->rtcp_rtt_stats(),
612 suspended_rtp_state);
solenbergc7a8b082015-10-16 14:35:07 -0700613 {
solenbergc7a8b082015-10-16 14:35:07 -0700614 WriteLockScoped write_lock(*send_crit_);
615 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
616 audio_send_ssrcs_.end());
617 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700618 }
solenberg7602aab2016-11-14 11:30:07 -0800619 {
620 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700621 for (AudioReceiveStream* stream : audio_receive_streams_) {
622 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
623 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800624 }
625 }
626 }
skvlad7a43d252016-03-22 15:32:27 -0700627 send_stream->SignalNetworkState(audio_network_state_);
628 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700629 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200630}
631
632void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700633 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700634 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700635 RTC_DCHECK(send_stream != nullptr);
636
637 send_stream->Stop();
638
eladalonabbc4302017-07-26 02:09:44 -0700639 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700640 webrtc::internal::AudioSendStream* audio_send_stream =
641 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700642 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700643 {
644 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800645 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
646 RTC_DCHECK_EQ(1, num_deleted);
647 }
648 {
649 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700650 for (AudioReceiveStream* stream : audio_receive_streams_) {
651 if (stream->config().rtp.local_ssrc == ssrc) {
652 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800653 }
654 }
solenbergc7a8b082015-10-16 14:35:07 -0700655 }
skvlad7a43d252016-03-22 15:32:27 -0700656 UpdateAggregateNetworkState();
sazac58f8c02017-07-19 00:39:19 -0700657 sent_rtp_audio_timer_ms_.Extend(audio_send_stream->GetActiveLifetime());
eladalonabbc4302017-07-26 02:09:44 -0700658 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200659}
660
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200661webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
662 const webrtc::AudioReceiveStream::Config& config) {
663 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700664 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200665 event_log_->Log(rtc::MakeUnique<RtcEventAudioReceiveStreamConfig>(
666 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700667 AudioReceiveStream* receive_stream = new AudioReceiveStream(
eladalon2a2b2972017-07-03 09:25:27 -0700668 &audio_receiver_controller_, transport_send_->packet_router(), config,
nisse0f15f922017-06-21 01:05:22 -0700669 config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200670 {
671 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800672 receive_rtp_config_[config.rtp.remote_ssrc] =
nisse4709e892017-02-07 01:18:43 -0800673 ReceiveRtpConfig(config.rtp.extensions, UseSendSideBwe(config));
nissee4bcd6d2017-05-16 04:47:04 -0700674 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800675
pbos8fc7fa72015-07-15 08:02:58 -0700676 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200677 }
solenberg7602aab2016-11-14 11:30:07 -0800678 {
679 ReadLockScoped read_lock(*send_crit_);
680 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
681 if (it != audio_send_ssrcs_.end()) {
682 receive_stream->AssociateSendStream(it->second);
683 }
684 }
skvlad7a43d252016-03-22 15:32:27 -0700685 receive_stream->SignalNetworkState(audio_network_state_);
686 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200687 return receive_stream;
688}
689
690void Call::DestroyAudioReceiveStream(
691 webrtc::AudioReceiveStream* receive_stream) {
692 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700693 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700694 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700695 webrtc::internal::AudioReceiveStream* audio_receive_stream =
696 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200697 {
698 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800699 const AudioReceiveStream::Config& config = audio_receive_stream->config();
700 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700701 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800702 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700703 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700704 const std::string& sync_group = audio_receive_stream->config().sync_group;
705 const auto it = sync_stream_mapping_.find(sync_group);
706 if (it != sync_stream_mapping_.end() &&
707 it->second == audio_receive_stream) {
708 sync_stream_mapping_.erase(it);
709 ConfigureSync(sync_group);
710 }
nissed44ce052017-02-06 02:23:00 -0800711 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200712 }
skvlad7a43d252016-03-22 15:32:27 -0700713 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200714 delete audio_receive_stream;
715}
716
717webrtc::VideoSendStream* Call::CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700718 webrtc::VideoSendStream::Config config,
719 VideoEncoderConfig encoder_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000720 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700721 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000722
asapersson35151f32016-05-02 23:44:01 -0700723 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700724 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
725 ++ssrc_index) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200726 event_log_->Log(rtc::MakeUnique<RtcEventVideoSendStreamConfig>(
727 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700728 }
perkj26091b12016-09-01 01:17:40 -0700729
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000730 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
731 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700732 // Copy ssrcs from |config| since |config| is moved.
733 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
mflodman0c478b32015-10-21 15:52:16 +0200734 VideoSendStream* send_stream = new VideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700735 num_cpu_cores_, module_process_thread_.get(), &worker_queue_,
nisseb8f9a322017-03-27 05:36:15 -0700736 call_stats_.get(), transport_send_.get(), bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700737 video_send_delay_stats_.get(), event_log_, std::move(config),
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200738 std::move(encoder_config), suspended_video_send_ssrcs_,
739 suspended_video_payload_states_);
perkj26091b12016-09-01 01:17:40 -0700740
skvlad7a43d252016-03-22 15:32:27 -0700741 {
742 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700743 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700744 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
745 video_send_ssrcs_[ssrc] = send_stream;
746 }
747 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000748 }
skvlad7a43d252016-03-22 15:32:27 -0700749 send_stream->SignalNetworkState(video_network_state_);
750 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700751
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000752 return send_stream;
753}
754
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000755void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000756 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700757 RTC_DCHECK(send_stream != nullptr);
eladalonf3f5c0e2017-08-18 02:47:08 -0700758 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000759
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000760 send_stream->Stop();
761
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000762 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000763 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000764 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200765 auto it = video_send_ssrcs_.begin();
766 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000767 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
768 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200769 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000770 } else {
771 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000772 }
773 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200774 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000775 }
henrikg91d6ede2015-09-17 00:24:34 -0700776 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000777
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200778 VideoSendStream::RtpStateMap rtp_states;
779 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
780 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
781 &rtp_payload_states);
782 for (const auto& kv : rtp_states) {
783 suspended_video_send_ssrcs_[kv.first] = kv.second;
784 }
785 for (const auto& kv : rtp_payload_states) {
786 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000787 }
788
skvlad7a43d252016-03-22 15:32:27 -0700789 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000790 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000791}
792
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200793webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200794 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000795 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700796 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800797
nisse0f15f922017-06-21 01:05:22 -0700798 VideoReceiveStream* receive_stream = new VideoReceiveStream(
eladalon2a2b2972017-07-03 09:25:27 -0700799 &video_receiver_controller_, num_cpu_cores_,
nisse0f15f922017-06-21 01:05:22 -0700800 transport_send_->packet_router(), std::move(configuration),
801 module_process_thread_.get(), call_stats_.get());
Tommi733b5472016-06-10 17:58:01 +0200802
803 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
nissed44ce052017-02-06 02:23:00 -0800804 ReceiveRtpConfig receive_config(config.rtp.extensions,
nisse4709e892017-02-07 01:18:43 -0800805 UseSendSideBwe(config));
skvlad7a43d252016-03-22 15:32:27 -0700806 {
807 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800808 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800809 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700810 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800811 // type, we may get an incorrect value for the rtx stream, but
812 // that is unlikely to matter in practice.
813 receive_rtp_config_[config.rtp.rtx_ssrc] = receive_config;
814 }
815 receive_rtp_config_[config.rtp.remote_ssrc] = receive_config;
skvlad7a43d252016-03-22 15:32:27 -0700816 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700817 ConfigureSync(config.sync_group);
818 }
819 receive_stream->SignalNetworkState(video_network_state_);
820 UpdateAggregateNetworkState();
Elad Alon4a87e1c2017-10-03 16:11:34 +0200821 event_log_->Log(rtc::MakeUnique<RtcEventVideoReceiveStreamConfig>(
822 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000823 return receive_stream;
824}
825
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000826void Call::DestroyVideoReceiveStream(
827 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000828 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700829 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700830 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700831 VideoReceiveStream* receive_stream_impl =
832 static_cast<VideoReceiveStream*>(receive_stream);
833 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000834 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000835 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000836 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
837 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700838 receive_rtp_config_.erase(config.rtp.remote_ssrc);
839 if (config.rtp.rtx_ssrc) {
840 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000841 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200842 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700843 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000844 }
nisse4709e892017-02-07 01:18:43 -0800845
nisse559af382017-03-21 06:41:12 -0700846 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800847 ->RemoveStream(config.rtp.remote_ssrc);
848
skvlad7a43d252016-03-22 15:32:27 -0700849 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000850 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000851}
852
brandtr7250b392016-12-19 01:13:46 -0800853FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
854 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700855 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700856 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800857
858 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700859
nisse0f15f922017-06-21 01:05:22 -0700860 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700861 {
862 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -0700863 // Unlike the video and audio receive streams,
864 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
865 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -0700866 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -0700867 // constructor while holding |receive_crit_| ensures that we don't
868 // call OnRtpPacket until the constructor is finished and the
869 // object is in a valid state.
870 // TODO(nisse): Fix constructor so that it can be moved outside of
871 // this locked scope.
872 receive_stream = new FlexfecReceiveStreamImpl(
eladalon2a2b2972017-07-03 09:25:27 -0700873 &video_receiver_controller_, config, recovered_packet_receiver,
nisse0f15f922017-06-21 01:05:22 -0700874 call_stats_->rtcp_rtt_stats(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -0800875
nissed44ce052017-02-06 02:23:00 -0800876 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
877 receive_rtp_config_.end());
878 receive_rtp_config_[config.remote_ssrc] =
nisse4709e892017-02-07 01:18:43 -0800879 ReceiveRtpConfig(config.rtp_header_extensions, UseSendSideBwe(config));
brandtr25445d32016-10-23 23:37:14 -0700880 }
brandtrb29e6522016-12-21 06:37:18 -0800881
brandtr25445d32016-10-23 23:37:14 -0700882 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -0800883
brandtr25445d32016-10-23 23:37:14 -0700884 return receive_stream;
885}
886
brandtr7250b392016-12-19 01:13:46 -0800887void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -0700888 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700889 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800890
brandtr25445d32016-10-23 23:37:14 -0700891 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -0700892 {
893 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800894
eladalon42f44f92017-07-25 06:40:06 -0700895 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -0800896 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -0800897 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -0800898
brandtr7250b392016-12-19 01:13:46 -0800899 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
900 // destroyed.
nisse559af382017-03-21 06:41:12 -0700901 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800902 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -0700903 }
brandtrb29e6522016-12-21 06:37:18 -0800904
eladalon42f44f92017-07-25 06:40:06 -0700905 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700906}
907
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000908Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700909 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
910 // thread. Re-enable once that is fixed.
eladalonf3f5c0e2017-08-18 02:47:08 -0700911 // RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000912 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200913 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000914 uint32_t send_bandwidth = 0;
nisseb8f9a322017-03-27 05:36:15 -0700915 transport_send_->send_side_cc()->GetBitrateController()->AvailableBandwidth(
916 &send_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200917 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000918 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -0700919 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700920 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200921 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000922 stats.recv_bandwidth_bps = recv_bandwidth;
nisseb8f9a322017-03-27 05:36:15 -0700923 stats.pacer_delay_ms =
924 transport_send_->send_side_cc()->GetPacerQueuingDelayMs();
sprange2d83d62016-02-19 09:03:26 -0800925 stats.rtt_ms = call_stats_->rtcp_rtt_stats()->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -0700926 {
927 rtc::CritScope cs(&bitrate_crit_);
928 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
929 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000930 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000931}
932
pbos@webrtc.org00873182014-11-25 14:03:34 +0000933void Call::SetBitrateConfig(
934 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000935 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
eladalonf3f5c0e2017-08-18 02:47:08 -0700936 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700937 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
zstein4b979802017-06-02 14:37:37 -0700938 RTC_DCHECK_NE(bitrate_config.start_bitrate_bps, 0);
939 if (bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700940 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
zstein4b979802017-06-02 14:37:37 -0700941 }
942
943 rtc::Optional<int> new_start;
944 // Only update the "start" bitrate if it's set, and different from the old
945 // value. In practice, this value comes from the x-google-start-bitrate codec
946 // parameter in SDP, and setting the same remote description twice shouldn't
947 // restart bandwidth estimation.
948 if (bitrate_config.start_bitrate_bps != -1 &&
949 bitrate_config.start_bitrate_bps !=
950 base_bitrate_config_.start_bitrate_bps) {
951 new_start.emplace(bitrate_config.start_bitrate_bps);
952 }
953 base_bitrate_config_ = bitrate_config;
954 UpdateCurrentBitrateConfig(new_start);
955}
956
957void Call::SetBitrateConfigMask(
958 const webrtc::Call::Config::BitrateConfigMask& mask) {
959 TRACE_EVENT0("webrtc", "Call::SetBitrateConfigMask");
eladalonf3f5c0e2017-08-18 02:47:08 -0700960 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
zstein4b979802017-06-02 14:37:37 -0700961
962 bitrate_config_mask_ = mask;
963 UpdateCurrentBitrateConfig(mask.start_bitrate_bps);
964}
965
zstein4b979802017-06-02 14:37:37 -0700966void Call::UpdateCurrentBitrateConfig(const rtc::Optional<int>& new_start) {
967 Config::BitrateConfig updated;
968 updated.min_bitrate_bps =
969 std::max(bitrate_config_mask_.min_bitrate_bps.value_or(0),
970 base_bitrate_config_.min_bitrate_bps);
971
972 updated.max_bitrate_bps =
973 MinPositive(bitrate_config_mask_.max_bitrate_bps.value_or(-1),
974 base_bitrate_config_.max_bitrate_bps);
975
976 // If the combined min ends up greater than the combined max, the max takes
977 // priority.
978 if (updated.max_bitrate_bps != -1 &&
979 updated.min_bitrate_bps > updated.max_bitrate_bps) {
980 updated.min_bitrate_bps = updated.max_bitrate_bps;
981 }
982
983 // If there is nothing to update (min/max unchanged, no new bandwidth
984 // estimation start value), return early.
985 if (updated.min_bitrate_bps == config_.bitrate_config.min_bitrate_bps &&
986 updated.max_bitrate_bps == config_.bitrate_config.max_bitrate_bps &&
987 !new_start) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100988 RTC_LOG(LS_VERBOSE) << "WebRTC.Call.UpdateCurrentBitrateConfig: "
989 << "nothing to update";
pbos@webrtc.org00873182014-11-25 14:03:34 +0000990 return;
991 }
zstein4b979802017-06-02 14:37:37 -0700992
993 if (new_start) {
994 // Clamp start by min and max.
995 updated.start_bitrate_bps = MinPositive(
996 std::max(*new_start, updated.min_bitrate_bps), updated.max_bitrate_bps);
997 } else {
998 updated.start_bitrate_bps = -1;
999 }
1000
Mirko Bonadei675513b2017-11-09 11:09:25 +01001001 RTC_LOG(INFO) << "WebRTC.Call.UpdateCurrentBitrateConfig: "
1002 << "calling SetBweBitrates with args ("
1003 << updated.min_bitrate_bps << ", " << updated.start_bitrate_bps
1004 << ", " << updated.max_bitrate_bps << ")";
zstein4b979802017-06-02 14:37:37 -07001005 transport_send_->send_side_cc()->SetBweBitrates(updated.min_bitrate_bps,
1006 updated.start_bitrate_bps,
1007 updated.max_bitrate_bps);
1008 if (!new_start) {
1009 updated.start_bitrate_bps = config_.bitrate_config.start_bitrate_bps;
1010 }
1011 config_.bitrate_config = updated;
pbos@webrtc.org00873182014-11-25 14:03:34 +00001012}
1013
Alex Narest78609d52017-10-20 10:37:47 +02001014void Call::SetBitrateAllocationStrategy(
1015 std::unique_ptr<rtc::BitrateAllocationStrategy>
1016 bitrate_allocation_strategy) {
1017 if (!worker_queue_.IsCurrent()) {
1018 rtc::BitrateAllocationStrategy* strategy_raw =
1019 bitrate_allocation_strategy.release();
1020 auto functor = [this, strategy_raw]() {
1021 SetBitrateAllocationStrategy(
1022 rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw));
1023 };
1024 worker_queue_.PostTask([functor] { functor(); });
1025 return;
1026 }
1027 RTC_DCHECK_RUN_ON(&worker_queue_);
1028 bitrate_allocator_->SetBitrateAllocationStrategy(
1029 std::move(bitrate_allocation_strategy));
1030}
1031
skvlad7a43d252016-03-22 15:32:27 -07001032void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
eladalonf3f5c0e2017-08-18 02:47:08 -07001033 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001034 switch (media) {
1035 case MediaType::AUDIO:
1036 audio_network_state_ = state;
1037 break;
1038 case MediaType::VIDEO:
1039 video_network_state_ = state;
1040 break;
1041 case MediaType::ANY:
1042 case MediaType::DATA:
1043 RTC_NOTREACHED();
1044 break;
1045 }
1046
1047 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001048 {
skvlad7a43d252016-03-22 15:32:27 -07001049 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -07001050 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001051 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -07001052 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001053 for (auto& kv : video_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001054 kv.second->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001055 }
1056 }
1057 {
skvlad7a43d252016-03-22 15:32:27 -07001058 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001059 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1060 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001061 }
nissee4bcd6d2017-05-16 04:47:04 -07001062 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1063 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001064 }
1065 }
1066}
1067
michaelt79e05882016-11-08 02:50:09 -08001068void Call::OnTransportOverheadChanged(MediaType media,
1069 int transport_overhead_per_packet) {
1070 switch (media) {
1071 case MediaType::AUDIO: {
1072 ReadLockScoped read_lock(*send_crit_);
1073 for (auto& kv : audio_send_ssrcs_) {
1074 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1075 }
1076 break;
1077 }
1078 case MediaType::VIDEO: {
1079 ReadLockScoped read_lock(*send_crit_);
1080 for (auto& kv : video_send_ssrcs_) {
1081 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1082 }
1083 break;
1084 }
1085 case MediaType::ANY:
1086 case MediaType::DATA:
1087 RTC_NOTREACHED();
1088 break;
1089 }
1090}
1091
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001092// TODO(honghaiz): Add tests for this method.
1093void Call::OnNetworkRouteChanged(const std::string& transport_name,
1094 const rtc::NetworkRoute& network_route) {
eladalonf3f5c0e2017-08-18 02:47:08 -07001095 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001096 // Check if the network route is connected.
1097 if (!network_route.connected) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001098 RTC_LOG(LS_INFO) << "Transport " << transport_name << " is disconnected";
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001099 // TODO(honghaiz): Perhaps handle this in SignalChannelNetworkState and
1100 // consider merging these two methods.
1101 return;
1102 }
1103
1104 // Check whether the network route has changed on each transport.
1105 auto result =
1106 network_routes_.insert(std::make_pair(transport_name, network_route));
1107 auto kv = result.first;
1108 bool inserted = result.second;
1109 if (inserted) {
1110 // No need to reset BWE if this is the first time the network connects.
1111 return;
1112 }
1113 if (kv->second != network_route) {
1114 kv->second = network_route;
Mirko Bonadei675513b2017-11-09 11:09:25 +01001115 RTC_LOG(LS_INFO)
1116 << "Network route changed on transport " << transport_name
1117 << ": new local network id " << network_route.local_network_id
1118 << " new remote network id " << network_route.remote_network_id
1119 << " Reset bitrates to min: " << config_.bitrate_config.min_bitrate_bps
1120 << " bps, start: " << config_.bitrate_config.start_bitrate_bps
1121 << " bps, max: " << config_.bitrate_config.start_bitrate_bps
1122 << " bps.";
stefan5a2c5062017-01-27 06:43:18 -08001123 RTC_DCHECK_GT(config_.bitrate_config.start_bitrate_bps, 0);
nisseb8f9a322017-03-27 05:36:15 -07001124 transport_send_->send_side_cc()->OnNetworkRouteChanged(
Stefan Holmer9ea46b52017-03-15 12:40:25 +01001125 network_route, config_.bitrate_config.start_bitrate_bps,
honghaiz059e1832016-06-24 11:03:55 -07001126 config_.bitrate_config.min_bitrate_bps,
1127 config_.bitrate_config.max_bitrate_bps);
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001128 }
1129}
1130
skvlad7a43d252016-03-22 15:32:27 -07001131void Call::UpdateAggregateNetworkState() {
eladalonf3f5c0e2017-08-18 02:47:08 -07001132 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001133
1134 bool have_audio = false;
1135 bool have_video = false;
1136 {
1137 ReadLockScoped read_lock(*send_crit_);
1138 if (audio_send_ssrcs_.size() > 0)
1139 have_audio = true;
1140 if (video_send_ssrcs_.size() > 0)
1141 have_video = true;
1142 }
1143 {
1144 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001145 if (audio_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001146 have_audio = true;
nissee4bcd6d2017-05-16 04:47:04 -07001147 if (video_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001148 have_video = true;
1149 }
1150
1151 NetworkState aggregate_state = kNetworkDown;
1152 if ((have_video && video_network_state_ == kNetworkUp) ||
1153 (have_audio && audio_network_state_ == kNetworkUp)) {
1154 aggregate_state = kNetworkUp;
1155 }
1156
Mirko Bonadei675513b2017-11-09 11:09:25 +01001157 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
1158 << (aggregate_state == kNetworkUp ? "up" : "down");
skvlad7a43d252016-03-22 15:32:27 -07001159
nisseb8f9a322017-03-27 05:36:15 -07001160 transport_send_->send_side_cc()->SignalNetworkState(aggregate_state);
skvlad7a43d252016-03-22 15:32:27 -07001161}
1162
stefanc1aeaf02015-10-15 07:26:07 -07001163void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001164 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1165 clock_->TimeInMilliseconds());
nisseb8f9a322017-03-27 05:36:15 -07001166 transport_send_->send_side_cc()->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001167}
1168
minyue78b4d562016-11-30 04:47:39 -08001169void Call::OnNetworkChanged(uint32_t target_bitrate_bps,
1170 uint8_t fraction_loss,
1171 int64_t rtt_ms,
1172 int64_t probing_interval_ms) {
perkj26091b12016-09-01 01:17:40 -07001173 // TODO(perkj): Consider making sure CongestionController operates on
1174 // |worker_queue_|.
1175 if (!worker_queue_.IsCurrent()) {
minyue78b4d562016-11-30 04:47:39 -08001176 worker_queue_.PostTask(
1177 [this, target_bitrate_bps, fraction_loss, rtt_ms, probing_interval_ms] {
1178 OnNetworkChanged(target_bitrate_bps, fraction_loss, rtt_ms,
1179 probing_interval_ms);
1180 });
perkj26091b12016-09-01 01:17:40 -07001181 return;
1182 }
1183 RTC_DCHECK_RUN_ON(&worker_queue_);
nisse559af382017-03-21 06:41:12 -07001184 // For controlling the rate of feedback messages.
1185 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
perkj71ee44c2016-06-15 00:47:53 -07001186 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss,
minyue78b4d562016-11-30 04:47:39 -08001187 rtt_ms, probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001188
asaperssonce2e1362016-09-09 00:13:35 -07001189 // Ignore updates if bitrate is zero (the aggregate network state is down).
1190 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001191 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001192 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1193 pacer_bitrate_kbps_counter_.ProcessAndPause();
1194 return;
stefan18adf0a2015-11-17 06:24:56 -08001195 }
asaperssonce2e1362016-09-09 00:13:35 -07001196
1197 bool sending_video;
1198 {
1199 ReadLockScoped read_lock(*send_crit_);
1200 sending_video = !video_send_streams_.empty();
1201 }
1202
1203 rtc::CritScope lock(&bitrate_crit_);
1204 if (!sending_video) {
1205 // Do not update the stats if we are not sending video.
1206 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1207 pacer_bitrate_kbps_counter_.ProcessAndPause();
1208 return;
1209 }
1210 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1211 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1212 uint32_t pacer_bitrate_bps =
1213 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1214 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001215}
mflodman101f2502016-06-09 17:21:19 +02001216
perkj71ee44c2016-06-15 00:47:53 -07001217void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
1218 uint32_t max_padding_bitrate_bps) {
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001219 transport_send_->SetAllocatedSendBitrateLimits(min_send_bitrate_bps,
1220 max_padding_bitrate_bps);
perkj71ee44c2016-06-15 00:47:53 -07001221 rtc::CritScope lock(&bitrate_crit_);
1222 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001223 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001224}
1225
pbos8fc7fa72015-07-15 08:02:58 -07001226void Call::ConfigureSync(const std::string& sync_group) {
1227 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001228 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001229 return;
1230
1231 AudioReceiveStream* sync_audio_stream = nullptr;
1232 // Find existing audio stream.
1233 const auto it = sync_stream_mapping_.find(sync_group);
1234 if (it != sync_stream_mapping_.end()) {
1235 sync_audio_stream = it->second;
1236 } else {
1237 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001238 for (AudioReceiveStream* stream : audio_receive_streams_) {
1239 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001240 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001241 RTC_LOG(LS_WARNING)
1242 << "Attempting to sync more than one audio stream "
1243 "within the same sync group. This is not "
1244 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001245 break;
1246 }
nissee4bcd6d2017-05-16 04:47:04 -07001247 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001248 }
1249 }
1250 }
1251 if (sync_audio_stream)
1252 sync_stream_mapping_[sync_group] = sync_audio_stream;
1253 size_t num_synced_streams = 0;
1254 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1255 if (video_stream->config().sync_group != sync_group)
1256 continue;
1257 ++num_synced_streams;
1258 if (num_synced_streams > 1) {
1259 // TODO(pbos): Support synchronizing more than one A/V pair.
1260 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001261 RTC_LOG(LS_WARNING)
1262 << "Attempting to sync more than one audio/video pair "
1263 "within the same sync group. This is not supported in "
1264 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001265 }
1266 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001267 if (num_synced_streams == 1) {
1268 // sync_audio_stream may be null and that's ok.
1269 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001270 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001271 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001272 }
1273 }
1274}
1275
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001276PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1277 const uint8_t* packet,
1278 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001279 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001280 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001281 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1282 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001283 if (received_bytes_per_second_counter_.HasSample()) {
1284 // First RTP packet has been received.
1285 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1286 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1287 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001288 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001289 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001290 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001291 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001292 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001293 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001294 }
1295 }
1296 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1297 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001298 for (AudioReceiveStream* stream : audio_receive_streams_) {
1299 if (stream->DeliverRtcp(packet, length))
mflodman3d7db262016-04-29 00:57:13 -07001300 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001301 }
1302 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001303 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001304 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001305 for (VideoSendStream* stream : video_send_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001306 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001307 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001308 }
1309 }
mflodman3d7db262016-04-29 00:57:13 -07001310 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1311 ReadLockScoped read_lock(*send_crit_);
1312 for (auto& kv : audio_send_ssrcs_) {
1313 if (kv.second->DeliverRtcp(packet, length))
1314 rtcp_delivered = true;
1315 }
1316 }
1317
Elad Alon4a87e1c2017-10-03 16:11:34 +02001318 if (rtcp_delivered) {
1319 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketIncoming>(
1320 rtc::MakeArrayView(packet, length)));
1321 }
mflodman3d7db262016-04-29 00:57:13 -07001322
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001323 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001324}
1325
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001326PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
1327 const uint8_t* packet,
stefan68786d22015-09-08 05:36:15 -07001328 size_t length,
1329 const PacketTime& packet_time) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001330 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001331
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001332 RtpPacketReceived parsed_packet;
1333 if (!parsed_packet.Parse(packet, length))
1334 return DELIVERY_PACKET_ERROR;
1335
1336 if (packet_time.timestamp != -1) {
1337 parsed_packet.set_arrival_time_ms((packet_time.timestamp + 500) / 1000);
1338 } else {
1339 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1340 }
nissed44ce052017-02-06 02:23:00 -08001341
sprangc1abde72017-07-11 03:56:21 -07001342 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1343 // These are empty (zero length payload) RTP packets with an unsignaled
1344 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001345 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001346
1347 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1348 is_keep_alive_packet);
1349
sprangc1abde72017-07-11 03:56:21 -07001350 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001351 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001352 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001353 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1354 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001355 // Destruction of the receive stream, including deregistering from the
1356 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1357 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1358 // So by not passing the packet on to demuxing in this case, we prevent
1359 // incoming packets to be passed on via the demuxer to a receive stream
1360 // which is being torned down.
1361 return DELIVERY_UNKNOWN_SSRC;
1362 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001363 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001364
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001365 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001366
nissee5ad5ca2017-03-29 23:57:43 -07001367 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001368 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
asapersson250fd972016-09-08 00:07:21 -07001369 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1370 received_audio_bytes_per_second_counter_.Add(static_cast<int>(length));
Elad Alon4a87e1c2017-10-03 16:11:34 +02001371 event_log_->Log(
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001372 rtc::MakeUnique<RtcEventRtpPacketIncoming>(parsed_packet));
1373 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001374 if (!first_received_rtp_audio_ms_) {
1375 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1376 }
1377 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001378 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001379 }
nissee4bcd6d2017-05-16 04:47:04 -07001380 } else if (media_type == MediaType::VIDEO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001381 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
asapersson250fd972016-09-08 00:07:21 -07001382 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1383 received_video_bytes_per_second_counter_.Add(static_cast<int>(length));
Elad Alon4a87e1c2017-10-03 16:11:34 +02001384 event_log_->Log(
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001385 rtc::MakeUnique<RtcEventRtpPacketIncoming>(parsed_packet));
1386 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001387 if (!first_received_rtp_video_ms_) {
1388 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1389 }
1390 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001391 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001392 }
1393 }
1394 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001395}
1396
stefan68786d22015-09-08 05:36:15 -07001397PacketReceiver::DeliveryStatus Call::DeliverPacket(
1398 MediaType media_type,
1399 const uint8_t* packet,
1400 size_t length,
1401 const PacketTime& packet_time) {
eladalond1dd2f72017-08-25 02:55:57 -07001402 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001403 if (RtpHeaderParser::IsRtcp(packet, length))
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001404 return DeliverRtcp(media_type, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001405
stefan68786d22015-09-08 05:36:15 -07001406 return DeliverRtp(media_type, packet, length, packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001407}
1408
nissed2ef3142017-05-11 08:00:58 -07001409void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001410 RtpPacketReceived parsed_packet;
1411 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001412 return;
1413
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001414 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001415
brandtrcaea68f2017-08-23 00:55:17 -07001416 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001417 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001418 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001419 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1420 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001421 // Destruction of the receive stream, including deregistering from the
1422 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1423 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1424 // So by not passing the packet on to demuxing in this case, we prevent
1425 // incoming packets to be passed on via the demuxer to a receive stream
1426 // which is being torned down.
1427 return;
1428 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001429 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001430
1431 // TODO(brandtr): Update here when we support protecting audio packets too.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001432 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001433}
1434
nissed44ce052017-02-06 02:23:00 -08001435void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1436 MediaType media_type) {
1437 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001438 bool use_send_side_bwe =
1439 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001440
brandtrb29e6522016-12-21 06:37:18 -08001441 RTPHeader header;
1442 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001443
nisse4709e892017-02-07 01:18:43 -08001444 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001445 // Inconsistent configuration of send side BWE. Do nothing.
1446 // TODO(nisse): Without this check, we may produce RTCP feedback
1447 // packets even when not negotiated. But it would be cleaner to
1448 // move the check down to RTCPSender::SendFeedbackPacket, which
1449 // would also help the PacketRouter to select an appropriate rtp
1450 // module in the case that some, but not all, have RTCP feedback
1451 // enabled.
1452 return;
1453 }
1454 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001455 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001456 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001457 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001458 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1459 header);
1460 }
brandtrb29e6522016-12-21 06:37:18 -08001461}
1462
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001463} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001464
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001465} // namespace webrtc