blob: 5df97e9d810bd6c2e6985fc952b01d79f8fd9883 [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 Jansson74682c12019-03-01 11:50:20 +010021#include "api/task_queue/global_task_queue_factory.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020022#include "api/transport/network_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "audio/audio_receive_stream.h"
24#include "audio/audio_send_stream.h"
25#include "audio/audio_state.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "call/bitrate_allocator.h"
27#include "call/call.h"
28#include "call/flexfec_receive_stream_impl.h"
Sebastian Janssonb34556e2018-03-21 14:38:32 +010029#include "call/receive_time_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "call/rtp_stream_receiver_controller.h"
31#include "call/rtp_transport_controller_send.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020032#include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020033#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
34#include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
35#include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
36#include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "logging/rtc_event_log/rtc_event_log.h"
Elad Alon99a81b62017-09-21 10:25:29 +020038#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "modules/bitrate_controller/include/bitrate_controller.h"
40#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
41#include "modules/rtp_rtcp/include/flexfec_receiver.h"
42#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
43#include "modules/rtp_rtcp/include/rtp_header_parser.h"
44#include "modules/rtp_rtcp/source/byte_io.h"
45#include "modules/rtp_rtcp/source/rtp_packet_received.h"
46#include "modules/utility/include/process_thread.h"
Ying Wang3b790f32018-01-19 17:58:57 +010047#include "modules/video_coding/fec_controller_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/location.h"
51#include "rtc_base/logging.h"
Sebastian Jansson19704ec2018-03-12 15:59:12 +010052#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "rtc_base/sequenced_task_checker.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020054#include "rtc_base/strings/string_builder.h"
Sebastian Janssonc6c44262018-05-09 10:33:39 +020055#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "rtc_base/task_queue.h"
57#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080058#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/trace_event.h"
60#include "system_wrappers/include/clock.h"
61#include "system_wrappers/include/cpu_info.h"
62#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "video/call_stats.h"
64#include "video/send_delay_stats.h"
65#include "video/stats_counter.h"
66#include "video/video_receive_stream.h"
67#include "video/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000068
69namespace webrtc {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000070
nisse4709e892017-02-07 01:18:43 -080071namespace {
Johannes Kron7ff164e2019-02-07 12:50:18 +010072bool SendFeedbackOnRequestOnly(const std::vector<RtpExtension>& extensions) {
73 for (const auto& extension : extensions) {
74 if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
75 return true;
76 }
77 return false;
78}
79
nisse4709e892017-02-07 01:18:43 -080080// TODO(nisse): This really begs for a shared context struct.
81bool UseSendSideBwe(const std::vector<RtpExtension>& extensions,
82 bool transport_cc) {
83 if (!transport_cc)
84 return false;
85 for (const auto& extension : extensions) {
Johannes Kron7ff164e2019-02-07 12:50:18 +010086 if (extension.uri == RtpExtension::kTransportSequenceNumberUri ||
87 extension.uri == RtpExtension::kTransportSequenceNumberV2Uri)
nisse4709e892017-02-07 01:18:43 -080088 return true;
89 }
90 return false;
91}
92
93bool UseSendSideBwe(const VideoReceiveStream::Config& config) {
94 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
95}
96
97bool UseSendSideBwe(const AudioReceiveStream::Config& config) {
98 return UseSendSideBwe(config.rtp.extensions, config.rtp.transport_cc);
99}
100
101bool UseSendSideBwe(const FlexfecReceiveStream::Config& config) {
102 return UseSendSideBwe(config.rtp_header_extensions, config.transport_cc);
103}
104
nisse26e3abb2017-08-25 04:44:25 -0700105const int* FindKeyByValue(const std::map<int, int>& m, int v) {
106 for (const auto& kv : m) {
107 if (kv.second == v)
108 return &kv.first;
109 }
110 return nullptr;
111}
112
eladalon8ec568a2017-09-08 06:15:52 -0700113std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkj09e71da2017-05-22 03:26:49 -0700114 const VideoReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200115 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700116 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
117 rtclog_config->local_ssrc = config.rtp.local_ssrc;
118 rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc;
119 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
120 rtclog_config->remb = config.rtp.remb;
121 rtclog_config->rtp_extensions = config.rtp.extensions;
perkj09e71da2017-05-22 03:26:49 -0700122
123 for (const auto& d : config.decoders) {
nisse26e3abb2017-08-25 04:44:25 -0700124 const int* search =
125 FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200126 rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200127 search ? *search : 0);
perkj09e71da2017-05-22 03:26:49 -0700128 }
129 return rtclog_config;
130}
131
eladalon8ec568a2017-09-08 06:15:52 -0700132std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjc0876aa2017-05-22 04:08:28 -0700133 const VideoSendStream::Config& config,
134 size_t ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200135 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700136 rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700137 if (ssrc_index < config.rtp.rtx.ssrcs.size()) {
eladalon8ec568a2017-09-08 06:15:52 -0700138 rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index];
perkjc0876aa2017-05-22 04:08:28 -0700139 }
eladalon8ec568a2017-09-08 06:15:52 -0700140 rtclog_config->rtcp_mode = config.rtp.rtcp_mode;
141 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjc0876aa2017-05-22 04:08:28 -0700142
Niels Möller259a4972018-04-05 15:36:51 +0200143 rtclog_config->codecs.emplace_back(config.rtp.payload_name,
144 config.rtp.payload_type,
eladalon8ec568a2017-09-08 06:15:52 -0700145 config.rtp.rtx.payload_type);
perkjc0876aa2017-05-22 04:08:28 -0700146 return rtclog_config;
147}
148
eladalon8ec568a2017-09-08 06:15:52 -0700149std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -0700150 const AudioReceiveStream::Config& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200151 auto rtclog_config = absl::make_unique<rtclog::StreamConfig>();
eladalon8ec568a2017-09-08 06:15:52 -0700152 rtclog_config->remote_ssrc = config.rtp.remote_ssrc;
153 rtclog_config->local_ssrc = config.rtp.local_ssrc;
154 rtclog_config->rtp_extensions = config.rtp.extensions;
perkjac8f52d2017-05-22 09:36:28 -0700155 return rtclog_config;
156}
157
nisse4709e892017-02-07 01:18:43 -0800158} // namespace
159
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000160namespace internal {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000161
Sebastian Janssone6256052018-05-04 14:08:15 +0200162class Call final : public webrtc::Call,
163 public PacketReceiver,
164 public RecoveredPacketReceiver,
165 public TargetTransferRateObserver,
166 public BitrateAllocator::LimitObserver {
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000167 public:
nisseb8f9a322017-03-27 05:36:15 -0700168 Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700169 std::unique_ptr<RtpTransportControllerSendInterface> transport_send);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +0200170 ~Call() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000171
brandtr25445d32016-10-23 23:37:14 -0700172 // Implements webrtc::Call.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000173 PacketReceiver* Receiver() override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000174
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200175 webrtc::AudioSendStream* CreateAudioSendStream(
176 const webrtc::AudioSendStream::Config& config) override;
177 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
178
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200179 webrtc::AudioReceiveStream* CreateAudioReceiveStream(
180 const webrtc::AudioReceiveStream::Config& config) override;
181 void DestroyAudioReceiveStream(
182 webrtc::AudioReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000183
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200184 webrtc::VideoSendStream* CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700185 webrtc::VideoSendStream::Config config,
186 VideoEncoderConfig encoder_config) override;
Ying Wang3b790f32018-01-19 17:58:57 +0100187 webrtc::VideoSendStream* CreateVideoSendStream(
188 webrtc::VideoSendStream::Config config,
189 VideoEncoderConfig encoder_config,
190 std::unique_ptr<FecController> fec_controller) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000191 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000192
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200193 webrtc::VideoReceiveStream* CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200194 webrtc::VideoReceiveStream::Config configuration) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000195 void DestroyVideoReceiveStream(
196 webrtc::VideoReceiveStream* receive_stream) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000197
brandtr7250b392016-12-19 01:13:46 -0800198 FlexfecReceiveStream* CreateFlexfecReceiveStream(
199 const FlexfecReceiveStream::Config& config) override;
brandtr25445d32016-10-23 23:37:14 -0700200 void DestroyFlexfecReceiveStream(
brandtr7250b392016-12-19 01:13:46 -0800201 FlexfecReceiveStream* receive_stream) override;
brandtr25445d32016-10-23 23:37:14 -0700202
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100203 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
204
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000205 Stats GetStats() const override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000206
brandtr25445d32016-10-23 23:37:14 -0700207 // Implements PacketReceiver.
stefan68786d22015-09-08 05:36:15 -0700208 DeliveryStatus DeliverPacket(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100209 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200210 int64_t packet_time_us) override;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000211
brandtr4e523862016-10-18 23:50:45 -0700212 // Implements RecoveredPacketReceiver.
nissed2ef3142017-05-11 08:00:58 -0700213 void OnRecoveredPacket(const uint8_t* packet, size_t length) override;
brandtr4e523862016-10-18 23:50:45 -0700214
Alex Narest78609d52017-10-20 10:37:47 +0200215 void SetBitrateAllocationStrategy(
216 std::unique_ptr<rtc::BitrateAllocationStrategy>
217 bitrate_allocation_strategy) override;
218
skvlad7a43d252016-03-22 15:32:27 -0700219 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000220
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200221 void OnAudioTransportOverheadChanged(
222 int transport_overhead_per_packet) override;
michaelt79e05882016-11-08 02:50:09 -0800223
stefanc1aeaf02015-10-15 07:26:07 -0700224 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
225
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100226 // Implements TargetTransferRateObserver,
227 void OnTargetTransferRate(TargetTransferRate msg) override;
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100228 void OnStartRateUpdate(DataRate start_rate) override;
mflodman0e7e2592015-11-12 21:02:42 -0800229
perkj71ee44c2016-06-15 00:47:53 -0700230 // Implements BitrateAllocator::LimitObserver.
231 void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +0100232 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100233 uint32_t total_bitrate_bps) override;
perkj71ee44c2016-06-15 00:47:53 -0700234
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800235 // This method is invoked when the media transport is created and when the
236 // media transport is being destructed.
237 // We only allow one media transport per connection.
238 //
239 // It should be called with non-null argument at most once, and if it was
240 // called with non-null argument, it has to be called with a null argument
241 // at least once after that.
242 void MediaTransportChange(MediaTransportInterface* media_transport) override;
243
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000244 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200245 DeliveryStatus DeliverRtcp(MediaType media_type,
246 const uint8_t* packet,
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200247 size_t length);
stefan68786d22015-09-08 05:36:15 -0700248 DeliveryStatus DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +0100249 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200250 int64_t packet_time_us);
pbos8fc7fa72015-07-15 08:02:58 -0700251 void ConfigureSync(const std::string& sync_group)
danilchapa37de392017-09-09 04:17:22 -0700252 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
pbos8fc7fa72015-07-15 08:02:58 -0700253
nissed44ce052017-02-06 02:23:00 -0800254 void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
255 MediaType media_type)
danilchapa37de392017-09-09 04:17:22 -0700256 RTC_SHARED_LOCKS_REQUIRED(receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800257
asaperssonfc5e81c2017-04-19 23:28:53 -0700258 void UpdateSendHistograms(int64_t first_sent_packet_ms)
danilchapa37de392017-09-09 04:17:22 -0700259 RTC_EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800260 void UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700261 void UpdateHistograms();
skvlad7a43d252016-03-22 15:32:27 -0700262 void UpdateAggregateNetworkState();
stefan91d92602015-11-11 10:13:02 -0800263
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800264 // If |media_transport| is not null, it registers the rate observer for the
265 // media transport.
266 void RegisterRateObserver() RTC_LOCKS_EXCLUDED(target_observer_crit_);
267
Niels Möller46879152019-01-07 15:54:47 +0100268 // Intended for DCHECKs, to avoid locking in production builds.
269 MediaTransportInterface* media_transport()
270 RTC_LOCKS_EXCLUDED(target_observer_crit_);
271
Peter Boströmd3c94472015-12-09 11:20:58 +0100272 Clock* const clock_;
stefan91d92602015-11-11 10:13:02 -0800273
Peter Boström45553ae2015-05-08 13:54:38 +0200274 const int num_cpu_cores_;
kwibergb25345e2016-03-12 06:10:44 -0800275 const std::unique_ptr<ProcessThread> module_process_thread_;
kwibergb25345e2016-03-12 06:10:44 -0800276 const std::unique_ptr<CallStats> call_stats_;
277 const std::unique_ptr<BitrateAllocator> bitrate_allocator_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000278 Call::Config config_;
eladalonf3f5c0e2017-08-18 02:47:08 -0700279 rtc::SequencedTaskChecker configuration_sequence_checker_;
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000280
skvlad7a43d252016-03-22 15:32:27 -0700281 NetworkState audio_network_state_;
282 NetworkState video_network_state_;
Sebastian Janssona06e9192018-03-07 18:49:55 +0100283 rtc::CriticalSection aggregate_network_up_crit_;
284 bool aggregate_network_up_ RTC_GUARDED_BY(aggregate_network_up_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000285
kwibergb25345e2016-03-12 06:10:44 -0800286 std::unique_ptr<RWLockWrapper> receive_crit_;
brandtr25445d32016-10-23 23:37:14 -0700287 // Audio, Video, and FlexFEC receive streams are owned by the client that
288 // creates them.
nissee4bcd6d2017-05-16 04:47:04 -0700289 std::set<AudioReceiveStream*> audio_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700290 RTC_GUARDED_BY(receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200291 std::set<VideoReceiveStream*> video_receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700292 RTC_GUARDED_BY(receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700293
pbos8fc7fa72015-07-15 08:02:58 -0700294 std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
danilchapa37de392017-09-09 04:17:22 -0700295 RTC_GUARDED_BY(receive_crit_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000296
nisse0f15f922017-06-21 01:05:22 -0700297 // TODO(nisse): Should eventually be injected at creation,
298 // with a single object in the bundled case.
eladalon2a2b2972017-07-03 09:25:27 -0700299 RtpStreamReceiverController audio_receiver_controller_;
300 RtpStreamReceiverController video_receiver_controller_;
nissee4bcd6d2017-05-16 04:47:04 -0700301
nissed44ce052017-02-06 02:23:00 -0800302 // This extra map is used for receive processing which is
303 // independent of media type.
304
305 // TODO(nisse): In the RTP transport refactoring, we should have a
306 // single mapping from ssrc to a more abstract receive stream, with
307 // accessor methods for all configuration we need at this level.
308 struct ReceiveRtpConfig {
Erik Språng09708512018-03-14 15:16:50 +0100309 explicit ReceiveRtpConfig(const webrtc::AudioReceiveStream::Config& config)
310 : extensions(config.rtp.extensions),
311 use_send_side_bwe(UseSendSideBwe(config)) {}
312 explicit ReceiveRtpConfig(const webrtc::VideoReceiveStream::Config& config)
313 : extensions(config.rtp.extensions),
314 use_send_side_bwe(UseSendSideBwe(config)) {}
315 explicit ReceiveRtpConfig(const FlexfecReceiveStream::Config& config)
316 : extensions(config.rtp_header_extensions),
317 use_send_side_bwe(UseSendSideBwe(config)) {}
nissed44ce052017-02-06 02:23:00 -0800318
319 // Registered RTP header extensions for each stream. Note that RTP header
320 // extensions are negotiated per track ("m= line") in the SDP, but we have
321 // no notion of tracks at the Call level. We therefore store the RTP header
322 // extensions per SSRC instead, which leads to some storage overhead.
Erik Språng09708512018-03-14 15:16:50 +0100323 const RtpHeaderExtensionMap extensions;
nisse4709e892017-02-07 01:18:43 -0800324 // Set if both RTP extension the RTCP feedback message needed for
325 // send side BWE are negotiated.
Erik Språng09708512018-03-14 15:16:50 +0100326 const bool use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -0800327 };
328 std::map<uint32_t, ReceiveRtpConfig> receive_rtp_config_
danilchapa37de392017-09-09 04:17:22 -0700329 RTC_GUARDED_BY(receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800330
kwibergb25345e2016-03-12 06:10:44 -0800331 std::unique_ptr<RWLockWrapper> send_crit_;
solenbergc7a8b082015-10-16 14:35:07 -0700332 // Audio and Video send streams are owned by the client that creates them.
danilchapa37de392017-09-09 04:17:22 -0700333 std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_
334 RTC_GUARDED_BY(send_crit_);
335 std::map<uint32_t, VideoSendStream*> video_send_ssrcs_
336 RTC_GUARDED_BY(send_crit_);
337 std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(send_crit_);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000338
ossuc3d4b482017-05-23 06:07:11 -0700339 using RtpStateMap = std::map<uint32_t, RtpState>;
340 RtpStateMap suspended_audio_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700341 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700342 RtpStateMap suspended_video_send_ssrcs_
danilchapa37de392017-09-09 04:17:22 -0700343 RTC_GUARDED_BY(configuration_sequence_checker_);
ossuc3d4b482017-05-23 06:07:11 -0700344
Åsa Persson4bece9a2017-10-06 10:04:04 +0200345 using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>;
346 RtpPayloadStateMap suspended_video_payload_states_
347 RTC_GUARDED_BY(configuration_sequence_checker_);
348
skvlad11a9cbf2016-10-07 11:53:05 -0700349 webrtc::RtcEventLog* event_log_;
ivocb04965c2015-09-09 00:09:43 -0700350
stefan18adf0a2015-11-17 06:24:56 -0800351 // The following members are only accessed (exclusively) from one thread and
352 // from the destructor, and therefore doesn't need any explicit
353 // synchronization.
asapersson250fd972016-09-08 00:07:21 -0700354 RateCounter received_bytes_per_second_counter_;
355 RateCounter received_audio_bytes_per_second_counter_;
356 RateCounter received_video_bytes_per_second_counter_;
357 RateCounter received_rtcp_bytes_per_second_counter_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200358 absl::optional<int64_t> first_received_rtp_audio_ms_;
359 absl::optional<int64_t> last_received_rtp_audio_ms_;
360 absl::optional<int64_t> first_received_rtp_video_ms_;
361 absl::optional<int64_t> last_received_rtp_video_ms_;
stefan91d92602015-11-11 10:13:02 -0800362
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100363 rtc::CriticalSection last_bandwidth_bps_crit_;
364 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&last_bandwidth_bps_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800365 // TODO(holmer): Remove this lock once BitrateController no longer calls
366 // OnNetworkChanged from multiple threads.
367 rtc::CriticalSection bitrate_crit_;
danilchapa37de392017-09-09 04:17:22 -0700368 uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
369 uint32_t configured_max_padding_bitrate_bps_ RTC_GUARDED_BY(&bitrate_crit_);
370 AvgCounter estimated_send_bitrate_kbps_counter_
371 RTC_GUARDED_BY(&bitrate_crit_);
372 AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(&bitrate_crit_);
stefan18adf0a2015-11-17 06:24:56 -0800373
nisse559af382017-03-21 06:41:12 -0700374 ReceiveSideCongestionController receive_side_cc_;
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100375
376 const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_;
377
asapersson35151f32016-05-02 23:44:01 -0700378 const std::unique_ptr<SendDelayStats> video_send_delay_stats_;
asapersson4374a092016-07-27 00:39:09 -0700379 const int64_t start_ms_;
mflodman0e7e2592015-11-12 21:02:42 -0800380
Sebastian Janssone6256052018-05-04 14:08:15 +0200381 // Caches transport_send_.get(), to avoid racing with destructor.
382 // Note that this is declared before transport_send_ to ensure that it is not
383 // invalidated until no more tasks can be running on the transport_send_ task
384 // queue.
385 RtpTransportControllerSendInterface* transport_send_ptr_;
386 // Declared last since it will issue callbacks from a task queue. Declaring it
387 // last ensures that it is destroyed first and any running tasks are finished.
388 std::unique_ptr<RtpTransportControllerSendInterface> transport_send_;
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800389
390 // This is a precaution, since |MediaTransportChange| is not guaranteed to be
391 // invoked on a particular thread.
392 rtc::CriticalSection target_observer_crit_;
393 bool is_target_rate_observer_registered_
394 RTC_GUARDED_BY(&target_observer_crit_) = false;
395 MediaTransportInterface* media_transport_
396 RTC_GUARDED_BY(&target_observer_crit_) = nullptr;
397
henrikg3c089d72015-09-16 05:37:44 -0700398 RTC_DISALLOW_COPY_AND_ASSIGN(Call);
pbos@webrtc.org16e03b72013-10-28 16:32:01 +0000399};
pbos@webrtc.orgc49d5b72013-12-05 12:11:47 +0000400} // namespace internal
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000401
asapersson2e5cfcd2016-08-11 08:41:18 -0700402std::string Call::Stats::ToString(int64_t time_ms) const {
Jonas Olsson0a713b62018-04-04 15:49:32 +0200403 char buf[1024];
404 rtc::SimpleStringBuilder ss(buf);
asapersson2e5cfcd2016-08-11 08:41:18 -0700405 ss << "Call stats: " << time_ms << ", {";
406 ss << "send_bw_bps: " << send_bandwidth_bps << ", ";
407 ss << "recv_bw_bps: " << recv_bandwidth_bps << ", ";
408 ss << "max_pad_bps: " << max_padding_bitrate_bps << ", ";
409 ss << "pacer_delay_ms: " << pacer_delay_ms << ", ";
410 ss << "rtt_ms: " << rtt_ms;
411 ss << '}';
412 return ss.str();
413}
414
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000415Call* Call::Create(const Call::Config& config) {
Sebastian Jansson97f61ea2018-02-21 13:01:55 +0100416 return new internal::Call(
Karl Wiberg918f50c2018-07-05 11:40:33 +0200417 config, absl::make_unique<RtpTransportControllerSend>(
Sebastian Janssondfce03a2018-05-18 18:05:10 +0200418 Clock::GetRealTimeClock(), config.event_log,
419 config.network_controller_factory, config.bitrate_config));
zstein7cb69d52017-05-08 11:52:38 -0700420}
421
422Call* Call::Create(
423 const Call::Config& config,
424 std::unique_ptr<RtpTransportControllerSendInterface> transport_send) {
425 return new internal::Call(config, std::move(transport_send));
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000426}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +0000427
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100428// This method here to avoid subclasses has to implement this method.
429// Call perf test will use Internal::Call::CreateVideoSendStream() to inject
430// FecController.
Ying Wang3b790f32018-01-19 17:58:57 +0100431VideoSendStream* Call::CreateVideoSendStream(
432 VideoSendStream::Config config,
433 VideoEncoderConfig encoder_config,
434 std::unique_ptr<FecController> fec_controller) {
435 return nullptr;
436}
437
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000438namespace internal {
439
nisseb8f9a322017-03-27 05:36:15 -0700440Call::Call(const Call::Config& config,
zstein7cb69d52017-05-08 11:52:38 -0700441 std::unique_ptr<RtpTransportControllerSendInterface> transport_send)
stefan91d92602015-11-11 10:13:02 -0800442 : clock_(Clock::GetRealTimeClock()),
443 num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
kwiberg1c7fdd82016-04-26 08:18:04 -0700444 module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
Tommi38c5d932018-03-27 23:11:09 +0200445 call_stats_(new CallStats(clock_, module_process_thread_.get())),
perkj71ee44c2016-06-15 00:47:53 -0700446 bitrate_allocator_(new BitrateAllocator(this)),
Peter Boström45553ae2015-05-08 13:54:38 +0200447 config_(config),
Sergey Ulanove2b15012016-11-22 16:08:30 -0800448 audio_network_state_(kNetworkDown),
449 video_network_state_(kNetworkDown),
Sebastian Janssona06e9192018-03-07 18:49:55 +0100450 aggregate_network_up_(false),
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000451 receive_crit_(RWLockWrapper::CreateRWLock()),
stefan91d92602015-11-11 10:13:02 -0800452 send_crit_(RWLockWrapper::CreateRWLock()),
skvlad11a9cbf2016-10-07 11:53:05 -0700453 event_log_(config.event_log),
asapersson250fd972016-09-08 00:07:21 -0700454 received_bytes_per_second_counter_(clock_, nullptr, true),
455 received_audio_bytes_per_second_counter_(clock_, nullptr, true),
456 received_video_bytes_per_second_counter_(clock_, nullptr, true),
457 received_rtcp_bytes_per_second_counter_(clock_, nullptr, true),
Sebastian Jansson19704ec2018-03-12 15:59:12 +0100458 last_bandwidth_bps_(0),
perkj71ee44c2016-06-15 00:47:53 -0700459 min_allocated_send_bitrate_bps_(0),
sprang9c0b5512016-07-06 00:54:28 -0700460 configured_max_padding_bitrate_bps_(0),
asaperssonce2e1362016-09-09 00:13:35 -0700461 estimated_send_bitrate_kbps_counter_(clock_, nullptr, true),
462 pacer_bitrate_kbps_counter_(clock_, nullptr, true),
nisse05843312017-04-18 23:38:35 -0700463 receive_side_cc_(clock_, transport_send->packet_router()),
Sebastian Janssonb34556e2018-03-21 14:38:32 +0100464 receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()),
asapersson4374a092016-07-27 00:39:09 -0700465 video_send_delay_stats_(new SendDelayStats(clock_)),
Sebastian Janssone6256052018-05-04 14:08:15 +0200466 start_ms_(clock_->TimeInMilliseconds()) {
skvlad11a9cbf2016-10-07 11:53:05 -0700467 RTC_DCHECK(config.event_log != nullptr);
nisse6167b262017-04-06 06:34:25 -0700468 transport_send_ = std::move(transport_send);
Sebastian Janssone6256052018-05-04 14:08:15 +0200469 transport_send_ptr_ = transport_send_.get();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000470}
471
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000472Call::~Call() {
eladalonf3f5c0e2017-08-18 02:47:08 -0700473 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
perkj26091b12016-09-01 01:17:40 -0700474
solenbergc7a8b082015-10-16 14:35:07 -0700475 RTC_CHECK(audio_send_ssrcs_.empty());
476 RTC_CHECK(video_send_ssrcs_.empty());
477 RTC_CHECK(video_send_streams_.empty());
nissee4bcd6d2017-05-16 04:47:04 -0700478 RTC_CHECK(audio_receive_streams_.empty());
solenbergc7a8b082015-10-16 14:35:07 -0700479 RTC_CHECK(video_receive_streams_.empty());
pbos@webrtc.org9e4e5242015-02-12 10:48:23 +0000480
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800481 if (!media_transport_) {
482 module_process_thread_->DeRegisterModule(
483 receive_side_cc_.GetRemoteBitrateEstimator(true));
484 module_process_thread_->DeRegisterModule(&receive_side_cc_);
485 module_process_thread_->DeRegisterModule(call_stats_.get());
486 module_process_thread_->Stop();
487 call_stats_->DeregisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800488 }
sprang6d6122b2016-07-13 06:37:09 -0700489
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100490 int64_t first_sent_packet_ms = transport_send_->GetFirstPacketTimeMs();
sprang6d6122b2016-07-13 06:37:09 -0700491 // Only update histograms after process threads have been shut down, so that
492 // they won't try to concurrently update stats.
perkj26091b12016-09-01 01:17:40 -0700493 {
494 rtc::CritScope lock(&bitrate_crit_);
asaperssonfc5e81c2017-04-19 23:28:53 -0700495 UpdateSendHistograms(first_sent_packet_ms);
perkj26091b12016-09-01 01:17:40 -0700496 }
sprang6d6122b2016-07-13 06:37:09 -0700497 UpdateReceiveHistograms();
asapersson4374a092016-07-27 00:39:09 -0700498 UpdateHistograms();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000499}
500
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800501void Call::RegisterRateObserver() {
502 rtc::CritScope lock(&target_observer_crit_);
503
504 if (is_target_rate_observer_registered_) {
505 return;
506 }
507
508 is_target_rate_observer_registered_ = true;
509
510 if (media_transport_) {
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800511 // TODO(bugs.webrtc.org/9719): We should report call_stats_ from
512 // media transport (at least Rtt). We should extend media transport
513 // interface to include "receive_side bwe" if needed.
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800514 media_transport_->AddTargetTransferRateObserver(this);
515 } else {
516 transport_send_ptr_->RegisterTargetTransferRateObserver(this);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800517
518 call_stats_->RegisterStatsObserver(&receive_side_cc_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800519
520 module_process_thread_->RegisterModule(
521 receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE);
522 module_process_thread_->RegisterModule(call_stats_.get(), RTC_FROM_HERE);
523 module_process_thread_->RegisterModule(&receive_side_cc_, RTC_FROM_HERE);
524 module_process_thread_->Start();
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800525 }
526}
527
Niels Möller46879152019-01-07 15:54:47 +0100528MediaTransportInterface* Call::media_transport() {
529 rtc::CritScope lock(&target_observer_crit_);
530 return media_transport_;
531}
532
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800533void Call::MediaTransportChange(MediaTransportInterface* media_transport) {
534 rtc::CritScope lock(&target_observer_crit_);
535
536 if (is_target_rate_observer_registered_) {
537 // Only used to unregister rate observer from media transport. Registration
538 // happens when the stream is created.
539 if (!media_transport && media_transport_) {
540 media_transport_->RemoveTargetTransferRateObserver(this);
541 media_transport_ = nullptr;
542 is_target_rate_observer_registered_ = false;
543 }
544 } else if (media_transport) {
545 RTC_DCHECK(media_transport_ == nullptr ||
546 media_transport_ == media_transport)
547 << "media_transport_=" << (media_transport_ != nullptr)
548 << ", (media_transport_==media_transport)="
549 << (media_transport_ == media_transport);
550 media_transport_ = media_transport;
551 }
552}
553
asapersson4374a092016-07-27 00:39:09 -0700554void Call::UpdateHistograms() {
asapersson1d02d3e2016-09-09 22:40:25 -0700555 RTC_HISTOGRAM_COUNTS_100000(
asapersson4374a092016-07-27 00:39:09 -0700556 "WebRTC.Call.LifetimeInSeconds",
557 (clock_->TimeInMilliseconds() - start_ms_) / 1000);
558}
559
asaperssonfc5e81c2017-04-19 23:28:53 -0700560void Call::UpdateSendHistograms(int64_t first_sent_packet_ms) {
561 if (first_sent_packet_ms == -1)
stefan18adf0a2015-11-17 06:24:56 -0800562 return;
563 int64_t elapsed_sec =
asaperssonfc5e81c2017-04-19 23:28:53 -0700564 (clock_->TimeInMilliseconds() - first_sent_packet_ms) / 1000;
stefan18adf0a2015-11-17 06:24:56 -0800565 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
566 return;
asaperssonce2e1362016-09-09 00:13:35 -0700567 const int kMinRequiredPeriodicSamples = 5;
568 AggregatedStats send_bitrate_stats =
569 estimated_send_bitrate_kbps_counter_.ProcessAndGetStats();
570 if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700571 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
572 send_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100573 RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, "
574 << send_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800575 }
asaperssonce2e1362016-09-09 00:13:35 -0700576 AggregatedStats pacer_bitrate_stats =
577 pacer_bitrate_kbps_counter_.ProcessAndGetStats();
578 if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700579 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps",
580 pacer_bitrate_stats.average);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100581 RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, "
582 << pacer_bitrate_stats.ToString();
stefan18adf0a2015-11-17 06:24:56 -0800583 }
584}
585
586void Call::UpdateReceiveHistograms() {
saza0d7f04d2017-07-04 04:05:06 -0700587 if (first_received_rtp_audio_ms_) {
588 RTC_HISTOGRAM_COUNTS_100000(
589 "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds",
590 (*last_received_rtp_audio_ms_ - *first_received_rtp_audio_ms_) / 1000);
591 }
592 if (first_received_rtp_video_ms_) {
593 RTC_HISTOGRAM_COUNTS_100000(
594 "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds",
595 (*last_received_rtp_video_ms_ - *first_received_rtp_video_ms_) / 1000);
596 }
asapersson250fd972016-09-08 00:07:21 -0700597 const int kMinRequiredPeriodicSamples = 5;
598 AggregatedStats video_bytes_per_sec =
599 received_video_bytes_per_second_counter_.GetStats();
600 if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700601 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
602 video_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100603 RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, "
604 << video_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800605 }
asapersson250fd972016-09-08 00:07:21 -0700606 AggregatedStats audio_bytes_per_sec =
607 received_audio_bytes_per_second_counter_.GetStats();
608 if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700609 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
610 audio_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100611 RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, "
612 << audio_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800613 }
asapersson250fd972016-09-08 00:07:21 -0700614 AggregatedStats rtcp_bytes_per_sec =
615 received_rtcp_bytes_per_second_counter_.GetStats();
616 if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700617 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
618 rtcp_bytes_per_sec.average * 8);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100619 RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, "
620 << rtcp_bytes_per_sec.ToStringWithMultiplier(8);
stefan91d92602015-11-11 10:13:02 -0800621 }
asapersson250fd972016-09-08 00:07:21 -0700622 AggregatedStats recv_bytes_per_sec =
623 received_bytes_per_second_counter_.GetStats();
624 if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) {
asapersson1d02d3e2016-09-09 22:40:25 -0700625 RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps",
626 recv_bytes_per_sec.average * 8 / 1000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100627 RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, "
628 << recv_bytes_per_sec.ToStringWithMultiplier(8);
asapersson250fd972016-09-08 00:07:21 -0700629 }
stefan91d92602015-11-11 10:13:02 -0800630}
631
solenberg5a289392015-10-19 03:39:20 -0700632PacketReceiver* Call::Receiver() {
eladalond1dd2f72017-08-25 02:55:57 -0700633 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenberg5a289392015-10-19 03:39:20 -0700634 return this;
635}
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000636
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200637webrtc::AudioSendStream* Call::CreateAudioSendStream(
638 const webrtc::AudioSendStream::Config& config) {
solenbergc7a8b082015-10-16 14:35:07 -0700639 TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700640 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800641
Niels Möller46879152019-01-07 15:54:47 +0100642 RTC_DCHECK(media_transport() == config.media_transport);
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800643
644 RegisterRateObserver();
645
Oskar Sundbom56ef3052018-10-30 16:11:02 +0100646 // Stream config is logged in AudioSendStream::ConfigureStream, as it may
647 // change during the stream's lifetime.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200648 absl::optional<RtpState> suspended_rtp_state;
ossuc3d4b482017-05-23 06:07:11 -0700649 {
650 const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc);
651 if (iter != suspended_audio_send_ssrcs_.end()) {
652 suspended_rtp_state.emplace(iter->second);
653 }
654 }
655
Sebastian Janssone6256052018-05-04 14:08:15 +0200656 // TODO(srte): AudioSendStream should call GetWorkerQueue directly rather than
657 // having it injected.
658
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100659 AudioSendStream* send_stream = new AudioSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200660 config, config_.audio_state, transport_send_ptr_->GetWorkerQueue(),
661 module_process_thread_.get(), transport_send_ptr_,
662 bitrate_allocator_.get(), event_log_, call_stats_.get(),
Sam Zackrissonff058162018-11-20 17:15:13 +0100663 suspended_rtp_state);
solenbergc7a8b082015-10-16 14:35:07 -0700664 {
solenbergc7a8b082015-10-16 14:35:07 -0700665 WriteLockScoped write_lock(*send_crit_);
666 RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
667 audio_send_ssrcs_.end());
668 audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
solenbergc7a8b082015-10-16 14:35:07 -0700669 }
solenberg7602aab2016-11-14 11:30:07 -0800670 {
671 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700672 for (AudioReceiveStream* stream : audio_receive_streams_) {
673 if (stream->config().rtp.local_ssrc == config.rtp.ssrc) {
674 stream->AssociateSendStream(send_stream);
solenberg7602aab2016-11-14 11:30:07 -0800675 }
676 }
677 }
skvlad7a43d252016-03-22 15:32:27 -0700678 send_stream->SignalNetworkState(audio_network_state_);
679 UpdateAggregateNetworkState();
solenbergc7a8b082015-10-16 14:35:07 -0700680 return send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200681}
682
683void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
solenbergc7a8b082015-10-16 14:35:07 -0700684 TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700685 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700686 RTC_DCHECK(send_stream != nullptr);
687
688 send_stream->Stop();
689
eladalonabbc4302017-07-26 02:09:44 -0700690 const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc;
solenbergc7a8b082015-10-16 14:35:07 -0700691 webrtc::internal::AudioSendStream* audio_send_stream =
692 static_cast<webrtc::internal::AudioSendStream*>(send_stream);
ossuc3d4b482017-05-23 06:07:11 -0700693 suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState();
solenbergc7a8b082015-10-16 14:35:07 -0700694 {
695 WriteLockScoped write_lock(*send_crit_);
solenberg7602aab2016-11-14 11:30:07 -0800696 size_t num_deleted = audio_send_ssrcs_.erase(ssrc);
697 RTC_DCHECK_EQ(1, num_deleted);
698 }
699 {
700 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -0700701 for (AudioReceiveStream* stream : audio_receive_streams_) {
702 if (stream->config().rtp.local_ssrc == ssrc) {
703 stream->AssociateSendStream(nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800704 }
705 }
solenbergc7a8b082015-10-16 14:35:07 -0700706 }
skvlad7a43d252016-03-22 15:32:27 -0700707 UpdateAggregateNetworkState();
eladalonabbc4302017-07-26 02:09:44 -0700708 delete send_stream;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200709}
710
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200711webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
712 const webrtc::AudioReceiveStream::Config& config) {
713 TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700714 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800715 RegisterRateObserver();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200716 event_log_->Log(absl::make_unique<RtcEventAudioReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200717 CreateRtcLogStreamConfig(config)));
nisse0f15f922017-06-21 01:05:22 -0700718 AudioReceiveStream* receive_stream = new AudioReceiveStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200719 &audio_receiver_controller_, transport_send_ptr_->packet_router(),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100720 module_process_thread_.get(), config, config_.audio_state, event_log_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200721 {
722 WriteLockScoped write_lock(*receive_crit_);
Erik Språng09708512018-03-14 15:16:50 +0100723 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
724 ReceiveRtpConfig(config));
nissee4bcd6d2017-05-16 04:47:04 -0700725 audio_receive_streams_.insert(receive_stream);
nissed44ce052017-02-06 02:23:00 -0800726
pbos8fc7fa72015-07-15 08:02:58 -0700727 ConfigureSync(config.sync_group);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200728 }
solenberg7602aab2016-11-14 11:30:07 -0800729 {
730 ReadLockScoped read_lock(*send_crit_);
731 auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc);
732 if (it != audio_send_ssrcs_.end()) {
733 receive_stream->AssociateSendStream(it->second);
734 }
735 }
skvlad7a43d252016-03-22 15:32:27 -0700736 receive_stream->SignalNetworkState(audio_network_state_);
737 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200738 return receive_stream;
739}
740
741void Call::DestroyAudioReceiveStream(
742 webrtc::AudioReceiveStream* receive_stream) {
743 TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700744 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700745 RTC_DCHECK(receive_stream != nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700746 webrtc::internal::AudioReceiveStream* audio_receive_stream =
747 static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200748 {
749 WriteLockScoped write_lock(*receive_crit_);
nisse4709e892017-02-07 01:18:43 -0800750 const AudioReceiveStream::Config& config = audio_receive_stream->config();
751 uint32_t ssrc = config.rtp.remote_ssrc;
nisse559af382017-03-21 06:41:12 -0700752 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800753 ->RemoveStream(ssrc);
nissee4bcd6d2017-05-16 04:47:04 -0700754 audio_receive_streams_.erase(audio_receive_stream);
pbos8fc7fa72015-07-15 08:02:58 -0700755 const std::string& sync_group = audio_receive_stream->config().sync_group;
756 const auto it = sync_stream_mapping_.find(sync_group);
757 if (it != sync_stream_mapping_.end() &&
758 it->second == audio_receive_stream) {
759 sync_stream_mapping_.erase(it);
760 ConfigureSync(sync_group);
761 }
nissed44ce052017-02-06 02:23:00 -0800762 receive_rtp_config_.erase(ssrc);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200763 }
skvlad7a43d252016-03-22 15:32:27 -0700764 UpdateAggregateNetworkState();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200765 delete audio_receive_stream;
766}
767
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100768// This method can be used for Call tests with external fec controller factory.
Ying Wang3b790f32018-01-19 17:58:57 +0100769webrtc::VideoSendStream* Call::CreateVideoSendStream(
770 webrtc::VideoSendStream::Config config,
771 VideoEncoderConfig encoder_config,
772 std::unique_ptr<FecController> fec_controller) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000773 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700774 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000775
Niels Möller46879152019-01-07 15:54:47 +0100776 RTC_DCHECK(media_transport() == config.media_transport);
777
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800778 RegisterRateObserver();
779
asapersson35151f32016-05-02 23:44:01 -0700780 video_send_delay_stats_->AddSsrcs(config);
perkjc0876aa2017-05-22 04:08:28 -0700781 for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size();
782 ++ssrc_index) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200783 event_log_->Log(absl::make_unique<RtcEventVideoSendStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200784 CreateRtcLogStreamConfig(config, ssrc_index)));
perkjc0876aa2017-05-22 04:08:28 -0700785 }
perkj26091b12016-09-01 01:17:40 -0700786
mflodman@webrtc.orgeb16b812014-06-16 08:57:39 +0000787 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
788 // the call has already started.
perkj26091b12016-09-01 01:17:40 -0700789 // Copy ssrcs from |config| since |config| is moved.
790 std::vector<uint32_t> ssrcs = config.rtp.ssrcs;
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100791
Sebastian Janssone6256052018-05-04 14:08:15 +0200792 // TODO(srte): VideoSendStream should call GetWorkerQueue directly rather than
793 // having it injected.
mflodman0c478b32015-10-21 15:52:16 +0200794 VideoSendStream* send_stream = new VideoSendStream(
Sebastian Janssone6256052018-05-04 14:08:15 +0200795 num_cpu_cores_, module_process_thread_.get(),
Sebastian Jansson74682c12019-03-01 11:50:20 +0100796 transport_send_ptr_->GetWorkerQueue(), &GlobalTaskQueueFactory(),
797 call_stats_.get(), transport_send_ptr_, bitrate_allocator_.get(),
nisse05843312017-04-18 23:38:35 -0700798 video_send_delay_stats_.get(), event_log_, std::move(config),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200799 std::move(encoder_config), suspended_video_send_ssrcs_,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200800 suspended_video_payload_states_, std::move(fec_controller));
perkj26091b12016-09-01 01:17:40 -0700801
skvlad7a43d252016-03-22 15:32:27 -0700802 {
803 WriteLockScoped write_lock(*send_crit_);
perkj26091b12016-09-01 01:17:40 -0700804 for (uint32_t ssrc : ssrcs) {
skvlad7a43d252016-03-22 15:32:27 -0700805 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
806 video_send_ssrcs_[ssrc] = send_stream;
807 }
808 video_send_streams_.insert(send_stream);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000809 }
skvlad7a43d252016-03-22 15:32:27 -0700810 UpdateAggregateNetworkState();
perkj26091b12016-09-01 01:17:40 -0700811
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000812 return send_stream;
813}
814
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100815webrtc::VideoSendStream* Call::CreateVideoSendStream(
816 webrtc::VideoSendStream::Config config,
817 VideoEncoderConfig encoder_config) {
Ying Wang012b7e72018-03-05 15:44:23 +0100818 if (config_.fec_controller_factory) {
819 RTC_LOG(LS_INFO) << "External FEC Controller will be used.";
820 }
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100821 std::unique_ptr<FecController> fec_controller =
822 config_.fec_controller_factory
823 ? config_.fec_controller_factory->CreateFecController()
Karl Wiberg918f50c2018-07-05 11:40:33 +0200824 : absl::make_unique<FecControllerDefault>(Clock::GetRealTimeClock());
Ying Wang0dd1b0a2018-02-20 12:50:27 +0100825 return CreateVideoSendStream(std::move(config), std::move(encoder_config),
826 std::move(fec_controller));
827}
828
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000829void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000830 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
henrikg91d6ede2015-09-17 00:24:34 -0700831 RTC_DCHECK(send_stream != nullptr);
eladalonf3f5c0e2017-08-18 02:47:08 -0700832 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000833
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000834 send_stream->Stop();
835
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000836 VideoSendStream* send_stream_impl = nullptr;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000837 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000838 WriteLockScoped write_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200839 auto it = video_send_ssrcs_.begin();
840 while (it != video_send_ssrcs_.end()) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000841 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
842 send_stream_impl = it->second;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200843 video_send_ssrcs_.erase(it++);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000844 } else {
845 ++it;
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000846 }
847 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200848 video_send_streams_.erase(send_stream_impl);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000849 }
henrikg91d6ede2015-09-17 00:24:34 -0700850 RTC_CHECK(send_stream_impl != nullptr);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000851
Åsa Persson4bece9a2017-10-06 10:04:04 +0200852 VideoSendStream::RtpStateMap rtp_states;
853 VideoSendStream::RtpPayloadStateMap rtp_payload_states;
854 send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states,
855 &rtp_payload_states);
856 for (const auto& kv : rtp_states) {
857 suspended_video_send_ssrcs_[kv.first] = kv.second;
858 }
859 for (const auto& kv : rtp_payload_states) {
860 suspended_video_payload_states_[kv.first] = kv.second;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000861 }
862
skvlad7a43d252016-03-22 15:32:27 -0700863 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000864 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000865}
866
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200867webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
Tommi733b5472016-06-10 17:58:01 +0200868 webrtc::VideoReceiveStream::Config configuration) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000869 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700870 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800871
Johannes Kron7ff164e2019-02-07 12:50:18 +0100872 receive_side_cc_.SetSendFeedbackOnRequestOnly(
873 SendFeedbackOnRequestOnly(configuration.rtp.extensions));
874
Piotr (Peter) Slatalab2757882018-12-18 11:17:09 -0800875 RegisterRateObserver();
876
nisse0f15f922017-06-21 01:05:22 -0700877 VideoReceiveStream* receive_stream = new VideoReceiveStream(
Sebastian Jansson74682c12019-03-01 11:50:20 +0100878 &GlobalTaskQueueFactory(), &video_receiver_controller_, num_cpu_cores_,
Sebastian Janssone6256052018-05-04 14:08:15 +0200879 transport_send_ptr_->packet_router(), std::move(configuration),
nisse0f15f922017-06-21 01:05:22 -0700880 module_process_thread_.get(), call_stats_.get());
Tommi733b5472016-06-10 17:58:01 +0200881
882 const webrtc::VideoReceiveStream::Config& config = receive_stream->config();
skvlad7a43d252016-03-22 15:32:27 -0700883 {
884 WriteLockScoped write_lock(*receive_crit_);
nissed44ce052017-02-06 02:23:00 -0800885 if (config.rtp.rtx_ssrc) {
nissed44ce052017-02-06 02:23:00 -0800886 // We record identical config for the rtx stream as for the main
nisseb8f9a322017-03-27 05:36:15 -0700887 // stream. Since the transport_send_cc negotiation is per payload
nissed44ce052017-02-06 02:23:00 -0800888 // type, we may get an incorrect value for the rtx stream, but
889 // that is unlikely to matter in practice.
Erik Språng09708512018-03-14 15:16:50 +0100890 receive_rtp_config_.emplace(config.rtp.rtx_ssrc,
891 ReceiveRtpConfig(config));
nissed44ce052017-02-06 02:23:00 -0800892 }
Erik Språng09708512018-03-14 15:16:50 +0100893 receive_rtp_config_.emplace(config.rtp.remote_ssrc,
894 ReceiveRtpConfig(config));
skvlad7a43d252016-03-22 15:32:27 -0700895 video_receive_streams_.insert(receive_stream);
skvlad7a43d252016-03-22 15:32:27 -0700896 ConfigureSync(config.sync_group);
897 }
898 receive_stream->SignalNetworkState(video_network_state_);
899 UpdateAggregateNetworkState();
Karl Wiberg918f50c2018-07-05 11:40:33 +0200900 event_log_->Log(absl::make_unique<RtcEventVideoReceiveStreamConfig>(
Elad Alon4a87e1c2017-10-03 16:11:34 +0200901 CreateRtcLogStreamConfig(config)));
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000902 return receive_stream;
903}
904
pbos@webrtc.org2c46f8d2013-11-21 13:49:43 +0000905void Call::DestroyVideoReceiveStream(
906 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000907 TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700908 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
henrikg91d6ede2015-09-17 00:24:34 -0700909 RTC_DCHECK(receive_stream != nullptr);
nissee4bcd6d2017-05-16 04:47:04 -0700910 VideoReceiveStream* receive_stream_impl =
911 static_cast<VideoReceiveStream*>(receive_stream);
912 const VideoReceiveStream::Config& config = receive_stream_impl->config();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000913 {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000914 WriteLockScoped write_lock(*receive_crit_);
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000915 // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
916 // separate SSRC there can be either one or two.
nissee4bcd6d2017-05-16 04:47:04 -0700917 receive_rtp_config_.erase(config.rtp.remote_ssrc);
918 if (config.rtp.rtx_ssrc) {
919 receive_rtp_config_.erase(config.rtp.rtx_ssrc);
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000920 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200921 video_receive_streams_.erase(receive_stream_impl);
nissee4bcd6d2017-05-16 04:47:04 -0700922 ConfigureSync(config.sync_group);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000923 }
nisse4709e892017-02-07 01:18:43 -0800924
nisse559af382017-03-21 06:41:12 -0700925 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800926 ->RemoveStream(config.rtp.remote_ssrc);
927
skvlad7a43d252016-03-22 15:32:27 -0700928 UpdateAggregateNetworkState();
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000929 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000930}
931
brandtr7250b392016-12-19 01:13:46 -0800932FlexfecReceiveStream* Call::CreateFlexfecReceiveStream(
933 const FlexfecReceiveStream::Config& config) {
brandtr25445d32016-10-23 23:37:14 -0700934 TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700935 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800936
937 RecoveredPacketReceiver* recovered_packet_receiver = this;
brandtr25445d32016-10-23 23:37:14 -0700938
nisse0f15f922017-06-21 01:05:22 -0700939 FlexfecReceiveStreamImpl* receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700940 {
941 WriteLockScoped write_lock(*receive_crit_);
nisse0f15f922017-06-21 01:05:22 -0700942 // Unlike the video and audio receive streams,
943 // FlexfecReceiveStream implements RtpPacketSinkInterface itself,
944 // and hence its constructor passes its |this| pointer to
eladalon2a2b2972017-07-03 09:25:27 -0700945 // video_receiver_controller_->CreateStream(). Calling the
nisse0f15f922017-06-21 01:05:22 -0700946 // constructor while holding |receive_crit_| ensures that we don't
947 // call OnRtpPacket until the constructor is finished and the
948 // object is in a valid state.
949 // TODO(nisse): Fix constructor so that it can be moved outside of
950 // this locked scope.
951 receive_stream = new FlexfecReceiveStreamImpl(
eladalon2a2b2972017-07-03 09:25:27 -0700952 &video_receiver_controller_, config, recovered_packet_receiver,
Tommi38c5d932018-03-27 23:11:09 +0200953 call_stats_.get(), module_process_thread_.get());
brandtrb29e6522016-12-21 06:37:18 -0800954
nissed44ce052017-02-06 02:23:00 -0800955 RTC_DCHECK(receive_rtp_config_.find(config.remote_ssrc) ==
956 receive_rtp_config_.end());
Erik Språng09708512018-03-14 15:16:50 +0100957 receive_rtp_config_.emplace(config.remote_ssrc, ReceiveRtpConfig(config));
brandtr25445d32016-10-23 23:37:14 -0700958 }
brandtrb29e6522016-12-21 06:37:18 -0800959
brandtr25445d32016-10-23 23:37:14 -0700960 // TODO(brandtr): Store config in RtcEventLog here.
brandtrb29e6522016-12-21 06:37:18 -0800961
brandtr25445d32016-10-23 23:37:14 -0700962 return receive_stream;
963}
964
brandtr7250b392016-12-19 01:13:46 -0800965void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) {
brandtr25445d32016-10-23 23:37:14 -0700966 TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream");
eladalonf3f5c0e2017-08-18 02:47:08 -0700967 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -0800968
brandtr25445d32016-10-23 23:37:14 -0700969 RTC_DCHECK(receive_stream != nullptr);
brandtr25445d32016-10-23 23:37:14 -0700970 {
971 WriteLockScoped write_lock(*receive_crit_);
brandtrb29e6522016-12-21 06:37:18 -0800972
eladalon42f44f92017-07-25 06:40:06 -0700973 const FlexfecReceiveStream::Config& config = receive_stream->GetConfig();
nisse4709e892017-02-07 01:18:43 -0800974 uint32_t ssrc = config.remote_ssrc;
nissed44ce052017-02-06 02:23:00 -0800975 receive_rtp_config_.erase(ssrc);
brandtrb29e6522016-12-21 06:37:18 -0800976
brandtr7250b392016-12-19 01:13:46 -0800977 // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be
978 // destroyed.
nisse559af382017-03-21 06:41:12 -0700979 receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config))
nisse4709e892017-02-07 01:18:43 -0800980 ->RemoveStream(ssrc);
brandtr25445d32016-10-23 23:37:14 -0700981 }
brandtrb29e6522016-12-21 06:37:18 -0800982
eladalon42f44f92017-07-25 06:40:06 -0700983 delete receive_stream;
brandtr25445d32016-10-23 23:37:14 -0700984}
985
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100986RtpTransportControllerSendInterface* Call::GetTransportControllerSend() {
Sebastian Janssone6256052018-05-04 14:08:15 +0200987 return transport_send_ptr_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100988}
989
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000990Call::Stats Call::GetStats() const {
solenberg5a289392015-10-19 03:39:20 -0700991 // TODO(solenberg): Some test cases in EndToEndTest use this from a different
992 // thread. Re-enable once that is fixed.
eladalonf3f5c0e2017-08-18 02:47:08 -0700993 // RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000994 Stats stats;
Peter Boström45553ae2015-05-08 13:54:38 +0200995 // Fetch available send/receive bitrates.
Peter Boström45553ae2015-05-08 13:54:38 +0200996 std::vector<unsigned int> ssrcs;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000997 uint32_t recv_bandwidth = 0;
nisse559af382017-03-21 06:41:12 -0700998 receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate(
mflodmana20de202015-10-18 22:08:19 -0700999 &ssrcs, &recv_bandwidth);
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001000
1001 {
1002 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1003 stats.send_bandwidth_bps = last_bandwidth_bps_;
1004 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001005 stats.recv_bandwidth_bps = recv_bandwidth;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001006 // TODO(srte): It is unclear if we only want to report queues if network is
1007 // available.
1008 {
1009 rtc::CritScope cs(&aggregate_network_up_crit_);
Sebastian Janssone6256052018-05-04 14:08:15 +02001010 stats.pacer_delay_ms = aggregate_network_up_
1011 ? transport_send_ptr_->GetPacerQueuingDelayMs()
1012 : 0;
Sebastian Janssona06e9192018-03-07 18:49:55 +01001013 }
1014
Tommi38c5d932018-03-27 23:11:09 +02001015 stats.rtt_ms = call_stats_->LastProcessedRtt();
sprang9c0b5512016-07-06 00:54:28 -07001016 {
1017 rtc::CritScope cs(&bitrate_crit_);
1018 stats.max_padding_bitrate_bps = configured_max_padding_bitrate_bps_;
1019 }
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001020 return stats;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001021}
1022
Alex Narest78609d52017-10-20 10:37:47 +02001023void Call::SetBitrateAllocationStrategy(
1024 std::unique_ptr<rtc::BitrateAllocationStrategy>
1025 bitrate_allocation_strategy) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001026 // TODO(srte): This function should be moved to RtpTransportControllerSend
1027 // when BitrateAllocator is moved there.
1028 struct Functor {
1029 void operator()() {
1030 bitrate_allocator_->SetBitrateAllocationStrategy(
1031 std::move(bitrate_allocation_strategy_));
1032 }
1033 BitrateAllocator* bitrate_allocator_;
1034 std::unique_ptr<rtc::BitrateAllocationStrategy>
1035 bitrate_allocation_strategy_;
1036 };
1037 transport_send_ptr_->GetWorkerQueue()->PostTask(Functor{
1038 bitrate_allocator_.get(), std::move(bitrate_allocation_strategy)});
Alex Narest78609d52017-10-20 10:37:47 +02001039}
1040
skvlad7a43d252016-03-22 15:32:27 -07001041void Call::SignalChannelNetworkState(MediaType media, NetworkState state) {
eladalonf3f5c0e2017-08-18 02:47:08 -07001042 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001043 switch (media) {
1044 case MediaType::AUDIO:
1045 audio_network_state_ = state;
1046 break;
1047 case MediaType::VIDEO:
1048 video_network_state_ = state;
1049 break;
1050 case MediaType::ANY:
1051 case MediaType::DATA:
1052 RTC_NOTREACHED();
1053 break;
1054 }
1055
1056 UpdateAggregateNetworkState();
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001057 {
skvlad7a43d252016-03-22 15:32:27 -07001058 ReadLockScoped read_lock(*send_crit_);
solenbergc7a8b082015-10-16 14:35:07 -07001059 for (auto& kv : audio_send_ssrcs_) {
skvlad7a43d252016-03-22 15:32:27 -07001060 kv.second->SignalNetworkState(audio_network_state_);
solenbergc7a8b082015-10-16 14:35:07 -07001061 }
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001062 }
1063 {
skvlad7a43d252016-03-22 15:32:27 -07001064 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001065 for (AudioReceiveStream* audio_receive_stream : audio_receive_streams_) {
1066 audio_receive_stream->SignalNetworkState(audio_network_state_);
skvlad7a43d252016-03-22 15:32:27 -07001067 }
nissee4bcd6d2017-05-16 04:47:04 -07001068 for (VideoReceiveStream* video_receive_stream : video_receive_streams_) {
1069 video_receive_stream->SignalNetworkState(video_network_state_);
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001070 }
1071 }
1072}
1073
Stefan Holmer64be7fa2018-10-04 15:21:55 +02001074void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) {
1075 ReadLockScoped read_lock(*send_crit_);
1076 for (auto& kv : audio_send_ssrcs_) {
1077 kv.second->SetTransportOverhead(transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -08001078 }
1079}
1080
skvlad7a43d252016-03-22 15:32:27 -07001081void Call::UpdateAggregateNetworkState() {
eladalonf3f5c0e2017-08-18 02:47:08 -07001082 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
skvlad7a43d252016-03-22 15:32:27 -07001083
1084 bool have_audio = false;
1085 bool have_video = false;
1086 {
1087 ReadLockScoped read_lock(*send_crit_);
1088 if (audio_send_ssrcs_.size() > 0)
1089 have_audio = true;
1090 if (video_send_ssrcs_.size() > 0)
1091 have_video = true;
1092 }
1093 {
1094 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001095 if (audio_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001096 have_audio = true;
nissee4bcd6d2017-05-16 04:47:04 -07001097 if (video_receive_streams_.size() > 0)
skvlad7a43d252016-03-22 15:32:27 -07001098 have_video = true;
1099 }
1100
Sebastian Janssona06e9192018-03-07 18:49:55 +01001101 bool aggregate_network_up =
1102 ((have_video && video_network_state_ == kNetworkUp) ||
1103 (have_audio && audio_network_state_ == kNetworkUp));
skvlad7a43d252016-03-22 15:32:27 -07001104
Mirko Bonadei675513b2017-11-09 11:09:25 +01001105 RTC_LOG(LS_INFO) << "UpdateAggregateNetworkState: aggregate_state="
Sebastian Janssona06e9192018-03-07 18:49:55 +01001106 << (aggregate_network_up ? "up" : "down");
1107 {
1108 rtc::CritScope cs(&aggregate_network_up_crit_);
1109 aggregate_network_up_ = aggregate_network_up;
1110 }
Sebastian Janssone6256052018-05-04 14:08:15 +02001111 transport_send_ptr_->OnNetworkAvailability(aggregate_network_up);
skvlad7a43d252016-03-22 15:32:27 -07001112}
1113
stefanc1aeaf02015-10-15 07:26:07 -07001114void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
asapersson35151f32016-05-02 23:44:01 -07001115 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id,
1116 clock_->TimeInMilliseconds());
Sebastian Janssone6256052018-05-04 14:08:15 +02001117 transport_send_ptr_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07001118}
1119
Sebastian Jansson2701bc92018-12-11 15:02:47 +01001120void Call::OnStartRateUpdate(DataRate start_rate) {
1121 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1122 transport_send_ptr_->GetWorkerQueue()->PostTask(
1123 [this, start_rate] { this->OnStartRateUpdate(start_rate); });
1124 return;
1125 }
1126 bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>());
1127}
1128
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001129void Call::OnTargetTransferRate(TargetTransferRate msg) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001130 // TODO(bugs.webrtc.org/9719)
1131 // Call::OnTargetTransferRate requires that on target transfer rate is invoked
1132 // from the worker queue (because bitrate_allocator_ requires it). Media
1133 // transport does not guarantee the callback on the worker queue.
1134 // When the threading model for MediaTransportInterface is update, reconsider
1135 // changing this implementation.
1136 if (!transport_send_ptr_->GetWorkerQueue()->IsCurrent()) {
1137 transport_send_ptr_->GetWorkerQueue()->PostTask(
1138 [this, msg] { this->OnTargetTransferRate(msg); });
1139 return;
1140 }
1141
Sebastian Jansson19704ec2018-03-12 15:59:12 +01001142 uint32_t target_bitrate_bps = msg.target_rate.bps();
1143 int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
1144 uint8_t fraction_loss =
1145 rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
1146 int64_t rtt_ms = msg.network_estimate.round_trip_time.ms();
1147 int64_t probing_interval_ms = msg.network_estimate.bwe_period.ms();
1148 uint32_t bandwidth_bps = msg.network_estimate.bandwidth.bps();
1149 {
1150 rtc::CritScope cs(&last_bandwidth_bps_crit_);
1151 last_bandwidth_bps_ = bandwidth_bps;
1152 }
nisse559af382017-03-21 06:41:12 -07001153 // For controlling the rate of feedback messages.
1154 receive_side_cc_.OnBitrateChanged(target_bitrate_bps);
Sebastian Jansson89c94b92018-11-20 17:16:36 +01001155 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, bandwidth_bps,
1156 fraction_loss, rtt_ms,
1157 probing_interval_ms);
mflodman0e7e2592015-11-12 21:02:42 -08001158
asaperssonce2e1362016-09-09 00:13:35 -07001159 // Ignore updates if bitrate is zero (the aggregate network state is down).
1160 if (target_bitrate_bps == 0) {
stefan18adf0a2015-11-17 06:24:56 -08001161 rtc::CritScope lock(&bitrate_crit_);
asaperssonce2e1362016-09-09 00:13:35 -07001162 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1163 pacer_bitrate_kbps_counter_.ProcessAndPause();
1164 return;
stefan18adf0a2015-11-17 06:24:56 -08001165 }
asaperssonce2e1362016-09-09 00:13:35 -07001166
1167 bool sending_video;
1168 {
1169 ReadLockScoped read_lock(*send_crit_);
1170 sending_video = !video_send_streams_.empty();
1171 }
1172
1173 rtc::CritScope lock(&bitrate_crit_);
1174 if (!sending_video) {
1175 // Do not update the stats if we are not sending video.
1176 estimated_send_bitrate_kbps_counter_.ProcessAndPause();
1177 pacer_bitrate_kbps_counter_.ProcessAndPause();
1178 return;
1179 }
1180 estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000);
1181 // Pacer bitrate may be higher than bitrate estimate if enforcing min bitrate.
1182 uint32_t pacer_bitrate_bps =
1183 std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_);
1184 pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000);
perkj71ee44c2016-06-15 00:47:53 -07001185}
mflodman101f2502016-06-09 17:21:19 +02001186
perkj71ee44c2016-06-15 00:47:53 -07001187void Call::OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
philipelf69e7682018-02-28 13:06:28 +01001188 uint32_t max_padding_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +01001189 uint32_t total_bitrate_bps) {
Sebastian Janssone6256052018-05-04 14:08:15 +02001190 transport_send_ptr_->SetAllocatedSendBitrateLimits(
Oleh Prypin04d49502018-03-19 13:29:42 +00001191 min_send_bitrate_bps, max_padding_bitrate_bps, total_bitrate_bps);
Sebastian Jansson35fa2802018-10-01 09:16:12 +02001192
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -08001193 {
1194 rtc::CritScope lock(&target_observer_crit_);
1195 if (media_transport_) {
1196 MediaTransportAllocatedBitrateLimits limits;
1197 limits.min_pacing_rate = DataRate::bps(min_send_bitrate_bps);
1198 limits.max_padding_bitrate = DataRate::bps(max_padding_bitrate_bps);
1199 limits.max_total_allocated_bitrate = DataRate::bps(total_bitrate_bps);
1200 media_transport_->SetAllocatedBitrateLimits(limits);
1201 }
1202 }
1203
perkj71ee44c2016-06-15 00:47:53 -07001204 rtc::CritScope lock(&bitrate_crit_);
1205 min_allocated_send_bitrate_bps_ = min_send_bitrate_bps;
sprang9c0b5512016-07-06 00:54:28 -07001206 configured_max_padding_bitrate_bps_ = max_padding_bitrate_bps;
mflodman0e7e2592015-11-12 21:02:42 -08001207}
1208
pbos8fc7fa72015-07-15 08:02:58 -07001209void Call::ConfigureSync(const std::string& sync_group) {
1210 // Set sync only if there was no previous one.
solenberg3ebbcb52017-01-31 03:58:40 -08001211 if (sync_group.empty())
pbos8fc7fa72015-07-15 08:02:58 -07001212 return;
1213
1214 AudioReceiveStream* sync_audio_stream = nullptr;
1215 // Find existing audio stream.
1216 const auto it = sync_stream_mapping_.find(sync_group);
1217 if (it != sync_stream_mapping_.end()) {
1218 sync_audio_stream = it->second;
1219 } else {
1220 // No configured audio stream, see if we can find one.
nissee4bcd6d2017-05-16 04:47:04 -07001221 for (AudioReceiveStream* stream : audio_receive_streams_) {
1222 if (stream->config().sync_group == sync_group) {
pbos8fc7fa72015-07-15 08:02:58 -07001223 if (sync_audio_stream != nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001224 RTC_LOG(LS_WARNING)
1225 << "Attempting to sync more than one audio stream "
1226 "within the same sync group. This is not "
1227 "supported in the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001228 break;
1229 }
nissee4bcd6d2017-05-16 04:47:04 -07001230 sync_audio_stream = stream;
pbos8fc7fa72015-07-15 08:02:58 -07001231 }
1232 }
1233 }
1234 if (sync_audio_stream)
1235 sync_stream_mapping_[sync_group] = sync_audio_stream;
1236 size_t num_synced_streams = 0;
1237 for (VideoReceiveStream* video_stream : video_receive_streams_) {
1238 if (video_stream->config().sync_group != sync_group)
1239 continue;
1240 ++num_synced_streams;
1241 if (num_synced_streams > 1) {
1242 // TODO(pbos): Support synchronizing more than one A/V pair.
1243 // https://code.google.com/p/webrtc/issues/detail?id=4762
Mirko Bonadei675513b2017-11-09 11:09:25 +01001244 RTC_LOG(LS_WARNING)
1245 << "Attempting to sync more than one audio/video pair "
1246 "within the same sync group. This is not supported in "
1247 "the current implementation.";
pbos8fc7fa72015-07-15 08:02:58 -07001248 }
1249 // Only sync the first A/V pair within this sync group.
solenberg3ebbcb52017-01-31 03:58:40 -08001250 if (num_synced_streams == 1) {
1251 // sync_audio_stream may be null and that's ok.
1252 video_stream->SetSync(sync_audio_stream);
pbos8fc7fa72015-07-15 08:02:58 -07001253 } else {
solenberg3ebbcb52017-01-31 03:58:40 -08001254 video_stream->SetSync(nullptr);
pbos8fc7fa72015-07-15 08:02:58 -07001255 }
1256 }
1257}
1258
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001259PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
1260 const uint8_t* packet,
1261 size_t length) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001262 TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
mflodman3d7db262016-04-29 00:57:13 -07001263 // TODO(pbos): Make sure it's a valid packet.
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001264 // Return DELIVERY_UNKNOWN_SSRC if it can be determined that
1265 // there's no receiver of the packet.
asapersson250fd972016-09-08 00:07:21 -07001266 if (received_bytes_per_second_counter_.HasSample()) {
1267 // First RTP packet has been received.
1268 received_bytes_per_second_counter_.Add(static_cast<int>(length));
1269 received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(length));
1270 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001271 bool rtcp_delivered = false;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001272 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001273 ReadLockScoped read_lock(*receive_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001274 for (VideoReceiveStream* stream : video_receive_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001275 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001276 rtcp_delivered = true;
mflodman3d7db262016-04-29 00:57:13 -07001277 }
1278 }
1279 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1280 ReadLockScoped read_lock(*receive_crit_);
nissee4bcd6d2017-05-16 04:47:04 -07001281 for (AudioReceiveStream* stream : audio_receive_streams_) {
1282 if (stream->DeliverRtcp(packet, length))
mflodman3d7db262016-04-29 00:57:13 -07001283 rtcp_delivered = true;
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +00001284 }
1285 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001286 if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001287 ReadLockScoped read_lock(*send_crit_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001288 for (VideoSendStream* stream : video_send_streams_) {
mflodman3d7db262016-04-29 00:57:13 -07001289 if (stream->DeliverRtcp(packet, length))
pbos@webrtc.org40523702013-08-05 12:49:22 +00001290 rtcp_delivered = true;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001291 }
1292 }
mflodman3d7db262016-04-29 00:57:13 -07001293 if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
1294 ReadLockScoped read_lock(*send_crit_);
1295 for (auto& kv : audio_send_ssrcs_) {
1296 if (kv.second->DeliverRtcp(packet, length))
1297 rtcp_delivered = true;
1298 }
1299 }
1300
Elad Alon4a87e1c2017-10-03 16:11:34 +02001301 if (rtcp_delivered) {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001302 event_log_->Log(absl::make_unique<RtcEventRtcpPacketIncoming>(
Elad Alon4a87e1c2017-10-03 16:11:34 +02001303 rtc::MakeArrayView(packet, length)));
1304 }
mflodman3d7db262016-04-29 00:57:13 -07001305
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +00001306 return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001307}
1308
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001309PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001310 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001311 int64_t packet_time_us) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001312 TRACE_EVENT0("webrtc", "Call::DeliverRtp");
nissed44ce052017-02-06 02:23:00 -08001313
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001314 RtpPacketReceived parsed_packet;
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001315 if (!parsed_packet.Parse(std::move(packet)))
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001316 return DELIVERY_PACKET_ERROR;
1317
Niels Möller70082872018-08-07 11:03:12 +02001318 if (packet_time_us != -1) {
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001319 if (receive_time_calculator_) {
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001320 // Repair packet_time_us for clock resets by comparing a new read of
1321 // the same clock (TimeUTCMicros) to a monotonic clock reading.
Niels Möller70082872018-08-07 11:03:12 +02001322 packet_time_us = receive_time_calculator_->ReconcileReceiveTimes(
Christoffer Rodbro992a8682018-10-30 15:14:36 +01001323 packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds());
Sebastian Janssonb34556e2018-03-21 14:38:32 +01001324 }
Niels Möller70082872018-08-07 11:03:12 +02001325 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001326 } else {
1327 parsed_packet.set_arrival_time_ms(clock_->TimeInMilliseconds());
1328 }
nissed44ce052017-02-06 02:23:00 -08001329
sprangc1abde72017-07-11 03:56:21 -07001330 // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6.
1331 // These are empty (zero length payload) RTP packets with an unsignaled
1332 // payload type.
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001333 const bool is_keep_alive_packet = parsed_packet.payload_size() == 0;
sprangc1abde72017-07-11 03:56:21 -07001334
1335 RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO ||
1336 is_keep_alive_packet);
1337
sprangc1abde72017-07-11 03:56:21 -07001338 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001339 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
nisse0f15f922017-06-21 01:05:22 -07001340 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001341 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1342 << parsed_packet.Ssrc();
nisse0f15f922017-06-21 01:05:22 -07001343 // Destruction of the receive stream, including deregistering from the
1344 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1345 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1346 // So by not passing the packet on to demuxing in this case, we prevent
1347 // incoming packets to be passed on via the demuxer to a receive stream
1348 // which is being torned down.
1349 return DELIVERY_UNKNOWN_SSRC;
1350 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001351 parsed_packet.IdentifyExtensions(it->second.extensions);
nisse0f15f922017-06-21 01:05:22 -07001352
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001353 NotifyBweOfReceivedPacket(parsed_packet, media_type);
nissed44ce052017-02-06 02:23:00 -08001354
Danil Chapovalovcbf5b732017-12-08 14:05:20 +01001355 // RateCounters expect input parameter as int, save it as int,
1356 // instead of converting each time it is passed to RateCounter::Add below.
1357 int length = static_cast<int>(parsed_packet.size());
nissee5ad5ca2017-03-29 23:57:43 -07001358 if (media_type == MediaType::AUDIO) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001359 if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001360 received_bytes_per_second_counter_.Add(length);
1361 received_audio_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001362 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001363 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001364 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001365 if (!first_received_rtp_audio_ms_) {
1366 first_received_rtp_audio_ms_.emplace(arrival_time_ms);
1367 }
1368 last_received_rtp_audio_ms_.emplace(arrival_time_ms);
nisse657bab22017-02-21 06:28:10 -08001369 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001370 }
nissee4bcd6d2017-05-16 04:47:04 -07001371 } else if (media_type == MediaType::VIDEO) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001372 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001373 if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001374 received_bytes_per_second_counter_.Add(length);
1375 received_video_bytes_per_second_counter_.Add(length);
Elad Alon4a87e1c2017-10-03 16:11:34 +02001376 event_log_->Log(
Karl Wiberg918f50c2018-07-05 11:40:33 +02001377 absl::make_unique<RtcEventRtpPacketIncoming>(parsed_packet));
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001378 const int64_t arrival_time_ms = parsed_packet.arrival_time_ms();
saza0d7f04d2017-07-04 04:05:06 -07001379 if (!first_received_rtp_video_ms_) {
1380 first_received_rtp_video_ms_.emplace(arrival_time_ms);
1381 }
1382 last_received_rtp_video_ms_.emplace(arrival_time_ms);
nisse5c29a7a2017-02-16 06:52:32 -08001383 return DELIVERY_OK;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001384 }
1385 }
1386 return DELIVERY_UNKNOWN_SSRC;
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001387}
1388
stefan68786d22015-09-08 05:36:15 -07001389PacketReceiver::DeliveryStatus Call::DeliverPacket(
1390 MediaType media_type,
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001391 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +02001392 int64_t packet_time_us) {
eladalond1dd2f72017-08-25 02:55:57 -07001393 RTC_DCHECK_CALLED_SEQUENTIALLY(&configuration_sequence_checker_);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001394 if (RtpHeaderParser::IsRtcp(packet.cdata(), packet.size()))
1395 return DeliverRtcp(media_type, packet.cdata(), packet.size());
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001396
Niels Möller70082872018-08-07 11:03:12 +02001397 return DeliverRtp(media_type, std::move(packet), packet_time_us);
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001398}
1399
nissed2ef3142017-05-11 08:00:58 -07001400void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001401 RtpPacketReceived parsed_packet;
1402 if (!parsed_packet.Parse(packet, length))
nissed2ef3142017-05-11 08:00:58 -07001403 return;
1404
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001405 parsed_packet.set_recovered(true);
nissed2ef3142017-05-11 08:00:58 -07001406
brandtrcaea68f2017-08-23 00:55:17 -07001407 ReadLockScoped read_lock(*receive_crit_);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001408 auto it = receive_rtp_config_.find(parsed_packet.Ssrc());
brandtrcaea68f2017-08-23 00:55:17 -07001409 if (it == receive_rtp_config_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001410 RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc "
1411 << parsed_packet.Ssrc();
brandtrcaea68f2017-08-23 00:55:17 -07001412 // Destruction of the receive stream, including deregistering from the
1413 // RtpDemuxer, is not protected by the |receive_crit_| lock. But
1414 // deregistering in the |receive_rtp_config_| map is protected by that lock.
1415 // So by not passing the packet on to demuxing in this case, we prevent
1416 // incoming packets to be passed on via the demuxer to a receive stream
Erik Språng09708512018-03-14 15:16:50 +01001417 // which is being torn down.
brandtrcaea68f2017-08-23 00:55:17 -07001418 return;
1419 }
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001420 parsed_packet.IdentifyExtensions(it->second.extensions);
brandtrcaea68f2017-08-23 00:55:17 -07001421
1422 // TODO(brandtr): Update here when we support protecting audio packets too.
Niels Möller2ff1f2a2018-08-09 16:16:34 +02001423 parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Danil Chapovalovb709cf82017-10-04 14:01:45 +02001424 video_receiver_controller_.OnRtpPacket(parsed_packet);
brandtr4e523862016-10-18 23:50:45 -07001425}
1426
nissed44ce052017-02-06 02:23:00 -08001427void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet,
1428 MediaType media_type) {
1429 auto it = receive_rtp_config_.find(packet.Ssrc());
nisse4709e892017-02-07 01:18:43 -08001430 bool use_send_side_bwe =
1431 (it != receive_rtp_config_.end()) && it->second.use_send_side_bwe;
nissed44ce052017-02-06 02:23:00 -08001432
brandtrb29e6522016-12-21 06:37:18 -08001433 RTPHeader header;
1434 packet.GetHeader(&header);
nissed44ce052017-02-06 02:23:00 -08001435
nisse4709e892017-02-07 01:18:43 -08001436 if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) {
nissed44ce052017-02-06 02:23:00 -08001437 // Inconsistent configuration of send side BWE. Do nothing.
1438 // TODO(nisse): Without this check, we may produce RTCP feedback
1439 // packets even when not negotiated. But it would be cleaner to
1440 // move the check down to RTCPSender::SendFeedbackPacket, which
1441 // would also help the PacketRouter to select an appropriate rtp
1442 // module in the case that some, but not all, have RTCP feedback
1443 // enabled.
1444 return;
1445 }
1446 // For audio, we only support send side BWE.
nissee5ad5ca2017-03-29 23:57:43 -07001447 if (media_type == MediaType::VIDEO ||
nisse4709e892017-02-07 01:18:43 -08001448 (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) {
nisse559af382017-03-21 06:41:12 -07001449 receive_side_cc_.OnReceivedPacket(
nissed44ce052017-02-06 02:23:00 -08001450 packet.arrival_time_ms(), packet.payload_size() + packet.padding_size(),
1451 header);
1452 }
brandtrb29e6522016-12-21 06:37:18 -08001453}
1454
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001455} // namespace internal
nisseb8f9a322017-03-27 05:36:15 -07001456
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001457} // namespace webrtc