blob: c68d28ec026383383c4b93e4cea4b19b322c7ed4 [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;
Karl Wibergc62f6c72017-10-04 12:38:53 +0200275 rtc::Optional<AudioPayload> reinitialize_audio_payload;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000276
277 {
danilchap7c9426c2016-04-14 03:05:31 -0700278 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000279
280 int8_t last_received_payload_type =
281 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000282 if (ssrc_ != rtp_header.ssrc ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000283 (last_received_payload_type == -1 && ssrc_ == 0)) {
284 // We need the payload_type_ to make the call if the remote SSRC is 0.
285 new_ssrc = true;
286
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000287 last_received_timestamp_ = 0;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000288 last_received_frame_time_ms_ = -1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000289
290 // Do we have a SSRC? Then the stream is restarted.
291 if (ssrc_ != 0) {
292 // Do we have the same codec? Then re-initialize coder.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000293 if (rtp_header.payloadType == last_received_payload_type) {
Karl Wiberg73b60b82017-09-21 15:00:58 +0200294 const auto payload = rtp_payload_registry_->PayloadTypeToPayload(
danilchap5c1def82015-12-10 09:51:54 -0800295 rtp_header.payloadType);
296 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000297 return;
298 }
Karl Wibergc856dc22017-09-28 20:13:59 +0200299 if (payload->typeSpecific.is_audio()) {
Karl Wibergc62f6c72017-10-04 12:38:53 +0200300 reinitialize_audio_payload.emplace(
301 payload->typeSpecific.audio_payload());
302 } else {
303 // OnInitializeDecoder() is only used for audio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000304 }
305 }
306 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000307 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000308 }
309 }
310
311 if (new_ssrc) {
312 // We need to get this to our RTCP sender and receiver.
313 // We need to do this outside critical section.
Peter Boströmac547a62015-09-17 23:03:57 +0200314 cb_rtp_feedback_->OnIncomingSSRCChanged(rtp_header.ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000315 }
316
Karl Wibergc62f6c72017-10-04 12:38:53 +0200317 if (reinitialize_audio_payload) {
318 if (-1 == cb_rtp_feedback_->OnInitializeDecoder(
319 rtp_header.payloadType, reinitialize_audio_payload->format,
320 reinitialize_audio_payload->rate)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000321 // New stream, same codec.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100322 RTC_LOG(LS_ERROR) << "Failed to create decoder for payload type: "
323 << static_cast<int>(rtp_header.payloadType);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000324 }
325 }
326}
327
328// Implementation note: must not hold critsect when called.
329// TODO(phoglund): Move as much as possible of this code path into the media
330// specific receivers. Basically this method goes through a lot of trouble to
331// compute something which is only used by the media specific parts later. If
332// this code path moves we can get rid of some of the rtp_receiver ->
333// media_specific interface (such as CheckPayloadChange, possibly get/set
334// last known payload).
pbosd4362982015-07-07 08:32:48 -0700335int32_t RtpReceiverImpl::CheckPayloadChanged(const RTPHeader& rtp_header,
336 const int8_t first_payload_byte,
pbosd4362982015-07-07 08:32:48 -0700337 PayloadUnion* specific_payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000338 bool re_initialize_decoder = false;
339
340 char payload_name[RTP_PAYLOAD_NAME_SIZE];
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000341 int8_t payload_type = rtp_header.payloadType;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000342
343 {
danilchap7c9426c2016-04-14 03:05:31 -0700344 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000345
346 int8_t last_received_payload_type =
347 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000348 // TODO(holmer): Remove this code when RED parsing has been broken out from
349 // RtpReceiverAudio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000350 if (payload_type != last_received_payload_type) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000351 bool should_discard_changes = false;
352
353 rtp_media_receiver_->CheckPayloadChanged(
pbosd4362982015-07-07 08:32:48 -0700354 payload_type, specific_payload,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000355 &should_discard_changes);
356
357 if (should_discard_changes) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000358 return 0;
359 }
360
Karl Wiberg73b60b82017-09-21 15:00:58 +0200361 const auto payload =
danilchap5c1def82015-12-10 09:51:54 -0800362 rtp_payload_registry_->PayloadTypeToPayload(payload_type);
363 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000364 // Not a registered payload type.
365 return -1;
366 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000367 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
368 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
369
370 rtp_payload_registry_->set_last_received_payload_type(payload_type);
371
372 re_initialize_decoder = true;
373
374 rtp_media_receiver_->SetLastMediaSpecificPayload(payload->typeSpecific);
375 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
376
Karl Wibergc856dc22017-09-28 20:13:59 +0200377 if (!payload->typeSpecific.is_audio()) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000378 bool media_type_unchanged =
379 rtp_payload_registry_->ReportMediaPayloadType(payload_type);
380 if (media_type_unchanged) {
381 // Only reset the decoder if the media codec type has changed.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000382 re_initialize_decoder = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000383 }
384 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000385 } else {
386 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000387 }
388 } // End critsect.
389
390 if (re_initialize_decoder) {
Peter Boströmac547a62015-09-17 23:03:57 +0200391 if (-1 ==
392 rtp_media_receiver_->InvokeOnInitializeDecoder(
393 cb_rtp_feedback_, payload_type, payload_name, *specific_payload)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000394 return -1; // Wrong payload type.
395 }
396 }
397 return 0;
398}
399
400// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000401void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000402 int32_t num_csrcs_diff = 0;
403 uint32_t old_remote_csrc[kRtpCsrcSize];
404 uint8_t old_num_csrcs = 0;
405
406 {
danilchap7c9426c2016-04-14 03:05:31 -0700407 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000408
409 if (!rtp_media_receiver_->ShouldReportCsrcChanges(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000410 rtp_header.header.payloadType)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000411 return;
412 }
413 old_num_csrcs = num_csrcs_;
414 if (old_num_csrcs > 0) {
415 // Make a copy of old.
416 memcpy(old_remote_csrc, current_remote_csrc_,
417 num_csrcs_ * sizeof(uint32_t));
418 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000419 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000420 if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
421 // Copy new.
422 memcpy(current_remote_csrc_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000423 rtp_header.header.arrOfCSRCs,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000424 num_csrcs * sizeof(uint32_t));
425 }
426 if (num_csrcs > 0 || old_num_csrcs > 0) {
427 num_csrcs_diff = num_csrcs - old_num_csrcs;
428 num_csrcs_ = num_csrcs; // Update stored CSRCs.
429 } else {
430 // No change.
431 return;
432 }
433 } // End critsect.
434
435 bool have_called_callback = false;
436 // Search for new CSRC in old array.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000437 for (uint8_t i = 0; i < rtp_header.header.numCSRCs; ++i) {
438 const uint32_t csrc = rtp_header.header.arrOfCSRCs[i];
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000439
440 bool found_match = false;
441 for (uint8_t j = 0; j < old_num_csrcs; ++j) {
442 if (csrc == old_remote_csrc[j]) { // old list
443 found_match = true;
444 break;
445 }
446 }
447 if (!found_match && csrc) {
448 // Didn't find it, report it as new.
449 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200450 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000451 }
452 }
453 // Search for old CSRC in new array.
454 for (uint8_t i = 0; i < old_num_csrcs; ++i) {
455 const uint32_t csrc = old_remote_csrc[i];
456
457 bool found_match = false;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000458 for (uint8_t j = 0; j < rtp_header.header.numCSRCs; ++j) {
459 if (csrc == rtp_header.header.arrOfCSRCs[j]) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000460 found_match = true;
461 break;
462 }
463 }
464 if (!found_match && csrc) {
465 // Did not find it, report as removed.
466 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200467 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000468 }
469 }
470 if (!have_called_callback) {
471 // If the CSRC list contain non-unique entries we will end up here.
472 // Using CSRC 0 to signal this event, not interop safe, other
473 // implementations might have CSRC 0 as a valid value.
474 if (num_csrcs_diff > 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200475 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000476 } else if (num_csrcs_diff < 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200477 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000478 }
479 }
480}
481
zstein2b706342017-08-24 14:52:17 -0700482void RtpReceiverImpl::UpdateSources(
483 const rtc::Optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700484 rtc::CritScope lock(&critical_section_rtp_receiver_);
485 int64_t now_ms = clock_->TimeInMilliseconds();
486
487 for (size_t i = 0; i < num_csrcs_; ++i) {
488 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
489 if (map_it == iterator_by_csrc_.end()) {
490 // If it is a new CSRC, append a new object to the end of the list.
491 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
492 RtpSourceType::CSRC);
493 } else {
494 // If it is an existing CSRC, move the object to the end of the list.
495 map_it->second->update_timestamp_ms(now_ms);
496 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
497 }
498 // Update the unordered_map.
499 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
500 }
501
502 // If this is the first packet or the SSRC is changed, insert a new
503 // contributing source that uses the SSRC.
504 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
505 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
506 } else {
507 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
508 }
509
zstein2b706342017-08-24 14:52:17 -0700510 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
511
hbos8d609f62017-04-10 07:39:05 -0700512 RemoveOutdatedSources(now_ms);
513}
514
515void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
516 std::list<RtpSource>::iterator it;
517 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
518 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
519 break;
520 }
521 iterator_by_csrc_.erase(it->source_id());
522 }
523 csrc_sources_.erase(csrc_sources_.begin(), it);
524
525 std::vector<RtpSource>::iterator vec_it;
526 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
527 ++vec_it) {
528 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
529 break;
530 }
531 }
532 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
533}
534
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000535} // namespace webrtc