2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_impl.h"
18 #include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
19 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
20 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
21 #include "webrtc/system_wrappers/interface/logging.h"
25 using ModuleRTPUtility::GetCurrentRTP;
26 using ModuleRTPUtility::Payload;
27 using ModuleRTPUtility::RTPPayloadParser;
28 using ModuleRTPUtility::StringCompare;
30 RtpReceiver* RtpReceiver::CreateVideoReceiver(
32 RtpData* incoming_payload_callback,
33 RtpFeedback* incoming_messages_callback,
34 RTPPayloadRegistry* rtp_payload_registry) {
35 if (!incoming_payload_callback)
36 incoming_payload_callback = NullObjectRtpData();
37 if (!incoming_messages_callback)
38 incoming_messages_callback = NullObjectRtpFeedback();
39 return new RtpReceiverImpl(
40 id, clock, NullObjectRtpAudioFeedback(), incoming_messages_callback,
42 RTPReceiverStrategy::CreateVideoStrategy(incoming_payload_callback));
45 RtpReceiver* RtpReceiver::CreateAudioReceiver(
47 RtpAudioFeedback* incoming_audio_feedback,
48 RtpData* incoming_payload_callback,
49 RtpFeedback* incoming_messages_callback,
50 RTPPayloadRegistry* rtp_payload_registry) {
51 if (!incoming_audio_feedback)
52 incoming_audio_feedback = NullObjectRtpAudioFeedback();
53 if (!incoming_payload_callback)
54 incoming_payload_callback = NullObjectRtpData();
55 if (!incoming_messages_callback)
56 incoming_messages_callback = NullObjectRtpFeedback();
57 return new RtpReceiverImpl(
58 id, clock, incoming_audio_feedback, incoming_messages_callback,
60 RTPReceiverStrategy::CreateAudioStrategy(id, incoming_payload_callback,
61 incoming_audio_feedback));
64 RtpReceiverImpl::RtpReceiverImpl(int32_t id,
66 RtpAudioFeedback* incoming_audio_messages_callback,
67 RtpFeedback* incoming_messages_callback,
68 RTPPayloadRegistry* rtp_payload_registry,
69 RTPReceiverStrategy* rtp_media_receiver)
71 rtp_payload_registry_(rtp_payload_registry),
72 rtp_media_receiver_(rtp_media_receiver),
74 cb_rtp_feedback_(incoming_messages_callback),
75 critical_section_rtp_receiver_(
76 CriticalSectionWrapper::CreateCriticalSection()),
77 last_receive_time_(0),
78 last_received_payload_length_(0),
81 current_remote_csrc_(),
82 last_received_timestamp_(0),
83 last_received_frame_time_ms_(-1),
84 last_received_sequence_number_(0),
85 nack_method_(kNackOff) {
86 assert(incoming_audio_messages_callback);
87 assert(incoming_messages_callback);
89 memset(current_remote_csrc_, 0, sizeof(current_remote_csrc_));
92 RtpReceiverImpl::~RtpReceiverImpl() {
93 for (int i = 0; i < num_csrcs_; ++i) {
94 cb_rtp_feedback_->OnIncomingCSRCChanged(id_, current_remote_csrc_[i],
99 RTPReceiverStrategy* RtpReceiverImpl::GetMediaReceiver() const {
100 return rtp_media_receiver_.get();
103 RtpVideoCodecTypes RtpReceiverImpl::VideoCodecType() const {
104 PayloadUnion media_specific;
105 rtp_media_receiver_->GetLastMediaSpecificPayload(&media_specific);
106 return media_specific.Video.videoCodecType;
109 int32_t RtpReceiverImpl::RegisterReceivePayload(
110 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
111 const int8_t payload_type,
112 const uint32_t frequency,
113 const uint8_t channels,
114 const uint32_t rate) {
115 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
117 // TODO(phoglund): Try to streamline handling of the RED codec and some other
118 // cases which makes it necessary to keep track of whether we created a
120 bool created_new_payload = false;
121 int32_t result = rtp_payload_registry_->RegisterReceivePayload(
122 payload_name, payload_type, frequency, channels, rate,
123 &created_new_payload);
124 if (created_new_payload) {
125 if (rtp_media_receiver_->OnNewPayloadTypeCreated(payload_name, payload_type,
127 LOG(LS_ERROR) << "Failed to register payload: " << payload_name << "/"
135 int32_t RtpReceiverImpl::DeRegisterReceivePayload(
136 const int8_t payload_type) {
137 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
138 return rtp_payload_registry_->DeRegisterReceivePayload(payload_type);
141 NACKMethod RtpReceiverImpl::NACK() const {
142 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
146 // Turn negative acknowledgment requests on/off.
147 void RtpReceiverImpl::SetNACKStatus(const NACKMethod method) {
148 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
149 nack_method_ = method;
152 uint32_t RtpReceiverImpl::SSRC() const {
153 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
158 int32_t RtpReceiverImpl::CSRCs(uint32_t array_of_csrcs[kRtpCsrcSize]) const {
159 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
161 assert(num_csrcs_ <= kRtpCsrcSize);
163 if (num_csrcs_ > 0) {
164 memcpy(array_of_csrcs, current_remote_csrc_, sizeof(uint32_t)*num_csrcs_);
169 int32_t RtpReceiverImpl::Energy(
170 uint8_t array_of_energy[kRtpCsrcSize]) const {
171 return rtp_media_receiver_->Energy(array_of_energy);
174 bool RtpReceiverImpl::IncomingRtpPacket(
175 const RTPHeader& rtp_header,
176 const uint8_t* payload,
178 PayloadUnion payload_specific,
181 assert(payload_length >= 0);
183 // Trigger our callbacks.
184 CheckSSRCChanged(rtp_header);
186 int8_t first_payload_byte = payload_length > 0 ? payload[0] : 0;
188 bool should_reset_statistics = false;
190 if (CheckPayloadChanged(rtp_header,
194 &should_reset_statistics) == -1) {
195 if (payload_length == 0) {
196 // OK, keep-alive packet.
199 LOG(LS_WARNING) << "Receiving invalid payload type.";
203 if (should_reset_statistics) {
204 cb_rtp_feedback_->ResetStatistics(ssrc_);
207 WebRtcRTPHeader webrtc_rtp_header;
208 memset(&webrtc_rtp_header, 0, sizeof(webrtc_rtp_header));
209 webrtc_rtp_header.header = rtp_header;
210 CheckCSRC(webrtc_rtp_header);
212 uint16_t payload_data_length = payload_length - rtp_header.paddingLength;
214 bool is_first_packet_in_frame = false;
216 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
217 if (HaveReceivedFrame()) {
218 is_first_packet_in_frame =
219 last_received_sequence_number_ + 1 == rtp_header.sequenceNumber &&
220 last_received_timestamp_ != rtp_header.timestamp;
222 is_first_packet_in_frame = true;
226 int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
227 &webrtc_rtp_header, payload_specific, is_red, payload, payload_length,
228 clock_->TimeInMilliseconds(), is_first_packet_in_frame);
235 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
237 last_receive_time_ = clock_->TimeInMilliseconds();
238 last_received_payload_length_ = payload_data_length;
241 if (last_received_timestamp_ != rtp_header.timestamp) {
242 last_received_timestamp_ = rtp_header.timestamp;
243 last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
245 last_received_sequence_number_ = rtp_header.sequenceNumber;
251 TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
252 return rtp_media_receiver_->GetTelephoneEventHandler();
255 bool RtpReceiverImpl::Timestamp(uint32_t* timestamp) const {
256 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
257 if (!HaveReceivedFrame())
259 *timestamp = last_received_timestamp_;
263 bool RtpReceiverImpl::LastReceivedTimeMs(int64_t* receive_time_ms) const {
264 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
265 if (!HaveReceivedFrame())
267 *receive_time_ms = last_received_frame_time_ms_;
271 bool RtpReceiverImpl::HaveReceivedFrame() const {
272 return last_received_frame_time_ms_ >= 0;
275 // Implementation note: must not hold critsect when called.
276 void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
277 bool new_ssrc = false;
278 bool re_initialize_decoder = false;
279 char payload_name[RTP_PAYLOAD_NAME_SIZE];
280 uint8_t channels = 1;
284 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
286 int8_t last_received_payload_type =
287 rtp_payload_registry_->last_received_payload_type();
288 if (ssrc_ != rtp_header.ssrc ||
289 (last_received_payload_type == -1 && ssrc_ == 0)) {
290 // We need the payload_type_ to make the call if the remote SSRC is 0.
293 cb_rtp_feedback_->ResetStatistics(ssrc_);
295 last_received_timestamp_ = 0;
296 last_received_sequence_number_ = 0;
297 last_received_frame_time_ms_ = -1;
299 // Do we have a SSRC? Then the stream is restarted.
301 // Do we have the same codec? Then re-initialize coder.
302 if (rtp_header.payloadType == last_received_payload_type) {
303 re_initialize_decoder = true;
306 if (!rtp_payload_registry_->PayloadTypeToPayload(
307 rtp_header.payloadType, payload)) {
311 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
312 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
313 if (payload->audio) {
314 channels = payload->typeSpecific.Audio.channels;
315 rate = payload->typeSpecific.Audio.rate;
319 ssrc_ = rtp_header.ssrc;
324 // We need to get this to our RTCP sender and receiver.
325 // We need to do this outside critical section.
326 cb_rtp_feedback_->OnIncomingSSRCChanged(id_, rtp_header.ssrc);
329 if (re_initialize_decoder) {
330 if (-1 == cb_rtp_feedback_->OnInitializeDecoder(
331 id_, rtp_header.payloadType, payload_name,
332 rtp_header.payload_type_frequency, channels, rate)) {
333 // New stream, same codec.
334 LOG(LS_ERROR) << "Failed to create decoder for payload type: "
335 << rtp_header.payloadType;
340 // Implementation note: must not hold critsect when called.
341 // TODO(phoglund): Move as much as possible of this code path into the media
342 // specific receivers. Basically this method goes through a lot of trouble to
343 // compute something which is only used by the media specific parts later. If
344 // this code path moves we can get rid of some of the rtp_receiver ->
345 // media_specific interface (such as CheckPayloadChange, possibly get/set
346 // last known payload).
347 int32_t RtpReceiverImpl::CheckPayloadChanged(
348 const RTPHeader& rtp_header,
349 const int8_t first_payload_byte,
351 PayloadUnion* specific_payload,
352 bool* should_reset_statistics) {
353 bool re_initialize_decoder = false;
355 char payload_name[RTP_PAYLOAD_NAME_SIZE];
356 int8_t payload_type = rtp_header.payloadType;
359 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
361 int8_t last_received_payload_type =
362 rtp_payload_registry_->last_received_payload_type();
363 // TODO(holmer): Remove this code when RED parsing has been broken out from
365 if (payload_type != last_received_payload_type) {
366 if (rtp_payload_registry_->red_payload_type() == payload_type) {
367 // Get the real codec payload type.
368 payload_type = first_payload_byte & 0x7f;
371 if (rtp_payload_registry_->red_payload_type() == payload_type) {
372 // Invalid payload type, traced by caller. If we proceeded here,
373 // this would be set as |_last_received_payload_type|, and we would no
374 // longer catch corrupt packets at this level.
378 // When we receive RED we need to check the real payload type.
379 if (payload_type == last_received_payload_type) {
380 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
384 *should_reset_statistics = false;
385 bool should_discard_changes = false;
387 rtp_media_receiver_->CheckPayloadChanged(
388 payload_type, specific_payload, should_reset_statistics,
389 &should_discard_changes);
391 if (should_discard_changes) {
397 if (!rtp_payload_registry_->PayloadTypeToPayload(payload_type, payload)) {
398 // Not a registered payload type.
402 payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
403 strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
405 rtp_payload_registry_->set_last_received_payload_type(payload_type);
407 re_initialize_decoder = true;
409 rtp_media_receiver_->SetLastMediaSpecificPayload(payload->typeSpecific);
410 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
412 if (!payload->audio) {
413 bool media_type_unchanged =
414 rtp_payload_registry_->ReportMediaPayloadType(payload_type);
415 if (media_type_unchanged) {
416 // Only reset the decoder if the media codec type has changed.
417 re_initialize_decoder = false;
420 if (re_initialize_decoder) {
421 *should_reset_statistics = true;
424 rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
429 if (re_initialize_decoder) {
430 if (-1 == rtp_media_receiver_->InvokeOnInitializeDecoder(
431 cb_rtp_feedback_, id_, payload_type, payload_name,
432 *specific_payload)) {
433 return -1; // Wrong payload type.
439 // Implementation note: must not hold critsect when called.
440 void RtpReceiverImpl::CheckCSRC(const WebRtcRTPHeader& rtp_header) {
441 int32_t num_csrcs_diff = 0;
442 uint32_t old_remote_csrc[kRtpCsrcSize];
443 uint8_t old_num_csrcs = 0;
446 CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
448 if (!rtp_media_receiver_->ShouldReportCsrcChanges(
449 rtp_header.header.payloadType)) {
452 old_num_csrcs = num_csrcs_;
453 if (old_num_csrcs > 0) {
454 // Make a copy of old.
455 memcpy(old_remote_csrc, current_remote_csrc_,
456 num_csrcs_ * sizeof(uint32_t));
458 const uint8_t num_csrcs = rtp_header.header.numCSRCs;
459 if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
461 memcpy(current_remote_csrc_,
462 rtp_header.header.arrOfCSRCs,
463 num_csrcs * sizeof(uint32_t));
465 if (num_csrcs > 0 || old_num_csrcs > 0) {
466 num_csrcs_diff = num_csrcs - old_num_csrcs;
467 num_csrcs_ = num_csrcs; // Update stored CSRCs.
474 bool have_called_callback = false;
475 // Search for new CSRC in old array.
476 for (uint8_t i = 0; i < rtp_header.header.numCSRCs; ++i) {
477 const uint32_t csrc = rtp_header.header.arrOfCSRCs[i];
479 bool found_match = false;
480 for (uint8_t j = 0; j < old_num_csrcs; ++j) {
481 if (csrc == old_remote_csrc[j]) { // old list
486 if (!found_match && csrc) {
487 // Didn't find it, report it as new.
488 have_called_callback = true;
489 cb_rtp_feedback_->OnIncomingCSRCChanged(id_, csrc, true);
492 // Search for old CSRC in new array.
493 for (uint8_t i = 0; i < old_num_csrcs; ++i) {
494 const uint32_t csrc = old_remote_csrc[i];
496 bool found_match = false;
497 for (uint8_t j = 0; j < rtp_header.header.numCSRCs; ++j) {
498 if (csrc == rtp_header.header.arrOfCSRCs[j]) {
503 if (!found_match && csrc) {
504 // Did not find it, report as removed.
505 have_called_callback = true;
506 cb_rtp_feedback_->OnIncomingCSRCChanged(id_, csrc, false);
509 if (!have_called_callback) {
510 // If the CSRC list contain non-unique entries we will end up here.
511 // Using CSRC 0 to signal this event, not interop safe, other
512 // implementations might have CSRC 0 as a valid value.
513 if (num_csrcs_diff > 0) {
514 cb_rtp_feedback_->OnIncomingCSRCChanged(id_, 0, true);
515 } else if (num_csrcs_diff < 0) {
516 cb_rtp_feedback_->OnIncomingCSRCChanged(id_, 0, false);
521 } // namespace webrtc