blob: b826ed5b02a0c4d100da993de0fba2ab05b6bc43 [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
mflodman4cd27902016-08-05 06:28:45 -070015#include "webrtc/base/checks.h"
Peter Boström415d2cd2015-10-26 11:35:17 +010016#include "webrtc/base/logging.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070017#include "webrtc/common_types.h"
Peter Boström9c017252016-02-26 16:26:20 +010018#include "webrtc/config.h"
mflodmanc0e58a32016-04-25 01:26:26 -070019#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000020#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021#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"
brandtrd55c3f62016-10-31 04:51:33 -070026#include "webrtc/modules/rtp_rtcp/include/ulpfec_receiver.h"
Peter Boström0b250722016-04-22 18:23:15 +020027#include "webrtc/modules/video_coding/video_coding_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010029#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,
Erik Språng737336d2016-07-29 12:59:36 +020043 TransportSequenceNumberAllocator* transport_sequence_number_allocator,
44 RateLimiter* retransmission_rate_limiter) {
mflodmanc0e58a32016-04-25 01:26:26 -070045 RtpRtcp::Configuration configuration;
46 configuration.audio = false;
47 configuration.receiver_only = true;
48 configuration.receive_statistics = receive_statistics;
49 configuration.outgoing_transport = outgoing_transport;
50 configuration.intra_frame_callback = nullptr;
51 configuration.rtt_stats = rtt_stats;
52 configuration.rtcp_packet_type_counter_observer =
53 rtcp_packet_type_counter_observer;
54 configuration.paced_sender = paced_sender;
55 configuration.transport_sequence_number_allocator =
56 transport_sequence_number_allocator;
57 configuration.send_bitrate_observer = nullptr;
58 configuration.send_frame_count_observer = nullptr;
59 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070060 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070061 configuration.bandwidth_callback = nullptr;
62 configuration.transport_feedback_callback = nullptr;
Erik Språng737336d2016-07-29 12:59:36 +020063 configuration.retransmission_rate_limiter = retransmission_rate_limiter;
mflodmanc0e58a32016-04-25 01:26:26 -070064
65 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
66 rtp_rtcp->SetSendingStatus(false);
67 rtp_rtcp->SetSendingMediaStatus(false);
68 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
69
70 return rtp_rtcp;
71}
72
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000073static const int kPacketLogIntervalMs = 10000;
74
mflodmanfa666592016-04-28 23:15:33 -070075RtpStreamReceiver::RtpStreamReceiver(
76 vcm::VideoReceiver* video_receiver,
77 RemoteBitrateEstimator* remote_bitrate_estimator,
78 Transport* transport,
79 RtcpRttStats* rtt_stats,
80 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070081 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070082 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020083 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070084 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020085 ProcessThread* process_thread,
86 RateLimiter* retransmission_rate_limiter)
Tommi97888bd2016-01-21 23:24:59 +010087 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020088 config_(*config),
Peter Boström0b250722016-04-22 18:23:15 +020089 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000090 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -070091 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -070092 remb_(remb),
93 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +010094 ntp_estimator_(clock_),
95 rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)),
96 rtp_header_parser_(RtpHeaderParser::Create()),
97 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
98 this,
mflodmanfa666592016-04-28 23:15:33 -070099 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100100 &rtp_payload_registry_)),
101 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
brandtrd55c3f62016-10-31 04:51:33 -0700102 ulpfec_receiver_(UlpfecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000103 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000104 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700105 last_packet_log_ms_(-1),
106 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
107 transport,
108 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700109 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700110 remote_bitrate_estimator_,
111 paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +0200112 packet_router,
113 retransmission_rate_limiter)) {
mflodmanc0e58a32016-04-25 01:26:26 -0700114 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700115 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
116 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
117
Tommi733b5472016-06-10 17:58:01 +0200118 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700119 << "A stream should not be configured with RTCP disabled. This value is "
120 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700121 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
122 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
123 RTC_DCHECK(config_.rtp.local_ssrc != 0);
124 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
125
Tommi733b5472016-06-10 17:58:01 +0200126 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
127 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700128 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200129 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700130 rtp_rtcp_->SetREMBStatus(true);
131 remb_->AddReceiveChannel(rtp_rtcp_.get());
132 }
133
Tommi733b5472016-06-10 17:58:01 +0200134 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
135 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
136 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700137 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700138
139 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200140 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
141 ? kMaxPacketAgeToNack
142 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700143 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700144
145 // TODO(pbos): Support multiple RTX, per video payload.
146 for (const auto& kv : config_.rtp.rtx) {
147 RTC_DCHECK(kv.second.ssrc != 0);
148 RTC_DCHECK(kv.second.payload_type != 0);
149
150 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
151 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
152 kv.first);
153 }
154
155 // If set to true, the RTX payload type mapping supplied in
156 // |SetRtxPayloadType| will be used when restoring RTX packets. Without it,
157 // RTX packets will always be restored to the last non-RTX packet payload type
158 // received.
159 // TODO(holmer): When Chrome no longer depends on this being false by default,
160 // always use the mapping and remove this whole codepath.
161 rtp_payload_registry_.set_use_rtx_payload_mapping_on_restore(
162 config_.rtp.use_rtx_payload_mapping_on_restore);
163
164 if (IsFecEnabled()) {
165 VideoCodec ulpfec_codec = {};
166 ulpfec_codec.codecType = kVideoCodecULPFEC;
167 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700168 ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type;
mflodmandc7d0d22016-05-06 05:32:22 -0700169 RTC_CHECK(SetReceiveCodec(ulpfec_codec));
170
171 VideoCodec red_codec = {};
172 red_codec.codecType = kVideoCodecRED;
173 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700174 red_codec.plType = config_.rtp.ulpfec.red_payload_type;
mflodmandc7d0d22016-05-06 05:32:22 -0700175 RTC_CHECK(SetReceiveCodec(red_codec));
brandtrb5f2c3f2016-10-04 23:28:39 -0700176 if (config_.rtp.ulpfec.red_rtx_payload_type != -1) {
mflodmandc7d0d22016-05-06 05:32:22 -0700177 rtp_payload_registry_.SetRtxPayloadType(
brandtrb5f2c3f2016-10-04 23:28:39 -0700178 config_.rtp.ulpfec.red_rtx_payload_type,
179 config_.rtp.ulpfec.red_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700180 }
philipelae284082016-05-09 12:14:29 +0200181
brandtrb5f2c3f2016-10-04 23:28:39 -0700182 rtp_rtcp_->SetGenericFECStatus(true, config_.rtp.ulpfec.red_payload_type,
183 config_.rtp.ulpfec.ulpfec_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700184 }
185
Tommi733b5472016-06-10 17:58:01 +0200186 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700187 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
188
189 // Stats callback for CNAME changes.
190 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
191
mflodmandc7d0d22016-05-06 05:32:22 -0700192 process_thread_->RegisterModule(rtp_rtcp_.get());
mflodmanc0e58a32016-04-25 01:26:26 -0700193}
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
mflodmanfa666592016-04-28 23:15:33 -0700195RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700196 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
mflodmandc7d0d22016-05-06 05:32:22 -0700198 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
199 rtp_rtcp_->SetREMBStatus(false);
200 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
201 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000202}
203
mflodmanfa666592016-04-28 23:15:33 -0700204bool RtpStreamReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000205 int8_t old_pltype = -1;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100206 if (rtp_payload_registry_.ReceivePayloadType(
207 video_codec.plName, kVideoPayloadTypeFrequency, 0,
208 video_codec.maxBitrate, &old_pltype) != -1) {
209 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000210 }
211
Peter Boström4fa7eca2016-03-02 15:05:53 +0100212 return rtp_receiver_->RegisterReceivePayload(
213 video_codec.plName, video_codec.plType, kVideoPayloadTypeFrequency,
214 0, 0) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000215}
216
mflodmanfa666592016-04-28 23:15:33 -0700217uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000218 return rtp_receiver_->SSRC();
219}
220
mflodmanfa666592016-04-28 23:15:33 -0700221int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000222 return rtp_receiver_->CSRCs(csrcs);
223}
224
mflodmanfa666592016-04-28 23:15:33 -0700225RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000226 return rtp_receiver_.get();
227}
228
mflodmanfa666592016-04-28 23:15:33 -0700229int32_t RtpStreamReceiver::OnReceivedPayloadData(
230 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200231 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700232 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200233 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000234 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000235 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100236 ntp_estimator_.Estimate(rtp_header->header.timestamp);
Peter Boström0b250722016-04-22 18:23:15 +0200237 if (video_receiver_->IncomingPacket(payload_data, payload_size,
238 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000239 // Check this...
240 return -1;
241 }
242 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
mflodmanfa666592016-04-28 23:15:33 -0700245bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
246 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247 RTPHeader header;
248 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249 return false;
250 }
251 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000252 bool in_order = IsPacketInOrder(header);
253 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000254}
255
mflodmanfa666592016-04-28 23:15:33 -0700256// TODO(pbos): Remove as soon as audio can handle a changing payload type
257// without this callback.
258int32_t RtpStreamReceiver::OnInitializeDecoder(
259 const int8_t payload_type,
260 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
261 const int frequency,
262 const size_t channels,
263 const uint32_t rate) {
264 RTC_NOTREACHED();
265 return 0;
266}
267
268void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
269 rtp_rtcp_->SetRemoteSSRC(ssrc);
270}
271
272bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
273 size_t rtp_packet_length,
274 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100275 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000276 {
Tommi97888bd2016-01-21 23:24:59 +0100277 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000278 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100279 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000280 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000281 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000282
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000283 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000284 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000285 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100286 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000287 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000288 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000289 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000290 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000291 if (packet_time.timestamp != -1)
292 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
293 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000294 arrival_time_ms = now_ms;
295
296 {
297 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100298 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000299 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
300 std::stringstream ss;
301 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
302 << static_cast<int>(header.payloadType) << ", timestamp: "
303 << header.timestamp << ", sequence number: " << header.sequenceNumber
304 << ", arrival time: " << arrival_time_ms;
305 if (header.extension.hasTransmissionTimeOffset)
306 ss << ", toffset: " << header.extension.transmissionTimeOffset;
307 if (header.extension.hasAbsoluteSendTime)
308 ss << ", abs send time: " << header.extension.absoluteSendTime;
309 LOG(LS_INFO) << ss.str();
310 last_packet_log_ms_ = now_ms;
311 }
312 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000313
Stefan Holmerff4ea932015-06-18 16:01:33 +0200314 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
pbos2169d8b2016-06-20 11:53:02 -0700315 header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000316 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000317
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000318 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100319 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100320 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000321 // Update receive statistics after ReceivePacket.
322 // Receive statistics will be reset if the payload type changes (make sure
323 // that the first packet is included in the stats).
324 rtp_receive_statistics_->IncomingPacket(
325 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
326 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327}
328
mflodmancfc8e3b2016-05-03 21:22:04 -0700329int32_t RtpStreamReceiver::RequestKeyFrame() {
330 return rtp_rtcp_->RequestKeyFrame();
331}
332
333int32_t RtpStreamReceiver::SliceLossIndicationRequest(
334 const uint64_t picture_id) {
335 return rtp_rtcp_->SendRTCPSliceLossIndication(
336 static_cast<uint8_t>(picture_id));
337}
338
mflodmandc7d0d22016-05-06 05:32:22 -0700339bool RtpStreamReceiver::IsFecEnabled() const {
brandtrb5f2c3f2016-10-04 23:28:39 -0700340 return config_.rtp.ulpfec.red_payload_type != -1 &&
341 config_.rtp.ulpfec.ulpfec_payload_type != -1;
mflodmandc7d0d22016-05-06 05:32:22 -0700342}
343
344bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
345 return config_.rtp.nack.rtp_history_ms > 0;
346}
347
348void RtpStreamReceiver::RequestPacketRetransmit(
349 const std::vector<uint16_t>& sequence_numbers) {
350 rtp_rtcp_->SendNack(sequence_numbers);
351}
352
mflodmancfc8e3b2016-05-03 21:22:04 -0700353int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
354 uint16_t length) {
355 return rtp_rtcp_->SendNACK(sequence_numbers, length);
356}
357
mflodmanfa666592016-04-28 23:15:33 -0700358bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
359 size_t packet_length,
360 const RTPHeader& header,
361 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100362 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000363 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
364 }
365 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000366 assert(packet_length >= header.headerLength);
367 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000368 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100369 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
370 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000371 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000372 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000373 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
374 payload_specific, in_order);
375}
376
mflodmanfa666592016-04-28 23:15:33 -0700377bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
378 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100379 if (rtp_payload_registry_.IsRed(header)) {
380 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000381 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000382 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200383 // Notify video_receiver about received FEC packets to avoid NACKing these
384 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000385 NotifyReceiverOfFecPacket(header);
386 }
brandtrd55c3f62016-10-31 04:51:33 -0700387 if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length,
388 ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000389 return false;
390 }
brandtrd55c3f62016-10-31 04:51:33 -0700391 return ulpfec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100392 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000393 if (header.headerLength + header.paddingLength == packet_length) {
394 // This is an empty packet and should be silently dropped before trying to
395 // parse the RTX header.
396 return true;
397 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000398 // Remove the RTX header and parse the original RTP header.
399 if (packet_length < header.headerLength)
400 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000401 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000402 return false;
Tommi97888bd2016-01-21 23:24:59 +0100403 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000404 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000405 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000406 return false;
407 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100408 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700409 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
410 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100411 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
412 << header.ssrc << " payload type: "
413 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000414 return false;
415 }
416 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700417 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000418 restored_packet_in_use_ = false;
419 return ret;
420 }
421 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422}
423
mflodmanfa666592016-04-28 23:15:33 -0700424void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000425 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100426 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000427 if (last_media_payload_type < 0) {
428 LOG(LS_WARNING) << "Failed to get last media payload type.";
429 return;
430 }
431 // Fake an empty media packet.
432 WebRtcRTPHeader rtp_header = {};
433 rtp_header.header = header;
434 rtp_header.header.payloadType = last_media_payload_type;
435 rtp_header.header.paddingLength = 0;
436 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100437 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
438 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000439 LOG(LS_WARNING) << "Failed to get payload specifics.";
440 return;
441 }
442 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000443 rtp_header.type.Video.rotation = kVideoRotation_0;
444 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700445 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000446 }
isheriff6b4b5f32016-06-08 00:24:21 -0700447 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
448
Peter Boström74f6e9e2016-04-04 17:56:10 +0200449 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000450}
451
mflodmanfa666592016-04-28 23:15:33 -0700452bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
453 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000454 {
Tommi97888bd2016-01-21 23:24:59 +0100455 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000456 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100457 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000458 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100459 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
Per83d09102016-04-15 14:59:13 +0200461 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000462
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000463 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200464 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000465 if (rtt == 0) {
466 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100467 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000468 }
469 uint32_t ntp_secs = 0;
470 uint32_t ntp_frac = 0;
471 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200472 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
473 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000474 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100475 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000476 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100477 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000478
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100479 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000480}
481
mflodmandc7d0d22016-05-06 05:32:22 -0700482void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
483 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
484 : RtcpMode::kOff);
485}
486
mflodmanfa666592016-04-28 23:15:33 -0700487void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100488 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000489 receiving_ = true;
490}
491
mflodmanfa666592016-04-28 23:15:33 -0700492void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100493 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000494 receiving_ = false;
495}
496
mflodmanfa666592016-04-28 23:15:33 -0700497bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000498 StreamStatistician* statistician =
499 rtp_receive_statistics_->GetStatistician(header.ssrc);
500 if (!statistician)
501 return false;
502 return statistician->IsPacketInOrder(header.sequenceNumber);
503}
504
mflodmanfa666592016-04-28 23:15:33 -0700505bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
506 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000507 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100508 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000509 return false;
510 StreamStatistician* statistician =
511 rtp_receive_statistics_->GetStatistician(header.ssrc);
512 if (!statistician)
513 return false;
514 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000515 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200516 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000517 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000518 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000519}
mflodmandc7d0d22016-05-06 05:32:22 -0700520
521void RtpStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700522 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
mflodmandc7d0d22016-05-06 05:32:22 -0700523 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700524 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700525 "WebRTC.Video.ReceivedFecPacketsInPercent",
526 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
527 }
528 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700529 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
530 static_cast<int>(counter.num_recovered_packets *
531 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700532 }
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