blob: 54956688ad164c03dfc9c6c5c9e7afcd6f56cb50 [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
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "media/base/mediaconstants.h"
21#include "modules/pacing/packet_router.h"
22#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
23#include "modules/rtp_rtcp/include/receive_statistics.h"
24#include "modules/rtp_rtcp/include/rtp_cvo.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/rtp_rtcp/include/rtp_rtcp.h"
26#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020027#include "modules/rtp_rtcp/source/rtp_format.h"
philipelb3e42a42018-09-13 10:57:14 +020028#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
30#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020031#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/video_coding/frame_object.h"
33#include "modules/video_coding/h264_sprop_parameter_sets.h"
34#include "modules/video_coding/h264_sps_pps_tracker.h"
Ilya Nikolaevskiy8643b782018-06-07 16:15:40 +020035#include "modules/video_coding/nack_module.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "modules/video_coding/packet_buffer.h"
37#include "modules/video_coding/video_coding_impl.h"
38#include "rtc_base/checks.h"
39#include "rtc_base/location.h"
40#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020041#include "rtc_base/strings/string_builder.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010042#include "rtc_base/system/fallthrough.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "system_wrappers/include/field_trial.h"
44#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "video/receive_statistics_proxy.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000046
47namespace webrtc {
48
philipelfd5a20f2016-11-15 00:57:57 -080049namespace {
philipel3bf97cf2017-08-10 18:10:59 +020050// TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see:
51// crbug.com/752886
52constexpr int kPacketBufferStartSize = 512;
Johannes Kron201596f2018-10-22 14:33:39 +020053constexpr int kPacketBufferMaxSize = 2048;
Yves Gerey665174f2018-06-19 15:03:05 +020054} // namespace
philipelfd5a20f2016-11-15 00:57:57 -080055
mflodmanc0e58a32016-04-25 01:26:26 -070056std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
57 ReceiveStatistics* receive_statistics,
58 Transport* outgoing_transport,
59 RtcpRttStats* rtt_stats,
60 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
nisse15389c02017-01-24 02:36:58 -080061 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
mflodmanc0e58a32016-04-25 01:26:26 -070062 RtpRtcp::Configuration configuration;
63 configuration.audio = false;
64 configuration.receiver_only = true;
65 configuration.receive_statistics = receive_statistics;
66 configuration.outgoing_transport = outgoing_transport;
67 configuration.intra_frame_callback = nullptr;
68 configuration.rtt_stats = rtt_stats;
69 configuration.rtcp_packet_type_counter_observer =
70 rtcp_packet_type_counter_observer;
mflodmanc0e58a32016-04-25 01:26:26 -070071 configuration.transport_sequence_number_allocator =
72 transport_sequence_number_allocator;
73 configuration.send_bitrate_observer = nullptr;
74 configuration.send_frame_count_observer = nullptr;
75 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070076 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070077 configuration.bandwidth_callback = nullptr;
78 configuration.transport_feedback_callback = nullptr;
79
80 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
mflodmanc0e58a32016-04-25 01:26:26 -070081 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
82
83 return rtp_rtcp;
84}
85
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000086static const int kPacketLogIntervalMs = 10000;
87
nisseb1f2ff92017-06-09 04:01:55 -070088RtpVideoStreamReceiver::RtpVideoStreamReceiver(
mflodmanfa666592016-04-28 23:15:33 -070089 Transport* transport,
90 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -070091 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +020092 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070093 ReceiveStatistics* rtp_receive_statistics,
mflodmandc7d0d22016-05-06 05:32:22 -070094 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020095 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080096 NackSender* nack_sender,
97 KeyFrameRequestSender* keyframe_request_sender,
Benjamin Wright192eeec2018-10-17 17:27:25 -070098 video_coding::OnCompleteFrameCallback* complete_frame_callback,
99 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)
Tommi97888bd2016-01-21 23:24:59 +0100100 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +0200101 config_(*config),
mflodmanc0e58a32016-04-25 01:26:26 -0700102 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700103 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100104 ntp_estimator_(clock_),
Niels Möllerb0573bc2017-09-25 10:47:00 +0200105 rtp_header_extensions_(config_.rtp.extensions),
nisseca5706d2017-09-11 02:32:16 -0700106 rtp_receive_statistics_(rtp_receive_statistics),
brandtrd726a3f2017-06-29 02:45:35 -0700107 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000108 receiving_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700109 last_packet_log_ms_(-1),
nisseca5706d2017-09-11 02:32:16 -0700110 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_,
mflodmanc0e58a32016-04-25 01:26:26 -0700111 transport,
112 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700113 receive_stats_proxy,
nisse15389c02017-01-24 02:36:58 -0800114 packet_router)),
philipelfd5a20f2016-11-15 00:57:57 -0800115 complete_frame_callback_(complete_frame_callback),
116 keyframe_request_sender_(keyframe_request_sender),
Benjamin Wright192eeec2018-10-17 17:27:25 -0700117 has_received_frame_(false),
118 frame_decryptor_(frame_decryptor) {
eladalon822ff2b2017-08-01 06:30:28 -0700119 constexpr bool remb_candidate = true;
120 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodmancfc8e3b2016-05-03 21:22:04 -0700121 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
122 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
123
Tommi733b5472016-06-10 17:58:01 +0200124 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700125 << "A stream should not be configured with RTCP disabled. This value is "
126 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700127 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
Tommi733b5472016-06-10 17:58:01 +0200132 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
133 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
stefanb4ab3812017-06-09 06:12:11 -0700134 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700135 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
mflodmandc7d0d22016-05-06 05:32:22 -0700136
mflodmancfc8e3b2016-05-03 21:22:04 -0700137 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200138 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
139 ? kMaxPacketAgeToNack
140 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700141 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700142
Tommi733b5472016-06-10 17:58:01 +0200143 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700144 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
145
146 // Stats callback for CNAME changes.
147 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
148
tommidea489f2017-03-03 03:20:24 -0800149 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800150
tommif284b7f2017-02-27 01:59:36 -0800151 if (config_.rtp.nack.rtp_history_ms != 0) {
152 nack_module_.reset(
153 new NackModule(clock_, nack_sender, keyframe_request_sender));
tommidea489f2017-03-03 03:20:24 -0800154 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800155 }
philipelfd5a20f2016-11-15 00:57:57 -0800156
Johannes Kron487e6942018-10-24 00:53:39 +0200157 // The group here must be a positive power of 2, in which case that is used as
158 // size. All other values shall result in the default value being used.
Johannes Kron201596f2018-10-22 14:33:39 +0200159 const std::string group_name =
160 webrtc::field_trial::FindFullName("WebRTC-PacketBufferMaxSize");
161 int packet_buffer_max_size = kPacketBufferMaxSize;
162 if (!group_name.empty() &&
163 (sscanf(group_name.c_str(), "%d", &packet_buffer_max_size) != 1 ||
Johannes Kron487e6942018-10-24 00:53:39 +0200164 packet_buffer_max_size <= 0 ||
165 // Verify that the number is a positive power of 2.
166 (packet_buffer_max_size & (packet_buffer_max_size - 1)) != 0)) {
Johannes Kron201596f2018-10-22 14:33:39 +0200167 RTC_LOG(LS_WARNING) << "Invalid packet buffer max size: " << group_name;
168 packet_buffer_max_size = kPacketBufferMaxSize;
169 }
170
philipela45102f2017-02-22 05:30:39 -0800171 packet_buffer_ = video_coding::PacketBuffer::Create(
Johannes Kron201596f2018-10-22 14:33:39 +0200172 clock_, kPacketBufferStartSize, packet_buffer_max_size, this);
philipela45102f2017-02-22 05:30:39 -0800173 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
mflodmanc0e58a32016-04-25 01:26:26 -0700174}
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
nisseb1f2ff92017-06-09 04:01:55 -0700176RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalonc0d481a2017-08-02 07:39:07 -0700177 RTC_DCHECK(secondary_sinks_.empty());
178
tommif284b7f2017-02-27 01:59:36 -0800179 if (nack_module_) {
180 process_thread_->DeRegisterModule(nack_module_.get());
181 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
tommif284b7f2017-02-27 01:59:36 -0800183 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800184
nissefdbfdc92017-03-31 05:44:52 -0700185 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodmandc7d0d22016-05-06 05:32:22 -0700186 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000187}
188
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200189void RtpVideoStreamReceiver::AddReceiveCodec(
philipel022b54e2016-12-20 04:15:59 -0800190 const VideoCodec& video_codec,
191 const std::map<std::string, std::string>& codec_params) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200192 pt_codec_type_.emplace(video_codec.plType, video_codec.codecType);
193 pt_codec_params_.emplace(video_codec.plType, codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000194}
195
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200196absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200197 Syncable::Info info;
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200198 if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs,
199 &info.capture_time_ntp_frac, nullptr, nullptr,
200 &info.capture_time_source_clock) != 0) {
201 return absl::nullopt;
202 }
Niels Möllerb0d4b412018-08-28 13:58:15 +0200203 {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200204 rtc::CritScope lock(&rtp_sources_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200205 if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
206 return absl::nullopt;
207 }
208 info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
209 info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
210 }
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200211
212 // Leaves info.current_delay_ms uninitialized.
213 return info;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000214}
215
nisseb1f2ff92017-06-09 04:01:55 -0700216int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
mflodmanfa666592016-04-28 23:15:33 -0700217 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200218 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700219 const WebRtcRTPHeader* rtp_header) {
philipel2837edc2018-10-02 13:55:47 +0200220 return OnReceivedPayloadData(payload_data, payload_size, rtp_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100221 absl::nullopt, false);
philipel2837edc2018-10-02 13:55:47 +0200222}
223
224int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
225 const uint8_t* payload_data,
226 size_t payload_size,
227 const WebRtcRTPHeader* rtp_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100228 const absl::optional<RtpGenericFrameDescriptor>& generic_descriptor,
229 bool is_recovered) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000230 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000231 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100232 ntp_estimator_.Estimate(rtp_header->header.timestamp);
Niels Möller8dad9b42018-08-22 10:36:35 +0200233
philipela45102f2017-02-22 05:30:39 -0800234 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
Niels Möller8dad9b42018-08-22 10:36:35 +0200235 if (nack_module_) {
236 const bool is_keyframe =
237 rtp_header->video_header().is_first_packet_in_frame &&
238 rtp_header->frameType == kVideoFrameKey;
239
240 packet.timesNacked = nack_module_->OnReceivedPacket(
Ying Wangb32bb952018-10-31 10:12:27 +0100241 rtp_header->header.sequenceNumber, is_keyframe, is_recovered);
242
Niels Möller8dad9b42018-08-22 10:36:35 +0200243 } else {
244 packet.timesNacked = -1;
245 }
ilnik04f4d122017-06-19 07:18:55 -0700246 packet.receive_time_ms = clock_->TimeInMilliseconds();
philipelfd5a20f2016-11-15 00:57:57 -0800247
philipel54ca9192017-03-21 05:45:18 -0700248 if (packet.sizeBytes == 0) {
Niels Möllerbc010472018-03-23 13:22:29 +0100249 NotifyReceiverOfEmptyPacket(packet.seqNum);
philipel54ca9192017-03-21 05:45:18 -0700250 return 0;
251 }
252
philipela45102f2017-02-22 05:30:39 -0800253 if (packet.codec == kVideoCodecH264) {
254 // Only when we start to receive packets will we know what payload type
255 // that will be used. When we know the payload type insert the correct
256 // sps/pps into the tracker.
257 if (packet.payloadType != last_payload_type_) {
258 last_payload_type_ = packet.payloadType;
259 InsertSpsPpsIntoTracker(packet.payloadType);
philipelfd5a20f2016-11-15 00:57:57 -0800260 }
261
philipela45102f2017-02-22 05:30:39 -0800262 switch (tracker_.CopyAndFixBitstream(&packet)) {
263 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
264 keyframe_request_sender_->RequestKeyFrame();
Karl Wiberg80ba3332018-02-05 10:33:35 +0100265 RTC_FALLTHROUGH();
philipela45102f2017-02-22 05:30:39 -0800266 case video_coding::H264SpsPpsTracker::kDrop:
267 return 0;
268 case video_coding::H264SpsPpsTracker::kInsert:
269 break;
270 }
271
philipelfd5a20f2016-11-15 00:57:57 -0800272 } else {
philipela45102f2017-02-22 05:30:39 -0800273 uint8_t* data = new uint8_t[packet.sizeBytes];
274 memcpy(data, packet.dataPtr, packet.sizeBytes);
275 packet.dataPtr = data;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000276 }
philipela45102f2017-02-22 05:30:39 -0800277
philipel2837edc2018-10-02 13:55:47 +0200278 packet.generic_descriptor = generic_descriptor;
279
philipela45102f2017-02-22 05:30:39 -0800280 packet_buffer_->InsertPacket(&packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000281 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000282}
283
nisseb1f2ff92017-06-09 04:01:55 -0700284void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
285 size_t rtp_packet_length) {
Niels Möllerb0573bc2017-09-25 10:47:00 +0200286 RtpPacketReceived packet;
287 if (!packet.Parse(rtp_packet, rtp_packet_length))
nisse30e89312017-05-29 08:16:37 -0700288 return;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200289 if (packet.PayloadType() == config_.rtp.red_payload_type) {
290 RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation";
291 return;
292 }
293
Niels Möllerb0573bc2017-09-25 10:47:00 +0200294 packet.IdentifyExtensions(rtp_header_extensions_);
295 packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200296 // TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both
297 // original (decapsulated) media packets and recovered packets to
298 // this callback. We need a way to distinguish, for setting
299 // packet.recovered() correctly. Ideally, move RED decapsulation out
300 // of the Ulpfec implementation.
Niels Möllerb0573bc2017-09-25 10:47:00 +0200301
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200302 ReceivePacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000303}
304
nissed2ef3142017-05-11 08:00:58 -0700305// This method handles both regular RTP packets and packets recovered
306// via FlexFEC.
nisseb1f2ff92017-06-09 04:01:55 -0700307void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
eladalon8b073052017-08-25 00:49:08 -0700308 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700309
eladalon8b073052017-08-25 00:49:08 -0700310 if (!receiving_) {
311 return;
312 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000313
eladalon8b073052017-08-25 00:49:08 -0700314 if (!packet.recovered()) {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200315 // TODO(nisse): Exclude out-of-order packets?
eladalon8b073052017-08-25 00:49:08 -0700316 int64_t now_ms = clock_->TimeInMilliseconds();
Niels Möllerb0d4b412018-08-28 13:58:15 +0200317 {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200318 rtc::CritScope cs(&rtp_sources_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200319 last_received_rtp_timestamp_ = packet.Timestamp();
320 last_received_rtp_system_time_ms_ = now_ms;
Jonas Oreland49ac5952018-09-26 16:04:32 +0200321
322 std::vector<uint32_t> csrcs = packet.Csrcs();
323 contributing_sources_.Update(now_ms, csrcs);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200324 }
eladalon8b073052017-08-25 00:49:08 -0700325 // Periodically log the RTP header of incoming packets.
326 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200327 rtc::StringBuilder ss;
eladalon8b073052017-08-25 00:49:08 -0700328 ss << "Packet received on SSRC: " << packet.Ssrc()
329 << " with payload type: " << static_cast<int>(packet.PayloadType())
330 << ", timestamp: " << packet.Timestamp()
331 << ", sequence number: " << packet.SequenceNumber()
332 << ", arrival time: " << packet.arrival_time_ms();
333 int32_t time_offset;
334 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
335 ss << ", toffset: " << time_offset;
nisse38cc1d62017-02-13 05:59:46 -0800336 }
eladalon8b073052017-08-25 00:49:08 -0700337 uint32_t send_time;
338 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
339 ss << ", abs send time: " << send_time;
340 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100341 RTC_LOG(LS_INFO) << ss.str();
eladalon8b073052017-08-25 00:49:08 -0700342 last_packet_log_ms_ = now_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000343 }
344 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000345
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200346 ReceivePacket(packet);
nisse38cc1d62017-02-13 05:59:46 -0800347
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000348 // Update receive statistics after ReceivePacket.
349 // Receive statistics will be reset if the payload type changes (make sure
350 // that the first packet is included in the stats).
nissed2ef3142017-05-11 08:00:58 -0700351 if (!packet.recovered()) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200352 rtp_receive_statistics_->OnRtpPacket(packet);
nissed2ef3142017-05-11 08:00:58 -0700353 }
eladalonc0d481a2017-08-02 07:39:07 -0700354
355 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
356 secondary_sink->OnRtpPacket(packet);
357 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000358}
359
nisseb1f2ff92017-06-09 04:01:55 -0700360int32_t RtpVideoStreamReceiver::RequestKeyFrame() {
mflodmancfc8e3b2016-05-03 21:22:04 -0700361 return rtp_rtcp_->RequestKeyFrame();
362}
363
nisseb1f2ff92017-06-09 04:01:55 -0700364bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
nisse3b3622f2017-09-26 02:49:21 -0700365 return config_.rtp.ulpfec_payload_type != -1;
brandtre6f98c72016-11-11 03:28:30 -0800366}
367
nisseb1f2ff92017-06-09 04:01:55 -0700368bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodmandc7d0d22016-05-06 05:32:22 -0700369 return config_.rtp.nack.rtp_history_ms > 0;
370}
371
nisseb1f2ff92017-06-09 04:01:55 -0700372void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodmandc7d0d22016-05-06 05:32:22 -0700373 const std::vector<uint16_t>& sequence_numbers) {
374 rtp_rtcp_->SendNack(sequence_numbers);
375}
376
nisseb1f2ff92017-06-09 04:01:55 -0700377int32_t RtpVideoStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
378 uint16_t length) {
mflodmancfc8e3b2016-05-03 21:22:04 -0700379 return rtp_rtcp_->SendNACK(sequence_numbers, length);
380}
381
nisseb1f2ff92017-06-09 04:01:55 -0700382void RtpVideoStreamReceiver::OnReceivedFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800383 std::unique_ptr<video_coding::RtpFrameObject> frame) {
Benjamin Wright39feabe2018-10-22 13:33:09 -0700384 // Request a key frame as soon as possible.
385 bool key_frame_requested = false;
386 if (!has_received_frame_) {
387 has_received_frame_ = true;
388 if (frame->FrameType() != kVideoFrameKey) {
389 key_frame_requested = true;
390 keyframe_request_sender_->RequestKeyFrame();
391 }
392 }
393
Benjamin Wright192eeec2018-10-17 17:27:25 -0700394 // Optionally attempt to decrypt the raw video frame if it was provided.
395 if (frame_decryptor_ != nullptr) {
396 // When using encryption we expect the frame to have the generic descriptor.
397 absl::optional<RtpGenericFrameDescriptor> descriptor =
398 frame->GetGenericFrameDescriptor();
399 if (!descriptor) {
400 RTC_LOG(LS_ERROR) << "No generic frame descriptor found dropping frame.";
401 return;
402 }
403
404 // Retrieve the bitstream of the encrypted video frame.
405 rtc::ArrayView<const uint8_t> encrypted_frame_bitstream(frame->Buffer(),
406 frame->size());
407 // Retrieve the maximum possible size of the decrypted payload.
408 const size_t max_plaintext_byte_size =
409 frame_decryptor_->GetMaxPlaintextByteSize(cricket::MEDIA_TYPE_VIDEO,
410 frame->size());
411 RTC_CHECK(max_plaintext_byte_size <= frame->size());
412 // Place the decrypted frame inline into the existing frame.
413 rtc::ArrayView<uint8_t> inline_decrypted_bitstream(frame->MutableBuffer(),
414 max_plaintext_byte_size);
415
416 // Attempt to decrypt the video frame.
417 size_t bytes_written = 0;
418 if (frame_decryptor_->Decrypt(
419 cricket::MEDIA_TYPE_VIDEO, /*csrcs=*/{},
420 /*additional_data=*/nullptr, encrypted_frame_bitstream,
421 inline_decrypted_bitstream, &bytes_written) != 0) {
422 return;
423 }
Benjamin Wright39feabe2018-10-22 13:33:09 -0700424
425 if (!has_received_decrypted_frame_ && !key_frame_requested) {
426 has_received_decrypted_frame_ = true;
427 if (frame->FrameType() != kVideoFrameKey) {
428 keyframe_request_sender_->RequestKeyFrame();
429 }
430 }
431
Benjamin Wright192eeec2018-10-17 17:27:25 -0700432 RTC_CHECK(bytes_written <= max_plaintext_byte_size);
433 // Update the frame to contain just the written bytes.
434 frame->SetLength(bytes_written);
435 } else if (config_.crypto_options.sframe.require_frame_encryption) {
436 RTC_LOG(LS_WARNING) << "Frame decryption required but not attached to this "
437 "stream. Dropping frame.";
438 return;
439 }
440
philipelfd5a20f2016-11-15 00:57:57 -0800441 reference_finder_->ManageFrame(std::move(frame));
442}
443
nisseb1f2ff92017-06-09 04:01:55 -0700444void RtpVideoStreamReceiver::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100445 std::unique_ptr<video_coding::EncodedFrame> frame) {
philipelfd5a20f2016-11-15 00:57:57 -0800446 {
447 rtc::CritScope lock(&last_seq_num_cs_);
448 video_coding::RtpFrameObject* rtp_frame =
449 static_cast<video_coding::RtpFrameObject*>(frame.get());
philipel0fa82a62018-03-19 15:34:53 +0100450 last_seq_num_for_pic_id_[rtp_frame->id.picture_id] =
451 rtp_frame->last_seq_num();
philipelfd5a20f2016-11-15 00:57:57 -0800452 }
453 complete_frame_callback_->OnCompleteFrame(std::move(frame));
454}
455
Tommi81de14f2018-03-25 22:19:25 +0200456void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800457 if (nack_module_)
458 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800459}
460
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200461absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700462 return packet_buffer_->LastReceivedPacketMs();
463}
464
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200465absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
nisseb1f2ff92017-06-09 04:01:55 -0700466 const {
philipel3184f8e2017-05-18 08:08:53 -0700467 return packet_buffer_->LastReceivedKeyframePacketMs();
468}
469
eladalonc0d481a2017-08-02 07:39:07 -0700470void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700471 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700472 RTC_DCHECK(std::find(secondary_sinks_.cbegin(), secondary_sinks_.cend(),
473 sink) == secondary_sinks_.cend());
474 secondary_sinks_.push_back(sink);
475}
476
477void RtpVideoStreamReceiver::RemoveSecondarySink(
478 const RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700479 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700480 auto it = std::find(secondary_sinks_.begin(), secondary_sinks_.end(), sink);
481 if (it == secondary_sinks_.end()) {
482 // We might be rolling-back a call whose setup failed mid-way. In such a
483 // case, it's simpler to remove "everything" rather than remember what
484 // has already been added.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100485 RTC_LOG(LS_WARNING) << "Removal of unknown sink.";
eladalonc0d481a2017-08-02 07:39:07 -0700486 return;
487 }
488 secondary_sinks_.erase(it);
489}
490
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200491void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) {
492 if (packet.payload_size() == 0) {
Niels Möller0b926782018-08-21 17:49:24 +0200493 // Padding or keep-alive packet.
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200494 // TODO(nisse): Could drop empty packets earlier, but need to figure out how
495 // they should be counted in stats.
Niels Möller0b926782018-08-21 17:49:24 +0200496 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
nisse30e89312017-05-29 08:16:37 -0700497 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000498 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200499 if (packet.PayloadType() == config_.rtp.red_payload_type) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200500 ParseAndHandleEncapsulatingHeader(packet);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200501 return;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000502 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200503
504 const auto codec_type_it = pt_codec_type_.find(packet.PayloadType());
505 if (codec_type_it == pt_codec_type_.end()) {
506 return;
507 }
508 auto depacketizer =
509 absl::WrapUnique(RtpDepacketizer::Create(codec_type_it->second));
510
511 if (!depacketizer) {
512 RTC_LOG(LS_ERROR) << "Failed to create depacketizer.";
513 return;
514 }
515 RtpDepacketizer::ParsedPayload parsed_payload;
516 if (!depacketizer->Parse(&parsed_payload, packet.payload().data(),
517 packet.payload().size())) {
518 RTC_LOG(LS_WARNING) << "Failed parsing payload.";
519 return;
520 }
521
522 WebRtcRTPHeader webrtc_rtp_header = {};
523 packet.GetHeader(&webrtc_rtp_header.header);
524
525 webrtc_rtp_header.frameType = parsed_payload.frame_type;
526 webrtc_rtp_header.video_header() = parsed_payload.video_header();
527 webrtc_rtp_header.video_header().rotation = kVideoRotation_0;
528 webrtc_rtp_header.video_header().content_type = VideoContentType::UNSPECIFIED;
529 webrtc_rtp_header.video_header().video_timing.flags =
530 VideoSendTiming::kInvalid;
531 webrtc_rtp_header.video_header().playout_delay.min_ms = -1;
532 webrtc_rtp_header.video_header().playout_delay.max_ms = -1;
philipelef615ea2018-09-13 11:07:48 +0200533 webrtc_rtp_header.video_header().is_last_packet_in_frame =
534 webrtc_rtp_header.header.markerBit;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200535
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200536 packet.GetExtension<VideoOrientation>(
537 &webrtc_rtp_header.video_header().rotation);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200538 packet.GetExtension<VideoContentTypeExtension>(
539 &webrtc_rtp_header.video_header().content_type);
540 packet.GetExtension<VideoTimingExtension>(
541 &webrtc_rtp_header.video_header().video_timing);
542 packet.GetExtension<PlayoutDelayLimits>(
543 &webrtc_rtp_header.video_header().playout_delay);
544
philipel2837edc2018-10-02 13:55:47 +0200545 absl::optional<RtpGenericFrameDescriptor> generic_descriptor_wire;
546 generic_descriptor_wire.emplace();
philipelb3e42a42018-09-13 10:57:14 +0200547 if (packet.GetExtension<RtpGenericFrameDescriptorExtension>(
philipel2837edc2018-10-02 13:55:47 +0200548 &generic_descriptor_wire.value())) {
549 generic_descriptor_wire->SetByteRepresentation(
550 packet.GetRawExtension<RtpGenericFrameDescriptorExtension>());
philipelb3e42a42018-09-13 10:57:14 +0200551 webrtc_rtp_header.video_header().is_first_packet_in_frame =
philipel2837edc2018-10-02 13:55:47 +0200552 generic_descriptor_wire->FirstSubFrameInFrame() &&
553 generic_descriptor_wire->FirstPacketInSubFrame();
philipelef615ea2018-09-13 11:07:48 +0200554 webrtc_rtp_header.video_header().is_last_packet_in_frame =
555 webrtc_rtp_header.header.markerBit ||
philipel2837edc2018-10-02 13:55:47 +0200556 (generic_descriptor_wire->LastSubFrameInFrame() &&
557 generic_descriptor_wire->LastPacketInSubFrame());
philipelfab91292018-10-17 14:36:08 +0200558
559 if (generic_descriptor_wire->FirstPacketInSubFrame()) {
560 webrtc_rtp_header.frameType =
561 generic_descriptor_wire->FrameDependenciesDiffs().empty()
562 ? kVideoFrameKey
563 : kVideoFrameDelta;
564 }
565
566 webrtc_rtp_header.video_header().width = generic_descriptor_wire->Width();
567 webrtc_rtp_header.video_header().height = generic_descriptor_wire->Height();
philipel2837edc2018-10-02 13:55:47 +0200568 } else {
569 generic_descriptor_wire.reset();
philipelb3e42a42018-09-13 10:57:14 +0200570 }
571
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200572 OnReceivedPayloadData(parsed_payload.payload, parsed_payload.payload_length,
Ying Wangb32bb952018-10-31 10:12:27 +0100573 &webrtc_rtp_header, generic_descriptor_wire,
574 packet.recovered());
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000575}
576
nisseb1f2ff92017-06-09 04:01:55 -0700577void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
Niels Möller1f3206c2018-09-14 08:26:32 +0200578 const RtpPacketReceived& packet) {
eladalon8b073052017-08-25 00:49:08 -0700579 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200580 if (packet.PayloadType() == config_.rtp.red_payload_type &&
581 packet.payload_size() > 0) {
582 if (packet.payload()[0] == config_.rtp.ulpfec_payload_type) {
583 rtp_receive_statistics_->FecPacketReceived(packet);
Peter Boström0b250722016-04-22 18:23:15 +0200584 // Notify video_receiver about received FEC packets to avoid NACKing these
585 // packets.
Niels Möller1f3206c2018-09-14 08:26:32 +0200586 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000587 }
Niels Möller1f3206c2018-09-14 08:26:32 +0200588 RTPHeader header;
589 packet.GetHeader(&header);
Niels Möller3f027b32018-03-14 08:04:58 +0100590 if (ulpfec_receiver_->AddReceivedRedPacket(
Niels Möller1f3206c2018-09-14 08:26:32 +0200591 header, packet.data(), packet.size(),
592 config_.rtp.ulpfec_payload_type) != 0) {
nisse30e89312017-05-29 08:16:37 -0700593 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000594 }
nisse30e89312017-05-29 08:16:37 -0700595 ulpfec_receiver_->ProcessReceivedFec();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000596 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000597}
598
Niels Möllerbc010472018-03-23 13:22:29 +0100599// In the case of a video stream without picture ids and no rtx the
600// RtpFrameReferenceFinder will need to know about padding to
601// correctly calculate frame references.
602void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) {
603 reference_finder_->PaddingReceived(seq_num);
604 packet_buffer_->PaddingReceived(seq_num);
Niels Möllerbc010472018-03-23 13:22:29 +0100605 if (nack_module_) {
Ying Wangb32bb952018-10-31 10:12:27 +0100606 nack_module_->OnReceivedPacket(seq_num, /* is_keyframe = */ false,
607 /* is _recovered = */ false);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000608 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000609}
610
nisseb1f2ff92017-06-09 04:01:55 -0700611bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
612 size_t rtcp_packet_length) {
eladalon8b073052017-08-25 00:49:08 -0700613 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
614
615 if (!receiving_) {
616 return false;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100617 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000618
Per83d09102016-04-15 14:59:13 +0200619 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000620
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000621 int64_t rtt = 0;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200622 rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000623 if (rtt == 0) {
624 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100625 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000626 }
627 uint32_t ntp_secs = 0;
628 uint32_t ntp_frac = 0;
629 uint32_t rtp_timestamp = 0;
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100630 uint32_t recieved_ntp_secs = 0;
631 uint32_t recieved_ntp_frac = 0;
632 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs,
633 &recieved_ntp_frac, &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000634 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100635 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000636 }
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100637 NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac);
638 int64_t time_since_recieved =
639 clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs();
640 // Don't use old SRs to estimate time.
641 if (time_since_recieved <= 1) {
642 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
643 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000644
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100645 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000646}
647
philipeld4fac692017-09-04 07:03:46 -0700648void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800649 if (!nack_module_)
650 return;
651
philipela45102f2017-02-22 05:30:39 -0800652 int seq_num = -1;
653 {
654 rtc::CritScope lock(&last_seq_num_cs_);
655 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
656 if (seq_num_it != last_seq_num_for_pic_id_.end())
657 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800658 }
philipela45102f2017-02-22 05:30:39 -0800659 if (seq_num != -1)
660 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800661}
662
philipeld4fac692017-09-04 07:03:46 -0700663void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800664 int seq_num = -1;
665 {
666 rtc::CritScope lock(&last_seq_num_cs_);
667 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
668 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
669 seq_num = seq_num_it->second;
670 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
671 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800672 }
philipela45102f2017-02-22 05:30:39 -0800673 }
674 if (seq_num != -1) {
675 packet_buffer_->ClearTo(seq_num);
676 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800677 }
678}
679
nisseb1f2ff92017-06-09 04:01:55 -0700680void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodmandc7d0d22016-05-06 05:32:22 -0700681 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
682 : RtcpMode::kOff);
683}
684
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100685int RtpVideoStreamReceiver::GetUniqueFramesSeen() const {
686 return packet_buffer_->GetUniqueFramesSeen();
687}
688
nisseb1f2ff92017-06-09 04:01:55 -0700689void RtpVideoStreamReceiver::StartReceive() {
eladalon8b073052017-08-25 00:49:08 -0700690 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000691 receiving_ = true;
692}
693
nisseb1f2ff92017-06-09 04:01:55 -0700694void RtpVideoStreamReceiver::StopReceive() {
eladalon8b073052017-08-25 00:49:08 -0700695 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000696 receiving_ = false;
697}
698
nisseb1f2ff92017-06-09 04:01:55 -0700699void RtpVideoStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700700 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800701 if (counter.first_packet_time_ms == -1)
702 return;
703
704 int64_t elapsed_sec =
705 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
706 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
707 return;
708
mflodmandc7d0d22016-05-06 05:32:22 -0700709 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700710 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700711 "WebRTC.Video.ReceivedFecPacketsInPercent",
712 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
713 }
714 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700715 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
716 static_cast<int>(counter.num_recovered_packets *
717 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700718 }
719}
720
nisseb1f2ff92017-06-09 04:01:55 -0700721void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel022b54e2016-12-20 04:15:59 -0800722 auto codec_params_it = pt_codec_params_.find(payload_type);
723 if (codec_params_it == pt_codec_params_.end())
724 return;
725
Mirko Bonadei675513b2017-11-09 11:09:25 +0100726 RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for"
727 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800728
729 H264SpropParameterSets sprop_decoder;
730 auto sprop_base64_it =
731 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
732
733 if (sprop_base64_it == codec_params_it->second.end())
734 return;
735
johan62d02c32017-01-24 04:38:27 -0800736 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800737 return;
738
johand2b092f2017-01-24 02:38:17 -0800739 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
740 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800741}
742
Jonas Oreland49ac5952018-09-26 16:04:32 +0200743std::vector<webrtc::RtpSource> RtpVideoStreamReceiver::GetSources() const {
744 int64_t now_ms = rtc::TimeMillis();
745 std::vector<RtpSource> sources;
746 {
747 rtc::CritScope cs(&rtp_sources_lock_);
748 sources = contributing_sources_.GetSources(now_ms);
749 if (last_received_rtp_system_time_ms_ >=
750 now_ms - ContributingSources::kHistoryMs) {
751 sources.emplace_back(*last_received_rtp_system_time_ms_,
752 config_.rtp.remote_ssrc, RtpSourceType::SSRC);
753 }
754 }
755 return sources;
756}
757
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000758} // namespace webrtc