blob: 1ecaa613e96106e29df4b6ef3c93ad46e161d6e3 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <string.h>
mflodman101f2502016-06-09 17:21:19 +020012#include <algorithm>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013#include <map>
kwibergb25345e2016-03-12 06:10:44 -080014#include <memory>
ossuf515ab82016-12-07 04:52:58 -080015#include <set>
brandtr25445d32016-10-23 23:37:14 -070016#include <utility>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000017#include <vector>
18
Karl Wiberg918f50c2018-07-05 11:40:33 +020019#include "absl/memory/memory.h"
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020020#include "absl/types/optional.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020021#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "audio/audio_receive_stream.h"
23#include "audio/audio_send_stream.h"
24#include "audio/audio_state.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "audio/time_interval.h"
26#include "call/bitrate_allocator.h"
27#include "call/call.h"
28#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010029#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "call/rtp_stream_receiver_controller.h"
31#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020032#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
33#include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h"
34#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
35#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
36#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
37#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon99a81b62017-09-21 10:25:29 +020039#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "modules/bitrate_controller/include/bitrate_controller.h"
41#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
42#include "modules/rtp_rtcp/include/flexfec_receiver.h"
43#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
44#include "modules/rtp_rtcp/include/rtp_header_parser.h"
45#include "modules/rtp_rtcp/source/byte_io.h"
46#include "modules/rtp_rtcp/source/rtp_packet_received.h"
47#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010048#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "rtc_base/checks.h"
50#include "rtc_base/constructormagic.h"
51#include "rtc_base/location.h"
52#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 15:59:12 +010053#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/sequenced_task_checker.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020055#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020056#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "rtc_base/task_queue.h"
58#include "rtc_base/thread_annotations.h"
Christoffer Rodbro76ad1542018-10-12 11:15:09 +020059#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/trace_event.h"
61#include "system_wrappers/include/clock.h"
62#include "system_wrappers/include/cpu_info.h"
63#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#include "video/call_stats.h"
65#include "video/send_delay_stats.h"
66#include "video/stats_counter.h"
67#include "video/video_receive_stream.h"
68#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000069
70namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000071
nisse4709e892017-02-07 01:18:43 -080072namespace {
nisse4709e892017-02-07 01:18:43 -080073// TODO(nisse): This really begs for a shared context struct.
74bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
75 bool transport_cc) {
76 if (!transport_cc)
77 return false;
78 for (const auto& extension : extensions) {
79 if (extension.uri == RtpExtension::kTransportSequenceNumberUri)
80 return true;
81 }
82 return false;
83}
84
85bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
86 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
87}
88
89bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
90 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
91}
92
93bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
94 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
95}
96
nisse26e3abb2017-08-25 04:44:25 -070097const int* FindKeyByValue(const std::map<int, int>& m, int v) {
98 for (const auto& kv : m) {
99 if (kv.second == v)
100 return &kv.first;
101 }
102 return nullptr;
103}
104
eladalon8ec568a2017-09-08 06:15:52 -0700105std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700106 const VideoReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200107 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700108 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
109 rtclog_config->local_ssrc = config.rtp.local_ssrc;
110 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
111 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
112 rtclog_config->remb = config.rtp.remb;
113 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700114
115 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700116 const int* search =
117 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200118 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200119 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700120 }
121 return rtclog_config;
122}
123
eladalon8ec568a2017-09-08 06:15:52 -0700124std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700125 const VideoSendStream::Config& config,
126 size_t ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200127 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700128 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700129 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700130 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700131 }
eladalon8ec568a2017-09-08 06:15:52 -0700132 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
133 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700134
Niels Möller259a4972018-04-05 15:36:51 +0200135 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
136 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700137 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700138 return rtclog_config;
139}
140
eladalon8ec568a2017-09-08 06:15:52 -0700141std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700142 const AudioReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200143 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700144 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
145 rtclog_config->local_ssrc = config.rtp.local_ssrc;
146 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700147 return rtclog_config;
148}
149
eladalon8ec568a2017-09-08 06:15:52 -0700150std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjf4726992017-05-22 10:12:26 -0700151 const AudioSendStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200152 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700153 rtclog_config->local_ssrc = config.rtp.ssrc;
154 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjf4726992017-05-22 10:12:26 -0700155 if (config.send_codec_spec) {
eladalon8ec568a2017-09-08 06:15:52 -0700156 rtclog_config->codecs.emplace_back(config.send_codec_spec->format.name,
157 config.send_codec_spec->payload_type, 0);
perkjf4726992017-05-22 10:12:26 -0700158 }
159 return rtclog_config;
160}
161
nisse4709e892017-02-07 01:18:43 -0800162} // namespace
163
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000164namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000165
Sebastian Janssone6256052018-05-04 14:08:15 +0200166class Call final : public webrtc::Call,
167 public PacketReceiver,
168 public RecoveredPacketReceiver,
169 public TargetTransferRateObserver,
170 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000171 public:
nisseb8f9a322017-03-27 05:36:15 -0700172 Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700173 std::unique_ptr<RtpTransportControllerSendInterface> transport_send);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200174 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000175
brandtr25445d32016-10-23 23:37:14 -0700176 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000177 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000178
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200179 webrtc::AudioSendStream* CreateAudioSendStream(
180 const webrtc::AudioSendStream::Config& config) override;
181 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
182
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200183 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
184 const webrtc::AudioReceiveStream::Config& config) override;
185 void DestroyAudioReceiveStream(
186 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000187
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200188 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700189 webrtc::VideoSendStream::Config config,
190 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100191 webrtc::VideoSendStream* CreateVideoSendStream(
192 webrtc::VideoSendStream::Config config,
193 VideoEncoderConfig encoder_config,
194 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000195 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000196
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200197 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200198 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000199 void DestroyVideoReceiveStream(
200 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000201
brandtr7250b392016-12-19 01:13:46 -0800202 FlexfecReceiveStream* CreateFlexfecReceiveStream(
203 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700204 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800205 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700206
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100207 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
208
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000209 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000210
brandtr25445d32016-10-23 23:37:14 -0700211 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700212 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100213 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200214 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000215
brandtr4e523862016-10-18 23:50:45 -0700216 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700217 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700218
Alex Narest78609d52017-10-20 10:37:47 +0200219 void SetBitrateAllocationStrategy(
220 std::unique_ptr<rtc::BitrateAllocationStrategy>
221 bitrate_allocation_strategy) override;
222
skvlad7a43d252016-03-22 15:32:27 -0700223 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000224
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200225 void OnAudioTransportOverheadChanged(
226 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800227
stefanc1aeaf02015-10-15 07:26:07 -0700228 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
229
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100230 // Implements TargetTransferRateObserver,
231 void OnTargetTransferRate(TargetTransferRate msg) override;
mflodman0e7e2592015-11-12 21:02:42 -0800232
perkj71ee44c2016-06-15 00:47:53 -0700233 // Implements BitrateAllocator::LimitObserver.
234 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +0100235 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +0100236 uint32_t total_bitrate_bps,
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200237 uint32_t allocated_without_feedback_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +0100238 bool has_packet_feedback) override;
perkj71ee44c2016-06-15 00:47:53 -0700239
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000240 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200241 DeliveryStatus DeliverRtcp(MediaType media_type,
242 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200243 size_t length);
stefan68786d22015-09-08 05:36:15 -0700244 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100245 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200246 int64_t packet_time_us);
pbos8fc7fa72015-07-15 08:02:58 -0700247 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700248 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700249
nissed44ce052017-02-06 02:23:00 -0800250 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
251 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700252 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800253
asaperssonfc5e81c2017-04-19 23:28:53 -0700254 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700255 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800256 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700257 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700258 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800259
Peter Boströmd3c94472015-12-09 11:20:58 +0100260 Clock* const clock_;
stefan91d92602015-11-11 10:13:02 -0800261
Peter Boström45553ae2015-05-08 13:54:38 +0200262 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800263 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800264 const std::unique_ptr<CallStats> call_stats_;
265 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000266 Call::Config config_;
eladalonf3f5c0e2017-08-18 02:47:08 -0700267 rtc::SequencedTaskChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000268
skvlad7a43d252016-03-22 15:32:27 -0700269 NetworkState audio_network_state_;
270 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100271 rtc::CriticalSection aggregate_network_up_crit_;
272 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000273
kwibergb25345e2016-03-12 06:10:44 -0800274 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700275 // Audio, Video, and FlexFEC receive streams are owned by the client that
276 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700277 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700278 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200279 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700280 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700281
pbos8fc7fa72015-07-15 08:02:58 -0700282 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700283 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000284
nisse0f15f922017-06-21 01:05:22 -0700285 // TODO(nisse): Should eventually be injected at creation,
286 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700287 RtpStreamReceiverController audio_receiver_controller_;
288 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700289
nissed44ce052017-02-06 02:23:00 -0800290 // This extra map is used for receive processing which is
291 // independent of media type.
292
293 // TODO(nisse): In the RTP transport refactoring, we should have a
294 // single mapping from ssrc to a more abstract receive stream, with
295 // accessor methods for all configuration we need at this level.
296 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100297 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
298 : extensions(config.rtp.extensions),
299 use_send_side_bwe(UseSendSideBwe(config)) {}
300 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
301 : extensions(config.rtp.extensions),
302 use_send_side_bwe(UseSendSideBwe(config)) {}
303 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
304 : extensions(config.rtp_header_extensions),
305 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800306
307 // Registered RTP header extensions for each stream. Note that RTP header
308 // extensions are negotiated per track ("m= line") in the SDP, but we have
309 // no notion of tracks at the Call level. We therefore store the RTP header
310 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100311 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800312 // Set if both RTP extension the RTCP feedback message needed for
313 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100314 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800315 };
316 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700317 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800318
kwibergb25345e2016-03-12 06:10:44 -0800319 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700320 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700321 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
322 RTC_GUARDED_BY(send_crit_);
323 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
324 RTC_GUARDED_BY(send_crit_);
325 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000326
ossuc3d4b482017-05-23 06:07:11 -0700327 using RtpStateMap = std::map<uint32_t, RtpState>;
328 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700329 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700330 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700331 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700332
Åsa Persson4bece9a2017-10-06 10:04:04 +0200333 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
334 RtpPayloadStateMap suspended_video_payload_states_
335 RTC_GUARDED_BY(configuration_sequence_checker_);
336
skvlad11a9cbf2016-10-07 11:53:05 -0700337 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700338
stefan18adf0a2015-11-17 06:24:56 -0800339 // The following members are only accessed (exclusively) from one thread and
340 // from the destructor, and therefore doesn't need any explicit
341 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700342 RateCounter received_bytes_per_second_counter_;
343 RateCounter received_audio_bytes_per_second_counter_;
344 RateCounter received_video_bytes_per_second_counter_;
345 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200346 absl::optional<int64_t> first_received_rtp_audio_ms_;
347 absl::optional<int64_t> last_received_rtp_audio_ms_;
348 absl::optional<int64_t> first_received_rtp_video_ms_;
349 absl::optional<int64_t> last_received_rtp_video_ms_;
sazac58f8c02017-07-19 00:39:19 -0700350 TimeInterval sent_rtp_audio_timer_ms_;
stefan91d92602015-11-11 10:13:02 -0800351
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100352 rtc::CriticalSection last_bandwidth_bps_crit_;
353 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
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
nisse559af382017-03-21 06:41:12 -0700363 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100364
365 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
366
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_;
mflodman0e7e2592015-11-12 21:02:42 -0800369
Sebastian Janssone6256052018-05-04 14:08:15 +0200370 // Caches transport_send_.get(), to avoid racing with destructor.
371 // Note that this is declared before transport_send_ to ensure that it is not
372 // invalidated until no more tasks can be running on the transport_send_ task
373 // queue.
374 RtpTransportControllerSendInterface* transport_send_ptr_;
375 // Declared last since it will issue callbacks from a task queue. Declaring it
376 // last ensures that it is destroyed first and any running tasks are finished.
377 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
henrikg3c089d72015-09-16 05:37:44 -0700378 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000379};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000380} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000381
asapersson2e5cfcd2016-08-11 08:41:18 -0700382std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200383 char buf[1024];
384 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700385 ss << "Call stats: " << time_ms << ", {";
386 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
387 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
388 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
389 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
390 ss << "rtt_ms: " << rtt_ms;
391 ss << '}';
392 return ss.str();
393}
394
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000395Call* Call::Create(const Call::Config& config) {
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100396 return new internal::Call(
Karl Wiberg918f50c2018-07-05 11:40:33 +0200397 config, absl::make_unique<RtpTransportControllerSend>(
Sebastian Janssondfce03a2018-05-18 18:05:10 +0200398 Clock::GetRealTimeClock(), config.event_log,
399 config.network_controller_factory, config.bitrate_config));
zstein7cb69d52017-05-08 11:52:38 -0700400}
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
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100408// This method here to avoid subclasses has to implement this method.
409// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
410// FecController.
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")),
Tommi38c5d932018-03-27 23:11:09 +0200425 call_stats_(new CallStats(clock_, module_process_thread_.get())),
perkj71ee44c2016-06-15 00:47:53 -0700426 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200427 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800428 audio_network_state_(kNetworkDown),
429 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100430 aggregate_network_up_(false),
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),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100438 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700439 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700440 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700441 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
442 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700443 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100444 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700445 video_send_delay_stats_(new SendDelayStats(clock_)),
Sebastian Janssone6256052018-05-04 14:08:15 +0200446 start_ms_(clock_->TimeInMilliseconds()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700447 RTC_DCHECK(config.event_log != nullptr);
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100448 transport_send->RegisterTargetTransferRateObserver(this);
nisse6167b262017-04-06 06:34:25 -0700449 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 14:08:15 +0200450 transport_send_ptr_ = transport_send_.get();
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100451
nissebcbaf742017-03-28 01:16:25 -0700452 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100453 call_stats_->RegisterStatsObserver(transport_send_->GetCallStatsObserver());
Stefan Holmerc379fcb2016-02-24 16:02:55 +0100454
Sebastian Janssonc33c0fc2018-02-22 11:10:18 +0100455 module_process_thread_->RegisterModule(
stefan64136af2017-08-14 08:03:17 -0700456 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
stefan9e117c5e12017-08-16 08:16:25 -0700457 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
458 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
stefan9e117c5e12017-08-16 08:16:25 -0700459 module_process_thread_->Start();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000460}
461
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000462Call::~Call() {
eladalonf3f5c0e2017-08-18 02:47:08 -0700463 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700464
solenbergc7a8b082015-10-16 14:35:07 -0700465 RTC_CHECK(audio_send_ssrcs_.empty());
466 RTC_CHECK(video_send_ssrcs_.empty());
467 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700468 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700469 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000470
Sebastian Janssonc33c0fc2018-02-22 11:10:18 +0100471 module_process_thread_->DeRegisterModule(
nisse559af382017-03-21 06:41:12 -0700472 receive_side_cc_.GetRemoteBitrateEstimator(true));
nisse559af382017-03-21 06:41:12 -0700473 module_process_thread_->DeRegisterModule(&receive_side_cc_);
mflodmane3787022015-10-21 13:24:28 +0200474 module_process_thread_->DeRegisterModule(call_stats_.get());
Peter Boström45553ae2015-05-08 13:54:38 +0200475 module_process_thread_->Stop();
nissebcbaf742017-03-28 01:16:25 -0700476 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100477 call_stats_->DeregisterStatsObserver(transport_send_->GetCallStatsObserver());
sprang6d6122b2016-07-13 06:37:09 -0700478
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100479 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700480 // Only update histograms after process threads have been shut down, so that
481 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700482 {
483 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700484 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700485 }
sprang6d6122b2016-07-13 06:37:09 -0700486 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700487 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000488}
489
asapersson4374a092016-07-27 00:39:09 -0700490void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700491 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700492 "WebRTC.Call.LifetimeInSeconds",
493 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
494}
495
asaperssonfc5e81c2017-04-19 23:28:53 -0700496void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
497 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800498 return;
sazac58f8c02017-07-19 00:39:19 -0700499 if (!sent_rtp_audio_timer_ms_.Empty()) {
500 RTC_HISTOGRAM_COUNTS_100000(
501 "WebRTC.Call.TimeSendingAudioRtpPacketsInSeconds",
502 sent_rtp_audio_timer_ms_.Length() / 1000);
503 }
stefan18adf0a2015-11-17 06:24:56 -0800504 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700505 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800506 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
507 return;
asaperssonce2e1362016-09-09 00:13:35 -0700508 const int kMinRequiredPeriodicSamples = 5;
509 AggregatedStats send_bitrate_stats =
510 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
511 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700512 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
513 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100514 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
515 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800516 }
asaperssonce2e1362016-09-09 00:13:35 -0700517 AggregatedStats pacer_bitrate_stats =
518 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
519 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700520 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
521 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100522 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
523 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800524 }
525}
526
527void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700528 if (first_received_rtp_audio_ms_) {
529 RTC_HISTOGRAM_COUNTS_100000(
530 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
531 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
532 }
533 if (first_received_rtp_video_ms_) {
534 RTC_HISTOGRAM_COUNTS_100000(
535 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
536 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
537 }
asapersson250fd972016-09-08 00:07:21 -0700538 const int kMinRequiredPeriodicSamples = 5;
539 AggregatedStats video_bytes_per_sec =
540 received_video_bytes_per_second_counter_.GetStats();
541 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700542 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
543 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100544 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
545 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800546 }
asapersson250fd972016-09-08 00:07:21 -0700547 AggregatedStats audio_bytes_per_sec =
548 received_audio_bytes_per_second_counter_.GetStats();
549 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700550 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
551 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100552 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
553 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800554 }
asapersson250fd972016-09-08 00:07:21 -0700555 AggregatedStats rtcp_bytes_per_sec =
556 received_rtcp_bytes_per_second_counter_.GetStats();
557 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700558 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
559 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100560 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
561 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800562 }
asapersson250fd972016-09-08 00:07:21 -0700563 AggregatedStats recv_bytes_per_sec =
564 received_bytes_per_second_counter_.GetStats();
565 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700566 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
567 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100568 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
569 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700570 }
stefan91d92602015-11-11 10:13:02 -0800571}
572
solenberg5a289392015-10-19 03:39:20 -0700573PacketReceiver* Call::Receiver() {
eladalond1dd2f72017-08-25 02:55:57 -0700574 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700575 return this;
576}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000577
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200578webrtc::AudioSendStream* Call::CreateAudioSendStream(
579 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700580 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700581 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200582 event_log_->Log(absl::make_unique<RtcEventAudioSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200583 CreateRtcLogStreamConfig(config)));
ossuc3d4b482017-05-23 06:07:11 -0700584
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200585 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700586 {
587 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
588 if (iter != suspended_audio_send_ssrcs_.end()) {
589 suspended_rtp_state.emplace(iter->second);
590 }
591 }
592
Sebastian Janssone6256052018-05-04 14:08:15 +0200593 // TODO(srte): AudioSendStream should call GetWorkerQueue directly rather than
594 // having it injected.
595
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100596 AudioSendStream* send_stream = new AudioSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200597 config, config_.audio_state, transport_send_ptr_->GetWorkerQueue(),
598 module_process_thread_.get(), transport_send_ptr_,
599 bitrate_allocator_.get(), event_log_, call_stats_.get(),
600 suspended_rtp_state, &sent_rtp_audio_timer_ms_);
solenbergc7a8b082015-10-16 14:35:07 -0700601 {
solenbergc7a8b082015-10-16 14:35:07 -0700602 WriteLockScoped write_lock(*send_crit_);
603 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
604 audio_send_ssrcs_.end());
605 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700606 }
solenberg7602aab2016-11-14 11:30:07 -0800607 {
608 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700609 for (AudioReceiveStream* stream : audio_receive_streams_) {
610 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
611 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800612 }
613 }
614 }
skvlad7a43d252016-03-22 15:32:27 -0700615 send_stream->SignalNetworkState(audio_network_state_);
616 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700617 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200618}
619
620void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700621 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700622 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700623 RTC_DCHECK(send_stream != nullptr);
624
625 send_stream->Stop();
626
eladalonabbc4302017-07-26 02:09:44 -0700627 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700628 webrtc::internal::AudioSendStream* audio_send_stream =
629 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700630 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700631 {
632 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800633 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
634 RTC_DCHECK_EQ(1, num_deleted);
635 }
636 {
637 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700638 for (AudioReceiveStream* stream : audio_receive_streams_) {
639 if (stream->config().rtp.local_ssrc == ssrc) {
640 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800641 }
642 }
solenbergc7a8b082015-10-16 14:35:07 -0700643 }
skvlad7a43d252016-03-22 15:32:27 -0700644 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700645 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200646}
647
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200648webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
649 const webrtc::AudioReceiveStream::Config& config) {
650 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700651 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200652 event_log_->Log(absl::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200653 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700654 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200655 &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100656 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200657 {
658 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100659 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
660 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700661 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800662
pbos8fc7fa72015-07-15 08:02:58 -0700663 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200664 }
solenberg7602aab2016-11-14 11:30:07 -0800665 {
666 ReadLockScoped read_lock(*send_crit_);
667 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
668 if (it != audio_send_ssrcs_.end()) {
669 receive_stream->AssociateSendStream(it->second);
670 }
671 }
skvlad7a43d252016-03-22 15:32:27 -0700672 receive_stream->SignalNetworkState(audio_network_state_);
673 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200674 return receive_stream;
675}
676
677void Call::DestroyAudioReceiveStream(
678 webrtc::AudioReceiveStream* receive_stream) {
679 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700680 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700681 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700682 webrtc::internal::AudioReceiveStream* audio_receive_stream =
683 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200684 {
685 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800686 const AudioReceiveStream::Config& config = audio_receive_stream->config();
687 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700688 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800689 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700690 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700691 const std::string& sync_group = audio_receive_stream->config().sync_group;
692 const auto it = sync_stream_mapping_.find(sync_group);
693 if (it != sync_stream_mapping_.end() &&
694 it->second == audio_receive_stream) {
695 sync_stream_mapping_.erase(it);
696 ConfigureSync(sync_group);
697 }
nissed44ce052017-02-06 02:23:00 -0800698 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200699 }
skvlad7a43d252016-03-22 15:32:27 -0700700 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200701 delete audio_receive_stream;
702}
703
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100704// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100705webrtc::VideoSendStream* Call::CreateVideoSendStream(
706 webrtc::VideoSendStream::Config config,
707 VideoEncoderConfig encoder_config,
708 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000709 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700710 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000711
asapersson35151f32016-05-02 23:44:01 -0700712 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700713 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
714 ++ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200715 event_log_->Log(absl::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200716 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700717 }
perkj26091b12016-09-01 01:17:40 -0700718
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000719 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
720 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700721 // Copy ssrcs from |config| since |config| is moved.
722 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100723
Sebastian Janssone6256052018-05-04 14:08:15 +0200724 // TODO(srte): VideoSendStream should call GetWorkerQueue directly rather than
725 // having it injected.
mflodman0c478b32015-10-21 15:52:16 +0200726 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200727 num_cpu_cores_, module_process_thread_.get(),
728 transport_send_ptr_->GetWorkerQueue(), call_stats_.get(),
729 transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700730 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200731 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200732 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700733
skvlad7a43d252016-03-22 15:32:27 -0700734 {
735 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700736 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700737 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
738 video_send_ssrcs_[ssrc] = send_stream;
739 }
740 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000741 }
skvlad7a43d252016-03-22 15:32:27 -0700742 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700743
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000744 return send_stream;
745}
746
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100747webrtc::VideoSendStream* Call::CreateVideoSendStream(
748 webrtc::VideoSendStream::Config config,
749 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100750 if (config_.fec_controller_factory) {
751 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
752 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100753 std::unique_ptr<FecController> fec_controller =
754 config_.fec_controller_factory
755 ? config_.fec_controller_factory->CreateFecController()
Karl Wiberg918f50c2018-07-05 11:40:33 +0200756 : absl::make_unique<FecControllerDefault>(Clock::GetRealTimeClock());
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100757 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
758 std::move(fec_controller));
759}
760
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000761void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000762 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700763 RTC_DCHECK(send_stream != nullptr);
eladalonf3f5c0e2017-08-18 02:47:08 -0700764 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000765
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000766 send_stream->Stop();
767
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000768 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000769 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000770 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200771 auto it = video_send_ssrcs_.begin();
772 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000773 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
774 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200775 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000776 } else {
777 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000778 }
779 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200780 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000781 }
henrikg91d6ede2015-09-17 00:24:34 -0700782 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000783
Åsa Persson4bece9a2017-10-06 10:04:04 +0200784 VideoSendStream::RtpStateMap rtp_states;
785 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
786 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
787 &rtp_payload_states);
788 for (const auto& kv : rtp_states) {
789 suspended_video_send_ssrcs_[kv.first] = kv.second;
790 }
791 for (const auto& kv : rtp_payload_states) {
792 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000793 }
794
skvlad7a43d252016-03-22 15:32:27 -0700795 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000796 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000797}
798
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200799webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200800 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000801 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700802 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800803
nisse0f15f922017-06-21 01:05:22 -0700804 VideoReceiveStream* receive_stream = new VideoReceiveStream(
eladalon2a2b2972017-07-03 09:25:27 -0700805 &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200806 transport_send_ptr_->packet_router(), std::move(configuration),
nisse0f15f922017-06-21 01:05:22 -0700807 module_process_thread_.get(), call_stats_.get());
Tommi733b5472016-06-10 17:58:01 +0200808
809 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700810 {
811 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800812 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800813 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700814 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800815 // type, we may get an incorrect value for the rtx stream, but
816 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100817 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
818 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800819 }
Erik Språng09708512018-03-14 15:16:50 +0100820 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
821 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700822 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700823 ConfigureSync(config.sync_group);
824 }
825 receive_stream->SignalNetworkState(video_network_state_);
826 UpdateAggregateNetworkState();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200827 event_log_->Log(absl::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200828 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000829 return receive_stream;
830}
831
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000832void Call::DestroyVideoReceiveStream(
833 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000834 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700835 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700836 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700837 VideoReceiveStream* receive_stream_impl =
838 static_cast<VideoReceiveStream*>(receive_stream);
839 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000840 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000841 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000842 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
843 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700844 receive_rtp_config_.erase(config.rtp.remote_ssrc);
845 if (config.rtp.rtx_ssrc) {
846 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000847 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200848 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700849 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000850 }
nisse4709e892017-02-07 01:18:43 -0800851
nisse559af382017-03-21 06:41:12 -0700852 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800853 ->RemoveStream(config.rtp.remote_ssrc);
854
skvlad7a43d252016-03-22 15:32:27 -0700855 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000856 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000857}
858
brandtr7250b392016-12-19 01:13:46 -0800859FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
860 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700861 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700862 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800863
864 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700865
nisse0f15f922017-06-21 01:05:22 -0700866 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700867 {
868 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -0700869 // Unlike the video and audio receive streams,
870 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
871 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -0700872 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -0700873 // constructor while holding |receive_crit_| ensures that we don't
874 // call OnRtpPacket until the constructor is finished and the
875 // object is in a valid state.
876 // TODO(nisse): Fix constructor so that it can be moved outside of
877 // this locked scope.
878 receive_stream = new FlexfecReceiveStreamImpl(
eladalon2a2b2972017-07-03 09:25:27 -0700879 &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +0200880 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -0800881
nissed44ce052017-02-06 02:23:00 -0800882 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
883 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +0100884 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -0700885 }
brandtrb29e6522016-12-21 06:37:18 -0800886
brandtr25445d32016-10-23 23:37:14 -0700887 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -0800888
brandtr25445d32016-10-23 23:37:14 -0700889 return receive_stream;
890}
891
brandtr7250b392016-12-19 01:13:46 -0800892void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -0700893 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700894 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800895
brandtr25445d32016-10-23 23:37:14 -0700896 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -0700897 {
898 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800899
eladalon42f44f92017-07-25 06:40:06 -0700900 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -0800901 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -0800902 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -0800903
brandtr7250b392016-12-19 01:13:46 -0800904 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
905 // destroyed.
nisse559af382017-03-21 06:41:12 -0700906 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800907 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -0700908 }
brandtrb29e6522016-12-21 06:37:18 -0800909
eladalon42f44f92017-07-25 06:40:06 -0700910 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700911}
912
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100913RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +0200914 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100915}
916
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000917Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700918 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
919 // thread. Re-enable once that is fixed.
eladalonf3f5c0e2017-08-18 02:47:08 -0700920 // RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000921 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200922 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +0200923 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000924 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -0700925 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700926 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100927
928 {
929 rtc::CritScope cs(&last_bandwidth_bps_crit_);
930 stats.send_bandwidth_bps = last_bandwidth_bps_;
931 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000932 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100933 // TODO(srte): It is unclear if we only want to report queues if network is
934 // available.
935 {
936 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 14:08:15 +0200937 stats.pacer_delay_ms = aggregate_network_up_
938 ? transport_send_ptr_->GetPacerQueuingDelayMs()
939 : 0;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100940 }
941
Tommi38c5d932018-03-27 23:11:09 +0200942 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -0700943 {
944 rtc::CritScope cs(&bitrate_crit_);
945 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
946 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000947 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000948}
949
Alex Narest78609d52017-10-20 10:37:47 +0200950void Call::SetBitrateAllocationStrategy(
951 std::unique_ptr<rtc::BitrateAllocationStrategy>
952 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 14:08:15 +0200953 // TODO(srte): This function should be moved to RtpTransportControllerSend
954 // when BitrateAllocator is moved there.
955 struct Functor {
956 void operator()() {
957 bitrate_allocator_->SetBitrateAllocationStrategy(
958 std::move(bitrate_allocation_strategy_));
959 }
960 BitrateAllocator* bitrate_allocator_;
961 std::unique_ptr<rtc::BitrateAllocationStrategy>
962 bitrate_allocation_strategy_;
963 };
964 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
965 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 10:37:47 +0200966}
967
skvlad7a43d252016-03-22 15:32:27 -0700968void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
eladalonf3f5c0e2017-08-18 02:47:08 -0700969 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -0700970 switch (media) {
971 case MediaType::AUDIO:
972 audio_network_state_ = state;
973 break;
974 case MediaType::VIDEO:
975 video_network_state_ = state;
976 break;
977 case MediaType::ANY:
978 case MediaType::DATA:
979 RTC_NOTREACHED();
980 break;
981 }
982
983 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000984 {
skvlad7a43d252016-03-22 15:32:27 -0700985 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -0700986 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -0700987 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -0700988 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000989 }
990 {
skvlad7a43d252016-03-22 15:32:27 -0700991 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700992 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
993 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -0700994 }
nissee4bcd6d2017-05-16 04:47:04 -0700995 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
996 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000997 }
998 }
999}
1000
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001001void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1002 ReadLockScoped read_lock(*send_crit_);
1003 for (auto& kv : audio_send_ssrcs_) {
1004 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -08001005 }
1006}
1007
skvlad7a43d252016-03-22 15:32:27 -07001008void Call::UpdateAggregateNetworkState() {
eladalonf3f5c0e2017-08-18 02:47:08 -07001009 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001010
1011 bool have_audio = false;
1012 bool have_video = false;
1013 {
1014 ReadLockScoped read_lock(*send_crit_);
1015 if (audio_send_ssrcs_.size() > 0)
1016 have_audio = true;
1017 if (video_send_ssrcs_.size() > 0)
1018 have_video = true;
1019 }
1020 {
1021 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001022 if (audio_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001023 have_audio = true;
nissee4bcd6d2017-05-16 04:47:04 -07001024 if (video_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001025 have_video = true;
1026 }
1027
Sebastian Janssona06e9192018-03-07 18:49:55 +01001028 bool aggregate_network_up =
1029 ((have_video && video_network_state_ == kNetworkUp) ||
1030 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001031
Mirko Bonadei675513b2017-11-09 11:09:25 +01001032 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001033 << (aggregate_network_up ? "up" : "down");
1034 {
1035 rtc::CritScope cs(&aggregate_network_up_crit_);
1036 aggregate_network_up_ = aggregate_network_up;
1037 }
Sebastian Janssone6256052018-05-04 14:08:15 +02001038 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001039}
1040
stefanc1aeaf02015-10-15 07:26:07 -07001041void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001042 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1043 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001044 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001045}
1046
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001047void Call::OnTargetTransferRate(TargetTransferRate msg) {
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001048 uint32_t target_bitrate_bps = msg.target_rate.bps();
1049 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1050 uint8_t fraction_loss =
1051 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1052 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1053 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1054 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1055 {
1056 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1057 last_bandwidth_bps_ = bandwidth_bps;
1058 }
nisse559af382017-03-21 06:41:12 -07001059 // For controlling the rate of feedback messages.
1060 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
perkj71ee44c2016-06-15 00:47:53 -07001061 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss,
minyue78b4d562016-11-30 04:47:39 -08001062 rtt_ms, probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001063
asaperssonce2e1362016-09-09 00:13:35 -07001064 // Ignore updates if bitrate is zero (the aggregate network state is down).
1065 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001066 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001067 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1068 pacer_bitrate_kbps_counter_.ProcessAndPause();
1069 return;
stefan18adf0a2015-11-17 06:24:56 -08001070 }
asaperssonce2e1362016-09-09 00:13:35 -07001071
1072 bool sending_video;
1073 {
1074 ReadLockScoped read_lock(*send_crit_);
1075 sending_video = !video_send_streams_.empty();
1076 }
1077
1078 rtc::CritScope lock(&bitrate_crit_);
1079 if (!sending_video) {
1080 // Do not update the stats if we are not sending video.
1081 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1082 pacer_bitrate_kbps_counter_.ProcessAndPause();
1083 return;
1084 }
1085 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1086 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1087 uint32_t pacer_bitrate_bps =
1088 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1089 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001090}
mflodman101f2502016-06-09 17:21:19 +02001091
perkj71ee44c2016-06-15 00:47:53 -07001092void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001093 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +01001094 uint32_t total_bitrate_bps,
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001095 uint32_t allocated_without_feedback_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +01001096 bool has_packet_feedback) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001097 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001098 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Janssone6256052018-05-04 14:08:15 +02001099 transport_send_ptr_->SetPerPacketFeedbackAvailable(has_packet_feedback);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001100 transport_send_ptr_->SetAllocatedBitrateWithoutFeedback(
1101 allocated_without_feedback_bps);
1102
perkj71ee44c2016-06-15 00:47:53 -07001103 rtc::CritScope lock(&bitrate_crit_);
1104 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001105 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001106}
1107
pbos8fc7fa72015-07-15 08:02:58 -07001108void Call::ConfigureSync(const std::string& sync_group) {
1109 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001110 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001111 return;
1112
1113 AudioReceiveStream* sync_audio_stream = nullptr;
1114 // Find existing audio stream.
1115 const auto it = sync_stream_mapping_.find(sync_group);
1116 if (it != sync_stream_mapping_.end()) {
1117 sync_audio_stream = it->second;
1118 } else {
1119 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001120 for (AudioReceiveStream* stream : audio_receive_streams_) {
1121 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001122 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001123 RTC_LOG(LS_WARNING)
1124 << "Attempting to sync more than one audio stream "
1125 "within the same sync group. This is not "
1126 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001127 break;
1128 }
nissee4bcd6d2017-05-16 04:47:04 -07001129 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001130 }
1131 }
1132 }
1133 if (sync_audio_stream)
1134 sync_stream_mapping_[sync_group] = sync_audio_stream;
1135 size_t num_synced_streams = 0;
1136 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1137 if (video_stream->config().sync_group != sync_group)
1138 continue;
1139 ++num_synced_streams;
1140 if (num_synced_streams > 1) {
1141 // TODO(pbos): Support synchronizing more than one A/V pair.
1142 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001143 RTC_LOG(LS_WARNING)
1144 << "Attempting to sync more than one audio/video pair "
1145 "within the same sync group. This is not supported in "
1146 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001147 }
1148 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001149 if (num_synced_streams == 1) {
1150 // sync_audio_stream may be null and that's ok.
1151 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001152 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001153 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001154 }
1155 }
1156}
1157
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001158PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1159 const uint8_t* packet,
1160 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001161 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001162 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001163 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1164 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001165 if (received_bytes_per_second_counter_.HasSample()) {
1166 // First RTP packet has been received.
1167 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1168 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1169 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001170 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001171 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001172 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001173 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001174 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001175 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001176 }
1177 }
1178 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1179 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001180 for (AudioReceiveStream* stream : audio_receive_streams_) {
1181 if (stream->DeliverRtcp(packet, length))
mflodman3d7db262016-04-29 00:57:13 -07001182 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001183 }
1184 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001185 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001186 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001187 for (VideoSendStream* stream : video_send_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001188 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001189 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001190 }
1191 }
mflodman3d7db262016-04-29 00:57:13 -07001192 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1193 ReadLockScoped read_lock(*send_crit_);
1194 for (auto& kv : audio_send_ssrcs_) {
1195 if (kv.second->DeliverRtcp(packet, length))
1196 rtcp_delivered = true;
1197 }
1198 }
1199
Elad Alon4a87e1c2017-10-03 16:11:34 +02001200 if (rtcp_delivered) {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001201 event_log_->Log(absl::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001202 rtc::MakeArrayView(packet, length)));
1203 }
mflodman3d7db262016-04-29 00:57:13 -07001204
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001205 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001206}
1207
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001208PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001209 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001210 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001211 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001212
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001213 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001214 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001215 return DELIVERY_PACKET_ERROR;
1216
Niels Möller70082872018-08-07 11:03:12 +02001217 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001218 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001219 // Repair packet_time_us for clock resets by comparing a new read of
1220 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001221 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001222 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001223 }
Niels Möller70082872018-08-07 11:03:12 +02001224 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001225 } else {
1226 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1227 }
nissed44ce052017-02-06 02:23:00 -08001228
sprangc1abde72017-07-11 03:56:21 -07001229 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1230 // These are empty (zero length payload) RTP packets with an unsignaled
1231 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001232 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001233
1234 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1235 is_keep_alive_packet);
1236
sprangc1abde72017-07-11 03:56:21 -07001237 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001238 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001239 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001240 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1241 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001242 // Destruction of the receive stream, including deregistering from the
1243 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1244 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1245 // So by not passing the packet on to demuxing in this case, we prevent
1246 // incoming packets to be passed on via the demuxer to a receive stream
1247 // which is being torned down.
1248 return DELIVERY_UNKNOWN_SSRC;
1249 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001250 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001251
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001252 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001253
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001254 // RateCounters expect input parameter as int, save it as int,
1255 // instead of converting each time it is passed to RateCounter::Add below.
1256 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001257 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001258 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001259 received_bytes_per_second_counter_.Add(length);
1260 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001261 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001262 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001263 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001264 if (!first_received_rtp_audio_ms_) {
1265 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1266 }
1267 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001268 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001269 }
nissee4bcd6d2017-05-16 04:47:04 -07001270 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001271 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001272 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001273 received_bytes_per_second_counter_.Add(length);
1274 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001275 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001276 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001277 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001278 if (!first_received_rtp_video_ms_) {
1279 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1280 }
1281 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001282 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001283 }
1284 }
1285 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001286}
1287
stefan68786d22015-09-08 05:36:15 -07001288PacketReceiver::DeliveryStatus Call::DeliverPacket(
1289 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001290 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001291 int64_t packet_time_us) {
eladalond1dd2f72017-08-25 02:55:57 -07001292 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001293 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1294 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001295
Niels Möller70082872018-08-07 11:03:12 +02001296 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001297}
1298
nissed2ef3142017-05-11 08:00:58 -07001299void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001300 RtpPacketReceived parsed_packet;
1301 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001302 return;
1303
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001304 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001305
brandtrcaea68f2017-08-23 00:55:17 -07001306 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001307 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001308 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001309 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1310 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001311 // Destruction of the receive stream, including deregistering from the
1312 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1313 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1314 // So by not passing the packet on to demuxing in this case, we prevent
1315 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001316 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001317 return;
1318 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001319 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001320
1321 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001322 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001323 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001324}
1325
nissed44ce052017-02-06 02:23:00 -08001326void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1327 MediaType media_type) {
1328 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001329 bool use_send_side_bwe =
1330 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001331
brandtrb29e6522016-12-21 06:37:18 -08001332 RTPHeader header;
1333 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001334
nisse4709e892017-02-07 01:18:43 -08001335 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001336 // Inconsistent configuration of send side BWE. Do nothing.
1337 // TODO(nisse): Without this check, we may produce RTCP feedback
1338 // packets even when not negotiated. But it would be cleaner to
1339 // move the check down to RTCPSender::SendFeedbackPacket, which
1340 // would also help the PacketRouter to select an appropriate rtp
1341 // module in the case that some, but not all, have RTCP feedback
1342 // enabled.
1343 return;
1344 }
1345 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001346 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001347 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001348 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001349 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1350 header);
1351 }
brandtrb29e6522016-12-21 06:37:18 -08001352}
1353
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001354} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001355
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001356} // namespace webrtc