blob: 23f8b8b7d5c1308a08be5af6f7dd47695897a62b [file] [log] [blame]
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtp_receiver_impl.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000012
13#include <assert.h>
14#include <math.h>
15#include <stdlib.h>
16#include <string.h>
17
hbos8d609f62017-04-10 07:39:05 -070018#include <set>
19#include <vector>
20
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "common_types.h" // NOLINT(build/include)
Karl Wibergc62f6c72017-10-04 12:38:53 +020022#include "modules/audio_coding/codecs/audio_format_conversion.h"
Niels Möller22ec9522017-10-05 08:39:15 +020023#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
25#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
26#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
27#include "rtc_base/logging.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000028
29namespace webrtc {
30
Niels Möller22ec9522017-10-05 08:39:15 +020031namespace {
Danil Chapovalovd264df52018-06-14 12:59:38 +020032bool InOrderPacket(absl::optional<uint16_t> latest_sequence_number,
Niels Möller22ec9522017-10-05 08:39:15 +020033 uint16_t current_sequence_number) {
34 if (!latest_sequence_number)
35 return true;
36
37 // We need to distinguish between a late or retransmitted packet,
38 // and a sequence number discontinuity.
39 if (IsNewerSequenceNumber(current_sequence_number, *latest_sequence_number)) {
40 return true;
41 } else {
42 // If we have a restart of the remote side this packet is still in order.
43 return !IsNewerSequenceNumber(
44 current_sequence_number,
45 *latest_sequence_number - kDefaultMaxReorderingThreshold);
46 }
47}
48
49} // namespace
50
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000051using RtpUtility::Payload;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000052
hbos8d609f62017-04-10 07:39:05 -070053// Only return the sources in the last 10 seconds.
54const int64_t kGetSourcesTimeoutMs = 10000;
55
wu@webrtc.org822fbd82013-08-15 23:38:54 +000056RtpReceiver* RtpReceiver::CreateVideoReceiver(
Peter Boströmac547a62015-09-17 23:03:57 +020057 Clock* clock,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000058 RtpData* incoming_payload_callback,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000059 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070060 RTC_DCHECK(incoming_payload_callback != nullptr);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000061 return new RtpReceiverImpl(
Niels Möllerf7824922018-05-25 13:41:10 +020062 clock, rtp_payload_registry,
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000063 RTPReceiverStrategy::CreateVideoStrategy(incoming_payload_callback));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000064}
65
66RtpReceiver* RtpReceiver::CreateAudioReceiver(
Peter Boströmac547a62015-09-17 23:03:57 +020067 Clock* clock,
solenberg1d031392016-03-30 02:42:32 -070068 RtpData* incoming_payload_callback,
solenberg1d031392016-03-30 02:42:32 -070069 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070070 RTC_DCHECK(incoming_payload_callback != nullptr);
solenberg1d031392016-03-30 02:42:32 -070071 return new RtpReceiverImpl(
Niels Möllerf7824922018-05-25 13:41:10 +020072 clock, rtp_payload_registry,
solenberg1d031392016-03-30 02:42:32 -070073 RTPReceiverStrategy::CreateAudioStrategy(incoming_payload_callback));
74}
75
Karl Wibergc62f6c72017-10-04 12:38:53 +020076int32_t RtpReceiver::RegisterReceivePayload(const CodecInst& audio_codec) {
77 return RegisterReceivePayload(audio_codec.pltype,
78 CodecInstToSdp(audio_codec));
79}
80
hbos8d609f62017-04-10 07:39:05 -070081RtpReceiverImpl::RtpReceiverImpl(Clock* clock,
hbos8d609f62017-04-10 07:39:05 -070082 RTPPayloadRegistry* rtp_payload_registry,
83 RTPReceiverStrategy* rtp_media_receiver)
wu@webrtc.org822fbd82013-08-15 23:38:54 +000084 : clock_(clock),
85 rtp_payload_registry_(rtp_payload_registry),
86 rtp_media_receiver_(rtp_media_receiver),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087 ssrc_(0),
88 num_csrcs_(0),
89 current_remote_csrc_(),
90 last_received_timestamp_(0),
Niels Möllerbbf389c2017-09-26 14:05:05 +020091 last_received_frame_time_ms_(-1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000092 memset(current_remote_csrc_, 0, sizeof(current_remote_csrc_));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000093}
94
Niels Mölleref998882018-03-23 08:54:34 +010095RtpReceiverImpl::~RtpReceiverImpl() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000096
Karl Wibergc62f6c72017-10-04 12:38:53 +020097int32_t RtpReceiverImpl::RegisterReceivePayload(
98 int payload_type,
99 const SdpAudioFormat& audio_format) {
danilchap7c9426c2016-04-14 03:05:31 -0700100 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000101
102 // TODO(phoglund): Try to streamline handling of the RED codec and some other
103 // cases which makes it necessary to keep track of whether we created a
104 // payload or not.
105 bool created_new_payload = false;
106 int32_t result = rtp_payload_registry_->RegisterReceivePayload(
Karl Wibergc62f6c72017-10-04 12:38:53 +0200107 payload_type, audio_format, &created_new_payload);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000108 return result;
109}
110
magjed6b272c52016-11-25 02:29:39 -0800111int32_t RtpReceiverImpl::RegisterReceivePayload(const VideoCodec& video_codec) {
112 rtc::CritScope lock(&critical_section_rtp_receiver_);
113 return rtp_payload_registry_->RegisterReceivePayload(video_codec);
114}
115
Yves Gerey665174f2018-06-19 15:03:05 +0200116int32_t RtpReceiverImpl::DeRegisterReceivePayload(const int8_t payload_type) {
danilchap7c9426c2016-04-14 03:05:31 -0700117 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000118 return rtp_payload_registry_->DeRegisterReceivePayload(payload_type);
119}
120
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000121uint32_t RtpReceiverImpl::SSRC() const {
danilchap7c9426c2016-04-14 03:05:31 -0700122 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000123 return ssrc_;
124}
125
Niels Möller22ec9522017-10-05 08:39:15 +0200126bool RtpReceiverImpl::IncomingRtpPacket(const RTPHeader& rtp_header,
127 const uint8_t* payload,
128 size_t payload_length,
129 PayloadUnion payload_specific) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000130 // Trigger our callbacks.
131 CheckSSRCChanged(rtp_header);
132
Niels Möllerfd77b782018-08-06 12:40:58 +0200133 if (payload_length == 0) {
134 // OK, keep-alive packet.
135 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000136 }
philipel1a4746a2018-07-09 15:52:29 +0200137 WebRtcRTPHeader webrtc_rtp_header{};
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000138 webrtc_rtp_header.header = rtp_header;
139 CheckCSRC(webrtc_rtp_header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000140
zstein2b706342017-08-24 14:52:17 -0700141 auto audio_level =
142 rtp_header.extension.hasAudioLevel
Danil Chapovalovd264df52018-06-14 12:59:38 +0200143 ? absl::optional<uint8_t>(rtp_header.extension.audioLevel)
144 : absl::nullopt;
zstein2b706342017-08-24 14:52:17 -0700145 UpdateSources(audio_level);
hbos8d609f62017-04-10 07:39:05 -0700146
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000147 int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
Niels Möller31791e72018-03-14 11:27:26 +0100148 &webrtc_rtp_header, payload_specific, payload, payload_length,
Niels Möllerbbf389c2017-09-26 14:05:05 +0200149 clock_->TimeInMilliseconds());
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000150
151 if (ret_val < 0) {
152 return false;
153 }
154
155 {
danilchap7c9426c2016-04-14 03:05:31 -0700156 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000157
Niels Möller22ec9522017-10-05 08:39:15 +0200158 // TODO(nisse): Do not rely on InOrderPacket for recovered packets, when
159 // packet is passed as RtpPacketReceived and that information is available.
160 // We should ideally never record timestamps for retransmitted or recovered
161 // packets.
162 if (InOrderPacket(last_received_sequence_number_,
163 rtp_header.sequenceNumber)) {
164 last_received_sequence_number_.emplace(rtp_header.sequenceNumber);
165 last_received_timestamp_ = rtp_header.timestamp;
166 last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000167 }
168 }
Niels Möller22ec9522017-10-05 08:39:15 +0200169
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000170 return true;
171}
172
hbos8d609f62017-04-10 07:39:05 -0700173std::vector<RtpSource> RtpReceiverImpl::GetSources() const {
zhihuang04262222017-04-11 11:28:10 -0700174 rtc::CritScope lock(&critical_section_rtp_receiver_);
175
hbos8d609f62017-04-10 07:39:05 -0700176 int64_t now_ms = clock_->TimeInMilliseconds();
177 std::vector<RtpSource> sources;
178
zhihuang04262222017-04-11 11:28:10 -0700179 RTC_DCHECK(std::is_sorted(ssrc_sources_.begin(), ssrc_sources_.end(),
180 [](const RtpSource& lhs, const RtpSource& rhs) {
181 return lhs.timestamp_ms() < rhs.timestamp_ms();
182 }));
183 RTC_DCHECK(std::is_sorted(csrc_sources_.begin(), csrc_sources_.end(),
184 [](const RtpSource& lhs, const RtpSource& rhs) {
185 return lhs.timestamp_ms() < rhs.timestamp_ms();
186 }));
hbos8d609f62017-04-10 07:39:05 -0700187
zhihuang04262222017-04-11 11:28:10 -0700188 std::set<uint32_t> selected_ssrcs;
189 for (auto rit = ssrc_sources_.rbegin(); rit != ssrc_sources_.rend(); ++rit) {
190 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
191 break;
hbos8d609f62017-04-10 07:39:05 -0700192 }
zhihuang04262222017-04-11 11:28:10 -0700193 if (selected_ssrcs.insert(rit->source_id()).second) {
hbos8d609f62017-04-10 07:39:05 -0700194 sources.push_back(*rit);
195 }
zhihuang04262222017-04-11 11:28:10 -0700196 }
hbos8d609f62017-04-10 07:39:05 -0700197
zhihuang04262222017-04-11 11:28:10 -0700198 for (auto rit = csrc_sources_.rbegin(); rit != csrc_sources_.rend(); ++rit) {
199 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
200 break;
201 }
202 sources.push_back(*rit);
203 }
hbos8d609f62017-04-10 07:39:05 -0700204 return sources;
205}
206
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200207bool RtpReceiverImpl::GetLatestTimestamps(uint32_t* timestamp,
208 int64_t* receive_time_ms) const {
danilchap7c9426c2016-04-14 03:05:31 -0700209 rtc::CritScope lock(&critical_section_rtp_receiver_);
Niels Möller22ec9522017-10-05 08:39:15 +0200210 if (!last_received_sequence_number_)
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000211 return false;
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200212
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000213 *timestamp = last_received_timestamp_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000214 *receive_time_ms = last_received_frame_time_ms_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000215
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200216 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000217}
218
Niels Möllerf7824922018-05-25 13:41:10 +0200219// TODO(nisse): Delete.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000220// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000221void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
Niels Möllerf7824922018-05-25 13:41:10 +0200222 rtc::CritScope lock(&critical_section_rtp_receiver_);
223 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000224}
225
226// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000227void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
Niels Mölleref998882018-03-23 08:54:34 +0100228 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
229 if (num_csrcs > kRtpCsrcSize) {
230 // Ignore.
231 return;
232 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000233 {
danilchap7c9426c2016-04-14 03:05:31 -0700234 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000235
Niels Mölleref998882018-03-23 08:54:34 +0100236 // Copy new.
Yves Gerey665174f2018-06-19 15:03:05 +0200237 memcpy(current_remote_csrc_, rtp_header.header.arrOfCSRCs,
Niels Mölleref998882018-03-23 08:54:34 +0100238 num_csrcs * sizeof(uint32_t));
239
240 num_csrcs_ = num_csrcs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000241 } // End critsect.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000242}
243
zstein2b706342017-08-24 14:52:17 -0700244void RtpReceiverImpl::UpdateSources(
Danil Chapovalovd264df52018-06-14 12:59:38 +0200245 const absl::optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700246 rtc::CritScope lock(&critical_section_rtp_receiver_);
247 int64_t now_ms = clock_->TimeInMilliseconds();
248
249 for (size_t i = 0; i < num_csrcs_; ++i) {
250 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
251 if (map_it == iterator_by_csrc_.end()) {
252 // If it is a new CSRC, append a new object to the end of the list.
253 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
254 RtpSourceType::CSRC);
255 } else {
256 // If it is an existing CSRC, move the object to the end of the list.
257 map_it->second->update_timestamp_ms(now_ms);
258 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
259 }
260 // Update the unordered_map.
261 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
262 }
263
264 // If this is the first packet or the SSRC is changed, insert a new
265 // contributing source that uses the SSRC.
266 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
267 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
268 } else {
269 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
270 }
271
zstein2b706342017-08-24 14:52:17 -0700272 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
273
hbos8d609f62017-04-10 07:39:05 -0700274 RemoveOutdatedSources(now_ms);
275}
276
277void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
278 std::list<RtpSource>::iterator it;
279 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
280 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
281 break;
282 }
283 iterator_by_csrc_.erase(it->source_id());
284 }
285 csrc_sources_.erase(csrc_sources_.begin(), it);
286
287 std::vector<RtpSource>::iterator vec_it;
288 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
289 ++vec_it) {
290 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
291 break;
292 }
293 }
294 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
295}
296
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000297} // namespace webrtc