blob: fb2cc6660ec720c252fb0d9966df4b8b43a768ce [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
93 memset(current_remote_csrc_, 0, sizeof(current_remote_csrc_));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000094}
95
Niels Mölleref998882018-03-23 08:54:34 +010096RtpReceiverImpl::~RtpReceiverImpl() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097
Karl Wibergc62f6c72017-10-04 12:38:53 +020098int32_t RtpReceiverImpl::RegisterReceivePayload(
99 int payload_type,
100 const SdpAudioFormat& audio_format) {
danilchap7c9426c2016-04-14 03:05:31 -0700101 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000102
103 // TODO(phoglund): Try to streamline handling of the RED codec and some other
104 // cases which makes it necessary to keep track of whether we created a
105 // payload or not.
106 bool created_new_payload = false;
107 int32_t result = rtp_payload_registry_->RegisterReceivePayload(
Karl Wibergc62f6c72017-10-04 12:38:53 +0200108 payload_type, audio_format, &created_new_payload);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000109 if (created_new_payload) {
Karl Wibergc62f6c72017-10-04 12:38:53 +0200110 if (rtp_media_receiver_->OnNewPayloadTypeCreated(payload_type,
111 audio_format) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100112 RTC_LOG(LS_ERROR) << "Failed to register payload: " << audio_format.name
113 << "/" << payload_type;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 return -1;
115 }
116 }
117 return result;
118}
119
magjed6b272c52016-11-25 02:29:39 -0800120int32_t RtpReceiverImpl::RegisterReceivePayload(const VideoCodec& video_codec) {
121 rtc::CritScope lock(&critical_section_rtp_receiver_);
122 return rtp_payload_registry_->RegisterReceivePayload(video_codec);
123}
124
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125int32_t RtpReceiverImpl::DeRegisterReceivePayload(
126 const int8_t payload_type) {
danilchap7c9426c2016-04-14 03:05:31 -0700127 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000128 return rtp_payload_registry_->DeRegisterReceivePayload(payload_type);
129}
130
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000131uint32_t RtpReceiverImpl::SSRC() const {
danilchap7c9426c2016-04-14 03:05:31 -0700132 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000133 return ssrc_;
134}
135
136// Get remote CSRC.
137int32_t RtpReceiverImpl::CSRCs(uint32_t array_of_csrcs[kRtpCsrcSize]) const {
danilchap7c9426c2016-04-14 03:05:31 -0700138 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000139
140 assert(num_csrcs_ <= kRtpCsrcSize);
141
142 if (num_csrcs_ > 0) {
143 memcpy(array_of_csrcs, current_remote_csrc_, sizeof(uint32_t)*num_csrcs_);
144 }
145 return num_csrcs_;
146}
147
148int32_t RtpReceiverImpl::Energy(
149 uint8_t array_of_energy[kRtpCsrcSize]) const {
150 return rtp_media_receiver_->Energy(array_of_energy);
151}
152
Niels Möller22ec9522017-10-05 08:39:15 +0200153bool RtpReceiverImpl::IncomingRtpPacket(const RTPHeader& rtp_header,
154 const uint8_t* payload,
155 size_t payload_length,
156 PayloadUnion payload_specific) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000157 // Trigger our callbacks.
158 CheckSSRCChanged(rtp_header);
159
Niels Möller70bb3262018-05-23 16:15:13 +0200160 if (CheckPayloadChanged(rtp_header,
pbosd4362982015-07-07 08:32:48 -0700161 &payload_specific) == -1) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000162 if (payload_length == 0) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000163 // OK, keep-alive packet.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000164 return true;
165 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100166 RTC_LOG(LS_WARNING) << "Receiving invalid payload type.";
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000167 return false;
168 }
169
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000170 WebRtcRTPHeader webrtc_rtp_header;
171 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000172 webrtc_rtp_header.header = rtp_header;
173 CheckCSRC(webrtc_rtp_header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000174
zstein2b706342017-08-24 14:52:17 -0700175 auto audio_level =
176 rtp_header.extension.hasAudioLevel
Danil Chapovalovd264df52018-06-14 12:59:38 +0200177 ? absl::optional<uint8_t>(rtp_header.extension.audioLevel)
178 : absl::nullopt;
zstein2b706342017-08-24 14:52:17 -0700179 UpdateSources(audio_level);
hbos8d609f62017-04-10 07:39:05 -0700180
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000181 int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
Niels Möller31791e72018-03-14 11:27:26 +0100182 &webrtc_rtp_header, payload_specific, payload, payload_length,
Niels Möllerbbf389c2017-09-26 14:05:05 +0200183 clock_->TimeInMilliseconds());
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000184
185 if (ret_val < 0) {
186 return false;
187 }
188
189 {
danilchap7c9426c2016-04-14 03:05:31 -0700190 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000191
Niels Möller22ec9522017-10-05 08:39:15 +0200192 // TODO(nisse): Do not rely on InOrderPacket for recovered packets, when
193 // packet is passed as RtpPacketReceived and that information is available.
194 // We should ideally never record timestamps for retransmitted or recovered
195 // packets.
196 if (InOrderPacket(last_received_sequence_number_,
197 rtp_header.sequenceNumber)) {
198 last_received_sequence_number_.emplace(rtp_header.sequenceNumber);
199 last_received_timestamp_ = rtp_header.timestamp;
200 last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000201 }
202 }
Niels Möller22ec9522017-10-05 08:39:15 +0200203
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000204 return true;
205}
206
danilchap799a9d02016-09-22 03:36:27 -0700207TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
208 return rtp_media_receiver_->GetTelephoneEventHandler();
209}
210
hbos8d609f62017-04-10 07:39:05 -0700211std::vector<RtpSource> RtpReceiverImpl::GetSources() const {
zhihuang04262222017-04-11 11:28:10 -0700212 rtc::CritScope lock(&critical_section_rtp_receiver_);
213
hbos8d609f62017-04-10 07:39:05 -0700214 int64_t now_ms = clock_->TimeInMilliseconds();
215 std::vector<RtpSource> sources;
216
zhihuang04262222017-04-11 11:28:10 -0700217 RTC_DCHECK(std::is_sorted(ssrc_sources_.begin(), ssrc_sources_.end(),
218 [](const RtpSource& lhs, const RtpSource& rhs) {
219 return lhs.timestamp_ms() < rhs.timestamp_ms();
220 }));
221 RTC_DCHECK(std::is_sorted(csrc_sources_.begin(), csrc_sources_.end(),
222 [](const RtpSource& lhs, const RtpSource& rhs) {
223 return lhs.timestamp_ms() < rhs.timestamp_ms();
224 }));
hbos8d609f62017-04-10 07:39:05 -0700225
zhihuang04262222017-04-11 11:28:10 -0700226 std::set<uint32_t> selected_ssrcs;
227 for (auto rit = ssrc_sources_.rbegin(); rit != ssrc_sources_.rend(); ++rit) {
228 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
229 break;
hbos8d609f62017-04-10 07:39:05 -0700230 }
zhihuang04262222017-04-11 11:28:10 -0700231 if (selected_ssrcs.insert(rit->source_id()).second) {
hbos8d609f62017-04-10 07:39:05 -0700232 sources.push_back(*rit);
233 }
zhihuang04262222017-04-11 11:28:10 -0700234 }
hbos8d609f62017-04-10 07:39:05 -0700235
zhihuang04262222017-04-11 11:28:10 -0700236 for (auto rit = csrc_sources_.rbegin(); rit != csrc_sources_.rend(); ++rit) {
237 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
238 break;
239 }
240 sources.push_back(*rit);
241 }
hbos8d609f62017-04-10 07:39:05 -0700242 return sources;
243}
244
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200245bool RtpReceiverImpl::GetLatestTimestamps(uint32_t* timestamp,
246 int64_t* receive_time_ms) const {
danilchap7c9426c2016-04-14 03:05:31 -0700247 rtc::CritScope lock(&critical_section_rtp_receiver_);
Niels Möller22ec9522017-10-05 08:39:15 +0200248 if (!last_received_sequence_number_)
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000249 return false;
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200250
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000251 *timestamp = last_received_timestamp_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000252 *receive_time_ms = last_received_frame_time_ms_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000253
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200254 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000255}
256
Niels Möllerf7824922018-05-25 13:41:10 +0200257// TODO(nisse): Delete.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000258// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000259void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
Niels Möllerf7824922018-05-25 13:41:10 +0200260 rtc::CritScope lock(&critical_section_rtp_receiver_);
261 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000262}
263
264// Implementation note: must not hold critsect when called.
265// TODO(phoglund): Move as much as possible of this code path into the media
266// specific receivers. Basically this method goes through a lot of trouble to
267// compute something which is only used by the media specific parts later. If
268// this code path moves we can get rid of some of the rtp_receiver ->
269// media_specific interface (such as CheckPayloadChange, possibly get/set
270// last known payload).
pbosd4362982015-07-07 08:32:48 -0700271int32_t RtpReceiverImpl::CheckPayloadChanged(const RTPHeader& rtp_header,
pbosd4362982015-07-07 08:32:48 -0700272 PayloadUnion* specific_payload) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000273 int8_t payload_type = rtp_header.payloadType;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000274
275 {
danilchap7c9426c2016-04-14 03:05:31 -0700276 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000277
278 int8_t last_received_payload_type =
279 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000280 // TODO(holmer): Remove this code when RED parsing has been broken out from
281 // RtpReceiverAudio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000282 if (payload_type != last_received_payload_type) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000283 bool should_discard_changes = false;
284
285 rtp_media_receiver_->CheckPayloadChanged(
pbosd4362982015-07-07 08:32:48 -0700286 payload_type, specific_payload,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000287 &should_discard_changes);
288
289 if (should_discard_changes) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000290 return 0;
291 }
292
Karl Wiberg73b60b82017-09-21 15:00:58 +0200293 const auto payload =
danilchap5c1def82015-12-10 09:51:54 -0800294 rtp_payload_registry_->PayloadTypeToPayload(payload_type);
295 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000296 // Not a registered payload type.
297 return -1;
298 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000299 rtp_payload_registry_->set_last_received_payload_type(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000300 }
301 } // End critsect.
302
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000303 return 0;
304}
305
306// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000307void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
Niels Mölleref998882018-03-23 08:54:34 +0100308 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
309 if (num_csrcs > kRtpCsrcSize) {
310 // Ignore.
311 return;
312 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000313 {
danilchap7c9426c2016-04-14 03:05:31 -0700314 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000315
Niels Mölleref998882018-03-23 08:54:34 +0100316 // Copy new.
317 memcpy(current_remote_csrc_,
318 rtp_header.header.arrOfCSRCs,
319 num_csrcs * sizeof(uint32_t));
320
321 num_csrcs_ = num_csrcs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000322 } // End critsect.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000323}
324
zstein2b706342017-08-24 14:52:17 -0700325void RtpReceiverImpl::UpdateSources(
Danil Chapovalovd264df52018-06-14 12:59:38 +0200326 const absl::optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700327 rtc::CritScope lock(&critical_section_rtp_receiver_);
328 int64_t now_ms = clock_->TimeInMilliseconds();
329
330 for (size_t i = 0; i < num_csrcs_; ++i) {
331 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
332 if (map_it == iterator_by_csrc_.end()) {
333 // If it is a new CSRC, append a new object to the end of the list.
334 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
335 RtpSourceType::CSRC);
336 } else {
337 // If it is an existing CSRC, move the object to the end of the list.
338 map_it->second->update_timestamp_ms(now_ms);
339 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
340 }
341 // Update the unordered_map.
342 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
343 }
344
345 // If this is the first packet or the SSRC is changed, insert a new
346 // contributing source that uses the SSRC.
347 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
348 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
349 } else {
350 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
351 }
352
zstein2b706342017-08-24 14:52:17 -0700353 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
354
hbos8d609f62017-04-10 07:39:05 -0700355 RemoveOutdatedSources(now_ms);
356}
357
358void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
359 std::list<RtpSource>::iterator it;
360 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
361 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
362 break;
363 }
364 iterator_by_csrc_.erase(it->source_id());
365 }
366 csrc_sources_.erase(csrc_sources_.begin(), it);
367
368 std::vector<RtpSource>::iterator vec_it;
369 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
370 ++vec_it) {
371 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
372 break;
373 }
374 }
375 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
376}
377
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000378} // namespace webrtc