blob: 0fd6cda413cbd020c5979b8c9bda133ca4ee695e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org39e96592012-03-01 18:22:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000013#include <vector>
14
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000015#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000016#include "webrtc/modules/rtp_rtcp/interface/fec_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000017#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000018#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000021#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
22#include "webrtc/modules/utility/interface/rtp_dump.h"
23#include "webrtc/modules/video_coding/main/interface/video_coding.h"
24#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
wu@webrtc.orgcd701192014-04-24 22:10:24 +000025#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000026#include "webrtc/system_wrappers/interface/tick_util.h"
wu@webrtc.org66773a02014-05-07 17:09:44 +000027#include "webrtc/system_wrappers/interface/timestamp_extrapolator.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
29namespace webrtc {
30
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000031ViEReceiver::ViEReceiver(const int32_t channel_id,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000032 VideoCodingModule* module_vcm,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000033 RemoteBitrateEstimator* remote_bitrate_estimator,
34 RtpFeedback* rtp_feedback)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000035 : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000036 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037 rtp_payload_registry_(new RTPPayloadRegistry(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000038 RTPPayloadStrategy::CreateStrategy(false))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000039 rtp_receiver_(RtpReceiver::CreateVideoReceiver(
40 channel_id, Clock::GetRealTimeClock(), this, rtp_feedback,
41 rtp_payload_registry_.get())),
42 rtp_receive_statistics_(ReceiveStatistics::Create(
43 Clock::GetRealTimeClock())),
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000044 fec_receiver_(FecReceiver::Create(this)),
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000045 rtp_rtcp_(NULL),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000046 vcm_(module_vcm),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000047 remote_bitrate_estimator_(remote_bitrate_estimator),
wu@webrtc.orgcd701192014-04-24 22:10:24 +000048 clock_(Clock::GetRealTimeClock()),
wu@webrtc.orged4cb562014-05-06 04:50:49 +000049 ts_extrapolator_(
wu@webrtc.org66773a02014-05-07 17:09:44 +000050 new TimestampExtrapolator(clock_->TimeInMilliseconds())),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000051 rtp_dump_(NULL),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000052 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +000053 restored_packet_in_use_(false),
54 receiving_ast_enabled_(false) {
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000055 assert(remote_bitrate_estimator);
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000058ViEReceiver::~ViEReceiver() {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000059 if (rtp_dump_) {
60 rtp_dump_->Stop();
61 RtpDump::DestroyRtpDump(rtp_dump_);
62 rtp_dump_ = NULL;
63 }
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
65
wu@webrtc.org822fbd82013-08-15 23:38:54 +000066bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
67 int8_t old_pltype = -1;
68 if (rtp_payload_registry_->ReceivePayloadType(video_codec.plName,
69 kVideoPayloadTypeFrequency,
70 0,
71 video_codec.maxBitrate,
72 &old_pltype) != -1) {
73 rtp_payload_registry_->DeRegisterReceivePayload(old_pltype);
74 }
75
76 return RegisterPayload(video_codec);
77}
78
79bool ViEReceiver::RegisterPayload(const VideoCodec& video_codec) {
80 return rtp_receiver_->RegisterReceivePayload(video_codec.plName,
81 video_codec.plType,
82 kVideoPayloadTypeFrequency,
83 0,
84 video_codec.maxBitrate) == 0;
85}
86
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000087void ViEReceiver::SetNackStatus(bool enable,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000088 int max_nack_reordering_threshold) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000089 if (!enable) {
90 // Reset the threshold back to the lower default threshold when NACK is
91 // disabled since we no longer will be receiving retransmissions.
92 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold;
93 }
94 rtp_receive_statistics_->SetMaxReorderingThreshold(
95 max_nack_reordering_threshold);
96 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097}
98
99void ViEReceiver::SetRtxStatus(bool enable, uint32_t ssrc) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000100 rtp_payload_registry_->SetRtxStatus(enable, ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000101}
102
103void ViEReceiver::SetRtxPayloadType(uint32_t payload_type) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000104 rtp_payload_registry_->SetRtxPayloadType(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000105}
106
107uint32_t ViEReceiver::GetRemoteSsrc() const {
108 return rtp_receiver_->SSRC();
109}
110
111int ViEReceiver::GetCsrcs(uint32_t* csrcs) const {
112 return rtp_receiver_->CSRCs(csrcs);
113}
114
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000115void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
116 rtp_rtcp_ = module;
117}
118
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000119RtpReceiver* ViEReceiver::GetRtpReceiver() const {
120 return rtp_receiver_.get();
121}
122
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000123void ViEReceiver::RegisterSimulcastRtpRtcpModules(
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000124 const std::list<RtpRtcp*>& rtp_modules) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000125 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000126 rtp_rtcp_simulcast_.clear();
127
128 if (!rtp_modules.empty()) {
129 rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
130 rtp_modules.begin(),
131 rtp_modules.end());
132 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000133}
134
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000135bool ViEReceiver::SetReceiveTimestampOffsetStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000136 if (enable) {
137 return rtp_header_parser_->RegisterRtpHeaderExtension(
138 kRtpExtensionTransmissionTimeOffset, id);
139 } else {
140 return rtp_header_parser_->DeregisterRtpHeaderExtension(
141 kRtpExtensionTransmissionTimeOffset);
142 }
143}
144
stefan@webrtc.org08994cc2013-05-29 13:28:21 +0000145bool ViEReceiver::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000146 if (enable) {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000147 if (rtp_header_parser_->RegisterRtpHeaderExtension(
148 kRtpExtensionAbsoluteSendTime, id)) {
149 receiving_ast_enabled_ = true;
150 return true;
151 } else {
152 return false;
153 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000154 } else {
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000155 receiving_ast_enabled_ = false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000156 return rtp_header_parser_->DeregisterRtpHeaderExtension(
157 kRtpExtensionAbsoluteSendTime);
158 }
159}
160
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000161int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000162 int rtp_packet_length,
163 const PacketTime& packet_time) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000164 return InsertRTPPacket(static_cast<const uint8_t*>(rtp_packet),
wu@webrtc.orga9890802013-12-13 00:21:03 +0000165 rtp_packet_length, packet_time);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000166}
167
168int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
169 int rtcp_packet_length) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000170 return InsertRTCPPacket(static_cast<const uint8_t*>(rtcp_packet),
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000171 rtcp_packet_length);
172}
173
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000174int32_t ViEReceiver::OnReceivedPayloadData(
175 const uint8_t* payload_data, const uint16_t payload_size,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000176 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000177 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
178 CalculateCaptureNtpTime(&rtp_header_with_ntp);
179 if (vcm_->IncomingPacket(payload_data,
180 payload_size,
181 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000182 // Check this...
183 return -1;
184 }
185 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000188void ViEReceiver::CalculateCaptureNtpTime(WebRtcRTPHeader* rtp_header) {
189 if (rtcp_list_.size() < 2) {
190 // We need two RTCP SR reports to calculate NTP.
191 return;
192 }
193
194 int64_t sender_capture_ntp_ms = 0;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000195 if (!RtpToNtpMs(rtp_header->header.timestamp,
196 rtcp_list_,
197 &sender_capture_ntp_ms)) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000198 return;
199 }
200 uint32_t timestamp = sender_capture_ntp_ms * 90;
201 int64_t receiver_capture_ms =
202 ts_extrapolator_->ExtrapolateLocalTime(timestamp);
203 int64_t ntp_offset =
204 clock_->CurrentNtpInMilliseconds() - clock_->TimeInMilliseconds();
205 rtp_header->ntp_time_ms = receiver_capture_ms + ntp_offset;
206}
207
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000208bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
209 int rtp_packet_length) {
210 RTPHeader header;
211 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000212 return false;
213 }
214 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000215 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000216}
217
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000218void ViEReceiver::ReceivedBWEPacket(
219 int64_t arrival_time_ms, int payload_size, const RTPHeader& header) {
220 // Only forward if the incoming packet *and* the channel are both configured
221 // to receive absolute sender time. RTP time stamps may have different rates
222 // for audio and video and shouldn't be mixed.
223 if (header.extension.hasAbsoluteSendTime && receiving_ast_enabled_) {
224 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
225 header);
226 }
227}
228
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000229int ViEReceiver::InsertRTPPacket(const uint8_t* rtp_packet,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000230 int rtp_packet_length,
231 const PacketTime& packet_time) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000232 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000233 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000234 if (!receiving_) {
235 return -1;
236 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000237 if (rtp_dump_) {
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000238 rtp_dump_->DumpPacket(rtp_packet,
239 static_cast<uint16_t>(rtp_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000240 }
241 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000242
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000243 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000244 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000245 &header)) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000246 return -1;
247 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000248 int payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000249 int64_t arrival_time_ms;
250 if (packet_time.timestamp != -1)
251 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
252 else
253 arrival_time_ms = TickTime::MillisecondTimestamp();
254
255 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000256 payload_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000257 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000258
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000259 bool in_order = IsPacketInOrder(header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000260 rtp_receive_statistics_->IncomingPacket(
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000261 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000262 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000263 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order)
264 ? 0
265 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000266}
267
268bool ViEReceiver::ReceivePacket(const uint8_t* packet,
269 int packet_length,
270 const RTPHeader& header,
271 bool in_order) {
272 if (rtp_payload_registry_->IsEncapsulated(header)) {
273 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
274 }
275 const uint8_t* payload = packet + header.headerLength;
276 int payload_length = packet_length - header.headerLength;
277 assert(payload_length >= 0);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000278 PayloadUnion payload_specific;
279 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
280 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000281 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000282 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000283 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
284 payload_specific, in_order);
285}
286
287bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
288 int packet_length,
289 const RTPHeader& header) {
290 if (rtp_payload_registry_->IsRed(header)) {
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000291 int8_t ulpfec_pt = rtp_payload_registry_->ulpfec_payload_type();
292 if (packet[header.headerLength] == ulpfec_pt)
293 rtp_receive_statistics_->FecPacketReceived(header.ssrc);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000294 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000295 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000296 return false;
297 }
298 return fec_receiver_->ProcessReceivedFec() == 0;
299 } else if (rtp_payload_registry_->IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000300 if (header.headerLength + header.paddingLength == packet_length) {
301 // This is an empty packet and should be silently dropped before trying to
302 // parse the RTX header.
303 return true;
304 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000305 // Remove the RTX header and parse the original RTP header.
306 if (packet_length < header.headerLength)
307 return false;
308 if (packet_length > static_cast<int>(sizeof(restored_packet_)))
309 return false;
310 CriticalSectionScoped cs(receive_cs_.get());
311 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000312 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000313 return false;
314 }
315 uint8_t* restored_packet_ptr = restored_packet_;
316 if (!rtp_payload_registry_->RestoreOriginalPacket(
317 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
318 header)) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000319 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320 return false;
321 }
322 restored_packet_in_use_ = true;
323 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
324 restored_packet_in_use_ = false;
325 return ret;
326 }
327 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000328}
329
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000330int ViEReceiver::InsertRTCPPacket(const uint8_t* rtcp_packet,
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000331 int rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000332 {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000333 CriticalSectionScoped cs(receive_cs_.get());
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000334 if (!receiving_) {
335 return -1;
336 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000337
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000338 if (rtp_dump_) {
339 rtp_dump_->DumpPacket(
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000340 rtcp_packet, static_cast<uint16_t>(rtcp_packet_length));
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000341 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000342
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000343 std::list<RtpRtcp*>::iterator it = rtp_rtcp_simulcast_.begin();
344 while (it != rtp_rtcp_simulcast_.end()) {
345 RtpRtcp* rtp_rtcp = *it++;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000346 rtp_rtcp->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000347 }
348 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000349 assert(rtp_rtcp_); // Should be set by owner at construction time.
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000350 int ret = rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
351 if (ret != 0) {
352 return ret;
353 }
354
355 if (!GetRtcpTimestamp()) {
356 LOG(LS_WARNING) << "Failed to retrieve timestamp information from RTCP SR.";
357 }
358
359 return 0;
360}
361
362bool ViEReceiver::GetRtcpTimestamp() {
363 uint16_t rtt = 0;
364 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, NULL, NULL, NULL);
365 if (rtt == 0) {
366 // Waiting for valid rtt.
367 return true;
368 }
369
370 // Update RTCP list
371 uint32_t ntp_secs = 0;
372 uint32_t ntp_frac = 0;
373 uint32_t rtp_timestamp = 0;
374 if (0 != rtp_rtcp_->RemoteNTP(&ntp_secs,
375 &ntp_frac,
376 NULL,
377 NULL,
378 &rtp_timestamp)) {
379 return false;
380 }
381
382 bool new_rtcp_sr = false;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000383 if (!UpdateRtcpList(ntp_secs,
384 ntp_frac,
385 rtp_timestamp,
386 &rtcp_list_,
387 &new_rtcp_sr)) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000388 return false;
389 }
390
391 if (!new_rtcp_sr) {
392 // No new RTCP SR since last time this function was called.
393 return true;
394 }
395
396 // Update extrapolator with the new arrival time.
397 // The extrapolator assumes the TimeInMilliseconds time.
398 int64_t receiver_arrival_time = clock_->TimeInMilliseconds();
399 int64_t sender_send_time_ms = Clock::NtpToMs(ntp_secs, ntp_frac);
400 int64_t sender_arrival_time_90k = (sender_send_time_ms + rtt / 2) * 90;
401 ts_extrapolator_->Update(receiver_arrival_time, sender_arrival_time_90k);
402 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000404
405void ViEReceiver::StartReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000406 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000407 receiving_ = true;
408}
409
410void ViEReceiver::StopReceive() {
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000411 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000412 receiving_ = false;
413}
414
415int ViEReceiver::StartRTPDump(const char file_nameUTF8[1024]) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000416 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000417 if (rtp_dump_) {
418 // Restart it if it already exists and is started
419 rtp_dump_->Stop();
420 } else {
421 rtp_dump_ = RtpDump::CreateRtpDump();
422 if (rtp_dump_ == NULL) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000423 return -1;
424 }
425 }
426 if (rtp_dump_->Start(file_nameUTF8) != 0) {
427 RtpDump::DestroyRtpDump(rtp_dump_);
428 rtp_dump_ = NULL;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000429 return -1;
430 }
431 return 0;
432}
433
434int ViEReceiver::StopRTPDump() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000435 CriticalSectionScoped cs(receive_cs_.get());
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000436 if (rtp_dump_) {
437 if (rtp_dump_->IsActive()) {
438 rtp_dump_->Stop();
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000439 }
440 RtpDump::DestroyRtpDump(rtp_dump_);
441 rtp_dump_ = NULL;
442 } else {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000443 return -1;
444 }
445 return 0;
446}
447
jiayl@webrtc.org1f64f062014-02-10 19:12:14 +0000448void ViEReceiver::GetReceiveBandwidthEstimatorStats(
449 ReceiveBandwidthEstimatorStats* output) const {
450 remote_bitrate_estimator_->GetStats(output);
451}
452
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000453ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const {
454 return rtp_receive_statistics_.get();
455}
456
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000457bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const {
458 StreamStatistician* statistician =
459 rtp_receive_statistics_->GetStatistician(header.ssrc);
460 if (!statistician)
461 return false;
462 return statistician->IsPacketInOrder(header.sequenceNumber);
463}
464
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000465bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header,
466 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000467 // Retransmissions are handled separately if RTX is enabled.
468 if (rtp_payload_registry_->RtxEnabled())
469 return false;
470 StreamStatistician* statistician =
471 rtp_receive_statistics_->GetStatistician(header.ssrc);
472 if (!statistician)
473 return false;
474 // Check if this is a retransmission.
475 uint16_t min_rtt = 0;
476 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000477 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000478 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000479}
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000480} // namespace webrtc