blob: 45faee2d90852dcf82d82a09d06f4cb1e699c970 [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)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
23#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
24#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
25#include "rtc_base/logging.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000026
27namespace webrtc {
28
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000029using RtpUtility::Payload;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030
hbos8d609f62017-04-10 07:39:05 -070031// Only return the sources in the last 10 seconds.
32const int64_t kGetSourcesTimeoutMs = 10000;
33
wu@webrtc.org822fbd82013-08-15 23:38:54 +000034RtpReceiver* RtpReceiver::CreateVideoReceiver(
Peter Boströmac547a62015-09-17 23:03:57 +020035 Clock* clock,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000036 RtpData* incoming_payload_callback,
37 RtpFeedback* incoming_messages_callback,
38 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070039 RTC_DCHECK(incoming_payload_callback != nullptr);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000040 if (!incoming_messages_callback)
41 incoming_messages_callback = NullObjectRtpFeedback();
42 return new RtpReceiverImpl(
solenberg1d031392016-03-30 02:42:32 -070043 clock, incoming_messages_callback, rtp_payload_registry,
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000044 RTPReceiverStrategy::CreateVideoStrategy(incoming_payload_callback));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045}
46
47RtpReceiver* RtpReceiver::CreateAudioReceiver(
Peter Boströmac547a62015-09-17 23:03:57 +020048 Clock* clock,
solenberg1d031392016-03-30 02:42:32 -070049 RtpData* incoming_payload_callback,
50 RtpFeedback* incoming_messages_callback,
51 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070052 RTC_DCHECK(incoming_payload_callback != nullptr);
solenberg1d031392016-03-30 02:42:32 -070053 if (!incoming_messages_callback)
54 incoming_messages_callback = NullObjectRtpFeedback();
55 return new RtpReceiverImpl(
56 clock, incoming_messages_callback, rtp_payload_registry,
57 RTPReceiverStrategy::CreateAudioStrategy(incoming_payload_callback));
58}
59
hbos8d609f62017-04-10 07:39:05 -070060RtpReceiverImpl::RtpReceiverImpl(Clock* clock,
61 RtpFeedback* incoming_messages_callback,
62 RTPPayloadRegistry* rtp_payload_registry,
63 RTPReceiverStrategy* rtp_media_receiver)
wu@webrtc.org822fbd82013-08-15 23:38:54 +000064 : clock_(clock),
65 rtp_payload_registry_(rtp_payload_registry),
66 rtp_media_receiver_(rtp_media_receiver),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000067 cb_rtp_feedback_(incoming_messages_callback),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000068 ssrc_(0),
69 num_csrcs_(0),
70 current_remote_csrc_(),
71 last_received_timestamp_(0),
Niels Möllerbbf389c2017-09-26 14:05:05 +020072 last_received_frame_time_ms_(-1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000073 assert(incoming_messages_callback);
74
75 memset(current_remote_csrc_, 0, sizeof(current_remote_csrc_));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000076}
77
78RtpReceiverImpl::~RtpReceiverImpl() {
79 for (int i = 0; i < num_csrcs_; ++i) {
Peter Boströmac547a62015-09-17 23:03:57 +020080 cb_rtp_feedback_->OnIncomingCSRCChanged(current_remote_csrc_[i], false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000081 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000082}
83
magjed56124bd2016-11-24 09:34:46 -080084int32_t RtpReceiverImpl::RegisterReceivePayload(const CodecInst& audio_codec) {
danilchap7c9426c2016-04-14 03:05:31 -070085 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000086
87 // TODO(phoglund): Try to streamline handling of the RED codec and some other
88 // cases which makes it necessary to keep track of whether we created a
89 // payload or not.
90 bool created_new_payload = false;
91 int32_t result = rtp_payload_registry_->RegisterReceivePayload(
magjed56124bd2016-11-24 09:34:46 -080092 audio_codec, &created_new_payload);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000093 if (created_new_payload) {
magjed56124bd2016-11-24 09:34:46 -080094 if (rtp_media_receiver_->OnNewPayloadTypeCreated(audio_codec) != 0) {
95 LOG(LS_ERROR) << "Failed to register payload: " << audio_codec.plname
96 << "/" << static_cast<int>(audio_codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097 return -1;
98 }
99 }
100 return result;
101}
102
magjed6b272c52016-11-25 02:29:39 -0800103int32_t RtpReceiverImpl::RegisterReceivePayload(const VideoCodec& video_codec) {
104 rtc::CritScope lock(&critical_section_rtp_receiver_);
105 return rtp_payload_registry_->RegisterReceivePayload(video_codec);
106}
107
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000108int32_t RtpReceiverImpl::DeRegisterReceivePayload(
109 const int8_t payload_type) {
danilchap7c9426c2016-04-14 03:05:31 -0700110 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000111 return rtp_payload_registry_->DeRegisterReceivePayload(payload_type);
112}
113
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114uint32_t RtpReceiverImpl::SSRC() const {
danilchap7c9426c2016-04-14 03:05:31 -0700115 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000116 return ssrc_;
117}
118
119// Get remote CSRC.
120int32_t RtpReceiverImpl::CSRCs(uint32_t array_of_csrcs[kRtpCsrcSize]) const {
danilchap7c9426c2016-04-14 03:05:31 -0700121 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000122
123 assert(num_csrcs_ <= kRtpCsrcSize);
124
125 if (num_csrcs_ > 0) {
126 memcpy(array_of_csrcs, current_remote_csrc_, sizeof(uint32_t)*num_csrcs_);
127 }
128 return num_csrcs_;
129}
130
131int32_t RtpReceiverImpl::Energy(
132 uint8_t array_of_energy[kRtpCsrcSize]) const {
133 return rtp_media_receiver_->Energy(array_of_energy);
134}
135
136bool RtpReceiverImpl::IncomingRtpPacket(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000137 const RTPHeader& rtp_header,
138 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000139 size_t payload_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000140 PayloadUnion payload_specific,
141 bool in_order) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000142 // Trigger our callbacks.
143 CheckSSRCChanged(rtp_header);
144
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000145 int8_t first_payload_byte = payload_length > 0 ? payload[0] : 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000146 bool is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000147
danilchap6db6cdc2015-12-15 02:54:47 -0800148 if (CheckPayloadChanged(rtp_header, first_payload_byte, &is_red,
pbosd4362982015-07-07 08:32:48 -0700149 &payload_specific) == -1) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000150 if (payload_length == 0) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000151 // OK, keep-alive packet.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000152 return true;
153 }
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000154 LOG(LS_WARNING) << "Receiving invalid payload type.";
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000155 return false;
156 }
157
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000158 WebRtcRTPHeader webrtc_rtp_header;
159 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000160 webrtc_rtp_header.header = rtp_header;
161 CheckCSRC(webrtc_rtp_header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000162
zstein2b706342017-08-24 14:52:17 -0700163 auto audio_level =
164 rtp_header.extension.hasAudioLevel
165 ? rtc::Optional<uint8_t>(rtp_header.extension.audioLevel)
166 : rtc::Optional<uint8_t>();
167 UpdateSources(audio_level);
hbos8d609f62017-04-10 07:39:05 -0700168
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000169 int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000170 &webrtc_rtp_header, payload_specific, is_red, payload, payload_length,
Niels Möllerbbf389c2017-09-26 14:05:05 +0200171 clock_->TimeInMilliseconds());
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000172
173 if (ret_val < 0) {
174 return false;
175 }
176
177 {
danilchap7c9426c2016-04-14 03:05:31 -0700178 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000179
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000180 if (in_order) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000181 if (last_received_timestamp_ != rtp_header.timestamp) {
182 last_received_timestamp_ = rtp_header.timestamp;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000183 last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
184 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000185 }
186 }
187 return true;
188}
189
danilchap799a9d02016-09-22 03:36:27 -0700190TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
191 return rtp_media_receiver_->GetTelephoneEventHandler();
192}
193
hbos8d609f62017-04-10 07:39:05 -0700194std::vector<RtpSource> RtpReceiverImpl::GetSources() const {
zhihuang04262222017-04-11 11:28:10 -0700195 rtc::CritScope lock(&critical_section_rtp_receiver_);
196
hbos8d609f62017-04-10 07:39:05 -0700197 int64_t now_ms = clock_->TimeInMilliseconds();
198 std::vector<RtpSource> sources;
199
zhihuang04262222017-04-11 11:28:10 -0700200 RTC_DCHECK(std::is_sorted(ssrc_sources_.begin(), ssrc_sources_.end(),
201 [](const RtpSource& lhs, const RtpSource& rhs) {
202 return lhs.timestamp_ms() < rhs.timestamp_ms();
203 }));
204 RTC_DCHECK(std::is_sorted(csrc_sources_.begin(), csrc_sources_.end(),
205 [](const RtpSource& lhs, const RtpSource& rhs) {
206 return lhs.timestamp_ms() < rhs.timestamp_ms();
207 }));
hbos8d609f62017-04-10 07:39:05 -0700208
zhihuang04262222017-04-11 11:28:10 -0700209 std::set<uint32_t> selected_ssrcs;
210 for (auto rit = ssrc_sources_.rbegin(); rit != ssrc_sources_.rend(); ++rit) {
211 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
212 break;
hbos8d609f62017-04-10 07:39:05 -0700213 }
zhihuang04262222017-04-11 11:28:10 -0700214 if (selected_ssrcs.insert(rit->source_id()).second) {
hbos8d609f62017-04-10 07:39:05 -0700215 sources.push_back(*rit);
216 }
zhihuang04262222017-04-11 11:28:10 -0700217 }
hbos8d609f62017-04-10 07:39:05 -0700218
zhihuang04262222017-04-11 11:28:10 -0700219 for (auto rit = csrc_sources_.rbegin(); rit != csrc_sources_.rend(); ++rit) {
220 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
221 break;
222 }
223 sources.push_back(*rit);
224 }
hbos8d609f62017-04-10 07:39:05 -0700225 return sources;
226}
227
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200228bool RtpReceiverImpl::GetLatestTimestamps(uint32_t* timestamp,
229 int64_t* receive_time_ms) const {
danilchap7c9426c2016-04-14 03:05:31 -0700230 rtc::CritScope lock(&critical_section_rtp_receiver_);
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200231 if (last_received_frame_time_ms_ < 0)
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000232 return false;
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200233
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000234 *timestamp = last_received_timestamp_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000235 *receive_time_ms = last_received_frame_time_ms_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000236
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200237 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000238}
239
240// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000241void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000242 bool new_ssrc = false;
243 bool re_initialize_decoder = false;
244 char payload_name[RTP_PAYLOAD_NAME_SIZE];
Peter Kasting69558702016-01-12 16:26:35 -0800245 size_t channels = 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000246 uint32_t rate = 0;
247
248 {
danilchap7c9426c2016-04-14 03:05:31 -0700249 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000250
251 int8_t last_received_payload_type =
252 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000253 if (ssrc_ != rtp_header.ssrc ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000254 (last_received_payload_type == -1 && ssrc_ == 0)) {
255 // We need the payload_type_ to make the call if the remote SSRC is 0.
256 new_ssrc = true;
257
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000258 last_received_timestamp_ = 0;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000259 last_received_frame_time_ms_ = -1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000260
261 // Do we have a SSRC? Then the stream is restarted.
262 if (ssrc_ != 0) {
263 // Do we have the same codec? Then re-initialize coder.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000264 if (rtp_header.payloadType == last_received_payload_type) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000265 re_initialize_decoder = true;
266
Karl Wiberg73b60b82017-09-21 15:00:58 +0200267 const auto payload = rtp_payload_registry_->PayloadTypeToPayload(
danilchap5c1def82015-12-10 09:51:54 -0800268 rtp_header.payloadType);
269 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000270 return;
271 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000272 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
273 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
Karl Wibergc856dc22017-09-28 20:13:59 +0200274 if (payload->typeSpecific.is_audio()) {
275 channels = payload->typeSpecific.audio_payload().channels;
276 rate = payload->typeSpecific.audio_payload().rate;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000277 }
278 }
279 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000280 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000281 }
282 }
283
284 if (new_ssrc) {
285 // We need to get this to our RTCP sender and receiver.
286 // We need to do this outside critical section.
Peter Boströmac547a62015-09-17 23:03:57 +0200287 cb_rtp_feedback_->OnIncomingSSRCChanged(rtp_header.ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288 }
289
290 if (re_initialize_decoder) {
Peter Boströmac547a62015-09-17 23:03:57 +0200291 if (-1 ==
292 cb_rtp_feedback_->OnInitializeDecoder(
293 rtp_header.payloadType, payload_name,
294 rtp_header.payload_type_frequency, channels, rate)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000295 // New stream, same codec.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000296 LOG(LS_ERROR) << "Failed to create decoder for payload type: "
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000297 << static_cast<int>(rtp_header.payloadType);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000298 }
299 }
300}
301
302// Implementation note: must not hold critsect when called.
303// TODO(phoglund): Move as much as possible of this code path into the media
304// specific receivers. Basically this method goes through a lot of trouble to
305// compute something which is only used by the media specific parts later. If
306// this code path moves we can get rid of some of the rtp_receiver ->
307// media_specific interface (such as CheckPayloadChange, possibly get/set
308// last known payload).
pbosd4362982015-07-07 08:32:48 -0700309int32_t RtpReceiverImpl::CheckPayloadChanged(const RTPHeader& rtp_header,
310 const int8_t first_payload_byte,
danilchap6db6cdc2015-12-15 02:54:47 -0800311 bool* is_red,
pbosd4362982015-07-07 08:32:48 -0700312 PayloadUnion* specific_payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000313 bool re_initialize_decoder = false;
314
315 char payload_name[RTP_PAYLOAD_NAME_SIZE];
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316 int8_t payload_type = rtp_header.payloadType;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000317
318 {
danilchap7c9426c2016-04-14 03:05:31 -0700319 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000320
321 int8_t last_received_payload_type =
322 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000323 // TODO(holmer): Remove this code when RED parsing has been broken out from
324 // RtpReceiverAudio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000325 if (payload_type != last_received_payload_type) {
326 if (rtp_payload_registry_->red_payload_type() == payload_type) {
327 // Get the real codec payload type.
328 payload_type = first_payload_byte & 0x7f;
danilchap6db6cdc2015-12-15 02:54:47 -0800329 *is_red = true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000330
331 if (rtp_payload_registry_->red_payload_type() == payload_type) {
332 // Invalid payload type, traced by caller. If we proceeded here,
333 // this would be set as |_last_received_payload_type|, and we would no
334 // longer catch corrupt packets at this level.
335 return -1;
336 }
337
338 // When we receive RED we need to check the real payload type.
339 if (payload_type == last_received_payload_type) {
340 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
341 return 0;
342 }
343 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000344 bool should_discard_changes = false;
345
346 rtp_media_receiver_->CheckPayloadChanged(
pbosd4362982015-07-07 08:32:48 -0700347 payload_type, specific_payload,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000348 &should_discard_changes);
349
350 if (should_discard_changes) {
danilchap6db6cdc2015-12-15 02:54:47 -0800351 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000352 return 0;
353 }
354
Karl Wiberg73b60b82017-09-21 15:00:58 +0200355 const auto payload =
danilchap5c1def82015-12-10 09:51:54 -0800356 rtp_payload_registry_->PayloadTypeToPayload(payload_type);
357 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000358 // Not a registered payload type.
359 return -1;
360 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000361 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
362 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
363
364 rtp_payload_registry_->set_last_received_payload_type(payload_type);
365
366 re_initialize_decoder = true;
367
368 rtp_media_receiver_->SetLastMediaSpecificPayload(payload->typeSpecific);
369 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
370
Karl Wibergc856dc22017-09-28 20:13:59 +0200371 if (!payload->typeSpecific.is_audio()) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000372 bool media_type_unchanged =
373 rtp_payload_registry_->ReportMediaPayloadType(payload_type);
374 if (media_type_unchanged) {
375 // Only reset the decoder if the media codec type has changed.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000376 re_initialize_decoder = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000377 }
378 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000379 } else {
380 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
danilchap6db6cdc2015-12-15 02:54:47 -0800381 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000382 }
383 } // End critsect.
384
385 if (re_initialize_decoder) {
Peter Boströmac547a62015-09-17 23:03:57 +0200386 if (-1 ==
387 rtp_media_receiver_->InvokeOnInitializeDecoder(
388 cb_rtp_feedback_, payload_type, payload_name, *specific_payload)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000389 return -1; // Wrong payload type.
390 }
391 }
392 return 0;
393}
394
395// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000396void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000397 int32_t num_csrcs_diff = 0;
398 uint32_t old_remote_csrc[kRtpCsrcSize];
399 uint8_t old_num_csrcs = 0;
400
401 {
danilchap7c9426c2016-04-14 03:05:31 -0700402 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000403
404 if (!rtp_media_receiver_->ShouldReportCsrcChanges(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000405 rtp_header.header.payloadType)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000406 return;
407 }
408 old_num_csrcs = num_csrcs_;
409 if (old_num_csrcs > 0) {
410 // Make a copy of old.
411 memcpy(old_remote_csrc, current_remote_csrc_,
412 num_csrcs_ * sizeof(uint32_t));
413 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000414 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000415 if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
416 // Copy new.
417 memcpy(current_remote_csrc_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000418 rtp_header.header.arrOfCSRCs,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000419 num_csrcs * sizeof(uint32_t));
420 }
421 if (num_csrcs > 0 || old_num_csrcs > 0) {
422 num_csrcs_diff = num_csrcs - old_num_csrcs;
423 num_csrcs_ = num_csrcs; // Update stored CSRCs.
424 } else {
425 // No change.
426 return;
427 }
428 } // End critsect.
429
430 bool have_called_callback = false;
431 // Search for new CSRC in old array.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000432 for (uint8_t i = 0; i < rtp_header.header.numCSRCs; ++i) {
433 const uint32_t csrc = rtp_header.header.arrOfCSRCs[i];
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000434
435 bool found_match = false;
436 for (uint8_t j = 0; j < old_num_csrcs; ++j) {
437 if (csrc == old_remote_csrc[j]) { // old list
438 found_match = true;
439 break;
440 }
441 }
442 if (!found_match && csrc) {
443 // Didn't find it, report it as new.
444 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200445 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000446 }
447 }
448 // Search for old CSRC in new array.
449 for (uint8_t i = 0; i < old_num_csrcs; ++i) {
450 const uint32_t csrc = old_remote_csrc[i];
451
452 bool found_match = false;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000453 for (uint8_t j = 0; j < rtp_header.header.numCSRCs; ++j) {
454 if (csrc == rtp_header.header.arrOfCSRCs[j]) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000455 found_match = true;
456 break;
457 }
458 }
459 if (!found_match && csrc) {
460 // Did not find it, report as removed.
461 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200462 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000463 }
464 }
465 if (!have_called_callback) {
466 // If the CSRC list contain non-unique entries we will end up here.
467 // Using CSRC 0 to signal this event, not interop safe, other
468 // implementations might have CSRC 0 as a valid value.
469 if (num_csrcs_diff > 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200470 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000471 } else if (num_csrcs_diff < 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200472 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000473 }
474 }
475}
476
zstein2b706342017-08-24 14:52:17 -0700477void RtpReceiverImpl::UpdateSources(
478 const rtc::Optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700479 rtc::CritScope lock(&critical_section_rtp_receiver_);
480 int64_t now_ms = clock_->TimeInMilliseconds();
481
482 for (size_t i = 0; i < num_csrcs_; ++i) {
483 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
484 if (map_it == iterator_by_csrc_.end()) {
485 // If it is a new CSRC, append a new object to the end of the list.
486 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
487 RtpSourceType::CSRC);
488 } else {
489 // If it is an existing CSRC, move the object to the end of the list.
490 map_it->second->update_timestamp_ms(now_ms);
491 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
492 }
493 // Update the unordered_map.
494 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
495 }
496
497 // If this is the first packet or the SSRC is changed, insert a new
498 // contributing source that uses the SSRC.
499 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
500 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
501 } else {
502 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
503 }
504
zstein2b706342017-08-24 14:52:17 -0700505 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
506
hbos8d609f62017-04-10 07:39:05 -0700507 RemoveOutdatedSources(now_ms);
508}
509
510void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
511 std::list<RtpSource>::iterator it;
512 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
513 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
514 break;
515 }
516 iterator_by_csrc_.erase(it->source_id());
517 }
518 csrc_sources_.erase(csrc_sources_.begin(), it);
519
520 std::vector<RtpSource>::iterator vec_it;
521 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
522 ++vec_it) {
523 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
524 break;
525 }
526 }
527 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
528}
529
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000530} // namespace webrtc