blob: 57ae87c4a5cd707d2d665ac141124dc9d122b32e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org39e96592012-03-01 18:22:48 +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
mflodmanfa666592016-04-28 23:15:33 -070011#include "webrtc/video/rtp_stream_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000013#include <vector>
14
Peter Boström415d2cd2015-10-26 11:35:17 +010015#include "webrtc/base/logging.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070016#include "webrtc/common_types.h"
Peter Boström9c017252016-02-26 16:26:20 +010017#include "webrtc/config.h"
mflodmanc0e58a32016-04-25 01:26:26 -070018#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000019#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/rtp_rtcp/include/fec_receiver.h"
21#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010022#include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
23#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010024#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
25#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Peter Boström0b250722016-04-22 18:23:15 +020026#include "webrtc/modules/video_coding/video_coding_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010027#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
29#include "webrtc/system_wrappers/include/trace.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070030#include "webrtc/video/receive_statistics_proxy.h"
mflodmandc7d0d22016-05-06 05:32:22 -070031#include "webrtc/video/vie_remb.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000032
33namespace webrtc {
34
mflodmanc0e58a32016-04-25 01:26:26 -070035std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
36 ReceiveStatistics* receive_statistics,
37 Transport* outgoing_transport,
38 RtcpRttStats* rtt_stats,
39 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
40 RemoteBitrateEstimator* remote_bitrate_estimator,
41 RtpPacketSender* paced_sender,
42 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
43 RtpRtcp::Configuration configuration;
44 configuration.audio = false;
45 configuration.receiver_only = true;
46 configuration.receive_statistics = receive_statistics;
47 configuration.outgoing_transport = outgoing_transport;
48 configuration.intra_frame_callback = nullptr;
49 configuration.rtt_stats = rtt_stats;
50 configuration.rtcp_packet_type_counter_observer =
51 rtcp_packet_type_counter_observer;
52 configuration.paced_sender = paced_sender;
53 configuration.transport_sequence_number_allocator =
54 transport_sequence_number_allocator;
55 configuration.send_bitrate_observer = nullptr;
56 configuration.send_frame_count_observer = nullptr;
57 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070058 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070059 configuration.bandwidth_callback = nullptr;
60 configuration.transport_feedback_callback = nullptr;
61
62 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
63 rtp_rtcp->SetSendingStatus(false);
64 rtp_rtcp->SetSendingMediaStatus(false);
65 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
66
67 return rtp_rtcp;
68}
69
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000070static const int kPacketLogIntervalMs = 10000;
71
mflodmanfa666592016-04-28 23:15:33 -070072RtpStreamReceiver::RtpStreamReceiver(
73 vcm::VideoReceiver* video_receiver,
74 RemoteBitrateEstimator* remote_bitrate_estimator,
75 Transport* transport,
76 RtcpRttStats* rtt_stats,
77 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070078 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070079 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020080 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070081 ReceiveStatisticsProxy* receive_stats_proxy,
82 ProcessThread* process_thread)
Tommi97888bd2016-01-21 23:24:59 +010083 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020084 config_(*config),
Peter Boström0b250722016-04-22 18:23:15 +020085 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000086 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -070087 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -070088 remb_(remb),
89 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +010090 ntp_estimator_(clock_),
91 rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)),
92 rtp_header_parser_(RtpHeaderParser::Create()),
93 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
94 this,
mflodmanfa666592016-04-28 23:15:33 -070095 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +010096 &rtp_payload_registry_)),
97 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
98 fec_receiver_(FecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000099 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000100 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700101 last_packet_log_ms_(-1),
102 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
103 transport,
104 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700105 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700106 remote_bitrate_estimator_,
107 paced_sender,
108 packet_router)) {
109 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700110 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
111 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
112
Tommi733b5472016-06-10 17:58:01 +0200113 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700114 << "A stream should not be configured with RTCP disabled. This value is "
115 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700116 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
117 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
118 RTC_DCHECK(config_.rtp.local_ssrc != 0);
119 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
120
Tommi733b5472016-06-10 17:58:01 +0200121 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
122 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700123 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200124 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700125 rtp_rtcp_->SetREMBStatus(true);
126 remb_->AddReceiveChannel(rtp_rtcp_.get());
127 }
128
Tommi733b5472016-06-10 17:58:01 +0200129 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
130 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
131 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700132 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700133
134 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200135 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
136 ? kMaxPacketAgeToNack
137 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700138 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700139
140 // TODO(pbos): Support multiple RTX, per video payload.
141 for (const auto& kv : config_.rtp.rtx) {
142 RTC_DCHECK(kv.second.ssrc != 0);
143 RTC_DCHECK(kv.second.payload_type != 0);
144
145 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
146 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
147 kv.first);
148 }
149
150 // If set to true, the RTX payload type mapping supplied in
151 // |SetRtxPayloadType| will be used when restoring RTX packets. Without it,
152 // RTX packets will always be restored to the last non-RTX packet payload type
153 // received.
154 // TODO(holmer): When Chrome no longer depends on this being false by default,
155 // always use the mapping and remove this whole codepath.
156 rtp_payload_registry_.set_use_rtx_payload_mapping_on_restore(
157 config_.rtp.use_rtx_payload_mapping_on_restore);
158
159 if (IsFecEnabled()) {
160 VideoCodec ulpfec_codec = {};
161 ulpfec_codec.codecType = kVideoCodecULPFEC;
162 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
163 ulpfec_codec.plType = config_.rtp.fec.ulpfec_payload_type;
164 RTC_CHECK(SetReceiveCodec(ulpfec_codec));
165
166 VideoCodec red_codec = {};
167 red_codec.codecType = kVideoCodecRED;
168 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
169 red_codec.plType = config_.rtp.fec.red_payload_type;
170 RTC_CHECK(SetReceiveCodec(red_codec));
171 if (config_.rtp.fec.red_rtx_payload_type != -1) {
172 rtp_payload_registry_.SetRtxPayloadType(
173 config_.rtp.fec.red_rtx_payload_type,
174 config_.rtp.fec.red_payload_type);
175 }
philipelae284082016-05-09 12:14:29 +0200176
177 rtp_rtcp_->SetGenericFECStatus(true,
178 config_.rtp.fec.red_payload_type,
179 config_.rtp.fec.ulpfec_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700180 }
181
Tommi733b5472016-06-10 17:58:01 +0200182 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700183 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
184
185 // Stats callback for CNAME changes.
186 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
187
188 process_thread_->RegisterModule(rtp_receive_statistics_.get());
189 process_thread_->RegisterModule(rtp_rtcp_.get());
mflodmanc0e58a32016-04-25 01:26:26 -0700190}
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
mflodmanfa666592016-04-28 23:15:33 -0700192RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700193 process_thread_->DeRegisterModule(rtp_receive_statistics_.get());
194 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
mflodmandc7d0d22016-05-06 05:32:22 -0700196 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
197 rtp_rtcp_->SetREMBStatus(false);
198 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
199 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000200}
201
mflodmanfa666592016-04-28 23:15:33 -0700202bool RtpStreamReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000203 int8_t old_pltype = -1;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100204 if (rtp_payload_registry_.ReceivePayloadType(
205 video_codec.plName, kVideoPayloadTypeFrequency, 0,
206 video_codec.maxBitrate, &old_pltype) != -1) {
207 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000208 }
209
Peter Boström4fa7eca2016-03-02 15:05:53 +0100210 return rtp_receiver_->RegisterReceivePayload(
211 video_codec.plName, video_codec.plType, kVideoPayloadTypeFrequency,
212 0, 0) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000213}
214
mflodmanfa666592016-04-28 23:15:33 -0700215uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000216 return rtp_receiver_->SSRC();
217}
218
mflodmanfa666592016-04-28 23:15:33 -0700219int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000220 return rtp_receiver_->CSRCs(csrcs);
221}
222
mflodmanfa666592016-04-28 23:15:33 -0700223RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000224 return rtp_receiver_.get();
225}
226
mflodmanfa666592016-04-28 23:15:33 -0700227int32_t RtpStreamReceiver::OnReceivedPayloadData(
228 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200229 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700230 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200231 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000232 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000233 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100234 ntp_estimator_.Estimate(rtp_header->header.timestamp);
Peter Boström0b250722016-04-22 18:23:15 +0200235 if (video_receiver_->IncomingPacket(payload_data, payload_size,
236 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000237 // Check this...
238 return -1;
239 }
240 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241}
242
mflodmanfa666592016-04-28 23:15:33 -0700243bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
244 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000245 RTPHeader header;
246 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247 return false;
248 }
249 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000250 bool in_order = IsPacketInOrder(header);
251 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000252}
253
mflodmanfa666592016-04-28 23:15:33 -0700254// TODO(pbos): Remove as soon as audio can handle a changing payload type
255// without this callback.
256int32_t RtpStreamReceiver::OnInitializeDecoder(
257 const int8_t payload_type,
258 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
259 const int frequency,
260 const size_t channels,
261 const uint32_t rate) {
262 RTC_NOTREACHED();
263 return 0;
264}
265
266void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
267 rtp_rtcp_->SetRemoteSSRC(ssrc);
268}
269
270bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
271 size_t rtp_packet_length,
272 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100273 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000274 {
Tommi97888bd2016-01-21 23:24:59 +0100275 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000276 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100277 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000278 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000279 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000280
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000281 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000282 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000283 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100284 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000285 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000286 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000287 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000288 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000289 if (packet_time.timestamp != -1)
290 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
291 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000292 arrival_time_ms = now_ms;
293
294 {
295 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100296 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000297 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
298 std::stringstream ss;
299 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
300 << static_cast<int>(header.payloadType) << ", timestamp: "
301 << header.timestamp << ", sequence number: " << header.sequenceNumber
302 << ", arrival time: " << arrival_time_ms;
303 if (header.extension.hasTransmissionTimeOffset)
304 ss << ", toffset: " << header.extension.transmissionTimeOffset;
305 if (header.extension.hasAbsoluteSendTime)
306 ss << ", abs send time: " << header.extension.absoluteSendTime;
307 LOG(LS_INFO) << ss.str();
308 last_packet_log_ms_ = now_ms;
309 }
310 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000311
Stefan Holmerff4ea932015-06-18 16:01:33 +0200312 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
313 header, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000314 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000316 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100317 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100318 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000319 // Update receive statistics after ReceivePacket.
320 // Receive statistics will be reset if the payload type changes (make sure
321 // that the first packet is included in the stats).
322 rtp_receive_statistics_->IncomingPacket(
323 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
324 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000325}
326
mflodmancfc8e3b2016-05-03 21:22:04 -0700327int32_t RtpStreamReceiver::RequestKeyFrame() {
328 return rtp_rtcp_->RequestKeyFrame();
329}
330
331int32_t RtpStreamReceiver::SliceLossIndicationRequest(
332 const uint64_t picture_id) {
333 return rtp_rtcp_->SendRTCPSliceLossIndication(
334 static_cast<uint8_t>(picture_id));
335}
336
mflodmandc7d0d22016-05-06 05:32:22 -0700337bool RtpStreamReceiver::IsFecEnabled() const {
338 return config_.rtp.fec.red_payload_type != -1 &&
339 config_.rtp.fec.ulpfec_payload_type != -1;
340}
341
342bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
343 return config_.rtp.nack.rtp_history_ms > 0;
344}
345
346void RtpStreamReceiver::RequestPacketRetransmit(
347 const std::vector<uint16_t>& sequence_numbers) {
348 rtp_rtcp_->SendNack(sequence_numbers);
349}
350
mflodmancfc8e3b2016-05-03 21:22:04 -0700351int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
352 uint16_t length) {
353 return rtp_rtcp_->SendNACK(sequence_numbers, length);
354}
355
mflodmanfa666592016-04-28 23:15:33 -0700356bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
357 size_t packet_length,
358 const RTPHeader& header,
359 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100360 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000361 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
362 }
363 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000364 assert(packet_length >= header.headerLength);
365 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000366 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100367 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
368 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000369 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000370 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000371 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
372 payload_specific, in_order);
373}
374
mflodmanfa666592016-04-28 23:15:33 -0700375bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
376 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100377 if (rtp_payload_registry_.IsRed(header)) {
378 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000379 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000380 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200381 // Notify video_receiver about received FEC packets to avoid NACKing these
382 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000383 NotifyReceiverOfFecPacket(header);
384 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000385 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000386 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000387 return false;
388 }
389 return fec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100390 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000391 if (header.headerLength + header.paddingLength == packet_length) {
392 // This is an empty packet and should be silently dropped before trying to
393 // parse the RTX header.
394 return true;
395 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000396 // Remove the RTX header and parse the original RTP header.
397 if (packet_length < header.headerLength)
398 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000399 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000400 return false;
Tommi97888bd2016-01-21 23:24:59 +0100401 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000402 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000403 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000404 return false;
405 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100406 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700407 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
408 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100409 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
410 << header.ssrc << " payload type: "
411 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000412 return false;
413 }
414 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700415 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000416 restored_packet_in_use_ = false;
417 return ret;
418 }
419 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420}
421
mflodmanfa666592016-04-28 23:15:33 -0700422void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000423 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100424 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000425 if (last_media_payload_type < 0) {
426 LOG(LS_WARNING) << "Failed to get last media payload type.";
427 return;
428 }
429 // Fake an empty media packet.
430 WebRtcRTPHeader rtp_header = {};
431 rtp_header.header = header;
432 rtp_header.header.payloadType = last_media_payload_type;
433 rtp_header.header.paddingLength = 0;
434 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100435 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
436 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000437 LOG(LS_WARNING) << "Failed to get payload specifics.";
438 return;
439 }
440 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000441 rtp_header.type.Video.rotation = kVideoRotation_0;
442 if (header.extension.hasVideoRotation) {
443 rtp_header.type.Video.rotation =
444 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
445 }
isheriff6b4b5f32016-06-08 00:24:21 -0700446 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
447
Peter Boström74f6e9e2016-04-04 17:56:10 +0200448 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000449}
450
mflodmanfa666592016-04-28 23:15:33 -0700451bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
452 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000453 {
Tommi97888bd2016-01-21 23:24:59 +0100454 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000455 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100456 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000457 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100458 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
Per83d09102016-04-15 14:59:13 +0200460 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000461
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000462 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200463 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000464 if (rtt == 0) {
465 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100466 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000467 }
468 uint32_t ntp_secs = 0;
469 uint32_t ntp_frac = 0;
470 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200471 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
472 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000473 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100474 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000475 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100476 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000477
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100478 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000479}
480
mflodmandc7d0d22016-05-06 05:32:22 -0700481void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
482 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
483 : RtcpMode::kOff);
484}
485
mflodmanfa666592016-04-28 23:15:33 -0700486void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100487 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000488 receiving_ = true;
489}
490
mflodmanfa666592016-04-28 23:15:33 -0700491void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100492 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000493 receiving_ = false;
494}
495
mflodmanfa666592016-04-28 23:15:33 -0700496bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000497 StreamStatistician* statistician =
498 rtp_receive_statistics_->GetStatistician(header.ssrc);
499 if (!statistician)
500 return false;
501 return statistician->IsPacketInOrder(header.sequenceNumber);
502}
503
mflodmanfa666592016-04-28 23:15:33 -0700504bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
505 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000506 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100507 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000508 return false;
509 StreamStatistician* statistician =
510 rtp_receive_statistics_->GetStatistician(header.ssrc);
511 if (!statistician)
512 return false;
513 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000514 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200515 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000516 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000517 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000518}
mflodmandc7d0d22016-05-06 05:32:22 -0700519
520void RtpStreamReceiver::UpdateHistograms() {
521 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
522 if (counter.num_packets > 0) {
523 RTC_LOGGED_HISTOGRAM_PERCENTAGE(
524 "WebRTC.Video.ReceivedFecPacketsInPercent",
525 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
526 }
527 if (counter.num_fec_packets > 0) {
528 RTC_LOGGED_HISTOGRAM_PERCENTAGE(
529 "WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
530 static_cast<int>(counter.num_recovered_packets * 100 /
531 counter.num_fec_packets));
532 }
533}
534
535void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
536 const std::string& extension, int id) {
537 // One-byte-extension local identifiers are in the range 1-14 inclusive.
538 RTC_DCHECK_GE(id, 1);
539 RTC_DCHECK_LE(id, 14);
540 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
541 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
542 StringToRtpExtensionType(extension), id));
543}
544
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000545} // namespace webrtc