blob: ca96888ae925538d95cdf57039f11693cabfd89e [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/congestion_controller/include/receive_side_congestion_controller.h"
39#include "modules/rtp_rtcp/include/flexfec_receiver.h"
40#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
41#include "modules/rtp_rtcp/include/rtp_header_parser.h"
42#include "modules/rtp_rtcp/source/byte_io.h"
43#include "modules/rtp_rtcp/source/rtp_packet_received.h"
44#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010045#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "rtc_base/location.h"
49#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 15:59:12 +010050#include "rtc_base/numerics/safe_minmax.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020051#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020052#include "rtc_base/synchronization/rw_lock_wrapper.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020053#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080055#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "rtc_base/trace_event.h"
57#include "system_wrappers/include/clock.h"
58#include "system_wrappers/include/cpu_info.h"
Jonas Oreland6d835922019-03-18 10:59:40 +010059#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "video/call_stats.h"
62#include "video/send_delay_stats.h"
63#include "video/stats_counter.h"
64#include "video/video_receive_stream.h"
65#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000066
67namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000068
nisse4709e892017-02-07 01:18:43 -080069namespace {
Johannes Kronf59666b2019-04-08 12:57:06 +020070bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010071 for (const auto& extension : extensions) {
72 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
Johannes Kronf59666b2019-04-08 12:57:06 +020073 return false;
Johannes Kron7ff164e2019-02-07 12:50:18 +010074 }
Johannes Kronf59666b2019-04-08 12:57:06 +020075 return true;
Johannes Kron7ff164e2019-02-07 12:50:18 +010076}
77
nisse4709e892017-02-07 01:18:43 -080078// TODO(nisse): This really begs for a shared context struct.
79bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
80 bool transport_cc) {
81 if (!transport_cc)
82 return false;
83 for (const auto& extension : extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010084 if (extension.uri == RtpExtension::kTransportSequenceNumberUri ||
85 extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
nisse4709e892017-02-07 01:18:43 -080086 return true;
87 }
88 return false;
89}
90
91bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
92 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
93}
94
95bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
96 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
97}
98
99bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
100 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
101}
102
nisse26e3abb2017-08-25 04:44:25 -0700103const int* FindKeyByValue(const std::map<int, int>& m, int v) {
104 for (const auto& kv : m) {
105 if (kv.second == v)
106 return &kv.first;
107 }
108 return nullptr;
109}
110
eladalon8ec568a2017-09-08 06:15:52 -0700111std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700112 const VideoReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200113 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700114 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
115 rtclog_config->local_ssrc = config.rtp.local_ssrc;
116 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
117 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
118 rtclog_config->remb = config.rtp.remb;
119 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700120
121 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700122 const int* search =
123 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200124 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200125 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700126 }
127 return rtclog_config;
128}
129
eladalon8ec568a2017-09-08 06:15:52 -0700130std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700131 const VideoSendStream::Config& config,
132 size_t ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200133 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700134 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700135 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700136 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700137 }
eladalon8ec568a2017-09-08 06:15:52 -0700138 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
139 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700140
Niels Möller259a4972018-04-05 15:36:51 +0200141 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
142 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700143 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700144 return rtclog_config;
145}
146
eladalon8ec568a2017-09-08 06:15:52 -0700147std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700148 const AudioReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200149 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700150 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
151 rtclog_config->local_ssrc = config.rtp.local_ssrc;
152 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700153 return rtclog_config;
154}
155
nisse4709e892017-02-07 01:18:43 -0800156} // namespace
157
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000158namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000159
Sebastian Janssone6256052018-05-04 14:08:15 +0200160class Call final : public webrtc::Call,
161 public PacketReceiver,
162 public RecoveredPacketReceiver,
163 public TargetTransferRateObserver,
164 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000165 public:
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100166 Call(Clock* clock,
167 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100168 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
169 std::unique_ptr<ProcessThread> module_process_thread,
170 TaskQueueFactory* task_queue_factory);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200171 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000172
brandtr25445d32016-10-23 23:37:14 -0700173 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000174 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000175
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200176 webrtc::AudioSendStream* CreateAudioSendStream(
177 const webrtc::AudioSendStream::Config& config) override;
178 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
179
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200180 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
181 const webrtc::AudioReceiveStream::Config& config) override;
182 void DestroyAudioReceiveStream(
183 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000184
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200185 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700186 webrtc::VideoSendStream::Config config,
187 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100188 webrtc::VideoSendStream* CreateVideoSendStream(
189 webrtc::VideoSendStream::Config config,
190 VideoEncoderConfig encoder_config,
191 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000193
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200194 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200195 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000196 void DestroyVideoReceiveStream(
197 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000198
brandtr7250b392016-12-19 01:13:46 -0800199 FlexfecReceiveStream* CreateFlexfecReceiveStream(
200 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700201 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800202 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700203
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100204 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
205
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000206 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000207
brandtr25445d32016-10-23 23:37:14 -0700208 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700209 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100210 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200211 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000212
brandtr4e523862016-10-18 23:50:45 -0700213 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700214 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700215
Alex Narest78609d52017-10-20 10:37:47 +0200216 void SetBitrateAllocationStrategy(
217 std::unique_ptr<rtc::BitrateAllocationStrategy>
218 bitrate_allocation_strategy) override;
219
skvlad7a43d252016-03-22 15:32:27 -0700220 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000221
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200222 void OnAudioTransportOverheadChanged(
223 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800224
stefanc1aeaf02015-10-15 07:26:07 -0700225 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
226
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100227 // Implements TargetTransferRateObserver,
228 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100229 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800230
perkj71ee44c2016-06-15 00:47:53 -0700231 // Implements BitrateAllocator::LimitObserver.
232 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +0100233 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100234 uint32_t total_bitrate_bps) override;
perkj71ee44c2016-06-15 00:47:53 -0700235
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800236 // This method is invoked when the media transport is created and when the
237 // media transport is being destructed.
238 // We only allow one media transport per connection.
239 //
240 // It should be called with non-null argument at most once, and if it was
241 // called with non-null argument, it has to be called with a null argument
242 // at least once after that.
243 void MediaTransportChange(MediaTransportInterface* media_transport) override;
244
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700245 void SetClientBitratePreferences(const BitrateSettings& preferences) override;
246
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000247 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200248 DeliveryStatus DeliverRtcp(MediaType media_type,
249 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200250 size_t length);
stefan68786d22015-09-08 05:36:15 -0700251 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100252 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200253 int64_t packet_time_us);
pbos8fc7fa72015-07-15 08:02:58 -0700254 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700255 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700256
nissed44ce052017-02-06 02:23:00 -0800257 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
258 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700259 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800260
asaperssonfc5e81c2017-04-19 23:28:53 -0700261 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700262 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800263 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700264 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700265 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800266
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800267 // If |media_transport| is not null, it registers the rate observer for the
268 // media transport.
269 void RegisterRateObserver() RTC_LOCKS_EXCLUDED(target_observer_crit_);
270
Niels Möller46879152019-01-07 15:54:47 +0100271 // Intended for DCHECKs, to avoid locking in production builds.
272 MediaTransportInterface* media_transport()
273 RTC_LOCKS_EXCLUDED(target_observer_crit_);
274
Peter Boströmd3c94472015-12-09 11:20:58 +0100275 Clock* const clock_;
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100276 TaskQueueFactory* const task_queue_factory_;
stefan91d92602015-11-11 10:13:02 -0800277
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700278 // Caching the last SetBitrate for media transport.
279 absl::optional<MediaTransportTargetRateConstraints> last_set_bitrate_
280 RTC_GUARDED_BY(&target_observer_crit_);
Peter Boström45553ae2015-05-08 13:54:38 +0200281 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800282 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800283 const std::unique_ptr<CallStats> call_stats_;
284 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000285 Call::Config config_;
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200286 SequenceChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000287
skvlad7a43d252016-03-22 15:32:27 -0700288 NetworkState audio_network_state_;
289 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100290 rtc::CriticalSection aggregate_network_up_crit_;
291 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000292
kwibergb25345e2016-03-12 06:10:44 -0800293 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700294 // Audio, Video, and FlexFEC receive streams are owned by the client that
295 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700296 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700297 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200298 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700299 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700300
pbos8fc7fa72015-07-15 08:02:58 -0700301 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700302 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000303
nisse0f15f922017-06-21 01:05:22 -0700304 // TODO(nisse): Should eventually be injected at creation,
305 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700306 RtpStreamReceiverController audio_receiver_controller_;
307 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700308
nissed44ce052017-02-06 02:23:00 -0800309 // This extra map is used for receive processing which is
310 // independent of media type.
311
312 // TODO(nisse): In the RTP transport refactoring, we should have a
313 // single mapping from ssrc to a more abstract receive stream, with
314 // accessor methods for all configuration we need at this level.
315 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100316 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
317 : extensions(config.rtp.extensions),
318 use_send_side_bwe(UseSendSideBwe(config)) {}
319 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
320 : extensions(config.rtp.extensions),
321 use_send_side_bwe(UseSendSideBwe(config)) {}
322 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
323 : extensions(config.rtp_header_extensions),
324 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800325
326 // Registered RTP header extensions for each stream. Note that RTP header
327 // extensions are negotiated per track ("m= line") in the SDP, but we have
328 // no notion of tracks at the Call level. We therefore store the RTP header
329 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100330 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800331 // Set if both RTP extension the RTCP feedback message needed for
332 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100333 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800334 };
335 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700336 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800337
kwibergb25345e2016-03-12 06:10:44 -0800338 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700339 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700340 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
341 RTC_GUARDED_BY(send_crit_);
342 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
343 RTC_GUARDED_BY(send_crit_);
344 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000345
ossuc3d4b482017-05-23 06:07:11 -0700346 using RtpStateMap = std::map<uint32_t, RtpState>;
347 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700348 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700349 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700350 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700351
Åsa Persson4bece9a2017-10-06 10:04:04 +0200352 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
353 RtpPayloadStateMap suspended_video_payload_states_
354 RTC_GUARDED_BY(configuration_sequence_checker_);
355
skvlad11a9cbf2016-10-07 11:53:05 -0700356 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700357
stefan18adf0a2015-11-17 06:24:56 -0800358 // The following members are only accessed (exclusively) from one thread and
359 // from the destructor, and therefore doesn't need any explicit
360 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700361 RateCounter received_bytes_per_second_counter_;
362 RateCounter received_audio_bytes_per_second_counter_;
363 RateCounter received_video_bytes_per_second_counter_;
364 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200365 absl::optional<int64_t> first_received_rtp_audio_ms_;
366 absl::optional<int64_t> last_received_rtp_audio_ms_;
367 absl::optional<int64_t> first_received_rtp_video_ms_;
368 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 10:13:02 -0800369
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100370 rtc::CriticalSection last_bandwidth_bps_crit_;
371 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800372 // TODO(holmer): Remove this lock once BitrateController no longer calls
373 // OnNetworkChanged from multiple threads.
374 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700375 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
376 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
377 AvgCounter estimated_send_bitrate_kbps_counter_
378 RTC_GUARDED_BY(&bitrate_crit_);
379 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800380
nisse559af382017-03-21 06:41:12 -0700381 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100382
383 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
384
asapersson35151f32016-05-02 23:44:01 -0700385 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700386 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800387
Sebastian Janssone6256052018-05-04 14:08:15 +0200388 // Caches transport_send_.get(), to avoid racing with destructor.
389 // Note that this is declared before transport_send_ to ensure that it is not
390 // invalidated until no more tasks can be running on the transport_send_ task
391 // queue.
392 RtpTransportControllerSendInterface* transport_send_ptr_;
393 // Declared last since it will issue callbacks from a task queue. Declaring it
394 // last ensures that it is destroyed first and any running tasks are finished.
395 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800396
397 // This is a precaution, since |MediaTransportChange| is not guaranteed to be
398 // invoked on a particular thread.
399 rtc::CriticalSection target_observer_crit_;
400 bool is_target_rate_observer_registered_
401 RTC_GUARDED_BY(&target_observer_crit_) = false;
402 MediaTransportInterface* media_transport_
403 RTC_GUARDED_BY(&target_observer_crit_) = nullptr;
404
henrikg3c089d72015-09-16 05:37:44 -0700405 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000406};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000407} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000408
asapersson2e5cfcd2016-08-11 08:41:18 -0700409std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200410 char buf[1024];
411 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700412 ss << "Call stats: " << time_ms << ", {";
413 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
414 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
415 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
416 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
417 ss << "rtt_ms: " << rtt_ms;
418 ss << '}';
419 return ss.str();
420}
421
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000422Call* Call::Create(const Call::Config& config) {
Danil Chapovalov359fe332019-04-01 10:46:36 +0200423 return Create(config, Clock::GetRealTimeClock(),
424 ProcessThread::Create("PacerThread"),
425 ProcessThread::Create("ModuleProcessThread"));
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100426}
427
428Call* Call::Create(const Call::Config& config,
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100429 Clock* clock,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100430 std::unique_ptr<ProcessThread> call_thread,
Danil Chapovalov359fe332019-04-01 10:46:36 +0200431 std::unique_ptr<ProcessThread> pacer_thread) {
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200432 RTC_DCHECK(config.task_queue_factory);
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100433 return new internal::Call(
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100434 clock, config,
Sebastian Janssoned50e6c2019-03-01 14:45:21 +0100435 absl::make_unique<RtpTransportControllerSend>(
Ying Wang0810a7c2019-04-10 13:48:24 +0200436 clock, config.event_log, config.network_state_predictor_factory,
437 config.network_controller_factory, config.bitrate_config,
Danil Chapovalov53d45ba2019-07-03 14:56:33 +0200438 std::move(pacer_thread), config.task_queue_factory),
439 std::move(call_thread), config.task_queue_factory);
zstein7cb69d52017-05-08 11:52:38 -0700440}
441
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100442// This method here to avoid subclasses has to implement this method.
443// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
444// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100445VideoSendStream* Call::CreateVideoSendStream(
446 VideoSendStream::Config config,
447 VideoEncoderConfig encoder_config,
448 std::unique_ptr<FecController> fec_controller) {
449 return nullptr;
450}
451
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000452namespace internal {
453
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100454Call::Call(Clock* clock,
455 const Call::Config& config,
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100456 std::unique_ptr<RtpTransportControllerSendInterface> transport_send,
457 std::unique_ptr<ProcessThread> module_process_thread,
458 TaskQueueFactory* task_queue_factory)
Sebastian Jansson4e5f5ed2019-03-01 18:13:27 +0100459 : clock_(clock),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100460 task_queue_factory_(task_queue_factory),
stefan91d92602015-11-11 10:13:02 -0800461 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100462 module_process_thread_(std::move(module_process_thread)),
Tommi38c5d932018-03-27 23:11:09 +0200463 call_stats_(new CallStats(clock_, module_process_thread_.get())),
Sebastian Janssonda6806c2019-03-04 17:05:12 +0100464 bitrate_allocator_(new BitrateAllocator(clock_, this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200465 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800466 audio_network_state_(kNetworkDown),
467 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100468 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000469 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800470 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700471 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700472 received_bytes_per_second_counter_(clock_, nullptr, true),
473 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
474 received_video_bytes_per_second_counter_(clock_, nullptr, true),
475 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100476 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700477 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700478 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700479 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
480 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700481 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100482 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700483 video_send_delay_stats_(new SendDelayStats(clock_)),
Benjamin Wrighta5564482019-04-03 10:44:18 -0700484 start_ms_(clock_->TimeInMilliseconds()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700485 RTC_DCHECK(config.event_log != nullptr);
nisse6167b262017-04-06 06:34:25 -0700486 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 14:08:15 +0200487 transport_send_ptr_ = transport_send_.get();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000488}
489
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000490Call::~Call() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200491 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700492
solenbergc7a8b082015-10-16 14:35:07 -0700493 RTC_CHECK(audio_send_ssrcs_.empty());
494 RTC_CHECK(video_send_ssrcs_.empty());
495 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700496 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700497 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000498
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800499 if (!media_transport_) {
500 module_process_thread_->DeRegisterModule(
501 receive_side_cc_.GetRemoteBitrateEstimator(true));
502 module_process_thread_->DeRegisterModule(&receive_side_cc_);
503 module_process_thread_->DeRegisterModule(call_stats_.get());
504 module_process_thread_->Stop();
505 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800506 }
sprang6d6122b2016-07-13 06:37:09 -0700507
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100508 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700509 // Only update histograms after process threads have been shut down, so that
510 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700511 {
512 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700513 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700514 }
sprang6d6122b2016-07-13 06:37:09 -0700515 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700516 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000517}
518
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800519void Call::RegisterRateObserver() {
520 rtc::CritScope lock(&target_observer_crit_);
521
522 if (is_target_rate_observer_registered_) {
523 return;
524 }
525
526 is_target_rate_observer_registered_ = true;
527
528 if (media_transport_) {
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800529 // TODO(bugs.webrtc.org/9719): We should report call_stats_ from
530 // media transport (at least Rtt). We should extend media transport
531 // interface to include "receive_side bwe" if needed.
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800532 media_transport_->AddTargetTransferRateObserver(this);
533 } else {
534 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800535
536 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800537
538 module_process_thread_->RegisterModule(
539 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
540 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
541 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
542 module_process_thread_->Start();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800543 }
544}
545
Niels Möller46879152019-01-07 15:54:47 +0100546MediaTransportInterface* Call::media_transport() {
547 rtc::CritScope lock(&target_observer_crit_);
548 return media_transport_;
549}
550
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800551void Call::MediaTransportChange(MediaTransportInterface* media_transport) {
552 rtc::CritScope lock(&target_observer_crit_);
553
554 if (is_target_rate_observer_registered_) {
555 // Only used to unregister rate observer from media transport. Registration
556 // happens when the stream is created.
557 if (!media_transport && media_transport_) {
558 media_transport_->RemoveTargetTransferRateObserver(this);
559 media_transport_ = nullptr;
560 is_target_rate_observer_registered_ = false;
561 }
562 } else if (media_transport) {
563 RTC_DCHECK(media_transport_ == nullptr ||
564 media_transport_ == media_transport)
565 << "media_transport_=" << (media_transport_ != nullptr)
566 << ", (media_transport_==media_transport)="
567 << (media_transport_ == media_transport);
568 media_transport_ = media_transport;
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -0700569 MediaTransportTargetRateConstraints constraints;
570 if (config_.bitrate_config.start_bitrate_bps > 0) {
571 constraints.starting_bitrate =
572 DataRate::bps(config_.bitrate_config.start_bitrate_bps);
573 }
574 if (config_.bitrate_config.max_bitrate_bps > 0) {
575 constraints.max_bitrate =
576 DataRate::bps(config_.bitrate_config.max_bitrate_bps);
577 }
578 if (config_.bitrate_config.min_bitrate_bps > 0) {
579 constraints.min_bitrate =
580 DataRate::bps(config_.bitrate_config.min_bitrate_bps);
581 }
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700582
583 // User called ::SetBitrate on peer connection before
584 // media transport was created.
585 if (last_set_bitrate_) {
586 media_transport_->SetTargetBitrateLimits(*last_set_bitrate_);
587 } else {
588 media_transport_->SetTargetBitrateLimits(constraints);
589 }
590 }
591}
592
593void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
594 GetTransportControllerSend()->SetClientBitratePreferences(preferences);
595 // Can the client code invoke 'SetBitrate' before media transport is created?
596 // It's probably possible :/
597 MediaTransportTargetRateConstraints constraints;
598 if (preferences.start_bitrate_bps.has_value()) {
599 constraints.starting_bitrate =
600 webrtc::DataRate::bps(*preferences.start_bitrate_bps);
601 }
602 if (preferences.max_bitrate_bps.has_value()) {
603 constraints.max_bitrate =
604 webrtc::DataRate::bps(*preferences.max_bitrate_bps);
605 }
606 if (preferences.min_bitrate_bps.has_value()) {
607 constraints.min_bitrate =
608 webrtc::DataRate::bps(*preferences.min_bitrate_bps);
609 }
610 rtc::CritScope lock(&target_observer_crit_);
611 last_set_bitrate_ = constraints;
612 if (media_transport_) {
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -0700613 media_transport_->SetTargetBitrateLimits(constraints);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800614 }
615}
616
asapersson4374a092016-07-27 00:39:09 -0700617void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700618 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700619 "WebRTC.Call.LifetimeInSeconds",
620 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
621}
622
asaperssonfc5e81c2017-04-19 23:28:53 -0700623void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
624 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800625 return;
626 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700627 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800628 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
629 return;
asaperssonce2e1362016-09-09 00:13:35 -0700630 const int kMinRequiredPeriodicSamples = 5;
631 AggregatedStats send_bitrate_stats =
632 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
633 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700634 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
635 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100636 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
637 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800638 }
asaperssonce2e1362016-09-09 00:13:35 -0700639 AggregatedStats pacer_bitrate_stats =
640 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
641 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700642 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
643 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100644 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
645 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800646 }
647}
648
649void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700650 if (first_received_rtp_audio_ms_) {
651 RTC_HISTOGRAM_COUNTS_100000(
652 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
653 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
654 }
655 if (first_received_rtp_video_ms_) {
656 RTC_HISTOGRAM_COUNTS_100000(
657 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
658 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
659 }
asapersson250fd972016-09-08 00:07:21 -0700660 const int kMinRequiredPeriodicSamples = 5;
661 AggregatedStats video_bytes_per_sec =
662 received_video_bytes_per_second_counter_.GetStats();
663 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700664 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
665 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100666 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
667 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800668 }
asapersson250fd972016-09-08 00:07:21 -0700669 AggregatedStats audio_bytes_per_sec =
670 received_audio_bytes_per_second_counter_.GetStats();
671 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700672 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
673 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100674 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
675 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800676 }
asapersson250fd972016-09-08 00:07:21 -0700677 AggregatedStats rtcp_bytes_per_sec =
678 received_rtcp_bytes_per_second_counter_.GetStats();
679 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700680 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
681 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100682 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
683 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800684 }
asapersson250fd972016-09-08 00:07:21 -0700685 AggregatedStats recv_bytes_per_sec =
686 received_bytes_per_second_counter_.GetStats();
687 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700688 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
689 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100690 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
691 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700692 }
stefan91d92602015-11-11 10:13:02 -0800693}
694
solenberg5a289392015-10-19 03:39:20 -0700695PacketReceiver* Call::Receiver() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200696 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700697 return this;
698}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000699
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200700webrtc::AudioSendStream* Call::CreateAudioSendStream(
701 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700702 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200703 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800704
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700705 RTC_DCHECK_EQ(media_transport(),
706 config.media_transport_config.media_transport);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800707
708 RegisterRateObserver();
709
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100710 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
711 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200712 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700713 {
714 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
715 if (iter != suspended_audio_send_ssrcs_.end()) {
716 suspended_rtp_state.emplace(iter->second);
717 }
718 }
719
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100720 AudioSendStream* send_stream =
721 new AudioSendStream(clock_, config, config_.audio_state,
722 task_queue_factory_, module_process_thread_.get(),
723 transport_send_ptr_, bitrate_allocator_.get(),
724 event_log_, call_stats_.get(), suspended_rtp_state);
solenbergc7a8b082015-10-16 14:35:07 -0700725 {
solenbergc7a8b082015-10-16 14:35:07 -0700726 WriteLockScoped write_lock(*send_crit_);
727 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
728 audio_send_ssrcs_.end());
729 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700730 }
solenberg7602aab2016-11-14 11:30:07 -0800731 {
732 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700733 for (AudioReceiveStream* stream : audio_receive_streams_) {
734 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
735 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800736 }
737 }
738 }
skvlad7a43d252016-03-22 15:32:27 -0700739 send_stream->SignalNetworkState(audio_network_state_);
740 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700741 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200742}
743
744void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700745 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200746 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700747 RTC_DCHECK(send_stream != nullptr);
748
749 send_stream->Stop();
750
eladalonabbc4302017-07-26 02:09:44 -0700751 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700752 webrtc::internal::AudioSendStream* audio_send_stream =
753 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700754 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700755 {
756 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800757 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
758 RTC_DCHECK_EQ(1, num_deleted);
759 }
760 {
761 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700762 for (AudioReceiveStream* stream : audio_receive_streams_) {
763 if (stream->config().rtp.local_ssrc == ssrc) {
764 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800765 }
766 }
solenbergc7a8b082015-10-16 14:35:07 -0700767 }
skvlad7a43d252016-03-22 15:32:27 -0700768 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700769 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200770}
771
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200772webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
773 const webrtc::AudioReceiveStream::Config& config) {
774 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200775 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800776 RegisterRateObserver();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200777 event_log_->Log(absl::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200778 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700779 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100780 clock_, &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100781 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200782 {
783 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100784 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
785 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700786 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800787
pbos8fc7fa72015-07-15 08:02:58 -0700788 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200789 }
solenberg7602aab2016-11-14 11:30:07 -0800790 {
791 ReadLockScoped read_lock(*send_crit_);
792 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
793 if (it != audio_send_ssrcs_.end()) {
794 receive_stream->AssociateSendStream(it->second);
795 }
796 }
skvlad7a43d252016-03-22 15:32:27 -0700797 receive_stream->SignalNetworkState(audio_network_state_);
798 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200799 return receive_stream;
800}
801
802void Call::DestroyAudioReceiveStream(
803 webrtc::AudioReceiveStream* receive_stream) {
804 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200805 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700806 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700807 webrtc::internal::AudioReceiveStream* audio_receive_stream =
808 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200809 {
810 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800811 const AudioReceiveStream::Config& config = audio_receive_stream->config();
812 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700813 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800814 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700815 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700816 const std::string& sync_group = audio_receive_stream->config().sync_group;
817 const auto it = sync_stream_mapping_.find(sync_group);
818 if (it != sync_stream_mapping_.end() &&
819 it->second == audio_receive_stream) {
820 sync_stream_mapping_.erase(it);
821 ConfigureSync(sync_group);
822 }
nissed44ce052017-02-06 02:23:00 -0800823 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200824 }
skvlad7a43d252016-03-22 15:32:27 -0700825 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200826 delete audio_receive_stream;
827}
828
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100829// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100830webrtc::VideoSendStream* Call::CreateVideoSendStream(
831 webrtc::VideoSendStream::Config config,
832 VideoEncoderConfig encoder_config,
833 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000834 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200835 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000836
Niels Möller46879152019-01-07 15:54:47 +0100837 RTC_DCHECK(media_transport() == config.media_transport);
838
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800839 RegisterRateObserver();
840
asapersson35151f32016-05-02 23:44:01 -0700841 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700842 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
843 ++ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200844 event_log_->Log(absl::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200845 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700846 }
perkj26091b12016-09-01 01:17:40 -0700847
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000848 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
849 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700850 // Copy ssrcs from |config| since |config| is moved.
851 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100852
mflodman0c478b32015-10-21 15:52:16 +0200853 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Jansson0b698262019-03-07 09:17:19 +0100854 clock_, num_cpu_cores_, module_process_thread_.get(), task_queue_factory_,
Sebastian Jansson74682c12019-03-01 11:50:20 +0100855 call_stats_.get(), transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700856 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200857 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200858 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700859
skvlad7a43d252016-03-22 15:32:27 -0700860 {
861 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700862 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700863 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
864 video_send_ssrcs_[ssrc] = send_stream;
865 }
866 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000867 }
skvlad7a43d252016-03-22 15:32:27 -0700868 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700869
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000870 return send_stream;
871}
872
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100873webrtc::VideoSendStream* Call::CreateVideoSendStream(
874 webrtc::VideoSendStream::Config config,
875 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100876 if (config_.fec_controller_factory) {
877 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
878 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100879 std::unique_ptr<FecController> fec_controller =
880 config_.fec_controller_factory
881 ? config_.fec_controller_factory->CreateFecController()
Sebastian Jansson11c012a2019-03-29 14:17:26 +0100882 : absl::make_unique<FecControllerDefault>(clock_);
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100883 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
884 std::move(fec_controller));
885}
886
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000887void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000888 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700889 RTC_DCHECK(send_stream != nullptr);
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200890 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000891
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000892 send_stream->Stop();
893
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000894 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000895 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000896 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200897 auto it = video_send_ssrcs_.begin();
898 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000899 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
900 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200901 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000902 } else {
903 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000904 }
905 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200906 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000907 }
henrikg91d6ede2015-09-17 00:24:34 -0700908 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000909
Åsa Persson4bece9a2017-10-06 10:04:04 +0200910 VideoSendStream::RtpStateMap rtp_states;
911 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
912 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
913 &rtp_payload_states);
914 for (const auto& kv : rtp_states) {
915 suspended_video_send_ssrcs_[kv.first] = kv.second;
916 }
917 for (const auto& kv : rtp_payload_states) {
918 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000919 }
920
skvlad7a43d252016-03-22 15:32:27 -0700921 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000922 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000923}
924
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200925webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200926 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000927 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200928 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800929
Johannes Kronf59666b2019-04-08 12:57:06 +0200930 receive_side_cc_.SetSendPeriodicFeedback(
931 SendPeriodicFeedback(configuration.rtp.extensions));
Johannes Kron7ff164e2019-02-07 12:50:18 +0100932
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800933 RegisterRateObserver();
934
nisse0f15f922017-06-21 01:05:22 -0700935 VideoReceiveStream* receive_stream = new VideoReceiveStream(
Sebastian Jansson896b47c2019-03-01 18:48:16 +0100936 task_queue_factory_, &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200937 transport_send_ptr_->packet_router(), std::move(configuration),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100938 module_process_thread_.get(), call_stats_.get(), clock_);
Tommi733b5472016-06-10 17:58:01 +0200939
940 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700941 {
942 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800943 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800944 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700945 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800946 // type, we may get an incorrect value for the rtx stream, but
947 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100948 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
949 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800950 }
Erik Språng09708512018-03-14 15:16:50 +0100951 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
952 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700953 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700954 ConfigureSync(config.sync_group);
955 }
956 receive_stream->SignalNetworkState(video_network_state_);
957 UpdateAggregateNetworkState();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200958 event_log_->Log(absl::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200959 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000960 return receive_stream;
961}
962
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000963void Call::DestroyVideoReceiveStream(
964 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000965 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200966 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700967 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700968 VideoReceiveStream* receive_stream_impl =
969 static_cast<VideoReceiveStream*>(receive_stream);
970 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000971 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000972 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000973 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
974 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700975 receive_rtp_config_.erase(config.rtp.remote_ssrc);
976 if (config.rtp.rtx_ssrc) {
977 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000978 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200979 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700980 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000981 }
nisse4709e892017-02-07 01:18:43 -0800982
nisse559af382017-03-21 06:41:12 -0700983 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800984 ->RemoveStream(config.rtp.remote_ssrc);
985
skvlad7a43d252016-03-22 15:32:27 -0700986 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000987 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000988}
989
brandtr7250b392016-12-19 01:13:46 -0800990FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
991 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700992 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200993 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800994
995 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700996
nisse0f15f922017-06-21 01:05:22 -0700997 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700998 {
999 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -07001000 // Unlike the video and audio receive streams,
1001 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
1002 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -07001003 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -07001004 // constructor while holding |receive_crit_| ensures that we don't
1005 // call OnRtpPacket until the constructor is finished and the
1006 // object is in a valid state.
1007 // TODO(nisse): Fix constructor so that it can be moved outside of
1008 // this locked scope.
1009 receive_stream = new FlexfecReceiveStreamImpl(
Sebastian Jansson8026d602019-03-04 19:39:01 +01001010 clock_, &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +02001011 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -08001012
nissed44ce052017-02-06 02:23:00 -08001013 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
1014 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +01001015 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -07001016 }
brandtrb29e6522016-12-21 06:37:18 -08001017
brandtr25445d32016-10-23 23:37:14 -07001018 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -08001019
brandtr25445d32016-10-23 23:37:14 -07001020 return receive_stream;
1021}
1022
brandtr7250b392016-12-19 01:13:46 -08001023void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -07001024 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001025 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -08001026
brandtr25445d32016-10-23 23:37:14 -07001027 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -07001028 {
1029 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -08001030
eladalon42f44f92017-07-25 06:40:06 -07001031 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -08001032 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -08001033 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -08001034
brandtr7250b392016-12-19 01:13:46 -08001035 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
1036 // destroyed.
nisse559af382017-03-21 06:41:12 -07001037 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -08001038 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -07001039 }
brandtrb29e6522016-12-21 06:37:18 -08001040
eladalon42f44f92017-07-25 06:40:06 -07001041 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -07001042}
1043
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001044RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +02001045 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001046}
1047
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001048Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -07001049 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
1050 // thread. Re-enable once that is fixed.
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001051 // RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001052 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +02001053 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +02001054 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001055 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -07001056 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -07001057 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001058
1059 {
1060 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1061 stats.send_bandwidth_bps = last_bandwidth_bps_;
1062 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001063 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001064 // TODO(srte): It is unclear if we only want to report queues if network is
1065 // available.
1066 {
1067 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 14:08:15 +02001068 stats.pacer_delay_ms = aggregate_network_up_
1069 ? transport_send_ptr_->GetPacerQueuingDelayMs()
1070 : 0;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001071 }
1072
Tommi38c5d932018-03-27 23:11:09 +02001073 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -07001074 {
1075 rtc::CritScope cs(&bitrate_crit_);
1076 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
1077 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001078 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001079}
1080
Alex Narest78609d52017-10-20 10:37:47 +02001081void Call::SetBitrateAllocationStrategy(
1082 std::unique_ptr<rtc::BitrateAllocationStrategy>
1083 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001084 // TODO(srte): This function should be moved to RtpTransportControllerSend
1085 // when BitrateAllocator is moved there.
1086 struct Functor {
1087 void operator()() {
1088 bitrate_allocator_->SetBitrateAllocationStrategy(
1089 std::move(bitrate_allocation_strategy_));
1090 }
1091 BitrateAllocator* bitrate_allocator_;
1092 std::unique_ptr<rtc::BitrateAllocationStrategy>
1093 bitrate_allocation_strategy_;
1094 };
1095 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
1096 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 10:37:47 +02001097}
1098
skvlad7a43d252016-03-22 15:32:27 -07001099void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001100 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001101 switch (media) {
1102 case MediaType::AUDIO:
1103 audio_network_state_ = state;
1104 break;
1105 case MediaType::VIDEO:
1106 video_network_state_ = state;
1107 break;
1108 case MediaType::ANY:
1109 case MediaType::DATA:
1110 RTC_NOTREACHED();
1111 break;
1112 }
1113
1114 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001115 {
skvlad7a43d252016-03-22 15:32:27 -07001116 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -07001117 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001118 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -07001119 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001120 }
1121 {
skvlad7a43d252016-03-22 15:32:27 -07001122 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001123 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1124 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001125 }
nissee4bcd6d2017-05-16 04:47:04 -07001126 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1127 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001128 }
1129 }
1130}
1131
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001132void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1133 ReadLockScoped read_lock(*send_crit_);
1134 for (auto& kv : audio_send_ssrcs_) {
1135 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -08001136 }
1137}
1138
skvlad7a43d252016-03-22 15:32:27 -07001139void Call::UpdateAggregateNetworkState() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001140 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001141
1142 bool have_audio = false;
1143 bool have_video = false;
1144 {
1145 ReadLockScoped read_lock(*send_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001146 if (!audio_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001147 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001148 if (!video_send_ssrcs_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001149 have_video = true;
1150 }
1151 {
1152 ReadLockScoped read_lock(*receive_crit_);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001153 if (!audio_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001154 have_audio = true;
Benjamin Wright41f9f2c2019-03-13 18:03:29 -07001155 if (!video_receive_streams_.empty())
skvlad7a43d252016-03-22 15:32:27 -07001156 have_video = true;
1157 }
1158
Sebastian Janssona06e9192018-03-07 18:49:55 +01001159 bool aggregate_network_up =
1160 ((have_video && video_network_state_ == kNetworkUp) ||
1161 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001162
Mirko Bonadei675513b2017-11-09 11:09:25 +01001163 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001164 << (aggregate_network_up ? "up" : "down");
1165 {
1166 rtc::CritScope cs(&aggregate_network_up_crit_);
1167 aggregate_network_up_ = aggregate_network_up;
1168 }
Sebastian Janssone6256052018-05-04 14:08:15 +02001169 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001170}
1171
stefanc1aeaf02015-10-15 07:26:07 -07001172void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001173 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1174 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001175 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001176}
1177
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001178void Call::OnStartRateUpdate(DataRate start_rate) {
1179 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1180 transport_send_ptr_->GetWorkerQueue()->PostTask(
1181 [this, start_rate] { this->OnStartRateUpdate(start_rate); });
1182 return;
1183 }
1184 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1185}
1186
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001187void Call::OnTargetTransferRate(TargetTransferRate msg) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001188 // TODO(bugs.webrtc.org/9719)
1189 // Call::OnTargetTransferRate requires that on target transfer rate is invoked
1190 // from the worker queue (because bitrate_allocator_ requires it). Media
1191 // transport does not guarantee the callback on the worker queue.
1192 // When the threading model for MediaTransportInterface is update, reconsider
1193 // changing this implementation.
1194 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1195 transport_send_ptr_->GetWorkerQueue()->PostTask(
1196 [this, msg] { this->OnTargetTransferRate(msg); });
1197 return;
1198 }
1199
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001200 uint32_t target_bitrate_bps = msg.target_rate.bps();
1201 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1202 uint8_t fraction_loss =
1203 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1204 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1205 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1206 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1207 {
1208 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1209 last_bandwidth_bps_ = bandwidth_bps;
1210 }
nisse559af382017-03-21 06:41:12 -07001211 // For controlling the rate of feedback messages.
1212 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +01001213 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, bandwidth_bps,
1214 fraction_loss, rtt_ms,
1215 probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001216
asaperssonce2e1362016-09-09 00:13:35 -07001217 // Ignore updates if bitrate is zero (the aggregate network state is down).
1218 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001219 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001220 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1221 pacer_bitrate_kbps_counter_.ProcessAndPause();
1222 return;
stefan18adf0a2015-11-17 06:24:56 -08001223 }
asaperssonce2e1362016-09-09 00:13:35 -07001224
1225 bool sending_video;
1226 {
1227 ReadLockScoped read_lock(*send_crit_);
1228 sending_video = !video_send_streams_.empty();
1229 }
1230
1231 rtc::CritScope lock(&bitrate_crit_);
1232 if (!sending_video) {
1233 // Do not update the stats if we are not sending video.
1234 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1235 pacer_bitrate_kbps_counter_.ProcessAndPause();
1236 return;
1237 }
1238 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1239 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1240 uint32_t pacer_bitrate_bps =
1241 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1242 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001243}
mflodman101f2502016-06-09 17:21:19 +02001244
perkj71ee44c2016-06-15 00:47:53 -07001245void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001246 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +01001247 uint32_t total_bitrate_bps) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001248 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001249 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001250
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -08001251 {
1252 rtc::CritScope lock(&target_observer_crit_);
1253 if (media_transport_) {
1254 MediaTransportAllocatedBitrateLimits limits;
1255 limits.min_pacing_rate = DataRate::bps(min_send_bitrate_bps);
1256 limits.max_padding_bitrate = DataRate::bps(max_padding_bitrate_bps);
1257 limits.max_total_allocated_bitrate = DataRate::bps(total_bitrate_bps);
1258 media_transport_->SetAllocatedBitrateLimits(limits);
1259 }
1260 }
1261
perkj71ee44c2016-06-15 00:47:53 -07001262 rtc::CritScope lock(&bitrate_crit_);
1263 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001264 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001265}
1266
pbos8fc7fa72015-07-15 08:02:58 -07001267void Call::ConfigureSync(const std::string& sync_group) {
1268 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001269 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001270 return;
1271
1272 AudioReceiveStream* sync_audio_stream = nullptr;
1273 // Find existing audio stream.
1274 const auto it = sync_stream_mapping_.find(sync_group);
1275 if (it != sync_stream_mapping_.end()) {
1276 sync_audio_stream = it->second;
1277 } else {
1278 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001279 for (AudioReceiveStream* stream : audio_receive_streams_) {
1280 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001281 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001282 RTC_LOG(LS_WARNING)
1283 << "Attempting to sync more than one audio stream "
1284 "within the same sync group. This is not "
1285 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001286 break;
1287 }
nissee4bcd6d2017-05-16 04:47:04 -07001288 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001289 }
1290 }
1291 }
1292 if (sync_audio_stream)
1293 sync_stream_mapping_[sync_group] = sync_audio_stream;
1294 size_t num_synced_streams = 0;
1295 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1296 if (video_stream->config().sync_group != sync_group)
1297 continue;
1298 ++num_synced_streams;
1299 if (num_synced_streams > 1) {
1300 // TODO(pbos): Support synchronizing more than one A/V pair.
1301 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001302 RTC_LOG(LS_WARNING)
1303 << "Attempting to sync more than one audio/video pair "
1304 "within the same sync group. This is not supported in "
1305 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001306 }
1307 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001308 if (num_synced_streams == 1) {
1309 // sync_audio_stream may be null and that's ok.
1310 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001311 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001312 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001313 }
1314 }
1315}
1316
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001317PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1318 const uint8_t* packet,
1319 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001320 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001321 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001322 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1323 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001324 if (received_bytes_per_second_counter_.HasSample()) {
1325 // First RTP packet has been received.
1326 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1327 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1328 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001329 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001330 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001331 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001332 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001333 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001334 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001335 }
1336 }
1337 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1338 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001339 for (AudioReceiveStream* stream : audio_receive_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001340 stream->DeliverRtcp(packet, length);
1341 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001342 }
1343 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001344 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001345 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001346 for (VideoSendStream* stream : video_send_streams_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001347 stream->DeliverRtcp(packet, length);
1348 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001349 }
1350 }
mflodman3d7db262016-04-29 00:57:13 -07001351 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1352 ReadLockScoped read_lock(*send_crit_);
1353 for (auto& kv : audio_send_ssrcs_) {
Niels Möller8fb1a6a2019-03-05 14:29:42 +01001354 kv.second->DeliverRtcp(packet, length);
1355 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001356 }
1357 }
1358
Elad Alon4a87e1c2017-10-03 16:11:34 +02001359 if (rtcp_delivered) {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001360 event_log_->Log(absl::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001361 rtc::MakeArrayView(packet, length)));
1362 }
mflodman3d7db262016-04-29 00:57:13 -07001363
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001364 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001365}
1366
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001367PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001368 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001369 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001370 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001371
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001372 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001373 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001374 return DELIVERY_PACKET_ERROR;
1375
Niels Möller70082872018-08-07 11:03:12 +02001376 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001377 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001378 // Repair packet_time_us for clock resets by comparing a new read of
1379 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001380 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001381 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001382 }
Niels Möller70082872018-08-07 11:03:12 +02001383 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001384 } else {
1385 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1386 }
nissed44ce052017-02-06 02:23:00 -08001387
sprangc1abde72017-07-11 03:56:21 -07001388 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1389 // These are empty (zero length payload) RTP packets with an unsignaled
1390 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001391 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001392
1393 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1394 is_keep_alive_packet);
1395
sprangc1abde72017-07-11 03:56:21 -07001396 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001397 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001398 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001399 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1400 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001401 // Destruction of the receive stream, including deregistering from the
1402 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1403 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1404 // So by not passing the packet on to demuxing in this case, we prevent
1405 // incoming packets to be passed on via the demuxer to a receive stream
1406 // which is being torned down.
1407 return DELIVERY_UNKNOWN_SSRC;
1408 }
Jonas Oreland6d835922019-03-18 10:59:40 +01001409
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001410 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001411
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001412 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001413
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001414 // RateCounters expect input parameter as int, save it as int,
1415 // instead of converting each time it is passed to RateCounter::Add below.
1416 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001417 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001418 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001419 received_bytes_per_second_counter_.Add(length);
1420 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001421 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001422 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001423 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001424 if (!first_received_rtp_audio_ms_) {
1425 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1426 }
1427 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001428 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001429 }
nissee4bcd6d2017-05-16 04:47:04 -07001430 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001431 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001432 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001433 received_bytes_per_second_counter_.Add(length);
1434 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001435 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001436 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001437 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001438 if (!first_received_rtp_video_ms_) {
1439 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1440 }
1441 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001442 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001443 }
1444 }
1445 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001446}
1447
stefan68786d22015-09-08 05:36:15 -07001448PacketReceiver::DeliveryStatus Call::DeliverPacket(
1449 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001450 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001451 int64_t packet_time_us) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +02001452 RTC_DCHECK_RUN_ON(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001453 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1454 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001455
Niels Möller70082872018-08-07 11:03:12 +02001456 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001457}
1458
nissed2ef3142017-05-11 08:00:58 -07001459void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001460 RtpPacketReceived parsed_packet;
1461 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001462 return;
1463
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001464 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001465
brandtrcaea68f2017-08-23 00:55:17 -07001466 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001467 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001468 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001469 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1470 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001471 // Destruction of the receive stream, including deregistering from the
1472 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1473 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1474 // So by not passing the packet on to demuxing in this case, we prevent
1475 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001476 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001477 return;
1478 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001479 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001480
1481 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001482 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001483 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001484}
1485
nissed44ce052017-02-06 02:23:00 -08001486void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1487 MediaType media_type) {
1488 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001489 bool use_send_side_bwe =
1490 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001491
brandtrb29e6522016-12-21 06:37:18 -08001492 RTPHeader header;
1493 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001494
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001495 ReceivedPacket packet_msg;
1496 packet_msg.size = DataSize::bytes(packet.payload_size());
1497 packet_msg.receive_time = Timestamp::ms(packet.arrival_time_ms());
Sebastian Jansson3d61ab12019-06-14 13:35:51 +02001498 if (header.extension.hasAbsoluteSendTime) {
1499 packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp();
1500 }
Sebastian Jansson607a6f12019-06-13 17:48:53 +02001501 transport_send_ptr_->OnReceivedPacket(packet_msg);
Ying Wang8b279102019-05-27 17:19:08 +02001502
nisse4709e892017-02-07 01:18:43 -08001503 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001504 // Inconsistent configuration of send side BWE. Do nothing.
1505 // TODO(nisse): Without this check, we may produce RTCP feedback
1506 // packets even when not negotiated. But it would be cleaner to
1507 // move the check down to RTCPSender::SendFeedbackPacket, which
1508 // would also help the PacketRouter to select an appropriate rtp
1509 // module in the case that some, but not all, have RTCP feedback
1510 // enabled.
1511 return;
1512 }
1513 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001514 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001515 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001516 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001517 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1518 header);
1519 }
brandtrb29e6522016-12-21 06:37:18 -08001520}
1521
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001522} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001523
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001524} // namespace webrtc