blob: 338a135900d3afcbb43a7006b2e3ca4884daecdc [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "audio/time_interval.h"
24#include "call/bitrate_allocator.h"
25#include "call/call.h"
26#include "call/flexfec_receive_stream_impl.h"
27#include "call/rtp_stream_receiver_controller.h"
28#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020029#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
30#include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h"
31#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
32#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
33#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
34#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon99a81b62017-09-21 10:25:29 +020036#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "modules/bitrate_controller/include/bitrate_controller.h"
Sebastian Janssone4be6da2018-02-15 16:51:41 +010038#include "modules/congestion_controller/include/network_changed_observer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
40#include "modules/rtp_rtcp/include/flexfec_receiver.h"
41#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
42#include "modules/rtp_rtcp/include/rtp_header_parser.h"
43#include "modules/rtp_rtcp/source/byte_io.h"
44#include "modules/rtp_rtcp/source/rtp_packet_received.h"
45#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010046#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/basictypes.h"
48#include "rtc_base/checks.h"
49#include "rtc_base/constructormagic.h"
50#include "rtc_base/location.h"
51#include "rtc_base/logging.h"
52#include "rtc_base/ptr_util.h"
53#include "rtc_base/sequenced_task_checker.h"
54#include "rtc_base/task_queue.h"
55#include "rtc_base/thread_annotations.h"
56#include "rtc_base/trace_event.h"
57#include "system_wrappers/include/clock.h"
58#include "system_wrappers/include/cpu_info.h"
59#include "system_wrappers/include/metrics.h"
60#include "system_wrappers/include/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "video/call_stats.h"
62#include "video/send_delay_stats.h"
63#include "video/stats_counter.h"
64#include "video/video_receive_stream.h"
65#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000066
67namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000068
nisse4709e892017-02-07 01:18:43 -080069namespace {
70
71// TODO(nisse): This really begs for a shared context struct.
72bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
73 bool transport_cc) {
74 if (!transport_cc)
75 return false;
76 for (const auto& extension : extensions) {
77 if (extension.uri == RtpExtension::kTransportSequenceNumberUri)
78 return true;
79 }
80 return false;
81}
82
83bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
84 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
85}
86
87bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
88 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
89}
90
91bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
92 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
93}
94
nisse26e3abb2017-08-25 04:44:25 -070095const int* FindKeyByValue(const std::map<int, int>& m, int v) {
96 for (const auto& kv : m) {
97 if (kv.second == v)
98 return &kv.first;
99 }
100 return nullptr;
101}
102
eladalon8ec568a2017-09-08 06:15:52 -0700103std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700104 const VideoReceiveStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700105 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
106 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
107 rtclog_config->local_ssrc = config.rtp.local_ssrc;
108 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
109 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
110 rtclog_config->remb = config.rtp.remb;
111 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700112
113 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700114 const int* search =
115 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
eladalon8ec568a2017-09-08 06:15:52 -0700116 rtclog_config->codecs.emplace_back(d.payload_name, d.payload_type,
nisse26e3abb2017-08-25 04:44:25 -0700117 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700118 }
119 return rtclog_config;
120}
121
eladalon8ec568a2017-09-08 06:15:52 -0700122std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700123 const VideoSendStream::Config& config,
124 size_t ssrc_index) {
eladalon8ec568a2017-09-08 06:15:52 -0700125 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
126 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700127 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700128 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700129 }
eladalon8ec568a2017-09-08 06:15:52 -0700130 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
131 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700132
eladalon8ec568a2017-09-08 06:15:52 -0700133 rtclog_config->codecs.emplace_back(config.encoder_settings.payload_name,
134 config.encoder_settings.payload_type,
135 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700136 return rtclog_config;
137}
138
eladalon8ec568a2017-09-08 06:15:52 -0700139std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700140 const AudioReceiveStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700141 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
142 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
143 rtclog_config->local_ssrc = config.rtp.local_ssrc;
144 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700145 return rtclog_config;
146}
147
eladalon8ec568a2017-09-08 06:15:52 -0700148std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjf4726992017-05-22 10:12:26 -0700149 const AudioSendStream::Config& config) {
eladalon8ec568a2017-09-08 06:15:52 -0700150 auto rtclog_config = rtc::MakeUnique<rtclog::StreamConfig>();
151 rtclog_config->local_ssrc = config.rtp.ssrc;
152 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjf4726992017-05-22 10:12:26 -0700153 if (config.send_codec_spec) {
eladalon8ec568a2017-09-08 06:15:52 -0700154 rtclog_config->codecs.emplace_back(config.send_codec_spec->format.name,
155 config.send_codec_spec->payload_type, 0);
perkjf4726992017-05-22 10:12:26 -0700156 }
157 return rtclog_config;
158}
159
nisse4709e892017-02-07 01:18:43 -0800160} // namespace
161
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000162namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000163
perkjec81bcd2016-05-11 06:01:13 -0700164class Call : public webrtc::Call,
165 public PacketReceiver,
brandtr4e523862016-10-18 23:50:45 -0700166 public RecoveredPacketReceiver,
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100167 public NetworkChangedObserver,
perkj71ee44c2016-06-15 00:47:53 -0700168 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000169 public:
nisseb8f9a322017-03-27 05:36:15 -0700170 Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700171 std::unique_ptr<RtpTransportControllerSendInterface> transport_send);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000172 virtual ~Call();
173
brandtr25445d32016-10-23 23:37:14 -0700174 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000175 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000176
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200177 webrtc::AudioSendStream* CreateAudioSendStream(
178 const webrtc::AudioSendStream::Config& config) override;
179 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
180
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200181 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
182 const webrtc::AudioReceiveStream::Config& config) override;
183 void DestroyAudioReceiveStream(
184 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000185
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200186 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700187 webrtc::VideoSendStream::Config config,
188 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100189 webrtc::VideoSendStream* CreateVideoSendStream(
190 webrtc::VideoSendStream::Config config,
191 VideoEncoderConfig encoder_config,
192 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000193 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000194
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200195 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200196 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000197 void DestroyVideoReceiveStream(
198 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000199
brandtr7250b392016-12-19 01:13:46 -0800200 FlexfecReceiveStream* CreateFlexfecReceiveStream(
201 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700202 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800203 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700204
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000205 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000206
brandtr25445d32016-10-23 23:37:14 -0700207 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700208 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100209 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -0700210 const PacketTime& packet_time) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000211
brandtr4e523862016-10-18 23:50:45 -0700212 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700213 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700214
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000215 void SetBitrateConfig(
216 const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
skvlad7a43d252016-03-22 15:32:27 -0700217
zstein4b979802017-06-02 14:37:37 -0700218 void SetBitrateConfigMask(
219 const webrtc::Call::Config::BitrateConfigMask& bitrate_config) override;
220
Alex Narest78609d52017-10-20 10:37:47 +0200221 void SetBitrateAllocationStrategy(
222 std::unique_ptr<rtc::BitrateAllocationStrategy>
223 bitrate_allocation_strategy) override;
224
skvlad7a43d252016-03-22 15:32:27 -0700225 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000226
michaelt79e05882016-11-08 02:50:09 -0800227 void OnTransportOverheadChanged(MediaType media,
228 int transport_overhead_per_packet) override;
229
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700230 void OnNetworkRouteChanged(const std::string& transport_name,
231 const rtc::NetworkRoute& network_route) override;
232
stefanc1aeaf02015-10-15 07:26:07 -0700233 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
234
mflodman0e7e2592015-11-12 21:02:42 -0800235 // Implements BitrateObserver.
minyue78b4d562016-11-30 04:47:39 -0800236 void OnNetworkChanged(uint32_t bitrate_bps,
237 uint8_t fraction_loss,
238 int64_t rtt_ms,
239 int64_t probing_interval_ms) override;
mflodman0e7e2592015-11-12 21:02:42 -0800240
perkj71ee44c2016-06-15 00:47:53 -0700241 // Implements BitrateAllocator::LimitObserver.
242 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
243 uint32_t max_padding_bitrate_bps) override;
244
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000245 private:
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200246 DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
247 size_t length);
stefan68786d22015-09-08 05:36:15 -0700248 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100249 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -0700250 const PacketTime& packet_time);
pbos8fc7fa72015-07-15 08:02:58 -0700251 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700252 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700253
nissed44ce052017-02-06 02:23:00 -0800254 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
255 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700256 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800257
asaperssonfc5e81c2017-04-19 23:28:53 -0700258 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700259 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800260 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700261 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700262 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800263
zstein4b979802017-06-02 14:37:37 -0700264 // Applies update to the BitrateConfig cached in |config_|, restarting
265 // bandwidth estimation from |new_start| if set.
266 void UpdateCurrentBitrateConfig(const rtc::Optional<int>& new_start);
267
Peter Boströmd3c94472015-12-09 11:20:58 +0100268 Clock* const clock_;
stefan91d92602015-11-11 10:13:02 -0800269
Peter Boström45553ae2015-05-08 13:54:38 +0200270 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800271 const std::unique_ptr<ProcessThread> module_process_thread_;
nisseb9359842017-01-19 05:41:25 -0800272 const std::unique_ptr<ProcessThread> pacer_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800273 const std::unique_ptr<CallStats> call_stats_;
274 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000275 Call::Config config_;
eladalonf3f5c0e2017-08-18 02:47:08 -0700276 rtc::SequencedTaskChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000277
skvlad7a43d252016-03-22 15:32:27 -0700278 NetworkState audio_network_state_;
279 NetworkState video_network_state_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000280
kwibergb25345e2016-03-12 06:10:44 -0800281 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700282 // Audio, Video, and FlexFEC receive streams are owned by the client that
283 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700284 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700285 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200286 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700287 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700288
pbos8fc7fa72015-07-15 08:02:58 -0700289 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700290 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000291
nisse0f15f922017-06-21 01:05:22 -0700292 // TODO(nisse): Should eventually be injected at creation,
293 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700294 RtpStreamReceiverController audio_receiver_controller_;
295 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700296
nissed44ce052017-02-06 02:23:00 -0800297 // This extra map is used for receive processing which is
298 // independent of media type.
299
300 // TODO(nisse): In the RTP transport refactoring, we should have a
301 // single mapping from ssrc to a more abstract receive stream, with
302 // accessor methods for all configuration we need at this level.
303 struct ReceiveRtpConfig {
304 ReceiveRtpConfig() = default; // Needed by std::map
305 ReceiveRtpConfig(const std::vector<RtpExtension>& extensions,
nisse4709e892017-02-07 01:18:43 -0800306 bool use_send_side_bwe)
307 : extensions(extensions), use_send_side_bwe(use_send_side_bwe) {}
nissed44ce052017-02-06 02:23:00 -0800308
309 // Registered RTP header extensions for each stream. Note that RTP header
310 // extensions are negotiated per track ("m= line") in the SDP, but we have
311 // no notion of tracks at the Call level. We therefore store the RTP header
312 // extensions per SSRC instead, which leads to some storage overhead.
313 RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800314 // Set if both RTP extension the RTCP feedback message needed for
315 // send side BWE are negotiated.
316 bool use_send_side_bwe = false;
nissed44ce052017-02-06 02:23:00 -0800317 };
318 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700319 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800320
kwibergb25345e2016-03-12 06:10:44 -0800321 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700322 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700323 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
324 RTC_GUARDED_BY(send_crit_);
325 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
326 RTC_GUARDED_BY(send_crit_);
327 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000328
ossuc3d4b482017-05-23 06:07:11 -0700329 using RtpStateMap = std::map<uint32_t, RtpState>;
330 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700331 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700332 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700333 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700334
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200335 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
336 RtpPayloadStateMap suspended_video_payload_states_
337 RTC_GUARDED_BY(configuration_sequence_checker_);
338
skvlad11a9cbf2016-10-07 11:53:05 -0700339 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700340
stefan18adf0a2015-11-17 06:24:56 -0800341 // The following members are only accessed (exclusively) from one thread and
342 // from the destructor, and therefore doesn't need any explicit
343 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700344 RateCounter received_bytes_per_second_counter_;
345 RateCounter received_audio_bytes_per_second_counter_;
346 RateCounter received_video_bytes_per_second_counter_;
347 RateCounter received_rtcp_bytes_per_second_counter_;
saza0d7f04d2017-07-04 04:05:06 -0700348 rtc::Optional<int64_t> first_received_rtp_audio_ms_;
349 rtc::Optional<int64_t> last_received_rtp_audio_ms_;
350 rtc::Optional<int64_t> first_received_rtp_video_ms_;
351 rtc::Optional<int64_t> last_received_rtp_video_ms_;
sazac58f8c02017-07-19 00:39:19 -0700352 TimeInterval sent_rtp_audio_timer_ms_;
stefan91d92602015-11-11 10:13:02 -0800353
stefan18adf0a2015-11-17 06:24:56 -0800354 // TODO(holmer): Remove this lock once BitrateController no longer calls
355 // OnNetworkChanged from multiple threads.
356 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700357 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
358 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
359 AvgCounter estimated_send_bitrate_kbps_counter_
360 RTC_GUARDED_BY(&bitrate_crit_);
361 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800362
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700363 std::map<std::string, rtc::NetworkRoute> network_routes_;
364
nisse6167b262017-04-06 06:34:25 -0700365 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
nisse559af382017-03-21 06:41:12 -0700366 ReceiveSideCongestionController receive_side_cc_;
asapersson35151f32016-05-02 23:44:01 -0700367 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700368 const int64_t start_ms_;
perkj26091b12016-09-01 01:17:40 -0700369 // TODO(perkj): |worker_queue_| is supposed to replace
370 // |module_process_thread_|.
371 // |worker_queue| is defined last to ensure all pending tasks are cancelled
372 // and deleted before any other members.
373 rtc::TaskQueue worker_queue_;
mflodman0e7e2592015-11-12 21:02:42 -0800374
zstein4b979802017-06-02 14:37:37 -0700375 // The config mask set by SetBitrateConfigMask.
376 // 0 <= min <= start <= max
377 Config::BitrateConfigMask bitrate_config_mask_;
378
379 // The config set by SetBitrateConfig.
380 // min >= 0, start != 0, max == -1 || max > 0
381 Config::BitrateConfig base_bitrate_config_;
382
henrikg3c089d72015-09-16 05:37:44 -0700383 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000384};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000385} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000386
asapersson2e5cfcd2016-08-11 08:41:18 -0700387std::string Call::Stats::ToString(int64_t time_ms) const {
388 std::stringstream ss;
389 ss << "Call stats: " << time_ms << ", {";
390 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
391 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
392 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
393 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
394 ss << "rtt_ms: " << rtt_ms;
395 ss << '}';
396 return ss.str();
397}
398
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000399Call* Call::Create(const Call::Config& config) {
zstein7cb69d52017-05-08 11:52:38 -0700400 return new internal::Call(config,
401 rtc::MakeUnique<RtpTransportControllerSend>(
402 Clock::GetRealTimeClock(), config.event_log));
403}
404
405Call* Call::Create(
406 const Call::Config& config,
407 std::unique_ptr<RtpTransportControllerSendInterface> transport_send) {
408 return new internal::Call(config, std::move(transport_send));
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000409}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000410
Ying Wang3b790f32018-01-19 17:58:57 +0100411VideoSendStream* Call::CreateVideoSendStream(
412 VideoSendStream::Config config,
413 VideoEncoderConfig encoder_config,
414 std::unique_ptr<FecController> fec_controller) {
415 return nullptr;
416}
417
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000418namespace internal {
419
nisseb8f9a322017-03-27 05:36:15 -0700420Call::Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700421 std::unique_ptr<RtpTransportControllerSendInterface> transport_send)
stefan91d92602015-11-11 10:13:02 -0800422 : clock_(Clock::GetRealTimeClock()),
423 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
kwiberg1c7fdd82016-04-26 08:18:04 -0700424 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
nisseb9359842017-01-19 05:41:25 -0800425 pacer_thread_(ProcessThread::Create("PacerThread")),
Peter Boströmd3c94472015-12-09 11:20:58 +0100426 call_stats_(new CallStats(clock_)),
perkj71ee44c2016-06-15 00:47:53 -0700427 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200428 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800429 audio_network_state_(kNetworkDown),
430 video_network_state_(kNetworkDown),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000431 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800432 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700433 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700434 received_bytes_per_second_counter_(clock_, nullptr, true),
435 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
436 received_video_bytes_per_second_counter_(clock_, nullptr, true),
437 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
perkj71ee44c2016-06-15 00:47:53 -0700438 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700439 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700440 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
441 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700442 receive_side_cc_(clock_, transport_send->packet_router()),
asapersson4374a092016-07-27 00:39:09 -0700443 video_send_delay_stats_(new SendDelayStats(clock_)),
perkj26091b12016-09-01 01:17:40 -0700444 start_ms_(clock_->TimeInMilliseconds()),
zstein4b979802017-06-02 14:37:37 -0700445 worker_queue_("call_worker_queue"),
446 base_bitrate_config_(config.bitrate_config) {
skvlad11a9cbf2016-10-07 11:53:05 -0700447 RTC_DCHECK(config.event_log != nullptr);
henrikg91d6ede2015-09-17 00:24:34 -0700448 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
stefanfca900a2017-04-10 03:53:00 -0700449 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
henrikg91d6ede2015-09-17 00:24:34 -0700450 config.bitrate_config.min_bitrate_bps);
Stefan Holmere5904162015-03-26 11:11:06 +0100451 if (config.bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700452 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
453 config.bitrate_config.start_bitrate_bps);
pbos@webrtc.org00873182014-11-25 14:03:34 +0000454 }
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100455 transport_send->RegisterNetworkObserver(this);
nisse6167b262017-04-06 06:34:25 -0700456 transport_send_ = std::move(transport_send);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100457 transport_send_->OnNetworkAvailability(false);
458 transport_send_->SetBweBitrates(config_.bitrate_config.min_bitrate_bps,
459 config_.bitrate_config.start_bitrate_bps,
460 config_.bitrate_config.max_bitrate_bps);
nissebcbaf742017-03-28 01:16:25 -0700461 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100462 call_stats_->RegisterStatsObserver(transport_send_->GetCallStatsObserver());
Stefan Holmerc379fcb2016-02-24 16:02:55 +0100463
stefan9e117c5e12017-08-16 08:16:25 -0700464 // We have to attach the pacer to the pacer thread before starting the
465 // module process thread to avoid a race accessing the process thread
466 // both from the process thread and the pacer thread.
Stefan Holmer5c8942a2017-08-22 16:16:44 +0200467 pacer_thread_->RegisterModule(transport_send_->pacer(), RTC_FROM_HERE);
stefan64136af2017-08-14 08:03:17 -0700468 pacer_thread_->RegisterModule(
469 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
stefan64136af2017-08-14 08:03:17 -0700470 pacer_thread_->Start();
stefan9e117c5e12017-08-16 08:16:25 -0700471
472 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
473 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100474 module_process_thread_->RegisterModule(transport_send_->GetModule(),
stefan9e117c5e12017-08-16 08:16:25 -0700475 RTC_FROM_HERE);
476 module_process_thread_->Start();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000477}
478
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000479Call::~Call() {
eladalonf3f5c0e2017-08-18 02:47:08 -0700480 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700481
solenbergc7a8b082015-10-16 14:35:07 -0700482 RTC_CHECK(audio_send_ssrcs_.empty());
483 RTC_CHECK(video_send_ssrcs_.empty());
484 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700485 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700486 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000487
stefan9e117c5e12017-08-16 08:16:25 -0700488 // The send-side congestion controller must be de-registered prior to
489 // the pacer thread being stopped to avoid a race when accessing the
490 // pacer thread object on the module process thread at the same time as
491 // the pacer thread is stopped.
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100492 module_process_thread_->DeRegisterModule(transport_send_->GetModule());
nisseb9359842017-01-19 05:41:25 -0800493 pacer_thread_->Stop();
Stefan Holmer5c8942a2017-08-22 16:16:44 +0200494 pacer_thread_->DeRegisterModule(transport_send_->pacer());
nisseb9359842017-01-19 05:41:25 -0800495 pacer_thread_->DeRegisterModule(
nisse559af382017-03-21 06:41:12 -0700496 receive_side_cc_.GetRemoteBitrateEstimator(true));
nisse559af382017-03-21 06:41:12 -0700497 module_process_thread_->DeRegisterModule(&receive_side_cc_);
mflodmane3787022015-10-21 13:24:28 +0200498 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200499 module_process_thread_->Stop();
nissebcbaf742017-03-28 01:16:25 -0700500 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100501 call_stats_->DeregisterStatsObserver(transport_send_->GetCallStatsObserver());
sprang6d6122b2016-07-13 06:37:09 -0700502
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100503 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700504 // Only update histograms after process threads have been shut down, so that
505 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700506 {
507 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700508 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700509 }
sprang6d6122b2016-07-13 06:37:09 -0700510 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700511 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000512}
513
asapersson4374a092016-07-27 00:39:09 -0700514void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700515 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700516 "WebRTC.Call.LifetimeInSeconds",
517 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
518}
519
asaperssonfc5e81c2017-04-19 23:28:53 -0700520void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
521 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800522 return;
sazac58f8c02017-07-19 00:39:19 -0700523 if (!sent_rtp_audio_timer_ms_.Empty()) {
524 RTC_HISTOGRAM_COUNTS_100000(
525 "WebRTC.Call.TimeSendingAudioRtpPacketsInSeconds",
526 sent_rtp_audio_timer_ms_.Length() / 1000);
527 }
stefan18adf0a2015-11-17 06:24:56 -0800528 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700529 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800530 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
531 return;
asaperssonce2e1362016-09-09 00:13:35 -0700532 const int kMinRequiredPeriodicSamples = 5;
533 AggregatedStats send_bitrate_stats =
534 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
535 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700536 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
537 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100538 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
539 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800540 }
asaperssonce2e1362016-09-09 00:13:35 -0700541 AggregatedStats pacer_bitrate_stats =
542 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
543 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700544 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
545 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100546 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
547 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800548 }
549}
550
551void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700552 if (first_received_rtp_audio_ms_) {
553 RTC_HISTOGRAM_COUNTS_100000(
554 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
555 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
556 }
557 if (first_received_rtp_video_ms_) {
558 RTC_HISTOGRAM_COUNTS_100000(
559 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
560 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
561 }
asapersson250fd972016-09-08 00:07:21 -0700562 const int kMinRequiredPeriodicSamples = 5;
563 AggregatedStats video_bytes_per_sec =
564 received_video_bytes_per_second_counter_.GetStats();
565 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700566 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
567 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100568 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
569 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800570 }
asapersson250fd972016-09-08 00:07:21 -0700571 AggregatedStats audio_bytes_per_sec =
572 received_audio_bytes_per_second_counter_.GetStats();
573 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700574 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
575 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100576 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
577 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800578 }
asapersson250fd972016-09-08 00:07:21 -0700579 AggregatedStats rtcp_bytes_per_sec =
580 received_rtcp_bytes_per_second_counter_.GetStats();
581 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700582 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
583 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100584 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
585 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800586 }
asapersson250fd972016-09-08 00:07:21 -0700587 AggregatedStats recv_bytes_per_sec =
588 received_bytes_per_second_counter_.GetStats();
589 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700590 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
591 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100592 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
593 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700594 }
stefan91d92602015-11-11 10:13:02 -0800595}
596
solenberg5a289392015-10-19 03:39:20 -0700597PacketReceiver* Call::Receiver() {
eladalond1dd2f72017-08-25 02:55:57 -0700598 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700599 return this;
600}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000601
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200602webrtc::AudioSendStream* Call::CreateAudioSendStream(
603 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700604 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700605 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200606 event_log_->Log(rtc::MakeUnique<RtcEventAudioSendStreamConfig>(
607 CreateRtcLogStreamConfig(config)));
ossuc3d4b482017-05-23 06:07:11 -0700608
609 rtc::Optional<RtpState> suspended_rtp_state;
610 {
611 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
612 if (iter != suspended_audio_send_ssrcs_.end()) {
613 suspended_rtp_state.emplace(iter->second);
614 }
615 }
616
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100617 AudioSendStream* send_stream = new AudioSendStream(
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100618 config, config_.audio_state, &worker_queue_, module_process_thread_.get(),
619 transport_send_.get(), bitrate_allocator_.get(), event_log_,
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100620 call_stats_->rtcp_rtt_stats(), suspended_rtp_state,
621 &sent_rtp_audio_timer_ms_);
solenbergc7a8b082015-10-16 14:35:07 -0700622 {
solenbergc7a8b082015-10-16 14:35:07 -0700623 WriteLockScoped write_lock(*send_crit_);
624 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
625 audio_send_ssrcs_.end());
626 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700627 }
solenberg7602aab2016-11-14 11:30:07 -0800628 {
629 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700630 for (AudioReceiveStream* stream : audio_receive_streams_) {
631 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
632 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800633 }
634 }
635 }
skvlad7a43d252016-03-22 15:32:27 -0700636 send_stream->SignalNetworkState(audio_network_state_);
637 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700638 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200639}
640
641void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700642 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700643 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700644 RTC_DCHECK(send_stream != nullptr);
645
646 send_stream->Stop();
647
eladalonabbc4302017-07-26 02:09:44 -0700648 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700649 webrtc::internal::AudioSendStream* audio_send_stream =
650 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700651 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700652 {
653 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800654 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
655 RTC_DCHECK_EQ(1, num_deleted);
656 }
657 {
658 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700659 for (AudioReceiveStream* stream : audio_receive_streams_) {
660 if (stream->config().rtp.local_ssrc == ssrc) {
661 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800662 }
663 }
solenbergc7a8b082015-10-16 14:35:07 -0700664 }
skvlad7a43d252016-03-22 15:32:27 -0700665 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700666 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200667}
668
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200669webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
670 const webrtc::AudioReceiveStream::Config& config) {
671 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700672 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Elad Alon4a87e1c2017-10-03 16:11:34 +0200673 event_log_->Log(rtc::MakeUnique<RtcEventAudioReceiveStreamConfig>(
674 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700675 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100676 &audio_receiver_controller_, transport_send_->packet_router(),
677 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200678 {
679 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800680 receive_rtp_config_[config.rtp.remote_ssrc] =
nisse4709e892017-02-07 01:18:43 -0800681 ReceiveRtpConfig(config.rtp.extensions, UseSendSideBwe(config));
nissee4bcd6d2017-05-16 04:47:04 -0700682 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800683
pbos8fc7fa72015-07-15 08:02:58 -0700684 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200685 }
solenberg7602aab2016-11-14 11:30:07 -0800686 {
687 ReadLockScoped read_lock(*send_crit_);
688 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
689 if (it != audio_send_ssrcs_.end()) {
690 receive_stream->AssociateSendStream(it->second);
691 }
692 }
skvlad7a43d252016-03-22 15:32:27 -0700693 receive_stream->SignalNetworkState(audio_network_state_);
694 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200695 return receive_stream;
696}
697
698void Call::DestroyAudioReceiveStream(
699 webrtc::AudioReceiveStream* receive_stream) {
700 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700701 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700702 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700703 webrtc::internal::AudioReceiveStream* audio_receive_stream =
704 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200705 {
706 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800707 const AudioReceiveStream::Config& config = audio_receive_stream->config();
708 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700709 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800710 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700711 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700712 const std::string& sync_group = audio_receive_stream->config().sync_group;
713 const auto it = sync_stream_mapping_.find(sync_group);
714 if (it != sync_stream_mapping_.end() &&
715 it->second == audio_receive_stream) {
716 sync_stream_mapping_.erase(it);
717 ConfigureSync(sync_group);
718 }
nissed44ce052017-02-06 02:23:00 -0800719 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200720 }
skvlad7a43d252016-03-22 15:32:27 -0700721 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200722 delete audio_receive_stream;
723}
724
Taylor Brandstetter00733012018-02-15 20:07:11 +0000725webrtc::VideoSendStream* Call::CreateVideoSendStream(
726 webrtc::VideoSendStream::Config config,
727 VideoEncoderConfig encoder_config) {
728 return CreateVideoSendStream(
729 std::move(config), std::move(encoder_config),
730 rtc::MakeUnique<FecControllerDefault>(Clock::GetRealTimeClock()));
731}
732
Ying Wang3b790f32018-01-19 17:58:57 +0100733webrtc::VideoSendStream* Call::CreateVideoSendStream(
734 webrtc::VideoSendStream::Config config,
735 VideoEncoderConfig encoder_config,
736 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000737 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700738 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000739
asapersson35151f32016-05-02 23:44:01 -0700740 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700741 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
742 ++ssrc_index) {
Elad Alon4a87e1c2017-10-03 16:11:34 +0200743 event_log_->Log(rtc::MakeUnique<RtcEventVideoSendStreamConfig>(
744 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700745 }
perkj26091b12016-09-01 01:17:40 -0700746
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000747 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
748 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700749 // Copy ssrcs from |config| since |config| is moved.
750 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
mflodman0c478b32015-10-21 15:52:16 +0200751 VideoSendStream* send_stream = new VideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700752 num_cpu_cores_, module_process_thread_.get(), &worker_queue_,
nisseb8f9a322017-03-27 05:36:15 -0700753 call_stats_.get(), transport_send_.get(), bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700754 video_send_delay_stats_.get(), event_log_, std::move(config),
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200755 std::move(encoder_config), suspended_video_send_ssrcs_,
Ying Wang3b790f32018-01-19 17:58:57 +0100756 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700757
skvlad7a43d252016-03-22 15:32:27 -0700758 {
759 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700760 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700761 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
762 video_send_ssrcs_[ssrc] = send_stream;
763 }
764 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000765 }
skvlad7a43d252016-03-22 15:32:27 -0700766 send_stream->SignalNetworkState(video_network_state_);
767 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700768
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000769 return send_stream;
770}
771
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000772void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000773 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700774 RTC_DCHECK(send_stream != nullptr);
eladalonf3f5c0e2017-08-18 02:47:08 -0700775 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000776
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000777 send_stream->Stop();
778
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000779 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000780 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000781 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200782 auto it = video_send_ssrcs_.begin();
783 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000784 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
785 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200786 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000787 } else {
788 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000789 }
790 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200791 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000792 }
henrikg91d6ede2015-09-17 00:24:34 -0700793 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000794
Ã…sa Persson4bece9a2017-10-06 10:04:04 +0200795 VideoSendStream::RtpStateMap rtp_states;
796 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
797 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
798 &rtp_payload_states);
799 for (const auto& kv : rtp_states) {
800 suspended_video_send_ssrcs_[kv.first] = kv.second;
801 }
802 for (const auto& kv : rtp_payload_states) {
803 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000804 }
805
skvlad7a43d252016-03-22 15:32:27 -0700806 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000807 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000808}
809
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200810webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200811 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000812 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700813 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800814
nisse0f15f922017-06-21 01:05:22 -0700815 VideoReceiveStream* receive_stream = new VideoReceiveStream(
eladalon2a2b2972017-07-03 09:25:27 -0700816 &video_receiver_controller_, num_cpu_cores_,
nisse0f15f922017-06-21 01:05:22 -0700817 transport_send_->packet_router(), std::move(configuration),
818 module_process_thread_.get(), call_stats_.get());
Tommi733b5472016-06-10 17:58:01 +0200819
820 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
nissed44ce052017-02-06 02:23:00 -0800821 ReceiveRtpConfig receive_config(config.rtp.extensions,
nisse4709e892017-02-07 01:18:43 -0800822 UseSendSideBwe(config));
skvlad7a43d252016-03-22 15:32:27 -0700823 {
824 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800825 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800826 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700827 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800828 // type, we may get an incorrect value for the rtx stream, but
829 // that is unlikely to matter in practice.
830 receive_rtp_config_[config.rtp.rtx_ssrc] = receive_config;
831 }
832 receive_rtp_config_[config.rtp.remote_ssrc] = receive_config;
skvlad7a43d252016-03-22 15:32:27 -0700833 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700834 ConfigureSync(config.sync_group);
835 }
836 receive_stream->SignalNetworkState(video_network_state_);
837 UpdateAggregateNetworkState();
Elad Alon4a87e1c2017-10-03 16:11:34 +0200838 event_log_->Log(rtc::MakeUnique<RtcEventVideoReceiveStreamConfig>(
839 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000840 return receive_stream;
841}
842
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000843void Call::DestroyVideoReceiveStream(
844 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000845 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700846 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700847 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700848 VideoReceiveStream* receive_stream_impl =
849 static_cast<VideoReceiveStream*>(receive_stream);
850 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000851 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000852 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000853 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
854 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700855 receive_rtp_config_.erase(config.rtp.remote_ssrc);
856 if (config.rtp.rtx_ssrc) {
857 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000858 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200859 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700860 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000861 }
nisse4709e892017-02-07 01:18:43 -0800862
nisse559af382017-03-21 06:41:12 -0700863 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800864 ->RemoveStream(config.rtp.remote_ssrc);
865
skvlad7a43d252016-03-22 15:32:27 -0700866 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000867 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000868}
869
brandtr7250b392016-12-19 01:13:46 -0800870FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
871 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700872 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700873 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800874
875 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700876
nisse0f15f922017-06-21 01:05:22 -0700877 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700878 {
879 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -0700880 // Unlike the video and audio receive streams,
881 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
882 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -0700883 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -0700884 // constructor while holding |receive_crit_| ensures that we don't
885 // call OnRtpPacket until the constructor is finished and the
886 // object is in a valid state.
887 // TODO(nisse): Fix constructor so that it can be moved outside of
888 // this locked scope.
889 receive_stream = new FlexfecReceiveStreamImpl(
eladalon2a2b2972017-07-03 09:25:27 -0700890 &video_receiver_controller_, config, recovered_packet_receiver,
nisse0f15f922017-06-21 01:05:22 -0700891 call_stats_->rtcp_rtt_stats(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -0800892
nissed44ce052017-02-06 02:23:00 -0800893 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
894 receive_rtp_config_.end());
895 receive_rtp_config_[config.remote_ssrc] =
nisse4709e892017-02-07 01:18:43 -0800896 ReceiveRtpConfig(config.rtp_header_extensions, UseSendSideBwe(config));
brandtr25445d32016-10-23 23:37:14 -0700897 }
brandtrb29e6522016-12-21 06:37:18 -0800898
brandtr25445d32016-10-23 23:37:14 -0700899 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -0800900
brandtr25445d32016-10-23 23:37:14 -0700901 return receive_stream;
902}
903
brandtr7250b392016-12-19 01:13:46 -0800904void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -0700905 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700906 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800907
brandtr25445d32016-10-23 23:37:14 -0700908 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -0700909 {
910 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800911
eladalon42f44f92017-07-25 06:40:06 -0700912 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -0800913 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -0800914 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -0800915
brandtr7250b392016-12-19 01:13:46 -0800916 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
917 // destroyed.
nisse559af382017-03-21 06:41:12 -0700918 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800919 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -0700920 }
brandtrb29e6522016-12-21 06:37:18 -0800921
eladalon42f44f92017-07-25 06:40:06 -0700922 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700923}
924
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000925Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700926 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
927 // thread. Re-enable once that is fixed.
eladalonf3f5c0e2017-08-18 02:47:08 -0700928 // RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000929 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200930 // Fetch available send/receive bitrates.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000931 uint32_t send_bandwidth = 0;
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100932 transport_send_->AvailableBandwidth(&send_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200933 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000934 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -0700935 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700936 &ssrcs, &recv_bandwidth);
Peter Boström45553ae2015-05-08 13:54:38 +0200937 stats.send_bandwidth_bps = send_bandwidth;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000938 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100939 stats.pacer_delay_ms = transport_send_->GetPacerQueuingDelayMs();
sprange2d83d62016-02-19 09:03:26 -0800940 stats.rtt_ms = call_stats_->rtcp_rtt_stats()->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -0700941 {
942 rtc::CritScope cs(&bitrate_crit_);
943 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
944 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000945 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000946}
947
pbos@webrtc.org00873182014-11-25 14:03:34 +0000948void Call::SetBitrateConfig(
949 const webrtc::Call::Config::BitrateConfig& bitrate_config) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000950 TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
eladalonf3f5c0e2017-08-18 02:47:08 -0700951 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700952 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
zstein4b979802017-06-02 14:37:37 -0700953 RTC_DCHECK_NE(bitrate_config.start_bitrate_bps, 0);
954 if (bitrate_config.max_bitrate_bps != -1) {
henrikg91d6ede2015-09-17 00:24:34 -0700955 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
zstein4b979802017-06-02 14:37:37 -0700956 }
957
958 rtc::Optional<int> new_start;
959 // Only update the "start" bitrate if it's set, and different from the old
960 // value. In practice, this value comes from the x-google-start-bitrate codec
961 // parameter in SDP, and setting the same remote description twice shouldn't
962 // restart bandwidth estimation.
963 if (bitrate_config.start_bitrate_bps != -1 &&
964 bitrate_config.start_bitrate_bps !=
965 base_bitrate_config_.start_bitrate_bps) {
966 new_start.emplace(bitrate_config.start_bitrate_bps);
967 }
968 base_bitrate_config_ = bitrate_config;
969 UpdateCurrentBitrateConfig(new_start);
970}
971
972void Call::SetBitrateConfigMask(
973 const webrtc::Call::Config::BitrateConfigMask& mask) {
974 TRACE_EVENT0("webrtc", "Call::SetBitrateConfigMask");
eladalonf3f5c0e2017-08-18 02:47:08 -0700975 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
zstein4b979802017-06-02 14:37:37 -0700976
977 bitrate_config_mask_ = mask;
978 UpdateCurrentBitrateConfig(mask.start_bitrate_bps);
979}
980
zstein4b979802017-06-02 14:37:37 -0700981void Call::UpdateCurrentBitrateConfig(const rtc::Optional<int>& new_start) {
982 Config::BitrateConfig updated;
983 updated.min_bitrate_bps =
984 std::max(bitrate_config_mask_.min_bitrate_bps.value_or(0),
985 base_bitrate_config_.min_bitrate_bps);
986
987 updated.max_bitrate_bps =
988 MinPositive(bitrate_config_mask_.max_bitrate_bps.value_or(-1),
989 base_bitrate_config_.max_bitrate_bps);
990
991 // If the combined min ends up greater than the combined max, the max takes
992 // priority.
993 if (updated.max_bitrate_bps != -1 &&
994 updated.min_bitrate_bps > updated.max_bitrate_bps) {
995 updated.min_bitrate_bps = updated.max_bitrate_bps;
996 }
997
998 // If there is nothing to update (min/max unchanged, no new bandwidth
999 // estimation start value), return early.
1000 if (updated.min_bitrate_bps == config_.bitrate_config.min_bitrate_bps &&
1001 updated.max_bitrate_bps == config_.bitrate_config.max_bitrate_bps &&
1002 !new_start) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001003 RTC_LOG(LS_VERBOSE) << "WebRTC.Call.UpdateCurrentBitrateConfig: "
1004 << "nothing to update";
pbos@webrtc.org00873182014-11-25 14:03:34 +00001005 return;
1006 }
zstein4b979802017-06-02 14:37:37 -07001007
1008 if (new_start) {
1009 // Clamp start by min and max.
1010 updated.start_bitrate_bps = MinPositive(
1011 std::max(*new_start, updated.min_bitrate_bps), updated.max_bitrate_bps);
1012 } else {
1013 updated.start_bitrate_bps = -1;
1014 }
1015
Mirko Bonadei675513b2017-11-09 11:09:25 +01001016 RTC_LOG(INFO) << "WebRTC.Call.UpdateCurrentBitrateConfig: "
1017 << "calling SetBweBitrates with args ("
1018 << updated.min_bitrate_bps << ", " << updated.start_bitrate_bps
1019 << ", " << updated.max_bitrate_bps << ")";
Sebastian Janssone4be6da2018-02-15 16:51:41 +01001020 transport_send_->SetBweBitrates(updated.min_bitrate_bps,
1021 updated.start_bitrate_bps,
1022 updated.max_bitrate_bps);
zstein4b979802017-06-02 14:37:37 -07001023 if (!new_start) {
1024 updated.start_bitrate_bps = config_.bitrate_config.start_bitrate_bps;
1025 }
1026 config_.bitrate_config = updated;
pbos@webrtc.org00873182014-11-25 14:03:34 +00001027}
1028
Alex Narest78609d52017-10-20 10:37:47 +02001029void Call::SetBitrateAllocationStrategy(
1030 std::unique_ptr<rtc::BitrateAllocationStrategy>
1031 bitrate_allocation_strategy) {
1032 if (!worker_queue_.IsCurrent()) {
1033 rtc::BitrateAllocationStrategy* strategy_raw =
1034 bitrate_allocation_strategy.release();
1035 auto functor = [this, strategy_raw]() {
1036 SetBitrateAllocationStrategy(
1037 rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw));
1038 };
1039 worker_queue_.PostTask([functor] { functor(); });
1040 return;
1041 }
1042 RTC_DCHECK_RUN_ON(&worker_queue_);
1043 bitrate_allocator_->SetBitrateAllocationStrategy(
1044 std::move(bitrate_allocation_strategy));
1045}
1046
skvlad7a43d252016-03-22 15:32:27 -07001047void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
eladalonf3f5c0e2017-08-18 02:47:08 -07001048 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001049 switch (media) {
1050 case MediaType::AUDIO:
1051 audio_network_state_ = state;
1052 break;
1053 case MediaType::VIDEO:
1054 video_network_state_ = state;
1055 break;
1056 case MediaType::ANY:
1057 case MediaType::DATA:
1058 RTC_NOTREACHED();
1059 break;
1060 }
1061
1062 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001063 {
skvlad7a43d252016-03-22 15:32:27 -07001064 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -07001065 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001066 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -07001067 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001068 for (auto& kv : video_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001069 kv.second->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001070 }
1071 }
1072 {
skvlad7a43d252016-03-22 15:32:27 -07001073 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001074 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1075 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001076 }
nissee4bcd6d2017-05-16 04:47:04 -07001077 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1078 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001079 }
1080 }
1081}
1082
michaelt79e05882016-11-08 02:50:09 -08001083void Call::OnTransportOverheadChanged(MediaType media,
1084 int transport_overhead_per_packet) {
1085 switch (media) {
1086 case MediaType::AUDIO: {
1087 ReadLockScoped read_lock(*send_crit_);
1088 for (auto& kv : audio_send_ssrcs_) {
1089 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1090 }
1091 break;
1092 }
1093 case MediaType::VIDEO: {
1094 ReadLockScoped read_lock(*send_crit_);
1095 for (auto& kv : video_send_ssrcs_) {
1096 kv.second->SetTransportOverhead(transport_overhead_per_packet);
1097 }
1098 break;
1099 }
1100 case MediaType::ANY:
1101 case MediaType::DATA:
1102 RTC_NOTREACHED();
1103 break;
1104 }
1105}
1106
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001107// TODO(honghaiz): Add tests for this method.
1108void Call::OnNetworkRouteChanged(const std::string& transport_name,
1109 const rtc::NetworkRoute& network_route) {
eladalonf3f5c0e2017-08-18 02:47:08 -07001110 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001111 // Check if the network route is connected.
1112 if (!network_route.connected) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001113 RTC_LOG(LS_INFO) << "Transport " << transport_name << " is disconnected";
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001114 // TODO(honghaiz): Perhaps handle this in SignalChannelNetworkState and
1115 // consider merging these two methods.
1116 return;
1117 }
1118
1119 // Check whether the network route has changed on each transport.
1120 auto result =
1121 network_routes_.insert(std::make_pair(transport_name, network_route));
1122 auto kv = result.first;
1123 bool inserted = result.second;
1124 if (inserted) {
1125 // No need to reset BWE if this is the first time the network connects.
1126 return;
1127 }
1128 if (kv->second != network_route) {
1129 kv->second = network_route;
Mirko Bonadei675513b2017-11-09 11:09:25 +01001130 RTC_LOG(LS_INFO)
1131 << "Network route changed on transport " << transport_name
1132 << ": new local network id " << network_route.local_network_id
1133 << " new remote network id " << network_route.remote_network_id
1134 << " Reset bitrates to min: " << config_.bitrate_config.min_bitrate_bps
1135 << " bps, start: " << config_.bitrate_config.start_bitrate_bps
1136 << " bps, max: " << config_.bitrate_config.start_bitrate_bps
1137 << " bps.";
stefan5a2c5062017-01-27 06:43:18 -08001138 RTC_DCHECK_GT(config_.bitrate_config.start_bitrate_bps, 0);
Sebastian Janssone4be6da2018-02-15 16:51:41 +01001139 transport_send_->OnNetworkRouteChanged(
Stefan Holmer9ea46b52017-03-15 12:40:25 +01001140 network_route, config_.bitrate_config.start_bitrate_bps,
honghaiz059e1832016-06-24 11:03:55 -07001141 config_.bitrate_config.min_bitrate_bps,
1142 config_.bitrate_config.max_bitrate_bps);
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001143 }
1144}
1145
skvlad7a43d252016-03-22 15:32:27 -07001146void Call::UpdateAggregateNetworkState() {
eladalonf3f5c0e2017-08-18 02:47:08 -07001147 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001148
1149 bool have_audio = false;
1150 bool have_video = false;
1151 {
1152 ReadLockScoped read_lock(*send_crit_);
1153 if (audio_send_ssrcs_.size() > 0)
1154 have_audio = true;
1155 if (video_send_ssrcs_.size() > 0)
1156 have_video = true;
1157 }
1158 {
1159 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001160 if (audio_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001161 have_audio = true;
nissee4bcd6d2017-05-16 04:47:04 -07001162 if (video_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001163 have_video = true;
1164 }
1165
1166 NetworkState aggregate_state = kNetworkDown;
1167 if ((have_video && video_network_state_ == kNetworkUp) ||
1168 (have_audio && audio_network_state_ == kNetworkUp)) {
1169 aggregate_state = kNetworkUp;
1170 }
1171
Mirko Bonadei675513b2017-11-09 11:09:25 +01001172 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
1173 << (aggregate_state == kNetworkUp ? "up" : "down");
skvlad7a43d252016-03-22 15:32:27 -07001174
Sebastian Janssone4be6da2018-02-15 16:51:41 +01001175 transport_send_->OnNetworkAvailability(aggregate_state == kNetworkUp);
skvlad7a43d252016-03-22 15:32:27 -07001176}
1177
stefanc1aeaf02015-10-15 07:26:07 -07001178void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001179 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1180 clock_->TimeInMilliseconds());
Sebastian Janssone4be6da2018-02-15 16:51:41 +01001181 transport_send_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001182}
1183
minyue78b4d562016-11-30 04:47:39 -08001184void Call::OnNetworkChanged(uint32_t target_bitrate_bps,
1185 uint8_t fraction_loss,
1186 int64_t rtt_ms,
1187 int64_t probing_interval_ms) {
perkj26091b12016-09-01 01:17:40 -07001188 // TODO(perkj): Consider making sure CongestionController operates on
1189 // |worker_queue_|.
1190 if (!worker_queue_.IsCurrent()) {
minyue78b4d562016-11-30 04:47:39 -08001191 worker_queue_.PostTask(
1192 [this, target_bitrate_bps, fraction_loss, rtt_ms, probing_interval_ms] {
1193 OnNetworkChanged(target_bitrate_bps, fraction_loss, rtt_ms,
1194 probing_interval_ms);
1195 });
perkj26091b12016-09-01 01:17:40 -07001196 return;
1197 }
1198 RTC_DCHECK_RUN_ON(&worker_queue_);
nisse559af382017-03-21 06:41:12 -07001199 // For controlling the rate of feedback messages.
1200 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
perkj71ee44c2016-06-15 00:47:53 -07001201 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss,
minyue78b4d562016-11-30 04:47:39 -08001202 rtt_ms, probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001203
asaperssonce2e1362016-09-09 00:13:35 -07001204 // Ignore updates if bitrate is zero (the aggregate network state is down).
1205 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001206 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001207 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1208 pacer_bitrate_kbps_counter_.ProcessAndPause();
1209 return;
stefan18adf0a2015-11-17 06:24:56 -08001210 }
asaperssonce2e1362016-09-09 00:13:35 -07001211
1212 bool sending_video;
1213 {
1214 ReadLockScoped read_lock(*send_crit_);
1215 sending_video = !video_send_streams_.empty();
1216 }
1217
1218 rtc::CritScope lock(&bitrate_crit_);
1219 if (!sending_video) {
1220 // Do not update the stats if we are not sending video.
1221 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1222 pacer_bitrate_kbps_counter_.ProcessAndPause();
1223 return;
1224 }
1225 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1226 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1227 uint32_t pacer_bitrate_bps =
1228 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1229 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001230}
mflodman101f2502016-06-09 17:21:19 +02001231
perkj71ee44c2016-06-15 00:47:53 -07001232void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
1233 uint32_t max_padding_bitrate_bps) {
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001234 transport_send_->SetAllocatedSendBitrateLimits(min_send_bitrate_bps,
1235 max_padding_bitrate_bps);
perkj71ee44c2016-06-15 00:47:53 -07001236 rtc::CritScope lock(&bitrate_crit_);
1237 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001238 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001239}
1240
pbos8fc7fa72015-07-15 08:02:58 -07001241void Call::ConfigureSync(const std::string& sync_group) {
1242 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001243 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001244 return;
1245
1246 AudioReceiveStream* sync_audio_stream = nullptr;
1247 // Find existing audio stream.
1248 const auto it = sync_stream_mapping_.find(sync_group);
1249 if (it != sync_stream_mapping_.end()) {
1250 sync_audio_stream = it->second;
1251 } else {
1252 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001253 for (AudioReceiveStream* stream : audio_receive_streams_) {
1254 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001255 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001256 RTC_LOG(LS_WARNING)
1257 << "Attempting to sync more than one audio stream "
1258 "within the same sync group. This is not "
1259 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001260 break;
1261 }
nissee4bcd6d2017-05-16 04:47:04 -07001262 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001263 }
1264 }
1265 }
1266 if (sync_audio_stream)
1267 sync_stream_mapping_[sync_group] = sync_audio_stream;
1268 size_t num_synced_streams = 0;
1269 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1270 if (video_stream->config().sync_group != sync_group)
1271 continue;
1272 ++num_synced_streams;
1273 if (num_synced_streams > 1) {
1274 // TODO(pbos): Support synchronizing more than one A/V pair.
1275 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001276 RTC_LOG(LS_WARNING)
1277 << "Attempting to sync more than one audio/video pair "
1278 "within the same sync group. This is not supported in "
1279 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001280 }
1281 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001282 if (num_synced_streams == 1) {
1283 // sync_audio_stream may be null and that's ok.
1284 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001285 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001286 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001287 }
1288 }
1289}
1290
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001291PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1292 const uint8_t* packet,
1293 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001294 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001295 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001296 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1297 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001298 if (received_bytes_per_second_counter_.HasSample()) {
1299 // First RTP packet has been received.
1300 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1301 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1302 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001303 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001304 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001305 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001306 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001307 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001308 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001309 }
1310 }
1311 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1312 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001313 for (AudioReceiveStream* stream : audio_receive_streams_) {
1314 if (stream->DeliverRtcp(packet, length))
mflodman3d7db262016-04-29 00:57:13 -07001315 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001316 }
1317 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001318 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001319 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001320 for (VideoSendStream* stream : video_send_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001321 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001322 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001323 }
1324 }
mflodman3d7db262016-04-29 00:57:13 -07001325 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1326 ReadLockScoped read_lock(*send_crit_);
1327 for (auto& kv : audio_send_ssrcs_) {
1328 if (kv.second->DeliverRtcp(packet, length))
1329 rtcp_delivered = true;
1330 }
1331 }
1332
Elad Alon4a87e1c2017-10-03 16:11:34 +02001333 if (rtcp_delivered) {
1334 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketIncoming>(
1335 rtc::MakeArrayView(packet, length)));
1336 }
mflodman3d7db262016-04-29 00:57:13 -07001337
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001338 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001339}
1340
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001341PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001342 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -07001343 const PacketTime& packet_time) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001344 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001345
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001346 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001347 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001348 return DELIVERY_PACKET_ERROR;
1349
1350 if (packet_time.timestamp != -1) {
1351 parsed_packet.set_arrival_time_ms((packet_time.timestamp + 500) / 1000);
1352 } else {
1353 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1354 }
nissed44ce052017-02-06 02:23:00 -08001355
sprangc1abde72017-07-11 03:56:21 -07001356 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1357 // These are empty (zero length payload) RTP packets with an unsignaled
1358 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001359 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001360
1361 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1362 is_keep_alive_packet);
1363
sprangc1abde72017-07-11 03:56:21 -07001364 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001365 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001366 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001367 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1368 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001369 // Destruction of the receive stream, including deregistering from the
1370 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1371 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1372 // So by not passing the packet on to demuxing in this case, we prevent
1373 // incoming packets to be passed on via the demuxer to a receive stream
1374 // which is being torned down.
1375 return DELIVERY_UNKNOWN_SSRC;
1376 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001377 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001378
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001379 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001380
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001381 // RateCounters expect input parameter as int, save it as int,
1382 // instead of converting each time it is passed to RateCounter::Add below.
1383 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001384 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001385 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001386 received_bytes_per_second_counter_.Add(length);
1387 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001388 event_log_->Log(
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001389 rtc::MakeUnique<RtcEventRtpPacketIncoming>(parsed_packet));
1390 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001391 if (!first_received_rtp_audio_ms_) {
1392 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1393 }
1394 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001395 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001396 }
nissee4bcd6d2017-05-16 04:47:04 -07001397 } else if (media_type == MediaType::VIDEO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001398 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001399 received_bytes_per_second_counter_.Add(length);
1400 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001401 event_log_->Log(
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001402 rtc::MakeUnique<RtcEventRtpPacketIncoming>(parsed_packet));
1403 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001404 if (!first_received_rtp_video_ms_) {
1405 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1406 }
1407 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001408 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001409 }
1410 }
1411 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001412}
1413
stefan68786d22015-09-08 05:36:15 -07001414PacketReceiver::DeliveryStatus Call::DeliverPacket(
1415 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001416 rtc::CopyOnWriteBuffer packet,
stefan68786d22015-09-08 05:36:15 -07001417 const PacketTime& packet_time) {
eladalond1dd2f72017-08-25 02:55:57 -07001418 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001419 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1420 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001421
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001422 return DeliverRtp(media_type, std::move(packet), packet_time);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001423}
1424
nissed2ef3142017-05-11 08:00:58 -07001425void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001426 RtpPacketReceived parsed_packet;
1427 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001428 return;
1429
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001430 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001431
brandtrcaea68f2017-08-23 00:55:17 -07001432 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001433 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001434 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001435 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1436 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001437 // Destruction of the receive stream, including deregistering from the
1438 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1439 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1440 // So by not passing the packet on to demuxing in this case, we prevent
1441 // incoming packets to be passed on via the demuxer to a receive stream
1442 // which is being torned down.
1443 return;
1444 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001445 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001446
1447 // TODO(brandtr): Update here when we support protecting audio packets too.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001448 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001449}
1450
nissed44ce052017-02-06 02:23:00 -08001451void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1452 MediaType media_type) {
1453 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001454 bool use_send_side_bwe =
1455 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001456
brandtrb29e6522016-12-21 06:37:18 -08001457 RTPHeader header;
1458 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001459
nisse4709e892017-02-07 01:18:43 -08001460 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001461 // Inconsistent configuration of send side BWE. Do nothing.
1462 // TODO(nisse): Without this check, we may produce RTCP feedback
1463 // packets even when not negotiated. But it would be cleaner to
1464 // move the check down to RTCPSender::SendFeedbackPacket, which
1465 // would also help the PacketRouter to select an appropriate rtp
1466 // module in the case that some, but not all, have RTCP feedback
1467 // enabled.
1468 return;
1469 }
1470 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001471 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001472 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001473 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001474 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1475 header);
1476 }
brandtrb29e6522016-12-21 06:37:18 -08001477}
1478
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001479} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001480
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001481} // namespace webrtc