blob: f9ef569670a53500520f3020d250d101d21e6ea3 [file] [log] [blame]
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +00001/*
2 * Copyright (c) 2015 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
Stefan Holmer9416ef82018-07-19 10:34:38 +020011#include "call/rtp_video_sender.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000012
philipel25d31ec2018-08-08 16:33:01 +020013#include <algorithm>
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020014#include <memory>
15#include <string>
16#include <utility>
17
Erik Språng845c6aa2019-05-29 13:02:24 +020018#include "absl/algorithm/container.h"
Erik Språng6cf554e2019-10-02 20:55:39 +020019#include "absl/strings/match.h"
Erik Språng490d76c2019-05-07 09:29:15 -070020#include "api/array_view.h"
Niels Möller5fe95102019-03-04 16:49:25 +010021#include "api/transport/field_trial_based_config.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020022#include "call/rtp_transport_controller_send_interface.h"
23#include "modules/pacing/packet_router.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/rtp_rtcp/include/rtp_rtcp.h"
25#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Niels Möller5fe95102019-03-04 16:49:25 +010026#include "modules/rtp_rtcp/source/playout_delay_oracle.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020027#include "modules/rtp_rtcp/source/rtp_sender.h"
28#include "modules/utility/include/process_thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/video_coding/include/video_codec_interface.h"
30#include "rtc_base/checks.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020031#include "rtc_base/location.h"
32#include "rtc_base/logging.h"
33#include "system_wrappers/include/field_trial.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000034
35namespace webrtc {
36
Niels Möller5fe95102019-03-04 16:49:25 +010037namespace webrtc_internal_rtp_video_sender {
38
39RtpStreamSender::RtpStreamSender(
40 std::unique_ptr<PlayoutDelayOracle> playout_delay_oracle,
41 std::unique_ptr<RtpRtcp> rtp_rtcp,
42 std::unique_ptr<RTPSenderVideo> sender_video)
43 : playout_delay_oracle(std::move(playout_delay_oracle)),
44 rtp_rtcp(std::move(rtp_rtcp)),
45 sender_video(std::move(sender_video)) {}
46
47RtpStreamSender::~RtpStreamSender() = default;
48
49} // namespace webrtc_internal_rtp_video_sender
50
kjellander02b3d272016-04-20 05:05:54 -070051namespace {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020052static const int kMinSendSidePacketHistorySize = 600;
Stefan Holmer64be7fa2018-10-04 15:21:55 +020053// We don't do MTU discovery, so assume that we have the standard ethernet MTU.
54static const size_t kPathMTU = 1500;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020055
Niels Möller5fe95102019-03-04 16:49:25 +010056using webrtc_internal_rtp_video_sender::RtpStreamSender;
57
Erik Språngdc34a252019-10-04 15:17:29 +020058bool PayloadTypeSupportsSkippingFecPackets(const std::string& payload_name) {
59 const VideoCodecType codecType = PayloadStringToCodecType(payload_name);
60 if (codecType == kVideoCodecVP8 || codecType == kVideoCodecVP9) {
61 return true;
62 }
63 if (codecType == kVideoCodecGeneric &&
64 field_trial::IsEnabled("WebRTC-GenericPictureId")) {
65 return true;
66 }
67 return false;
68}
69
70bool ShouldDisableRedAndUlpfec(bool flexfec_enabled,
71 const RtpConfig& rtp_config) {
72 // Consistency of NACK and RED+ULPFEC parameters is checked in this function.
73 const bool nack_enabled = rtp_config.nack.rtp_history_ms > 0;
74
75 // Shorthands.
76 auto IsRedEnabled = [&]() { return rtp_config.ulpfec.red_payload_type >= 0; };
77 auto IsUlpfecEnabled = [&]() {
78 return rtp_config.ulpfec.ulpfec_payload_type >= 0;
79 };
80
81 bool should_disable_red_and_ulpfec = false;
82
83 if (webrtc::field_trial::IsEnabled("WebRTC-DisableUlpFecExperiment")) {
84 RTC_LOG(LS_INFO) << "Experiment to disable sending ULPFEC is enabled.";
85 should_disable_red_and_ulpfec = true;
86 }
87
88 // If enabled, FlexFEC takes priority over RED+ULPFEC.
89 if (flexfec_enabled) {
90 if (IsUlpfecEnabled()) {
91 RTC_LOG(LS_INFO)
92 << "Both FlexFEC and ULPFEC are configured. Disabling ULPFEC.";
93 }
94 should_disable_red_and_ulpfec = true;
95 }
96
97 // Payload types without picture ID cannot determine that a stream is complete
98 // without retransmitting FEC, so using ULPFEC + NACK for H.264 (for instance)
99 // is a waste of bandwidth since FEC packets still have to be transmitted.
100 // Note that this is not the case with FlexFEC.
101 if (nack_enabled && IsUlpfecEnabled() &&
102 !PayloadTypeSupportsSkippingFecPackets(rtp_config.payload_name)) {
103 RTC_LOG(LS_WARNING)
104 << "Transmitting payload type without picture ID using "
105 "NACK+ULPFEC is a waste of bandwidth since ULPFEC packets "
106 "also have to be retransmitted. Disabling ULPFEC.";
107 should_disable_red_and_ulpfec = true;
108 }
109
110 // Verify payload types.
111 if (IsUlpfecEnabled() ^ IsRedEnabled()) {
112 RTC_LOG(LS_WARNING)
113 << "Only RED or only ULPFEC enabled, but not both. Disabling both.";
114 should_disable_red_and_ulpfec = true;
115 }
116
117 return should_disable_red_and_ulpfec;
118}
119
Niels Möller5fe95102019-03-04 16:49:25 +0100120std::vector<RtpStreamSender> CreateRtpStreamSenders(
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100121 Clock* clock,
Johannes Kron9190b822018-10-29 11:22:05 +0100122 const RtpConfig& rtp_config,
Erik Språng7ea9b802019-10-16 19:03:38 +0200123 const RtpSenderObservers& observers,
Jiawei Ou55718122018-11-09 13:17:39 -0800124 int rtcp_report_interval_ms,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200125 Transport* send_transport,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200126 RtcpBandwidthObserver* bandwidth_callback,
127 RtpTransportControllerSendInterface* transport,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200128 FlexfecSender* flexfec_sender,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200129 RtcEventLog* event_log,
130 RateLimiter* retransmission_rate_limiter,
131 OverheadObserver* overhead_observer,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700132 FrameEncryptorInterface* frame_encryptor,
133 const CryptoOptions& crypto_options) {
Amit Hilbuch0fc28432018-12-18 13:01:47 -0800134 RTC_DCHECK_GT(rtp_config.ssrcs.size(), 0);
Benjamin Wright192eeec2018-10-17 17:27:25 -0700135
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200136 RtpRtcp::Configuration configuration;
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100137 configuration.clock = clock;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200138 configuration.audio = false;
139 configuration.receiver_only = false;
140 configuration.outgoing_transport = send_transport;
Erik Språng7ea9b802019-10-16 19:03:38 +0200141 configuration.intra_frame_callback = observers.intra_frame_callback;
Elad Alon0a8562e2019-04-09 11:55:13 +0200142 configuration.rtcp_loss_notification_observer =
Erik Språng7ea9b802019-10-16 19:03:38 +0200143 observers.rtcp_loss_notification_observer;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200144 configuration.bandwidth_callback = bandwidth_callback;
Sebastian Janssone1795f42019-07-24 11:38:03 +0200145 configuration.network_state_estimate_observer =
146 transport->network_state_estimate_observer();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200147 configuration.transport_feedback_callback =
148 transport->transport_feedback_observer();
Erik Språng7ea9b802019-10-16 19:03:38 +0200149 configuration.rtt_stats = observers.rtcp_rtt_stats;
150 configuration.rtcp_packet_type_counter_observer =
151 observers.rtcp_type_observer;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200152 configuration.paced_sender = transport->packet_sender();
Erik Språng7ea9b802019-10-16 19:03:38 +0200153 configuration.send_bitrate_observer = observers.bitrate_observer;
154 configuration.send_side_delay_observer = observers.send_delay_observer;
155 configuration.send_packet_observer = observers.send_packet_observer;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200156 configuration.event_log = event_log;
157 configuration.retransmission_rate_limiter = retransmission_rate_limiter;
158 configuration.overhead_observer = overhead_observer;
Erik Språng7ea9b802019-10-16 19:03:38 +0200159 configuration.rtp_stats_callback = observers.rtp_stats;
Benjamin Wright192eeec2018-10-17 17:27:25 -0700160 configuration.frame_encryptor = frame_encryptor;
161 configuration.require_frame_encryption =
162 crypto_options.sframe.require_frame_encryption;
Johannes Kron9190b822018-10-29 11:22:05 +0100163 configuration.extmap_allow_mixed = rtp_config.extmap_allow_mixed;
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800164 configuration.rtcp_report_interval_ms = rtcp_report_interval_ms;
Benjamin Wright192eeec2018-10-17 17:27:25 -0700165
Niels Möller5fe95102019-03-04 16:49:25 +0100166 std::vector<RtpStreamSender> rtp_streams;
Johannes Kron9190b822018-10-29 11:22:05 +0100167 const std::vector<uint32_t>& flexfec_protected_ssrcs =
168 rtp_config.flexfec.protected_media_ssrcs;
Erik Språng93f51892019-08-19 12:42:58 +0200169 RTC_DCHECK(rtp_config.rtx.ssrcs.empty() ||
170 rtp_config.rtx.ssrcs.size() == rtp_config.rtx.ssrcs.size());
171 for (size_t i = 0; i < rtp_config.ssrcs.size(); ++i) {
Erik Språng54d5d2c2019-08-20 17:22:36 +0200172 configuration.local_media_ssrc = rtp_config.ssrcs[i];
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200173 bool enable_flexfec = flexfec_sender != nullptr &&
174 std::find(flexfec_protected_ssrcs.begin(),
175 flexfec_protected_ssrcs.end(),
Erik Språng6841d252019-10-15 14:29:11 +0200176 configuration.local_media_ssrc) !=
Erik Språng93f51892019-08-19 12:42:58 +0200177 flexfec_protected_ssrcs.end();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200178 configuration.flexfec_sender = enable_flexfec ? flexfec_sender : nullptr;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200179 auto playout_delay_oracle = std::make_unique<PlayoutDelayOracle>();
Niels Möller5fe95102019-03-04 16:49:25 +0100180
181 configuration.ack_observer = playout_delay_oracle.get();
Erik Språng93f51892019-08-19 12:42:58 +0200182 if (rtp_config.rtx.ssrcs.size() > i) {
183 configuration.rtx_send_ssrc = rtp_config.rtx.ssrcs[i];
184 }
185
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +0100186 auto rtp_rtcp = RtpRtcp::Create(configuration);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200187 rtp_rtcp->SetSendingStatus(false);
188 rtp_rtcp->SetSendingMediaStatus(false);
189 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
Erik Språngdc34a252019-10-04 15:17:29 +0200190 // Set NACK.
191 rtp_rtcp->SetStorePacketsStatus(true, kMinSendSidePacketHistorySize);
Niels Möller5fe95102019-03-04 16:49:25 +0100192
Erik Språngdc34a252019-10-04 15:17:29 +0200193 FieldTrialBasedConfig field_trial_config;
194 RTPSenderVideo::Config video_config;
195 video_config.clock = configuration.clock;
196 video_config.rtp_sender = rtp_rtcp->RtpSender();
197 video_config.flexfec_sender = configuration.flexfec_sender;
198 video_config.playout_delay_oracle = playout_delay_oracle.get();
199 video_config.frame_encryptor = frame_encryptor;
200 video_config.require_frame_encryption =
201 crypto_options.sframe.require_frame_encryption;
202 video_config.need_rtp_packet_infos = rtp_config.lntf.enabled;
203 video_config.enable_retransmit_all_layers = false;
204 video_config.field_trials = &field_trial_config;
205 const bool should_disable_red_and_ulpfec =
206 ShouldDisableRedAndUlpfec(enable_flexfec, rtp_config);
207 if (rtp_config.ulpfec.red_payload_type != -1 &&
208 !should_disable_red_and_ulpfec) {
209 video_config.red_payload_type = rtp_config.ulpfec.red_payload_type;
210 }
211 if (rtp_config.ulpfec.ulpfec_payload_type != -1 &&
212 !should_disable_red_and_ulpfec) {
213 video_config.ulpfec_payload_type = rtp_config.ulpfec.ulpfec_payload_type;
214 }
215 auto sender_video = std::make_unique<RTPSenderVideo>(video_config);
Niels Möller5fe95102019-03-04 16:49:25 +0100216 rtp_streams.emplace_back(std::move(playout_delay_oracle),
217 std::move(rtp_rtcp), std::move(sender_video));
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200218 }
Niels Möller5fe95102019-03-04 16:49:25 +0100219 return rtp_streams;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200220}
221
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200222// TODO(brandtr): Update this function when we support multistream protection.
223std::unique_ptr<FlexfecSender> MaybeCreateFlexfecSender(
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100224 Clock* clock,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200225 const RtpConfig& rtp,
226 const std::map<uint32_t, RtpState>& suspended_ssrcs) {
227 if (rtp.flexfec.payload_type < 0) {
228 return nullptr;
229 }
230 RTC_DCHECK_GE(rtp.flexfec.payload_type, 0);
231 RTC_DCHECK_LE(rtp.flexfec.payload_type, 127);
232 if (rtp.flexfec.ssrc == 0) {
233 RTC_LOG(LS_WARNING) << "FlexFEC is enabled, but no FlexFEC SSRC given. "
234 "Therefore disabling FlexFEC.";
235 return nullptr;
236 }
237 if (rtp.flexfec.protected_media_ssrcs.empty()) {
238 RTC_LOG(LS_WARNING)
239 << "FlexFEC is enabled, but no protected media SSRC given. "
240 "Therefore disabling FlexFEC.";
241 return nullptr;
242 }
243
244 if (rtp.flexfec.protected_media_ssrcs.size() > 1) {
245 RTC_LOG(LS_WARNING)
246 << "The supplied FlexfecConfig contained multiple protected "
247 "media streams, but our implementation currently only "
248 "supports protecting a single media stream. "
249 "To avoid confusion, disabling FlexFEC completely.";
250 return nullptr;
251 }
252
253 const RtpState* rtp_state = nullptr;
254 auto it = suspended_ssrcs.find(rtp.flexfec.ssrc);
255 if (it != suspended_ssrcs.end()) {
256 rtp_state = &it->second;
257 }
258
259 RTC_DCHECK_EQ(1U, rtp.flexfec.protected_media_ssrcs.size());
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200260 return std::make_unique<FlexfecSender>(
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200261 rtp.flexfec.payload_type, rtp.flexfec.ssrc,
262 rtp.flexfec.protected_media_ssrcs[0], rtp.mid, rtp.extensions,
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100263 RTPSender::FecExtensionSizes(), rtp_state, clock);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200264}
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200265
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200266DataRate CalculateOverheadRate(DataRate data_rate,
267 DataSize packet_size,
268 DataSize overhead_per_packet) {
269 Frequency packet_rate = data_rate / packet_size;
270 // TOSO(srte): We should not need to round to nearest whole packet per second
271 // rate here.
272 return packet_rate.RoundUpTo(Frequency::hertz(1)) * overhead_per_packet;
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200273}
Erik Språng6cf554e2019-10-02 20:55:39 +0200274
275absl::optional<VideoCodecType> GetVideoCodecType(const RtpConfig& config) {
276 absl::optional<VideoCodecType> video_type;
277 if (!config.raw_payload) {
278 if (absl::EqualsIgnoreCase(config.payload_name, "VP8")) {
279 video_type = kVideoCodecVP8;
280 } else if (absl::EqualsIgnoreCase(config.payload_name, "VP9")) {
281 video_type = kVideoCodecVP9;
282 } else if (absl::EqualsIgnoreCase(config.payload_name, "H264")) {
283 video_type = kVideoCodecH264;
284 } else {
285 video_type = kVideoCodecGeneric;
286 }
287 }
288 return video_type;
289}
kjellander02b3d272016-04-20 05:05:54 -0700290} // namespace
291
Stefan Holmer9416ef82018-07-19 10:34:38 +0200292RtpVideoSender::RtpVideoSender(
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100293 Clock* clock,
Stefan Holmer9416ef82018-07-19 10:34:38 +0200294 std::map<uint32_t, RtpState> suspended_ssrcs,
295 const std::map<uint32_t, RtpPayloadState>& states,
296 const RtpConfig& rtp_config,
Jiawei Ou55718122018-11-09 13:17:39 -0800297 int rtcp_report_interval_ms,
Stefan Holmer9416ef82018-07-19 10:34:38 +0200298 Transport* send_transport,
299 const RtpSenderObservers& observers,
300 RtpTransportControllerSendInterface* transport,
301 RtcEventLog* event_log,
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200302 RateLimiter* retransmission_limiter,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700303 std::unique_ptr<FecController> fec_controller,
304 FrameEncryptorInterface* frame_encryptor,
305 const CryptoOptions& crypto_options)
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200306 : send_side_bwe_with_overhead_(
307 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
Erik Språngc12d41b2019-01-09 09:55:31 +0100308 account_for_packetization_overhead_(!webrtc::field_trial::IsDisabled(
309 "WebRTC-SubtractPacketizationOverhead")),
Erik Språng845c6aa2019-05-29 13:02:24 +0200310 use_early_loss_detection_(
311 !webrtc::field_trial::IsDisabled("WebRTC-UseEarlyLossDetection")),
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200312 active_(false),
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200313 module_process_thread_(nullptr),
314 suspended_ssrcs_(std::move(suspended_ssrcs)),
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100315 flexfec_sender_(
316 MaybeCreateFlexfecSender(clock, rtp_config, suspended_ssrcs_)),
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200317 fec_controller_(std::move(fec_controller)),
Elad Alon67daf712019-06-28 18:14:36 +0200318 fec_allowed_(true),
Erik Språng7ea9b802019-10-16 19:03:38 +0200319 rtp_streams_(CreateRtpStreamSenders(clock,
320 rtp_config,
321 observers,
322 rtcp_report_interval_ms,
323 send_transport,
324 transport->GetBandwidthObserver(),
325 transport,
326 flexfec_sender_.get(),
327 event_log,
328 retransmission_limiter,
329 this,
330 frame_encryptor,
331 crypto_options)),
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200332 rtp_config_(rtp_config),
Erik Språng6cf554e2019-10-02 20:55:39 +0200333 codec_type_(GetVideoCodecType(rtp_config)),
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200334 transport_(transport),
335 transport_overhead_bytes_per_packet_(0),
336 overhead_bytes_per_packet_(0),
Niels Möller949f0fd2019-01-29 09:44:24 +0100337 encoder_target_rate_bps_(0),
338 frame_counts_(rtp_config.ssrcs.size()),
Oleh Prypine8964902019-03-29 15:33:01 +0000339 frame_count_observer_(observers.frame_count_observer) {
Elad Alon62ce0352019-05-23 16:58:53 +0200340 RTC_DCHECK_EQ(rtp_config_.ssrcs.size(), rtp_streams_.size());
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200341 module_process_thread_checker_.Detach();
Åsa Persson4bece9a2017-10-06 10:04:04 +0200342 // SSRCs are assumed to be sorted in the same order as |rtp_modules|.
Elad Alon62ce0352019-05-23 16:58:53 +0200343 for (uint32_t ssrc : rtp_config_.ssrcs) {
Åsa Persson4bece9a2017-10-06 10:04:04 +0200344 // Restore state if it previously existed.
345 const RtpPayloadState* state = nullptr;
346 auto it = states.find(ssrc);
347 if (it != states.end()) {
348 state = &it->second;
philipel25d31ec2018-08-08 16:33:01 +0200349 shared_frame_id_ = std::max(shared_frame_id_, state->shared_frame_id);
Åsa Persson4bece9a2017-10-06 10:04:04 +0200350 }
351 params_.push_back(RtpPayloadParams(ssrc, state));
352 }
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200353
354 // RTP/RTCP initialization.
355
356 // We add the highest spatial layer first to ensure it'll be prioritized
357 // when sending padding, with the hope that the packet rate will be smaller,
358 // and that it's more important to protect than the lower layers.
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200359
360 // TODO(nisse): Consider moving registration with PacketRouter last, after the
361 // modules are fully configured.
Niels Möller5fe95102019-03-04 16:49:25 +0100362 for (const RtpStreamSender& stream : rtp_streams_) {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200363 constexpr bool remb_candidate = true;
Niels Möller5fe95102019-03-04 16:49:25 +0100364 transport->packet_router()->AddSendRtpModule(stream.rtp_rtcp.get(),
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200365 remb_candidate);
366 }
367
368 for (size_t i = 0; i < rtp_config_.extensions.size(); ++i) {
369 const std::string& extension = rtp_config_.extensions[i].uri;
370 int id = rtp_config_.extensions[i].id;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200371 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
Niels Möller5fe95102019-03-04 16:49:25 +0100372 for (const RtpStreamSender& stream : rtp_streams_) {
Sebastian Janssonf39c8152019-10-14 17:32:21 +0200373 stream.rtp_rtcp->RegisterRtpHeaderExtension(extension, id);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200374 }
375 }
376
Elad Alon62ce0352019-05-23 16:58:53 +0200377 ConfigureSsrcs();
378 ConfigureRids();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200379
Elad Alon62ce0352019-05-23 16:58:53 +0200380 if (!rtp_config_.mid.empty()) {
Niels Möller5fe95102019-03-04 16:49:25 +0100381 for (const RtpStreamSender& stream : rtp_streams_) {
Elad Alon62ce0352019-05-23 16:58:53 +0200382 stream.rtp_rtcp->SetMid(rtp_config_.mid);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200383 }
384 }
385
Niels Möller5fe95102019-03-04 16:49:25 +0100386 for (const RtpStreamSender& stream : rtp_streams_) {
Amit Hilbuch38e6c662019-03-08 16:17:21 -0800387 // Simulcast has one module for each layer. Set the CNAME on all modules.
Elad Alon62ce0352019-05-23 16:58:53 +0200388 stream.rtp_rtcp->SetCNAME(rtp_config_.c_name.c_str());
Niels Möller5fe95102019-03-04 16:49:25 +0100389 stream.rtp_rtcp->RegisterRtcpStatisticsCallback(observers.rtcp_stats);
Henrik Boström87e3f9d2019-05-27 10:44:24 +0200390 stream.rtp_rtcp->SetReportBlockDataObserver(
391 observers.report_block_data_observer);
Elad Alon62ce0352019-05-23 16:58:53 +0200392 stream.rtp_rtcp->SetMaxRtpPacketSize(rtp_config_.max_packet_size);
393 stream.rtp_rtcp->RegisterSendPayloadFrequency(rtp_config_.payload_type,
Niels Möller5fe95102019-03-04 16:49:25 +0100394 kVideoPayloadTypeFrequency);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200395 }
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200396 // Currently, both ULPFEC and FlexFEC use the same FEC rate calculation logic,
397 // so enable that logic if either of those FEC schemes are enabled.
398 fec_controller_->SetProtectionMethod(FecEnabled(), NackEnabled());
399
400 fec_controller_->SetProtectionCallback(this);
401 // Signal congestion controller this object is ready for OnPacket* callbacks.
Sebastian Janssonf2988552019-10-29 17:18:51 +0100402 transport_->GetStreamFeedbackProvider()->RegisterStreamFeedbackObserver(
403 rtp_config_.ssrcs, this);
Per83d09102016-04-15 14:59:13 +0200404}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000405
Stefan Holmer9416ef82018-07-19 10:34:38 +0200406RtpVideoSender::~RtpVideoSender() {
Niels Möller5fe95102019-03-04 16:49:25 +0100407 for (const RtpStreamSender& stream : rtp_streams_) {
408 transport_->packet_router()->RemoveSendRtpModule(stream.rtp_rtcp.get());
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200409 }
Sebastian Janssonf2988552019-10-29 17:18:51 +0100410 transport_->GetStreamFeedbackProvider()->DeRegisterStreamFeedbackObserver(
411 this);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200412}
413
Stefan Holmer9416ef82018-07-19 10:34:38 +0200414void RtpVideoSender::RegisterProcessThread(
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200415 ProcessThread* module_process_thread) {
416 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
417 RTC_DCHECK(!module_process_thread_);
418 module_process_thread_ = module_process_thread;
419
Niels Möller5fe95102019-03-04 16:49:25 +0100420 for (const RtpStreamSender& stream : rtp_streams_) {
421 module_process_thread_->RegisterModule(stream.rtp_rtcp.get(),
422 RTC_FROM_HERE);
423 }
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200424}
425
Stefan Holmer9416ef82018-07-19 10:34:38 +0200426void RtpVideoSender::DeRegisterProcessThread() {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200427 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
Niels Möller5fe95102019-03-04 16:49:25 +0100428 for (const RtpStreamSender& stream : rtp_streams_)
429 module_process_thread_->DeRegisterModule(stream.rtp_rtcp.get());
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200430}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000431
Stefan Holmer9416ef82018-07-19 10:34:38 +0200432void RtpVideoSender::SetActive(bool active) {
Tommi97888bd2016-01-21 23:24:59 +0100433 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100434 if (active_ == active)
435 return;
Niels Möller5fe95102019-03-04 16:49:25 +0100436 const std::vector<bool> active_modules(rtp_streams_.size(), active);
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800437 SetActiveModules(active_modules);
438}
Per512ecb32016-09-23 15:52:06 +0200439
Stefan Holmer9416ef82018-07-19 10:34:38 +0200440void RtpVideoSender::SetActiveModules(const std::vector<bool> active_modules) {
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800441 rtc::CritScope lock(&crit_);
Niels Möller5fe95102019-03-04 16:49:25 +0100442 RTC_DCHECK_EQ(rtp_streams_.size(), active_modules.size());
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800443 active_ = false;
444 for (size_t i = 0; i < active_modules.size(); ++i) {
445 if (active_modules[i]) {
446 active_ = true;
447 }
448 // Sends a kRtcpByeCode when going from true to false.
Niels Möller5fe95102019-03-04 16:49:25 +0100449 rtp_streams_[i].rtp_rtcp->SetSendingStatus(active_modules[i]);
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800450 // If set to false this module won't send media.
Niels Möller5fe95102019-03-04 16:49:25 +0100451 rtp_streams_[i].rtp_rtcp->SetSendingMediaStatus(active_modules[i]);
Per512ecb32016-09-23 15:52:06 +0200452 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000453}
454
Stefan Holmer9416ef82018-07-19 10:34:38 +0200455bool RtpVideoSender::IsActive() {
Tommi97888bd2016-01-21 23:24:59 +0100456 rtc::CritScope lock(&crit_);
Niels Möller5fe95102019-03-04 16:49:25 +0100457 return active_ && !rtp_streams_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000458}
459
Stefan Holmer9416ef82018-07-19 10:34:38 +0200460EncodedImageCallback::Result RtpVideoSender::OnEncodedImage(
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700461 const EncodedImage& encoded_image,
462 const CodecSpecificInfo* codec_specific_info,
463 const RTPFragmentationHeader* fragmentation) {
Niels Möller77536a22019-01-15 08:50:01 +0100464 fec_controller_->UpdateWithEncodedData(encoded_image.size(),
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200465 encoded_image._frameType);
Tommi97888bd2016-01-21 23:24:59 +0100466 rtc::CritScope lock(&crit_);
Niels Möller5fe95102019-03-04 16:49:25 +0100467 RTC_DCHECK(!rtp_streams_.empty());
Per512ecb32016-09-23 15:52:06 +0200468 if (!active_)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700469 return Result(Result::ERROR_SEND_FAILED);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000470
philipelbf2b6202018-08-27 14:33:18 +0200471 shared_frame_id_++;
Niels Möllerd3b8c632018-08-27 15:33:42 +0200472 size_t stream_index = 0;
473 if (codec_specific_info &&
474 (codec_specific_info->codecType == kVideoCodecVP8 ||
475 codec_specific_info->codecType == kVideoCodecH264 ||
476 codec_specific_info->codecType == kVideoCodecGeneric)) {
477 // Map spatial index to simulcast.
478 stream_index = encoded_image.SpatialIndex().value_or(0);
479 }
Niels Möller5fe95102019-03-04 16:49:25 +0100480 RTC_DCHECK_LT(stream_index, rtp_streams_.size());
Niels Möllerbb894ff2018-03-15 12:28:53 +0100481
Niels Möller5fe95102019-03-04 16:49:25 +0100482 uint32_t rtp_timestamp =
483 encoded_image.Timestamp() +
484 rtp_streams_[stream_index].rtp_rtcp->StartTimestamp();
485
486 // RTCPSender has it's own copy of the timestamp offset, added in
487 // RTCPSender::BuildSR, hence we must not add the in the offset for this call.
488 // TODO(nisse): Delete RTCPSender:timestamp_offset_, and see if we can confine
489 // knowledge of the offset to a single place.
490 if (!rtp_streams_[stream_index].rtp_rtcp->OnSendingRtpFrame(
491 encoded_image.Timestamp(), encoded_image.capture_time_ms_,
492 rtp_config_.payload_type,
Niels Möller8f7ce222019-03-21 15:43:58 +0100493 encoded_image._frameType == VideoFrameType::kVideoFrameKey)) {
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800494 // The payload router could be active but this module isn't sending.
495 return Result(Result::ERROR_SEND_FAILED);
496 }
Elad Alonb64af4b2019-06-05 11:39:37 +0200497
498 absl::optional<int64_t> expected_retransmission_time_ms;
499 if (encoded_image.RetransmissionAllowed()) {
500 expected_retransmission_time_ms =
501 rtp_streams_[stream_index].rtp_rtcp->ExpectedRetransmissionTimeMs();
502 }
Niels Möller949f0fd2019-01-29 09:44:24 +0100503
Niels Möller5fe95102019-03-04 16:49:25 +0100504 bool send_result = rtp_streams_[stream_index].sender_video->SendVideo(
Danil Chapovalov51bf2002019-10-11 10:53:27 +0200505 rtp_config_.payload_type, codec_type_, rtp_timestamp,
506 encoded_image.capture_time_ms_, encoded_image, fragmentation,
507 params_[stream_index].GetRtpVideoHeader(
508 encoded_image, codec_specific_info, shared_frame_id_),
Niels Möller5fe95102019-03-04 16:49:25 +0100509 expected_retransmission_time_ms);
Niels Möller949f0fd2019-01-29 09:44:24 +0100510 if (frame_count_observer_) {
511 FrameCounts& counts = frame_counts_[stream_index];
Niels Möller8f7ce222019-03-21 15:43:58 +0100512 if (encoded_image._frameType == VideoFrameType::kVideoFrameKey) {
Niels Möller949f0fd2019-01-29 09:44:24 +0100513 ++counts.key_frames;
Niels Möller8f7ce222019-03-21 15:43:58 +0100514 } else if (encoded_image._frameType == VideoFrameType::kVideoFrameDelta) {
Niels Möller949f0fd2019-01-29 09:44:24 +0100515 ++counts.delta_frames;
516 } else {
Niels Möller8f7ce222019-03-21 15:43:58 +0100517 RTC_DCHECK(encoded_image._frameType == VideoFrameType::kEmptyFrame);
Niels Möller949f0fd2019-01-29 09:44:24 +0100518 }
519 frame_count_observer_->FrameCountUpdated(counts,
520 rtp_config_.ssrcs[stream_index]);
521 }
sergeyu7b9feee2016-11-17 16:16:14 -0800522 if (!send_result)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700523 return Result(Result::ERROR_SEND_FAILED);
524
Niels Möller5fe95102019-03-04 16:49:25 +0100525 return Result(Result::OK, rtp_timestamp);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000526}
527
Stefan Holmer9416ef82018-07-19 10:34:38 +0200528void RtpVideoSender::OnBitrateAllocationUpdated(
Erik Språng566124a2018-04-23 12:32:22 +0200529 const VideoBitrateAllocation& bitrate) {
sprang1a646ee2016-12-01 06:34:11 -0800530 rtc::CritScope lock(&crit_);
531 if (IsActive()) {
Oleh Prypine8964902019-03-29 15:33:01 +0000532 if (rtp_streams_.size() == 1) {
sprang1a646ee2016-12-01 06:34:11 -0800533 // If spatial scalability is enabled, it is covered by a single stream.
Niels Möller5fe95102019-03-04 16:49:25 +0100534 rtp_streams_[0].rtp_rtcp->SetVideoBitrateAllocation(bitrate);
sprang1a646ee2016-12-01 06:34:11 -0800535 } else {
Stefan Holmerf7044682018-07-17 10:16:41 +0200536 std::vector<absl::optional<VideoBitrateAllocation>> layer_bitrates =
537 bitrate.GetSimulcastAllocations();
Erik Språng566124a2018-04-23 12:32:22 +0200538 // Simulcast is in use, split the VideoBitrateAllocation into one struct
539 // per rtp stream, moving over the temporal layer allocation.
Niels Möller5fe95102019-03-04 16:49:25 +0100540 for (size_t i = 0; i < rtp_streams_.size(); ++i) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200541 // The next spatial layer could be used if the current one is
542 // inactive.
543 if (layer_bitrates[i]) {
Niels Möller5fe95102019-03-04 16:49:25 +0100544 rtp_streams_[i].rtp_rtcp->SetVideoBitrateAllocation(
545 *layer_bitrates[i]);
Ilya Nikolaevskiyb0588e62018-08-27 14:12:27 +0200546 } else {
547 // Signal a 0 bitrate on a simulcast stream.
Niels Möller5fe95102019-03-04 16:49:25 +0100548 rtp_streams_[i].rtp_rtcp->SetVideoBitrateAllocation(
549 VideoBitrateAllocation());
Seth Hampson46e31ba2018-01-18 10:39:54 -0800550 }
sprang1a646ee2016-12-01 06:34:11 -0800551 }
552 }
553 }
554}
555
Stefan Holmer9416ef82018-07-19 10:34:38 +0200556bool RtpVideoSender::FecEnabled() const {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200557 const bool flexfec_enabled = (flexfec_sender_ != nullptr);
Emircan Uysalera7af0212018-09-22 19:11:29 -0400558 const bool ulpfec_enabled =
559 !webrtc::field_trial::IsEnabled("WebRTC-DisableUlpFecExperiment") &&
560 (rtp_config_.ulpfec.ulpfec_payload_type >= 0);
561 return flexfec_enabled || ulpfec_enabled;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200562}
563
Stefan Holmer9416ef82018-07-19 10:34:38 +0200564bool RtpVideoSender::NackEnabled() const {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200565 const bool nack_enabled = rtp_config_.nack.rtp_history_ms > 0;
566 return nack_enabled;
567}
568
Erik Språng482b3ef2019-01-08 16:19:11 +0100569uint32_t RtpVideoSender::GetPacketizationOverheadRate() const {
570 uint32_t packetization_overhead_bps = 0;
Niels Möller5fe95102019-03-04 16:49:25 +0100571 for (size_t i = 0; i < rtp_streams_.size(); ++i) {
572 if (rtp_streams_[i].rtp_rtcp->SendingMedia()) {
573 packetization_overhead_bps +=
574 rtp_streams_[i].sender_video->PacketizationOverheadBps();
Erik Språng482b3ef2019-01-08 16:19:11 +0100575 }
576 }
577 return packetization_overhead_bps;
578}
579
Stefan Holmer9416ef82018-07-19 10:34:38 +0200580void RtpVideoSender::DeliverRtcp(const uint8_t* packet, size_t length) {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200581 // Runs on a network thread.
Niels Möller5fe95102019-03-04 16:49:25 +0100582 for (const RtpStreamSender& stream : rtp_streams_)
583 stream.rtp_rtcp->IncomingRtcpPacket(packet, length);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200584}
585
Elad Alon62ce0352019-05-23 16:58:53 +0200586void RtpVideoSender::ConfigureSsrcs() {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200587 // Configure regular SSRCs.
Erik Språnga9229042019-10-24 12:39:32 +0200588 RTC_CHECK(ssrc_to_rtp_module_.empty());
Elad Alon62ce0352019-05-23 16:58:53 +0200589 for (size_t i = 0; i < rtp_config_.ssrcs.size(); ++i) {
590 uint32_t ssrc = rtp_config_.ssrcs[i];
Niels Möller5fe95102019-03-04 16:49:25 +0100591 RtpRtcp* const rtp_rtcp = rtp_streams_[i].rtp_rtcp.get();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200592
593 // Restore RTP state if previous existed.
594 auto it = suspended_ssrcs_.find(ssrc);
595 if (it != suspended_ssrcs_.end())
596 rtp_rtcp->SetRtpState(it->second);
Erik Språng490d76c2019-05-07 09:29:15 -0700597
Erik Språnga9229042019-10-24 12:39:32 +0200598 ssrc_to_rtp_module_[ssrc] = rtp_rtcp;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200599 }
600
601 // Set up RTX if available.
Elad Alon62ce0352019-05-23 16:58:53 +0200602 if (rtp_config_.rtx.ssrcs.empty())
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200603 return;
604
Elad Alon62ce0352019-05-23 16:58:53 +0200605 RTC_DCHECK_EQ(rtp_config_.rtx.ssrcs.size(), rtp_config_.ssrcs.size());
606 for (size_t i = 0; i < rtp_config_.rtx.ssrcs.size(); ++i) {
607 uint32_t ssrc = rtp_config_.rtx.ssrcs[i];
Niels Möller5fe95102019-03-04 16:49:25 +0100608 RtpRtcp* const rtp_rtcp = rtp_streams_[i].rtp_rtcp.get();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200609 auto it = suspended_ssrcs_.find(ssrc);
610 if (it != suspended_ssrcs_.end())
611 rtp_rtcp->SetRtxState(it->second);
612 }
613
614 // Configure RTX payload types.
Elad Alon62ce0352019-05-23 16:58:53 +0200615 RTC_DCHECK_GE(rtp_config_.rtx.payload_type, 0);
Niels Möller5fe95102019-03-04 16:49:25 +0100616 for (const RtpStreamSender& stream : rtp_streams_) {
Elad Alon62ce0352019-05-23 16:58:53 +0200617 stream.rtp_rtcp->SetRtxSendPayloadType(rtp_config_.rtx.payload_type,
618 rtp_config_.payload_type);
Niels Möller5fe95102019-03-04 16:49:25 +0100619 stream.rtp_rtcp->SetRtxSendStatus(kRtxRetransmitted |
620 kRtxRedundantPayloads);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200621 }
Elad Alon62ce0352019-05-23 16:58:53 +0200622 if (rtp_config_.ulpfec.red_payload_type != -1 &&
623 rtp_config_.ulpfec.red_rtx_payload_type != -1) {
Niels Möller5fe95102019-03-04 16:49:25 +0100624 for (const RtpStreamSender& stream : rtp_streams_) {
625 stream.rtp_rtcp->SetRtxSendPayloadType(
Elad Alon62ce0352019-05-23 16:58:53 +0200626 rtp_config_.ulpfec.red_rtx_payload_type,
627 rtp_config_.ulpfec.red_payload_type);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200628 }
629 }
630}
631
Elad Alon62ce0352019-05-23 16:58:53 +0200632void RtpVideoSender::ConfigureRids() {
Florent Castelli463d44a2019-07-12 15:35:53 +0200633 if (rtp_config_.rids.empty())
634 return;
635
636 // Some streams could have been disabled, but the rids are still there.
637 // This will occur when simulcast has been disabled for a codec (e.g. VP9)
638 RTC_DCHECK(rtp_config_.rids.size() >= rtp_streams_.size());
639 for (size_t i = 0; i < rtp_streams_.size(); ++i) {
640 rtp_streams_[i].rtp_rtcp->SetRid(rtp_config_.rids[i]);
Amit Hilbuch77938e62018-12-21 09:23:38 -0800641 }
642}
643
Stefan Holmer9416ef82018-07-19 10:34:38 +0200644void RtpVideoSender::OnNetworkAvailability(bool network_available) {
Niels Möller5fe95102019-03-04 16:49:25 +0100645 for (const RtpStreamSender& stream : rtp_streams_) {
646 stream.rtp_rtcp->SetRTCPStatus(network_available ? rtp_config_.rtcp_mode
647 : RtcpMode::kOff);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200648 }
649}
650
Stefan Holmer9416ef82018-07-19 10:34:38 +0200651std::map<uint32_t, RtpState> RtpVideoSender::GetRtpStates() const {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200652 std::map<uint32_t, RtpState> rtp_states;
653
654 for (size_t i = 0; i < rtp_config_.ssrcs.size(); ++i) {
655 uint32_t ssrc = rtp_config_.ssrcs[i];
Niels Möller5fe95102019-03-04 16:49:25 +0100656 RTC_DCHECK_EQ(ssrc, rtp_streams_[i].rtp_rtcp->SSRC());
657 rtp_states[ssrc] = rtp_streams_[i].rtp_rtcp->GetRtpState();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200658 }
659
660 for (size_t i = 0; i < rtp_config_.rtx.ssrcs.size(); ++i) {
661 uint32_t ssrc = rtp_config_.rtx.ssrcs[i];
Niels Möller5fe95102019-03-04 16:49:25 +0100662 rtp_states[ssrc] = rtp_streams_[i].rtp_rtcp->GetRtxState();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200663 }
664
665 if (flexfec_sender_) {
666 uint32_t ssrc = rtp_config_.flexfec.ssrc;
667 rtp_states[ssrc] = flexfec_sender_->GetRtpState();
668 }
669
670 return rtp_states;
671}
672
Stefan Holmer9416ef82018-07-19 10:34:38 +0200673std::map<uint32_t, RtpPayloadState> RtpVideoSender::GetRtpPayloadStates()
674 const {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200675 rtc::CritScope lock(&crit_);
676 std::map<uint32_t, RtpPayloadState> payload_states;
677 for (const auto& param : params_) {
678 payload_states[param.ssrc()] = param.state();
philipel25d31ec2018-08-08 16:33:01 +0200679 payload_states[param.ssrc()].shared_frame_id = shared_frame_id_;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200680 }
681 return payload_states;
682}
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200683
684void RtpVideoSender::OnTransportOverheadChanged(
685 size_t transport_overhead_bytes_per_packet) {
686 rtc::CritScope lock(&crit_);
687 transport_overhead_bytes_per_packet_ = transport_overhead_bytes_per_packet;
688
689 size_t max_rtp_packet_size =
690 std::min(rtp_config_.max_packet_size,
691 kPathMTU - transport_overhead_bytes_per_packet_);
Niels Möller5fe95102019-03-04 16:49:25 +0100692 for (const RtpStreamSender& stream : rtp_streams_) {
693 stream.rtp_rtcp->SetMaxRtpPacketSize(max_rtp_packet_size);
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200694 }
695}
696
697void RtpVideoSender::OnOverheadChanged(size_t overhead_bytes_per_packet) {
698 rtc::CritScope lock(&crit_);
699 overhead_bytes_per_packet_ = overhead_bytes_per_packet;
700}
701
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200702void RtpVideoSender::OnBitrateUpdated(BitrateAllocationUpdate update,
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200703 int framerate) {
704 // Substract overhead from bitrate.
705 rtc::CritScope lock(&crit_);
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200706 DataSize packet_overhead = DataSize::bytes(
707 overhead_bytes_per_packet_ + transport_overhead_bytes_per_packet_);
708 DataSize max_total_packet_size = DataSize::bytes(
709 rtp_config_.max_packet_size + transport_overhead_bytes_per_packet_);
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200710 uint32_t payload_bitrate_bps = update.target_bitrate.bps();
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200711 if (send_side_bwe_with_overhead_) {
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200712 DataRate overhead_rate = CalculateOverheadRate(
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200713 update.target_bitrate, max_total_packet_size, packet_overhead);
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200714 // TODO(srte): We probably should not accept 0 payload bitrate here.
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200715 payload_bitrate_bps = rtc::saturated_cast<uint32_t>(payload_bitrate_bps -
716 overhead_rate.bps());
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200717 }
718
719 // Get the encoder target rate. It is the estimated network rate -
720 // protection overhead.
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200721 // TODO(srte): We should multiply with 255 here.
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200722 encoder_target_rate_bps_ = fec_controller_->UpdateFecRates(
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200723 payload_bitrate_bps, framerate,
724 rtc::saturated_cast<uint8_t>(update.packet_loss_ratio * 256),
725 loss_mask_vector_, update.round_trip_time.ms());
Elad Alon67daf712019-06-28 18:14:36 +0200726 if (!fec_allowed_) {
727 encoder_target_rate_bps_ = payload_bitrate_bps;
728 // fec_controller_->UpdateFecRates() was still called so as to allow
729 // |fec_controller_| to update whatever internal state it might have,
730 // since |fec_allowed_| may be toggled back on at any moment.
731 }
Erik Språng482b3ef2019-01-08 16:19:11 +0100732
Erik Språngd15687d2019-01-18 10:47:07 +0100733 uint32_t packetization_rate_bps = 0;
Erik Språngc12d41b2019-01-09 09:55:31 +0100734 if (account_for_packetization_overhead_) {
Erik Språngd15687d2019-01-18 10:47:07 +0100735 // Subtract packetization overhead from the encoder target. If target rate
736 // is really low, cap the overhead at 50%. This also avoids the case where
737 // |encoder_target_rate_bps_| is 0 due to encoder pause event while the
738 // packetization rate is positive since packets are still flowing.
739 packetization_rate_bps =
740 std::min(GetPacketizationOverheadRate(), encoder_target_rate_bps_ / 2);
741 encoder_target_rate_bps_ -= packetization_rate_bps;
Erik Språngc12d41b2019-01-09 09:55:31 +0100742 }
Erik Språng482b3ef2019-01-08 16:19:11 +0100743
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200744 loss_mask_vector_.clear();
745
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200746 uint32_t encoder_overhead_rate_bps = 0;
747 if (send_side_bwe_with_overhead_) {
748 // TODO(srte): The packet size should probably be the same as in the
749 // CalculateOverheadRate call above (just max_total_packet_size), it doesn't
750 // make sense to use different packet rates for different overhead
751 // calculations.
752 DataRate encoder_overhead_rate = CalculateOverheadRate(
753 DataRate::bps(encoder_target_rate_bps_),
754 max_total_packet_size - DataSize::bytes(overhead_bytes_per_packet_),
755 packet_overhead);
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200756 encoder_overhead_rate_bps = std::min(
757 encoder_overhead_rate.bps<uint32_t>(),
758 update.target_bitrate.bps<uint32_t>() - encoder_target_rate_bps_);
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200759 }
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200760 // When the field trial "WebRTC-SendSideBwe-WithOverhead" is enabled
761 // protection_bitrate includes overhead.
Erik Språngd15687d2019-01-18 10:47:07 +0100762 const uint32_t media_rate = encoder_target_rate_bps_ +
763 encoder_overhead_rate_bps +
764 packetization_rate_bps;
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200765 RTC_DCHECK_GE(update.target_bitrate, DataRate::bps(media_rate));
766 protection_bitrate_bps_ = update.target_bitrate.bps() - media_rate;
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200767}
768
769uint32_t RtpVideoSender::GetPayloadBitrateBps() const {
770 return encoder_target_rate_bps_;
771}
772
773uint32_t RtpVideoSender::GetProtectionBitrateBps() const {
774 return protection_bitrate_bps_;
775}
776
Elad Alon898395d2019-04-10 15:55:00 +0200777std::vector<RtpSequenceNumberMap::Info> RtpVideoSender::GetSentRtpPacketInfos(
Elad Alon8b60e8b2019-04-08 14:14:05 +0200778 uint32_t ssrc,
Elad Alon898395d2019-04-10 15:55:00 +0200779 rtc::ArrayView<const uint16_t> sequence_numbers) const {
Elad Alon8b60e8b2019-04-08 14:14:05 +0200780 for (const auto& rtp_stream : rtp_streams_) {
781 if (ssrc == rtp_stream.rtp_rtcp->SSRC()) {
Elad Alon898395d2019-04-10 15:55:00 +0200782 return rtp_stream.sender_video->GetSentRtpPacketInfos(sequence_numbers);
Elad Alon8b60e8b2019-04-08 14:14:05 +0200783 }
784 }
Elad Alon898395d2019-04-10 15:55:00 +0200785 return std::vector<RtpSequenceNumberMap::Info>();
Elad Alon8b60e8b2019-04-08 14:14:05 +0200786}
787
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200788int RtpVideoSender::ProtectionRequest(const FecProtectionParams* delta_params,
789 const FecProtectionParams* key_params,
790 uint32_t* sent_video_rate_bps,
791 uint32_t* sent_nack_rate_bps,
792 uint32_t* sent_fec_rate_bps) {
793 *sent_video_rate_bps = 0;
794 *sent_nack_rate_bps = 0;
795 *sent_fec_rate_bps = 0;
Niels Möller5fe95102019-03-04 16:49:25 +0100796 for (const RtpStreamSender& stream : rtp_streams_) {
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200797 uint32_t not_used = 0;
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200798 uint32_t module_nack_rate = 0;
Niels Möller5fe95102019-03-04 16:49:25 +0100799 stream.sender_video->SetFecParameters(*delta_params, *key_params);
800 *sent_video_rate_bps += stream.sender_video->VideoBitrateSent();
801 *sent_fec_rate_bps += stream.sender_video->FecOverheadRate();
802 stream.rtp_rtcp->BitrateSent(&not_used, /*video_rate=*/nullptr,
803 /*fec_rate=*/nullptr, &module_nack_rate);
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200804 *sent_nack_rate_bps += module_nack_rate;
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200805 }
806 return 0;
807}
808
Elad Alon8f01c4e2019-06-28 15:19:43 +0200809void RtpVideoSender::SetFecAllowed(bool fec_allowed) {
Elad Alon67daf712019-06-28 18:14:36 +0200810 rtc::CritScope cs(&crit_);
811 fec_allowed_ = fec_allowed;
Elad Alon8f01c4e2019-06-28 15:19:43 +0200812}
813
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200814void RtpVideoSender::OnPacketFeedbackVector(
Sebastian Janssonf2988552019-10-29 17:18:51 +0100815 std::vector<StreamPacketInfo> packet_feedback_vector) {
Erik Språng490d76c2019-05-07 09:29:15 -0700816 if (fec_controller_->UseLossVectorMask()) {
817 rtc::CritScope cs(&crit_);
Sebastian Janssonf2988552019-10-29 17:18:51 +0100818 for (const StreamPacketInfo& packet : packet_feedback_vector) {
819 loss_mask_vector_.push_back(!packet.received);
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200820 }
821 }
Erik Språng490d76c2019-05-07 09:29:15 -0700822
823 // Map from SSRC to all acked packets for that RTP module.
824 std::map<uint32_t, std::vector<uint16_t>> acked_packets_per_ssrc;
Sebastian Janssonf2988552019-10-29 17:18:51 +0100825 for (const StreamPacketInfo& packet : packet_feedback_vector) {
826 if (packet.received) {
827 acked_packets_per_ssrc[packet.ssrc].push_back(packet.rtp_sequence_number);
Erik Språng490d76c2019-05-07 09:29:15 -0700828 }
829 }
830
Erik Språng845c6aa2019-05-29 13:02:24 +0200831 if (use_early_loss_detection_) {
832 // Map from SSRC to vector of RTP sequence numbers that are indicated as
833 // lost by feedback, without being trailed by any received packets.
834 std::map<uint32_t, std::vector<uint16_t>> early_loss_detected_per_ssrc;
835
Sebastian Janssonf2988552019-10-29 17:18:51 +0100836 for (const StreamPacketInfo& packet : packet_feedback_vector) {
837 if (!packet.received) {
Erik Språng845c6aa2019-05-29 13:02:24 +0200838 // Last known lost packet, might not be detectable as lost by remote
839 // jitter buffer.
Sebastian Janssonf2988552019-10-29 17:18:51 +0100840 early_loss_detected_per_ssrc[packet.ssrc].push_back(
Erik Språng845c6aa2019-05-29 13:02:24 +0200841 packet.rtp_sequence_number);
842 } else {
843 // Packet received, so any loss prior to this is already detectable.
Sebastian Janssonf2988552019-10-29 17:18:51 +0100844 early_loss_detected_per_ssrc.erase(packet.ssrc);
Erik Språng845c6aa2019-05-29 13:02:24 +0200845 }
846 }
847
848 for (const auto& kv : early_loss_detected_per_ssrc) {
849 const uint32_t ssrc = kv.first;
Erik Språnga9229042019-10-24 12:39:32 +0200850 auto it = ssrc_to_rtp_module_.find(ssrc);
851 RTC_DCHECK(it != ssrc_to_rtp_module_.end());
852 RTPSender* rtp_sender = it->second->RtpSender();
Erik Språng845c6aa2019-05-29 13:02:24 +0200853 for (uint16_t sequence_number : kv.second) {
854 rtp_sender->ReSendPacket(sequence_number);
855 }
856 }
857 }
858
Erik Språng490d76c2019-05-07 09:29:15 -0700859 for (const auto& kv : acked_packets_per_ssrc) {
860 const uint32_t ssrc = kv.first;
Erik Språnga9229042019-10-24 12:39:32 +0200861 auto it = ssrc_to_rtp_module_.find(ssrc);
862 if (it == ssrc_to_rtp_module_.end()) {
Erik Språng490d76c2019-05-07 09:29:15 -0700863 // Packets not for a media SSRC, so likely RTX or FEC. If so, ignore
864 // since there's no RTP history to clean up anyway.
865 continue;
866 }
867 rtc::ArrayView<const uint16_t> rtp_sequence_numbers(kv.second);
Erik Språng845c6aa2019-05-29 13:02:24 +0200868 it->second->OnPacketsAcknowledged(rtp_sequence_numbers);
Erik Språng490d76c2019-05-07 09:29:15 -0700869 }
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200870}
871
872void RtpVideoSender::SetEncodingData(size_t width,
873 size_t height,
874 size_t num_temporal_layers) {
875 fec_controller_->SetEncodingData(width, height, num_temporal_layers,
876 rtp_config_.max_packet_size);
877}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000878} // namespace webrtc