blob: a47055abf08465a2ed0e03886c2770e636a62af0 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:231/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
nisse14ec7e02017-06-09 11:01:5511#include "webrtc/video/rtp_video_stream_receiver.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:2312
eladalon2c1cbc12017-08-02 14:39:0713#include <algorithm>
philipelba3a19d2016-11-15 08:57:5714#include <utility>
ilnik39e693a2017-06-19 14:18:5515#include <vector>
mflodman@webrtc.orgcd1ac8b2013-02-06 17:46:3916
Stefan Holmer36189cd2017-09-01 13:29:2817#include "webrtc/call/video_config.h"
mflodman639986e2016-05-04 04:22:0418#include "webrtc/common_types.h"
philipel09c99cf2016-12-20 12:15:5919#include "webrtc/media/base/mediaconstants.h"
mflodman09003172016-04-25 08:26:2620#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.org281cff82013-05-17 13:44:4821#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellander36a14b52015-11-04 07:31:5222#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Henrik Kjellander36a14b52015-11-04 07:31:5223#include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
24#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
Henrik Kjellander36a14b52015-11-04 07:31:5225#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
26#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
brandtr001769c2016-10-31 11:51:3327#include "webrtc/modules/rtp_rtcp/include/ulpfec_receiver.h"
nisse6d5d8142017-02-13 13:59:4628#include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
29#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
philipelba3a19d2016-11-15 08:57:5730#include "webrtc/modules/video_coding/frame_object.h"
philipel09c99cf2016-12-20 12:15:5931#include "webrtc/modules/video_coding/h264_sprop_parameter_sets.h"
philipelba3a19d2016-11-15 08:57:5732#include "webrtc/modules/video_coding/h264_sps_pps_tracker.h"
33#include "webrtc/modules/video_coding/packet_buffer.h"
Peter Boström64cd1782016-04-22 16:23:1534#include "webrtc/modules/video_coding/video_coding_impl.h"
Edward Lemur76de83e2017-07-06 17:44:3435#include "webrtc/rtc_base/checks.h"
36#include "webrtc/rtc_base/location.h"
37#include "webrtc/rtc_base/logging.h"
philipelba3a19d2016-11-15 08:57:5738#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander78f65d02015-10-28 17:17:4039#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander78f65d02015-10-28 17:17:4040#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
mflodman639986e2016-05-04 04:22:0441#include "webrtc/video/receive_statistics_proxy.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:2342
43namespace webrtc {
44
philipelba3a19d2016-11-15 08:57:5745namespace {
philipeld8a48fb2017-08-10 16:10:5946// TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see:
47// crbug.com/752886
48constexpr int kPacketBufferStartSize = 512;
philipelba3a19d2016-11-15 08:57:5749constexpr int kPacketBufferMaxSixe = 2048;
50}
51
mflodman09003172016-04-25 08:26:2652std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
53 ReceiveStatistics* receive_statistics,
54 Transport* outgoing_transport,
55 RtcpRttStats* rtt_stats,
56 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
nissedbce2972017-01-24 10:36:5857 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
mflodman09003172016-04-25 08:26:2658 RtpRtcp::Configuration configuration;
59 configuration.audio = false;
60 configuration.receiver_only = true;
61 configuration.receive_statistics = receive_statistics;
62 configuration.outgoing_transport = outgoing_transport;
63 configuration.intra_frame_callback = nullptr;
64 configuration.rtt_stats = rtt_stats;
65 configuration.rtcp_packet_type_counter_observer =
66 rtcp_packet_type_counter_observer;
mflodman09003172016-04-25 08:26:2667 configuration.transport_sequence_number_allocator =
68 transport_sequence_number_allocator;
69 configuration.send_bitrate_observer = nullptr;
70 configuration.send_frame_count_observer = nullptr;
71 configuration.send_side_delay_observer = nullptr;
asapersson86a285e2016-05-03 06:44:0172 configuration.send_packet_observer = nullptr;
mflodman09003172016-04-25 08:26:2673 configuration.bandwidth_callback = nullptr;
74 configuration.transport_feedback_callback = nullptr;
75
76 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
mflodman09003172016-04-25 08:26:2677 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
78
79 return rtp_rtcp;
80}
81
stefan@webrtc.org88085a12014-10-14 11:40:1382static const int kPacketLogIntervalMs = 10000;
83
nisse14ec7e02017-06-09 11:01:5584RtpVideoStreamReceiver::RtpVideoStreamReceiver(
mflodmancdadcb12016-04-29 06:15:3385 Transport* transport,
86 RtcpRttStats* rtt_stats,
mflodman639986e2016-05-04 04:22:0487 PacketRouter* packet_router,
Tommi87875ef2016-06-10 15:58:0188 const VideoReceiveStream::Config* config,
mflodman33927c72016-05-06 12:32:2289 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng00779972016-07-29 10:59:3690 ProcessThread* process_thread,
philipelba3a19d2016-11-15 08:57:5791 NackSender* nack_sender,
92 KeyFrameRequestSender* keyframe_request_sender,
93 video_coding::OnCompleteFrameCallback* complete_frame_callback,
94 VCMTiming* timing)
Tommif93f04c2016-01-21 22:24:5995 : clock_(Clock::GetRealTimeClock()),
Tommi87875ef2016-06-10 15:58:0196 config_(*config),
mflodman09003172016-04-25 08:26:2697 packet_router_(packet_router),
mflodman33927c72016-05-06 12:32:2298 process_thread_(process_thread),
Peter Boströme5d4a262016-03-02 14:05:5399 ntp_estimator_(clock_),
Peter Boströme5d4a262016-03-02 14:05:53100 rtp_header_parser_(RtpHeaderParser::Create()),
101 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
102 this,
mflodmancdadcb12016-04-29 06:15:33103 this,
Peter Boströme5d4a262016-03-02 14:05:53104 &rtp_payload_registry_)),
105 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
brandtr0e9e5a22017-06-29 09:45:35106 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
stefan@webrtc.orgdb74c612013-09-06 13:40:11107 receiving_(false),
nissedfa8f5e2017-09-04 11:22:15108 restored_packet_in_use_(false),
mflodman09003172016-04-25 08:26:26109 last_packet_log_ms_(-1),
110 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
111 transport,
112 rtt_stats,
mflodman639986e2016-05-04 04:22:04113 receive_stats_proxy,
nissedbce2972017-01-24 10:36:58114 packet_router)),
philipelba3a19d2016-11-15 08:57:57115 complete_frame_callback_(complete_frame_callback),
116 keyframe_request_sender_(keyframe_request_sender),
philipel22e01822017-05-16 15:06:30117 timing_(timing),
118 has_received_frame_(false) {
eladalona207a3a2017-08-01 13:30:28119 constexpr bool remb_candidate = true;
120 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodman639986e2016-05-04 04:22:04121 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
122 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
123
Tommi87875ef2016-06-10 15:58:01124 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodman639986e2016-05-04 04:22:04125 << "A stream should not be configured with RTCP disabled. This value is "
126 "reserved for internal usage.";
mflodman33927c72016-05-06 12:32:22127 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
128 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
129 RTC_DCHECK(config_.rtp.local_ssrc != 0);
130 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
131
Tommi87875ef2016-06-10 15:58:01132 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
133 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
stefan9fe41912017-06-09 13:12:11134 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodman09003172016-04-25 08:26:26135 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
mflodman33927c72016-05-06 12:32:22136
Tommi87875ef2016-06-10 15:58:01137 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
138 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
139 config_.rtp.extensions[i].id);
mflodman33927c72016-05-06 12:32:22140 }
mflodman639986e2016-05-04 04:22:04141
142 static const int kMaxPacketAgeToNack = 450;
Tommi87875ef2016-06-10 15:58:01143 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
144 ? kMaxPacketAgeToNack
145 : kDefaultMaxReorderingThreshold;
mflodman639986e2016-05-04 04:22:04146 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodman33927c72016-05-06 12:32:22147
brandtrbcf3fd32017-01-27 12:53:07148 if (config_.rtp.rtx_ssrc) {
149 rtp_payload_registry_.SetRtxSsrc(config_.rtp.rtx_ssrc);
nissedfa8f5e2017-09-04 11:22:15150
151 for (const auto& kv : config_.rtp.rtx_associated_payload_types) {
152 RTC_DCHECK_NE(kv.first, 0);
153 rtp_payload_registry_.SetRtxPayloadType(kv.first, kv.second);
154 }
mflodman33927c72016-05-06 12:32:22155 }
156
brandtrcc248462016-12-08 16:25:47157 if (IsUlpfecEnabled()) {
mflodman33927c72016-05-06 12:32:22158 VideoCodec ulpfec_codec = {};
159 ulpfec_codec.codecType = kVideoCodecULPFEC;
160 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
brandtrd984c572016-10-05 06:28:39161 ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type;
johanf3b4b212016-12-13 10:23:03162 RTC_CHECK(AddReceiveCodec(ulpfec_codec));
brandtrc1d93462016-11-11 11:28:30163 }
mflodman33927c72016-05-06 12:32:22164
brandtrc1d93462016-11-11 11:28:30165 if (IsRedEnabled()) {
mflodman33927c72016-05-06 12:32:22166 VideoCodec red_codec = {};
167 red_codec.codecType = kVideoCodecRED;
168 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
brandtrd984c572016-10-05 06:28:39169 red_codec.plType = config_.rtp.ulpfec.red_payload_type;
johanf3b4b212016-12-13 10:23:03170 RTC_CHECK(AddReceiveCodec(red_codec));
nissedfa8f5e2017-09-04 11:22:15171 if (config_.rtp.ulpfec.red_rtx_payload_type != -1) {
172 rtp_payload_registry_.SetRtxPayloadType(
173 config_.rtp.ulpfec.red_rtx_payload_type,
174 config_.rtp.ulpfec.red_payload_type);
175 }
mflodman33927c72016-05-06 12:32:22176 }
177
Tommi87875ef2016-06-10 15:58:01178 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodman33927c72016-05-06 12:32:22179 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
180
181 // Stats callback for CNAME changes.
182 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
183
tommic72b1c82017-03-03 11:20:24184 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelba3a19d2016-11-15 08:57:57185
tommi451d9c22017-02-27 09:59:36186 if (config_.rtp.nack.rtp_history_ms != 0) {
187 nack_module_.reset(
188 new NackModule(clock_, nack_sender, keyframe_request_sender));
tommic72b1c82017-03-03 11:20:24189 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommi451d9c22017-02-27 09:59:36190 }
philipelba3a19d2016-11-15 08:57:57191
philipela7cca312017-02-22 13:30:39192 packet_buffer_ = video_coding::PacketBuffer::Create(
193 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
194 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
mflodman09003172016-04-25 08:26:26195}
andrew@webrtc.orgb015cbe2012-10-22 18:19:23196
nisse14ec7e02017-06-09 11:01:55197RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalon2c1cbc12017-08-02 14:39:07198 RTC_DCHECK(secondary_sinks_.empty());
199
tommi451d9c22017-02-27 09:59:36200 if (nack_module_) {
201 process_thread_->DeRegisterModule(nack_module_.get());
202 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23203
tommi451d9c22017-02-27 09:59:36204 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelba3a19d2016-11-15 08:57:57205
nisse5e9233b2017-03-31 12:44:52206 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodman33927c72016-05-06 12:32:22207 UpdateHistograms();
asapersson@webrtc.org4935b802015-01-15 07:40:20208}
209
nisse14ec7e02017-06-09 11:01:55210bool RtpVideoStreamReceiver::AddReceiveCodec(
philipel09c99cf2016-12-20 12:15:59211 const VideoCodec& video_codec,
212 const std::map<std::string, std::string>& codec_params) {
213 pt_codec_params_.insert(make_pair(video_codec.plType, codec_params));
214 return AddReceiveCodec(video_codec);
215}
216
nisse14ec7e02017-06-09 11:01:55217bool RtpVideoStreamReceiver::AddReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org7fc75bb2013-08-15 23:38:54218 int8_t old_pltype = -1;
magjed4c1eb052016-11-24 17:34:46219 if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) !=
220 -1) {
Peter Boströme5d4a262016-03-02 14:05:53221 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org7fc75bb2013-08-15 23:38:54222 }
magjed4c1eb052016-11-24 17:34:46223 return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
wu@webrtc.org7fc75bb2013-08-15 23:38:54224}
225
nisse14ec7e02017-06-09 11:01:55226uint32_t RtpVideoStreamReceiver::GetRemoteSsrc() const {
stefan9fe41912017-06-09 13:12:11227 return config_.rtp.remote_ssrc;
wu@webrtc.org7fc75bb2013-08-15 23:38:54228}
229
nisse14ec7e02017-06-09 11:01:55230int RtpVideoStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org7fc75bb2013-08-15 23:38:54231 return rtp_receiver_->CSRCs(csrcs);
232}
233
nisse14ec7e02017-06-09 11:01:55234RtpReceiver* RtpVideoStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org7fc75bb2013-08-15 23:38:54235 return rtp_receiver_.get();
236}
237
nisse14ec7e02017-06-09 11:01:55238int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
mflodmancdadcb12016-04-29 06:15:33239 const uint8_t* payload_data,
Peter Boströmc9da01c2016-06-14 10:52:54240 size_t payload_size,
mflodmancdadcb12016-04-29 06:15:33241 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.org093fc0b2014-04-24 22:10:24242 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org2fae0d12014-05-14 16:53:51243 rtp_header_with_ntp.ntp_time_ms =
Peter Boströme5d4a262016-03-02 14:05:53244 ntp_estimator_.Estimate(rtp_header->header.timestamp);
philipela7cca312017-02-22 13:30:39245 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
tommi451d9c22017-02-27 09:59:36246 packet.timesNacked =
247 nack_module_ ? nack_module_->OnReceivedPacket(packet) : -1;
ilnik39e693a2017-06-19 14:18:55248 packet.receive_time_ms = clock_->TimeInMilliseconds();
philipelba3a19d2016-11-15 08:57:57249
philipel59601fc2017-03-21 12:45:18250 // In the case of a video stream without picture ids and no rtx the
251 // RtpFrameReferenceFinder will need to know about padding to
252 // correctly calculate frame references.
253 if (packet.sizeBytes == 0) {
254 reference_finder_->PaddingReceived(packet.seqNum);
philipele65f1372017-06-13 09:47:28255 packet_buffer_->PaddingReceived(packet.seqNum);
philipel59601fc2017-03-21 12:45:18256 return 0;
257 }
258
philipela7cca312017-02-22 13:30:39259 if (packet.codec == kVideoCodecH264) {
260 // Only when we start to receive packets will we know what payload type
261 // that will be used. When we know the payload type insert the correct
262 // sps/pps into the tracker.
263 if (packet.payloadType != last_payload_type_) {
264 last_payload_type_ = packet.payloadType;
265 InsertSpsPpsIntoTracker(packet.payloadType);
philipelba3a19d2016-11-15 08:57:57266 }
267
philipela7cca312017-02-22 13:30:39268 switch (tracker_.CopyAndFixBitstream(&packet)) {
269 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
270 keyframe_request_sender_->RequestKeyFrame();
271 FALLTHROUGH();
272 case video_coding::H264SpsPpsTracker::kDrop:
273 return 0;
274 case video_coding::H264SpsPpsTracker::kInsert:
275 break;
276 }
277
philipelba3a19d2016-11-15 08:57:57278 } else {
philipela7cca312017-02-22 13:30:39279 uint8_t* data = new uint8_t[packet.sizeBytes];
280 memcpy(data, packet.dataPtr, packet.sizeBytes);
281 packet.dataPtr = data;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23282 }
philipela7cca312017-02-22 13:30:39283
284 packet_buffer_->InsertPacket(&packet);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23285 return 0;
286}
287
nisseba6f4782017-05-11 15:00:58288// TODO(nisse): Try to delete this method. Obstacles: It is used by
nisse032d66e2017-05-29 15:16:37289// ParseAndHandleEncapsulatingHeader, for handling Rtx packets, and
290// for callbacks from |ulpfec_receiver_|.
nisse14ec7e02017-06-09 11:01:55291void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
292 size_t rtp_packet_length) {
wu@webrtc.org7fc75bb2013-08-15 23:38:54293 RTPHeader header;
294 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
nisse032d66e2017-05-29 15:16:37295 return;
wu@webrtc.org7fc75bb2013-08-15 23:38:54296 }
297 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.orgd01c4912014-09-04 06:48:14298 bool in_order = IsPacketInOrder(header);
nisse032d66e2017-05-29 15:16:37299 ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org7fc75bb2013-08-15 23:38:54300}
301
mflodmancdadcb12016-04-29 06:15:33302// TODO(pbos): Remove as soon as audio can handle a changing payload type
303// without this callback.
nisse14ec7e02017-06-09 11:01:55304int32_t RtpVideoStreamReceiver::OnInitializeDecoder(
mflodmancdadcb12016-04-29 06:15:33305 const int8_t payload_type,
306 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
307 const int frequency,
308 const size_t channels,
309 const uint32_t rate) {
310 RTC_NOTREACHED();
311 return 0;
312}
313
nisseba6f4782017-05-11 15:00:58314// This method handles both regular RTP packets and packets recovered
315// via FlexFEC.
nisse14ec7e02017-06-09 11:01:55316void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
eladalon7f622012017-08-25 07:49:08317 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalon2c1cbc12017-08-02 14:39:07318
eladalon7f622012017-08-25 07:49:08319 if (!receiving_) {
320 return;
321 }
solenberg@webrtc.org800136d2014-02-11 15:27:49322
eladalon7f622012017-08-25 07:49:08323 if (!packet.recovered()) {
324 int64_t now_ms = clock_->TimeInMilliseconds();
stefan@webrtc.org88085a12014-10-14 11:40:13325
eladalon7f622012017-08-25 07:49:08326 // Periodically log the RTP header of incoming packets.
327 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
328 std::stringstream ss;
329 ss << "Packet received on SSRC: " << packet.Ssrc()
330 << " with payload type: " << static_cast<int>(packet.PayloadType())
331 << ", timestamp: " << packet.Timestamp()
332 << ", sequence number: " << packet.SequenceNumber()
333 << ", arrival time: " << packet.arrival_time_ms();
334 int32_t time_offset;
335 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
336 ss << ", toffset: " << time_offset;
nisse6d5d8142017-02-13 13:59:46337 }
eladalon7f622012017-08-25 07:49:08338 uint32_t send_time;
339 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
340 ss << ", abs send time: " << send_time;
341 }
342 LOG(LS_INFO) << ss.str();
343 last_packet_log_ms_ = now_ms;
stefan@webrtc.org88085a12014-10-14 11:40:13344 }
345 }
wu@webrtc.orgefeb8ce2013-12-13 00:21:03346
nisse6d5d8142017-02-13 13:59:46347 // TODO(nisse): Delete use of GetHeader, but needs refactoring of
348 // ReceivePacket and IncomingPacket methods below.
349 RTPHeader header;
350 packet.GetHeader(&header);
351
wu@webrtc.org7fc75bb2013-08-15 23:38:54352 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.orgdb74c612013-09-06 13:40:11353
stefan@webrtc.org7e97e4c2013-11-08 15:18:52354 bool in_order = IsPacketInOrder(header);
nisseba6f4782017-05-11 15:00:58355 if (!packet.recovered()) {
356 // TODO(nisse): Why isn't this done for recovered packets?
357 rtp_payload_registry_.SetIncomingPayloadType(header);
358 }
nisse6d5d8142017-02-13 13:59:46359 ReceivePacket(packet.data(), packet.size(), header, in_order);
asapersson@webrtc.org99153ba2014-05-26 13:06:04360 // Update receive statistics after ReceivePacket.
361 // Receive statistics will be reset if the payload type changes (make sure
362 // that the first packet is included in the stats).
nisseba6f4782017-05-11 15:00:58363 if (!packet.recovered()) {
364 // TODO(nisse): We should pass a recovered flag to stats, to aid
365 // fixing bug bugs.webrtc.org/6339.
366 rtp_receive_statistics_->IncomingPacket(
367 header, packet.size(), IsPacketRetransmitted(header, in_order));
368 }
eladalon2c1cbc12017-08-02 14:39:07369
370 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
371 secondary_sink->OnRtpPacket(packet);
372 }
stefan@webrtc.orgdb74c612013-09-06 13:40:11373}
374
nisse14ec7e02017-06-09 11:01:55375int32_t RtpVideoStreamReceiver::RequestKeyFrame() {
mflodman639986e2016-05-04 04:22:04376 return rtp_rtcp_->RequestKeyFrame();
377}
378
nisse14ec7e02017-06-09 11:01:55379bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
brandtrc1d93462016-11-11 11:28:30380 return config_.rtp.ulpfec.ulpfec_payload_type != -1;
381}
382
nisse14ec7e02017-06-09 11:01:55383bool RtpVideoStreamReceiver::IsRedEnabled() const {
brandtrc1d93462016-11-11 11:28:30384 return config_.rtp.ulpfec.red_payload_type != -1;
mflodman33927c72016-05-06 12:32:22385}
386
nisse14ec7e02017-06-09 11:01:55387bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodman33927c72016-05-06 12:32:22388 return config_.rtp.nack.rtp_history_ms > 0;
389}
390
nisse14ec7e02017-06-09 11:01:55391void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodman33927c72016-05-06 12:32:22392 const std::vector<uint16_t>& sequence_numbers) {
393 rtp_rtcp_->SendNack(sequence_numbers);
394}
395
nisse14ec7e02017-06-09 11:01:55396int32_t RtpVideoStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
397 uint16_t length) {
mflodman639986e2016-05-04 04:22:04398 return rtp_rtcp_->SendNACK(sequence_numbers, length);
399}
400
nisse14ec7e02017-06-09 11:01:55401void RtpVideoStreamReceiver::OnReceivedFrame(
philipelba3a19d2016-11-15 08:57:57402 std::unique_ptr<video_coding::RtpFrameObject> frame) {
philipel22e01822017-05-16 15:06:30403 if (!has_received_frame_) {
404 has_received_frame_ = true;
405 if (frame->FrameType() != kVideoFrameKey)
406 keyframe_request_sender_->RequestKeyFrame();
407 }
408
philipel685e50e2017-01-29 12:04:47409 if (!frame->delayed_by_retransmission())
410 timing_->IncomingTimestamp(frame->timestamp, clock_->TimeInMilliseconds());
philipelba3a19d2016-11-15 08:57:57411 reference_finder_->ManageFrame(std::move(frame));
412}
413
nisse14ec7e02017-06-09 11:01:55414void RtpVideoStreamReceiver::OnCompleteFrame(
philipelba3a19d2016-11-15 08:57:57415 std::unique_ptr<video_coding::FrameObject> frame) {
416 {
417 rtc::CritScope lock(&last_seq_num_cs_);
418 video_coding::RtpFrameObject* rtp_frame =
419 static_cast<video_coding::RtpFrameObject*>(frame.get());
420 last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num();
421 }
422 complete_frame_callback_->OnCompleteFrame(std::move(frame));
423}
424
nisse14ec7e02017-06-09 11:01:55425void RtpVideoStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms,
426 int64_t max_rtt_ms) {
tommi451d9c22017-02-27 09:59:36427 if (nack_module_)
428 nack_module_->UpdateRtt(max_rtt_ms);
philipelba3a19d2016-11-15 08:57:57429}
430
nisse14ec7e02017-06-09 11:01:55431rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
philipel47be53a2017-05-18 15:08:53432 return packet_buffer_->LastReceivedPacketMs();
433}
434
nisse14ec7e02017-06-09 11:01:55435rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
436 const {
philipel47be53a2017-05-18 15:08:53437 return packet_buffer_->LastReceivedKeyframePacketMs();
438}
439
eladalon2c1cbc12017-08-02 14:39:07440void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
eladalon7f622012017-08-25 07:49:08441 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalon2c1cbc12017-08-02 14:39:07442 RTC_DCHECK(std::find(secondary_sinks_.cbegin(), secondary_sinks_.cend(),
443 sink) == secondary_sinks_.cend());
444 secondary_sinks_.push_back(sink);
445}
446
447void RtpVideoStreamReceiver::RemoveSecondarySink(
448 const RtpPacketSinkInterface* sink) {
eladalon7f622012017-08-25 07:49:08449 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalon2c1cbc12017-08-02 14:39:07450 auto it = std::find(secondary_sinks_.begin(), secondary_sinks_.end(), sink);
451 if (it == secondary_sinks_.end()) {
452 // We might be rolling-back a call whose setup failed mid-way. In such a
453 // case, it's simpler to remove "everything" rather than remember what
454 // has already been added.
455 LOG(LS_WARNING) << "Removal of unknown sink.";
456 return;
457 }
458 secondary_sinks_.erase(it);
459}
460
nisse14ec7e02017-06-09 11:01:55461void RtpVideoStreamReceiver::ReceivePacket(const uint8_t* packet,
462 size_t packet_length,
463 const RTPHeader& header,
464 bool in_order) {
Peter Boströme5d4a262016-03-02 14:05:53465 if (rtp_payload_registry_.IsEncapsulated(header)) {
nisse032d66e2017-05-29 15:16:37466 ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
467 return;
stefan@webrtc.orgdb74c612013-09-06 13:40:11468 }
469 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org0ab923a2014-11-20 22:28:14470 assert(packet_length >= header.headerLength);
471 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org7fc75bb2013-08-15 23:38:54472 PayloadUnion payload_specific;
Peter Boströme5d4a262016-03-02 14:05:53473 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
474 &payload_specific)) {
nisse032d66e2017-05-29 15:16:37475 return;
wu@webrtc.org7fc75bb2013-08-15 23:38:54476 }
nisse032d66e2017-05-29 15:16:37477 rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
478 payload_specific, in_order);
stefan@webrtc.orgdb74c612013-09-06 13:40:11479}
480
nisse14ec7e02017-06-09 11:01:55481void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
mflodmancdadcb12016-04-29 06:15:33482 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
eladalon7f622012017-08-25 07:49:08483 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
Peter Boströme5d4a262016-03-02 14:05:53484 if (rtp_payload_registry_.IsRed(header)) {
485 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org413575d2015-01-28 13:58:27486 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org08c57f72015-01-27 12:17:29487 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström64cd1782016-04-22 16:23:15488 // Notify video_receiver about received FEC packets to avoid NACKing these
489 // packets.
asapersson@webrtc.org413575d2015-01-28 13:58:27490 NotifyReceiverOfFecPacket(header);
491 }
brandtr001769c2016-10-31 11:51:33492 if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length,
493 ulpfec_pt) != 0) {
nisse032d66e2017-05-29 15:16:37494 return;
stefan@webrtc.orgdb74c612013-09-06 13:40:11495 }
nisse032d66e2017-05-29 15:16:37496 ulpfec_receiver_->ProcessReceivedFec();
Peter Boströme5d4a262016-03-02 14:05:53497 } else if (rtp_payload_registry_.IsRtx(header)) {
nissedfa8f5e2017-09-04 11:22:15498 if (header.headerLength + header.paddingLength == packet_length) {
499 // This is an empty packet and should be silently dropped before trying to
500 // parse the RTX header.
501 return;
502 }
503 // Remove the RTX header and parse the original RTP header.
504 if (packet_length < header.headerLength)
505 return;
506 if (packet_length > sizeof(restored_packet_))
507 return;
508 if (restored_packet_in_use_) {
509 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
510 return;
511 }
512 if (!rtp_payload_registry_.RestoreOriginalPacket(
513 restored_packet_, packet, &packet_length, config_.rtp.remote_ssrc,
514 header)) {
515 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
516 << header.ssrc << " payload type: "
517 << static_cast<int>(header.payloadType);
518 return;
519 }
520 restored_packet_in_use_ = true;
521 OnRecoveredPacket(restored_packet_, packet_length);
522 restored_packet_in_use_ = false;
stefan@webrtc.orgdb74c612013-09-06 13:40:11523 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23524}
525
nisse14ec7e02017-06-09 11:01:55526void RtpVideoStreamReceiver::NotifyReceiverOfFecPacket(
527 const RTPHeader& header) {
asapersson@webrtc.org413575d2015-01-28 13:58:27528 int8_t last_media_payload_type =
Peter Boströme5d4a262016-03-02 14:05:53529 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org413575d2015-01-28 13:58:27530 if (last_media_payload_type < 0) {
531 LOG(LS_WARNING) << "Failed to get last media payload type.";
532 return;
533 }
534 // Fake an empty media packet.
535 WebRtcRTPHeader rtp_header = {};
536 rtp_header.header = header;
537 rtp_header.header.payloadType = last_media_payload_type;
538 rtp_header.header.paddingLength = 0;
539 PayloadUnion payload_specific;
Peter Boströme5d4a262016-03-02 14:05:53540 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
541 &payload_specific)) {
asapersson@webrtc.org413575d2015-01-28 13:58:27542 LOG(LS_WARNING) << "Failed to get payload specifics.";
543 return;
544 }
545 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.org42e1d112015-03-12 20:50:57546 rtp_header.type.Video.rotation = kVideoRotation_0;
547 if (header.extension.hasVideoRotation) {
magjed78706f42016-09-08 10:24:58548 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.org42e1d112015-03-12 20:50:57549 }
ilnikcb436f62017-04-11 17:34:31550 rtp_header.type.Video.content_type = VideoContentType::UNSPECIFIED;
551 if (header.extension.hasVideoContentType) {
552 rtp_header.type.Video.content_type = header.extension.videoContentType;
553 }
ilnik39e693a2017-06-19 14:18:55554 rtp_header.type.Video.video_timing = {0u, 0u, 0u, 0u, 0u, 0u, false};
555 if (header.extension.has_video_timing) {
556 rtp_header.type.Video.video_timing = header.extension.video_timing;
ilnik39e693a2017-06-19 14:18:55557 }
isheriff00cc0452016-06-08 07:24:21558 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
559
Peter Boström81e36b52016-04-04 15:56:10560 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org413575d2015-01-28 13:58:27561}
562
nisse14ec7e02017-06-09 11:01:55563bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
564 size_t rtcp_packet_length) {
eladalon7f622012017-08-25 07:49:08565 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
566
567 if (!receiving_) {
568 return false;
Peter Boströme5d4a262016-03-02 14:05:53569 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23570
Percd997652016-04-15 12:59:13571 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.org093fc0b2014-04-24 22:10:24572
pkasting@chromium.orga3166c42015-01-12 21:51:21573 int64_t rtt = 0;
Percd997652016-04-15 12:59:13574 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org1e168412014-10-09 10:52:43575 if (rtt == 0) {
576 // Waiting for valid rtt.
Peter Boström70ff7ff2016-02-08 13:07:14577 return true;
minyue@webrtc.org1e168412014-10-09 10:52:43578 }
579 uint32_t ntp_secs = 0;
580 uint32_t ntp_frac = 0;
581 uint32_t rtp_timestamp = 0;
Percd997652016-04-15 12:59:13582 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
583 &rtp_timestamp) != 0) {
minyue@webrtc.org1e168412014-10-09 10:52:43584 // Waiting for RTCP.
Peter Boström70ff7ff2016-02-08 13:07:14585 return true;
minyue@webrtc.org1e168412014-10-09 10:52:43586 }
Peter Boströme5d4a262016-03-02 14:05:53587 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.org093fc0b2014-04-24 22:10:24588
Peter Boström70ff7ff2016-02-08 13:07:14589 return true;
wu@webrtc.org093fc0b2014-04-24 22:10:24590}
591
philipel19cf6be2017-09-04 14:03:46592void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommi451d9c22017-02-27 09:59:36593 if (!nack_module_)
594 return;
595
philipela7cca312017-02-22 13:30:39596 int seq_num = -1;
597 {
598 rtc::CritScope lock(&last_seq_num_cs_);
599 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
600 if (seq_num_it != last_seq_num_for_pic_id_.end())
601 seq_num = seq_num_it->second;
philipelba3a19d2016-11-15 08:57:57602 }
philipela7cca312017-02-22 13:30:39603 if (seq_num != -1)
604 nack_module_->ClearUpTo(seq_num);
philipelba3a19d2016-11-15 08:57:57605}
606
philipel19cf6be2017-09-04 14:03:46607void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela7cca312017-02-22 13:30:39608 int seq_num = -1;
609 {
610 rtc::CritScope lock(&last_seq_num_cs_);
611 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
612 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
613 seq_num = seq_num_it->second;
614 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
615 ++seq_num_it);
philipelba3a19d2016-11-15 08:57:57616 }
philipela7cca312017-02-22 13:30:39617 }
618 if (seq_num != -1) {
619 packet_buffer_->ClearTo(seq_num);
620 reference_finder_->ClearTo(seq_num);
philipelba3a19d2016-11-15 08:57:57621 }
622}
623
nisse14ec7e02017-06-09 11:01:55624void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodman33927c72016-05-06 12:32:22625 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
626 : RtcpMode::kOff);
627}
628
nisse14ec7e02017-06-09 11:01:55629void RtpVideoStreamReceiver::StartReceive() {
eladalon7f622012017-08-25 07:49:08630 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23631 receiving_ = true;
632}
633
nisse14ec7e02017-06-09 11:01:55634void RtpVideoStreamReceiver::StopReceive() {
eladalon7f622012017-08-25 07:49:08635 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23636 receiving_ = false;
637}
638
nisse14ec7e02017-06-09 11:01:55639bool RtpVideoStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.orgdb74c612013-09-06 13:40:11640 StreamStatistician* statistician =
641 rtp_receive_statistics_->GetStatistician(header.ssrc);
642 if (!statistician)
643 return false;
644 return statistician->IsPacketInOrder(header.sequenceNumber);
645}
646
nisse14ec7e02017-06-09 11:01:55647bool RtpVideoStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
648 bool in_order) const {
stefan@webrtc.orgdb74c612013-09-06 13:40:11649 // Retransmissions are handled separately if RTX is enabled.
Peter Boströme5d4a262016-03-02 14:05:53650 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.orgdb74c612013-09-06 13:40:11651 return false;
652 StreamStatistician* statistician =
653 rtp_receive_statistics_->GetStatistician(header.ssrc);
654 if (!statistician)
655 return false;
656 // Check if this is a retransmission.
pkasting@chromium.orga3166c42015-01-12 21:51:21657 int64_t min_rtt = 0;
stefan9fe41912017-06-09 13:12:11658 rtp_rtcp_->RTT(config_.rtp.remote_ssrc, nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org7e97e4c2013-11-08 15:18:52659 return !in_order &&
stefan@webrtc.orgdb74c612013-09-06 13:40:11660 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org7fc75bb2013-08-15 23:38:54661}
mflodman33927c72016-05-06 12:32:22662
nisse14ec7e02017-06-09 11:01:55663void RtpVideoStreamReceiver::UpdateHistograms() {
brandtr001769c2016-10-31 11:51:33664 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asaperssonfc3c29b2016-11-30 09:42:26665 if (counter.first_packet_time_ms == -1)
666 return;
667
668 int64_t elapsed_sec =
669 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
670 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
671 return;
672
mflodman33927c72016-05-06 12:32:22673 if (counter.num_packets > 0) {
asapersson56fc6372016-09-10 05:40:25674 RTC_HISTOGRAM_PERCENTAGE(
mflodman33927c72016-05-06 12:32:22675 "WebRTC.Video.ReceivedFecPacketsInPercent",
676 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
677 }
678 if (counter.num_fec_packets > 0) {
asapersson56fc6372016-09-10 05:40:25679 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
680 static_cast<int>(counter.num_recovered_packets *
681 100 / counter.num_fec_packets));
mflodman33927c72016-05-06 12:32:22682 }
683}
684
nisse14ec7e02017-06-09 11:01:55685void RtpVideoStreamReceiver::EnableReceiveRtpHeaderExtension(
mflodman33927c72016-05-06 12:32:22686 const std::string& extension, int id) {
687 // One-byte-extension local identifiers are in the range 1-14 inclusive.
688 RTC_DCHECK_GE(id, 1);
689 RTC_DCHECK_LE(id, 14);
690 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
691 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
692 StringToRtpExtensionType(extension), id));
693}
694
nisse14ec7e02017-06-09 11:01:55695void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel09c99cf2016-12-20 12:15:59696 auto codec_params_it = pt_codec_params_.find(payload_type);
697 if (codec_params_it == pt_codec_params_.end())
698 return;
699
700 LOG(LS_INFO) << "Found out of band supplied codec parameters for"
johan900d66d2017-01-24 12:38:27701 << " payload type: " << static_cast<int>(payload_type);
philipel09c99cf2016-12-20 12:15:59702
703 H264SpropParameterSets sprop_decoder;
704 auto sprop_base64_it =
705 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
706
707 if (sprop_base64_it == codec_params_it->second.end())
708 return;
709
johan900d66d2017-01-24 12:38:27710 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel09c99cf2016-12-20 12:15:59711 return;
712
johan684009a2017-01-24 10:38:17713 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
714 sprop_decoder.pps_nalu());
philipel09c99cf2016-12-20 12:15:59715}
716
andrew@webrtc.orgb015cbe2012-10-22 18:19:23717} // namespace webrtc