blob: df492ffff68bb59bafdfab2fae908674042d65b2 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/rtp_video_stream_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
eladalonc0d481a2017-08-02 07:39:07 -070013#include <algorithm>
philipelfd5a20f2016-11-15 00:57:57 -080014#include <utility>
ilnik04f4d122017-06-19 07:18:55 -070015#include <vector>
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000016
Niels Möller2ff1f2a2018-08-09 16:16:34 +020017#include "absl/memory/memory.h"
18
Steve Anton10542f22019-01-11 09:11:00 -080019#include "media/base/media_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/pacing/packet_router.h"
21#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
22#include "modules/rtp_rtcp/include/receive_statistics.h"
23#include "modules/rtp_rtcp/include/rtp_cvo.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/rtp_rtcp/include/rtp_rtcp.h"
25#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020026#include "modules/rtp_rtcp/source/rtp_format.h"
philipelb3e42a42018-09-13 10:57:14 +020027#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
29#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020030#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/video_coding/frame_object.h"
32#include "modules/video_coding/h264_sprop_parameter_sets.h"
33#include "modules/video_coding/h264_sps_pps_tracker.h"
Ilya Nikolaevskiy8643b782018-06-07 16:15:40 +020034#include "modules/video_coding/nack_module.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "modules/video_coding/packet_buffer.h"
36#include "modules/video_coding/video_coding_impl.h"
37#include "rtc_base/checks.h"
38#include "rtc_base/location.h"
39#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020040#include "rtc_base/strings/string_builder.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010041#include "rtc_base/system/fallthrough.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "system_wrappers/include/field_trial.h"
43#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "video/receive_statistics_proxy.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000045
46namespace webrtc {
47
philipelfd5a20f2016-11-15 00:57:57 -080048namespace {
philipel3bf97cf2017-08-10 18:10:59 +020049// TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see:
50// crbug.com/752886
51constexpr int kPacketBufferStartSize = 512;
Johannes Kron201596f2018-10-22 14:33:39 +020052constexpr int kPacketBufferMaxSize = 2048;
Yves Gerey665174f2018-06-19 15:03:05 +020053} // namespace
philipelfd5a20f2016-11-15 00:57:57 -080054
mflodmanc0e58a32016-04-25 01:26:26 -070055std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
56 ReceiveStatistics* receive_statistics,
57 Transport* outgoing_transport,
58 RtcpRttStats* rtt_stats,
59 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
nisse15389c02017-01-24 02:36:58 -080060 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
mflodmanc0e58a32016-04-25 01:26:26 -070061 RtpRtcp::Configuration configuration;
62 configuration.audio = false;
63 configuration.receiver_only = true;
64 configuration.receive_statistics = receive_statistics;
65 configuration.outgoing_transport = outgoing_transport;
66 configuration.intra_frame_callback = nullptr;
67 configuration.rtt_stats = rtt_stats;
68 configuration.rtcp_packet_type_counter_observer =
69 rtcp_packet_type_counter_observer;
mflodmanc0e58a32016-04-25 01:26:26 -070070 configuration.transport_sequence_number_allocator =
71 transport_sequence_number_allocator;
72 configuration.send_bitrate_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070073 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070074 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070075 configuration.bandwidth_callback = nullptr;
76 configuration.transport_feedback_callback = nullptr;
77
78 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
mflodmanc0e58a32016-04-25 01:26:26 -070079 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
80
81 return rtp_rtcp;
82}
83
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000084static const int kPacketLogIntervalMs = 10000;
85
nisseb1f2ff92017-06-09 04:01:55 -070086RtpVideoStreamReceiver::RtpVideoStreamReceiver(
mflodmanfa666592016-04-28 23:15:33 -070087 Transport* transport,
88 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -070089 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +020090 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070091 ReceiveStatistics* rtp_receive_statistics,
mflodmandc7d0d22016-05-06 05:32:22 -070092 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020093 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080094 NackSender* nack_sender,
95 KeyFrameRequestSender* keyframe_request_sender,
Benjamin Wright192eeec2018-10-17 17:27:25 -070096 video_coding::OnCompleteFrameCallback* complete_frame_callback,
97 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)
Tommi97888bd2016-01-21 23:24:59 +010098 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020099 config_(*config),
mflodmanc0e58a32016-04-25 01:26:26 -0700100 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700101 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100102 ntp_estimator_(clock_),
Niels Möllerb0573bc2017-09-25 10:47:00 +0200103 rtp_header_extensions_(config_.rtp.extensions),
nisseca5706d2017-09-11 02:32:16 -0700104 rtp_receive_statistics_(rtp_receive_statistics),
brandtrd726a3f2017-06-29 02:45:35 -0700105 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000106 receiving_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700107 last_packet_log_ms_(-1),
nisseca5706d2017-09-11 02:32:16 -0700108 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_,
mflodmanc0e58a32016-04-25 01:26:26 -0700109 transport,
110 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700111 receive_stats_proxy,
nisse15389c02017-01-24 02:36:58 -0800112 packet_router)),
philipelfd5a20f2016-11-15 00:57:57 -0800113 complete_frame_callback_(complete_frame_callback),
114 keyframe_request_sender_(keyframe_request_sender),
Benjamin Wright00765292018-11-30 16:18:26 -0800115 has_received_frame_(false) {
eladalon822ff2b2017-08-01 06:30:28 -0700116 constexpr bool remb_candidate = true;
117 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodmancfc8e3b2016-05-03 21:22:04 -0700118
Tommi733b5472016-06-10 17:58:01 +0200119 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700120 << "A stream should not be configured with RTCP disabled. This value is "
121 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700122 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
123 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
124 RTC_DCHECK(config_.rtp.local_ssrc != 0);
125 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
126
Tommi733b5472016-06-10 17:58:01 +0200127 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
128 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
stefanb4ab3812017-06-09 06:12:11 -0700129 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700130 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
mflodmandc7d0d22016-05-06 05:32:22 -0700131
mflodmancfc8e3b2016-05-03 21:22:04 -0700132 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200133 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
134 ? kMaxPacketAgeToNack
135 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700136 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700137
Tommi733b5472016-06-10 17:58:01 +0200138 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700139 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
140
141 // Stats callback for CNAME changes.
142 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
143
tommidea489f2017-03-03 03:20:24 -0800144 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800145
tommif284b7f2017-02-27 01:59:36 -0800146 if (config_.rtp.nack.rtp_history_ms != 0) {
Elad Alona8f54612018-11-06 11:21:25 +0100147 nack_module_ = absl::make_unique<NackModule>(clock_, nack_sender,
148 keyframe_request_sender);
tommidea489f2017-03-03 03:20:24 -0800149 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800150 }
philipelfd5a20f2016-11-15 00:57:57 -0800151
Johannes Kron487e6942018-10-24 00:53:39 +0200152 // The group here must be a positive power of 2, in which case that is used as
153 // size. All other values shall result in the default value being used.
Johannes Kron201596f2018-10-22 14:33:39 +0200154 const std::string group_name =
155 webrtc::field_trial::FindFullName("WebRTC-PacketBufferMaxSize");
156 int packet_buffer_max_size = kPacketBufferMaxSize;
157 if (!group_name.empty() &&
158 (sscanf(group_name.c_str(), "%d", &packet_buffer_max_size) != 1 ||
Johannes Kron487e6942018-10-24 00:53:39 +0200159 packet_buffer_max_size <= 0 ||
160 // Verify that the number is a positive power of 2.
161 (packet_buffer_max_size & (packet_buffer_max_size - 1)) != 0)) {
Johannes Kron201596f2018-10-22 14:33:39 +0200162 RTC_LOG(LS_WARNING) << "Invalid packet buffer max size: " << group_name;
163 packet_buffer_max_size = kPacketBufferMaxSize;
164 }
165
philipela45102f2017-02-22 05:30:39 -0800166 packet_buffer_ = video_coding::PacketBuffer::Create(
Johannes Kron201596f2018-10-22 14:33:39 +0200167 clock_, kPacketBufferStartSize, packet_buffer_max_size, this);
Elad Alona8f54612018-11-06 11:21:25 +0100168 reference_finder_ =
169 absl::make_unique<video_coding::RtpFrameReferenceFinder>(this);
Benjamin Wright00765292018-11-30 16:18:26 -0800170 // Only construct the encrypted receiver if frame encryption is enabled.
171 if (frame_decryptor != nullptr ||
172 config_.crypto_options.sframe.require_frame_encryption) {
173 buffered_frame_decryptor_ =
174 absl::make_unique<BufferedFrameDecryptor>(this, frame_decryptor);
175 }
mflodmanc0e58a32016-04-25 01:26:26 -0700176}
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
nisseb1f2ff92017-06-09 04:01:55 -0700178RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalonc0d481a2017-08-02 07:39:07 -0700179 RTC_DCHECK(secondary_sinks_.empty());
180
tommif284b7f2017-02-27 01:59:36 -0800181 if (nack_module_) {
182 process_thread_->DeRegisterModule(nack_module_.get());
183 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
tommif284b7f2017-02-27 01:59:36 -0800185 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800186
nissefdbfdc92017-03-31 05:44:52 -0700187 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodmandc7d0d22016-05-06 05:32:22 -0700188 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000189}
190
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200191void RtpVideoStreamReceiver::AddReceiveCodec(
philipel022b54e2016-12-20 04:15:59 -0800192 const VideoCodec& video_codec,
193 const std::map<std::string, std::string>& codec_params) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200194 pt_codec_type_.emplace(video_codec.plType, video_codec.codecType);
195 pt_codec_params_.emplace(video_codec.plType, codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000196}
197
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200198absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200199 Syncable::Info info;
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200200 if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs,
201 &info.capture_time_ntp_frac, nullptr, nullptr,
202 &info.capture_time_source_clock) != 0) {
203 return absl::nullopt;
204 }
Niels Möllerb0d4b412018-08-28 13:58:15 +0200205 {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200206 rtc::CritScope lock(&rtp_sources_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200207 if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
208 return absl::nullopt;
209 }
210 info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
211 info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
212 }
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200213
214 // Leaves info.current_delay_ms uninitialized.
215 return info;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000216}
217
nisseb1f2ff92017-06-09 04:01:55 -0700218int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
mflodmanfa666592016-04-28 23:15:33 -0700219 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200220 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700221 const WebRtcRTPHeader* rtp_header) {
philipel2837edc2018-10-02 13:55:47 +0200222 return OnReceivedPayloadData(payload_data, payload_size, rtp_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100223 absl::nullopt, false);
philipel2837edc2018-10-02 13:55:47 +0200224}
225
226int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
227 const uint8_t* payload_data,
228 size_t payload_size,
229 const WebRtcRTPHeader* rtp_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100230 const absl::optional<RtpGenericFrameDescriptor>& generic_descriptor,
231 bool is_recovered) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000232 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000233 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100234 ntp_estimator_.Estimate(rtp_header->header.timestamp);
Niels Möller8dad9b42018-08-22 10:36:35 +0200235
philipela45102f2017-02-22 05:30:39 -0800236 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
Niels Möller8dad9b42018-08-22 10:36:35 +0200237 if (nack_module_) {
238 const bool is_keyframe =
239 rtp_header->video_header().is_first_packet_in_frame &&
240 rtp_header->frameType == kVideoFrameKey;
241
242 packet.timesNacked = nack_module_->OnReceivedPacket(
Ying Wangb32bb952018-10-31 10:12:27 +0100243 rtp_header->header.sequenceNumber, is_keyframe, is_recovered);
244
Niels Möller8dad9b42018-08-22 10:36:35 +0200245 } else {
246 packet.timesNacked = -1;
247 }
ilnik04f4d122017-06-19 07:18:55 -0700248 packet.receive_time_ms = clock_->TimeInMilliseconds();
philipelfd5a20f2016-11-15 00:57:57 -0800249
philipel54ca9192017-03-21 05:45:18 -0700250 if (packet.sizeBytes == 0) {
Niels Möllerbc010472018-03-23 13:22:29 +0100251 NotifyReceiverOfEmptyPacket(packet.seqNum);
philipel54ca9192017-03-21 05:45:18 -0700252 return 0;
253 }
254
Niels Möllerd5e02f02019-02-20 13:12:21 +0100255 if (packet.codec() == kVideoCodecH264) {
philipela45102f2017-02-22 05:30:39 -0800256 // Only when we start to receive packets will we know what payload type
257 // that will be used. When we know the payload type insert the correct
258 // sps/pps into the tracker.
259 if (packet.payloadType != last_payload_type_) {
260 last_payload_type_ = packet.payloadType;
261 InsertSpsPpsIntoTracker(packet.payloadType);
philipelfd5a20f2016-11-15 00:57:57 -0800262 }
263
philipela45102f2017-02-22 05:30:39 -0800264 switch (tracker_.CopyAndFixBitstream(&packet)) {
265 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
266 keyframe_request_sender_->RequestKeyFrame();
Karl Wiberg80ba3332018-02-05 10:33:35 +0100267 RTC_FALLTHROUGH();
philipela45102f2017-02-22 05:30:39 -0800268 case video_coding::H264SpsPpsTracker::kDrop:
269 return 0;
270 case video_coding::H264SpsPpsTracker::kInsert:
271 break;
272 }
273
philipelfd5a20f2016-11-15 00:57:57 -0800274 } else {
philipela45102f2017-02-22 05:30:39 -0800275 uint8_t* data = new uint8_t[packet.sizeBytes];
276 memcpy(data, packet.dataPtr, packet.sizeBytes);
277 packet.dataPtr = data;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000278 }
philipela45102f2017-02-22 05:30:39 -0800279
philipel2837edc2018-10-02 13:55:47 +0200280 packet.generic_descriptor = generic_descriptor;
281
philipela45102f2017-02-22 05:30:39 -0800282 packet_buffer_->InsertPacket(&packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000283 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000284}
285
nisseb1f2ff92017-06-09 04:01:55 -0700286void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
287 size_t rtp_packet_length) {
Niels Möllerb0573bc2017-09-25 10:47:00 +0200288 RtpPacketReceived packet;
289 if (!packet.Parse(rtp_packet, rtp_packet_length))
nisse30e89312017-05-29 08:16:37 -0700290 return;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200291 if (packet.PayloadType() == config_.rtp.red_payload_type) {
292 RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation";
293 return;
294 }
295
Niels Möllerb0573bc2017-09-25 10:47:00 +0200296 packet.IdentifyExtensions(rtp_header_extensions_);
297 packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200298 // TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both
299 // original (decapsulated) media packets and recovered packets to
300 // this callback. We need a way to distinguish, for setting
301 // packet.recovered() correctly. Ideally, move RED decapsulation out
302 // of the Ulpfec implementation.
Niels Möllerb0573bc2017-09-25 10:47:00 +0200303
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200304 ReceivePacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000305}
306
nissed2ef3142017-05-11 08:00:58 -0700307// This method handles both regular RTP packets and packets recovered
308// via FlexFEC.
nisseb1f2ff92017-06-09 04:01:55 -0700309void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
eladalon8b073052017-08-25 00:49:08 -0700310 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700311
eladalon8b073052017-08-25 00:49:08 -0700312 if (!receiving_) {
313 return;
314 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000315
eladalon8b073052017-08-25 00:49:08 -0700316 if (!packet.recovered()) {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200317 // TODO(nisse): Exclude out-of-order packets?
eladalon8b073052017-08-25 00:49:08 -0700318 int64_t now_ms = clock_->TimeInMilliseconds();
Niels Möllerb0d4b412018-08-28 13:58:15 +0200319 {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200320 rtc::CritScope cs(&rtp_sources_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200321 last_received_rtp_timestamp_ = packet.Timestamp();
322 last_received_rtp_system_time_ms_ = now_ms;
Jonas Oreland49ac5952018-09-26 16:04:32 +0200323
324 std::vector<uint32_t> csrcs = packet.Csrcs();
Jonas Oreland967f7d52018-11-06 07:35:06 +0100325 contributing_sources_.Update(now_ms, csrcs,
Benjamin Wright00765292018-11-30 16:18:26 -0800326 /* audio level */ absl::nullopt);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200327 }
eladalon8b073052017-08-25 00:49:08 -0700328 // Periodically log the RTP header of incoming packets.
329 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200330 rtc::StringBuilder ss;
eladalon8b073052017-08-25 00:49:08 -0700331 ss << "Packet received on SSRC: " << packet.Ssrc()
332 << " with payload type: " << static_cast<int>(packet.PayloadType())
333 << ", timestamp: " << packet.Timestamp()
334 << ", sequence number: " << packet.SequenceNumber()
335 << ", arrival time: " << packet.arrival_time_ms();
336 int32_t time_offset;
337 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
338 ss << ", toffset: " << time_offset;
nisse38cc1d62017-02-13 05:59:46 -0800339 }
eladalon8b073052017-08-25 00:49:08 -0700340 uint32_t send_time;
341 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
342 ss << ", abs send time: " << send_time;
343 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100344 RTC_LOG(LS_INFO) << ss.str();
eladalon8b073052017-08-25 00:49:08 -0700345 last_packet_log_ms_ = now_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000346 }
347 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000348
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200349 ReceivePacket(packet);
nisse38cc1d62017-02-13 05:59:46 -0800350
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000351 // Update receive statistics after ReceivePacket.
352 // Receive statistics will be reset if the payload type changes (make sure
353 // that the first packet is included in the stats).
nissed2ef3142017-05-11 08:00:58 -0700354 if (!packet.recovered()) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200355 rtp_receive_statistics_->OnRtpPacket(packet);
nissed2ef3142017-05-11 08:00:58 -0700356 }
eladalonc0d481a2017-08-02 07:39:07 -0700357
358 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
359 secondary_sink->OnRtpPacket(packet);
360 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000361}
362
nisseb1f2ff92017-06-09 04:01:55 -0700363int32_t RtpVideoStreamReceiver::RequestKeyFrame() {
mflodmancfc8e3b2016-05-03 21:22:04 -0700364 return rtp_rtcp_->RequestKeyFrame();
365}
366
nisseb1f2ff92017-06-09 04:01:55 -0700367bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
nisse3b3622f2017-09-26 02:49:21 -0700368 return config_.rtp.ulpfec_payload_type != -1;
brandtre6f98c72016-11-11 03:28:30 -0800369}
370
nisseb1f2ff92017-06-09 04:01:55 -0700371bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodmandc7d0d22016-05-06 05:32:22 -0700372 return config_.rtp.nack.rtp_history_ms > 0;
373}
374
nisseb1f2ff92017-06-09 04:01:55 -0700375void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodmandc7d0d22016-05-06 05:32:22 -0700376 const std::vector<uint16_t>& sequence_numbers) {
377 rtp_rtcp_->SendNack(sequence_numbers);
378}
379
nisseb1f2ff92017-06-09 04:01:55 -0700380int32_t RtpVideoStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
381 uint16_t length) {
mflodmancfc8e3b2016-05-03 21:22:04 -0700382 return rtp_rtcp_->SendNACK(sequence_numbers, length);
383}
384
Elad Alonb4643ad2019-02-22 11:19:50 +0100385void RtpVideoStreamReceiver::OnAssembledFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800386 std::unique_ptr<video_coding::RtpFrameObject> frame) {
Benjamin Wright00765292018-11-30 16:18:26 -0800387 RTC_DCHECK_RUN_ON(&network_tc_);
Benjamin Wright39feabe2018-10-22 13:33:09 -0700388 // Request a key frame as soon as possible.
389 bool key_frame_requested = false;
390 if (!has_received_frame_) {
391 has_received_frame_ = true;
392 if (frame->FrameType() != kVideoFrameKey) {
393 key_frame_requested = true;
394 keyframe_request_sender_->RequestKeyFrame();
395 }
396 }
Benjamin Wright00765292018-11-30 16:18:26 -0800397 if (buffered_frame_decryptor_ == nullptr) {
398 reference_finder_->ManageFrame(std::move(frame));
399 } else {
400 buffered_frame_decryptor_->ManageEncryptedFrame(std::move(frame));
Benjamin Wright192eeec2018-10-17 17:27:25 -0700401 }
philipelfd5a20f2016-11-15 00:57:57 -0800402}
403
nisseb1f2ff92017-06-09 04:01:55 -0700404void RtpVideoStreamReceiver::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100405 std::unique_ptr<video_coding::EncodedFrame> frame) {
philipelfd5a20f2016-11-15 00:57:57 -0800406 {
407 rtc::CritScope lock(&last_seq_num_cs_);
408 video_coding::RtpFrameObject* rtp_frame =
409 static_cast<video_coding::RtpFrameObject*>(frame.get());
philipel0fa82a62018-03-19 15:34:53 +0100410 last_seq_num_for_pic_id_[rtp_frame->id.picture_id] =
411 rtp_frame->last_seq_num();
philipelfd5a20f2016-11-15 00:57:57 -0800412 }
413 complete_frame_callback_->OnCompleteFrame(std::move(frame));
414}
415
Benjamin Wright00765292018-11-30 16:18:26 -0800416void RtpVideoStreamReceiver::OnDecryptedFrame(
417 std::unique_ptr<video_coding::RtpFrameObject> frame) {
418 reference_finder_->ManageFrame(std::move(frame));
419}
420
Tommi81de14f2018-03-25 22:19:25 +0200421void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800422 if (nack_module_)
423 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800424}
425
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200426absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700427 return packet_buffer_->LastReceivedPacketMs();
428}
429
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200430absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
nisseb1f2ff92017-06-09 04:01:55 -0700431 const {
philipel3184f8e2017-05-18 08:08:53 -0700432 return packet_buffer_->LastReceivedKeyframePacketMs();
433}
434
eladalonc0d481a2017-08-02 07:39:07 -0700435void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700436 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700437 RTC_DCHECK(std::find(secondary_sinks_.cbegin(), secondary_sinks_.cend(),
438 sink) == secondary_sinks_.cend());
439 secondary_sinks_.push_back(sink);
440}
441
442void RtpVideoStreamReceiver::RemoveSecondarySink(
443 const RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700444 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700445 auto it = std::find(secondary_sinks_.begin(), secondary_sinks_.end(), sink);
446 if (it == secondary_sinks_.end()) {
447 // We might be rolling-back a call whose setup failed mid-way. In such a
448 // case, it's simpler to remove "everything" rather than remember what
449 // has already been added.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100450 RTC_LOG(LS_WARNING) << "Removal of unknown sink.";
eladalonc0d481a2017-08-02 07:39:07 -0700451 return;
452 }
453 secondary_sinks_.erase(it);
454}
455
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200456void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) {
457 if (packet.payload_size() == 0) {
Niels Möller0b926782018-08-21 17:49:24 +0200458 // Padding or keep-alive packet.
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200459 // TODO(nisse): Could drop empty packets earlier, but need to figure out how
460 // they should be counted in stats.
Niels Möller0b926782018-08-21 17:49:24 +0200461 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
nisse30e89312017-05-29 08:16:37 -0700462 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000463 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200464 if (packet.PayloadType() == config_.rtp.red_payload_type) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200465 ParseAndHandleEncapsulatingHeader(packet);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200466 return;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000467 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200468
469 const auto codec_type_it = pt_codec_type_.find(packet.PayloadType());
470 if (codec_type_it == pt_codec_type_.end()) {
471 return;
472 }
473 auto depacketizer =
474 absl::WrapUnique(RtpDepacketizer::Create(codec_type_it->second));
475
476 if (!depacketizer) {
477 RTC_LOG(LS_ERROR) << "Failed to create depacketizer.";
478 return;
479 }
480 RtpDepacketizer::ParsedPayload parsed_payload;
481 if (!depacketizer->Parse(&parsed_payload, packet.payload().data(),
482 packet.payload().size())) {
483 RTC_LOG(LS_WARNING) << "Failed parsing payload.";
484 return;
485 }
486
487 WebRtcRTPHeader webrtc_rtp_header = {};
488 packet.GetHeader(&webrtc_rtp_header.header);
489
490 webrtc_rtp_header.frameType = parsed_payload.frame_type;
491 webrtc_rtp_header.video_header() = parsed_payload.video_header();
492 webrtc_rtp_header.video_header().rotation = kVideoRotation_0;
493 webrtc_rtp_header.video_header().content_type = VideoContentType::UNSPECIFIED;
494 webrtc_rtp_header.video_header().video_timing.flags =
495 VideoSendTiming::kInvalid;
philipelef615ea2018-09-13 11:07:48 +0200496 webrtc_rtp_header.video_header().is_last_packet_in_frame =
497 webrtc_rtp_header.header.markerBit;
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500498 webrtc_rtp_header.video_header().frame_marking.temporal_id = kNoTemporalIdx;
499
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100500 if (parsed_payload.video_header().codec == kVideoCodecVP9) {
501 const RTPVideoHeaderVP9& codec_header = absl::get<RTPVideoHeaderVP9>(
502 parsed_payload.video_header().video_type_header);
503 webrtc_rtp_header.video_header().is_last_packet_in_frame |=
504 codec_header.end_of_frame;
505 webrtc_rtp_header.video_header().is_first_packet_in_frame |=
506 codec_header.beginning_of_frame;
507 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200508
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200509 packet.GetExtension<VideoOrientation>(
510 &webrtc_rtp_header.video_header().rotation);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200511 packet.GetExtension<VideoContentTypeExtension>(
512 &webrtc_rtp_header.video_header().content_type);
513 packet.GetExtension<VideoTimingExtension>(
514 &webrtc_rtp_header.video_header().video_timing);
515 packet.GetExtension<PlayoutDelayLimits>(
516 &webrtc_rtp_header.video_header().playout_delay);
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500517 packet.GetExtension<FrameMarkingExtension>(
518 &webrtc_rtp_header.video_header().frame_marking);
519
Johannes Krond0b69a82018-12-03 14:18:53 +0100520 webrtc_rtp_header.video_header().color_space =
521 packet.GetExtension<ColorSpaceExtension>();
522 if (webrtc_rtp_header.video_header().color_space ||
523 webrtc_rtp_header.frameType == kVideoFrameKey) {
524 // Store color space since it's only transmitted when changed or for key
525 // frames. Color space will be cleared if a key frame is transmitted without
526 // color space information.
527 last_color_space_ = webrtc_rtp_header.video_header().color_space;
528 } else if (last_color_space_) {
529 webrtc_rtp_header.video_header().color_space = last_color_space_;
530 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500531
philipel2837edc2018-10-02 13:55:47 +0200532 absl::optional<RtpGenericFrameDescriptor> generic_descriptor_wire;
533 generic_descriptor_wire.emplace();
Elad Alonccb9b752019-02-19 13:01:31 +0100534 const bool generic_descriptor_v00 =
535 packet.GetExtension<RtpGenericFrameDescriptorExtension00>(
536 &generic_descriptor_wire.value());
537 const bool generic_descriptor_v01 =
538 packet.GetExtension<RtpGenericFrameDescriptorExtension01>(
539 &generic_descriptor_wire.value());
540 if (generic_descriptor_v00 && generic_descriptor_v01) {
541 RTC_LOG(LS_WARNING) << "RTP packet had two different GFD versions.";
542 return;
543 }
544
545 if (generic_descriptor_v00 || generic_descriptor_v01) {
546 if (generic_descriptor_v00) {
547 generic_descriptor_wire->SetByteRepresentation(
548 packet.GetRawExtension<RtpGenericFrameDescriptorExtension00>());
549 } else {
550 generic_descriptor_wire->SetByteRepresentation(
551 packet.GetRawExtension<RtpGenericFrameDescriptorExtension01>());
552 }
553
philipelb3e42a42018-09-13 10:57:14 +0200554 webrtc_rtp_header.video_header().is_first_packet_in_frame =
philipel2837edc2018-10-02 13:55:47 +0200555 generic_descriptor_wire->FirstPacketInSubFrame();
philipelef615ea2018-09-13 11:07:48 +0200556 webrtc_rtp_header.video_header().is_last_packet_in_frame =
557 webrtc_rtp_header.header.markerBit ||
Elad Alonccb9b752019-02-19 13:01:31 +0100558 generic_descriptor_wire->LastPacketInSubFrame();
philipelfab91292018-10-17 14:36:08 +0200559
560 if (generic_descriptor_wire->FirstPacketInSubFrame()) {
561 webrtc_rtp_header.frameType =
562 generic_descriptor_wire->FrameDependenciesDiffs().empty()
563 ? kVideoFrameKey
564 : kVideoFrameDelta;
565 }
566
567 webrtc_rtp_header.video_header().width = generic_descriptor_wire->Width();
568 webrtc_rtp_header.video_header().height = generic_descriptor_wire->Height();
philipel2837edc2018-10-02 13:55:47 +0200569 } else {
570 generic_descriptor_wire.reset();
philipelb3e42a42018-09-13 10:57:14 +0200571 }
572
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200573 OnReceivedPayloadData(parsed_payload.payload, parsed_payload.payload_length,
Ying Wangb32bb952018-10-31 10:12:27 +0100574 &webrtc_rtp_header, generic_descriptor_wire,
575 packet.recovered());
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000576}
577
nisseb1f2ff92017-06-09 04:01:55 -0700578void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
Niels Möller1f3206c2018-09-14 08:26:32 +0200579 const RtpPacketReceived& packet) {
eladalon8b073052017-08-25 00:49:08 -0700580 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200581 if (packet.PayloadType() == config_.rtp.red_payload_type &&
582 packet.payload_size() > 0) {
583 if (packet.payload()[0] == config_.rtp.ulpfec_payload_type) {
584 rtp_receive_statistics_->FecPacketReceived(packet);
Peter Boström0b250722016-04-22 18:23:15 +0200585 // Notify video_receiver about received FEC packets to avoid NACKing these
586 // packets.
Niels Möller1f3206c2018-09-14 08:26:32 +0200587 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000588 }
Niels Möller1f3206c2018-09-14 08:26:32 +0200589 RTPHeader header;
590 packet.GetHeader(&header);
Niels Möller3f027b32018-03-14 08:04:58 +0100591 if (ulpfec_receiver_->AddReceivedRedPacket(
Niels Möller1f3206c2018-09-14 08:26:32 +0200592 header, packet.data(), packet.size(),
593 config_.rtp.ulpfec_payload_type) != 0) {
nisse30e89312017-05-29 08:16:37 -0700594 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000595 }
nisse30e89312017-05-29 08:16:37 -0700596 ulpfec_receiver_->ProcessReceivedFec();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000597 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000598}
599
Niels Möllerbc010472018-03-23 13:22:29 +0100600// In the case of a video stream without picture ids and no rtx the
601// RtpFrameReferenceFinder will need to know about padding to
602// correctly calculate frame references.
603void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) {
604 reference_finder_->PaddingReceived(seq_num);
605 packet_buffer_->PaddingReceived(seq_num);
Niels Möllerbc010472018-03-23 13:22:29 +0100606 if (nack_module_) {
Ying Wangb32bb952018-10-31 10:12:27 +0100607 nack_module_->OnReceivedPacket(seq_num, /* is_keyframe = */ false,
608 /* is _recovered = */ false);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000609 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000610}
611
nisseb1f2ff92017-06-09 04:01:55 -0700612bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
613 size_t rtcp_packet_length) {
eladalon8b073052017-08-25 00:49:08 -0700614 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
615
616 if (!receiving_) {
617 return false;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100618 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000619
Per83d09102016-04-15 14:59:13 +0200620 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000621
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000622 int64_t rtt = 0;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200623 rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000624 if (rtt == 0) {
625 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100626 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000627 }
628 uint32_t ntp_secs = 0;
629 uint32_t ntp_frac = 0;
630 uint32_t rtp_timestamp = 0;
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100631 uint32_t recieved_ntp_secs = 0;
632 uint32_t recieved_ntp_frac = 0;
633 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs,
634 &recieved_ntp_frac, &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000635 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100636 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000637 }
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100638 NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac);
639 int64_t time_since_recieved =
640 clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs();
641 // Don't use old SRs to estimate time.
642 if (time_since_recieved <= 1) {
643 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
644 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000645
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100646 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000647}
648
philipeld4fac692017-09-04 07:03:46 -0700649void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800650 if (!nack_module_)
651 return;
652
philipela45102f2017-02-22 05:30:39 -0800653 int seq_num = -1;
654 {
655 rtc::CritScope lock(&last_seq_num_cs_);
656 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
657 if (seq_num_it != last_seq_num_for_pic_id_.end())
658 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800659 }
philipela45102f2017-02-22 05:30:39 -0800660 if (seq_num != -1)
661 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800662}
663
philipeld4fac692017-09-04 07:03:46 -0700664void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800665 int seq_num = -1;
666 {
667 rtc::CritScope lock(&last_seq_num_cs_);
668 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
669 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
670 seq_num = seq_num_it->second;
671 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
672 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800673 }
philipela45102f2017-02-22 05:30:39 -0800674 }
675 if (seq_num != -1) {
676 packet_buffer_->ClearTo(seq_num);
677 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800678 }
679}
680
nisseb1f2ff92017-06-09 04:01:55 -0700681void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodmandc7d0d22016-05-06 05:32:22 -0700682 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
683 : RtcpMode::kOff);
684}
685
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100686int RtpVideoStreamReceiver::GetUniqueFramesSeen() const {
687 return packet_buffer_->GetUniqueFramesSeen();
688}
689
nisseb1f2ff92017-06-09 04:01:55 -0700690void RtpVideoStreamReceiver::StartReceive() {
eladalon8b073052017-08-25 00:49:08 -0700691 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000692 receiving_ = true;
693}
694
nisseb1f2ff92017-06-09 04:01:55 -0700695void RtpVideoStreamReceiver::StopReceive() {
eladalon8b073052017-08-25 00:49:08 -0700696 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000697 receiving_ = false;
698}
699
nisseb1f2ff92017-06-09 04:01:55 -0700700void RtpVideoStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700701 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800702 if (counter.first_packet_time_ms == -1)
703 return;
704
705 int64_t elapsed_sec =
706 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
707 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
708 return;
709
mflodmandc7d0d22016-05-06 05:32:22 -0700710 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700711 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700712 "WebRTC.Video.ReceivedFecPacketsInPercent",
713 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
714 }
715 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700716 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
717 static_cast<int>(counter.num_recovered_packets *
718 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700719 }
720}
721
nisseb1f2ff92017-06-09 04:01:55 -0700722void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel022b54e2016-12-20 04:15:59 -0800723 auto codec_params_it = pt_codec_params_.find(payload_type);
724 if (codec_params_it == pt_codec_params_.end())
725 return;
726
Mirko Bonadei675513b2017-11-09 11:09:25 +0100727 RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for"
728 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800729
730 H264SpropParameterSets sprop_decoder;
731 auto sprop_base64_it =
732 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
733
734 if (sprop_base64_it == codec_params_it->second.end())
735 return;
736
johan62d02c32017-01-24 04:38:27 -0800737 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800738 return;
739
johand2b092f2017-01-24 02:38:17 -0800740 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
741 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800742}
743
Jonas Oreland49ac5952018-09-26 16:04:32 +0200744std::vector<webrtc::RtpSource> RtpVideoStreamReceiver::GetSources() const {
745 int64_t now_ms = rtc::TimeMillis();
746 std::vector<RtpSource> sources;
747 {
748 rtc::CritScope cs(&rtp_sources_lock_);
749 sources = contributing_sources_.GetSources(now_ms);
750 if (last_received_rtp_system_time_ms_ >=
751 now_ms - ContributingSources::kHistoryMs) {
752 sources.emplace_back(*last_received_rtp_system_time_ms_,
753 config_.rtp.remote_ssrc, RtpSourceType::SSRC);
754 }
755 }
756 return sources;
757}
758
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000759} // namespace webrtc