blob: c200c1f39801e8d2d64cb76e4a68d12e5909af8b [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
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000228bool RtpReceiverImpl::Timestamp(uint32_t* timestamp) const {
danilchap7c9426c2016-04-14 03:05:31 -0700229 rtc::CritScope lock(&critical_section_rtp_receiver_);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000230 if (!HaveReceivedFrame())
231 return false;
232 *timestamp = last_received_timestamp_;
233 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000234}
235
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000236bool RtpReceiverImpl::LastReceivedTimeMs(int64_t* receive_time_ms) const {
danilchap7c9426c2016-04-14 03:05:31 -0700237 rtc::CritScope lock(&critical_section_rtp_receiver_);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000238 if (!HaveReceivedFrame())
239 return false;
240 *receive_time_ms = last_received_frame_time_ms_;
241 return true;
242}
243
244bool RtpReceiverImpl::HaveReceivedFrame() const {
245 return last_received_frame_time_ms_ >= 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000246}
247
248// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000249void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000250 bool new_ssrc = false;
251 bool re_initialize_decoder = false;
252 char payload_name[RTP_PAYLOAD_NAME_SIZE];
Peter Kasting69558702016-01-12 16:26:35 -0800253 size_t channels = 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000254 uint32_t rate = 0;
255
256 {
danilchap7c9426c2016-04-14 03:05:31 -0700257 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000258
259 int8_t last_received_payload_type =
260 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000261 if (ssrc_ != rtp_header.ssrc ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000262 (last_received_payload_type == -1 && ssrc_ == 0)) {
263 // We need the payload_type_ to make the call if the remote SSRC is 0.
264 new_ssrc = true;
265
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000266 last_received_timestamp_ = 0;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000267 last_received_frame_time_ms_ = -1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000268
269 // Do we have a SSRC? Then the stream is restarted.
270 if (ssrc_ != 0) {
271 // Do we have the same codec? Then re-initialize coder.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000272 if (rtp_header.payloadType == last_received_payload_type) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000273 re_initialize_decoder = true;
274
Karl Wiberg73b60b82017-09-21 15:00:58 +0200275 const auto payload = rtp_payload_registry_->PayloadTypeToPayload(
danilchap5c1def82015-12-10 09:51:54 -0800276 rtp_header.payloadType);
277 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000278 return;
279 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000280 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
281 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
282 if (payload->audio) {
283 channels = payload->typeSpecific.Audio.channels;
284 rate = payload->typeSpecific.Audio.rate;
285 }
286 }
287 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000288 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000289 }
290 }
291
292 if (new_ssrc) {
293 // We need to get this to our RTCP sender and receiver.
294 // We need to do this outside critical section.
Peter Boströmac547a62015-09-17 23:03:57 +0200295 cb_rtp_feedback_->OnIncomingSSRCChanged(rtp_header.ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000296 }
297
298 if (re_initialize_decoder) {
Peter Boströmac547a62015-09-17 23:03:57 +0200299 if (-1 ==
300 cb_rtp_feedback_->OnInitializeDecoder(
301 rtp_header.payloadType, payload_name,
302 rtp_header.payload_type_frequency, channels, rate)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000303 // New stream, same codec.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000304 LOG(LS_ERROR) << "Failed to create decoder for payload type: "
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000305 << static_cast<int>(rtp_header.payloadType);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000306 }
307 }
308}
309
310// Implementation note: must not hold critsect when called.
311// TODO(phoglund): Move as much as possible of this code path into the media
312// specific receivers. Basically this method goes through a lot of trouble to
313// compute something which is only used by the media specific parts later. If
314// this code path moves we can get rid of some of the rtp_receiver ->
315// media_specific interface (such as CheckPayloadChange, possibly get/set
316// last known payload).
pbosd4362982015-07-07 08:32:48 -0700317int32_t RtpReceiverImpl::CheckPayloadChanged(const RTPHeader& rtp_header,
318 const int8_t first_payload_byte,
danilchap6db6cdc2015-12-15 02:54:47 -0800319 bool* is_red,
pbosd4362982015-07-07 08:32:48 -0700320 PayloadUnion* specific_payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000321 bool re_initialize_decoder = false;
322
323 char payload_name[RTP_PAYLOAD_NAME_SIZE];
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000324 int8_t payload_type = rtp_header.payloadType;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000325
326 {
danilchap7c9426c2016-04-14 03:05:31 -0700327 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000328
329 int8_t last_received_payload_type =
330 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000331 // TODO(holmer): Remove this code when RED parsing has been broken out from
332 // RtpReceiverAudio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000333 if (payload_type != last_received_payload_type) {
334 if (rtp_payload_registry_->red_payload_type() == payload_type) {
335 // Get the real codec payload type.
336 payload_type = first_payload_byte & 0x7f;
danilchap6db6cdc2015-12-15 02:54:47 -0800337 *is_red = true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000338
339 if (rtp_payload_registry_->red_payload_type() == payload_type) {
340 // Invalid payload type, traced by caller. If we proceeded here,
341 // this would be set as |_last_received_payload_type|, and we would no
342 // longer catch corrupt packets at this level.
343 return -1;
344 }
345
346 // When we receive RED we need to check the real payload type.
347 if (payload_type == last_received_payload_type) {
348 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
349 return 0;
350 }
351 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000352 bool should_discard_changes = false;
353
354 rtp_media_receiver_->CheckPayloadChanged(
pbosd4362982015-07-07 08:32:48 -0700355 payload_type, specific_payload,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000356 &should_discard_changes);
357
358 if (should_discard_changes) {
danilchap6db6cdc2015-12-15 02:54:47 -0800359 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000360 return 0;
361 }
362
Karl Wiberg73b60b82017-09-21 15:00:58 +0200363 const auto payload =
danilchap5c1def82015-12-10 09:51:54 -0800364 rtp_payload_registry_->PayloadTypeToPayload(payload_type);
365 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000366 // Not a registered payload type.
367 return -1;
368 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000369 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
370 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
371
372 rtp_payload_registry_->set_last_received_payload_type(payload_type);
373
374 re_initialize_decoder = true;
375
376 rtp_media_receiver_->SetLastMediaSpecificPayload(payload->typeSpecific);
377 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
378
379 if (!payload->audio) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000380 bool media_type_unchanged =
381 rtp_payload_registry_->ReportMediaPayloadType(payload_type);
382 if (media_type_unchanged) {
383 // Only reset the decoder if the media codec type has changed.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000384 re_initialize_decoder = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000385 }
386 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000387 } else {
388 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
danilchap6db6cdc2015-12-15 02:54:47 -0800389 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000390 }
391 } // End critsect.
392
393 if (re_initialize_decoder) {
Peter Boströmac547a62015-09-17 23:03:57 +0200394 if (-1 ==
395 rtp_media_receiver_->InvokeOnInitializeDecoder(
396 cb_rtp_feedback_, payload_type, payload_name, *specific_payload)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000397 return -1; // Wrong payload type.
398 }
399 }
400 return 0;
401}
402
403// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000404void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000405 int32_t num_csrcs_diff = 0;
406 uint32_t old_remote_csrc[kRtpCsrcSize];
407 uint8_t old_num_csrcs = 0;
408
409 {
danilchap7c9426c2016-04-14 03:05:31 -0700410 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000411
412 if (!rtp_media_receiver_->ShouldReportCsrcChanges(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000413 rtp_header.header.payloadType)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000414 return;
415 }
416 old_num_csrcs = num_csrcs_;
417 if (old_num_csrcs > 0) {
418 // Make a copy of old.
419 memcpy(old_remote_csrc, current_remote_csrc_,
420 num_csrcs_ * sizeof(uint32_t));
421 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000422 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000423 if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
424 // Copy new.
425 memcpy(current_remote_csrc_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000426 rtp_header.header.arrOfCSRCs,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000427 num_csrcs * sizeof(uint32_t));
428 }
429 if (num_csrcs > 0 || old_num_csrcs > 0) {
430 num_csrcs_diff = num_csrcs - old_num_csrcs;
431 num_csrcs_ = num_csrcs; // Update stored CSRCs.
432 } else {
433 // No change.
434 return;
435 }
436 } // End critsect.
437
438 bool have_called_callback = false;
439 // Search for new CSRC in old array.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000440 for (uint8_t i = 0; i < rtp_header.header.numCSRCs; ++i) {
441 const uint32_t csrc = rtp_header.header.arrOfCSRCs[i];
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000442
443 bool found_match = false;
444 for (uint8_t j = 0; j < old_num_csrcs; ++j) {
445 if (csrc == old_remote_csrc[j]) { // old list
446 found_match = true;
447 break;
448 }
449 }
450 if (!found_match && csrc) {
451 // Didn't find it, report it as new.
452 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200453 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000454 }
455 }
456 // Search for old CSRC in new array.
457 for (uint8_t i = 0; i < old_num_csrcs; ++i) {
458 const uint32_t csrc = old_remote_csrc[i];
459
460 bool found_match = false;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000461 for (uint8_t j = 0; j < rtp_header.header.numCSRCs; ++j) {
462 if (csrc == rtp_header.header.arrOfCSRCs[j]) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000463 found_match = true;
464 break;
465 }
466 }
467 if (!found_match && csrc) {
468 // Did not find it, report as removed.
469 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200470 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000471 }
472 }
473 if (!have_called_callback) {
474 // If the CSRC list contain non-unique entries we will end up here.
475 // Using CSRC 0 to signal this event, not interop safe, other
476 // implementations might have CSRC 0 as a valid value.
477 if (num_csrcs_diff > 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200478 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000479 } else if (num_csrcs_diff < 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200480 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000481 }
482 }
483}
484
zstein2b706342017-08-24 14:52:17 -0700485void RtpReceiverImpl::UpdateSources(
486 const rtc::Optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700487 rtc::CritScope lock(&critical_section_rtp_receiver_);
488 int64_t now_ms = clock_->TimeInMilliseconds();
489
490 for (size_t i = 0; i < num_csrcs_; ++i) {
491 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
492 if (map_it == iterator_by_csrc_.end()) {
493 // If it is a new CSRC, append a new object to the end of the list.
494 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
495 RtpSourceType::CSRC);
496 } else {
497 // If it is an existing CSRC, move the object to the end of the list.
498 map_it->second->update_timestamp_ms(now_ms);
499 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
500 }
501 // Update the unordered_map.
502 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
503 }
504
505 // If this is the first packet or the SSRC is changed, insert a new
506 // contributing source that uses the SSRC.
507 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
508 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
509 } else {
510 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
511 }
512
zstein2b706342017-08-24 14:52:17 -0700513 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
514
hbos8d609f62017-04-10 07:39:05 -0700515 RemoveOutdatedSources(now_ms);
516}
517
518void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
519 std::list<RtpSource>::iterator it;
520 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
521 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
522 break;
523 }
524 iterator_by_csrc_.erase(it->source_id());
525 }
526 csrc_sources_.erase(csrc_sources_.begin(), it);
527
528 std::vector<RtpSource>::iterator vec_it;
529 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
530 ++vec_it) {
531 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
532 break;
533 }
534 }
535 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
536}
537
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000538} // namespace webrtc