blob: 3ffea84e2c65551e5585059b11b6e0b0b6862f92 [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 "call/bitrate_allocator.h"
26#include "call/call.h"
27#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010028#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "call/rtp_stream_receiver_controller.h"
30#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020031#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020032#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
33#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
34#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
35#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon99a81b62017-09-21 10:25:29 +020037#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "modules/bitrate_controller/include/bitrate_controller.h"
39#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
40#include "modules/rtp_rtcp/include/flexfec_receiver.h"
41#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
42#include "modules/rtp_rtcp/include/rtp_header_parser.h"
43#include "modules/rtp_rtcp/source/byte_io.h"
44#include "modules/rtp_rtcp/source/rtp_packet_received.h"
45#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010046#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/checks.h"
48#include "rtc_base/constructormagic.h"
49#include "rtc_base/location.h"
50#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 15:59:12 +010051#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/sequenced_task_checker.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020053#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020054#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/task_queue.h"
56#include "rtc_base/thread_annotations.h"
Christoffer Rodbro76ad1542018-10-12 11:15:09 +020057#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "rtc_base/trace_event.h"
59#include "system_wrappers/include/clock.h"
60#include "system_wrappers/include/cpu_info.h"
61#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "video/call_stats.h"
63#include "video/send_delay_stats.h"
64#include "video/stats_counter.h"
65#include "video/video_receive_stream.h"
66#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000067
68namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000069
nisse4709e892017-02-07 01:18:43 -080070namespace {
nisse4709e892017-02-07 01:18:43 -080071// 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) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200105 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700106 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);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200116 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200117 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) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200125 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700126 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
Niels Möller259a4972018-04-05 15:36:51 +0200133 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
134 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700135 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) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200141 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700142 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
nisse4709e892017-02-07 01:18:43 -0800148} // namespace
149
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000150namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000151
Sebastian Janssone6256052018-05-04 14:08:15 +0200152class Call final : public webrtc::Call,
153 public PacketReceiver,
154 public RecoveredPacketReceiver,
155 public TargetTransferRateObserver,
156 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000157 public:
nisseb8f9a322017-03-27 05:36:15 -0700158 Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700159 std::unique_ptr<RtpTransportControllerSendInterface> transport_send);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200160 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000161
brandtr25445d32016-10-23 23:37:14 -0700162 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000163 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000164
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200165 webrtc::AudioSendStream* CreateAudioSendStream(
166 const webrtc::AudioSendStream::Config& config) override;
167 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
168
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200169 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
170 const webrtc::AudioReceiveStream::Config& config) override;
171 void DestroyAudioReceiveStream(
172 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000173
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200174 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700175 webrtc::VideoSendStream::Config config,
176 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100177 webrtc::VideoSendStream* CreateVideoSendStream(
178 webrtc::VideoSendStream::Config config,
179 VideoEncoderConfig encoder_config,
180 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000181 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000182
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200183 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200184 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000185 void DestroyVideoReceiveStream(
186 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000187
brandtr7250b392016-12-19 01:13:46 -0800188 FlexfecReceiveStream* CreateFlexfecReceiveStream(
189 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700190 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800191 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700192
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100193 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
194
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000195 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000196
brandtr25445d32016-10-23 23:37:14 -0700197 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700198 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100199 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200200 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000201
brandtr4e523862016-10-18 23:50:45 -0700202 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700203 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700204
Alex Narest78609d52017-10-20 10:37:47 +0200205 void SetBitrateAllocationStrategy(
206 std::unique_ptr<rtc::BitrateAllocationStrategy>
207 bitrate_allocation_strategy) override;
208
skvlad7a43d252016-03-22 15:32:27 -0700209 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000210
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200211 void OnAudioTransportOverheadChanged(
212 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800213
stefanc1aeaf02015-10-15 07:26:07 -0700214 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
215
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100216 // Implements TargetTransferRateObserver,
217 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100218 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800219
perkj71ee44c2016-06-15 00:47:53 -0700220 // Implements BitrateAllocator::LimitObserver.
221 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +0100222 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +0100223 uint32_t total_bitrate_bps,
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200224 uint32_t allocated_without_feedback_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +0100225 bool has_packet_feedback) override;
perkj71ee44c2016-06-15 00:47:53 -0700226
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800227 // This method is invoked when the media transport is created and when the
228 // media transport is being destructed.
229 // We only allow one media transport per connection.
230 //
231 // It should be called with non-null argument at most once, and if it was
232 // called with non-null argument, it has to be called with a null argument
233 // at least once after that.
234 void MediaTransportChange(MediaTransportInterface* media_transport) override;
235
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000236 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200237 DeliveryStatus DeliverRtcp(MediaType media_type,
238 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200239 size_t length);
stefan68786d22015-09-08 05:36:15 -0700240 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100241 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200242 int64_t packet_time_us);
pbos8fc7fa72015-07-15 08:02:58 -0700243 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700244 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700245
nissed44ce052017-02-06 02:23:00 -0800246 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
247 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700248 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800249
asaperssonfc5e81c2017-04-19 23:28:53 -0700250 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700251 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800252 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700253 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700254 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800255
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800256 // If |media_transport| is not null, it registers the rate observer for the
257 // media transport.
258 void RegisterRateObserver() RTC_LOCKS_EXCLUDED(target_observer_crit_);
259
Niels Möller46879152019-01-07 15:54:47 +0100260 // Intended for DCHECKs, to avoid locking in production builds.
261 MediaTransportInterface* media_transport()
262 RTC_LOCKS_EXCLUDED(target_observer_crit_);
263
Peter Boströmd3c94472015-12-09 11:20:58 +0100264 Clock* const clock_;
stefan91d92602015-11-11 10:13:02 -0800265
Peter Boström45553ae2015-05-08 13:54:38 +0200266 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800267 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800268 const std::unique_ptr<CallStats> call_stats_;
269 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000270 Call::Config config_;
eladalonf3f5c0e2017-08-18 02:47:08 -0700271 rtc::SequencedTaskChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000272
skvlad7a43d252016-03-22 15:32:27 -0700273 NetworkState audio_network_state_;
274 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100275 rtc::CriticalSection aggregate_network_up_crit_;
276 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000277
kwibergb25345e2016-03-12 06:10:44 -0800278 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700279 // Audio, Video, and FlexFEC receive streams are owned by the client that
280 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700281 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700282 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200283 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700284 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700285
pbos8fc7fa72015-07-15 08:02:58 -0700286 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700287 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000288
nisse0f15f922017-06-21 01:05:22 -0700289 // TODO(nisse): Should eventually be injected at creation,
290 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700291 RtpStreamReceiverController audio_receiver_controller_;
292 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700293
nissed44ce052017-02-06 02:23:00 -0800294 // This extra map is used for receive processing which is
295 // independent of media type.
296
297 // TODO(nisse): In the RTP transport refactoring, we should have a
298 // single mapping from ssrc to a more abstract receive stream, with
299 // accessor methods for all configuration we need at this level.
300 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100301 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
302 : extensions(config.rtp.extensions),
303 use_send_side_bwe(UseSendSideBwe(config)) {}
304 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
305 : extensions(config.rtp.extensions),
306 use_send_side_bwe(UseSendSideBwe(config)) {}
307 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
308 : extensions(config.rtp_header_extensions),
309 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800310
311 // Registered RTP header extensions for each stream. Note that RTP header
312 // extensions are negotiated per track ("m= line") in the SDP, but we have
313 // no notion of tracks at the Call level. We therefore store the RTP header
314 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100315 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800316 // Set if both RTP extension the RTCP feedback message needed for
317 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100318 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800319 };
320 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700321 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800322
kwibergb25345e2016-03-12 06:10:44 -0800323 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700324 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700325 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
326 RTC_GUARDED_BY(send_crit_);
327 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
328 RTC_GUARDED_BY(send_crit_);
329 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000330
ossuc3d4b482017-05-23 06:07:11 -0700331 using RtpStateMap = std::map<uint32_t, RtpState>;
332 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700333 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700334 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700335 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700336
Åsa Persson4bece9a2017-10-06 10:04:04 +0200337 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
338 RtpPayloadStateMap suspended_video_payload_states_
339 RTC_GUARDED_BY(configuration_sequence_checker_);
340
skvlad11a9cbf2016-10-07 11:53:05 -0700341 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700342
stefan18adf0a2015-11-17 06:24:56 -0800343 // The following members are only accessed (exclusively) from one thread and
344 // from the destructor, and therefore doesn't need any explicit
345 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700346 RateCounter received_bytes_per_second_counter_;
347 RateCounter received_audio_bytes_per_second_counter_;
348 RateCounter received_video_bytes_per_second_counter_;
349 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200350 absl::optional<int64_t> first_received_rtp_audio_ms_;
351 absl::optional<int64_t> last_received_rtp_audio_ms_;
352 absl::optional<int64_t> first_received_rtp_video_ms_;
353 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 10:13:02 -0800354
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100355 rtc::CriticalSection last_bandwidth_bps_crit_;
356 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800357 // TODO(holmer): Remove this lock once BitrateController no longer calls
358 // OnNetworkChanged from multiple threads.
359 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700360 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
361 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
362 AvgCounter estimated_send_bitrate_kbps_counter_
363 RTC_GUARDED_BY(&bitrate_crit_);
364 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800365
nisse559af382017-03-21 06:41:12 -0700366 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100367
368 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
369
asapersson35151f32016-05-02 23:44:01 -0700370 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700371 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800372
Sebastian Janssone6256052018-05-04 14:08:15 +0200373 // Caches transport_send_.get(), to avoid racing with destructor.
374 // Note that this is declared before transport_send_ to ensure that it is not
375 // invalidated until no more tasks can be running on the transport_send_ task
376 // queue.
377 RtpTransportControllerSendInterface* transport_send_ptr_;
378 // Declared last since it will issue callbacks from a task queue. Declaring it
379 // last ensures that it is destroyed first and any running tasks are finished.
380 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800381
382 // This is a precaution, since |MediaTransportChange| is not guaranteed to be
383 // invoked on a particular thread.
384 rtc::CriticalSection target_observer_crit_;
385 bool is_target_rate_observer_registered_
386 RTC_GUARDED_BY(&target_observer_crit_) = false;
387 MediaTransportInterface* media_transport_
388 RTC_GUARDED_BY(&target_observer_crit_) = nullptr;
389
henrikg3c089d72015-09-16 05:37:44 -0700390 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000391};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000392} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000393
asapersson2e5cfcd2016-08-11 08:41:18 -0700394std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200395 char buf[1024];
396 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700397 ss << "Call stats: " << time_ms << ", {";
398 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
399 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
400 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
401 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
402 ss << "rtt_ms: " << rtt_ms;
403 ss << '}';
404 return ss.str();
405}
406
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000407Call* Call::Create(const Call::Config& config) {
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100408 return new internal::Call(
Karl Wiberg918f50c2018-07-05 11:40:33 +0200409 config, absl::make_unique<RtpTransportControllerSend>(
Sebastian Janssondfce03a2018-05-18 18:05:10 +0200410 Clock::GetRealTimeClock(), config.event_log,
411 config.network_controller_factory, config.bitrate_config));
zstein7cb69d52017-05-08 11:52:38 -0700412}
413
414Call* Call::Create(
415 const Call::Config& config,
416 std::unique_ptr<RtpTransportControllerSendInterface> transport_send) {
417 return new internal::Call(config, std::move(transport_send));
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000418}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000419
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100420// This method here to avoid subclasses has to implement this method.
421// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
422// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100423VideoSendStream* Call::CreateVideoSendStream(
424 VideoSendStream::Config config,
425 VideoEncoderConfig encoder_config,
426 std::unique_ptr<FecController> fec_controller) {
427 return nullptr;
428}
429
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000430namespace internal {
431
nisseb8f9a322017-03-27 05:36:15 -0700432Call::Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700433 std::unique_ptr<RtpTransportControllerSendInterface> transport_send)
stefan91d92602015-11-11 10:13:02 -0800434 : clock_(Clock::GetRealTimeClock()),
435 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
kwiberg1c7fdd82016-04-26 08:18:04 -0700436 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Tommi38c5d932018-03-27 23:11:09 +0200437 call_stats_(new CallStats(clock_, module_process_thread_.get())),
perkj71ee44c2016-06-15 00:47:53 -0700438 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200439 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800440 audio_network_state_(kNetworkDown),
441 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100442 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000443 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800444 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700445 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700446 received_bytes_per_second_counter_(clock_, nullptr, true),
447 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
448 received_video_bytes_per_second_counter_(clock_, nullptr, true),
449 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100450 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700451 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700452 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700453 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
454 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700455 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100456 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700457 video_send_delay_stats_(new SendDelayStats(clock_)),
Sebastian Janssone6256052018-05-04 14:08:15 +0200458 start_ms_(clock_->TimeInMilliseconds()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700459 RTC_DCHECK(config.event_log != nullptr);
nisse6167b262017-04-06 06:34:25 -0700460 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 14:08:15 +0200461 transport_send_ptr_ = transport_send_.get();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000462}
463
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000464Call::~Call() {
eladalonf3f5c0e2017-08-18 02:47:08 -0700465 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700466
solenbergc7a8b082015-10-16 14:35:07 -0700467 RTC_CHECK(audio_send_ssrcs_.empty());
468 RTC_CHECK(video_send_ssrcs_.empty());
469 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700470 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700471 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000472
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800473 if (!media_transport_) {
474 module_process_thread_->DeRegisterModule(
475 receive_side_cc_.GetRemoteBitrateEstimator(true));
476 module_process_thread_->DeRegisterModule(&receive_side_cc_);
477 module_process_thread_->DeRegisterModule(call_stats_.get());
478 module_process_thread_->Stop();
479 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
480 call_stats_->DeregisterStatsObserver(
481 transport_send_->GetCallStatsObserver());
482 }
sprang6d6122b2016-07-13 06:37:09 -0700483
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100484 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700485 // Only update histograms after process threads have been shut down, so that
486 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700487 {
488 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700489 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700490 }
sprang6d6122b2016-07-13 06:37:09 -0700491 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700492 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000493}
494
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800495void Call::RegisterRateObserver() {
496 rtc::CritScope lock(&target_observer_crit_);
497
498 if (is_target_rate_observer_registered_) {
499 return;
500 }
501
502 is_target_rate_observer_registered_ = true;
503
504 if (media_transport_) {
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800505 // TODO(bugs.webrtc.org/9719): We should report call_stats_ from
506 // media transport (at least Rtt). We should extend media transport
507 // interface to include "receive_side bwe" if needed.
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800508 media_transport_->AddTargetTransferRateObserver(this);
509 } else {
510 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800511
512 call_stats_->RegisterStatsObserver(&receive_side_cc_);
513 call_stats_->RegisterStatsObserver(transport_send_->GetCallStatsObserver());
514
515 module_process_thread_->RegisterModule(
516 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
517 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
518 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
519 module_process_thread_->Start();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800520 }
521}
522
Niels Möller46879152019-01-07 15:54:47 +0100523MediaTransportInterface* Call::media_transport() {
524 rtc::CritScope lock(&target_observer_crit_);
525 return media_transport_;
526}
527
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800528void Call::MediaTransportChange(MediaTransportInterface* media_transport) {
529 rtc::CritScope lock(&target_observer_crit_);
530
531 if (is_target_rate_observer_registered_) {
532 // Only used to unregister rate observer from media transport. Registration
533 // happens when the stream is created.
534 if (!media_transport && media_transport_) {
535 media_transport_->RemoveTargetTransferRateObserver(this);
536 media_transport_ = nullptr;
537 is_target_rate_observer_registered_ = false;
538 }
539 } else if (media_transport) {
540 RTC_DCHECK(media_transport_ == nullptr ||
541 media_transport_ == media_transport)
542 << "media_transport_=" << (media_transport_ != nullptr)
543 << ", (media_transport_==media_transport)="
544 << (media_transport_ == media_transport);
545 media_transport_ = media_transport;
546 }
547}
548
asapersson4374a092016-07-27 00:39:09 -0700549void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700550 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700551 "WebRTC.Call.LifetimeInSeconds",
552 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
553}
554
asaperssonfc5e81c2017-04-19 23:28:53 -0700555void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
556 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800557 return;
558 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700559 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800560 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
561 return;
asaperssonce2e1362016-09-09 00:13:35 -0700562 const int kMinRequiredPeriodicSamples = 5;
563 AggregatedStats send_bitrate_stats =
564 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
565 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700566 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
567 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100568 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
569 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800570 }
asaperssonce2e1362016-09-09 00:13:35 -0700571 AggregatedStats pacer_bitrate_stats =
572 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
573 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700574 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
575 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100576 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
577 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800578 }
579}
580
581void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700582 if (first_received_rtp_audio_ms_) {
583 RTC_HISTOGRAM_COUNTS_100000(
584 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
585 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
586 }
587 if (first_received_rtp_video_ms_) {
588 RTC_HISTOGRAM_COUNTS_100000(
589 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
590 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
591 }
asapersson250fd972016-09-08 00:07:21 -0700592 const int kMinRequiredPeriodicSamples = 5;
593 AggregatedStats video_bytes_per_sec =
594 received_video_bytes_per_second_counter_.GetStats();
595 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700596 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
597 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100598 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
599 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800600 }
asapersson250fd972016-09-08 00:07:21 -0700601 AggregatedStats audio_bytes_per_sec =
602 received_audio_bytes_per_second_counter_.GetStats();
603 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700604 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
605 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100606 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
607 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800608 }
asapersson250fd972016-09-08 00:07:21 -0700609 AggregatedStats rtcp_bytes_per_sec =
610 received_rtcp_bytes_per_second_counter_.GetStats();
611 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700612 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
613 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100614 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
615 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800616 }
asapersson250fd972016-09-08 00:07:21 -0700617 AggregatedStats recv_bytes_per_sec =
618 received_bytes_per_second_counter_.GetStats();
619 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700620 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
621 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100622 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
623 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700624 }
stefan91d92602015-11-11 10:13:02 -0800625}
626
solenberg5a289392015-10-19 03:39:20 -0700627PacketReceiver* Call::Receiver() {
eladalond1dd2f72017-08-25 02:55:57 -0700628 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700629 return this;
630}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000631
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200632webrtc::AudioSendStream* Call::CreateAudioSendStream(
633 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700634 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700635 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800636
Niels Möller46879152019-01-07 15:54:47 +0100637 RTC_DCHECK(media_transport() == config.media_transport);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800638
639 RegisterRateObserver();
640
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100641 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
642 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200643 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700644 {
645 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
646 if (iter != suspended_audio_send_ssrcs_.end()) {
647 suspended_rtp_state.emplace(iter->second);
648 }
649 }
650
Sebastian Janssone6256052018-05-04 14:08:15 +0200651 // TODO(srte): AudioSendStream should call GetWorkerQueue directly rather than
652 // having it injected.
653
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100654 AudioSendStream* send_stream = new AudioSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200655 config, config_.audio_state, transport_send_ptr_->GetWorkerQueue(),
656 module_process_thread_.get(), transport_send_ptr_,
657 bitrate_allocator_.get(), event_log_, call_stats_.get(),
Sam Zackrissonff058162018-11-20 17:15:13 +0100658 suspended_rtp_state);
solenbergc7a8b082015-10-16 14:35:07 -0700659 {
solenbergc7a8b082015-10-16 14:35:07 -0700660 WriteLockScoped write_lock(*send_crit_);
661 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
662 audio_send_ssrcs_.end());
663 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700664 }
solenberg7602aab2016-11-14 11:30:07 -0800665 {
666 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700667 for (AudioReceiveStream* stream : audio_receive_streams_) {
668 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
669 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800670 }
671 }
672 }
skvlad7a43d252016-03-22 15:32:27 -0700673 send_stream->SignalNetworkState(audio_network_state_);
674 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700675 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200676}
677
678void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700679 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700680 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700681 RTC_DCHECK(send_stream != nullptr);
682
683 send_stream->Stop();
684
eladalonabbc4302017-07-26 02:09:44 -0700685 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700686 webrtc::internal::AudioSendStream* audio_send_stream =
687 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700688 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700689 {
690 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800691 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
692 RTC_DCHECK_EQ(1, num_deleted);
693 }
694 {
695 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700696 for (AudioReceiveStream* stream : audio_receive_streams_) {
697 if (stream->config().rtp.local_ssrc == ssrc) {
698 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800699 }
700 }
solenbergc7a8b082015-10-16 14:35:07 -0700701 }
skvlad7a43d252016-03-22 15:32:27 -0700702 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700703 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200704}
705
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200706webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
707 const webrtc::AudioReceiveStream::Config& config) {
708 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700709 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800710 RegisterRateObserver();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200711 event_log_->Log(absl::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200712 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700713 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200714 &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100715 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200716 {
717 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100718 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
719 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700720 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800721
pbos8fc7fa72015-07-15 08:02:58 -0700722 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200723 }
solenberg7602aab2016-11-14 11:30:07 -0800724 {
725 ReadLockScoped read_lock(*send_crit_);
726 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
727 if (it != audio_send_ssrcs_.end()) {
728 receive_stream->AssociateSendStream(it->second);
729 }
730 }
skvlad7a43d252016-03-22 15:32:27 -0700731 receive_stream->SignalNetworkState(audio_network_state_);
732 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200733 return receive_stream;
734}
735
736void Call::DestroyAudioReceiveStream(
737 webrtc::AudioReceiveStream* receive_stream) {
738 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700739 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700740 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700741 webrtc::internal::AudioReceiveStream* audio_receive_stream =
742 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200743 {
744 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800745 const AudioReceiveStream::Config& config = audio_receive_stream->config();
746 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700747 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800748 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700749 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700750 const std::string& sync_group = audio_receive_stream->config().sync_group;
751 const auto it = sync_stream_mapping_.find(sync_group);
752 if (it != sync_stream_mapping_.end() &&
753 it->second == audio_receive_stream) {
754 sync_stream_mapping_.erase(it);
755 ConfigureSync(sync_group);
756 }
nissed44ce052017-02-06 02:23:00 -0800757 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200758 }
skvlad7a43d252016-03-22 15:32:27 -0700759 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200760 delete audio_receive_stream;
761}
762
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100763// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100764webrtc::VideoSendStream* Call::CreateVideoSendStream(
765 webrtc::VideoSendStream::Config config,
766 VideoEncoderConfig encoder_config,
767 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000768 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700769 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000770
Niels Möller46879152019-01-07 15:54:47 +0100771 RTC_DCHECK(media_transport() == config.media_transport);
772
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800773 RegisterRateObserver();
774
asapersson35151f32016-05-02 23:44:01 -0700775 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700776 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
777 ++ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200778 event_log_->Log(absl::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200779 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700780 }
perkj26091b12016-09-01 01:17:40 -0700781
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000782 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
783 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700784 // Copy ssrcs from |config| since |config| is moved.
785 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100786
Sebastian Janssone6256052018-05-04 14:08:15 +0200787 // TODO(srte): VideoSendStream should call GetWorkerQueue directly rather than
788 // having it injected.
mflodman0c478b32015-10-21 15:52:16 +0200789 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200790 num_cpu_cores_, module_process_thread_.get(),
791 transport_send_ptr_->GetWorkerQueue(), call_stats_.get(),
792 transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700793 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200794 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200795 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700796
skvlad7a43d252016-03-22 15:32:27 -0700797 {
798 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700799 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700800 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
801 video_send_ssrcs_[ssrc] = send_stream;
802 }
803 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000804 }
skvlad7a43d252016-03-22 15:32:27 -0700805 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700806
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000807 return send_stream;
808}
809
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100810webrtc::VideoSendStream* Call::CreateVideoSendStream(
811 webrtc::VideoSendStream::Config config,
812 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100813 if (config_.fec_controller_factory) {
814 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
815 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100816 std::unique_ptr<FecController> fec_controller =
817 config_.fec_controller_factory
818 ? config_.fec_controller_factory->CreateFecController()
Karl Wiberg918f50c2018-07-05 11:40:33 +0200819 : absl::make_unique<FecControllerDefault>(Clock::GetRealTimeClock());
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100820 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
821 std::move(fec_controller));
822}
823
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000824void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000825 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700826 RTC_DCHECK(send_stream != nullptr);
eladalonf3f5c0e2017-08-18 02:47:08 -0700827 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000828
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000829 send_stream->Stop();
830
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000831 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000832 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000833 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200834 auto it = video_send_ssrcs_.begin();
835 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000836 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
837 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200838 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000839 } else {
840 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000841 }
842 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200843 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000844 }
henrikg91d6ede2015-09-17 00:24:34 -0700845 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000846
Åsa Persson4bece9a2017-10-06 10:04:04 +0200847 VideoSendStream::RtpStateMap rtp_states;
848 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
849 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
850 &rtp_payload_states);
851 for (const auto& kv : rtp_states) {
852 suspended_video_send_ssrcs_[kv.first] = kv.second;
853 }
854 for (const auto& kv : rtp_payload_states) {
855 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000856 }
857
skvlad7a43d252016-03-22 15:32:27 -0700858 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000859 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000860}
861
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200862webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200863 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000864 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700865 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800866
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800867 RegisterRateObserver();
868
nisse0f15f922017-06-21 01:05:22 -0700869 VideoReceiveStream* receive_stream = new VideoReceiveStream(
eladalon2a2b2972017-07-03 09:25:27 -0700870 &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200871 transport_send_ptr_->packet_router(), std::move(configuration),
nisse0f15f922017-06-21 01:05:22 -0700872 module_process_thread_.get(), call_stats_.get());
Tommi733b5472016-06-10 17:58:01 +0200873
874 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700875 {
876 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800877 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800878 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700879 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800880 // type, we may get an incorrect value for the rtx stream, but
881 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100882 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
883 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800884 }
Erik Språng09708512018-03-14 15:16:50 +0100885 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
886 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700887 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700888 ConfigureSync(config.sync_group);
889 }
890 receive_stream->SignalNetworkState(video_network_state_);
891 UpdateAggregateNetworkState();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200892 event_log_->Log(absl::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200893 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000894 return receive_stream;
895}
896
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000897void Call::DestroyVideoReceiveStream(
898 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000899 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700900 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700901 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700902 VideoReceiveStream* receive_stream_impl =
903 static_cast<VideoReceiveStream*>(receive_stream);
904 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000905 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000906 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000907 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
908 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700909 receive_rtp_config_.erase(config.rtp.remote_ssrc);
910 if (config.rtp.rtx_ssrc) {
911 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000912 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200913 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700914 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000915 }
nisse4709e892017-02-07 01:18:43 -0800916
nisse559af382017-03-21 06:41:12 -0700917 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800918 ->RemoveStream(config.rtp.remote_ssrc);
919
skvlad7a43d252016-03-22 15:32:27 -0700920 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000921 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000922}
923
brandtr7250b392016-12-19 01:13:46 -0800924FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
925 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700926 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700927 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800928
929 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700930
nisse0f15f922017-06-21 01:05:22 -0700931 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700932 {
933 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -0700934 // Unlike the video and audio receive streams,
935 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
936 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -0700937 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -0700938 // constructor while holding |receive_crit_| ensures that we don't
939 // call OnRtpPacket until the constructor is finished and the
940 // object is in a valid state.
941 // TODO(nisse): Fix constructor so that it can be moved outside of
942 // this locked scope.
943 receive_stream = new FlexfecReceiveStreamImpl(
eladalon2a2b2972017-07-03 09:25:27 -0700944 &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +0200945 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -0800946
nissed44ce052017-02-06 02:23:00 -0800947 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
948 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +0100949 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -0700950 }
brandtrb29e6522016-12-21 06:37:18 -0800951
brandtr25445d32016-10-23 23:37:14 -0700952 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -0800953
brandtr25445d32016-10-23 23:37:14 -0700954 return receive_stream;
955}
956
brandtr7250b392016-12-19 01:13:46 -0800957void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -0700958 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700959 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800960
brandtr25445d32016-10-23 23:37:14 -0700961 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -0700962 {
963 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800964
eladalon42f44f92017-07-25 06:40:06 -0700965 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -0800966 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -0800967 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -0800968
brandtr7250b392016-12-19 01:13:46 -0800969 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
970 // destroyed.
nisse559af382017-03-21 06:41:12 -0700971 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800972 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -0700973 }
brandtrb29e6522016-12-21 06:37:18 -0800974
eladalon42f44f92017-07-25 06:40:06 -0700975 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700976}
977
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100978RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +0200979 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100980}
981
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000982Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700983 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
984 // thread. Re-enable once that is fixed.
eladalonf3f5c0e2017-08-18 02:47:08 -0700985 // RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000986 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200987 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +0200988 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000989 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -0700990 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700991 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100992
993 {
994 rtc::CritScope cs(&last_bandwidth_bps_crit_);
995 stats.send_bandwidth_bps = last_bandwidth_bps_;
996 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000997 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100998 // TODO(srte): It is unclear if we only want to report queues if network is
999 // available.
1000 {
1001 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 14:08:15 +02001002 stats.pacer_delay_ms = aggregate_network_up_
1003 ? transport_send_ptr_->GetPacerQueuingDelayMs()
1004 : 0;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001005 }
1006
Tommi38c5d932018-03-27 23:11:09 +02001007 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -07001008 {
1009 rtc::CritScope cs(&bitrate_crit_);
1010 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
1011 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001012 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001013}
1014
Alex Narest78609d52017-10-20 10:37:47 +02001015void Call::SetBitrateAllocationStrategy(
1016 std::unique_ptr<rtc::BitrateAllocationStrategy>
1017 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001018 // TODO(srte): This function should be moved to RtpTransportControllerSend
1019 // when BitrateAllocator is moved there.
1020 struct Functor {
1021 void operator()() {
1022 bitrate_allocator_->SetBitrateAllocationStrategy(
1023 std::move(bitrate_allocation_strategy_));
1024 }
1025 BitrateAllocator* bitrate_allocator_;
1026 std::unique_ptr<rtc::BitrateAllocationStrategy>
1027 bitrate_allocation_strategy_;
1028 };
1029 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
1030 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 10:37:47 +02001031}
1032
skvlad7a43d252016-03-22 15:32:27 -07001033void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
eladalonf3f5c0e2017-08-18 02:47:08 -07001034 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001035 switch (media) {
1036 case MediaType::AUDIO:
1037 audio_network_state_ = state;
1038 break;
1039 case MediaType::VIDEO:
1040 video_network_state_ = state;
1041 break;
1042 case MediaType::ANY:
1043 case MediaType::DATA:
1044 RTC_NOTREACHED();
1045 break;
1046 }
1047
1048 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001049 {
skvlad7a43d252016-03-22 15:32:27 -07001050 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -07001051 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001052 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -07001053 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001054 }
1055 {
skvlad7a43d252016-03-22 15:32:27 -07001056 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001057 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1058 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001059 }
nissee4bcd6d2017-05-16 04:47:04 -07001060 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1061 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001062 }
1063 }
1064}
1065
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001066void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1067 ReadLockScoped read_lock(*send_crit_);
1068 for (auto& kv : audio_send_ssrcs_) {
1069 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -08001070 }
1071}
1072
skvlad7a43d252016-03-22 15:32:27 -07001073void Call::UpdateAggregateNetworkState() {
eladalonf3f5c0e2017-08-18 02:47:08 -07001074 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001075
1076 bool have_audio = false;
1077 bool have_video = false;
1078 {
1079 ReadLockScoped read_lock(*send_crit_);
1080 if (audio_send_ssrcs_.size() > 0)
1081 have_audio = true;
1082 if (video_send_ssrcs_.size() > 0)
1083 have_video = true;
1084 }
1085 {
1086 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001087 if (audio_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001088 have_audio = true;
nissee4bcd6d2017-05-16 04:47:04 -07001089 if (video_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001090 have_video = true;
1091 }
1092
Sebastian Janssona06e9192018-03-07 18:49:55 +01001093 bool aggregate_network_up =
1094 ((have_video && video_network_state_ == kNetworkUp) ||
1095 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001096
Mirko Bonadei675513b2017-11-09 11:09:25 +01001097 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001098 << (aggregate_network_up ? "up" : "down");
1099 {
1100 rtc::CritScope cs(&aggregate_network_up_crit_);
1101 aggregate_network_up_ = aggregate_network_up;
1102 }
Sebastian Janssone6256052018-05-04 14:08:15 +02001103 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001104}
1105
stefanc1aeaf02015-10-15 07:26:07 -07001106void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001107 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1108 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001109 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001110}
1111
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001112void Call::OnStartRateUpdate(DataRate start_rate) {
1113 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1114 transport_send_ptr_->GetWorkerQueue()->PostTask(
1115 [this, start_rate] { this->OnStartRateUpdate(start_rate); });
1116 return;
1117 }
1118 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1119}
1120
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001121void Call::OnTargetTransferRate(TargetTransferRate msg) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001122 // TODO(bugs.webrtc.org/9719)
1123 // Call::OnTargetTransferRate requires that on target transfer rate is invoked
1124 // from the worker queue (because bitrate_allocator_ requires it). Media
1125 // transport does not guarantee the callback on the worker queue.
1126 // When the threading model for MediaTransportInterface is update, reconsider
1127 // changing this implementation.
1128 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1129 transport_send_ptr_->GetWorkerQueue()->PostTask(
1130 [this, msg] { this->OnTargetTransferRate(msg); });
1131 return;
1132 }
1133
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001134 uint32_t target_bitrate_bps = msg.target_rate.bps();
1135 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1136 uint8_t fraction_loss =
1137 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1138 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1139 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1140 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1141 {
1142 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1143 last_bandwidth_bps_ = bandwidth_bps;
1144 }
nisse559af382017-03-21 06:41:12 -07001145 // For controlling the rate of feedback messages.
1146 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +01001147 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, bandwidth_bps,
1148 fraction_loss, rtt_ms,
1149 probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001150
asaperssonce2e1362016-09-09 00:13:35 -07001151 // Ignore updates if bitrate is zero (the aggregate network state is down).
1152 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001153 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001154 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1155 pacer_bitrate_kbps_counter_.ProcessAndPause();
1156 return;
stefan18adf0a2015-11-17 06:24:56 -08001157 }
asaperssonce2e1362016-09-09 00:13:35 -07001158
1159 bool sending_video;
1160 {
1161 ReadLockScoped read_lock(*send_crit_);
1162 sending_video = !video_send_streams_.empty();
1163 }
1164
1165 rtc::CritScope lock(&bitrate_crit_);
1166 if (!sending_video) {
1167 // Do not update the stats if we are not sending video.
1168 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1169 pacer_bitrate_kbps_counter_.ProcessAndPause();
1170 return;
1171 }
1172 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1173 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1174 uint32_t pacer_bitrate_bps =
1175 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1176 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001177}
mflodman101f2502016-06-09 17:21:19 +02001178
perkj71ee44c2016-06-15 00:47:53 -07001179void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001180 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +01001181 uint32_t total_bitrate_bps,
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001182 uint32_t allocated_without_feedback_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +01001183 bool has_packet_feedback) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001184 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001185 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Janssone6256052018-05-04 14:08:15 +02001186 transport_send_ptr_->SetPerPacketFeedbackAvailable(has_packet_feedback);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001187 transport_send_ptr_->SetAllocatedBitrateWithoutFeedback(
1188 allocated_without_feedback_bps);
1189
perkj71ee44c2016-06-15 00:47:53 -07001190 rtc::CritScope lock(&bitrate_crit_);
1191 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001192 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001193}
1194
pbos8fc7fa72015-07-15 08:02:58 -07001195void Call::ConfigureSync(const std::string& sync_group) {
1196 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001197 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001198 return;
1199
1200 AudioReceiveStream* sync_audio_stream = nullptr;
1201 // Find existing audio stream.
1202 const auto it = sync_stream_mapping_.find(sync_group);
1203 if (it != sync_stream_mapping_.end()) {
1204 sync_audio_stream = it->second;
1205 } else {
1206 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001207 for (AudioReceiveStream* stream : audio_receive_streams_) {
1208 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001209 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001210 RTC_LOG(LS_WARNING)
1211 << "Attempting to sync more than one audio stream "
1212 "within the same sync group. This is not "
1213 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001214 break;
1215 }
nissee4bcd6d2017-05-16 04:47:04 -07001216 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001217 }
1218 }
1219 }
1220 if (sync_audio_stream)
1221 sync_stream_mapping_[sync_group] = sync_audio_stream;
1222 size_t num_synced_streams = 0;
1223 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1224 if (video_stream->config().sync_group != sync_group)
1225 continue;
1226 ++num_synced_streams;
1227 if (num_synced_streams > 1) {
1228 // TODO(pbos): Support synchronizing more than one A/V pair.
1229 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001230 RTC_LOG(LS_WARNING)
1231 << "Attempting to sync more than one audio/video pair "
1232 "within the same sync group. This is not supported in "
1233 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001234 }
1235 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001236 if (num_synced_streams == 1) {
1237 // sync_audio_stream may be null and that's ok.
1238 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001239 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001240 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001241 }
1242 }
1243}
1244
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001245PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1246 const uint8_t* packet,
1247 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001248 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001249 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001250 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1251 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001252 if (received_bytes_per_second_counter_.HasSample()) {
1253 // First RTP packet has been received.
1254 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1255 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1256 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001257 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001258 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001259 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001260 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001261 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001262 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001263 }
1264 }
1265 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1266 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001267 for (AudioReceiveStream* stream : audio_receive_streams_) {
1268 if (stream->DeliverRtcp(packet, length))
mflodman3d7db262016-04-29 00:57:13 -07001269 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001270 }
1271 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001272 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001273 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001274 for (VideoSendStream* stream : video_send_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001275 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001276 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001277 }
1278 }
mflodman3d7db262016-04-29 00:57:13 -07001279 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1280 ReadLockScoped read_lock(*send_crit_);
1281 for (auto& kv : audio_send_ssrcs_) {
1282 if (kv.second->DeliverRtcp(packet, length))
1283 rtcp_delivered = true;
1284 }
1285 }
1286
Elad Alon4a87e1c2017-10-03 16:11:34 +02001287 if (rtcp_delivered) {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001288 event_log_->Log(absl::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001289 rtc::MakeArrayView(packet, length)));
1290 }
mflodman3d7db262016-04-29 00:57:13 -07001291
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001292 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001293}
1294
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001295PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001296 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001297 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001298 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001299
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001300 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001301 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001302 return DELIVERY_PACKET_ERROR;
1303
Niels Möller70082872018-08-07 11:03:12 +02001304 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001305 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001306 // Repair packet_time_us for clock resets by comparing a new read of
1307 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001308 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001309 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001310 }
Niels Möller70082872018-08-07 11:03:12 +02001311 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001312 } else {
1313 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1314 }
nissed44ce052017-02-06 02:23:00 -08001315
sprangc1abde72017-07-11 03:56:21 -07001316 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1317 // These are empty (zero length payload) RTP packets with an unsignaled
1318 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001319 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001320
1321 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1322 is_keep_alive_packet);
1323
sprangc1abde72017-07-11 03:56:21 -07001324 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001325 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001326 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001327 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1328 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001329 // Destruction of the receive stream, including deregistering from the
1330 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1331 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1332 // So by not passing the packet on to demuxing in this case, we prevent
1333 // incoming packets to be passed on via the demuxer to a receive stream
1334 // which is being torned down.
1335 return DELIVERY_UNKNOWN_SSRC;
1336 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001337 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001338
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001339 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001340
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001341 // RateCounters expect input parameter as int, save it as int,
1342 // instead of converting each time it is passed to RateCounter::Add below.
1343 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001344 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001345 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001346 received_bytes_per_second_counter_.Add(length);
1347 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001348 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001349 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001350 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001351 if (!first_received_rtp_audio_ms_) {
1352 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1353 }
1354 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001355 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001356 }
nissee4bcd6d2017-05-16 04:47:04 -07001357 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001358 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001359 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001360 received_bytes_per_second_counter_.Add(length);
1361 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001362 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001363 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001364 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001365 if (!first_received_rtp_video_ms_) {
1366 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1367 }
1368 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001369 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001370 }
1371 }
1372 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001373}
1374
stefan68786d22015-09-08 05:36:15 -07001375PacketReceiver::DeliveryStatus Call::DeliverPacket(
1376 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001377 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001378 int64_t packet_time_us) {
eladalond1dd2f72017-08-25 02:55:57 -07001379 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001380 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1381 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001382
Niels Möller70082872018-08-07 11:03:12 +02001383 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001384}
1385
nissed2ef3142017-05-11 08:00:58 -07001386void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001387 RtpPacketReceived parsed_packet;
1388 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001389 return;
1390
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001391 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001392
brandtrcaea68f2017-08-23 00:55:17 -07001393 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001394 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001395 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001396 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1397 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001398 // Destruction of the receive stream, including deregistering from the
1399 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1400 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1401 // So by not passing the packet on to demuxing in this case, we prevent
1402 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001403 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001404 return;
1405 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001406 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001407
1408 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001409 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001410 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001411}
1412
nissed44ce052017-02-06 02:23:00 -08001413void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1414 MediaType media_type) {
1415 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001416 bool use_send_side_bwe =
1417 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001418
brandtrb29e6522016-12-21 06:37:18 -08001419 RTPHeader header;
1420 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001421
nisse4709e892017-02-07 01:18:43 -08001422 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001423 // Inconsistent configuration of send side BWE. Do nothing.
1424 // TODO(nisse): Without this check, we may produce RTCP feedback
1425 // packets even when not negotiated. But it would be cleaner to
1426 // move the check down to RTCPSender::SendFeedbackPacket, which
1427 // would also help the PacketRouter to select an appropriate rtp
1428 // module in the case that some, but not all, have RTCP feedback
1429 // enabled.
1430 return;
1431 }
1432 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001433 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001434 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001435 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001436 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1437 header);
1438 }
brandtrb29e6522016-12-21 06:37:18 -08001439}
1440
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001441} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001442
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001443} // namespace webrtc