Home | History | Annotate | Download | only in source
      1 /*
      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 
     11 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_impl.h"
     12 
     13 #include <assert.h>
     14 #include <math.h>
     15 #include <stdlib.h>
     16 #include <string.h>
     17 
     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"
     22 
     23 namespace webrtc {
     24 
     25 using ModuleRTPUtility::GetCurrentRTP;
     26 using ModuleRTPUtility::Payload;
     27 using ModuleRTPUtility::RTPPayloadParser;
     28 using ModuleRTPUtility::StringCompare;
     29 
     30 RtpReceiver* RtpReceiver::CreateVideoReceiver(
     31     int id, Clock* clock,
     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,
     41       rtp_payload_registry,
     42       RTPReceiverStrategy::CreateVideoStrategy(incoming_payload_callback));
     43 }
     44 
     45 RtpReceiver* RtpReceiver::CreateAudioReceiver(
     46     int id, Clock* clock,
     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,
     59       rtp_payload_registry,
     60       RTPReceiverStrategy::CreateAudioStrategy(id, incoming_payload_callback,
     61                                                incoming_audio_feedback));
     62 }
     63 
     64 RtpReceiverImpl::RtpReceiverImpl(int32_t id,
     65                          Clock* clock,
     66                          RtpAudioFeedback* incoming_audio_messages_callback,
     67                          RtpFeedback* incoming_messages_callback,
     68                          RTPPayloadRegistry* rtp_payload_registry,
     69                          RTPReceiverStrategy* rtp_media_receiver)
     70     : clock_(clock),
     71       rtp_payload_registry_(rtp_payload_registry),
     72       rtp_media_receiver_(rtp_media_receiver),
     73       id_(id),
     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),
     79       ssrc_(0),
     80       num_csrcs_(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);
     88 
     89   memset(current_remote_csrc_, 0, sizeof(current_remote_csrc_));
     90 }
     91 
     92 RtpReceiverImpl::~RtpReceiverImpl() {
     93   for (int i = 0; i < num_csrcs_; ++i) {
     94     cb_rtp_feedback_->OnIncomingCSRCChanged(id_, current_remote_csrc_[i],
     95                                             false);
     96   }
     97 }
     98 
     99 RTPReceiverStrategy* RtpReceiverImpl::GetMediaReceiver() const {
    100   return rtp_media_receiver_.get();
    101 }
    102 
    103 RtpVideoCodecTypes RtpReceiverImpl::VideoCodecType() const {
    104   PayloadUnion media_specific;
    105   rtp_media_receiver_->GetLastMediaSpecificPayload(&media_specific);
    106   return media_specific.Video.videoCodecType;
    107 }
    108 
    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());
    116 
    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
    119   // payload or not.
    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,
    126                                                      frequency) != 0) {
    127       LOG(LS_ERROR) << "Failed to register payload: " << payload_name << "/"
    128                  << payload_type;
    129       return -1;
    130     }
    131   }
    132   return result;
    133 }
    134 
    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);
    139 }
    140 
    141 NACKMethod RtpReceiverImpl::NACK() const {
    142   CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    143   return nack_method_;
    144 }
    145 
    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;
    150 }
    151 
    152 uint32_t RtpReceiverImpl::SSRC() const {
    153   CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    154   return ssrc_;
    155 }
    156 
    157 // Get remote CSRC.
    158 int32_t RtpReceiverImpl::CSRCs(uint32_t array_of_csrcs[kRtpCsrcSize]) const {
    159   CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    160 
    161   assert(num_csrcs_ <= kRtpCsrcSize);
    162 
    163   if (num_csrcs_ > 0) {
    164     memcpy(array_of_csrcs, current_remote_csrc_, sizeof(uint32_t)*num_csrcs_);
    165   }
    166   return num_csrcs_;
    167 }
    168 
    169 int32_t RtpReceiverImpl::Energy(
    170     uint8_t array_of_energy[kRtpCsrcSize]) const {
    171   return rtp_media_receiver_->Energy(array_of_energy);
    172 }
    173 
    174 bool RtpReceiverImpl::IncomingRtpPacket(
    175   const RTPHeader& rtp_header,
    176   const uint8_t* payload,
    177   int payload_length,
    178   PayloadUnion payload_specific,
    179   bool in_order) {
    180   // Sanity check.
    181   assert(payload_length >= 0);
    182 
    183   // Trigger our callbacks.
    184   CheckSSRCChanged(rtp_header);
    185 
    186   int8_t first_payload_byte = payload_length > 0 ? payload[0] : 0;
    187   bool is_red = false;
    188   bool should_reset_statistics = false;
    189 
    190   if (CheckPayloadChanged(rtp_header,
    191                           first_payload_byte,
    192                           is_red,
    193                           &payload_specific,
    194                           &should_reset_statistics) == -1) {
    195     if (payload_length == 0) {
    196       // OK, keep-alive packet.
    197       return true;
    198     }
    199     LOG(LS_WARNING) << "Receiving invalid payload type.";
    200     return false;
    201   }
    202 
    203   if (should_reset_statistics) {
    204     cb_rtp_feedback_->ResetStatistics(ssrc_);
    205   }
    206 
    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);
    211 
    212   uint16_t payload_data_length = payload_length - rtp_header.paddingLength;
    213 
    214   bool is_first_packet_in_frame = false;
    215   {
    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;
    221     } else {
    222       is_first_packet_in_frame = true;
    223     }
    224   }
    225 
    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);
    229 
    230   if (ret_val < 0) {
    231     return false;
    232   }
    233 
    234   {
    235     CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    236 
    237     last_receive_time_ = clock_->TimeInMilliseconds();
    238     last_received_payload_length_ = payload_data_length;
    239 
    240     if (in_order) {
    241       if (last_received_timestamp_ != rtp_header.timestamp) {
    242         last_received_timestamp_ = rtp_header.timestamp;
    243         last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
    244       }
    245       last_received_sequence_number_ = rtp_header.sequenceNumber;
    246     }
    247   }
    248   return true;
    249 }
    250 
    251 TelephoneEventHandler* RtpReceiverImpl::GetTelephoneEventHandler() {
    252   return rtp_media_receiver_->GetTelephoneEventHandler();
    253 }
    254 
    255 bool RtpReceiverImpl::Timestamp(uint32_t* timestamp) const {
    256   CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    257   if (!HaveReceivedFrame())
    258     return false;
    259   *timestamp = last_received_timestamp_;
    260   return true;
    261 }
    262 
    263 bool RtpReceiverImpl::LastReceivedTimeMs(int64_t* receive_time_ms) const {
    264   CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    265   if (!HaveReceivedFrame())
    266     return false;
    267   *receive_time_ms = last_received_frame_time_ms_;
    268   return true;
    269 }
    270 
    271 bool RtpReceiverImpl::HaveReceivedFrame() const {
    272   return last_received_frame_time_ms_ >= 0;
    273 }
    274 
    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;
    281   uint32_t rate = 0;
    282 
    283   {
    284     CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    285 
    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.
    291       new_ssrc = true;
    292 
    293       cb_rtp_feedback_->ResetStatistics(ssrc_);
    294 
    295       last_received_timestamp_ = 0;
    296       last_received_sequence_number_ = 0;
    297       last_received_frame_time_ms_ = -1;
    298 
    299       // Do we have a SSRC? Then the stream is restarted.
    300       if (ssrc_ != 0) {
    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;
    304 
    305           Payload* payload;
    306           if (!rtp_payload_registry_->PayloadTypeToPayload(
    307               rtp_header.payloadType, payload)) {
    308             return;
    309           }
    310           assert(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;
    316           }
    317         }
    318       }
    319       ssrc_ = rtp_header.ssrc;
    320     }
    321   }
    322 
    323   if (new_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);
    327   }
    328 
    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;
    336     }
    337   }
    338 }
    339 
    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,
    350   bool& is_red,
    351   PayloadUnion* specific_payload,
    352   bool* should_reset_statistics) {
    353   bool re_initialize_decoder = false;
    354 
    355   char payload_name[RTP_PAYLOAD_NAME_SIZE];
    356   int8_t payload_type = rtp_header.payloadType;
    357 
    358   {
    359     CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    360 
    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
    364     // RtpReceiverAudio.
    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;
    369         is_red = true;
    370 
    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.
    375           return -1;
    376         }
    377 
    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);
    381           return 0;
    382         }
    383       }
    384       *should_reset_statistics = false;
    385       bool should_discard_changes = false;
    386 
    387       rtp_media_receiver_->CheckPayloadChanged(
    388         payload_type, specific_payload, should_reset_statistics,
    389         &should_discard_changes);
    390 
    391       if (should_discard_changes) {
    392         is_red = false;
    393         return 0;
    394       }
    395 
    396       Payload* payload;
    397       if (!rtp_payload_registry_->PayloadTypeToPayload(payload_type, payload)) {
    398         // Not a registered payload type.
    399         return -1;
    400       }
    401       assert(payload);
    402       payload_name[RTP_PAYLOAD_NAME_SIZE - 1] = 0;
    403       strncpy(payload_name, payload->name, RTP_PAYLOAD_NAME_SIZE - 1);
    404 
    405       rtp_payload_registry_->set_last_received_payload_type(payload_type);
    406 
    407       re_initialize_decoder = true;
    408 
    409       rtp_media_receiver_->SetLastMediaSpecificPayload(payload->typeSpecific);
    410       rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
    411 
    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;
    418         }
    419       }
    420       if (re_initialize_decoder) {
    421         *should_reset_statistics = true;
    422       }
    423     } else {
    424       rtp_media_receiver_->GetLastMediaSpecificPayload(specific_payload);
    425       is_red = false;
    426     }
    427   }  // End critsect.
    428 
    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.
    434     }
    435   }
    436   return 0;
    437 }
    438 
    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;
    444 
    445   {
    446     CriticalSectionScoped lock(critical_section_rtp_receiver_.get());
    447 
    448     if (!rtp_media_receiver_->ShouldReportCsrcChanges(
    449         rtp_header.header.payloadType)) {
    450       return;
    451     }
    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));
    457     }
    458     const uint8_t num_csrcs = rtp_header.header.numCSRCs;
    459     if ((num_csrcs > 0) && (num_csrcs <= kRtpCsrcSize)) {
    460       // Copy new.
    461       memcpy(current_remote_csrc_,
    462              rtp_header.header.arrOfCSRCs,
    463              num_csrcs * sizeof(uint32_t));
    464     }
    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.
    468     } else {
    469       // No change.
    470       return;
    471     }
    472   }  // End critsect.
    473 
    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];
    478 
    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
    482         found_match = true;
    483         break;
    484       }
    485     }
    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);
    490     }
    491   }
    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];
    495 
    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]) {
    499         found_match = true;
    500         break;
    501       }
    502     }
    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);
    507     }
    508   }
    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);
    517     }
    518   }
    519 }
    520 
    521 }  // namespace webrtc
    522