niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 39e9659 | 2012-03-01 18:22:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/rtp_video_stream_receiver.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 13 | #include <algorithm> |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 14 | #include <utility> |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 15 | #include <vector> |
mflodman@webrtc.org | 4fd5527 | 2013-02-06 17:46:39 +0000 | [diff] [blame] | 16 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
| 18 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 19 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 26 | #include "modules/rtp_rtcp/include/ulpfec_receiver.h" |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 27 | #include "modules/rtp_rtcp/source/rtp_format.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "modules/rtp_rtcp/source/rtp_header_extensions.h" |
| 29 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 30 | #include "modules/rtp_rtcp/source/rtp_rtcp_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #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 Nikolaevskiy | 8643b78 | 2018-06-07 16:15:40 +0200 | [diff] [blame] | 34 | #include "modules/video_coding/nack_module.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 35 | #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" |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 40 | #include "rtc_base/system/fallthrough.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "system_wrappers/include/field_trial.h" |
| 42 | #include "system_wrappers/include/metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "video/receive_statistics_proxy.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
| 45 | namespace webrtc { |
| 46 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 47 | namespace { |
philipel | 3bf97cf | 2017-08-10 18:10:59 +0200 | [diff] [blame] | 48 | // TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see: |
| 49 | // crbug.com/752886 |
| 50 | constexpr int kPacketBufferStartSize = 512; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 51 | constexpr int kPacketBufferMaxSixe = 2048; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | } // namespace |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 53 | |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 54 | std::unique_ptr<RtpRtcp> CreateRtpRtcpModule( |
| 55 | ReceiveStatistics* receive_statistics, |
| 56 | Transport* outgoing_transport, |
| 57 | RtcpRttStats* rtt_stats, |
| 58 | RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer, |
nisse | 15389c0 | 2017-01-24 02:36:58 -0800 | [diff] [blame] | 59 | TransportSequenceNumberAllocator* transport_sequence_number_allocator) { |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 60 | RtpRtcp::Configuration configuration; |
| 61 | configuration.audio = false; |
| 62 | configuration.receiver_only = true; |
| 63 | configuration.receive_statistics = receive_statistics; |
| 64 | configuration.outgoing_transport = outgoing_transport; |
| 65 | configuration.intra_frame_callback = nullptr; |
| 66 | configuration.rtt_stats = rtt_stats; |
| 67 | configuration.rtcp_packet_type_counter_observer = |
| 68 | rtcp_packet_type_counter_observer; |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 69 | configuration.transport_sequence_number_allocator = |
| 70 | transport_sequence_number_allocator; |
| 71 | configuration.send_bitrate_observer = nullptr; |
| 72 | configuration.send_frame_count_observer = nullptr; |
| 73 | configuration.send_side_delay_observer = nullptr; |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 74 | configuration.send_packet_observer = nullptr; |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 75 | configuration.bandwidth_callback = nullptr; |
| 76 | configuration.transport_feedback_callback = nullptr; |
| 77 | |
| 78 | std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration)); |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 79 | rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound); |
| 80 | |
| 81 | return rtp_rtcp; |
| 82 | } |
| 83 | |
stefan@webrtc.org | eb24b04 | 2014-10-14 11:40:13 +0000 | [diff] [blame] | 84 | static const int kPacketLogIntervalMs = 10000; |
| 85 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 86 | RtpVideoStreamReceiver::RtpVideoStreamReceiver( |
mflodman | fa66659 | 2016-04-28 23:15:33 -0700 | [diff] [blame] | 87 | Transport* transport, |
| 88 | RtcpRttStats* rtt_stats, |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 89 | PacketRouter* packet_router, |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 90 | const VideoReceiveStream::Config* config, |
nisse | ca5706d | 2017-09-11 02:32:16 -0700 | [diff] [blame] | 91 | ReceiveStatistics* rtp_receive_statistics, |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 92 | ReceiveStatisticsProxy* receive_stats_proxy, |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 93 | ProcessThread* process_thread, |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 94 | NackSender* nack_sender, |
| 95 | KeyFrameRequestSender* keyframe_request_sender, |
philipel | 0a9f6de | 2018-02-28 11:29:47 +0100 | [diff] [blame] | 96 | video_coding::OnCompleteFrameCallback* complete_frame_callback) |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 97 | : clock_(Clock::GetRealTimeClock()), |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 98 | config_(*config), |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 99 | packet_router_(packet_router), |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 100 | process_thread_(process_thread), |
Peter Boström | 4fa7eca | 2016-03-02 15:05:53 +0100 | [diff] [blame] | 101 | ntp_estimator_(clock_), |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 102 | rtp_header_extensions_(config_.rtp.extensions), |
nisse | ca5706d | 2017-09-11 02:32:16 -0700 | [diff] [blame] | 103 | rtp_receive_statistics_(rtp_receive_statistics), |
brandtr | d726a3f | 2017-06-29 02:45:35 -0700 | [diff] [blame] | 104 | ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)), |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 105 | receiving_(false), |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 106 | last_packet_log_ms_(-1), |
nisse | ca5706d | 2017-09-11 02:32:16 -0700 | [diff] [blame] | 107 | rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_, |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 108 | transport, |
| 109 | rtt_stats, |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 110 | receive_stats_proxy, |
nisse | 15389c0 | 2017-01-24 02:36:58 -0800 | [diff] [blame] | 111 | packet_router)), |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 112 | complete_frame_callback_(complete_frame_callback), |
| 113 | keyframe_request_sender_(keyframe_request_sender), |
philipel | 2c53b13 | 2017-05-16 08:06:30 -0700 | [diff] [blame] | 114 | has_received_frame_(false) { |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 115 | constexpr bool remb_candidate = true; |
| 116 | packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate); |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 117 | rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy); |
| 118 | rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy); |
| 119 | |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 120 | RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff) |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 121 | << "A stream should not be configured with RTCP disabled. This value is " |
| 122 | "reserved for internal usage."; |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 123 | RTC_DCHECK(config_.rtp.remote_ssrc != 0); |
| 124 | // TODO(pbos): What's an appropriate local_ssrc for receive-only streams? |
| 125 | RTC_DCHECK(config_.rtp.local_ssrc != 0); |
| 126 | RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc); |
| 127 | |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 128 | rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode); |
| 129 | rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc); |
stefan | b4ab381 | 2017-06-09 06:12:11 -0700 | [diff] [blame] | 130 | rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc); |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 131 | rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 132 | |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 133 | static const int kMaxPacketAgeToNack = 450; |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 134 | const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0) |
| 135 | ? kMaxPacketAgeToNack |
| 136 | : kDefaultMaxReorderingThreshold; |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 137 | rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 138 | |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 139 | if (config_.rtp.rtcp_xr.receiver_reference_time_report) |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 140 | rtp_rtcp_->SetRtcpXrRrtrStatus(true); |
| 141 | |
| 142 | // Stats callback for CNAME changes. |
| 143 | rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy); |
| 144 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 145 | process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 146 | |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 147 | if (config_.rtp.nack.rtp_history_ms != 0) { |
| 148 | nack_module_.reset( |
| 149 | new NackModule(clock_, nack_sender, keyframe_request_sender)); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 150 | process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE); |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 151 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 152 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 153 | packet_buffer_ = video_coding::PacketBuffer::Create( |
| 154 | clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this); |
| 155 | reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this)); |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 156 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 158 | RtpVideoStreamReceiver::~RtpVideoStreamReceiver() { |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 159 | RTC_DCHECK(secondary_sinks_.empty()); |
| 160 | |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 161 | if (nack_module_) { |
| 162 | process_thread_->DeRegisterModule(nack_module_.get()); |
| 163 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 165 | process_thread_->DeRegisterModule(rtp_rtcp_.get()); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 166 | |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 167 | packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get()); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 168 | UpdateHistograms(); |
asapersson@webrtc.org | 0800db7 | 2015-01-15 07:40:20 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 171 | void RtpVideoStreamReceiver::AddReceiveCodec( |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 172 | const VideoCodec& video_codec, |
| 173 | const std::map<std::string, std::string>& codec_params) { |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 174 | pt_codec_type_.emplace(video_codec.plType, video_codec.codecType); |
| 175 | pt_codec_params_.emplace(video_codec.plType, codec_params); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 178 | absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const { |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 179 | Syncable::Info info; |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 180 | if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs, |
| 181 | &info.capture_time_ntp_frac, nullptr, nullptr, |
| 182 | &info.capture_time_source_clock) != 0) { |
| 183 | return absl::nullopt; |
| 184 | } |
Niels Möller | b0d4b41 | 2018-08-28 13:58:15 +0200 | [diff] [blame^] | 185 | { |
| 186 | rtc::CritScope lock(&last_seq_num_cs_); |
| 187 | if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) { |
| 188 | return absl::nullopt; |
| 189 | } |
| 190 | info.latest_received_capture_timestamp = *last_received_rtp_timestamp_; |
| 191 | info.latest_receive_time_ms = *last_received_rtp_system_time_ms_; |
| 192 | } |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 193 | |
| 194 | // Leaves info.current_delay_ms uninitialized. |
| 195 | return info; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 196 | } |
| 197 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 198 | int32_t RtpVideoStreamReceiver::OnReceivedPayloadData( |
mflodman | fa66659 | 2016-04-28 23:15:33 -0700 | [diff] [blame] | 199 | const uint8_t* payload_data, |
Peter Boström | 0208322 | 2016-06-14 12:52:54 +0200 | [diff] [blame] | 200 | size_t payload_size, |
mflodman | fa66659 | 2016-04-28 23:15:33 -0700 | [diff] [blame] | 201 | const WebRtcRTPHeader* rtp_header) { |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 202 | WebRtcRTPHeader rtp_header_with_ntp = *rtp_header; |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 203 | rtp_header_with_ntp.ntp_time_ms = |
Peter Boström | 4fa7eca | 2016-03-02 15:05:53 +0100 | [diff] [blame] | 204 | ntp_estimator_.Estimate(rtp_header->header.timestamp); |
Niels Möller | 8dad9b4 | 2018-08-22 10:36:35 +0200 | [diff] [blame] | 205 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 206 | VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp); |
Niels Möller | 8dad9b4 | 2018-08-22 10:36:35 +0200 | [diff] [blame] | 207 | if (nack_module_) { |
| 208 | const bool is_keyframe = |
| 209 | rtp_header->video_header().is_first_packet_in_frame && |
| 210 | rtp_header->frameType == kVideoFrameKey; |
| 211 | |
| 212 | packet.timesNacked = nack_module_->OnReceivedPacket( |
| 213 | rtp_header->header.sequenceNumber, is_keyframe); |
| 214 | } else { |
| 215 | packet.timesNacked = -1; |
| 216 | } |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 217 | packet.receive_time_ms = clock_->TimeInMilliseconds(); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 218 | |
philipel | 54ca919 | 2017-03-21 05:45:18 -0700 | [diff] [blame] | 219 | if (packet.sizeBytes == 0) { |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 220 | NotifyReceiverOfEmptyPacket(packet.seqNum); |
philipel | 54ca919 | 2017-03-21 05:45:18 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 224 | if (packet.codec == kVideoCodecH264) { |
| 225 | // Only when we start to receive packets will we know what payload type |
| 226 | // that will be used. When we know the payload type insert the correct |
| 227 | // sps/pps into the tracker. |
| 228 | if (packet.payloadType != last_payload_type_) { |
| 229 | last_payload_type_ = packet.payloadType; |
| 230 | InsertSpsPpsIntoTracker(packet.payloadType); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 231 | } |
| 232 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 233 | switch (tracker_.CopyAndFixBitstream(&packet)) { |
| 234 | case video_coding::H264SpsPpsTracker::kRequestKeyframe: |
| 235 | keyframe_request_sender_->RequestKeyFrame(); |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 236 | RTC_FALLTHROUGH(); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 237 | case video_coding::H264SpsPpsTracker::kDrop: |
| 238 | return 0; |
| 239 | case video_coding::H264SpsPpsTracker::kInsert: |
| 240 | break; |
| 241 | } |
| 242 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 243 | } else { |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 244 | uint8_t* data = new uint8_t[packet.sizeBytes]; |
| 245 | memcpy(data, packet.dataPtr, packet.sizeBytes); |
| 246 | packet.dataPtr = data; |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 247 | } |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 248 | |
| 249 | packet_buffer_->InsertPacket(&packet); |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 250 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | } |
| 252 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 253 | void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet, |
| 254 | size_t rtp_packet_length) { |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 255 | RtpPacketReceived packet; |
| 256 | if (!packet.Parse(rtp_packet, rtp_packet_length)) |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 257 | return; |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 258 | if (packet.PayloadType() == config_.rtp.red_payload_type) { |
| 259 | RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation"; |
| 260 | return; |
| 261 | } |
| 262 | |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 263 | packet.IdentifyExtensions(rtp_header_extensions_); |
| 264 | packet.set_payload_type_frequency(kVideoPayloadTypeFrequency); |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 265 | // TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both |
| 266 | // original (decapsulated) media packets and recovered packets to |
| 267 | // this callback. We need a way to distinguish, for setting |
| 268 | // packet.recovered() correctly. Ideally, move RED decapsulation out |
| 269 | // of the Ulpfec implementation. |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 270 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 271 | ReceivePacket(packet); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 272 | } |
| 273 | |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 274 | // This method handles both regular RTP packets and packets recovered |
| 275 | // via FlexFEC. |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 276 | void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) { |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 277 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 278 | |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 279 | if (!receiving_) { |
| 280 | return; |
| 281 | } |
solenberg@webrtc.org | fc32046 | 2014-02-11 15:27:49 +0000 | [diff] [blame] | 282 | |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 283 | if (!packet.recovered()) { |
| 284 | int64_t now_ms = clock_->TimeInMilliseconds(); |
Niels Möller | b0d4b41 | 2018-08-28 13:58:15 +0200 | [diff] [blame^] | 285 | { |
| 286 | rtc::CritScope lock(&last_seq_num_cs_); |
stefan@webrtc.org | eb24b04 | 2014-10-14 11:40:13 +0000 | [diff] [blame] | 287 | |
Niels Möller | b0d4b41 | 2018-08-28 13:58:15 +0200 | [diff] [blame^] | 288 | // TODO(nisse): Exclude out-of-order packets? |
| 289 | last_received_rtp_timestamp_ = packet.Timestamp(); |
| 290 | last_received_rtp_system_time_ms_ = now_ms; |
| 291 | } |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 292 | // Periodically log the RTP header of incoming packets. |
| 293 | if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) { |
| 294 | std::stringstream ss; |
| 295 | ss << "Packet received on SSRC: " << packet.Ssrc() |
| 296 | << " with payload type: " << static_cast<int>(packet.PayloadType()) |
| 297 | << ", timestamp: " << packet.Timestamp() |
| 298 | << ", sequence number: " << packet.SequenceNumber() |
| 299 | << ", arrival time: " << packet.arrival_time_ms(); |
| 300 | int32_t time_offset; |
| 301 | if (packet.GetExtension<TransmissionOffset>(&time_offset)) { |
| 302 | ss << ", toffset: " << time_offset; |
nisse | 38cc1d6 | 2017-02-13 05:59:46 -0800 | [diff] [blame] | 303 | } |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 304 | uint32_t send_time; |
| 305 | if (packet.GetExtension<AbsoluteSendTime>(&send_time)) { |
| 306 | ss << ", abs send time: " << send_time; |
| 307 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 308 | RTC_LOG(LS_INFO) << ss.str(); |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 309 | last_packet_log_ms_ = now_ms; |
stefan@webrtc.org | eb24b04 | 2014-10-14 11:40:13 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 312 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 313 | ReceivePacket(packet); |
nisse | 38cc1d6 | 2017-02-13 05:59:46 -0800 | [diff] [blame] | 314 | |
asapersson@webrtc.org | 1457b47 | 2014-05-26 13:06:04 +0000 | [diff] [blame] | 315 | // Update receive statistics after ReceivePacket. |
| 316 | // Receive statistics will be reset if the payload type changes (make sure |
| 317 | // that the first packet is included in the stats). |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 318 | if (!packet.recovered()) { |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 319 | RTPHeader header; |
| 320 | packet.GetHeader(&header); |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 321 | // TODO(nisse): We should pass a recovered flag to stats, to aid |
| 322 | // fixing bug bugs.webrtc.org/6339. |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 323 | rtp_receive_statistics_->IncomingPacket(header, packet.size()); |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 324 | } |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 325 | |
| 326 | for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) { |
| 327 | secondary_sink->OnRtpPacket(packet); |
| 328 | } |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 329 | } |
| 330 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 331 | int32_t RtpVideoStreamReceiver::RequestKeyFrame() { |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 332 | return rtp_rtcp_->RequestKeyFrame(); |
| 333 | } |
| 334 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 335 | bool RtpVideoStreamReceiver::IsUlpfecEnabled() const { |
nisse | 3b3622f | 2017-09-26 02:49:21 -0700 | [diff] [blame] | 336 | return config_.rtp.ulpfec_payload_type != -1; |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 337 | } |
| 338 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 339 | bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const { |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 340 | return config_.rtp.nack.rtp_history_ms > 0; |
| 341 | } |
| 342 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 343 | void RtpVideoStreamReceiver::RequestPacketRetransmit( |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 344 | const std::vector<uint16_t>& sequence_numbers) { |
| 345 | rtp_rtcp_->SendNack(sequence_numbers); |
| 346 | } |
| 347 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 348 | int32_t RtpVideoStreamReceiver::ResendPackets(const uint16_t* sequence_numbers, |
| 349 | uint16_t length) { |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 350 | return rtp_rtcp_->SendNACK(sequence_numbers, length); |
| 351 | } |
| 352 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 353 | void RtpVideoStreamReceiver::OnReceivedFrame( |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 354 | std::unique_ptr<video_coding::RtpFrameObject> frame) { |
philipel | 2c53b13 | 2017-05-16 08:06:30 -0700 | [diff] [blame] | 355 | if (!has_received_frame_) { |
| 356 | has_received_frame_ = true; |
| 357 | if (frame->FrameType() != kVideoFrameKey) |
| 358 | keyframe_request_sender_->RequestKeyFrame(); |
| 359 | } |
| 360 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 361 | reference_finder_->ManageFrame(std::move(frame)); |
| 362 | } |
| 363 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 364 | void RtpVideoStreamReceiver::OnCompleteFrame( |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 365 | std::unique_ptr<video_coding::EncodedFrame> frame) { |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 366 | { |
| 367 | rtc::CritScope lock(&last_seq_num_cs_); |
| 368 | video_coding::RtpFrameObject* rtp_frame = |
| 369 | static_cast<video_coding::RtpFrameObject*>(frame.get()); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 370 | last_seq_num_for_pic_id_[rtp_frame->id.picture_id] = |
| 371 | rtp_frame->last_seq_num(); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 372 | } |
| 373 | complete_frame_callback_->OnCompleteFrame(std::move(frame)); |
| 374 | } |
| 375 | |
Tommi | 81de14f | 2018-03-25 22:19:25 +0200 | [diff] [blame] | 376 | void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) { |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 377 | if (nack_module_) |
| 378 | nack_module_->UpdateRtt(max_rtt_ms); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 381 | absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const { |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 382 | return packet_buffer_->LastReceivedPacketMs(); |
| 383 | } |
| 384 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 385 | absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs() |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 386 | const { |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 387 | return packet_buffer_->LastReceivedKeyframePacketMs(); |
| 388 | } |
| 389 | |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 390 | void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) { |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 391 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 392 | RTC_DCHECK(std::find(secondary_sinks_.cbegin(), secondary_sinks_.cend(), |
| 393 | sink) == secondary_sinks_.cend()); |
| 394 | secondary_sinks_.push_back(sink); |
| 395 | } |
| 396 | |
| 397 | void RtpVideoStreamReceiver::RemoveSecondarySink( |
| 398 | const RtpPacketSinkInterface* sink) { |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 399 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 400 | auto it = std::find(secondary_sinks_.begin(), secondary_sinks_.end(), sink); |
| 401 | if (it == secondary_sinks_.end()) { |
| 402 | // We might be rolling-back a call whose setup failed mid-way. In such a |
| 403 | // case, it's simpler to remove "everything" rather than remember what |
| 404 | // has already been added. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 405 | RTC_LOG(LS_WARNING) << "Removal of unknown sink."; |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | secondary_sinks_.erase(it); |
| 409 | } |
| 410 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 411 | void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) { |
| 412 | if (packet.payload_size() == 0) { |
Niels Möller | 0b92678 | 2018-08-21 17:49:24 +0200 | [diff] [blame] | 413 | // Padding or keep-alive packet. |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 414 | // TODO(nisse): Could drop empty packets earlier, but need to figure out how |
| 415 | // they should be counted in stats. |
Niels Möller | 0b92678 | 2018-08-21 17:49:24 +0200 | [diff] [blame] | 416 | NotifyReceiverOfEmptyPacket(packet.SequenceNumber()); |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 417 | return; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 418 | } |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 419 | if (packet.PayloadType() == config_.rtp.red_payload_type) { |
| 420 | RTPHeader header; |
| 421 | packet.GetHeader(&header); |
| 422 | ParseAndHandleEncapsulatingHeader(packet.data(), packet.size(), header); |
| 423 | return; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 424 | } |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 425 | |
| 426 | const auto codec_type_it = pt_codec_type_.find(packet.PayloadType()); |
| 427 | if (codec_type_it == pt_codec_type_.end()) { |
| 428 | return; |
| 429 | } |
| 430 | auto depacketizer = |
| 431 | absl::WrapUnique(RtpDepacketizer::Create(codec_type_it->second)); |
| 432 | |
| 433 | if (!depacketizer) { |
| 434 | RTC_LOG(LS_ERROR) << "Failed to create depacketizer."; |
| 435 | return; |
| 436 | } |
| 437 | RtpDepacketizer::ParsedPayload parsed_payload; |
| 438 | if (!depacketizer->Parse(&parsed_payload, packet.payload().data(), |
| 439 | packet.payload().size())) { |
| 440 | RTC_LOG(LS_WARNING) << "Failed parsing payload."; |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | WebRtcRTPHeader webrtc_rtp_header = {}; |
| 445 | packet.GetHeader(&webrtc_rtp_header.header); |
| 446 | |
| 447 | webrtc_rtp_header.frameType = parsed_payload.frame_type; |
| 448 | webrtc_rtp_header.video_header() = parsed_payload.video_header(); |
| 449 | webrtc_rtp_header.video_header().rotation = kVideoRotation_0; |
| 450 | webrtc_rtp_header.video_header().content_type = VideoContentType::UNSPECIFIED; |
| 451 | webrtc_rtp_header.video_header().video_timing.flags = |
| 452 | VideoSendTiming::kInvalid; |
| 453 | webrtc_rtp_header.video_header().playout_delay.min_ms = -1; |
| 454 | webrtc_rtp_header.video_header().playout_delay.max_ms = -1; |
| 455 | |
| 456 | // Retrieve the video rotation information. |
| 457 | packet.GetExtension<VideoOrientation>( |
| 458 | &webrtc_rtp_header.video_header().rotation); |
| 459 | |
| 460 | packet.GetExtension<VideoContentTypeExtension>( |
| 461 | &webrtc_rtp_header.video_header().content_type); |
| 462 | packet.GetExtension<VideoTimingExtension>( |
| 463 | &webrtc_rtp_header.video_header().video_timing); |
| 464 | packet.GetExtension<PlayoutDelayLimits>( |
| 465 | &webrtc_rtp_header.video_header().playout_delay); |
| 466 | |
| 467 | OnReceivedPayloadData(parsed_payload.payload, parsed_payload.payload_length, |
| 468 | &webrtc_rtp_header); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 469 | } |
| 470 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 471 | void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 472 | const uint8_t* packet, |
| 473 | size_t packet_length, |
| 474 | const RTPHeader& header) { |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 475 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
Danil Chapovalov | a715f28 | 2018-07-11 17:50:41 +0200 | [diff] [blame] | 476 | if (header.payloadType == config_.rtp.red_payload_type && |
| 477 | packet_length > header.headerLength + header.paddingLength) { |
Niels Möller | 3f027b3 | 2018-03-14 08:04:58 +0100 | [diff] [blame] | 478 | if (packet[header.headerLength] == config_.rtp.ulpfec_payload_type) { |
asapersson@webrtc.org | 273fbbb | 2015-01-27 12:17:29 +0000 | [diff] [blame] | 479 | rtp_receive_statistics_->FecPacketReceived(header, packet_length); |
Peter Boström | 0b25072 | 2016-04-22 18:23:15 +0200 | [diff] [blame] | 480 | // Notify video_receiver about received FEC packets to avoid NACKing these |
| 481 | // packets. |
Niels Möller | 0b92678 | 2018-08-21 17:49:24 +0200 | [diff] [blame] | 482 | NotifyReceiverOfEmptyPacket(header.sequenceNumber); |
asapersson@webrtc.org | 37c0559 | 2015-01-28 13:58:27 +0000 | [diff] [blame] | 483 | } |
Niels Möller | 3f027b3 | 2018-03-14 08:04:58 +0100 | [diff] [blame] | 484 | if (ulpfec_receiver_->AddReceivedRedPacket( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 485 | header, packet, packet_length, config_.rtp.ulpfec_payload_type) != |
| 486 | 0) { |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 487 | return; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 488 | } |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 489 | ulpfec_receiver_->ProcessReceivedFec(); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 490 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 493 | // In the case of a video stream without picture ids and no rtx the |
| 494 | // RtpFrameReferenceFinder will need to know about padding to |
| 495 | // correctly calculate frame references. |
| 496 | void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) { |
| 497 | reference_finder_->PaddingReceived(seq_num); |
| 498 | packet_buffer_->PaddingReceived(seq_num); |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 499 | if (nack_module_) { |
Niels Möller | 0b92678 | 2018-08-21 17:49:24 +0200 | [diff] [blame] | 500 | nack_module_->OnReceivedPacket(seq_num, /* is_keyframe = */ false); |
asapersson@webrtc.org | 37c0559 | 2015-01-28 13:58:27 +0000 | [diff] [blame] | 501 | } |
asapersson@webrtc.org | 37c0559 | 2015-01-28 13:58:27 +0000 | [diff] [blame] | 502 | } |
| 503 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 504 | bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet, |
| 505 | size_t rtcp_packet_length) { |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 506 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
| 507 | |
| 508 | if (!receiving_) { |
| 509 | return false; |
Peter Boström | 4fa7eca | 2016-03-02 15:05:53 +0100 | [diff] [blame] | 510 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 512 | rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length); |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 513 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 514 | int64_t rtt = 0; |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 515 | rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr); |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 516 | if (rtt == 0) { |
| 517 | // Waiting for valid rtt. |
Peter Boström | d1d66ba | 2016-02-08 14:07:14 +0100 | [diff] [blame] | 518 | return true; |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 519 | } |
| 520 | uint32_t ntp_secs = 0; |
| 521 | uint32_t ntp_frac = 0; |
| 522 | uint32_t rtp_timestamp = 0; |
Ilya Nikolaevskiy | 7172ea1 | 2017-10-30 11:17:34 +0100 | [diff] [blame] | 523 | uint32_t recieved_ntp_secs = 0; |
| 524 | uint32_t recieved_ntp_frac = 0; |
| 525 | if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs, |
| 526 | &recieved_ntp_frac, &rtp_timestamp) != 0) { |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 527 | // Waiting for RTCP. |
Peter Boström | d1d66ba | 2016-02-08 14:07:14 +0100 | [diff] [blame] | 528 | return true; |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 529 | } |
Ilya Nikolaevskiy | 7172ea1 | 2017-10-30 11:17:34 +0100 | [diff] [blame] | 530 | NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac); |
| 531 | int64_t time_since_recieved = |
| 532 | clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs(); |
| 533 | // Don't use old SRs to estimate time. |
| 534 | if (time_since_recieved <= 1) { |
| 535 | ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp); |
| 536 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 537 | |
Peter Boström | d1d66ba | 2016-02-08 14:07:14 +0100 | [diff] [blame] | 538 | return true; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 539 | } |
| 540 | |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 541 | void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) { |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 542 | if (!nack_module_) |
| 543 | return; |
| 544 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 545 | int seq_num = -1; |
| 546 | { |
| 547 | rtc::CritScope lock(&last_seq_num_cs_); |
| 548 | auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id); |
| 549 | if (seq_num_it != last_seq_num_for_pic_id_.end()) |
| 550 | seq_num = seq_num_it->second; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 551 | } |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 552 | if (seq_num != -1) |
| 553 | nack_module_->ClearUpTo(seq_num); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 554 | } |
| 555 | |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 556 | void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) { |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 557 | int seq_num = -1; |
| 558 | { |
| 559 | rtc::CritScope lock(&last_seq_num_cs_); |
| 560 | auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id); |
| 561 | if (seq_num_it != last_seq_num_for_pic_id_.end()) { |
| 562 | seq_num = seq_num_it->second; |
| 563 | last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(), |
| 564 | ++seq_num_it); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 565 | } |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 566 | } |
| 567 | if (seq_num != -1) { |
| 568 | packet_buffer_->ClearTo(seq_num); |
| 569 | reference_finder_->ClearTo(seq_num); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 573 | void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) { |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 574 | rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode |
| 575 | : RtcpMode::kOff); |
| 576 | } |
| 577 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 578 | int RtpVideoStreamReceiver::GetUniqueFramesSeen() const { |
| 579 | return packet_buffer_->GetUniqueFramesSeen(); |
| 580 | } |
| 581 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 582 | void RtpVideoStreamReceiver::StartReceive() { |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 583 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 584 | receiving_ = true; |
| 585 | } |
| 586 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 587 | void RtpVideoStreamReceiver::StopReceive() { |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 588 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 589 | receiving_ = false; |
| 590 | } |
| 591 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 592 | void RtpVideoStreamReceiver::UpdateHistograms() { |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 593 | FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter(); |
asapersson | 0c43f77 | 2016-11-30 01:42:26 -0800 | [diff] [blame] | 594 | if (counter.first_packet_time_ms == -1) |
| 595 | return; |
| 596 | |
| 597 | int64_t elapsed_sec = |
| 598 | (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000; |
| 599 | if (elapsed_sec < metrics::kMinRunTimeInSeconds) |
| 600 | return; |
| 601 | |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 602 | if (counter.num_packets > 0) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 603 | RTC_HISTOGRAM_PERCENTAGE( |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 604 | "WebRTC.Video.ReceivedFecPacketsInPercent", |
| 605 | static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets)); |
| 606 | } |
| 607 | if (counter.num_fec_packets > 0) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 608 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec", |
| 609 | static_cast<int>(counter.num_recovered_packets * |
| 610 | 100 / counter.num_fec_packets)); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 614 | void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) { |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 615 | auto codec_params_it = pt_codec_params_.find(payload_type); |
| 616 | if (codec_params_it == pt_codec_params_.end()) |
| 617 | return; |
| 618 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 619 | RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for" |
| 620 | << " payload type: " << static_cast<int>(payload_type); |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 621 | |
| 622 | H264SpropParameterSets sprop_decoder; |
| 623 | auto sprop_base64_it = |
| 624 | codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets); |
| 625 | |
| 626 | if (sprop_base64_it == codec_params_it->second.end()) |
| 627 | return; |
| 628 | |
johan | 62d02c3 | 2017-01-24 04:38:27 -0800 | [diff] [blame] | 629 | if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str())) |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 630 | return; |
| 631 | |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 632 | tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(), |
| 633 | sprop_decoder.pps_nalu()); |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 634 | } |
| 635 | |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 636 | } // namespace webrtc |