blob: 4d5402f7d9df61879d4f318a575c9654200dc99d [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"
28#include "webrtc/system_wrappers/include/tick_util.h"
29#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
30#include "webrtc/system_wrappers/include/trace.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070031#include "webrtc/video/receive_statistics_proxy.h"
mflodmandc7d0d22016-05-06 05:32:22 -070032#include "webrtc/video/vie_remb.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000033
34namespace webrtc {
35
mflodmanc0e58a32016-04-25 01:26:26 -070036std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
37 ReceiveStatistics* receive_statistics,
38 Transport* outgoing_transport,
39 RtcpRttStats* rtt_stats,
40 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
41 RemoteBitrateEstimator* remote_bitrate_estimator,
42 RtpPacketSender* paced_sender,
43 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
44 RtpRtcp::Configuration configuration;
45 configuration.audio = false;
46 configuration.receiver_only = true;
47 configuration.receive_statistics = receive_statistics;
48 configuration.outgoing_transport = outgoing_transport;
49 configuration.intra_frame_callback = nullptr;
50 configuration.rtt_stats = rtt_stats;
51 configuration.rtcp_packet_type_counter_observer =
52 rtcp_packet_type_counter_observer;
53 configuration.paced_sender = paced_sender;
54 configuration.transport_sequence_number_allocator =
55 transport_sequence_number_allocator;
56 configuration.send_bitrate_observer = nullptr;
57 configuration.send_frame_count_observer = nullptr;
58 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070059 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070060 configuration.bandwidth_callback = nullptr;
61 configuration.transport_feedback_callback = nullptr;
62
63 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
64 rtp_rtcp->SetSendingStatus(false);
65 rtp_rtcp->SetSendingMediaStatus(false);
66 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
67
68 return rtp_rtcp;
69}
70
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000071static const int kPacketLogIntervalMs = 10000;
72
mflodmanfa666592016-04-28 23:15:33 -070073RtpStreamReceiver::RtpStreamReceiver(
74 vcm::VideoReceiver* video_receiver,
75 RemoteBitrateEstimator* remote_bitrate_estimator,
76 Transport* transport,
77 RtcpRttStats* rtt_stats,
78 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070079 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070080 VieRemb* remb,
mflodmancfc8e3b2016-05-03 21:22:04 -070081 const VideoReceiveStream::Config& config,
mflodmandc7d0d22016-05-06 05:32:22 -070082 ReceiveStatisticsProxy* receive_stats_proxy,
83 ProcessThread* process_thread)
Tommi97888bd2016-01-21 23:24:59 +010084 : clock_(Clock::GetRealTimeClock()),
mflodmandc7d0d22016-05-06 05:32:22 -070085 config_(config),
Peter Boström0b250722016-04-22 18:23:15 +020086 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000087 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -070088 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -070089 remb_(remb),
90 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +010091 ntp_estimator_(clock_),
92 rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)),
93 rtp_header_parser_(RtpHeaderParser::Create()),
94 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
95 this,
mflodmanfa666592016-04-28 23:15:33 -070096 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +010097 &rtp_payload_registry_)),
98 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
99 fec_receiver_(FecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000100 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000101 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700102 last_packet_log_ms_(-1),
103 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
104 transport,
105 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700106 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700107 remote_bitrate_estimator_,
108 paced_sender,
109 packet_router)) {
110 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700111 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
112 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
113
114 RTC_DCHECK(config.rtp.rtcp_mode != RtcpMode::kOff)
115 << "A stream should not be configured with RTCP disabled. This value is "
116 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700117 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
118 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
119 RTC_DCHECK(config_.rtp.local_ssrc != 0);
120 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
121
mflodmancfc8e3b2016-05-03 21:22:04 -0700122 rtp_rtcp_->SetRTCPStatus(config.rtp.rtcp_mode);
mflodmandc7d0d22016-05-06 05:32:22 -0700123 rtp_rtcp_->SetSSRC(config.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700124 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
mflodmandc7d0d22016-05-06 05:32:22 -0700125 if (config.rtp.remb) {
126 rtp_rtcp_->SetREMBStatus(true);
127 remb_->AddReceiveChannel(rtp_rtcp_.get());
128 }
129
130 for (size_t i = 0; i < config.rtp.extensions.size(); ++i) {
131 EnableReceiveRtpHeaderExtension(config.rtp.extensions[i].name,
132 config.rtp.extensions[i].id);
133 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700134
135 static const int kMaxPacketAgeToNack = 450;
136 NACKMethod nack_method =
137 config.rtp.nack.rtp_history_ms > 0 ? kNackRtcp : kNackOff;
138 const int max_reordering_threshold = (nack_method == kNackRtcp)
139 ? kMaxPacketAgeToNack : kDefaultMaxReorderingThreshold;
140 rtp_receiver_->SetNACKStatus(nack_method);
141 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700142
143 // TODO(pbos): Support multiple RTX, per video payload.
144 for (const auto& kv : config_.rtp.rtx) {
145 RTC_DCHECK(kv.second.ssrc != 0);
146 RTC_DCHECK(kv.second.payload_type != 0);
147
148 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
149 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
150 kv.first);
151 }
152
153 // If set to true, the RTX payload type mapping supplied in
154 // |SetRtxPayloadType| will be used when restoring RTX packets. Without it,
155 // RTX packets will always be restored to the last non-RTX packet payload type
156 // received.
157 // TODO(holmer): When Chrome no longer depends on this being false by default,
158 // always use the mapping and remove this whole codepath.
159 rtp_payload_registry_.set_use_rtx_payload_mapping_on_restore(
160 config_.rtp.use_rtx_payload_mapping_on_restore);
161
162 if (IsFecEnabled()) {
163 VideoCodec ulpfec_codec = {};
164 ulpfec_codec.codecType = kVideoCodecULPFEC;
165 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
166 ulpfec_codec.plType = config_.rtp.fec.ulpfec_payload_type;
167 RTC_CHECK(SetReceiveCodec(ulpfec_codec));
168
169 VideoCodec red_codec = {};
170 red_codec.codecType = kVideoCodecRED;
171 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
172 red_codec.plType = config_.rtp.fec.red_payload_type;
173 RTC_CHECK(SetReceiveCodec(red_codec));
174 if (config_.rtp.fec.red_rtx_payload_type != -1) {
175 rtp_payload_registry_.SetRtxPayloadType(
176 config_.rtp.fec.red_rtx_payload_type,
177 config_.rtp.fec.red_payload_type);
178 }
179 }
180
181 if (config.rtp.rtcp_xr.receiver_reference_time_report)
182 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
183
184 // Stats callback for CNAME changes.
185 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
186
187 process_thread_->RegisterModule(rtp_receive_statistics_.get());
188 process_thread_->RegisterModule(rtp_rtcp_.get());
mflodmanc0e58a32016-04-25 01:26:26 -0700189}
niklase@google.com470e71d2011-07-07 08:21:25 +0000190
mflodmanfa666592016-04-28 23:15:33 -0700191RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700192 process_thread_->DeRegisterModule(rtp_receive_statistics_.get());
193 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
mflodmandc7d0d22016-05-06 05:32:22 -0700195 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
196 rtp_rtcp_->SetREMBStatus(false);
197 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
198 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000199}
200
mflodmanfa666592016-04-28 23:15:33 -0700201bool RtpStreamReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000202 int8_t old_pltype = -1;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100203 if (rtp_payload_registry_.ReceivePayloadType(
204 video_codec.plName, kVideoPayloadTypeFrequency, 0,
205 video_codec.maxBitrate, &old_pltype) != -1) {
206 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000207 }
208
Peter Boström4fa7eca2016-03-02 15:05:53 +0100209 return rtp_receiver_->RegisterReceivePayload(
210 video_codec.plName, video_codec.plType, kVideoPayloadTypeFrequency,
211 0, 0) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000212}
213
mflodmanfa666592016-04-28 23:15:33 -0700214uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000215 return rtp_receiver_->SSRC();
216}
217
mflodmanfa666592016-04-28 23:15:33 -0700218int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000219 return rtp_receiver_->CSRCs(csrcs);
220}
221
mflodmanfa666592016-04-28 23:15:33 -0700222RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000223 return rtp_receiver_.get();
224}
225
mflodmanfa666592016-04-28 23:15:33 -0700226int32_t RtpStreamReceiver::OnReceivedPayloadData(
227 const uint8_t* payload_data,
228 const size_t payload_size,
229 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200230 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000231 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000232 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100233 ntp_estimator_.Estimate(rtp_header->header.timestamp);
Peter Boström0b250722016-04-22 18:23:15 +0200234 if (video_receiver_->IncomingPacket(payload_data, payload_size,
235 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000236 // Check this...
237 return -1;
238 }
239 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240}
241
mflodmanfa666592016-04-28 23:15:33 -0700242bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
243 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000244 RTPHeader header;
245 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000246 return false;
247 }
248 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000249 bool in_order = IsPacketInOrder(header);
250 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000251}
252
mflodmanfa666592016-04-28 23:15:33 -0700253// TODO(pbos): Remove as soon as audio can handle a changing payload type
254// without this callback.
255int32_t RtpStreamReceiver::OnInitializeDecoder(
256 const int8_t payload_type,
257 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
258 const int frequency,
259 const size_t channels,
260 const uint32_t rate) {
261 RTC_NOTREACHED();
262 return 0;
263}
264
265void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
266 rtp_rtcp_->SetRemoteSSRC(ssrc);
267}
268
269bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
270 size_t rtp_packet_length,
271 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100272 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000273 {
Tommi97888bd2016-01-21 23:24:59 +0100274 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000275 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100276 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000277 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000278 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000279
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000280 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000281 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000282 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100283 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000284 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000285 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000286 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000287 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000288 if (packet_time.timestamp != -1)
289 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
290 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000291 arrival_time_ms = now_ms;
292
293 {
294 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100295 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000296 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
297 std::stringstream ss;
298 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
299 << static_cast<int>(header.payloadType) << ", timestamp: "
300 << header.timestamp << ", sequence number: " << header.sequenceNumber
301 << ", arrival time: " << arrival_time_ms;
302 if (header.extension.hasTransmissionTimeOffset)
303 ss << ", toffset: " << header.extension.transmissionTimeOffset;
304 if (header.extension.hasAbsoluteSendTime)
305 ss << ", abs send time: " << header.extension.absoluteSendTime;
306 LOG(LS_INFO) << ss.str();
307 last_packet_log_ms_ = now_ms;
308 }
309 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000310
Stefan Holmerff4ea932015-06-18 16:01:33 +0200311 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
312 header, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000313 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000315 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100316 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100317 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000318 // Update receive statistics after ReceivePacket.
319 // Receive statistics will be reset if the payload type changes (make sure
320 // that the first packet is included in the stats).
321 rtp_receive_statistics_->IncomingPacket(
322 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
323 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000324}
325
mflodmancfc8e3b2016-05-03 21:22:04 -0700326int32_t RtpStreamReceiver::RequestKeyFrame() {
327 return rtp_rtcp_->RequestKeyFrame();
328}
329
330int32_t RtpStreamReceiver::SliceLossIndicationRequest(
331 const uint64_t picture_id) {
332 return rtp_rtcp_->SendRTCPSliceLossIndication(
333 static_cast<uint8_t>(picture_id));
334}
335
mflodmandc7d0d22016-05-06 05:32:22 -0700336bool RtpStreamReceiver::IsFecEnabled() const {
337 return config_.rtp.fec.red_payload_type != -1 &&
338 config_.rtp.fec.ulpfec_payload_type != -1;
339}
340
341bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
342 return config_.rtp.nack.rtp_history_ms > 0;
343}
344
345void RtpStreamReceiver::RequestPacketRetransmit(
346 const std::vector<uint16_t>& sequence_numbers) {
347 rtp_rtcp_->SendNack(sequence_numbers);
348}
349
mflodmancfc8e3b2016-05-03 21:22:04 -0700350int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
351 uint16_t length) {
352 return rtp_rtcp_->SendNACK(sequence_numbers, length);
353}
354
mflodmanfa666592016-04-28 23:15:33 -0700355bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
356 size_t packet_length,
357 const RTPHeader& header,
358 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100359 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000360 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
361 }
362 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000363 assert(packet_length >= header.headerLength);
364 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000365 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100366 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
367 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000368 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000369 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000370 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
371 payload_specific, in_order);
372}
373
mflodmanfa666592016-04-28 23:15:33 -0700374bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
375 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100376 if (rtp_payload_registry_.IsRed(header)) {
377 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000378 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000379 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200380 // Notify video_receiver about received FEC packets to avoid NACKing these
381 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000382 NotifyReceiverOfFecPacket(header);
383 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000384 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000385 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000386 return false;
387 }
388 return fec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100389 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000390 if (header.headerLength + header.paddingLength == packet_length) {
391 // This is an empty packet and should be silently dropped before trying to
392 // parse the RTX header.
393 return true;
394 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000395 // Remove the RTX header and parse the original RTP header.
396 if (packet_length < header.headerLength)
397 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000398 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000399 return false;
Tommi97888bd2016-01-21 23:24:59 +0100400 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000401 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000402 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000403 return false;
404 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100405 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700406 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
407 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100408 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
409 << header.ssrc << " payload type: "
410 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000411 return false;
412 }
413 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700414 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000415 restored_packet_in_use_ = false;
416 return ret;
417 }
418 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000419}
420
mflodmanfa666592016-04-28 23:15:33 -0700421void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000422 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100423 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000424 if (last_media_payload_type < 0) {
425 LOG(LS_WARNING) << "Failed to get last media payload type.";
426 return;
427 }
428 // Fake an empty media packet.
429 WebRtcRTPHeader rtp_header = {};
430 rtp_header.header = header;
431 rtp_header.header.payloadType = last_media_payload_type;
432 rtp_header.header.paddingLength = 0;
433 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100434 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
435 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000436 LOG(LS_WARNING) << "Failed to get payload specifics.";
437 return;
438 }
439 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000440 rtp_header.type.Video.rotation = kVideoRotation_0;
441 if (header.extension.hasVideoRotation) {
442 rtp_header.type.Video.rotation =
443 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
444 }
Peter Boström74f6e9e2016-04-04 17:56:10 +0200445 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000446}
447
mflodmanfa666592016-04-28 23:15:33 -0700448bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
449 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000450 {
Tommi97888bd2016-01-21 23:24:59 +0100451 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000452 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100453 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000454 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100455 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
Per83d09102016-04-15 14:59:13 +0200457 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000458
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000459 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200460 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000461 if (rtt == 0) {
462 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100463 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000464 }
465 uint32_t ntp_secs = 0;
466 uint32_t ntp_frac = 0;
467 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200468 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
469 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000470 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100471 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000472 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100473 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000474
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100475 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000476}
477
mflodmandc7d0d22016-05-06 05:32:22 -0700478void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
479 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
480 : RtcpMode::kOff);
481}
482
mflodmanfa666592016-04-28 23:15:33 -0700483void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100484 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000485 receiving_ = true;
486}
487
mflodmanfa666592016-04-28 23:15:33 -0700488void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100489 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000490 receiving_ = false;
491}
492
mflodmanfa666592016-04-28 23:15:33 -0700493bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000494 StreamStatistician* statistician =
495 rtp_receive_statistics_->GetStatistician(header.ssrc);
496 if (!statistician)
497 return false;
498 return statistician->IsPacketInOrder(header.sequenceNumber);
499}
500
mflodmanfa666592016-04-28 23:15:33 -0700501bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
502 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000503 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100504 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000505 return false;
506 StreamStatistician* statistician =
507 rtp_receive_statistics_->GetStatistician(header.ssrc);
508 if (!statistician)
509 return false;
510 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000511 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200512 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000513 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000514 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000515}
mflodmandc7d0d22016-05-06 05:32:22 -0700516
517void RtpStreamReceiver::UpdateHistograms() {
518 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
519 if (counter.num_packets > 0) {
520 RTC_LOGGED_HISTOGRAM_PERCENTAGE(
521 "WebRTC.Video.ReceivedFecPacketsInPercent",
522 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
523 }
524 if (counter.num_fec_packets > 0) {
525 RTC_LOGGED_HISTOGRAM_PERCENTAGE(
526 "WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
527 static_cast<int>(counter.num_recovered_packets * 100 /
528 counter.num_fec_packets));
529 }
530}
531
532void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
533 const std::string& extension, int id) {
534 // One-byte-extension local identifiers are in the range 1-14 inclusive.
535 RTC_DCHECK_GE(id, 1);
536 RTC_DCHECK_LE(id, 14);
537 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
538 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
539 StringToRtpExtensionType(extension), id));
540}
541
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000542} // namespace webrtc