blob: a47aa15ba73772e5583853abfaeb9c2c31a069cd [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
24#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
25#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
26#include "rtc_base/logging.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000027
28namespace webrtc {
29
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000030using RtpUtility::Payload;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000031
hbos8d609f62017-04-10 07:39:05 -070032// Only return the sources in the last 10 seconds.
33const int64_t kGetSourcesTimeoutMs = 10000;
34
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035RtpReceiver* RtpReceiver::CreateVideoReceiver(
Peter Boströmac547a62015-09-17 23:03:57 +020036 Clock* clock,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037 RtpData* incoming_payload_callback,
38 RtpFeedback* incoming_messages_callback,
39 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070040 RTC_DCHECK(incoming_payload_callback != nullptr);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000041 if (!incoming_messages_callback)
42 incoming_messages_callback = NullObjectRtpFeedback();
43 return new RtpReceiverImpl(
solenberg1d031392016-03-30 02:42:32 -070044 clock, incoming_messages_callback, rtp_payload_registry,
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000045 RTPReceiverStrategy::CreateVideoStrategy(incoming_payload_callback));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000046}
47
48RtpReceiver* RtpReceiver::CreateAudioReceiver(
Peter Boströmac547a62015-09-17 23:03:57 +020049 Clock* clock,
solenberg1d031392016-03-30 02:42:32 -070050 RtpData* incoming_payload_callback,
51 RtpFeedback* incoming_messages_callback,
52 RTPPayloadRegistry* rtp_payload_registry) {
nisse7fcdb6d2017-06-01 00:30:55 -070053 RTC_DCHECK(incoming_payload_callback != nullptr);
solenberg1d031392016-03-30 02:42:32 -070054 if (!incoming_messages_callback)
55 incoming_messages_callback = NullObjectRtpFeedback();
56 return new RtpReceiverImpl(
57 clock, incoming_messages_callback, rtp_payload_registry,
58 RTPReceiverStrategy::CreateAudioStrategy(incoming_payload_callback));
59}
60
Karl Wibergc62f6c72017-10-04 12:38:53 +020061int32_t RtpReceiver::RegisterReceivePayload(const CodecInst& audio_codec) {
62 return RegisterReceivePayload(audio_codec.pltype,
63 CodecInstToSdp(audio_codec));
64}
65
hbos8d609f62017-04-10 07:39:05 -070066RtpReceiverImpl::RtpReceiverImpl(Clock* clock,
67 RtpFeedback* incoming_messages_callback,
68 RTPPayloadRegistry* rtp_payload_registry,
69 RTPReceiverStrategy* rtp_media_receiver)
wu@webrtc.org822fbd82013-08-15 23:38:54 +000070 : clock_(clock),
71 rtp_payload_registry_(rtp_payload_registry),
72 rtp_media_receiver_(rtp_media_receiver),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000073 cb_rtp_feedback_(incoming_messages_callback),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000074 ssrc_(0),
75 num_csrcs_(0),
76 current_remote_csrc_(),
77 last_received_timestamp_(0),
Niels Möllerbbf389c2017-09-26 14:05:05 +020078 last_received_frame_time_ms_(-1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000079 assert(incoming_messages_callback);
80
81 memset(current_remote_csrc_, 0, sizeof(current_remote_csrc_));
wu@webrtc.org822fbd82013-08-15 23:38:54 +000082}
83
84RtpReceiverImpl::~RtpReceiverImpl() {
85 for (int i = 0; i < num_csrcs_; ++i) {
Peter Boströmac547a62015-09-17 23:03:57 +020086 cb_rtp_feedback_->OnIncomingCSRCChanged(current_remote_csrc_[i], false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000088}
89
Karl Wibergc62f6c72017-10-04 12:38:53 +020090int32_t RtpReceiverImpl::RegisterReceivePayload(
91 int payload_type,
92 const SdpAudioFormat& audio_format) {
danilchap7c9426c2016-04-14 03:05:31 -070093 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000094
95 // TODO(phoglund): Try to streamline handling of the RED codec and some other
96 // cases which makes it necessary to keep track of whether we created a
97 // payload or not.
98 bool created_new_payload = false;
99 int32_t result = rtp_payload_registry_->RegisterReceivePayload(
Karl Wibergc62f6c72017-10-04 12:38:53 +0200100 payload_type, audio_format, &created_new_payload);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000101 if (created_new_payload) {
Karl Wibergc62f6c72017-10-04 12:38:53 +0200102 if (rtp_media_receiver_->OnNewPayloadTypeCreated(payload_type,
103 audio_format) != 0) {
104 LOG(LS_ERROR) << "Failed to register payload: " << audio_format.name
105 << "/" << payload_type;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000106 return -1;
107 }
108 }
109 return result;
110}
111
magjed6b272c52016-11-25 02:29:39 -0800112int32_t RtpReceiverImpl::RegisterReceivePayload(const VideoCodec& video_codec) {
113 rtc::CritScope lock(&critical_section_rtp_receiver_);
114 return rtp_payload_registry_->RegisterReceivePayload(video_codec);
115}
116
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000117int32_t RtpReceiverImpl::DeRegisterReceivePayload(
118 const int8_t payload_type) {
danilchap7c9426c2016-04-14 03:05:31 -0700119 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000120 return rtp_payload_registry_->DeRegisterReceivePayload(payload_type);
121}
122
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000123uint32_t RtpReceiverImpl::SSRC() const {
danilchap7c9426c2016-04-14 03:05:31 -0700124 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125 return ssrc_;
126}
127
128// Get remote CSRC.
129int32_t RtpReceiverImpl::CSRCs(uint32_t array_of_csrcs[kRtpCsrcSize]) const {
danilchap7c9426c2016-04-14 03:05:31 -0700130 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000131
132 assert(num_csrcs_ <= kRtpCsrcSize);
133
134 if (num_csrcs_ > 0) {
135 memcpy(array_of_csrcs, current_remote_csrc_, sizeof(uint32_t)*num_csrcs_);
136 }
137 return num_csrcs_;
138}
139
140int32_t RtpReceiverImpl::Energy(
141 uint8_t array_of_energy[kRtpCsrcSize]) const {
142 return rtp_media_receiver_->Energy(array_of_energy);
143}
144
145bool RtpReceiverImpl::IncomingRtpPacket(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000146 const RTPHeader& rtp_header,
147 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000148 size_t payload_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000149 PayloadUnion payload_specific,
150 bool in_order) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000151 // Trigger our callbacks.
152 CheckSSRCChanged(rtp_header);
153
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000154 int8_t first_payload_byte = payload_length > 0 ? payload[0] : 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000155 bool is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000156
danilchap6db6cdc2015-12-15 02:54:47 -0800157 if (CheckPayloadChanged(rtp_header, first_payload_byte, &is_red,
pbosd4362982015-07-07 08:32:48 -0700158 &payload_specific) == -1) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000159 if (payload_length == 0) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000160 // OK, keep-alive packet.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000161 return true;
162 }
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000163 LOG(LS_WARNING) << "Receiving invalid payload type.";
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000164 return false;
165 }
166
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000167 WebRtcRTPHeader webrtc_rtp_header;
168 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000169 webrtc_rtp_header.header = rtp_header;
170 CheckCSRC(webrtc_rtp_header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000171
zstein2b706342017-08-24 14:52:17 -0700172 auto audio_level =
173 rtp_header.extension.hasAudioLevel
174 ? rtc::Optional<uint8_t>(rtp_header.extension.audioLevel)
175 : rtc::Optional<uint8_t>();
176 UpdateSources(audio_level);
hbos8d609f62017-04-10 07:39:05 -0700177
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000178 int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000179 &webrtc_rtp_header, payload_specific, is_red, payload, payload_length,
Niels Möllerbbf389c2017-09-26 14:05:05 +0200180 clock_->TimeInMilliseconds());
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000181
182 if (ret_val < 0) {
183 return false;
184 }
185
186 {
danilchap7c9426c2016-04-14 03:05:31 -0700187 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000188
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000189 if (in_order) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000190 if (last_received_timestamp_ != rtp_header.timestamp) {
191 last_received_timestamp_ = rtp_header.timestamp;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000192 last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
193 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000194 }
195 }
196 return true;
197}
198
danilchap799a9d02016-09-22 03:36:27 -0700199TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
200 return rtp_media_receiver_->GetTelephoneEventHandler();
201}
202
hbos8d609f62017-04-10 07:39:05 -0700203std::vector<RtpSource> RtpReceiverImpl::GetSources() const {
zhihuang04262222017-04-11 11:28:10 -0700204 rtc::CritScope lock(&critical_section_rtp_receiver_);
205
hbos8d609f62017-04-10 07:39:05 -0700206 int64_t now_ms = clock_->TimeInMilliseconds();
207 std::vector<RtpSource> sources;
208
zhihuang04262222017-04-11 11:28:10 -0700209 RTC_DCHECK(std::is_sorted(ssrc_sources_.begin(), ssrc_sources_.end(),
210 [](const RtpSource& lhs, const RtpSource& rhs) {
211 return lhs.timestamp_ms() < rhs.timestamp_ms();
212 }));
213 RTC_DCHECK(std::is_sorted(csrc_sources_.begin(), csrc_sources_.end(),
214 [](const RtpSource& lhs, const RtpSource& rhs) {
215 return lhs.timestamp_ms() < rhs.timestamp_ms();
216 }));
hbos8d609f62017-04-10 07:39:05 -0700217
zhihuang04262222017-04-11 11:28:10 -0700218 std::set<uint32_t> selected_ssrcs;
219 for (auto rit = ssrc_sources_.rbegin(); rit != ssrc_sources_.rend(); ++rit) {
220 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
221 break;
hbos8d609f62017-04-10 07:39:05 -0700222 }
zhihuang04262222017-04-11 11:28:10 -0700223 if (selected_ssrcs.insert(rit->source_id()).second) {
hbos8d609f62017-04-10 07:39:05 -0700224 sources.push_back(*rit);
225 }
zhihuang04262222017-04-11 11:28:10 -0700226 }
hbos8d609f62017-04-10 07:39:05 -0700227
zhihuang04262222017-04-11 11:28:10 -0700228 for (auto rit = csrc_sources_.rbegin(); rit != csrc_sources_.rend(); ++rit) {
229 if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
230 break;
231 }
232 sources.push_back(*rit);
233 }
hbos8d609f62017-04-10 07:39:05 -0700234 return sources;
235}
236
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200237bool RtpReceiverImpl::GetLatestTimestamps(uint32_t* timestamp,
238 int64_t* receive_time_ms) const {
danilchap7c9426c2016-04-14 03:05:31 -0700239 rtc::CritScope lock(&critical_section_rtp_receiver_);
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200240 if (last_received_frame_time_ms_ < 0)
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000241 return false;
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200242
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000243 *timestamp = last_received_timestamp_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000244 *receive_time_ms = last_received_frame_time_ms_;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000245
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200246 return true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247}
248
249// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000250void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000251 bool new_ssrc = false;
Karl Wibergc62f6c72017-10-04 12:38:53 +0200252 rtc::Optional<AudioPayload> reinitialize_audio_payload;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000253
254 {
danilchap7c9426c2016-04-14 03:05:31 -0700255 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000256
257 int8_t last_received_payload_type =
258 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000259 if (ssrc_ != rtp_header.ssrc ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000260 (last_received_payload_type == -1 && ssrc_ == 0)) {
261 // We need the payload_type_ to make the call if the remote SSRC is 0.
262 new_ssrc = true;
263
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000264 last_received_timestamp_ = 0;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000265 last_received_frame_time_ms_ = -1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000266
267 // Do we have a SSRC? Then the stream is restarted.
268 if (ssrc_ != 0) {
269 // Do we have the same codec? Then re-initialize coder.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000270 if (rtp_header.payloadType == last_received_payload_type) {
Karl Wiberg73b60b82017-09-21 15:00:58 +0200271 const auto payload = rtp_payload_registry_->PayloadTypeToPayload(
danilchap5c1def82015-12-10 09:51:54 -0800272 rtp_header.payloadType);
273 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000274 return;
275 }
Karl Wibergc856dc22017-09-28 20:13:59 +0200276 if (payload->typeSpecific.is_audio()) {
Karl Wibergc62f6c72017-10-04 12:38:53 +0200277 reinitialize_audio_payload.emplace(
278 payload->typeSpecific.audio_payload());
279 } else {
280 // OnInitializeDecoder() is only used for audio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000281 }
282 }
283 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000284 ssrc_ = rtp_header.ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000285 }
286 }
287
288 if (new_ssrc) {
289 // We need to get this to our RTCP sender and receiver.
290 // We need to do this outside critical section.
Peter Boströmac547a62015-09-17 23:03:57 +0200291 cb_rtp_feedback_->OnIncomingSSRCChanged(rtp_header.ssrc);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 }
293
Karl Wibergc62f6c72017-10-04 12:38:53 +0200294 if (reinitialize_audio_payload) {
295 if (-1 == cb_rtp_feedback_->OnInitializeDecoder(
296 rtp_header.payloadType, reinitialize_audio_payload->format,
297 reinitialize_audio_payload->rate)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000298 // New stream, same codec.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000299 LOG(LS_ERROR) << "Failed to create decoder for payload type: "
pkasting@chromium.org026b8922015-01-30 19:53:42 +0000300 << static_cast<int>(rtp_header.payloadType);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000301 }
302 }
303}
304
305// Implementation note: must not hold critsect when called.
306// TODO(phoglund): Move as much as possible of this code path into the media
307// specific receivers. Basically this method goes through a lot of trouble to
308// compute something which is only used by the media specific parts later. If
309// this code path moves we can get rid of some of the rtp_receiver ->
310// media_specific interface (such as CheckPayloadChange, possibly get/set
311// last known payload).
pbosd4362982015-07-07 08:32:48 -0700312int32_t RtpReceiverImpl::CheckPayloadChanged(const RTPHeader& rtp_header,
313 const int8_t first_payload_byte,
danilchap6db6cdc2015-12-15 02:54:47 -0800314 bool* is_red,
pbosd4362982015-07-07 08:32:48 -0700315 PayloadUnion* specific_payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000316 bool re_initialize_decoder = false;
317
318 char payload_name[RTP_PAYLOAD_NAME_SIZE];
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319 int8_t payload_type = rtp_header.payloadType;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000320
321 {
danilchap7c9426c2016-04-14 03:05:31 -0700322 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000323
324 int8_t last_received_payload_type =
325 rtp_payload_registry_->last_received_payload_type();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000326 // TODO(holmer): Remove this code when RED parsing has been broken out from
327 // RtpReceiverAudio.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000328 if (payload_type != last_received_payload_type) {
329 if (rtp_payload_registry_->red_payload_type() == payload_type) {
330 // Get the real codec payload type.
331 payload_type = first_payload_byte & 0x7f;
danilchap6db6cdc2015-12-15 02:54:47 -0800332 *is_red = true;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000333
334 if (rtp_payload_registry_->red_payload_type() == payload_type) {
335 // Invalid payload type, traced by caller. If we proceeded here,
336 // this would be set as |_last_received_payload_type|, and we would no
337 // longer catch corrupt packets at this level.
338 return -1;
339 }
340
341 // When we receive RED we need to check the real payload type.
342 if (payload_type == last_received_payload_type) {
343 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
344 return 0;
345 }
346 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000347 bool should_discard_changes = false;
348
349 rtp_media_receiver_->CheckPayloadChanged(
pbosd4362982015-07-07 08:32:48 -0700350 payload_type, specific_payload,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000351 &should_discard_changes);
352
353 if (should_discard_changes) {
danilchap6db6cdc2015-12-15 02:54:47 -0800354 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000355 return 0;
356 }
357
Karl Wiberg73b60b82017-09-21 15:00:58 +0200358 const auto payload =
danilchap5c1def82015-12-10 09:51:54 -0800359 rtp_payload_registry_->PayloadTypeToPayload(payload_type);
360 if (!payload) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000361 // Not a registered payload type.
362 return -1;
363 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000364 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
365 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
366
367 rtp_payload_registry_->set_last_received_payload_type(payload_type);
368
369 re_initialize_decoder = true;
370
371 rtp_media_receiver_->SetLastMediaSpecificPayload(payload->typeSpecific);
372 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
373
Karl Wibergc856dc22017-09-28 20:13:59 +0200374 if (!payload->typeSpecific.is_audio()) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000375 bool media_type_unchanged =
376 rtp_payload_registry_->ReportMediaPayloadType(payload_type);
377 if (media_type_unchanged) {
378 // Only reset the decoder if the media codec type has changed.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000379 re_initialize_decoder = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000380 }
381 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000382 } else {
383 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
danilchap6db6cdc2015-12-15 02:54:47 -0800384 *is_red = false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000385 }
386 } // End critsect.
387
388 if (re_initialize_decoder) {
Peter Boströmac547a62015-09-17 23:03:57 +0200389 if (-1 ==
390 rtp_media_receiver_->InvokeOnInitializeDecoder(
391 cb_rtp_feedback_, payload_type, payload_name, *specific_payload)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000392 return -1; // Wrong payload type.
393 }
394 }
395 return 0;
396}
397
398// Implementation note: must not hold critsect when called.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000399void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000400 int32_t num_csrcs_diff = 0;
401 uint32_t old_remote_csrc[kRtpCsrcSize];
402 uint8_t old_num_csrcs = 0;
403
404 {
danilchap7c9426c2016-04-14 03:05:31 -0700405 rtc::CritScope lock(&critical_section_rtp_receiver_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000406
407 if (!rtp_media_receiver_->ShouldReportCsrcChanges(
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000408 rtp_header.header.payloadType)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000409 return;
410 }
411 old_num_csrcs = num_csrcs_;
412 if (old_num_csrcs > 0) {
413 // Make a copy of old.
414 memcpy(old_remote_csrc, current_remote_csrc_,
415 num_csrcs_ * sizeof(uint32_t));
416 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000417 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000418 if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
419 // Copy new.
420 memcpy(current_remote_csrc_,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000421 rtp_header.header.arrOfCSRCs,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000422 num_csrcs * sizeof(uint32_t));
423 }
424 if (num_csrcs > 0 || old_num_csrcs > 0) {
425 num_csrcs_diff = num_csrcs - old_num_csrcs;
426 num_csrcs_ = num_csrcs; // Update stored CSRCs.
427 } else {
428 // No change.
429 return;
430 }
431 } // End critsect.
432
433 bool have_called_callback = false;
434 // Search for new CSRC in old array.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000435 for (uint8_t i = 0; i < rtp_header.header.numCSRCs; ++i) {
436 const uint32_t csrc = rtp_header.header.arrOfCSRCs[i];
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000437
438 bool found_match = false;
439 for (uint8_t j = 0; j < old_num_csrcs; ++j) {
440 if (csrc == old_remote_csrc[j]) { // old list
441 found_match = true;
442 break;
443 }
444 }
445 if (!found_match && csrc) {
446 // Didn't find it, report it as new.
447 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200448 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000449 }
450 }
451 // Search for old CSRC in new array.
452 for (uint8_t i = 0; i < old_num_csrcs; ++i) {
453 const uint32_t csrc = old_remote_csrc[i];
454
455 bool found_match = false;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000456 for (uint8_t j = 0; j < rtp_header.header.numCSRCs; ++j) {
457 if (csrc == rtp_header.header.arrOfCSRCs[j]) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000458 found_match = true;
459 break;
460 }
461 }
462 if (!found_match && csrc) {
463 // Did not find it, report as removed.
464 have_called_callback = true;
Peter Boströmac547a62015-09-17 23:03:57 +0200465 cb_rtp_feedback_->OnIncomingCSRCChanged(csrc, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000466 }
467 }
468 if (!have_called_callback) {
469 // If the CSRC list contain non-unique entries we will end up here.
470 // Using CSRC 0 to signal this event, not interop safe, other
471 // implementations might have CSRC 0 as a valid value.
472 if (num_csrcs_diff > 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200473 cb_rtp_feedback_->OnIncomingCSRCChanged(0, true);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000474 } else if (num_csrcs_diff < 0) {
Peter Boströmac547a62015-09-17 23:03:57 +0200475 cb_rtp_feedback_->OnIncomingCSRCChanged(0, false);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000476 }
477 }
478}
479
zstein2b706342017-08-24 14:52:17 -0700480void RtpReceiverImpl::UpdateSources(
481 const rtc::Optional<uint8_t>& ssrc_audio_level) {
hbos8d609f62017-04-10 07:39:05 -0700482 rtc::CritScope lock(&critical_section_rtp_receiver_);
483 int64_t now_ms = clock_->TimeInMilliseconds();
484
485 for (size_t i = 0; i < num_csrcs_; ++i) {
486 auto map_it = iterator_by_csrc_.find(current_remote_csrc_[i]);
487 if (map_it == iterator_by_csrc_.end()) {
488 // If it is a new CSRC, append a new object to the end of the list.
489 csrc_sources_.emplace_back(now_ms, current_remote_csrc_[i],
490 RtpSourceType::CSRC);
491 } else {
492 // If it is an existing CSRC, move the object to the end of the list.
493 map_it->second->update_timestamp_ms(now_ms);
494 csrc_sources_.splice(csrc_sources_.end(), csrc_sources_, map_it->second);
495 }
496 // Update the unordered_map.
497 iterator_by_csrc_[current_remote_csrc_[i]] = std::prev(csrc_sources_.end());
498 }
499
500 // If this is the first packet or the SSRC is changed, insert a new
501 // contributing source that uses the SSRC.
502 if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
503 ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
504 } else {
505 ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
506 }
507
zstein2b706342017-08-24 14:52:17 -0700508 ssrc_sources_.back().set_audio_level(ssrc_audio_level);
509
hbos8d609f62017-04-10 07:39:05 -0700510 RemoveOutdatedSources(now_ms);
511}
512
513void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
514 std::list<RtpSource>::iterator it;
515 for (it = csrc_sources_.begin(); it != csrc_sources_.end(); ++it) {
516 if ((now_ms - it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
517 break;
518 }
519 iterator_by_csrc_.erase(it->source_id());
520 }
521 csrc_sources_.erase(csrc_sources_.begin(), it);
522
523 std::vector<RtpSource>::iterator vec_it;
524 for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
525 ++vec_it) {
526 if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
527 break;
528 }
529 }
530 ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
531}
532
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000533} // namespace webrtc