blob: 64acfb58be708477f5b94a321e1a1830afb25df3 [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 {
32bool InOrderPacket(rtc::Optional<uint16_t> latest_sequence_number,
33 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,
59 RtpFeedback* incoming_messages_callback,
60 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070061 RTC_DCHECK(incoming_payload_callback != nullptr);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000062 if (!incoming_messages_callback)
63 incoming_messages_callback = NullObjectRtpFeedback();
64 return new RtpReceiverImpl(
solenberg1d031392016-03-30 02:42:32 -070065 clock, incoming_messages_callback, rtp_payload_registry,
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000066 RTPReceiverStrategy::CreateVideoStrategy(incoming_payload_callback));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000067}
68
69RtpReceiver* RtpReceiver::CreateAudioReceiver(
Peter Boströmac547a62015-09-17 23:03:57 +020070 Clock* clock,
solenberg1d031392016-03-30 02:42:32 -070071 RtpData* incoming_payload_callback,
72 RtpFeedback* incoming_messages_callback,
73 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070074 RTC_DCHECK(incoming_payload_callback != nullptr);
solenberg1d031392016-03-30 02:42:32 -070075 if (!incoming_messages_callback)
76 incoming_messages_callback = NullObjectRtpFeedback();
77 return new RtpReceiverImpl(
78 clock, incoming_messages_callback, rtp_payload_registry,
79 RTPReceiverStrategy::CreateAudioStrategy(incoming_payload_callback));
80}
81
Karl Wibergc62f6c72017-10-04 12:38:53 +020082int32_t RtpReceiver::RegisterReceivePayload(const CodecInst& audio_codec) {
83 return RegisterReceivePayload(audio_codec.pltype,
84 CodecInstToSdp(audio_codec));
85}
86
hbos8d609f62017-04-10 07:39:05 -070087RtpReceiverImpl::RtpReceiverImpl(Clock* clock,
88 RtpFeedback* incoming_messages_callback,
89 RTPPayloadRegistry* rtp_payload_registry,
90 RTPReceiverStrategy* rtp_media_receiver)
wu@webrtc.org822fbd82013-08-15 23:38:54 +000091 : clock_(clock),
92 rtp_payload_registry_(rtp_payload_registry),
93 rtp_media_receiver_(rtp_media_receiver),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000094 cb_rtp_feedback_(incoming_messages_callback),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000095 ssrc_(0),
96 num_csrcs_(0),
97 current_remote_csrc_(),
98 last_received_timestamp_(0),
Niels Möllerbbf389c2017-09-26 14:05:05 +020099 last_received_frame_time_ms_(-1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000100 assert(incoming_messages_callback);
101
102 memset(current_remote_csrc_, 0, sizeof(current_remote_csrc_));
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000103}
104
105RtpReceiverImpl::~RtpReceiverImpl() {
106 for (int i = 0; i < num_csrcs_; ++i) {
Peter Boströmac547a62015-09-17 23:03:57 +0200107 cb_rtp_feedback_->OnIncomingCSRCChanged(current_remote_csrc_[i], false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000108 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000109}
110
Karl Wibergc62f6c72017-10-04 12:38:53 +0200111int32_t RtpReceiverImpl::RegisterReceivePayload(
112 int payload_type,
113 const SdpAudioFormat& audio_format) {
danilchap7c9426c2016-04-14 03:05:31 -0700114 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000115
116 // TODO(phoglund): Try to streamline handling of the RED codec and some other
117 // cases which makes it necessary to keep track of whether we created a
118 // payload or not.
119 bool created_new_payload = false;
120 int32_t result = rtp_payload_registry_->RegisterReceivePayload(
Karl Wibergc62f6c72017-10-04 12:38:53 +0200121 payload_type, audio_format, &created_new_payload);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000122 if (created_new_payload) {
Karl Wibergc62f6c72017-10-04 12:38:53 +0200123 if (rtp_media_receiver_->OnNewPayloadTypeCreated(payload_type,
124 audio_format) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100125 RTC_LOG(LS_ERROR) << "Failed to register payload: " << audio_format.name
126 << "/" << payload_type;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000127 return -1;
128 }
129 }
130 return result;
131}
132
magjed6b272c52016-11-25 02:29:39 -0800133int32_t RtpReceiverImpl::RegisterReceivePayload(const VideoCodec& video_codec) {
134 rtc::CritScope lock(&critical_section_rtp_receiver_);
135 return rtp_payload_registry_->RegisterReceivePayload(video_codec);
136}
137
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000138int32_t RtpReceiverImpl::DeRegisterReceivePayload(
139 const int8_t payload_type) {
danilchap7c9426c2016-04-14 03:05:31 -0700140 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000141 return rtp_payload_registry_->DeRegisterReceivePayload(payload_type);
142}
143
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000144uint32_t RtpReceiverImpl::SSRC() const {
danilchap7c9426c2016-04-14 03:05:31 -0700145 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000146 return ssrc_;
147}
148
149// Get remote CSRC.
150int32_t RtpReceiverImpl::CSRCs(uint32_t array_of_csrcs[kRtpCsrcSize]) const {
danilchap7c9426c2016-04-14 03:05:31 -0700151 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000152
153 assert(num_csrcs_ <= kRtpCsrcSize);
154
155 if (num_csrcs_ > 0) {
156 memcpy(array_of_csrcs, current_remote_csrc_, sizeof(uint32_t)*num_csrcs_);
157 }
158 return num_csrcs_;
159}
160
161int32_t RtpReceiverImpl::Energy(
162 uint8_t array_of_energy[kRtpCsrcSize]) const {
163 return rtp_media_receiver_->Energy(array_of_energy);
164}
165
Niels Möller22ec9522017-10-05 08:39:15 +0200166bool RtpReceiverImpl::IncomingRtpPacket(const RTPHeader& rtp_header,
167 const uint8_t* payload,
168 size_t payload_length,
169 PayloadUnion payload_specific) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000170 // Trigger our callbacks.
171 CheckSSRCChanged(rtp_header);
172
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000173 int8_t first_payload_byte = payload_length > 0 ? payload[0] : 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000174 bool is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000175
danilchap6db6cdc2015-12-15 02:54:47 -0800176 if (CheckPayloadChanged(rtp_header, first_payload_byte, &is_red,
pbosd4362982015-07-07 08:32:48 -0700177 &payload_specific) == -1) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000178 if (payload_length == 0) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000179 // OK, keep-alive packet.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000180 return true;
181 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100182 RTC_LOG(LS_WARNING) << "Receiving invalid payload type.";
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000183 return false;
184 }
185
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000186 WebRtcRTPHeader webrtc_rtp_header;
187 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000188 webrtc_rtp_header.header = rtp_header;
189 CheckCSRC(webrtc_rtp_header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000190
zstein2b706342017-08-24 14:52:17 -0700191 auto audio_level =
192 rtp_header.extension.hasAudioLevel
193 ? rtc::Optional<uint8_t>(rtp_header.extension.audioLevel)
194 : rtc::Optional<uint8_t>();
195 UpdateSources(audio_level);
hbos8d609f62017-04-10 07:39:05 -0700196
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000197 int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000198 &webrtc_rtp_header, payload_specific, is_red, payload, payload_length,
Niels Möllerbbf389c2017-09-26 14:05:05 +0200199 clock_->TimeInMilliseconds());
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000200
201 if (ret_val < 0) {
202 return false;
203 }
204
205 {
danilchap7c9426c2016-04-14 03:05:31 -0700206 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000207
Niels Möller22ec9522017-10-05 08:39:15 +0200208 // TODO(nisse): Do not rely on InOrderPacket for recovered packets, when
209 // packet is passed as RtpPacketReceived and that information is available.
210 // We should ideally never record timestamps for retransmitted or recovered
211 // packets.
212 if (InOrderPacket(last_received_sequence_number_,
213 rtp_header.sequenceNumber)) {
214 last_received_sequence_number_.emplace(rtp_header.sequenceNumber);
215 last_received_timestamp_ = rtp_header.timestamp;
216 last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000217 }
218 }
Niels Möller22ec9522017-10-05 08:39:15 +0200219
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000220 return true;
221}
222
danilchap799a9d02016-09-22 03:36:27 -0700223TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
224 return rtp_media_receiver_->GetTelephoneEventHandler();
225}
226
hbos8d609f62017-04-10 07:39:05 -0700227std::vector<RtpSource> RtpReceiverImpl::GetSources() const {
zhihuang04262222017-04-11 11:28:10 -0700228 rtc::CritScope lock(&critical_section_rtp_receiver_);
229
hbos8d609f62017-04-10 07:39:05 -0700230 int64_t now_ms = clock_->TimeInMilliseconds();
231 std::vector<RtpSource> sources;
232
zhihuang04262222017-04-11 11:28:10 -0700233 RTC_DCHECK(std::is_sorted(ssrc_sources_.begin(), ssrc_sources_.end(),
234 [](const RtpSource& lhs, const RtpSource& rhs) {
235 return lhs.timestamp_ms() < rhs.timestamp_ms();
236 }));
237 RTC_DCHECK(std::is_sorted(csrc_sources_.begin(), csrc_sources_.end(),
238 [](const RtpSource& lhs, const RtpSource& rhs) {
239 return lhs.timestamp_ms() < rhs.timestamp_ms();
240 }));
hbos8d609f62017-04-10 07:39:05 -0700241
zhihuang04262222017-04-11 11:28:10 -0700242 std::set<uint32_t> selected_ssrcs;
243 for (auto rit = ssrc_sources_.rbegin(); rit != ssrc_sources_.rend(); ++rit) {
244 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
245 break;
hbos8d609f62017-04-10 07:39:05 -0700246 }
zhihuang04262222017-04-11 11:28:10 -0700247 if (selected_ssrcs.insert(rit->source_id()).second) {
hbos8d609f62017-04-10 07:39:05 -0700248 sources.push_back(*rit);
249 }
zhihuang04262222017-04-11 11:28:10 -0700250 }
hbos8d609f62017-04-10 07:39:05 -0700251
zhihuang04262222017-04-11 11:28:10 -0700252 for (auto rit = csrc_sources_.rbegin(); rit != csrc_sources_.rend(); ++rit) {
253 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
254 break;
255 }
256 sources.push_back(*rit);
257 }
hbos8d609f62017-04-10 07:39:05 -0700258 return sources;
259}
260
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200261bool RtpReceiverImpl::GetLatestTimestamps(uint32_t* timestamp,
262 int64_t* receive_time_ms) const {
danilchap7c9426c2016-04-14 03:05:31 -0700263 rtc::CritScope lock(&critical_section_rtp_receiver_);
Niels Möller22ec9522017-10-05 08:39:15 +0200264 if (!last_received_sequence_number_)
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000265 return false;
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200266
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000267 *timestamp = last_received_timestamp_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000268 *receive_time_ms = last_received_frame_time_ms_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000269
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200270 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000271}
272
273// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000274void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000275 bool new_ssrc = false;
Karl Wibergc62f6c72017-10-04 12:38:53 +0200276 rtc::Optional<AudioPayload> reinitialize_audio_payload;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000277
278 {
danilchap7c9426c2016-04-14 03:05:31 -0700279 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000280
281 int8_t last_received_payload_type =
282 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000283 if (ssrc_ != rtp_header.ssrc ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000284 (last_received_payload_type == -1 && ssrc_ == 0)) {
285 // We need the payload_type_ to make the call if the remote SSRC is 0.
286 new_ssrc = true;
287
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288 last_received_timestamp_ = 0;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000289 last_received_frame_time_ms_ = -1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000290
291 // Do we have a SSRC? Then the stream is restarted.
292 if (ssrc_ != 0) {
293 // Do we have the same codec? Then re-initialize coder.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000294 if (rtp_header.payloadType == last_received_payload_type) {
Karl Wiberg73b60b82017-09-21 15:00:58 +0200295 const auto payload = rtp_payload_registry_->PayloadTypeToPayload(
danilchap5c1def82015-12-10 09:51:54 -0800296 rtp_header.payloadType);
297 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000298 return;
299 }
Karl Wibergc856dc22017-09-28 20:13:59 +0200300 if (payload->typeSpecific.is_audio()) {
Karl Wibergc62f6c72017-10-04 12:38:53 +0200301 reinitialize_audio_payload.emplace(
302 payload->typeSpecific.audio_payload());
303 } else {
304 // OnInitializeDecoder() is only used for audio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000305 }
306 }
307 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000308 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000309 }
310 }
311
312 if (new_ssrc) {
313 // We need to get this to our RTCP sender and receiver.
314 // We need to do this outside critical section.
Peter Boströmac547a62015-09-17 23:03:57 +0200315 cb_rtp_feedback_->OnIncomingSSRCChanged(rtp_header.ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000316 }
317
Karl Wibergc62f6c72017-10-04 12:38:53 +0200318 if (reinitialize_audio_payload) {
319 if (-1 == cb_rtp_feedback_->OnInitializeDecoder(
320 rtp_header.payloadType, reinitialize_audio_payload->format,
321 reinitialize_audio_payload->rate)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000322 // New stream, same codec.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100323 RTC_LOG(LS_ERROR) << "Failed to create decoder for payload type: "
324 << static_cast<int>(rtp_header.payloadType);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000325 }
326 }
327}
328
329// Implementation note: must not hold critsect when called.
330// TODO(phoglund): Move as much as possible of this code path into the media
331// specific receivers. Basically this method goes through a lot of trouble to
332// compute something which is only used by the media specific parts later. If
333// this code path moves we can get rid of some of the rtp_receiver ->
334// media_specific interface (such as CheckPayloadChange, possibly get/set
335// last known payload).
pbosd4362982015-07-07 08:32:48 -0700336int32_t RtpReceiverImpl::CheckPayloadChanged(const RTPHeader& rtp_header,
337 const int8_t first_payload_byte,
danilchap6db6cdc2015-12-15 02:54:47 -0800338 bool* is_red,
pbosd4362982015-07-07 08:32:48 -0700339 PayloadUnion* specific_payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000340 bool re_initialize_decoder = false;
341
342 char payload_name[RTP_PAYLOAD_NAME_SIZE];
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000343 int8_t payload_type = rtp_header.payloadType;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000344
345 {
danilchap7c9426c2016-04-14 03:05:31 -0700346 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000347
348 int8_t last_received_payload_type =
349 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000350 // TODO(holmer): Remove this code when RED parsing has been broken out from
351 // RtpReceiverAudio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000352 if (payload_type != last_received_payload_type) {
353 if (rtp_payload_registry_->red_payload_type() == payload_type) {
354 // Get the real codec payload type.
355 payload_type = first_payload_byte & 0x7f;
danilchap6db6cdc2015-12-15 02:54:47 -0800356 *is_red = true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000357
358 if (rtp_payload_registry_->red_payload_type() == payload_type) {
359 // Invalid payload type, traced by caller. If we proceeded here,
360 // this would be set as |_last_received_payload_type|, and we would no
361 // longer catch corrupt packets at this level.
362 return -1;
363 }
364
365 // When we receive RED we need to check the real payload type.
366 if (payload_type == last_received_payload_type) {
367 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
368 return 0;
369 }
370 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000371 bool should_discard_changes = false;
372
373 rtp_media_receiver_->CheckPayloadChanged(
pbosd4362982015-07-07 08:32:48 -0700374 payload_type, specific_payload,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000375 &should_discard_changes);
376
377 if (should_discard_changes) {
danilchap6db6cdc2015-12-15 02:54:47 -0800378 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000379 return 0;
380 }
381
Karl Wiberg73b60b82017-09-21 15:00:58 +0200382 const auto payload =
danilchap5c1def82015-12-10 09:51:54 -0800383 rtp_payload_registry_->PayloadTypeToPayload(payload_type);
384 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000385 // Not a registered payload type.
386 return -1;
387 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000388 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
389 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
390
391 rtp_payload_registry_->set_last_received_payload_type(payload_type);
392
393 re_initialize_decoder = true;
394
395 rtp_media_receiver_->SetLastMediaSpecificPayload(payload->typeSpecific);
396 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
397
Karl Wibergc856dc22017-09-28 20:13:59 +0200398 if (!payload->typeSpecific.is_audio()) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000399 bool media_type_unchanged =
400 rtp_payload_registry_->ReportMediaPayloadType(payload_type);
401 if (media_type_unchanged) {
402 // Only reset the decoder if the media codec type has changed.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000403 re_initialize_decoder = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000404 }
405 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000406 } else {
407 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
danilchap6db6cdc2015-12-15 02:54:47 -0800408 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000409 }
410 } // End critsect.
411
412 if (re_initialize_decoder) {
Peter Boströmac547a62015-09-17 23:03:57 +0200413 if (-1 ==
414 rtp_media_receiver_->InvokeOnInitializeDecoder(
415 cb_rtp_feedback_, payload_type, payload_name, *specific_payload)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000416 return -1; // Wrong payload type.
417 }
418 }
419 return 0;
420}
421
422// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000423void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000424 int32_t num_csrcs_diff = 0;
425 uint32_t old_remote_csrc[kRtpCsrcSize];
426 uint8_t old_num_csrcs = 0;
427
428 {
danilchap7c9426c2016-04-14 03:05:31 -0700429 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000430
431 if (!rtp_media_receiver_->ShouldReportCsrcChanges(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000432 rtp_header.header.payloadType)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000433 return;
434 }
435 old_num_csrcs = num_csrcs_;
436 if (old_num_csrcs > 0) {
437 // Make a copy of old.
438 memcpy(old_remote_csrc, current_remote_csrc_,
439 num_csrcs_ * sizeof(uint32_t));
440 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000441 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000442 if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
443 // Copy new.
444 memcpy(current_remote_csrc_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000445 rtp_header.header.arrOfCSRCs,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000446 num_csrcs * sizeof(uint32_t));
447 }
448 if (num_csrcs > 0 || old_num_csrcs > 0) {
449 num_csrcs_diff = num_csrcs - old_num_csrcs;
450 num_csrcs_ = num_csrcs; // Update stored CSRCs.
451 } else {
452 // No change.
453 return;
454 }
455 } // End critsect.
456
457 bool have_called_callback = false;
458 // Search for new CSRC in old array.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000459 for (uint8_t i = 0; i < rtp_header.header.numCSRCs; ++i) {
460 const uint32_t csrc = rtp_header.header.arrOfCSRCs[i];
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000461
462 bool found_match = false;
463 for (uint8_t j = 0; j < old_num_csrcs; ++j) {
464 if (csrc == old_remote_csrc[j]) { // old list
465 found_match = true;
466 break;
467 }
468 }
469 if (!found_match && csrc) {
470 // Didn't find it, report it as new.
471 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200472 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000473 }
474 }
475 // Search for old CSRC in new array.
476 for (uint8_t i = 0; i < old_num_csrcs; ++i) {
477 const uint32_t csrc = old_remote_csrc[i];
478
479 bool found_match = false;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000480 for (uint8_t j = 0; j < rtp_header.header.numCSRCs; ++j) {
481 if (csrc == rtp_header.header.arrOfCSRCs[j]) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000482 found_match = true;
483 break;
484 }
485 }
486 if (!found_match && csrc) {
487 // Did not find it, report as removed.
488 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200489 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000490 }
491 }
492 if (!have_called_callback) {
493 // If the CSRC list contain non-unique entries we will end up here.
494 // Using CSRC 0 to signal this event, not interop safe, other
495 // implementations might have CSRC 0 as a valid value.
496 if (num_csrcs_diff > 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200497 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000498 } else if (num_csrcs_diff < 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200499 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000500 }
501 }
502}
503
zstein2b706342017-08-24 14:52:17 -0700504void RtpReceiverImpl::UpdateSources(
505 const rtc::Optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700506 rtc::CritScope lock(&critical_section_rtp_receiver_);
507 int64_t now_ms = clock_->TimeInMilliseconds();
508
509 for (size_t i = 0; i < num_csrcs_; ++i) {
510 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
511 if (map_it == iterator_by_csrc_.end()) {
512 // If it is a new CSRC, append a new object to the end of the list.
513 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
514 RtpSourceType::CSRC);
515 } else {
516 // If it is an existing CSRC, move the object to the end of the list.
517 map_it->second->update_timestamp_ms(now_ms);
518 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
519 }
520 // Update the unordered_map.
521 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
522 }
523
524 // If this is the first packet or the SSRC is changed, insert a new
525 // contributing source that uses the SSRC.
526 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
527 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
528 } else {
529 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
530 }
531
zstein2b706342017-08-24 14:52:17 -0700532 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
533
hbos8d609f62017-04-10 07:39:05 -0700534 RemoveOutdatedSources(now_ms);
535}
536
537void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
538 std::list<RtpSource>::iterator it;
539 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
540 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
541 break;
542 }
543 iterator_by_csrc_.erase(it->source_id());
544 }
545 csrc_sources_.erase(csrc_sources_.begin(), it);
546
547 std::vector<RtpSource>::iterator vec_it;
548 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
549 ++vec_it) {
550 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
551 break;
552 }
553 }
554 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
555}
556
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000557} // namespace webrtc