andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 11 | #include "webrtc/video/rtp_video_stream_receiver.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 12 | |
eladalon | 2c1cbc1 | 2017-08-02 14:39:07 | [diff] [blame] | 13 | #include <algorithm> |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 14 | #include <utility> |
ilnik | 39e693a | 2017-06-19 14:18:55 | [diff] [blame] | 15 | #include <vector> |
mflodman@webrtc.org | cd1ac8b | 2013-02-06 17:46:39 | [diff] [blame] | 16 | |
Stefan Holmer | 36189cd | 2017-09-01 13:29:28 | [diff] [blame] | 17 | #include "webrtc/call/video_config.h" |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 18 | #include "webrtc/common_types.h" |
philipel | 09c99cf | 2016-12-20 12:15:59 | [diff] [blame] | 19 | #include "webrtc/media/base/mediaconstants.h" |
mflodman | 0900317 | 2016-04-25 08:26:26 | [diff] [blame] | 20 | #include "webrtc/modules/pacing/packet_router.h" |
pbos@webrtc.org | 281cff8 | 2013-05-17 13:44:48 | [diff] [blame] | 21 | #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
Henrik Kjellander | 36a14b5 | 2015-11-04 07:31:52 | [diff] [blame] | 22 | #include "webrtc/modules/rtp_rtcp/include/receive_statistics.h" |
Henrik Kjellander | 36a14b5 | 2015-11-04 07:31:52 | [diff] [blame] | 23 | #include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h" |
| 24 | #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h" |
Henrik Kjellander | 36a14b5 | 2015-11-04 07:31:52 | [diff] [blame] | 25 | #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
| 26 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
brandtr | 001769c | 2016-10-31 11:51:33 | [diff] [blame] | 27 | #include "webrtc/modules/rtp_rtcp/include/ulpfec_receiver.h" |
nisse | 6d5d814 | 2017-02-13 13:59:46 | [diff] [blame] | 28 | #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
| 29 | #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 30 | #include "webrtc/modules/video_coding/frame_object.h" |
philipel | 09c99cf | 2016-12-20 12:15:59 | [diff] [blame] | 31 | #include "webrtc/modules/video_coding/h264_sprop_parameter_sets.h" |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 32 | #include "webrtc/modules/video_coding/h264_sps_pps_tracker.h" |
| 33 | #include "webrtc/modules/video_coding/packet_buffer.h" |
Peter Boström | 64cd178 | 2016-04-22 16:23:15 | [diff] [blame] | 34 | #include "webrtc/modules/video_coding/video_coding_impl.h" |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 35 | #include "webrtc/rtc_base/checks.h" |
| 36 | #include "webrtc/rtc_base/location.h" |
| 37 | #include "webrtc/rtc_base/logging.h" |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 38 | #include "webrtc/system_wrappers/include/field_trial.h" |
Henrik Kjellander | 78f65d0 | 2015-10-28 17:17:40 | [diff] [blame] | 39 | #include "webrtc/system_wrappers/include/metrics.h" |
Henrik Kjellander | 78f65d0 | 2015-10-28 17:17:40 | [diff] [blame] | 40 | #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 41 | #include "webrtc/video/receive_statistics_proxy.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 42 | |
| 43 | namespace webrtc { |
| 44 | |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 45 | namespace { |
philipel | d8a48fb | 2017-08-10 16:10:59 | [diff] [blame] | 46 | // TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see: |
| 47 | // crbug.com/752886 |
| 48 | constexpr int kPacketBufferStartSize = 512; |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 49 | constexpr int kPacketBufferMaxSixe = 2048; |
| 50 | } |
| 51 | |
mflodman | 0900317 | 2016-04-25 08:26:26 | [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 | dbce297 | 2017-01-24 10:36:58 | [diff] [blame] | 57 | TransportSequenceNumberAllocator* transport_sequence_number_allocator) { |
mflodman | 0900317 | 2016-04-25 08:26:26 | [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 | 0900317 | 2016-04-25 08:26:26 | [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 | 86a285e | 2016-05-03 06:44:01 | [diff] [blame] | 72 | configuration.send_packet_observer = nullptr; |
mflodman | 0900317 | 2016-04-25 08:26:26 | [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 | 0900317 | 2016-04-25 08:26:26 | [diff] [blame] | 77 | rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound); |
| 78 | |
| 79 | return rtp_rtcp; |
| 80 | } |
| 81 | |
stefan@webrtc.org | 88085a1 | 2014-10-14 11:40:13 | [diff] [blame] | 82 | static const int kPacketLogIntervalMs = 10000; |
| 83 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 84 | RtpVideoStreamReceiver::RtpVideoStreamReceiver( |
mflodman | cdadcb1 | 2016-04-29 06:15:33 | [diff] [blame] | 85 | Transport* transport, |
| 86 | RtcpRttStats* rtt_stats, |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 87 | PacketRouter* packet_router, |
Tommi | 87875ef | 2016-06-10 15:58:01 | [diff] [blame] | 88 | const VideoReceiveStream::Config* config, |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 89 | ReceiveStatisticsProxy* receive_stats_proxy, |
Erik Språng | 0077997 | 2016-07-29 10:59:36 | [diff] [blame] | 90 | ProcessThread* process_thread, |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 91 | NackSender* nack_sender, |
| 92 | KeyFrameRequestSender* keyframe_request_sender, |
| 93 | video_coding::OnCompleteFrameCallback* complete_frame_callback, |
| 94 | VCMTiming* timing) |
Tommi | f93f04c | 2016-01-21 22:24:59 | [diff] [blame] | 95 | : clock_(Clock::GetRealTimeClock()), |
Tommi | 87875ef | 2016-06-10 15:58:01 | [diff] [blame] | 96 | config_(*config), |
mflodman | 0900317 | 2016-04-25 08:26:26 | [diff] [blame] | 97 | packet_router_(packet_router), |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 98 | process_thread_(process_thread), |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 99 | ntp_estimator_(clock_), |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 100 | rtp_header_parser_(RtpHeaderParser::Create()), |
| 101 | rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_, |
| 102 | this, |
mflodman | cdadcb1 | 2016-04-29 06:15:33 | [diff] [blame] | 103 | this, |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 104 | &rtp_payload_registry_)), |
| 105 | rtp_receive_statistics_(ReceiveStatistics::Create(clock_)), |
brandtr | 0e9e5a2 | 2017-06-29 09:45:35 | [diff] [blame] | 106 | ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)), |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 107 | receiving_(false), |
nisse | dfa8f5e | 2017-09-04 11:22:15 | [diff] [blame] | 108 | restored_packet_in_use_(false), |
mflodman | 0900317 | 2016-04-25 08:26:26 | [diff] [blame] | 109 | last_packet_log_ms_(-1), |
| 110 | rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(), |
| 111 | transport, |
| 112 | rtt_stats, |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 113 | receive_stats_proxy, |
nisse | dbce297 | 2017-01-24 10:36:58 | [diff] [blame] | 114 | packet_router)), |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 115 | complete_frame_callback_(complete_frame_callback), |
| 116 | keyframe_request_sender_(keyframe_request_sender), |
philipel | 22e0182 | 2017-05-16 15:06:30 | [diff] [blame] | 117 | timing_(timing), |
| 118 | has_received_frame_(false) { |
eladalon | a207a3a | 2017-08-01 13:30:28 | [diff] [blame] | 119 | constexpr bool remb_candidate = true; |
| 120 | packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate); |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 121 | rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy); |
| 122 | rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy); |
| 123 | |
Tommi | 87875ef | 2016-06-10 15:58:01 | [diff] [blame] | 124 | RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff) |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 125 | << "A stream should not be configured with RTCP disabled. This value is " |
| 126 | "reserved for internal usage."; |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [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 | 87875ef | 2016-06-10 15:58:01 | [diff] [blame] | 132 | rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode); |
| 133 | rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc); |
stefan | 9fe4191 | 2017-06-09 13:12:11 | [diff] [blame] | 134 | rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc); |
mflodman | 0900317 | 2016-04-25 08:26:26 | [diff] [blame] | 135 | rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp); |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 136 | |
Tommi | 87875ef | 2016-06-10 15:58:01 | [diff] [blame] | 137 | for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
| 138 | EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri, |
| 139 | config_.rtp.extensions[i].id); |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 140 | } |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 141 | |
| 142 | static const int kMaxPacketAgeToNack = 450; |
Tommi | 87875ef | 2016-06-10 15:58:01 | [diff] [blame] | 143 | const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0) |
| 144 | ? kMaxPacketAgeToNack |
| 145 | : kDefaultMaxReorderingThreshold; |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 146 | rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold); |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 147 | |
brandtr | bcf3fd3 | 2017-01-27 12:53:07 | [diff] [blame] | 148 | if (config_.rtp.rtx_ssrc) { |
| 149 | rtp_payload_registry_.SetRtxSsrc(config_.rtp.rtx_ssrc); |
nisse | dfa8f5e | 2017-09-04 11:22:15 | [diff] [blame] | 150 | |
| 151 | for (const auto& kv : config_.rtp.rtx_associated_payload_types) { |
| 152 | RTC_DCHECK_NE(kv.first, 0); |
| 153 | rtp_payload_registry_.SetRtxPayloadType(kv.first, kv.second); |
| 154 | } |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 155 | } |
| 156 | |
brandtr | cc24846 | 2016-12-08 16:25:47 | [diff] [blame] | 157 | if (IsUlpfecEnabled()) { |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 158 | VideoCodec ulpfec_codec = {}; |
| 159 | ulpfec_codec.codecType = kVideoCodecULPFEC; |
| 160 | strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName)); |
brandtr | d984c57 | 2016-10-05 06:28:39 | [diff] [blame] | 161 | ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type; |
johan | f3b4b21 | 2016-12-13 10:23:03 | [diff] [blame] | 162 | RTC_CHECK(AddReceiveCodec(ulpfec_codec)); |
brandtr | c1d9346 | 2016-11-11 11:28:30 | [diff] [blame] | 163 | } |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 164 | |
brandtr | c1d9346 | 2016-11-11 11:28:30 | [diff] [blame] | 165 | if (IsRedEnabled()) { |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 166 | VideoCodec red_codec = {}; |
| 167 | red_codec.codecType = kVideoCodecRED; |
| 168 | strncpy(red_codec.plName, "red", sizeof(red_codec.plName)); |
brandtr | d984c57 | 2016-10-05 06:28:39 | [diff] [blame] | 169 | red_codec.plType = config_.rtp.ulpfec.red_payload_type; |
johan | f3b4b21 | 2016-12-13 10:23:03 | [diff] [blame] | 170 | RTC_CHECK(AddReceiveCodec(red_codec)); |
nisse | dfa8f5e | 2017-09-04 11:22:15 | [diff] [blame] | 171 | if (config_.rtp.ulpfec.red_rtx_payload_type != -1) { |
| 172 | rtp_payload_registry_.SetRtxPayloadType( |
| 173 | config_.rtp.ulpfec.red_rtx_payload_type, |
| 174 | config_.rtp.ulpfec.red_payload_type); |
| 175 | } |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 176 | } |
| 177 | |
Tommi | 87875ef | 2016-06-10 15:58:01 | [diff] [blame] | 178 | if (config_.rtp.rtcp_xr.receiver_reference_time_report) |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 179 | rtp_rtcp_->SetRtcpXrRrtrStatus(true); |
| 180 | |
| 181 | // Stats callback for CNAME changes. |
| 182 | rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy); |
| 183 | |
tommi | c72b1c8 | 2017-03-03 11:20:24 | [diff] [blame] | 184 | process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 185 | |
tommi | 451d9c2 | 2017-02-27 09:59:36 | [diff] [blame] | 186 | if (config_.rtp.nack.rtp_history_ms != 0) { |
| 187 | nack_module_.reset( |
| 188 | new NackModule(clock_, nack_sender, keyframe_request_sender)); |
tommi | c72b1c8 | 2017-03-03 11:20:24 | [diff] [blame] | 189 | process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE); |
tommi | 451d9c2 | 2017-02-27 09:59:36 | [diff] [blame] | 190 | } |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 191 | |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 192 | packet_buffer_ = video_coding::PacketBuffer::Create( |
| 193 | clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this); |
| 194 | reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this)); |
mflodman | 0900317 | 2016-04-25 08:26:26 | [diff] [blame] | 195 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 196 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 197 | RtpVideoStreamReceiver::~RtpVideoStreamReceiver() { |
eladalon | 2c1cbc1 | 2017-08-02 14:39:07 | [diff] [blame] | 198 | RTC_DCHECK(secondary_sinks_.empty()); |
| 199 | |
tommi | 451d9c2 | 2017-02-27 09:59:36 | [diff] [blame] | 200 | if (nack_module_) { |
| 201 | process_thread_->DeRegisterModule(nack_module_.get()); |
| 202 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 203 | |
tommi | 451d9c2 | 2017-02-27 09:59:36 | [diff] [blame] | 204 | process_thread_->DeRegisterModule(rtp_rtcp_.get()); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 205 | |
nisse | 5e9233b | 2017-03-31 12:44:52 | [diff] [blame] | 206 | packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get()); |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 207 | UpdateHistograms(); |
asapersson@webrtc.org | 4935b80 | 2015-01-15 07:40:20 | [diff] [blame] | 208 | } |
| 209 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 210 | bool RtpVideoStreamReceiver::AddReceiveCodec( |
philipel | 09c99cf | 2016-12-20 12:15:59 | [diff] [blame] | 211 | const VideoCodec& video_codec, |
| 212 | const std::map<std::string, std::string>& codec_params) { |
| 213 | pt_codec_params_.insert(make_pair(video_codec.plType, codec_params)); |
| 214 | return AddReceiveCodec(video_codec); |
| 215 | } |
| 216 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 217 | bool RtpVideoStreamReceiver::AddReceiveCodec(const VideoCodec& video_codec) { |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 218 | int8_t old_pltype = -1; |
magjed | 4c1eb05 | 2016-11-24 17:34:46 | [diff] [blame] | 219 | if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) != |
| 220 | -1) { |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 221 | rtp_payload_registry_.DeRegisterReceivePayload(old_pltype); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 222 | } |
magjed | 4c1eb05 | 2016-11-24 17:34:46 | [diff] [blame] | 223 | return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 224 | } |
| 225 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 226 | uint32_t RtpVideoStreamReceiver::GetRemoteSsrc() const { |
stefan | 9fe4191 | 2017-06-09 13:12:11 | [diff] [blame] | 227 | return config_.rtp.remote_ssrc; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 228 | } |
| 229 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 230 | int RtpVideoStreamReceiver::GetCsrcs(uint32_t* csrcs) const { |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 231 | return rtp_receiver_->CSRCs(csrcs); |
| 232 | } |
| 233 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 234 | RtpReceiver* RtpVideoStreamReceiver::GetRtpReceiver() const { |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 235 | return rtp_receiver_.get(); |
| 236 | } |
| 237 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 238 | int32_t RtpVideoStreamReceiver::OnReceivedPayloadData( |
mflodman | cdadcb1 | 2016-04-29 06:15:33 | [diff] [blame] | 239 | const uint8_t* payload_data, |
Peter Boström | c9da01c | 2016-06-14 10:52:54 | [diff] [blame] | 240 | size_t payload_size, |
mflodman | cdadcb1 | 2016-04-29 06:15:33 | [diff] [blame] | 241 | const WebRtcRTPHeader* rtp_header) { |
wu@webrtc.org | 093fc0b | 2014-04-24 22:10:24 | [diff] [blame] | 242 | WebRtcRTPHeader rtp_header_with_ntp = *rtp_header; |
wu@webrtc.org | 2fae0d1 | 2014-05-14 16:53:51 | [diff] [blame] | 243 | rtp_header_with_ntp.ntp_time_ms = |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 244 | ntp_estimator_.Estimate(rtp_header->header.timestamp); |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 245 | VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp); |
tommi | 451d9c2 | 2017-02-27 09:59:36 | [diff] [blame] | 246 | packet.timesNacked = |
| 247 | nack_module_ ? nack_module_->OnReceivedPacket(packet) : -1; |
ilnik | 39e693a | 2017-06-19 14:18:55 | [diff] [blame] | 248 | packet.receive_time_ms = clock_->TimeInMilliseconds(); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 249 | |
philipel | 59601fc | 2017-03-21 12:45:18 | [diff] [blame] | 250 | // In the case of a video stream without picture ids and no rtx the |
| 251 | // RtpFrameReferenceFinder will need to know about padding to |
| 252 | // correctly calculate frame references. |
| 253 | if (packet.sizeBytes == 0) { |
| 254 | reference_finder_->PaddingReceived(packet.seqNum); |
philipel | e65f137 | 2017-06-13 09:47:28 | [diff] [blame] | 255 | packet_buffer_->PaddingReceived(packet.seqNum); |
philipel | 59601fc | 2017-03-21 12:45:18 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 259 | if (packet.codec == kVideoCodecH264) { |
| 260 | // Only when we start to receive packets will we know what payload type |
| 261 | // that will be used. When we know the payload type insert the correct |
| 262 | // sps/pps into the tracker. |
| 263 | if (packet.payloadType != last_payload_type_) { |
| 264 | last_payload_type_ = packet.payloadType; |
| 265 | InsertSpsPpsIntoTracker(packet.payloadType); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 266 | } |
| 267 | |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 268 | switch (tracker_.CopyAndFixBitstream(&packet)) { |
| 269 | case video_coding::H264SpsPpsTracker::kRequestKeyframe: |
| 270 | keyframe_request_sender_->RequestKeyFrame(); |
| 271 | FALLTHROUGH(); |
| 272 | case video_coding::H264SpsPpsTracker::kDrop: |
| 273 | return 0; |
| 274 | case video_coding::H264SpsPpsTracker::kInsert: |
| 275 | break; |
| 276 | } |
| 277 | |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 278 | } else { |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 279 | uint8_t* data = new uint8_t[packet.sizeBytes]; |
| 280 | memcpy(data, packet.dataPtr, packet.sizeBytes); |
| 281 | packet.dataPtr = data; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 282 | } |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 283 | |
| 284 | packet_buffer_->InsertPacket(&packet); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
nisse | ba6f478 | 2017-05-11 15:00:58 | [diff] [blame] | 288 | // TODO(nisse): Try to delete this method. Obstacles: It is used by |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 289 | // ParseAndHandleEncapsulatingHeader, for handling Rtx packets, and |
| 290 | // for callbacks from |ulpfec_receiver_|. |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 291 | void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet, |
| 292 | size_t rtp_packet_length) { |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 293 | RTPHeader header; |
| 294 | if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) { |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 295 | return; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 296 | } |
| 297 | header.payload_type_frequency = kVideoPayloadTypeFrequency; |
stefan@webrtc.org | d01c491 | 2014-09-04 06:48:14 | [diff] [blame] | 298 | bool in_order = IsPacketInOrder(header); |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 299 | ReceivePacket(rtp_packet, rtp_packet_length, header, in_order); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 300 | } |
| 301 | |
mflodman | cdadcb1 | 2016-04-29 06:15:33 | [diff] [blame] | 302 | // TODO(pbos): Remove as soon as audio can handle a changing payload type |
| 303 | // without this callback. |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 304 | int32_t RtpVideoStreamReceiver::OnInitializeDecoder( |
mflodman | cdadcb1 | 2016-04-29 06:15:33 | [diff] [blame] | 305 | const int8_t payload_type, |
| 306 | const char payload_name[RTP_PAYLOAD_NAME_SIZE], |
| 307 | const int frequency, |
| 308 | const size_t channels, |
| 309 | const uint32_t rate) { |
| 310 | RTC_NOTREACHED(); |
| 311 | return 0; |
| 312 | } |
| 313 | |
nisse | ba6f478 | 2017-05-11 15:00:58 | [diff] [blame] | 314 | // This method handles both regular RTP packets and packets recovered |
| 315 | // via FlexFEC. |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 316 | void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) { |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 317 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
eladalon | 2c1cbc1 | 2017-08-02 14:39:07 | [diff] [blame] | 318 | |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 319 | if (!receiving_) { |
| 320 | return; |
| 321 | } |
solenberg@webrtc.org | 800136d | 2014-02-11 15:27:49 | [diff] [blame] | 322 | |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 323 | if (!packet.recovered()) { |
| 324 | int64_t now_ms = clock_->TimeInMilliseconds(); |
stefan@webrtc.org | 88085a1 | 2014-10-14 11:40:13 | [diff] [blame] | 325 | |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 326 | // Periodically log the RTP header of incoming packets. |
| 327 | if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) { |
| 328 | std::stringstream ss; |
| 329 | ss << "Packet received on SSRC: " << packet.Ssrc() |
| 330 | << " with payload type: " << static_cast<int>(packet.PayloadType()) |
| 331 | << ", timestamp: " << packet.Timestamp() |
| 332 | << ", sequence number: " << packet.SequenceNumber() |
| 333 | << ", arrival time: " << packet.arrival_time_ms(); |
| 334 | int32_t time_offset; |
| 335 | if (packet.GetExtension<TransmissionOffset>(&time_offset)) { |
| 336 | ss << ", toffset: " << time_offset; |
nisse | 6d5d814 | 2017-02-13 13:59:46 | [diff] [blame] | 337 | } |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 338 | uint32_t send_time; |
| 339 | if (packet.GetExtension<AbsoluteSendTime>(&send_time)) { |
| 340 | ss << ", abs send time: " << send_time; |
| 341 | } |
| 342 | LOG(LS_INFO) << ss.str(); |
| 343 | last_packet_log_ms_ = now_ms; |
stefan@webrtc.org | 88085a1 | 2014-10-14 11:40:13 | [diff] [blame] | 344 | } |
| 345 | } |
wu@webrtc.org | efeb8ce | 2013-12-13 00:21:03 | [diff] [blame] | 346 | |
nisse | 6d5d814 | 2017-02-13 13:59:46 | [diff] [blame] | 347 | // TODO(nisse): Delete use of GetHeader, but needs refactoring of |
| 348 | // ReceivePacket and IncomingPacket methods below. |
| 349 | RTPHeader header; |
| 350 | packet.GetHeader(&header); |
| 351 | |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 352 | header.payload_type_frequency = kVideoPayloadTypeFrequency; |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 353 | |
stefan@webrtc.org | 7e97e4c | 2013-11-08 15:18:52 | [diff] [blame] | 354 | bool in_order = IsPacketInOrder(header); |
nisse | ba6f478 | 2017-05-11 15:00:58 | [diff] [blame] | 355 | if (!packet.recovered()) { |
| 356 | // TODO(nisse): Why isn't this done for recovered packets? |
| 357 | rtp_payload_registry_.SetIncomingPayloadType(header); |
| 358 | } |
nisse | 6d5d814 | 2017-02-13 13:59:46 | [diff] [blame] | 359 | ReceivePacket(packet.data(), packet.size(), header, in_order); |
asapersson@webrtc.org | 99153ba | 2014-05-26 13:06:04 | [diff] [blame] | 360 | // Update receive statistics after ReceivePacket. |
| 361 | // Receive statistics will be reset if the payload type changes (make sure |
| 362 | // that the first packet is included in the stats). |
nisse | ba6f478 | 2017-05-11 15:00:58 | [diff] [blame] | 363 | if (!packet.recovered()) { |
| 364 | // TODO(nisse): We should pass a recovered flag to stats, to aid |
| 365 | // fixing bug bugs.webrtc.org/6339. |
| 366 | rtp_receive_statistics_->IncomingPacket( |
| 367 | header, packet.size(), IsPacketRetransmitted(header, in_order)); |
| 368 | } |
eladalon | 2c1cbc1 | 2017-08-02 14:39:07 | [diff] [blame] | 369 | |
| 370 | for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) { |
| 371 | secondary_sink->OnRtpPacket(packet); |
| 372 | } |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 373 | } |
| 374 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 375 | int32_t RtpVideoStreamReceiver::RequestKeyFrame() { |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 376 | return rtp_rtcp_->RequestKeyFrame(); |
| 377 | } |
| 378 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 379 | bool RtpVideoStreamReceiver::IsUlpfecEnabled() const { |
brandtr | c1d9346 | 2016-11-11 11:28:30 | [diff] [blame] | 380 | return config_.rtp.ulpfec.ulpfec_payload_type != -1; |
| 381 | } |
| 382 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 383 | bool RtpVideoStreamReceiver::IsRedEnabled() const { |
brandtr | c1d9346 | 2016-11-11 11:28:30 | [diff] [blame] | 384 | return config_.rtp.ulpfec.red_payload_type != -1; |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 385 | } |
| 386 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 387 | bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const { |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 388 | return config_.rtp.nack.rtp_history_ms > 0; |
| 389 | } |
| 390 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 391 | void RtpVideoStreamReceiver::RequestPacketRetransmit( |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 392 | const std::vector<uint16_t>& sequence_numbers) { |
| 393 | rtp_rtcp_->SendNack(sequence_numbers); |
| 394 | } |
| 395 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 396 | int32_t RtpVideoStreamReceiver::ResendPackets(const uint16_t* sequence_numbers, |
| 397 | uint16_t length) { |
mflodman | 639986e | 2016-05-04 04:22:04 | [diff] [blame] | 398 | return rtp_rtcp_->SendNACK(sequence_numbers, length); |
| 399 | } |
| 400 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 401 | void RtpVideoStreamReceiver::OnReceivedFrame( |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 402 | std::unique_ptr<video_coding::RtpFrameObject> frame) { |
philipel | 22e0182 | 2017-05-16 15:06:30 | [diff] [blame] | 403 | if (!has_received_frame_) { |
| 404 | has_received_frame_ = true; |
| 405 | if (frame->FrameType() != kVideoFrameKey) |
| 406 | keyframe_request_sender_->RequestKeyFrame(); |
| 407 | } |
| 408 | |
philipel | 685e50e | 2017-01-29 12:04:47 | [diff] [blame] | 409 | if (!frame->delayed_by_retransmission()) |
| 410 | timing_->IncomingTimestamp(frame->timestamp, clock_->TimeInMilliseconds()); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 411 | reference_finder_->ManageFrame(std::move(frame)); |
| 412 | } |
| 413 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 414 | void RtpVideoStreamReceiver::OnCompleteFrame( |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 415 | std::unique_ptr<video_coding::FrameObject> frame) { |
| 416 | { |
| 417 | rtc::CritScope lock(&last_seq_num_cs_); |
| 418 | video_coding::RtpFrameObject* rtp_frame = |
| 419 | static_cast<video_coding::RtpFrameObject*>(frame.get()); |
| 420 | last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num(); |
| 421 | } |
| 422 | complete_frame_callback_->OnCompleteFrame(std::move(frame)); |
| 423 | } |
| 424 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 425 | void RtpVideoStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms, |
| 426 | int64_t max_rtt_ms) { |
tommi | 451d9c2 | 2017-02-27 09:59:36 | [diff] [blame] | 427 | if (nack_module_) |
| 428 | nack_module_->UpdateRtt(max_rtt_ms); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 429 | } |
| 430 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 431 | rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const { |
philipel | 47be53a | 2017-05-18 15:08:53 | [diff] [blame] | 432 | return packet_buffer_->LastReceivedPacketMs(); |
| 433 | } |
| 434 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 435 | rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs() |
| 436 | const { |
philipel | 47be53a | 2017-05-18 15:08:53 | [diff] [blame] | 437 | return packet_buffer_->LastReceivedKeyframePacketMs(); |
| 438 | } |
| 439 | |
eladalon | 2c1cbc1 | 2017-08-02 14:39:07 | [diff] [blame] | 440 | void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) { |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 441 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
eladalon | 2c1cbc1 | 2017-08-02 14:39:07 | [diff] [blame] | 442 | RTC_DCHECK(std::find(secondary_sinks_.cbegin(), secondary_sinks_.cend(), |
| 443 | sink) == secondary_sinks_.cend()); |
| 444 | secondary_sinks_.push_back(sink); |
| 445 | } |
| 446 | |
| 447 | void RtpVideoStreamReceiver::RemoveSecondarySink( |
| 448 | const RtpPacketSinkInterface* sink) { |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 449 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
eladalon | 2c1cbc1 | 2017-08-02 14:39:07 | [diff] [blame] | 450 | auto it = std::find(secondary_sinks_.begin(), secondary_sinks_.end(), sink); |
| 451 | if (it == secondary_sinks_.end()) { |
| 452 | // We might be rolling-back a call whose setup failed mid-way. In such a |
| 453 | // case, it's simpler to remove "everything" rather than remember what |
| 454 | // has already been added. |
| 455 | LOG(LS_WARNING) << "Removal of unknown sink."; |
| 456 | return; |
| 457 | } |
| 458 | secondary_sinks_.erase(it); |
| 459 | } |
| 460 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 461 | void RtpVideoStreamReceiver::ReceivePacket(const uint8_t* packet, |
| 462 | size_t packet_length, |
| 463 | const RTPHeader& header, |
| 464 | bool in_order) { |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 465 | if (rtp_payload_registry_.IsEncapsulated(header)) { |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 466 | ParseAndHandleEncapsulatingHeader(packet, packet_length, header); |
| 467 | return; |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 468 | } |
| 469 | const uint8_t* payload = packet + header.headerLength; |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 470 | assert(packet_length >= header.headerLength); |
| 471 | size_t payload_length = packet_length - header.headerLength; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 472 | PayloadUnion payload_specific; |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 473 | if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType, |
| 474 | &payload_specific)) { |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 475 | return; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 476 | } |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 477 | rtp_receiver_->IncomingRtpPacket(header, payload, payload_length, |
| 478 | payload_specific, in_order); |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 479 | } |
| 480 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 481 | void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader( |
mflodman | cdadcb1 | 2016-04-29 06:15:33 | [diff] [blame] | 482 | const uint8_t* packet, size_t packet_length, const RTPHeader& header) { |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 483 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 484 | if (rtp_payload_registry_.IsRed(header)) { |
| 485 | int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type(); |
asapersson@webrtc.org | 413575d | 2015-01-28 13:58:27 | [diff] [blame] | 486 | if (packet[header.headerLength] == ulpfec_pt) { |
asapersson@webrtc.org | 08c57f7 | 2015-01-27 12:17:29 | [diff] [blame] | 487 | rtp_receive_statistics_->FecPacketReceived(header, packet_length); |
Peter Boström | 64cd178 | 2016-04-22 16:23:15 | [diff] [blame] | 488 | // Notify video_receiver about received FEC packets to avoid NACKing these |
| 489 | // packets. |
asapersson@webrtc.org | 413575d | 2015-01-28 13:58:27 | [diff] [blame] | 490 | NotifyReceiverOfFecPacket(header); |
| 491 | } |
brandtr | 001769c | 2016-10-31 11:51:33 | [diff] [blame] | 492 | if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length, |
| 493 | ulpfec_pt) != 0) { |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 494 | return; |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 495 | } |
nisse | 032d66e | 2017-05-29 15:16:37 | [diff] [blame] | 496 | ulpfec_receiver_->ProcessReceivedFec(); |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 497 | } else if (rtp_payload_registry_.IsRtx(header)) { |
nisse | dfa8f5e | 2017-09-04 11:22:15 | [diff] [blame] | 498 | if (header.headerLength + header.paddingLength == packet_length) { |
| 499 | // This is an empty packet and should be silently dropped before trying to |
| 500 | // parse the RTX header. |
| 501 | return; |
| 502 | } |
| 503 | // Remove the RTX header and parse the original RTP header. |
| 504 | if (packet_length < header.headerLength) |
| 505 | return; |
| 506 | if (packet_length > sizeof(restored_packet_)) |
| 507 | return; |
| 508 | if (restored_packet_in_use_) { |
| 509 | LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet."; |
| 510 | return; |
| 511 | } |
| 512 | if (!rtp_payload_registry_.RestoreOriginalPacket( |
| 513 | restored_packet_, packet, &packet_length, config_.rtp.remote_ssrc, |
| 514 | header)) { |
| 515 | LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: " |
| 516 | << header.ssrc << " payload type: " |
| 517 | << static_cast<int>(header.payloadType); |
| 518 | return; |
| 519 | } |
| 520 | restored_packet_in_use_ = true; |
| 521 | OnRecoveredPacket(restored_packet_, packet_length); |
| 522 | restored_packet_in_use_ = false; |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 523 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 524 | } |
| 525 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 526 | void RtpVideoStreamReceiver::NotifyReceiverOfFecPacket( |
| 527 | const RTPHeader& header) { |
asapersson@webrtc.org | 413575d | 2015-01-28 13:58:27 | [diff] [blame] | 528 | int8_t last_media_payload_type = |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 529 | rtp_payload_registry_.last_received_media_payload_type(); |
asapersson@webrtc.org | 413575d | 2015-01-28 13:58:27 | [diff] [blame] | 530 | if (last_media_payload_type < 0) { |
| 531 | LOG(LS_WARNING) << "Failed to get last media payload type."; |
| 532 | return; |
| 533 | } |
| 534 | // Fake an empty media packet. |
| 535 | WebRtcRTPHeader rtp_header = {}; |
| 536 | rtp_header.header = header; |
| 537 | rtp_header.header.payloadType = last_media_payload_type; |
| 538 | rtp_header.header.paddingLength = 0; |
| 539 | PayloadUnion payload_specific; |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 540 | if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type, |
| 541 | &payload_specific)) { |
asapersson@webrtc.org | 413575d | 2015-01-28 13:58:27 | [diff] [blame] | 542 | LOG(LS_WARNING) << "Failed to get payload specifics."; |
| 543 | return; |
| 544 | } |
| 545 | rtp_header.type.Video.codec = payload_specific.Video.videoCodecType; |
guoweis@webrtc.org | 42e1d11 | 2015-03-12 20:50:57 | [diff] [blame] | 546 | rtp_header.type.Video.rotation = kVideoRotation_0; |
| 547 | if (header.extension.hasVideoRotation) { |
magjed | 78706f4 | 2016-09-08 10:24:58 | [diff] [blame] | 548 | rtp_header.type.Video.rotation = header.extension.videoRotation; |
guoweis@webrtc.org | 42e1d11 | 2015-03-12 20:50:57 | [diff] [blame] | 549 | } |
ilnik | cb436f6 | 2017-04-11 17:34:31 | [diff] [blame] | 550 | rtp_header.type.Video.content_type = VideoContentType::UNSPECIFIED; |
| 551 | if (header.extension.hasVideoContentType) { |
| 552 | rtp_header.type.Video.content_type = header.extension.videoContentType; |
| 553 | } |
ilnik | 39e693a | 2017-06-19 14:18:55 | [diff] [blame] | 554 | rtp_header.type.Video.video_timing = {0u, 0u, 0u, 0u, 0u, 0u, false}; |
| 555 | if (header.extension.has_video_timing) { |
| 556 | rtp_header.type.Video.video_timing = header.extension.video_timing; |
ilnik | 39e693a | 2017-06-19 14:18:55 | [diff] [blame] | 557 | } |
isheriff | 00cc045 | 2016-06-08 07:24:21 | [diff] [blame] | 558 | rtp_header.type.Video.playout_delay = header.extension.playout_delay; |
| 559 | |
Peter Boström | 81e36b5 | 2016-04-04 15:56:10 | [diff] [blame] | 560 | OnReceivedPayloadData(nullptr, 0, &rtp_header); |
asapersson@webrtc.org | 413575d | 2015-01-28 13:58:27 | [diff] [blame] | 561 | } |
| 562 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 563 | bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet, |
| 564 | size_t rtcp_packet_length) { |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 565 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
| 566 | |
| 567 | if (!receiving_) { |
| 568 | return false; |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 569 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 570 | |
Per | cd99765 | 2016-04-15 12:59:13 | [diff] [blame] | 571 | rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length); |
wu@webrtc.org | 093fc0b | 2014-04-24 22:10:24 | [diff] [blame] | 572 | |
pkasting@chromium.org | a3166c4 | 2015-01-12 21:51:21 | [diff] [blame] | 573 | int64_t rtt = 0; |
Per | cd99765 | 2016-04-15 12:59:13 | [diff] [blame] | 574 | rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr); |
minyue@webrtc.org | 1e16841 | 2014-10-09 10:52:43 | [diff] [blame] | 575 | if (rtt == 0) { |
| 576 | // Waiting for valid rtt. |
Peter Boström | 70ff7ff | 2016-02-08 13:07:14 | [diff] [blame] | 577 | return true; |
minyue@webrtc.org | 1e16841 | 2014-10-09 10:52:43 | [diff] [blame] | 578 | } |
| 579 | uint32_t ntp_secs = 0; |
| 580 | uint32_t ntp_frac = 0; |
| 581 | uint32_t rtp_timestamp = 0; |
Per | cd99765 | 2016-04-15 12:59:13 | [diff] [blame] | 582 | if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr, |
| 583 | &rtp_timestamp) != 0) { |
minyue@webrtc.org | 1e16841 | 2014-10-09 10:52:43 | [diff] [blame] | 584 | // Waiting for RTCP. |
Peter Boström | 70ff7ff | 2016-02-08 13:07:14 | [diff] [blame] | 585 | return true; |
minyue@webrtc.org | 1e16841 | 2014-10-09 10:52:43 | [diff] [blame] | 586 | } |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 587 | ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp); |
wu@webrtc.org | 093fc0b | 2014-04-24 22:10:24 | [diff] [blame] | 588 | |
Peter Boström | 70ff7ff | 2016-02-08 13:07:14 | [diff] [blame] | 589 | return true; |
wu@webrtc.org | 093fc0b | 2014-04-24 22:10:24 | [diff] [blame] | 590 | } |
| 591 | |
philipel | 19cf6be | 2017-09-04 14:03:46 | [diff] [blame] | 592 | void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) { |
tommi | 451d9c2 | 2017-02-27 09:59:36 | [diff] [blame] | 593 | if (!nack_module_) |
| 594 | return; |
| 595 | |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 596 | int seq_num = -1; |
| 597 | { |
| 598 | rtc::CritScope lock(&last_seq_num_cs_); |
| 599 | auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id); |
| 600 | if (seq_num_it != last_seq_num_for_pic_id_.end()) |
| 601 | seq_num = seq_num_it->second; |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 602 | } |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 603 | if (seq_num != -1) |
| 604 | nack_module_->ClearUpTo(seq_num); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 605 | } |
| 606 | |
philipel | 19cf6be | 2017-09-04 14:03:46 | [diff] [blame] | 607 | void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) { |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 608 | int seq_num = -1; |
| 609 | { |
| 610 | rtc::CritScope lock(&last_seq_num_cs_); |
| 611 | auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id); |
| 612 | if (seq_num_it != last_seq_num_for_pic_id_.end()) { |
| 613 | seq_num = seq_num_it->second; |
| 614 | last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(), |
| 615 | ++seq_num_it); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 616 | } |
philipel | a7cca31 | 2017-02-22 13:30:39 | [diff] [blame] | 617 | } |
| 618 | if (seq_num != -1) { |
| 619 | packet_buffer_->ClearTo(seq_num); |
| 620 | reference_finder_->ClearTo(seq_num); |
philipel | ba3a19d | 2016-11-15 08:57:57 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 624 | void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) { |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 625 | rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode |
| 626 | : RtcpMode::kOff); |
| 627 | } |
| 628 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 629 | void RtpVideoStreamReceiver::StartReceive() { |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 630 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 631 | receiving_ = true; |
| 632 | } |
| 633 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 634 | void RtpVideoStreamReceiver::StopReceive() { |
eladalon | 7f62201 | 2017-08-25 07:49:08 | [diff] [blame] | 635 | RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 636 | receiving_ = false; |
| 637 | } |
| 638 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 639 | bool RtpVideoStreamReceiver::IsPacketInOrder(const RTPHeader& header) const { |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 640 | StreamStatistician* statistician = |
| 641 | rtp_receive_statistics_->GetStatistician(header.ssrc); |
| 642 | if (!statistician) |
| 643 | return false; |
| 644 | return statistician->IsPacketInOrder(header.sequenceNumber); |
| 645 | } |
| 646 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 647 | bool RtpVideoStreamReceiver::IsPacketRetransmitted(const RTPHeader& header, |
| 648 | bool in_order) const { |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 649 | // Retransmissions are handled separately if RTX is enabled. |
Peter Boström | e5d4a26 | 2016-03-02 14:05:53 | [diff] [blame] | 650 | if (rtp_payload_registry_.RtxEnabled()) |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 651 | return false; |
| 652 | StreamStatistician* statistician = |
| 653 | rtp_receive_statistics_->GetStatistician(header.ssrc); |
| 654 | if (!statistician) |
| 655 | return false; |
| 656 | // Check if this is a retransmission. |
pkasting@chromium.org | a3166c4 | 2015-01-12 21:51:21 | [diff] [blame] | 657 | int64_t min_rtt = 0; |
stefan | 9fe4191 | 2017-06-09 13:12:11 | [diff] [blame] | 658 | rtp_rtcp_->RTT(config_.rtp.remote_ssrc, nullptr, nullptr, &min_rtt, nullptr); |
stefan@webrtc.org | 7e97e4c | 2013-11-08 15:18:52 | [diff] [blame] | 659 | return !in_order && |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 660 | statistician->IsRetransmitOfOldPacket(header, min_rtt); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 661 | } |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 662 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 663 | void RtpVideoStreamReceiver::UpdateHistograms() { |
brandtr | 001769c | 2016-10-31 11:51:33 | [diff] [blame] | 664 | FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter(); |
asapersson | fc3c29b | 2016-11-30 09:42:26 | [diff] [blame] | 665 | if (counter.first_packet_time_ms == -1) |
| 666 | return; |
| 667 | |
| 668 | int64_t elapsed_sec = |
| 669 | (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000; |
| 670 | if (elapsed_sec < metrics::kMinRunTimeInSeconds) |
| 671 | return; |
| 672 | |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 673 | if (counter.num_packets > 0) { |
asapersson | 56fc637 | 2016-09-10 05:40:25 | [diff] [blame] | 674 | RTC_HISTOGRAM_PERCENTAGE( |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 675 | "WebRTC.Video.ReceivedFecPacketsInPercent", |
| 676 | static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets)); |
| 677 | } |
| 678 | if (counter.num_fec_packets > 0) { |
asapersson | 56fc637 | 2016-09-10 05:40:25 | [diff] [blame] | 679 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec", |
| 680 | static_cast<int>(counter.num_recovered_packets * |
| 681 | 100 / counter.num_fec_packets)); |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 685 | void RtpVideoStreamReceiver::EnableReceiveRtpHeaderExtension( |
mflodman | 33927c7 | 2016-05-06 12:32:22 | [diff] [blame] | 686 | const std::string& extension, int id) { |
| 687 | // One-byte-extension local identifiers are in the range 1-14 inclusive. |
| 688 | RTC_DCHECK_GE(id, 1); |
| 689 | RTC_DCHECK_LE(id, 14); |
| 690 | RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); |
| 691 | RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension( |
| 692 | StringToRtpExtensionType(extension), id)); |
| 693 | } |
| 694 | |
nisse | 14ec7e0 | 2017-06-09 11:01:55 | [diff] [blame] | 695 | void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) { |
philipel | 09c99cf | 2016-12-20 12:15:59 | [diff] [blame] | 696 | auto codec_params_it = pt_codec_params_.find(payload_type); |
| 697 | if (codec_params_it == pt_codec_params_.end()) |
| 698 | return; |
| 699 | |
| 700 | LOG(LS_INFO) << "Found out of band supplied codec parameters for" |
johan | 900d66d | 2017-01-24 12:38:27 | [diff] [blame] | 701 | << " payload type: " << static_cast<int>(payload_type); |
philipel | 09c99cf | 2016-12-20 12:15:59 | [diff] [blame] | 702 | |
| 703 | H264SpropParameterSets sprop_decoder; |
| 704 | auto sprop_base64_it = |
| 705 | codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets); |
| 706 | |
| 707 | if (sprop_base64_it == codec_params_it->second.end()) |
| 708 | return; |
| 709 | |
johan | 900d66d | 2017-01-24 12:38:27 | [diff] [blame] | 710 | if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str())) |
philipel | 09c99cf | 2016-12-20 12:15:59 | [diff] [blame] | 711 | return; |
| 712 | |
johan | 684009a | 2017-01-24 10:38:17 | [diff] [blame] | 713 | tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(), |
| 714 | sprop_decoder.pps_nalu()); |
philipel | 09c99cf | 2016-12-20 12:15:59 | [diff] [blame] | 715 | } |
| 716 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 717 | } // namespace webrtc |