blob: cf23bd3c7b592bdcb906a26eace0101ac71044b3 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtp_sender_video.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000013#include <stdlib.h>
14#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
spranga8ae6f22017-09-04 07:23:56 -070016#include <limits>
kwiberg84be5112016-04-27 01:19:58 -070017#include <memory>
danilchap74110612016-10-02 10:54:29 -070018#include <utility>
spranga8ae6f22017-09-04 07:23:56 -070019#include <vector>
mflodmanfcf54bd2015-04-14 21:28:08 +020020
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
22#include "modules/rtp_rtcp/source/byte_io.h"
23#include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
24#include "modules/rtp_rtcp/source/rtp_format_vp8.h"
25#include "modules/rtp_rtcp/source/rtp_format_vp9.h"
26#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
27#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
28#include "rtc_base/checks.h"
29#include "rtc_base/logging.h"
30#include "rtc_base/ptr_util.h"
31#include "rtc_base/trace_event.h"
ilniked9b9ff2017-06-02 07:30:20 -070032
niklase@google.com470e71d2011-07-07 08:21:25 +000033namespace webrtc {
Sergey Ulanovec4f0682016-07-28 15:19:10 -070034
brandtr6631e8a2016-09-13 03:23:29 -070035namespace {
36constexpr size_t kRedForFecHeaderLength = 1;
spranga8ae6f22017-09-04 07:23:56 -070037constexpr int64_t kMaxUnretransmittableFrameIntervalMs = 33 * 4;
danilchap74110612016-10-02 10:54:29 -070038
39void BuildRedPayload(const RtpPacketToSend& media_packet,
40 RtpPacketToSend* red_packet) {
41 uint8_t* red_payload = red_packet->AllocatePayload(
42 kRedForFecHeaderLength + media_packet.payload_size());
43 RTC_DCHECK(red_payload);
44 red_payload[0] = media_packet.PayloadType();
danilchap96c15872016-11-21 01:35:29 -080045
46 auto media_payload = media_packet.payload();
47 memcpy(&red_payload[kRedForFecHeaderLength], media_payload.data(),
48 media_payload.size());
danilchap74110612016-10-02 10:54:29 -070049}
brandtr6631e8a2016-09-13 03:23:29 -070050} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000051
brandtrdbdb3f12016-11-10 05:04:48 -080052RTPSenderVideo::RTPSenderVideo(Clock* clock,
53 RTPSender* rtp_sender,
54 FlexfecSender* flexfec_sender)
Sergey Ulanovec4f0682016-07-28 15:19:10 -070055 : rtp_sender_(rtp_sender),
sprangcd349d92016-07-13 09:11:28 -070056 clock_(clock),
brandtrd8048952016-11-07 02:08:51 -080057 video_type_(kRtpVideoGeneric),
spranga8ae6f22017-09-04 07:23:56 -070058 retransmission_settings_(kRetransmitBaseLayer |
59 kConditionallyRetransmitHigherLayers),
brandtrd8048952016-11-07 02:08:51 -080060 last_rotation_(kVideoRotation_0),
brandtrd8048952016-11-07 02:08:51 -080061 red_payload_type_(-1),
brandtrf1bb4762016-11-07 03:05:06 -080062 ulpfec_payload_type_(-1),
brandtrdbdb3f12016-11-10 05:04:48 -080063 flexfec_sender_(flexfec_sender),
brandtrd8048952016-11-07 02:08:51 -080064 delta_fec_params_{0, 1, kFecMaskRandom},
65 key_fec_params_{0, 1, kFecMaskRandom},
sprangcd349d92016-07-13 09:11:28 -070066 fec_bitrate_(1000, RateStatistics::kBpsScale),
danilchap2a615fc2016-11-11 02:27:35 -080067 video_bitrate_(1000, RateStatistics::kBpsScale) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000068
Sergey Ulanovec4f0682016-07-28 15:19:10 -070069RTPSenderVideo::~RTPSenderVideo() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000070
Sergey Ulanovec4f0682016-07-28 15:19:10 -070071void RTPSenderVideo::SetVideoCodecType(RtpVideoCodecTypes video_type) {
72 video_type_ = video_type;
niklase@google.com470e71d2011-07-07 08:21:25 +000073}
74
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000075RtpVideoCodecTypes RTPSenderVideo::VideoCodecType() const {
Sergey Ulanovec4f0682016-07-28 15:19:10 -070076 return video_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
mflodmanfcf54bd2015-04-14 21:28:08 +020079// Static.
80RtpUtility::Payload* RTPSenderVideo::CreateVideoPayload(
Sergey Ulanovec4f0682016-07-28 15:19:10 -070081 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
82 int8_t payload_type) {
83 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
84 if (RtpUtility::StringCompare(payload_name, "VP8", 3)) {
85 video_type = kRtpVideoVp8;
86 } else if (RtpUtility::StringCompare(payload_name, "VP9", 3)) {
87 video_type = kRtpVideoVp9;
88 } else if (RtpUtility::StringCompare(payload_name, "H264", 4)) {
89 video_type = kRtpVideoH264;
90 } else if (RtpUtility::StringCompare(payload_name, "I420", 4)) {
91 video_type = kRtpVideoGeneric;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +000092 } else {
Sergey Ulanovec4f0682016-07-28 15:19:10 -070093 video_type = kRtpVideoGeneric;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +000094 }
Karl Wiberg83d3ec12017-09-28 19:54:38 +020095 VideoPayload vp;
96 vp.videoCodecType = video_type;
97 return new RtpUtility::Payload(payload_name, PayloadUnion(vp));
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
danilchap74110612016-10-02 10:54:29 -0700100void RTPSenderVideo::SendVideoPacket(std::unique_ptr<RtpPacketToSend> packet,
mflodmanfcf54bd2015-04-14 21:28:08 +0200101 StorageType storage) {
danilchap74110612016-10-02 10:54:29 -0700102 // Remember some values about the packet before sending it away.
103 size_t packet_size = packet->size();
104 uint16_t seq_num = packet->SequenceNumber();
105 uint32_t rtp_timestamp = packet->Timestamp();
106 if (!rtp_sender_->SendToNetwork(std::move(packet), storage,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700107 RtpPacketSender::kLowPriority)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG(LS_WARNING) << "Failed to send video packet " << seq_num;
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700109 return;
mflodmanfcf54bd2015-04-14 21:28:08 +0200110 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700111 rtc::CritScope cs(&stats_crit_);
danilchap74110612016-10-02 10:54:29 -0700112 video_bitrate_.Update(packet_size, clock_->TimeInMilliseconds());
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700113 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
danilchape5b41412016-08-22 03:39:23 -0700114 "Video::PacketNormal", "timestamp", rtp_timestamp,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700115 "seqnum", seq_num);
mflodmanfcf54bd2015-04-14 21:28:08 +0200116}
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
brandtr131bc492016-11-10 05:01:11 -0800118void RTPSenderVideo::SendVideoPacketAsRedMaybeWithUlpfec(
danilchap74110612016-10-02 10:54:29 -0700119 std::unique_ptr<RtpPacketToSend> media_packet,
120 StorageType media_packet_storage,
brandtr131bc492016-11-10 05:01:11 -0800121 bool protect_media_packet) {
danilchap74110612016-10-02 10:54:29 -0700122 uint32_t rtp_timestamp = media_packet->Timestamp();
123 uint16_t media_seq_num = media_packet->SequenceNumber();
124
125 std::unique_ptr<RtpPacketToSend> red_packet(
126 new RtpPacketToSend(*media_packet));
127 BuildRedPayload(*media_packet, red_packet.get());
128
brandtr74811e52016-08-10 00:51:50 -0700129 std::vector<std::unique_ptr<RedPacket>> fec_packets;
mflodmanfcf54bd2015-04-14 21:28:08 +0200130 StorageType fec_storage = kDontRetransmit;
mflodmanfcf54bd2015-04-14 21:28:08 +0200131 {
132 // Only protect while creating RED and FEC packets, not when sending.
danilchap7c9426c2016-04-14 03:05:31 -0700133 rtc::CritScope cs(&crit_);
danilchap74110612016-10-02 10:54:29 -0700134 red_packet->SetPayloadType(red_payload_type_);
brandtr131bc492016-11-10 05:01:11 -0800135 if (ulpfec_enabled()) {
136 if (protect_media_packet) {
137 ulpfec_generator_.AddRtpPacketAndGenerateFec(
138 media_packet->data(), media_packet->payload_size(),
139 media_packet->headers_size());
140 }
141 uint16_t num_fec_packets = ulpfec_generator_.NumAvailableFecPackets();
142 if (num_fec_packets > 0) {
143 uint16_t first_fec_sequence_number =
144 rtp_sender_->AllocateSequenceNumber(num_fec_packets);
145 fec_packets = ulpfec_generator_.GetUlpfecPacketsAsRed(
146 red_payload_type_, ulpfec_payload_type_, first_fec_sequence_number,
147 media_packet->headers_size());
148 RTC_DCHECK_EQ(num_fec_packets, fec_packets.size());
149 if (retransmission_settings_ & kRetransmitFECPackets)
150 fec_storage = kAllowRetransmission;
151 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200152 }
153 }
danilchap74110612016-10-02 10:54:29 -0700154 // Send |red_packet| instead of |packet| for allocated sequence number.
155 size_t red_packet_size = red_packet->size();
156 if (rtp_sender_->SendToNetwork(std::move(red_packet), media_packet_storage,
157 RtpPacketSender::kLowPriority)) {
sprangcd349d92016-07-13 09:11:28 -0700158 rtc::CritScope cs(&stats_crit_);
danilchap74110612016-10-02 10:54:29 -0700159 video_bitrate_.Update(red_packet_size, clock_->TimeInMilliseconds());
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000160 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
danilchape5b41412016-08-22 03:39:23 -0700161 "Video::PacketRed", "timestamp", rtp_timestamp,
mflodmanfcf54bd2015-04-14 21:28:08 +0200162 "seqnum", media_seq_num);
163 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100164 RTC_LOG(LS_WARNING) << "Failed to send RED packet " << media_seq_num;
mflodmanfcf54bd2015-04-14 21:28:08 +0200165 }
brandtr74811e52016-08-10 00:51:50 -0700166 for (const auto& fec_packet : fec_packets) {
brandtr869e7cd2016-10-31 05:27:07 -0700167 // TODO(danilchap): Make ulpfec_generator_ generate RtpPacketToSend to avoid
danilchap74110612016-10-02 10:54:29 -0700168 // reparsing them.
169 std::unique_ptr<RtpPacketToSend> rtp_packet(
170 new RtpPacketToSend(*media_packet));
171 RTC_CHECK(rtp_packet->Parse(fec_packet->data(), fec_packet->length()));
172 rtp_packet->set_capture_time_ms(media_packet->capture_time_ms());
173 uint16_t fec_sequence_number = rtp_packet->SequenceNumber();
174 if (rtp_sender_->SendToNetwork(std::move(rtp_packet), fec_storage,
175 RtpPacketSender::kLowPriority)) {
sprangcd349d92016-07-13 09:11:28 -0700176 rtc::CritScope cs(&stats_crit_);
177 fec_bitrate_.Update(fec_packet->length(), clock_->TimeInMilliseconds());
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000178 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
brandtr131bc492016-11-10 05:01:11 -0800179 "Video::PacketUlpfec", "timestamp", rtp_timestamp,
danilchap74110612016-10-02 10:54:29 -0700180 "seqnum", fec_sequence_number);
mflodmanfcf54bd2015-04-14 21:28:08 +0200181 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100182 RTC_LOG(LS_WARNING) << "Failed to send ULPFEC packet "
183 << fec_sequence_number;
brandtr131bc492016-11-10 05:01:11 -0800184 }
185 }
186}
187
188void RTPSenderVideo::SendVideoPacketWithFlexfec(
189 std::unique_ptr<RtpPacketToSend> media_packet,
190 StorageType media_packet_storage,
191 bool protect_media_packet) {
192 RTC_DCHECK(flexfec_sender_);
193
194 if (protect_media_packet)
195 flexfec_sender_->AddRtpPacketAndGenerateFec(*media_packet);
196
197 SendVideoPacket(std::move(media_packet), media_packet_storage);
198
199 if (flexfec_sender_->FecAvailable()) {
200 std::vector<std::unique_ptr<RtpPacketToSend>> fec_packets =
201 flexfec_sender_->GetFecPackets();
202 for (auto& fec_packet : fec_packets) {
brandtr81eab612017-01-24 04:06:09 -0800203 size_t packet_length = fec_packet->size();
brandtr131bc492016-11-10 05:01:11 -0800204 uint32_t timestamp = fec_packet->Timestamp();
205 uint16_t seq_num = fec_packet->SequenceNumber();
206 if (rtp_sender_->SendToNetwork(std::move(fec_packet), kDontRetransmit,
207 RtpPacketSender::kLowPriority)) {
brandtr81eab612017-01-24 04:06:09 -0800208 rtc::CritScope cs(&stats_crit_);
209 fec_bitrate_.Update(packet_length, clock_->TimeInMilliseconds());
brandtr131bc492016-11-10 05:01:11 -0800210 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
211 "Video::PacketFlexfec", "timestamp", timestamp,
212 "seqnum", seq_num);
213 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100214 RTC_LOG(LS_WARNING) << "Failed to send FlexFEC packet " << seq_num;
brandtr131bc492016-11-10 05:01:11 -0800215 }
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000216 }
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000217 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000218}
219
brandtrf1bb4762016-11-07 03:05:06 -0800220void RTPSenderVideo::SetUlpfecConfig(int red_payload_type,
brandtrd8048952016-11-07 02:08:51 -0800221 int ulpfec_payload_type) {
brandtrf1bb4762016-11-07 03:05:06 -0800222 // Sanity check. Per the definition of UlpfecConfig (see config.h),
223 // a payload type of -1 means that the corresponding feature is
224 // turned off.
225 RTC_DCHECK_GE(red_payload_type, -1);
brandtrd8048952016-11-07 02:08:51 -0800226 RTC_DCHECK_LE(red_payload_type, 127);
brandtrf1bb4762016-11-07 03:05:06 -0800227 RTC_DCHECK_GE(ulpfec_payload_type, -1);
brandtrd8048952016-11-07 02:08:51 -0800228 RTC_DCHECK_LE(ulpfec_payload_type, 127);
229
danilchap7c9426c2016-04-14 03:05:31 -0700230 rtc::CritScope cs(&crit_);
brandtrd8048952016-11-07 02:08:51 -0800231 red_payload_type_ = red_payload_type;
brandtrf1bb4762016-11-07 03:05:06 -0800232 ulpfec_payload_type_ = ulpfec_payload_type;
233
234 // Must not enable ULPFEC without RED.
235 // TODO(brandtr): We currently support enabling RED without ULPFEC. Change
236 // this when we have removed the RED/RTX send-side workaround, so that we
237 // ensure that RED and ULPFEC are only enabled together.
238 RTC_DCHECK(red_enabled() || !ulpfec_enabled());
brandtrd8048952016-11-07 02:08:51 -0800239
brandtr131bc492016-11-10 05:01:11 -0800240 // Reset FEC parameters.
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700241 delta_fec_params_ = FecProtectionParams{0, 1, kFecMaskRandom};
242 key_fec_params_ = FecProtectionParams{0, 1, kFecMaskRandom};
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
brandtrf1bb4762016-11-07 03:05:06 -0800245void RTPSenderVideo::GetUlpfecConfig(int* red_payload_type,
brandtrd8048952016-11-07 02:08:51 -0800246 int* ulpfec_payload_type) const {
danilchap7c9426c2016-04-14 03:05:31 -0700247 rtc::CritScope cs(&crit_);
brandtrd8048952016-11-07 02:08:51 -0800248 *red_payload_type = red_payload_type_;
brandtrf1bb4762016-11-07 03:05:06 -0800249 *ulpfec_payload_type = ulpfec_payload_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250}
251
danilchap2a615fc2016-11-11 02:27:35 -0800252size_t RTPSenderVideo::CalculateFecPacketOverhead() const {
brandtr131bc492016-11-10 05:01:11 -0800253 if (flexfec_enabled())
254 return flexfec_sender_->MaxPacketOverhead();
255
stefan8f4c77f2016-06-03 00:16:45 -0700256 size_t overhead = 0;
brandtrf1bb4762016-11-07 03:05:06 -0800257 if (red_enabled()) {
brandtr131bc492016-11-10 05:01:11 -0800258 // The RED overhead is due to a small header.
259 overhead += kRedForFecHeaderLength;
260 }
261 if (ulpfec_enabled()) {
262 // For ULPFEC, the overhead is the FEC headers plus RED for FEC header
263 // (see above) plus anything in RTP header beyond the 12 bytes base header
264 // (CSRC list, extensions...)
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000265 // This reason for the header extensions to be included here is that
266 // from an FEC viewpoint, they are part of the payload to be protected.
267 // (The base RTP header is already protected by the FEC header.)
brandtr131bc492016-11-10 05:01:11 -0800268 overhead += ulpfec_generator_.MaxPacketOverhead() +
269 (rtp_sender_->RtpHeaderLength() - kRtpHeaderSize);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000270 }
stefan8f4c77f2016-06-03 00:16:45 -0700271 return overhead;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272}
273
brandtr1743a192016-11-07 03:36:05 -0800274void RTPSenderVideo::SetFecParameters(const FecProtectionParams& delta_params,
275 const FecProtectionParams& key_params) {
danilchap7c9426c2016-04-14 03:05:31 -0700276 rtc::CritScope cs(&crit_);
brandtr131bc492016-11-10 05:01:11 -0800277 delta_fec_params_ = delta_params;
278 key_fec_params_ = key_params;
marpan@google.com80c5d7a2011-07-15 21:32:40 +0000279}
280
brandtr9dfff292016-11-14 05:14:50 -0800281rtc::Optional<uint32_t> RTPSenderVideo::FlexfecSsrc() const {
282 if (flexfec_sender_) {
Oskar Sundbom3419cf92017-11-16 10:55:48 +0100283 return flexfec_sender_->ssrc();
brandtr9dfff292016-11-14 05:14:50 -0800284 }
Oskar Sundbom3419cf92017-11-16 10:55:48 +0100285 return rtc::nullopt;
brandtr9dfff292016-11-14 05:14:50 -0800286}
287
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700288bool RTPSenderVideo::SendVideo(RtpVideoCodecTypes video_type,
289 FrameType frame_type,
290 int8_t payload_type,
danilchape5b41412016-08-22 03:39:23 -0700291 uint32_t rtp_timestamp,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700292 int64_t capture_time_ms,
293 const uint8_t* payload_data,
294 size_t payload_size,
295 const RTPFragmentationHeader* fragmentation,
spranga8ae6f22017-09-04 07:23:56 -0700296 const RTPVideoHeader* video_header,
297 int64_t expected_retransmission_time_ms) {
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700298 if (payload_size == 0)
299 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
danilchap74110612016-10-02 10:54:29 -0700301 // Create header that will be reused in all packets.
302 std::unique_ptr<RtpPacketToSend> rtp_header = rtp_sender_->AllocatePacket();
303 rtp_header->SetPayloadType(payload_type);
304 rtp_header->SetTimestamp(rtp_timestamp);
305 rtp_header->set_capture_time_ms(capture_time_ms);
ilnik7a3006b2017-05-23 09:34:21 -0700306 auto last_packet = rtc::MakeUnique<RtpPacketToSend>(*rtp_header);
danilchap2a615fc2016-11-11 02:27:35 -0800307
308 size_t fec_packet_overhead;
309 bool red_enabled;
310 int32_t retransmission_settings;
311 {
kthelgason917a4ee2016-11-10 06:22:17 -0800312 rtc::CritScope cs(&crit_);
danilchap2a615fc2016-11-11 02:27:35 -0800313 // According to
314 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
315 // ts_126114v120700p.pdf Section 7.4.5:
316 // The MTSI client shall add the payload bytes as defined in this clause
317 // onto the last RTP packet in each group of packets which make up a key
318 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
319 // (HEVC)). The MTSI client may also add the payload bytes onto the last RTP
320 // packet in each group of packets which make up another type of frame
321 // (e.g. a P-Frame) only if the current value is different from the previous
322 // value sent.
323 if (video_header) {
324 // Set rotation when key frame or when changed (to follow standard).
325 // Or when different from 0 (to follow current receiver implementation).
326 VideoRotation current_rotation = video_header->rotation;
327 if (frame_type == kVideoFrameKey || current_rotation != last_rotation_ ||
328 current_rotation != kVideoRotation_0)
ilnik7a3006b2017-05-23 09:34:21 -0700329 last_packet->SetExtension<VideoOrientation>(current_rotation);
danilchap2a615fc2016-11-11 02:27:35 -0800330 last_rotation_ = current_rotation;
ilnik00d802b2017-04-11 10:34:31 -0700331 // Report content type only for key frames.
332 if (frame_type == kVideoFrameKey &&
ilniked9b9ff2017-06-02 07:30:20 -0700333 video_header->content_type != VideoContentType::UNSPECIFIED) {
ilnik7a3006b2017-05-23 09:34:21 -0700334 last_packet->SetExtension<VideoContentTypeExtension>(
ilnik00d802b2017-04-11 10:34:31 -0700335 video_header->content_type);
336 }
sprangba050a62017-08-18 02:51:12 -0700337 if (video_header->video_timing.flags != TimingFrameFlags::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -0700338 last_packet->SetExtension<VideoTimingExtension>(
339 video_header->video_timing);
ilnik04f4d122017-06-19 07:18:55 -0700340 }
danilchap2a615fc2016-11-11 02:27:35 -0800341 }
342
343 // FEC settings.
344 const FecProtectionParams& fec_params =
345 frame_type == kVideoFrameKey ? key_fec_params_ : delta_fec_params_;
346 if (flexfec_enabled())
347 flexfec_sender_->SetFecParameters(fec_params);
348 if (ulpfec_enabled())
349 ulpfec_generator_.SetFecParameters(fec_params);
350
351 fec_packet_overhead = CalculateFecPacketOverhead();
352 red_enabled = this->red_enabled();
353 retransmission_settings = retransmission_settings_;
danilchapc1600c52016-10-26 03:33:11 -0700354 }
danilchap74110612016-10-02 10:54:29 -0700355
nisse284542b2017-01-10 08:58:32 -0800356 size_t packet_capacity = rtp_sender_->MaxRtpPacketSize() -
danilchap2a615fc2016-11-11 02:27:35 -0800357 fec_packet_overhead -
danilchap74110612016-10-02 10:54:29 -0700358 (rtp_sender_->RtxStatus() ? kRtxHeaderSize : 0);
359 RTC_DCHECK_LE(packet_capacity, rtp_header->capacity());
360 RTC_DCHECK_GT(packet_capacity, rtp_header->headers_size());
ilnik7a3006b2017-05-23 09:34:21 -0700361 RTC_DCHECK_GT(packet_capacity, last_packet->headers_size());
danilchap74110612016-10-02 10:54:29 -0700362 size_t max_data_payload_length = packet_capacity - rtp_header->headers_size();
ilnik7a3006b2017-05-23 09:34:21 -0700363 RTC_DCHECK_GE(last_packet->headers_size(), rtp_header->headers_size());
364 size_t last_packet_reduction_len =
365 last_packet->headers_size() - rtp_header->headers_size();
danilchap74110612016-10-02 10:54:29 -0700366
kwiberg84be5112016-04-27 01:19:58 -0700367 std::unique_ptr<RtpPacketizer> packetizer(RtpPacketizer::Create(
ilnik7a3006b2017-05-23 09:34:21 -0700368 video_type, max_data_payload_length, last_packet_reduction_len,
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700369 video_header ? &(video_header->codecHeader) : nullptr, frame_type));
spranga8ae6f22017-09-04 07:23:56 -0700370
371 const uint8_t temporal_id =
372 video_header ? GetTemporalId(*video_header) : kNoTemporalIdx;
373 StorageType storage = GetStorageType(temporal_id, retransmission_settings,
374 expected_retransmission_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
ilnik7a3006b2017-05-23 09:34:21 -0700376 size_t num_packets =
Niels Möller34fa2952017-10-27 10:19:44 +0200377 packetizer->SetPayloadData(payload_data, payload_size, fragmentation);
ilnik7a3006b2017-05-23 09:34:21 -0700378
379 if (num_packets == 0)
380 return false;
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000381
danilchap2a615fc2016-11-11 02:27:35 -0800382 bool first_frame = first_frame_sent_();
ilnik7a3006b2017-05-23 09:34:21 -0700383 for (size_t i = 0; i < num_packets; ++i) {
384 bool last = (i + 1) == num_packets;
385 auto packet = last ? std::move(last_packet)
386 : rtc::MakeUnique<RtpPacketToSend>(*rtp_header);
387 if (!packetizer->NextPacket(packet.get()))
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700388 return false;
ilnik7a3006b2017-05-23 09:34:21 -0700389 RTC_DCHECK_LE(packet->payload_size(),
390 last ? max_data_payload_length - last_packet_reduction_len
391 : max_data_payload_length);
danilchap74110612016-10-02 10:54:29 -0700392 if (!rtp_sender_->AssignSequenceNumber(packet.get()))
393 return false;
394
spranga8ae6f22017-09-04 07:23:56 -0700395 // No FEC protection for upper temporal layers, if used.
396 bool protect_packet = temporal_id == 0 || temporal_id == kNoTemporalIdx;
397
ilnik04f4d122017-06-19 07:18:55 -0700398 // Put packetization finish timestamp into extension.
ilnike4350192017-06-29 02:27:44 -0700399 if (packet->HasExtension<VideoTimingExtension>()) {
ilnik04f4d122017-06-19 07:18:55 -0700400 packet->set_packetization_finish_time_ms(clock_->TimeInMilliseconds());
ilnik10894992017-06-21 08:23:19 -0700401 // TODO(ilnik): Due to webrtc:7859, packets with timing extensions are not
402 // protected by FEC. It reduces FEC efficiency a bit. When FEC is moved
403 // below the pacer, it can be re-enabled for these packets.
404 // NOTE: Any RTP stream processor in the network, modifying 'network'
405 // timestamps in the timing frames extension have to be an end-point for
406 // FEC, otherwise recovered by FEC packets will be corrupted.
407 protect_packet = false;
ilnik04f4d122017-06-19 07:18:55 -0700408 }
409
brandtr131bc492016-11-10 05:01:11 -0800410 if (flexfec_enabled()) {
411 // TODO(brandtr): Remove the FlexFEC code path when FlexfecSender
412 // is wired up to PacedSender instead.
413 SendVideoPacketWithFlexfec(std::move(packet), storage, protect_packet);
414 } else if (red_enabled) {
415 SendVideoPacketAsRedMaybeWithUlpfec(std::move(packet), storage,
416 protect_packet);
mflodmanfcf54bd2015-04-14 21:28:08 +0200417 } else {
danilchap74110612016-10-02 10:54:29 -0700418 SendVideoPacket(std::move(packet), storage);
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000419 }
skvlad98bb6642016-04-07 15:36:45 -0700420
421 if (first_frame) {
ilnik7a3006b2017-05-23 09:34:21 -0700422 if (i == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100423 RTC_LOG(LS_INFO)
skvlad98bb6642016-04-07 15:36:45 -0700424 << "Sent first RTP packet of the first video frame (pre-pacer)";
425 }
426 if (last) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100427 RTC_LOG(LS_INFO)
skvlad98bb6642016-04-07 15:36:45 -0700428 << "Sent last RTP packet of the first video frame (pre-pacer)";
429 }
430 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000431 }
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000432
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700433 TRACE_EVENT_ASYNC_END1("webrtc", "Video", capture_time_ms, "timestamp",
danilchape5b41412016-08-22 03:39:23 -0700434 rtp_timestamp);
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700435 return true;
mflodmanfcf54bd2015-04-14 21:28:08 +0200436}
437
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000438uint32_t RTPSenderVideo::VideoBitrateSent() const {
sprangcd349d92016-07-13 09:11:28 -0700439 rtc::CritScope cs(&stats_crit_);
440 return video_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000441}
442
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000443uint32_t RTPSenderVideo::FecOverheadRate() const {
sprangcd349d92016-07-13 09:11:28 -0700444 rtc::CritScope cs(&stats_crit_);
445 return fec_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000446}
447
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000448int RTPSenderVideo::SelectiveRetransmissions() const {
danilchap7c9426c2016-04-14 03:05:31 -0700449 rtc::CritScope cs(&crit_);
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700450 return retransmission_settings_;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000451}
452
mflodmanfcf54bd2015-04-14 21:28:08 +0200453void RTPSenderVideo::SetSelectiveRetransmissions(uint8_t settings) {
danilchap7c9426c2016-04-14 03:05:31 -0700454 rtc::CritScope cs(&crit_);
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700455 retransmission_settings_ = settings;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000456}
457
spranga8ae6f22017-09-04 07:23:56 -0700458StorageType RTPSenderVideo::GetStorageType(
459 uint8_t temporal_id,
460 int32_t retransmission_settings,
461 int64_t expected_retransmission_time_ms) {
462 if (retransmission_settings == kRetransmitOff)
463 return StorageType::kDontRetransmit;
464 if (retransmission_settings == kRetransmitAllPackets)
465 return StorageType::kAllowRetransmission;
466
467 rtc::CritScope cs(&stats_crit_);
468 // Media packet storage.
469 if ((retransmission_settings & kConditionallyRetransmitHigherLayers) &&
470 UpdateConditionalRetransmit(temporal_id,
471 expected_retransmission_time_ms)) {
472 retransmission_settings |= kRetransmitHigherLayers;
473 }
474
475 if (temporal_id == kNoTemporalIdx)
476 return kAllowRetransmission;
477
478 if ((retransmission_settings & kRetransmitBaseLayer) && temporal_id == 0)
479 return kAllowRetransmission;
480
481 if ((retransmission_settings & kRetransmitHigherLayers) && temporal_id > 0)
482 return kAllowRetransmission;
483
484 return kDontRetransmit;
485}
486
487uint8_t RTPSenderVideo::GetTemporalId(const RTPVideoHeader& header) {
488 switch (header.codec) {
489 case kRtpVideoVp8:
490 return header.codecHeader.VP8.temporalIdx;
491 case kRtpVideoVp9:
492 return header.codecHeader.VP9.temporal_idx;
493 default:
494 return kNoTemporalIdx;
495 }
496}
497
498bool RTPSenderVideo::UpdateConditionalRetransmit(
499 uint8_t temporal_id,
500 int64_t expected_retransmission_time_ms) {
501 int64_t now_ms = clock_->TimeInMilliseconds();
502 // Update stats for any temporal layer.
503 TemporalLayerStats* current_layer_stats =
504 &frame_stats_by_temporal_layer_[temporal_id];
505 current_layer_stats->frame_rate_fp1000s.Update(1, now_ms);
506 int64_t tl_frame_interval = now_ms - current_layer_stats->last_frame_time_ms;
507 current_layer_stats->last_frame_time_ms = now_ms;
508
509 // Conditional retransmit only applies to upper layers.
510 if (temporal_id != kNoTemporalIdx && temporal_id > 0) {
511 if (tl_frame_interval >= kMaxUnretransmittableFrameIntervalMs) {
512 // Too long since a retransmittable frame in this layer, enable NACK
513 // protection.
514 return true;
515 } else {
516 // Estimate when the next frame of any lower layer will be sent.
517 const int64_t kUndefined = std::numeric_limits<int64_t>::max();
518 int64_t expected_next_frame_time = kUndefined;
519 for (int i = temporal_id - 1; i >= 0; --i) {
520 TemporalLayerStats* stats = &frame_stats_by_temporal_layer_[i];
521 rtc::Optional<uint32_t> rate = stats->frame_rate_fp1000s.Rate(now_ms);
522 if (rate) {
523 int64_t tl_next = stats->last_frame_time_ms + 1000000 / *rate;
524 if (tl_next - now_ms > -expected_retransmission_time_ms &&
525 tl_next < expected_next_frame_time) {
526 expected_next_frame_time = tl_next;
527 }
528 }
529 }
530
531 if (expected_next_frame_time == kUndefined ||
532 expected_next_frame_time - now_ms > expected_retransmission_time_ms) {
533 // The next frame in a lower layer is expected at a later time (or
534 // unable to tell due to lack of data) than a retransmission is
535 // estimated to be able to arrive, so allow this packet to be nacked.
536 return true;
537 }
538 }
539 }
540
541 return false;
542}
543
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000544} // namespace webrtc