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