blob: 8dcd785b8cd40d6840ad332e9b0fa29d059875f3 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:031/*
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:5311#include <string.h>
mflodman101f2502016-06-09 15:21:1912#include <algorithm>
pbos@webrtc.org29d58392013-05-16 12:08:0313#include <map>
kwibergb25345e2016-03-12 14:10:4414#include <memory>
ossuf515ab82016-12-07 12:52:5815#include <set>
brandtr25445d32016-10-24 06:37:1416#include <utility>
pbos@webrtc.org29d58392013-05-16 12:08:0317#include <vector>
18
Karl Wiberg918f50c2018-07-05 09:40:3319#include "absl/memory/memory.h"
Danil Chapovalovb9b146c2018-06-15 10:28:0720#include "absl/types/optional.h"
Sebastian Jansson74682c12019-03-01 10:50:2021#include "api/task_queue/global_task_queue_factory.h"
Sebastian Janssonc6c44262018-05-09 08:33:3922#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "audio/audio_receive_stream.h"
24#include "audio/audio_send_stream.h"
25#include "audio/audio_state.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "call/bitrate_allocator.h"
27#include "call/call.h"
28#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 13:38:3229#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "call/rtp_stream_receiver_controller.h"
31#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 14:11:3432#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 14:11:3433#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
34#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
35#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
36#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3137#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon99a81b62017-09-21 08:25:2938#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3139#include "modules/bitrate_controller/include/bitrate_controller.h"
40#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
41#include "modules/rtp_rtcp/include/flexfec_receiver.h"
42#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
43#include "modules/rtp_rtcp/include/rtp_header_parser.h"
44#include "modules/rtp_rtcp/source/byte_io.h"
45#include "modules/rtp_rtcp/source/rtp_packet_received.h"
46#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 16:58:5747#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3148#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0049#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3150#include "rtc_base/location.h"
51#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 14:59:1252#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3153#include "rtc_base/sequenced_task_checker.h"
Jonas Olsson0a713b62018-04-04 13:49:3254#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 08:33:3955#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3156#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 17:11:0057#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3158#include "rtc_base/trace_event.h"
59#include "system_wrappers/include/clock.h"
60#include "system_wrappers/include/cpu_info.h"
Jonas Oreland6d835922019-03-18 09:59:4061#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3162#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3163#include "video/call_stats.h"
64#include "video/send_delay_stats.h"
65#include "video/stats_counter.h"
66#include "video/video_receive_stream.h"
67#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:0368
69namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:2570
nisse4709e892017-02-07 09:18:4371namespace {
Johannes Kron7ff164e2019-02-07 11:50:1872bool SendFeedbackOnRequestOnly(const std::vector<RtpExtension>& extensions) {
73 for (const auto& extension : extensions) {
74 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
75 return true;
76 }
77 return false;
78}
79
nisse4709e892017-02-07 09:18:4380// TODO(nisse): This really begs for a shared context struct.
81bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
82 bool transport_cc) {
83 if (!transport_cc)
84 return false;
85 for (const auto& extension : extensions) {
Johannes Kron7ff164e2019-02-07 11:50:1886 if (extension.uri == RtpExtension::kTransportSequenceNumberUri ||
87 extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
nisse4709e892017-02-07 09:18:4388 return true;
89 }
90 return false;
91}
92
93bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
94 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
95}
96
97bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
98 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
99}
100
101bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
102 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
103}
104
nisse26e3abb2017-08-25 11:44:25105const int* FindKeyByValue(const std::map<int, int>& m, int v) {
106 for (const auto& kv : m) {
107 if (kv.second == v)
108 return &kv.first;
109 }
110 return nullptr;
111}
112
eladalon8ec568a2017-09-08 13:15:52113std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 10:26:49114 const VideoReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 09:40:33115 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 13:15:52116 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
117 rtclog_config->local_ssrc = config.rtp.local_ssrc;
118 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
119 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
120 rtclog_config->remb = config.rtp.remb;
121 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 10:26:49122
123 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 11:44:25124 const int* search =
125 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 13:56:04126 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 13:03:05127 search ? *search : 0);
perkj09e71da2017-05-22 10:26:49128 }
129 return rtclog_config;
130}
131
eladalon8ec568a2017-09-08 13:15:52132std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 11:08:28133 const VideoSendStream::Config& config,
134 size_t ssrc_index) {
Karl Wiberg918f50c2018-07-05 09:40:33135 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 13:15:52136 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 11:08:28137 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 13:15:52138 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 11:08:28139 }
eladalon8ec568a2017-09-08 13:15:52140 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
141 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 11:08:28142
Niels Möller259a4972018-04-05 13:36:51143 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
144 config.rtp.payload_type,
eladalon8ec568a2017-09-08 13:15:52145 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 11:08:28146 return rtclog_config;
147}
148
eladalon8ec568a2017-09-08 13:15:52149std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 16:36:28150 const AudioReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 09:40:33151 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 13:15:52152 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
153 rtclog_config->local_ssrc = config.rtp.local_ssrc;
154 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 16:36:28155 return rtclog_config;
156}
157
nisse4709e892017-02-07 09:18:43158} // namespace
159
pbos@webrtc.org16e03b72013-10-28 16:32:01160namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07161
Sebastian Janssone6256052018-05-04 12:08:15162class Call final : public webrtc::Call,
163 public PacketReceiver,
164 public RecoveredPacketReceiver,
165 public TargetTransferRateObserver,
166 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01167 public:
Sebastian Jansson4e5f5ed2019-03-01 17:13:27168 Call(Clock* clock,
169 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 17:48:16170 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
171 std::unique_ptr<ProcessThread> module_process_thread,
172 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 14:30:18173 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01174
brandtr25445d32016-10-24 06:37:14175 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35176 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01177
Fredrik Solenberg04f49312015-06-08 11:04:56178 webrtc::AudioSendStream* CreateAudioSendStream(
179 const webrtc::AudioSendStream::Config& config) override;
180 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
181
Fredrik Solenberg23fba1f2015-04-29 13:24:01182 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
183 const webrtc::AudioReceiveStream::Config& config) override;
184 void DestroyAudioReceiveStream(
185 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01186
Fredrik Solenberg23fba1f2015-04-29 13:24:01187 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 08:17:40188 webrtc::VideoSendStream::Config config,
189 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 16:58:57190 webrtc::VideoSendStream* CreateVideoSendStream(
191 webrtc::VideoSendStream::Config config,
192 VideoEncoderConfig encoder_config,
193 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35194 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01195
Fredrik Solenberg23fba1f2015-04-29 13:24:01196 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 15:58:01197 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35198 void DestroyVideoReceiveStream(
199 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01200
brandtr7250b392016-12-19 09:13:46201 FlexfecReceiveStream* CreateFlexfecReceiveStream(
202 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-24 06:37:14203 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 09:13:46204 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-24 06:37:14205
Sebastian Jansson8f83b422018-02-21 12:07:13206 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
207
kjellander@webrtc.org14665ff2015-03-04 12:58:35208 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01209
brandtr25445d32016-10-24 06:37:14210 // Implements PacketReceiver.
stefan68786d22015-09-08 12:36:15211 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 16:00:40212 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 09:03:12213 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01214
brandtr4e523862016-10-19 06:50:45215 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 15:00:58216 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-19 06:50:45217
Alex Narest78609d52017-10-20 08:37:47218 void SetBitrateAllocationStrategy(
219 std::unique_ptr<rtc::BitrateAllocationStrategy>
220 bitrate_allocation_strategy) override;
221
skvlad7a43d252016-03-22 22:32:27222 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12223
Stefan Holmer64be7fa2018-10-04 13:21:55224 void OnAudioTransportOverheadChanged(
225 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 10:50:09226
stefanc1aeaf02015-10-15 14:26:07227 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
228
Sebastian Jansson19704ec2018-03-12 14:59:12229 // Implements TargetTransferRateObserver,
230 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 14:02:47231 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-13 05:02:42232
perkj71ee44c2016-06-15 07:47:53233 // Implements BitrateAllocator::LimitObserver.
234 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 12:06:28235 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 08:41:43236 uint32_t total_bitrate_bps) override;
perkj71ee44c2016-06-15 07:47:53237
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19238 // This method is invoked when the media transport is created and when the
239 // media transport is being destructed.
240 // We only allow one media transport per connection.
241 //
242 // It should be called with non-null argument at most once, and if it was
243 // called with non-null argument, it has to be called with a null argument
244 // at least once after that.
245 void MediaTransportChange(MediaTransportInterface* media_transport) override;
246
Piotr (Peter) Slatala7fbfaa42019-03-18 17:31:54247 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
248
pbos@webrtc.org16e03b72013-10-28 16:32:01249 private:
Yves Gerey665174f2018-06-19 13:03:05250 DeliveryStatus DeliverRtcp(MediaType media_type,
251 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 13:24:01252 size_t length);
stefan68786d22015-09-08 12:36:15253 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 16:00:40254 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 09:03:12255 int64_t packet_time_us);
pbos8fc7fa72015-07-15 15:02:58256 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 11:17:22257 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 15:02:58258
nissed44ce052017-02-06 10:23:00259 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
260 MediaType media_type)
danilchapa37de392017-09-09 11:17:22261 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 10:23:00262
asaperssonfc5e81c2017-04-20 06:28:53263 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 11:17:22264 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 14:24:56265 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 07:39:09266 void UpdateHistograms();
skvlad7a43d252016-03-22 22:32:27267 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 18:13:02268
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19269 // If |media_transport| is not null, it registers the rate observer for the
270 // media transport.
271 void RegisterRateObserver() RTC_LOCKS_EXCLUDED(target_observer_crit_);
272
Niels Möller46879152019-01-07 14:54:47273 // Intended for DCHECKs, to avoid locking in production builds.
274 MediaTransportInterface* media_transport()
275 RTC_LOCKS_EXCLUDED(target_observer_crit_);
276
Peter Boströmd3c94472015-12-09 10:20:58277 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 17:48:16278 TaskQueueFactory* const task_queue_factory_;
stefan91d92602015-11-11 18:13:02279
Piotr (Peter) Slatala7fbfaa42019-03-18 17:31:54280 // Caching the last SetBitrate for media transport.
281 absl::optional<MediaTransportTargetRateConstraints> last_set_bitrate_
282 RTC_GUARDED_BY(&target_observer_crit_);
Peter Boström45553ae2015-05-08 11:54:38283 const int num_cpu_cores_;
kwibergb25345e2016-03-12 14:10:44284 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 14:10:44285 const std::unique_ptr<CallStats> call_stats_;
286 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01287 Call::Config config_;
eladalonf3f5c0e2017-08-18 09:47:08288 rtc::SequencedTaskChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01289
skvlad7a43d252016-03-22 22:32:27290 NetworkState audio_network_state_;
291 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 17:49:55292 rtc::CriticalSection aggregate_network_up_crit_;
293 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01294
kwibergb25345e2016-03-12 14:10:44295 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-24 06:37:14296 // Audio, Video, and FlexFEC receive streams are owned by the client that
297 // creates them.
nissee4bcd6d2017-05-16 11:47:04298 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 11:17:22299 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 13:24:01300 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 11:17:22301 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 11:47:04302
pbos8fc7fa72015-07-15 15:02:58303 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 11:17:22304 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12305
nisse0f15f922017-06-21 08:05:22306 // TODO(nisse): Should eventually be injected at creation,
307 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 16:25:27308 RtpStreamReceiverController audio_receiver_controller_;
309 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 11:47:04310
nissed44ce052017-02-06 10:23:00311 // This extra map is used for receive processing which is
312 // independent of media type.
313
314 // TODO(nisse): In the RTP transport refactoring, we should have a
315 // single mapping from ssrc to a more abstract receive stream, with
316 // accessor methods for all configuration we need at this level.
317 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 14:16:50318 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
319 : extensions(config.rtp.extensions),
320 use_send_side_bwe(UseSendSideBwe(config)) {}
321 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
322 : extensions(config.rtp.extensions),
323 use_send_side_bwe(UseSendSideBwe(config)) {}
324 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
325 : extensions(config.rtp_header_extensions),
326 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 10:23:00327
328 // Registered RTP header extensions for each stream. Note that RTP header
329 // extensions are negotiated per track ("m= line") in the SDP, but we have
330 // no notion of tracks at the Call level. We therefore store the RTP header
331 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 14:16:50332 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 09:18:43333 // Set if both RTP extension the RTCP feedback message needed for
334 // send side BWE are negotiated.
Erik Språng09708512018-03-14 14:16:50335 const bool use_send_side_bwe;
nissed44ce052017-02-06 10:23:00336 };
337 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 11:17:22338 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 14:37:18339
kwibergb25345e2016-03-12 14:10:44340 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 21:35:07341 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 11:17:22342 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
343 RTC_GUARDED_BY(send_crit_);
344 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
345 RTC_GUARDED_BY(send_crit_);
346 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01347
ossuc3d4b482017-05-23 13:07:11348 using RtpStateMap = std::map<uint32_t, RtpState>;
349 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 11:17:22350 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 13:07:11351 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 11:17:22352 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 13:07:11353
Åsa Persson4bece9a2017-10-06 08:04:04354 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
355 RtpPayloadStateMap suspended_video_payload_states_
356 RTC_GUARDED_BY(configuration_sequence_checker_);
357
skvlad11a9cbf2016-10-07 18:53:05358 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 07:09:43359
stefan18adf0a2015-11-17 14:24:56360 // The following members are only accessed (exclusively) from one thread and
361 // from the destructor, and therefore doesn't need any explicit
362 // synchronization.
asapersson250fd972016-09-08 07:07:21363 RateCounter received_bytes_per_second_counter_;
364 RateCounter received_audio_bytes_per_second_counter_;
365 RateCounter received_video_bytes_per_second_counter_;
366 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 10:28:07367 absl::optional<int64_t> first_received_rtp_audio_ms_;
368 absl::optional<int64_t> last_received_rtp_audio_ms_;
369 absl::optional<int64_t> first_received_rtp_video_ms_;
370 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 18:13:02371
Sebastian Jansson19704ec2018-03-12 14:59:12372 rtc::CriticalSection last_bandwidth_bps_crit_;
373 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 14:24:56374 // TODO(holmer): Remove this lock once BitrateController no longer calls
375 // OnNetworkChanged from multiple threads.
376 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 11:17:22377 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
378 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
379 AvgCounter estimated_send_bitrate_kbps_counter_
380 RTC_GUARDED_BY(&bitrate_crit_);
381 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 14:24:56382
nisse559af382017-03-21 13:41:12383 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 13:38:32384
385 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
386
asapersson35151f32016-05-03 06:44:01387 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 07:39:09388 const int64_t start_ms_;
mflodman0e7e2592015-11-13 05:02:42389
Sebastian Janssone6256052018-05-04 12:08:15390 // Caches transport_send_.get(), to avoid racing with destructor.
391 // Note that this is declared before transport_send_ to ensure that it is not
392 // invalidated until no more tasks can be running on the transport_send_ task
393 // queue.
394 RtpTransportControllerSendInterface* transport_send_ptr_;
395 // Declared last since it will issue callbacks from a task queue. Declaring it
396 // last ensures that it is destroyed first and any running tasks are finished.
397 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19398
399 // This is a precaution, since |MediaTransportChange| is not guaranteed to be
400 // invoked on a particular thread.
401 rtc::CriticalSection target_observer_crit_;
402 bool is_target_rate_observer_registered_
403 RTC_GUARDED_BY(&target_observer_crit_) = false;
404 MediaTransportInterface* media_transport_
405 RTC_GUARDED_BY(&target_observer_crit_) = nullptr;
406
Jonas Oreland6d835922019-03-18 09:59:40407 const bool field_trial_webrtc_video_buffer_packets_with_unknown_ssrc_;
408
henrikg3c089d72015-09-16 12:37:44409 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01410};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47411} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52412
asapersson2e5cfcd2016-08-11 15:41:18413std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 13:49:32414 char buf[1024];
415 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 15:41:18416 ss << "Call stats: " << time_ms << ", {";
417 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
418 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
419 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
420 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
421 ss << "rtt_ms: " << rtt_ms;
422 ss << '}';
423 return ss.str();
424}
425
stefan@webrtc.org7e9315b2013-12-04 10:24:26426Call* Call::Create(const Call::Config& config) {
Sebastian Jansson4e5f5ed2019-03-01 17:13:27427 return Create(
428 config, Clock::GetRealTimeClock(), ProcessThread::Create("PacerThread"),
429 ProcessThread::Create("ModuleProcessThread"), &GlobalTaskQueueFactory());
Sebastian Jansson896b47c2019-03-01 17:48:16430}
431
432Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 17:13:27433 Clock* clock,
Sebastian Jansson896b47c2019-03-01 17:48:16434 std::unique_ptr<ProcessThread> call_thread,
435 std::unique_ptr<ProcessThread> pacer_thread,
436 TaskQueueFactory* task_queue_factory) {
Sebastian Jansson97f61ea2018-02-21 12:01:55437 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 17:13:27438 clock, config,
Sebastian Janssoned50e6c2019-03-01 13:45:21439 absl::make_unique<RtpTransportControllerSend>(
Sebastian Jansson4e5f5ed2019-03-01 17:13:27440 clock, config.event_log, config.network_controller_factory,
441 config.bitrate_config, std::move(pacer_thread), task_queue_factory),
Sebastian Jansson896b47c2019-03-01 17:48:16442 std::move(call_thread), task_queue_factory);
zstein7cb69d52017-05-08 18:52:38443}
444
Ying Wang0dd1b0a2018-02-20 11:50:27445// This method here to avoid subclasses has to implement this method.
446// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
447// FecController.
Ying Wang3b790f32018-01-19 16:58:57448VideoSendStream* Call::CreateVideoSendStream(
449 VideoSendStream::Config config,
450 VideoEncoderConfig encoder_config,
451 std::unique_ptr<FecController> fec_controller) {
452 return nullptr;
453}
454
pbos@webrtc.org29d58392013-05-16 12:08:03455namespace internal {
456
Sebastian Jansson4e5f5ed2019-03-01 17:13:27457Call::Call(Clock* clock,
458 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 17:48:16459 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
460 std::unique_ptr<ProcessThread> module_process_thread,
461 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 17:13:27462 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 17:48:16463 task_queue_factory_(task_queue_factory),
stefan91d92602015-11-11 18:13:02464 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 17:48:16465 module_process_thread_(std::move(module_process_thread)),
Tommi38c5d932018-03-27 21:11:09466 call_stats_(new CallStats(clock_, module_process_thread_.get())),
Sebastian Janssonda6806c2019-03-04 16:05:12467 bitrate_allocator_(new BitrateAllocator(clock_, this)),
Peter Boström45553ae2015-05-08 11:54:38468 config_(config),
Sergey Ulanove2b15012016-11-23 00:08:30469 audio_network_state_(kNetworkDown),
470 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 17:49:55471 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12472 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 18:13:02473 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 18:53:05474 event_log_(config.event_log),
asapersson250fd972016-09-08 07:07:21475 received_bytes_per_second_counter_(clock_, nullptr, true),
476 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
477 received_video_bytes_per_second_counter_(clock_, nullptr, true),
478 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 14:59:12479 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 07:47:53480 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 07:54:28481 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 07:13:35482 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
483 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-19 06:38:35484 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 13:38:32485 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 07:39:09486 video_send_delay_stats_(new SendDelayStats(clock_)),
Jonas Oreland6d835922019-03-18 09:59:40487 start_ms_(clock_->TimeInMilliseconds()),
488 field_trial_webrtc_video_buffer_packets_with_unknown_ssrc_(
489 webrtc::field_trial::IsEnabled(
490 "WebRTC-Video-BufferPacketsWithUnknownSsrc")) {
skvlad11a9cbf2016-10-07 18:53:05491 RTC_DCHECK(config.event_log != nullptr);
nisse6167b262017-04-06 13:34:25492 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 12:08:15493 transport_send_ptr_ = transport_send_.get();
pbos@webrtc.org29d58392013-05-16 12:08:03494}
495
pbos@webrtc.org841c8a42013-09-09 15:04:25496Call::~Call() {
eladalonf3f5c0e2017-08-18 09:47:08497 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
perkj26091b12016-09-01 08:17:40498
solenbergc7a8b082015-10-16 21:35:07499 RTC_CHECK(audio_send_ssrcs_.empty());
500 RTC_CHECK(video_send_ssrcs_.empty());
501 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 11:47:04502 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 21:35:07503 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23504
Piotr (Peter) Slatalab2757882018-12-18 19:17:09505 if (!media_transport_) {
506 module_process_thread_->DeRegisterModule(
507 receive_side_cc_.GetRemoteBitrateEstimator(true));
508 module_process_thread_->DeRegisterModule(&receive_side_cc_);
509 module_process_thread_->DeRegisterModule(call_stats_.get());
510 module_process_thread_->Stop();
511 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 19:17:09512 }
sprang6d6122b2016-07-13 13:37:09513
Sebastian Janssone4be6da2018-02-15 15:51:41514 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 13:37:09515 // Only update histograms after process threads have been shut down, so that
516 // they won't try to concurrently update stats.
perkj26091b12016-09-01 08:17:40517 {
518 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-20 06:28:53519 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 08:17:40520 }
sprang6d6122b2016-07-13 13:37:09521 UpdateReceiveHistograms();
asapersson4374a092016-07-27 07:39:09522 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03523}
524
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19525void Call::RegisterRateObserver() {
526 rtc::CritScope lock(&target_observer_crit_);
527
528 if (is_target_rate_observer_registered_) {
529 return;
530 }
531
532 is_target_rate_observer_registered_ = true;
533
534 if (media_transport_) {
Piotr (Peter) Slatalab2757882018-12-18 19:17:09535 // TODO(bugs.webrtc.org/9719): We should report call_stats_ from
536 // media transport (at least Rtt). We should extend media transport
537 // interface to include "receive_side bwe" if needed.
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19538 media_transport_->AddTargetTransferRateObserver(this);
539 } else {
540 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 19:17:09541
542 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 19:17:09543
544 module_process_thread_->RegisterModule(
545 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
546 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
547 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
548 module_process_thread_->Start();
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19549 }
550}
551
Niels Möller46879152019-01-07 14:54:47552MediaTransportInterface* Call::media_transport() {
553 rtc::CritScope lock(&target_observer_crit_);
554 return media_transport_;
555}
556
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19557void Call::MediaTransportChange(MediaTransportInterface* media_transport) {
558 rtc::CritScope lock(&target_observer_crit_);
559
560 if (is_target_rate_observer_registered_) {
561 // Only used to unregister rate observer from media transport. Registration
562 // happens when the stream is created.
563 if (!media_transport && media_transport_) {
564 media_transport_->RemoveTargetTransferRateObserver(this);
565 media_transport_ = nullptr;
566 is_target_rate_observer_registered_ = false;
567 }
568 } else if (media_transport) {
569 RTC_DCHECK(media_transport_ == nullptr ||
570 media_transport_ == media_transport)
571 << "media_transport_=" << (media_transport_ != nullptr)
572 << ", (media_transport_==media_transport)="
573 << (media_transport_ == media_transport);
574 media_transport_ = media_transport;
Piotr (Peter) Slatala946b9682019-03-18 17:25:02575 MediaTransportTargetRateConstraints constraints;
576 if (config_.bitrate_config.start_bitrate_bps > 0) {
577 constraints.starting_bitrate =
578 DataRate::bps(config_.bitrate_config.start_bitrate_bps);
579 }
580 if (config_.bitrate_config.max_bitrate_bps > 0) {
581 constraints.max_bitrate =
582 DataRate::bps(config_.bitrate_config.max_bitrate_bps);
583 }
584 if (config_.bitrate_config.min_bitrate_bps > 0) {
585 constraints.min_bitrate =
586 DataRate::bps(config_.bitrate_config.min_bitrate_bps);
587 }
Piotr (Peter) Slatala7fbfaa42019-03-18 17:31:54588
589 // User called ::SetBitrate on peer connection before
590 // media transport was created.
591 if (last_set_bitrate_) {
592 media_transport_->SetTargetBitrateLimits(*last_set_bitrate_);
593 } else {
594 media_transport_->SetTargetBitrateLimits(constraints);
595 }
596 }
597}
598
599void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
600 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
601 // Can the client code invoke 'SetBitrate' before media transport is created?
602 // It's probably possible :/
603 MediaTransportTargetRateConstraints constraints;
604 if (preferences.start_bitrate_bps.has_value()) {
605 constraints.starting_bitrate =
606 webrtc::DataRate::bps(*preferences.start_bitrate_bps);
607 }
608 if (preferences.max_bitrate_bps.has_value()) {
609 constraints.max_bitrate =
610 webrtc::DataRate::bps(*preferences.max_bitrate_bps);
611 }
612 if (preferences.min_bitrate_bps.has_value()) {
613 constraints.min_bitrate =
614 webrtc::DataRate::bps(*preferences.min_bitrate_bps);
615 }
616 rtc::CritScope lock(&target_observer_crit_);
617 last_set_bitrate_ = constraints;
618 if (media_transport_) {
Piotr (Peter) Slatala946b9682019-03-18 17:25:02619 media_transport_->SetTargetBitrateLimits(constraints);
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19620 }
621}
622
asapersson4374a092016-07-27 07:39:09623void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-10 05:40:25624 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 07:39:09625 "WebRTC.Call.LifetimeInSeconds",
626 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
627}
628
asaperssonfc5e81c2017-04-20 06:28:53629void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
630 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 14:24:56631 return;
632 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-20 06:28:53633 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 14:24:56634 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
635 return;
asaperssonce2e1362016-09-09 07:13:35636 const int kMinRequiredPeriodicSamples = 5;
637 AggregatedStats send_bitrate_stats =
638 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
639 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-10 05:40:25640 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
641 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 10:09:25642 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
643 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 14:24:56644 }
asaperssonce2e1362016-09-09 07:13:35645 AggregatedStats pacer_bitrate_stats =
646 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
647 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-10 05:40:25648 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
649 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 10:09:25650 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
651 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 14:24:56652 }
653}
654
655void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 11:05:06656 if (first_received_rtp_audio_ms_) {
657 RTC_HISTOGRAM_COUNTS_100000(
658 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
659 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
660 }
661 if (first_received_rtp_video_ms_) {
662 RTC_HISTOGRAM_COUNTS_100000(
663 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
664 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
665 }
asapersson250fd972016-09-08 07:07:21666 const int kMinRequiredPeriodicSamples = 5;
667 AggregatedStats video_bytes_per_sec =
668 received_video_bytes_per_second_counter_.GetStats();
669 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-10 05:40:25670 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
671 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 10:09:25672 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
673 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 18:13:02674 }
asapersson250fd972016-09-08 07:07:21675 AggregatedStats audio_bytes_per_sec =
676 received_audio_bytes_per_second_counter_.GetStats();
677 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-10 05:40:25678 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
679 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 10:09:25680 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
681 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 18:13:02682 }
asapersson250fd972016-09-08 07:07:21683 AggregatedStats rtcp_bytes_per_sec =
684 received_rtcp_bytes_per_second_counter_.GetStats();
685 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-10 05:40:25686 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
687 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 10:09:25688 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
689 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 18:13:02690 }
asapersson250fd972016-09-08 07:07:21691 AggregatedStats recv_bytes_per_sec =
692 received_bytes_per_second_counter_.GetStats();
693 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-10 05:40:25694 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
695 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 10:09:25696 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
697 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 07:07:21698 }
stefan91d92602015-11-11 18:13:02699}
700
solenberg5a289392015-10-19 10:39:20701PacketReceiver* Call::Receiver() {
eladalond1dd2f72017-08-25 09:55:57702 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenberg5a289392015-10-19 10:39:20703 return this;
704}
pbos@webrtc.org29d58392013-05-16 12:08:03705
Fredrik Solenberg04f49312015-06-08 11:04:56706webrtc::AudioSendStream* Call::CreateAudioSendStream(
707 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 21:35:07708 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
eladalonf3f5c0e2017-08-18 09:47:08709 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19710
Niels Möller46879152019-01-07 14:54:47711 RTC_DCHECK(media_transport() == config.media_transport);
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19712
713 RegisterRateObserver();
714
Oskar Sundbom56ef3052018-10-30 15:11:02715 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
716 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 10:28:07717 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 13:07:11718 {
719 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
720 if (iter != suspended_audio_send_ssrcs_.end()) {
721 suspended_rtp_state.emplace(iter->second);
722 }
723 }
724
Sebastian Jansson44dd9f22019-03-08 13:50:30725 AudioSendStream* send_stream =
726 new AudioSendStream(clock_, config, config_.audio_state,
727 task_queue_factory_, module_process_thread_.get(),
728 transport_send_ptr_, bitrate_allocator_.get(),
729 event_log_, call_stats_.get(), suspended_rtp_state);
solenbergc7a8b082015-10-16 21:35:07730 {
solenbergc7a8b082015-10-16 21:35:07731 WriteLockScoped write_lock(*send_crit_);
732 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
733 audio_send_ssrcs_.end());
734 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 21:35:07735 }
solenberg7602aab2016-11-14 19:30:07736 {
737 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 11:47:04738 for (AudioReceiveStream* stream : audio_receive_streams_) {
739 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
740 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 19:30:07741 }
742 }
743 }
skvlad7a43d252016-03-22 22:32:27744 send_stream->SignalNetworkState(audio_network_state_);
745 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 21:35:07746 return send_stream;
Fredrik Solenberg04f49312015-06-08 11:04:56747}
748
749void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 21:35:07750 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
eladalonf3f5c0e2017-08-18 09:47:08751 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 21:35:07752 RTC_DCHECK(send_stream != nullptr);
753
754 send_stream->Stop();
755
eladalonabbc4302017-07-26 09:09:44756 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 21:35:07757 webrtc::internal::AudioSendStream* audio_send_stream =
758 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 13:07:11759 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 21:35:07760 {
761 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 19:30:07762 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
763 RTC_DCHECK_EQ(1, num_deleted);
764 }
765 {
766 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 11:47:04767 for (AudioReceiveStream* stream : audio_receive_streams_) {
768 if (stream->config().rtp.local_ssrc == ssrc) {
769 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 19:30:07770 }
771 }
solenbergc7a8b082015-10-16 21:35:07772 }
skvlad7a43d252016-03-22 22:32:27773 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 09:09:44774 delete send_stream;
Fredrik Solenberg04f49312015-06-08 11:04:56775}
776
Fredrik Solenberg23fba1f2015-04-29 13:24:01777webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
778 const webrtc::AudioReceiveStream::Config& config) {
779 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
eladalonf3f5c0e2017-08-18 09:47:08780 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Piotr (Peter) Slatalab2757882018-12-18 19:17:09781 RegisterRateObserver();
Karl Wiberg918f50c2018-07-05 09:40:33782 event_log_->Log(absl::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 14:11:34783 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 08:05:22784 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 16:43:34785 clock_, &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 12:52:30786 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 13:24:01787 {
788 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 14:16:50789 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
790 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 11:47:04791 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 10:23:00792
pbos8fc7fa72015-07-15 15:02:58793 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 13:24:01794 }
solenberg7602aab2016-11-14 19:30:07795 {
796 ReadLockScoped read_lock(*send_crit_);
797 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
798 if (it != audio_send_ssrcs_.end()) {
799 receive_stream->AssociateSendStream(it->second);
800 }
801 }
skvlad7a43d252016-03-22 22:32:27802 receive_stream->SignalNetworkState(audio_network_state_);
803 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 13:24:01804 return receive_stream;
805}
806
807void Call::DestroyAudioReceiveStream(
808 webrtc::AudioReceiveStream* receive_stream) {
809 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
eladalonf3f5c0e2017-08-18 09:47:08810 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 07:24:34811 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 21:35:07812 webrtc::internal::AudioReceiveStream* audio_receive_stream =
813 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 13:24:01814 {
815 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 09:18:43816 const AudioReceiveStream::Config& config = audio_receive_stream->config();
817 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 13:41:12818 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 09:18:43819 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 11:47:04820 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 15:02:58821 const std::string& sync_group = audio_receive_stream->config().sync_group;
822 const auto it = sync_stream_mapping_.find(sync_group);
823 if (it != sync_stream_mapping_.end() &&
824 it->second == audio_receive_stream) {
825 sync_stream_mapping_.erase(it);
826 ConfigureSync(sync_group);
827 }
nissed44ce052017-02-06 10:23:00828 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 13:24:01829 }
skvlad7a43d252016-03-22 22:32:27830 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 13:24:01831 delete audio_receive_stream;
832}
833
Ying Wang0dd1b0a2018-02-20 11:50:27834// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 16:58:57835webrtc::VideoSendStream* Call::CreateVideoSendStream(
836 webrtc::VideoSendStream::Config config,
837 VideoEncoderConfig encoder_config,
838 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07839 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
eladalonf3f5c0e2017-08-18 09:47:08840 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26841
Niels Möller46879152019-01-07 14:54:47842 RTC_DCHECK(media_transport() == config.media_transport);
843
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:19844 RegisterRateObserver();
845
asapersson35151f32016-05-03 06:44:01846 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 11:08:28847 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
848 ++ssrc_index) {
Karl Wiberg918f50c2018-07-05 09:40:33849 event_log_->Log(absl::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 14:11:34850 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 11:08:28851 }
perkj26091b12016-09-01 08:17:40852
mflodman@webrtc.orgeb16b812014-06-16 08:57:39853 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
854 // the call has already started.
perkj26091b12016-09-01 08:17:40855 // Copy ssrcs from |config| since |config| is moved.
856 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 11:50:27857
mflodman0c478b32015-10-21 13:52:16858 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Jansson0b698262019-03-07 08:17:19859 clock_, num_cpu_cores_, module_process_thread_.get(), task_queue_factory_,
Sebastian Jansson74682c12019-03-01 10:50:20860 call_stats_.get(), transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-19 06:38:35861 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 08:04:04862 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 14:03:46863 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 08:17:40864
skvlad7a43d252016-03-22 22:32:27865 {
866 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 08:17:40867 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 22:32:27868 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
869 video_send_ssrcs_[ssrc] = send_stream;
870 }
871 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03872 }
skvlad7a43d252016-03-22 22:32:27873 UpdateAggregateNetworkState();
perkj26091b12016-09-01 08:17:40874
pbos@webrtc.org29d58392013-05-16 12:08:03875 return send_stream;
876}
877
Ying Wang0dd1b0a2018-02-20 11:50:27878webrtc::VideoSendStream* Call::CreateVideoSendStream(
879 webrtc::VideoSendStream::Config config,
880 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 14:44:23881 if (config_.fec_controller_factory) {
882 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
883 }
Ying Wang0dd1b0a2018-02-20 11:50:27884 std::unique_ptr<FecController> fec_controller =
885 config_.fec_controller_factory
886 ? config_.fec_controller_factory->CreateFecController()
Karl Wiberg918f50c2018-07-05 09:40:33887 : absl::make_unique<FecControllerDefault>(Clock::GetRealTimeClock());
Ying Wang0dd1b0a2018-02-20 11:50:27888 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
889 std::move(fec_controller));
890}
891
pbos@webrtc.org2c46f8d2013-11-21 13:49:43892void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07893 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 07:24:34894 RTC_DCHECK(send_stream != nullptr);
eladalonf3f5c0e2017-08-18 09:47:08895 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54896
pbos@webrtc.org2bb1bda2014-07-07 13:06:48897 send_stream->Stop();
898
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24899 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54900 {
pbos@webrtc.org26c0c412014-09-03 16:17:12901 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 13:24:01902 auto it = video_send_ssrcs_.begin();
903 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54904 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
905 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 13:24:01906 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48907 } else {
908 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54909 }
910 }
Fredrik Solenberg23fba1f2015-04-29 13:24:01911 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03912 }
henrikg91d6ede2015-09-17 07:24:34913 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54914
Åsa Persson4bece9a2017-10-06 08:04:04915 VideoSendStream::RtpStateMap rtp_states;
916 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
917 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
918 &rtp_payload_states);
919 for (const auto& kv : rtp_states) {
920 suspended_video_send_ssrcs_[kv.first] = kv.second;
921 }
922 for (const auto& kv : rtp_payload_states) {
923 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48924 }
925
skvlad7a43d252016-03-22 22:32:27926 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54927 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03928}
929
Fredrik Solenberg23fba1f2015-04-29 13:24:01930webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 15:58:01931 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07932 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
eladalonf3f5c0e2017-08-18 09:47:08933 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 14:47:55934
Johannes Kron7ff164e2019-02-07 11:50:18935 receive_side_cc_.SetSendFeedbackOnRequestOnly(
936 SendFeedbackOnRequestOnly(configuration.rtp.extensions));
937
Piotr (Peter) Slatalab2757882018-12-18 19:17:09938 RegisterRateObserver();
939
nisse0f15f922017-06-21 08:05:22940 VideoReceiveStream* receive_stream = new VideoReceiveStream(
Sebastian Jansson896b47c2019-03-01 17:48:16941 task_queue_factory_, &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 12:08:15942 transport_send_ptr_->packet_router(), std::move(configuration),
Sebastian Jansson8026d602019-03-04 18:39:01943 module_process_thread_.get(), call_stats_.get(), clock_);
Tommi733b5472016-06-10 15:58:01944
945 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 22:32:27946 {
947 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 10:23:00948 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 10:23:00949 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 12:36:15950 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 10:23:00951 // type, we may get an incorrect value for the rtx stream, but
952 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 14:16:50953 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
954 ReceiveRtpConfig(config));
nissed44ce052017-02-06 10:23:00955 }
Erik Språng09708512018-03-14 14:16:50956 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
957 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 22:32:27958 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 22:32:27959 ConfigureSync(config.sync_group);
960 }
961 receive_stream->SignalNetworkState(video_network_state_);
962 UpdateAggregateNetworkState();
Karl Wiberg918f50c2018-07-05 09:40:33963 event_log_->Log(absl::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 14:11:34964 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03965 return receive_stream;
966}
967
pbos@webrtc.org2c46f8d2013-11-21 13:49:43968void Call::DestroyVideoReceiveStream(
969 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07970 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
eladalonf3f5c0e2017-08-18 09:47:08971 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 07:24:34972 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 11:47:04973 VideoReceiveStream* receive_stream_impl =
974 static_cast<VideoReceiveStream*>(receive_stream);
975 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54976 {
pbos@webrtc.org26c0c412014-09-03 16:17:12977 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53978 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
979 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 11:47:04980 receive_rtp_config_.erase(config.rtp.remote_ssrc);
981 if (config.rtp.rtx_ssrc) {
982 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54983 }
Fredrik Solenberg23fba1f2015-04-29 13:24:01984 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 11:47:04985 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03986 }
nisse4709e892017-02-07 09:18:43987
nisse559af382017-03-21 13:41:12988 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 09:18:43989 ->RemoveStream(config.rtp.remote_ssrc);
990
skvlad7a43d252016-03-22 22:32:27991 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54992 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03993}
994
brandtr7250b392016-12-19 09:13:46995FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
996 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-24 06:37:14997 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 09:47:08998 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 14:37:18999
1000 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-24 06:37:141001
nisse0f15f922017-06-21 08:05:221002 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-24 06:37:141003 {
1004 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 08:05:221005 // Unlike the video and audio receive streams,
1006 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
1007 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 16:25:271008 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 08:05:221009 // constructor while holding |receive_crit_| ensures that we don't
1010 // call OnRtpPacket until the constructor is finished and the
1011 // object is in a valid state.
1012 // TODO(nisse): Fix constructor so that it can be moved outside of
1013 // this locked scope.
1014 receive_stream = new FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 18:39:011015 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 21:11:091016 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 14:37:181017
nissed44ce052017-02-06 10:23:001018 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1019 receive_rtp_config_.end());
Erik Språng09708512018-03-14 14:16:501020 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-24 06:37:141021 }
brandtrb29e6522016-12-21 14:37:181022
brandtr25445d32016-10-24 06:37:141023 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 14:37:181024
brandtr25445d32016-10-24 06:37:141025 return receive_stream;
1026}
1027
brandtr7250b392016-12-19 09:13:461028void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-24 06:37:141029 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 09:47:081030 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 14:37:181031
brandtr25445d32016-10-24 06:37:141032 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-24 06:37:141033 {
1034 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 14:37:181035
eladalon42f44f92017-07-25 13:40:061036 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 09:18:431037 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 10:23:001038 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 14:37:181039
brandtr7250b392016-12-19 09:13:461040 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1041 // destroyed.
nisse559af382017-03-21 13:41:121042 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 09:18:431043 ->RemoveStream(ssrc);
brandtr25445d32016-10-24 06:37:141044 }
brandtrb29e6522016-12-21 14:37:181045
eladalon42f44f92017-07-25 13:40:061046 delete receive_stream;
brandtr25445d32016-10-24 06:37:141047}
1048
Sebastian Jansson8f83b422018-02-21 12:07:131049RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 12:08:151050 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 12:07:131051}
1052
stefan@webrtc.org0bae1fa2014-11-05 14:05:291053Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 10:39:201054 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
1055 // thread. Re-enable once that is fixed.
eladalonf3f5c0e2017-08-18 09:47:081056 // RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:291057 Stats stats;
Peter Boström45553ae2015-05-08 11:54:381058 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 11:54:381059 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:291060 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 13:41:121061 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-19 05:08:191062 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 14:59:121063
1064 {
1065 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1066 stats.send_bandwidth_bps = last_bandwidth_bps_;
1067 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:291068 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 17:49:551069 // TODO(srte): It is unclear if we only want to report queues if network is
1070 // available.
1071 {
1072 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 12:08:151073 stats.pacer_delay_ms = aggregate_network_up_
1074 ? transport_send_ptr_->GetPacerQueuingDelayMs()
1075 : 0;
Sebastian Janssona06e9192018-03-07 17:49:551076 }
1077
Tommi38c5d932018-03-27 21:11:091078 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 07:54:281079 {
1080 rtc::CritScope cs(&bitrate_crit_);
1081 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
1082 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:291083 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:031084}
1085
Alex Narest78609d52017-10-20 08:37:471086void Call::SetBitrateAllocationStrategy(
1087 std::unique_ptr<rtc::BitrateAllocationStrategy>
1088 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 12:08:151089 // TODO(srte): This function should be moved to RtpTransportControllerSend
1090 // when BitrateAllocator is moved there.
1091 struct Functor {
1092 void operator()() {
1093 bitrate_allocator_->SetBitrateAllocationStrategy(
1094 std::move(bitrate_allocation_strategy_));
1095 }
1096 BitrateAllocator* bitrate_allocator_;
1097 std::unique_ptr<rtc::BitrateAllocationStrategy>
1098 bitrate_allocation_strategy_;
1099 };
1100 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
1101 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 08:37:471102}
1103
skvlad7a43d252016-03-22 22:32:271104void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
eladalonf3f5c0e2017-08-18 09:47:081105 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 22:32:271106 switch (media) {
1107 case MediaType::AUDIO:
1108 audio_network_state_ = state;
1109 break;
1110 case MediaType::VIDEO:
1111 video_network_state_ = state;
1112 break;
1113 case MediaType::ANY:
1114 case MediaType::DATA:
1115 RTC_NOTREACHED();
1116 break;
1117 }
1118
1119 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:121120 {
skvlad7a43d252016-03-22 22:32:271121 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 21:35:071122 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 22:32:271123 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 21:35:071124 }
pbos@webrtc.org26c0c412014-09-03 16:17:121125 }
1126 {
skvlad7a43d252016-03-22 22:32:271127 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 11:47:041128 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1129 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 22:32:271130 }
nissee4bcd6d2017-05-16 11:47:041131 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1132 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:121133 }
1134 }
1135}
1136
Stefan Holmer64be7fa2018-10-04 13:21:551137void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1138 ReadLockScoped read_lock(*send_crit_);
1139 for (auto& kv : audio_send_ssrcs_) {
1140 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 10:50:091141 }
1142}
1143
skvlad7a43d252016-03-22 22:32:271144void Call::UpdateAggregateNetworkState() {
eladalonf3f5c0e2017-08-18 09:47:081145 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 22:32:271146
1147 bool have_audio = false;
1148 bool have_video = false;
1149 {
1150 ReadLockScoped read_lock(*send_crit_);
Benjamin Wright41f9f2c2019-03-14 01:03:291151 if (!audio_send_ssrcs_.empty())
skvlad7a43d252016-03-22 22:32:271152 have_audio = true;
Benjamin Wright41f9f2c2019-03-14 01:03:291153 if (!video_send_ssrcs_.empty())
skvlad7a43d252016-03-22 22:32:271154 have_video = true;
1155 }
1156 {
1157 ReadLockScoped read_lock(*receive_crit_);
Benjamin Wright41f9f2c2019-03-14 01:03:291158 if (!audio_receive_streams_.empty())
skvlad7a43d252016-03-22 22:32:271159 have_audio = true;
Benjamin Wright41f9f2c2019-03-14 01:03:291160 if (!video_receive_streams_.empty())
skvlad7a43d252016-03-22 22:32:271161 have_video = true;
1162 }
1163
Sebastian Janssona06e9192018-03-07 17:49:551164 bool aggregate_network_up =
1165 ((have_video && video_network_state_ == kNetworkUp) ||
1166 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 22:32:271167
Mirko Bonadei675513b2017-11-09 10:09:251168 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 17:49:551169 << (aggregate_network_up ? "up" : "down");
1170 {
1171 rtc::CritScope cs(&aggregate_network_up_crit_);
1172 aggregate_network_up_ = aggregate_network_up;
1173 }
Sebastian Janssone6256052018-05-04 12:08:151174 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 22:32:271175}
1176
stefanc1aeaf02015-10-15 14:26:071177void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-03 06:44:011178 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1179 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 12:08:151180 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 14:26:071181}
1182
Sebastian Jansson2701bc92018-12-11 14:02:471183void Call::OnStartRateUpdate(DataRate start_rate) {
1184 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1185 transport_send_ptr_->GetWorkerQueue()->PostTask(
1186 [this, start_rate] { this->OnStartRateUpdate(start_rate); });
1187 return;
1188 }
1189 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1190}
1191
Sebastian Jansson19704ec2018-03-12 14:59:121192void Call::OnTargetTransferRate(TargetTransferRate msg) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 16:26:191193 // TODO(bugs.webrtc.org/9719)
1194 // Call::OnTargetTransferRate requires that on target transfer rate is invoked
1195 // from the worker queue (because bitrate_allocator_ requires it). Media
1196 // transport does not guarantee the callback on the worker queue.
1197 // When the threading model for MediaTransportInterface is update, reconsider
1198 // changing this implementation.
1199 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1200 transport_send_ptr_->GetWorkerQueue()->PostTask(
1201 [this, msg] { this->OnTargetTransferRate(msg); });
1202 return;
1203 }
1204
Sebastian Jansson19704ec2018-03-12 14:59:121205 uint32_t target_bitrate_bps = msg.target_rate.bps();
1206 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1207 uint8_t fraction_loss =
1208 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1209 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1210 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1211 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1212 {
1213 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1214 last_bandwidth_bps_ = bandwidth_bps;
1215 }
nisse559af382017-03-21 13:41:121216 // For controlling the rate of feedback messages.
1217 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 16:16:361218 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, bandwidth_bps,
1219 fraction_loss, rtt_ms,
1220 probing_interval_ms);
mflodman0e7e2592015-11-13 05:02:421221
asaperssonce2e1362016-09-09 07:13:351222 // Ignore updates if bitrate is zero (the aggregate network state is down).
1223 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 14:24:561224 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 07:13:351225 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1226 pacer_bitrate_kbps_counter_.ProcessAndPause();
1227 return;
stefan18adf0a2015-11-17 14:24:561228 }
asaperssonce2e1362016-09-09 07:13:351229
1230 bool sending_video;
1231 {
1232 ReadLockScoped read_lock(*send_crit_);
1233 sending_video = !video_send_streams_.empty();
1234 }
1235
1236 rtc::CritScope lock(&bitrate_crit_);
1237 if (!sending_video) {
1238 // Do not update the stats if we are not sending video.
1239 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1240 pacer_bitrate_kbps_counter_.ProcessAndPause();
1241 return;
1242 }
1243 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1244 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1245 uint32_t pacer_bitrate_bps =
1246 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1247 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 07:47:531248}
mflodman101f2502016-06-09 15:21:191249
perkj71ee44c2016-06-15 07:47:531250void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 12:06:281251 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 08:41:431252 uint32_t total_bitrate_bps) {
Sebastian Janssone6256052018-05-04 12:08:151253 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:421254 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Jansson35fa2802018-10-01 07:16:121255
Piotr (Peter) Slatala48c54932019-01-28 14:50:381256 {
1257 rtc::CritScope lock(&target_observer_crit_);
1258 if (media_transport_) {
1259 MediaTransportAllocatedBitrateLimits limits;
1260 limits.min_pacing_rate = DataRate::bps(min_send_bitrate_bps);
1261 limits.max_padding_bitrate = DataRate::bps(max_padding_bitrate_bps);
1262 limits.max_total_allocated_bitrate = DataRate::bps(total_bitrate_bps);
1263 media_transport_->SetAllocatedBitrateLimits(limits);
1264 }
1265 }
1266
perkj71ee44c2016-06-15 07:47:531267 rtc::CritScope lock(&bitrate_crit_);
1268 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 07:54:281269 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-13 05:02:421270}
1271
pbos8fc7fa72015-07-15 15:02:581272void Call::ConfigureSync(const std::string& sync_group) {
1273 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 11:58:401274 if (sync_group.empty())
pbos8fc7fa72015-07-15 15:02:581275 return;
1276
1277 AudioReceiveStream* sync_audio_stream = nullptr;
1278 // Find existing audio stream.
1279 const auto it = sync_stream_mapping_.find(sync_group);
1280 if (it != sync_stream_mapping_.end()) {
1281 sync_audio_stream = it->second;
1282 } else {
1283 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 11:47:041284 for (AudioReceiveStream* stream : audio_receive_streams_) {
1285 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 15:02:581286 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 10:09:251287 RTC_LOG(LS_WARNING)
1288 << "Attempting to sync more than one audio stream "
1289 "within the same sync group. This is not "
1290 "supported in the current implementation.";
pbos8fc7fa72015-07-15 15:02:581291 break;
1292 }
nissee4bcd6d2017-05-16 11:47:041293 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 15:02:581294 }
1295 }
1296 }
1297 if (sync_audio_stream)
1298 sync_stream_mapping_[sync_group] = sync_audio_stream;
1299 size_t num_synced_streams = 0;
1300 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1301 if (video_stream->config().sync_group != sync_group)
1302 continue;
1303 ++num_synced_streams;
1304 if (num_synced_streams > 1) {
1305 // TODO(pbos): Support synchronizing more than one A/V pair.
1306 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 10:09:251307 RTC_LOG(LS_WARNING)
1308 << "Attempting to sync more than one audio/video pair "
1309 "within the same sync group. This is not supported in "
1310 "the current implementation.";
pbos8fc7fa72015-07-15 15:02:581311 }
1312 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 11:58:401313 if (num_synced_streams == 1) {
1314 // sync_audio_stream may be null and that's ok.
1315 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 15:02:581316 } else {
solenberg3ebbcb52017-01-31 11:58:401317 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 15:02:581318 }
1319 }
1320}
1321
Fredrik Solenberg23fba1f2015-04-29 13:24:011322PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1323 const uint8_t* packet,
1324 size_t length) {
Peter Boström6f28cf02015-12-07 22:17:151325 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 07:57:131326 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:121327 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1328 // there's no receiver of the packet.
asapersson250fd972016-09-08 07:07:211329 if (received_bytes_per_second_counter_.HasSample()) {
1330 // First RTP packet has been received.
1331 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1332 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1333 }
pbos@webrtc.org29d58392013-05-16 12:08:031334 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 13:24:011335 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:121336 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 13:24:011337 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 07:57:131338 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:221339 rtcp_delivered = true;
mflodman3d7db262016-04-29 07:57:131340 }
1341 }
1342 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1343 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 11:47:041344 for (AudioReceiveStream* stream : audio_receive_streams_) {
Niels Möller8fb1a6a2019-03-05 13:29:421345 stream->DeliverRtcp(packet, length);
1346 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:361347 }
1348 }
Fredrik Solenberg23fba1f2015-04-29 13:24:011349 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:121350 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 13:24:011351 for (VideoSendStream* stream : video_send_streams_) {
Niels Möller8fb1a6a2019-03-05 13:29:421352 stream->DeliverRtcp(packet, length);
1353 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:031354 }
1355 }
mflodman3d7db262016-04-29 07:57:131356 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1357 ReadLockScoped read_lock(*send_crit_);
1358 for (auto& kv : audio_send_ssrcs_) {
Niels Möller8fb1a6a2019-03-05 13:29:421359 kv.second->DeliverRtcp(packet, length);
1360 rtcp_delivered = true;
mflodman3d7db262016-04-29 07:57:131361 }
1362 }
1363
Elad Alon4a87e1c2017-10-03 14:11:341364 if (rtcp_delivered) {
Karl Wiberg918f50c2018-07-05 09:40:331365 event_log_->Log(absl::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 14:11:341366 rtc::MakeArrayView(packet, length)));
1367 }
mflodman3d7db262016-04-29 07:57:131368
pbos@webrtc.orgcaba2d22014-05-14 13:57:121369 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:031370}
1371
Fredrik Solenberg23fba1f2015-04-29 13:24:011372PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 16:00:401373 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 09:03:121374 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 22:17:151375 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 10:23:001376
Danil Chapovalovb709cf82017-10-04 12:01:451377 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 16:00:401378 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 12:01:451379 return DELIVERY_PACKET_ERROR;
1380
Niels Möller70082872018-08-07 09:03:121381 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 13:38:321382 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 14:14:361383 // Repair packet_time_us for clock resets by comparing a new read of
1384 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 09:03:121385 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 14:14:361386 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 13:38:321387 }
Niels Möller70082872018-08-07 09:03:121388 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 12:01:451389 } else {
1390 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1391 }
nissed44ce052017-02-06 10:23:001392
sprangc1abde72017-07-11 10:56:211393 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1394 // These are empty (zero length payload) RTP packets with an unsignaled
1395 // payload type.
Danil Chapovalovb709cf82017-10-04 12:01:451396 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 10:56:211397
1398 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1399 is_keep_alive_packet);
1400
sprangc1abde72017-07-11 10:56:211401 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 12:01:451402 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 08:05:221403 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251404 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1405 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 08:05:221406 // Destruction of the receive stream, including deregistering from the
1407 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1408 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1409 // So by not passing the packet on to demuxing in this case, we prevent
1410 // incoming packets to be passed on via the demuxer to a receive stream
1411 // which is being torned down.
1412 return DELIVERY_UNKNOWN_SSRC;
1413 }
Jonas Oreland6d835922019-03-18 09:59:401414
1415 if (field_trial_webrtc_video_buffer_packets_with_unknown_ssrc_) {
1416 // Check if packet arrives for a stream that requires decryption
1417 // but does not yet have a FrameDecryptor.
1418 // In that case buffer the packet and replay it when the frame decryptor
1419 // is set.
1420 // TODO(bugs.webrtc.org/10416) : Remove this check once FrameDecryptor
1421 // is created as part of creating receive stream.
1422 const uint32_t ssrc = parsed_packet.Ssrc();
1423 auto vrs = std::find_if(
1424 video_receive_streams_.begin(), video_receive_streams_.end(),
1425 [&](const VideoReceiveStream* stream) {
1426 return (stream->config().rtp.remote_ssrc == ssrc ||
1427 stream->config().rtp.rtx_ssrc == ssrc);
1428 });
1429 if (vrs != video_receive_streams_.end() &&
1430 (*vrs)->config().crypto_options.sframe.require_frame_encryption &&
1431 (*vrs)->config().frame_decryptor == nullptr) {
1432 return DELIVERY_UNKNOWN_SSRC;
1433 }
1434 }
1435
Danil Chapovalovb709cf82017-10-04 12:01:451436 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 08:05:221437
Danil Chapovalovb709cf82017-10-04 12:01:451438 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 10:23:001439
Danil Chapovalovcbf5b732017-12-08 13:05:201440 // RateCounters expect input parameter as int, save it as int,
1441 // instead of converting each time it is passed to RateCounter::Add below.
1442 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-30 06:57:431443 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 12:01:451444 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 16:00:401445 received_bytes_per_second_counter_.Add(length);
1446 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 14:11:341447 event_log_->Log(
Karl Wiberg918f50c2018-07-05 09:40:331448 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 12:01:451449 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 11:05:061450 if (!first_received_rtp_audio_ms_) {
1451 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1452 }
1453 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 14:28:101454 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 13:24:011455 }
nissee4bcd6d2017-05-16 11:47:041456 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 14:16:341457 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 12:01:451458 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 16:00:401459 received_bytes_per_second_counter_.Add(length);
1460 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 14:11:341461 event_log_->Log(
Karl Wiberg918f50c2018-07-05 09:40:331462 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 12:01:451463 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 11:05:061464 if (!first_received_rtp_video_ms_) {
1465 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1466 }
1467 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 14:52:321468 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 13:24:011469 }
1470 }
1471 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:031472}
1473
stefan68786d22015-09-08 12:36:151474PacketReceiver::DeliveryStatus Call::DeliverPacket(
1475 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 16:00:401476 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 09:03:121477 int64_t packet_time_us) {
eladalond1dd2f72017-08-25 09:55:571478 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 16:00:401479 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1480 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:031481
Niels Möller70082872018-08-07 09:03:121482 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:031483}
1484
nissed2ef3142017-05-11 15:00:581485void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 12:01:451486 RtpPacketReceived parsed_packet;
1487 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 15:00:581488 return;
1489
Danil Chapovalovb709cf82017-10-04 12:01:451490 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 15:00:581491
brandtrcaea68f2017-08-23 07:55:171492 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 12:01:451493 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 07:55:171494 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251495 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1496 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 07:55:171497 // Destruction of the receive stream, including deregistering from the
1498 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1499 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1500 // So by not passing the packet on to demuxing in this case, we prevent
1501 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 14:16:501502 // which is being torn down.
brandtrcaea68f2017-08-23 07:55:171503 return;
1504 }
Danil Chapovalovb709cf82017-10-04 12:01:451505 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 07:55:171506
1507 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 14:16:341508 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 12:01:451509 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-19 06:50:451510}
1511
nissed44ce052017-02-06 10:23:001512void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1513 MediaType media_type) {
1514 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 09:18:431515 bool use_send_side_bwe =
1516 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 10:23:001517
brandtrb29e6522016-12-21 14:37:181518 RTPHeader header;
1519 packet.GetHeader(&header);
nissed44ce052017-02-06 10:23:001520
nisse4709e892017-02-07 09:18:431521 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 10:23:001522 // Inconsistent configuration of send side BWE. Do nothing.
1523 // TODO(nisse): Without this check, we may produce RTCP feedback
1524 // packets even when not negotiated. But it would be cleaner to
1525 // move the check down to RTCPSender::SendFeedbackPacket, which
1526 // would also help the PacketRouter to select an appropriate rtp
1527 // module in the case that some, but not all, have RTCP feedback
1528 // enabled.
1529 return;
1530 }
1531 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-30 06:57:431532 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 09:18:431533 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 13:41:121534 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 10:23:001535 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1536 header);
1537 }
brandtrb29e6522016-12-21 14:37:181538}
1539
pbos@webrtc.org29d58392013-05-16 12:08:031540} // namespace internal
nisseb8f9a322017-03-27 12:36:151541
pbos@webrtc.org29d58392013-05-16 12:08:031542} // namespace webrtc