blob: 1fd45bd3ff652db43030d560dfe982e3daf2f103 [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
Niels Möller31791e72018-03-14 11:27:26 +0100175 if (CheckPayloadChanged(rtp_header, first_payload_byte,
pbosd4362982015-07-07 08:32:48 -0700176 &payload_specific) == -1) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000177 if (payload_length == 0) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000178 // OK, keep-alive packet.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000179 return true;
180 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_WARNING) << "Receiving invalid payload type.";
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000182 return false;
183 }
184
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000185 WebRtcRTPHeader webrtc_rtp_header;
186 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000187 webrtc_rtp_header.header = rtp_header;
188 CheckCSRC(webrtc_rtp_header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000189
zstein2b706342017-08-24 14:52:17 -0700190 auto audio_level =
191 rtp_header.extension.hasAudioLevel
192 ? rtc::Optional<uint8_t>(rtp_header.extension.audioLevel)
Oskar Sundbom3419cf92017-11-16 10:55:48 +0100193 : rtc::nullopt;
zstein2b706342017-08-24 14:52:17 -0700194 UpdateSources(audio_level);
hbos8d609f62017-04-10 07:39:05 -0700195
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000196 int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
Niels Möller31791e72018-03-14 11:27:26 +0100197 &webrtc_rtp_header, payload_specific, payload, payload_length,
Niels Möllerbbf389c2017-09-26 14:05:05 +0200198 clock_->TimeInMilliseconds());
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000199
200 if (ret_val < 0) {
201 return false;
202 }
203
204 {
danilchap7c9426c2016-04-14 03:05:31 -0700205 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000206
Niels Möller22ec9522017-10-05 08:39:15 +0200207 // TODO(nisse): Do not rely on InOrderPacket for recovered packets, when
208 // packet is passed as RtpPacketReceived and that information is available.
209 // We should ideally never record timestamps for retransmitted or recovered
210 // packets.
211 if (InOrderPacket(last_received_sequence_number_,
212 rtp_header.sequenceNumber)) {
213 last_received_sequence_number_.emplace(rtp_header.sequenceNumber);
214 last_received_timestamp_ = rtp_header.timestamp;
215 last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000216 }
217 }
Niels Möller22ec9522017-10-05 08:39:15 +0200218
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000219 return true;
220}
221
danilchap799a9d02016-09-22 03:36:27 -0700222TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
223 return rtp_media_receiver_->GetTelephoneEventHandler();
224}
225
hbos8d609f62017-04-10 07:39:05 -0700226std::vector<RtpSource> RtpReceiverImpl::GetSources() const {
zhihuang04262222017-04-11 11:28:10 -0700227 rtc::CritScope lock(&critical_section_rtp_receiver_);
228
hbos8d609f62017-04-10 07:39:05 -0700229 int64_t now_ms = clock_->TimeInMilliseconds();
230 std::vector<RtpSource> sources;
231
zhihuang04262222017-04-11 11:28:10 -0700232 RTC_DCHECK(std::is_sorted(ssrc_sources_.begin(), ssrc_sources_.end(),
233 [](const RtpSource& lhs, const RtpSource& rhs) {
234 return lhs.timestamp_ms() < rhs.timestamp_ms();
235 }));
236 RTC_DCHECK(std::is_sorted(csrc_sources_.begin(), csrc_sources_.end(),
237 [](const RtpSource& lhs, const RtpSource& rhs) {
238 return lhs.timestamp_ms() < rhs.timestamp_ms();
239 }));
hbos8d609f62017-04-10 07:39:05 -0700240
zhihuang04262222017-04-11 11:28:10 -0700241 std::set<uint32_t> selected_ssrcs;
242 for (auto rit = ssrc_sources_.rbegin(); rit != ssrc_sources_.rend(); ++rit) {
243 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
244 break;
hbos8d609f62017-04-10 07:39:05 -0700245 }
zhihuang04262222017-04-11 11:28:10 -0700246 if (selected_ssrcs.insert(rit->source_id()).second) {
hbos8d609f62017-04-10 07:39:05 -0700247 sources.push_back(*rit);
248 }
zhihuang04262222017-04-11 11:28:10 -0700249 }
hbos8d609f62017-04-10 07:39:05 -0700250
zhihuang04262222017-04-11 11:28:10 -0700251 for (auto rit = csrc_sources_.rbegin(); rit != csrc_sources_.rend(); ++rit) {
252 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
253 break;
254 }
255 sources.push_back(*rit);
256 }
hbos8d609f62017-04-10 07:39:05 -0700257 return sources;
258}
259
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200260bool RtpReceiverImpl::GetLatestTimestamps(uint32_t* timestamp,
261 int64_t* receive_time_ms) const {
danilchap7c9426c2016-04-14 03:05:31 -0700262 rtc::CritScope lock(&critical_section_rtp_receiver_);
Niels Möller22ec9522017-10-05 08:39:15 +0200263 if (!last_received_sequence_number_)
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000264 return false;
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200265
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000266 *timestamp = last_received_timestamp_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000267 *receive_time_ms = last_received_frame_time_ms_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000268
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200269 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000270}
271
272// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000273void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000274 bool new_ssrc = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000275
276 {
danilchap7c9426c2016-04-14 03:05:31 -0700277 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000278
279 int8_t last_received_payload_type =
280 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000281 if (ssrc_ != rtp_header.ssrc ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000282 (last_received_payload_type == -1 && ssrc_ == 0)) {
283 // We need the payload_type_ to make the call if the remote SSRC is 0.
284 new_ssrc = true;
285
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000286 last_received_timestamp_ = 0;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000287 last_received_frame_time_ms_ = -1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000289 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000290 }
291 }
292
293 if (new_ssrc) {
294 // We need to get this to our RTCP sender and receiver.
295 // We need to do this outside critical section.
Peter Boströmac547a62015-09-17 23:03:57 +0200296 cb_rtp_feedback_->OnIncomingSSRCChanged(rtp_header.ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000297 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000298}
299
300// Implementation note: must not hold critsect when called.
301// TODO(phoglund): Move as much as possible of this code path into the media
302// specific receivers. Basically this method goes through a lot of trouble to
303// compute something which is only used by the media specific parts later. If
304// this code path moves we can get rid of some of the rtp_receiver ->
305// media_specific interface (such as CheckPayloadChange, possibly get/set
306// last known payload).
pbosd4362982015-07-07 08:32:48 -0700307int32_t RtpReceiverImpl::CheckPayloadChanged(const RTPHeader& rtp_header,
308 const int8_t first_payload_byte,
pbosd4362982015-07-07 08:32:48 -0700309 PayloadUnion* specific_payload) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000310 int8_t payload_type = rtp_header.payloadType;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000311
312 {
danilchap7c9426c2016-04-14 03:05:31 -0700313 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000314
315 int8_t last_received_payload_type =
316 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000317 // TODO(holmer): Remove this code when RED parsing has been broken out from
318 // RtpReceiverAudio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000319 if (payload_type != last_received_payload_type) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000320 bool should_discard_changes = false;
321
322 rtp_media_receiver_->CheckPayloadChanged(
pbosd4362982015-07-07 08:32:48 -0700323 payload_type, specific_payload,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000324 &should_discard_changes);
325
326 if (should_discard_changes) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000327 return 0;
328 }
329
Karl Wiberg73b60b82017-09-21 15:00:58 +0200330 const auto payload =
danilchap5c1def82015-12-10 09:51:54 -0800331 rtp_payload_registry_->PayloadTypeToPayload(payload_type);
332 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000333 // Not a registered payload type.
334 return -1;
335 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000336 rtp_payload_registry_->set_last_received_payload_type(payload_type);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000337 }
338 } // End critsect.
339
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000340 return 0;
341}
342
343// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000344void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000345 int32_t num_csrcs_diff = 0;
346 uint32_t old_remote_csrc[kRtpCsrcSize];
347 uint8_t old_num_csrcs = 0;
348
349 {
danilchap7c9426c2016-04-14 03:05:31 -0700350 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000351
352 if (!rtp_media_receiver_->ShouldReportCsrcChanges(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000353 rtp_header.header.payloadType)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000354 return;
355 }
356 old_num_csrcs = num_csrcs_;
357 if (old_num_csrcs > 0) {
358 // Make a copy of old.
359 memcpy(old_remote_csrc, current_remote_csrc_,
360 num_csrcs_ * sizeof(uint32_t));
361 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000362 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000363 if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
364 // Copy new.
365 memcpy(current_remote_csrc_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000366 rtp_header.header.arrOfCSRCs,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000367 num_csrcs * sizeof(uint32_t));
368 }
369 if (num_csrcs > 0 || old_num_csrcs > 0) {
370 num_csrcs_diff = num_csrcs - old_num_csrcs;
371 num_csrcs_ = num_csrcs; // Update stored CSRCs.
372 } else {
373 // No change.
374 return;
375 }
376 } // End critsect.
377
378 bool have_called_callback = false;
379 // Search for new CSRC in old array.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000380 for (uint8_t i = 0; i < rtp_header.header.numCSRCs; ++i) {
381 const uint32_t csrc = rtp_header.header.arrOfCSRCs[i];
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000382
383 bool found_match = false;
384 for (uint8_t j = 0; j < old_num_csrcs; ++j) {
385 if (csrc == old_remote_csrc[j]) { // old list
386 found_match = true;
387 break;
388 }
389 }
390 if (!found_match && csrc) {
391 // Didn't find it, report it as new.
392 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200393 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000394 }
395 }
396 // Search for old CSRC in new array.
397 for (uint8_t i = 0; i < old_num_csrcs; ++i) {
398 const uint32_t csrc = old_remote_csrc[i];
399
400 bool found_match = false;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000401 for (uint8_t j = 0; j < rtp_header.header.numCSRCs; ++j) {
402 if (csrc == rtp_header.header.arrOfCSRCs[j]) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000403 found_match = true;
404 break;
405 }
406 }
407 if (!found_match && csrc) {
408 // Did not find it, report as removed.
409 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200410 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000411 }
412 }
413 if (!have_called_callback) {
414 // If the CSRC list contain non-unique entries we will end up here.
415 // Using CSRC 0 to signal this event, not interop safe, other
416 // implementations might have CSRC 0 as a valid value.
417 if (num_csrcs_diff > 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200418 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000419 } else if (num_csrcs_diff < 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200420 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000421 }
422 }
423}
424
zstein2b706342017-08-24 14:52:17 -0700425void RtpReceiverImpl::UpdateSources(
426 const rtc::Optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700427 rtc::CritScope lock(&critical_section_rtp_receiver_);
428 int64_t now_ms = clock_->TimeInMilliseconds();
429
430 for (size_t i = 0; i < num_csrcs_; ++i) {
431 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
432 if (map_it == iterator_by_csrc_.end()) {
433 // If it is a new CSRC, append a new object to the end of the list.
434 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
435 RtpSourceType::CSRC);
436 } else {
437 // If it is an existing CSRC, move the object to the end of the list.
438 map_it->second->update_timestamp_ms(now_ms);
439 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
440 }
441 // Update the unordered_map.
442 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
443 }
444
445 // If this is the first packet or the SSRC is changed, insert a new
446 // contributing source that uses the SSRC.
447 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
448 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
449 } else {
450 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
451 }
452
zstein2b706342017-08-24 14:52:17 -0700453 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
454
hbos8d609f62017-04-10 07:39:05 -0700455 RemoveOutdatedSources(now_ms);
456}
457
458void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
459 std::list<RtpSource>::iterator it;
460 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
461 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
462 break;
463 }
464 iterator_by_csrc_.erase(it->source_id());
465 }
466 csrc_sources_.erase(csrc_sources_.begin(), it);
467
468 std::vector<RtpSource>::iterator vec_it;
469 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
470 ++vec_it) {
471 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
472 break;
473 }
474 }
475 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
476}
477
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000478} // namespace webrtc