Home | History | Annotate | Download | only in interface
      1 /*
      2  *  Copyright (c) 2013 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 #ifndef WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_PAYLOAD_REGISTRY_H_
     12 #define WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_PAYLOAD_REGISTRY_H_
     13 
     14 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
     15 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
     16 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     17 
     18 namespace webrtc {
     19 
     20 // This strategy deals with the audio/video-specific aspects
     21 // of payload handling.
     22 class RTPPayloadStrategy {
     23  public:
     24   virtual ~RTPPayloadStrategy() {}
     25 
     26   virtual bool CodecsMustBeUnique() const = 0;
     27 
     28   virtual bool PayloadIsCompatible(
     29       const ModuleRTPUtility::Payload& payload,
     30       const uint32_t frequency,
     31       const uint8_t channels,
     32       const uint32_t rate) const = 0;
     33 
     34   virtual void UpdatePayloadRate(
     35       ModuleRTPUtility::Payload* payload,
     36       const uint32_t rate) const = 0;
     37 
     38   virtual ModuleRTPUtility::Payload* CreatePayloadType(
     39       const char payloadName[RTP_PAYLOAD_NAME_SIZE],
     40       const int8_t payloadType,
     41       const uint32_t frequency,
     42       const uint8_t channels,
     43       const uint32_t rate) const = 0;
     44 
     45   virtual int GetPayloadTypeFrequency(
     46       const ModuleRTPUtility::Payload& payload) const = 0;
     47 
     48   static RTPPayloadStrategy* CreateStrategy(const bool handling_audio);
     49 
     50  protected:
     51   RTPPayloadStrategy() {}
     52 };
     53 
     54 class RTPPayloadRegistry {
     55  public:
     56   // The registry takes ownership of the strategy.
     57   RTPPayloadRegistry(RTPPayloadStrategy* rtp_payload_strategy);
     58   ~RTPPayloadRegistry();
     59 
     60   int32_t RegisterReceivePayload(
     61       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
     62       const int8_t payload_type,
     63       const uint32_t frequency,
     64       const uint8_t channels,
     65       const uint32_t rate,
     66       bool* created_new_payload_type);
     67 
     68   int32_t DeRegisterReceivePayload(
     69       const int8_t payload_type);
     70 
     71   int32_t ReceivePayloadType(
     72       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
     73       const uint32_t frequency,
     74       const uint8_t channels,
     75       const uint32_t rate,
     76       int8_t* payload_type) const;
     77 
     78   bool RtxEnabled() const;
     79 
     80   void SetRtxSsrc(uint32_t ssrc);
     81 
     82   void SetRtxPayloadType(int payload_type);
     83 
     84   bool IsRtx(const RTPHeader& header) const;
     85 
     86   bool RestoreOriginalPacket(uint8_t** restored_packet,
     87                              const uint8_t* packet,
     88                              int* packet_length,
     89                              uint32_t original_ssrc,
     90                              const RTPHeader& header) const;
     91 
     92   bool IsRed(const RTPHeader& header) const;
     93 
     94   // Returns true if the media of this RTP packet is encapsulated within an
     95   // extra header, such as RTX or RED.
     96   bool IsEncapsulated(const RTPHeader& header) const;
     97 
     98   bool GetPayloadSpecifics(uint8_t payload_type, PayloadUnion* payload) const;
     99 
    100   int GetPayloadTypeFrequency(uint8_t payload_type) const;
    101 
    102   bool PayloadTypeToPayload(
    103     const uint8_t payload_type,
    104     ModuleRTPUtility::Payload*& payload) const;
    105 
    106   void ResetLastReceivedPayloadTypes() {
    107     CriticalSectionScoped cs(crit_sect_.get());
    108     last_received_payload_type_ = -1;
    109     last_received_media_payload_type_ = -1;
    110   }
    111 
    112   // This sets the payload type of the packets being received from the network
    113   // on the media SSRC. For instance if packets are encapsulated with RED, this
    114   // payload type will be the RED payload type.
    115   void SetIncomingPayloadType(const RTPHeader& header);
    116 
    117   // Returns true if the new media payload type has not changed.
    118   bool ReportMediaPayloadType(uint8_t media_payload_type);
    119 
    120   int8_t red_payload_type() const {
    121     CriticalSectionScoped cs(crit_sect_.get());
    122     return red_payload_type_;
    123   }
    124   int8_t ulpfec_payload_type() const {
    125     CriticalSectionScoped cs(crit_sect_.get());
    126     return ulpfec_payload_type_;
    127   }
    128   int8_t last_received_payload_type() const {
    129     CriticalSectionScoped cs(crit_sect_.get());
    130     return last_received_payload_type_;
    131   }
    132   void set_last_received_payload_type(int8_t last_received_payload_type) {
    133     CriticalSectionScoped cs(crit_sect_.get());
    134     last_received_payload_type_ = last_received_payload_type;
    135   }
    136 
    137   int8_t last_received_media_payload_type() const {
    138     CriticalSectionScoped cs(crit_sect_.get());
    139     return last_received_media_payload_type_;
    140   };
    141 
    142  private:
    143   // Prunes the payload type map of the specific payload type, if it exists.
    144   void DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(
    145       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
    146       const size_t payload_name_length,
    147       const uint32_t frequency,
    148       const uint8_t channels,
    149       const uint32_t rate);
    150 
    151   bool IsRtxInternal(const RTPHeader& header) const;
    152 
    153   scoped_ptr<CriticalSectionWrapper> crit_sect_;
    154   ModuleRTPUtility::PayloadTypeMap payload_type_map_;
    155   scoped_ptr<RTPPayloadStrategy> rtp_payload_strategy_;
    156   int8_t  red_payload_type_;
    157   int8_t ulpfec_payload_type_;
    158   int8_t incoming_payload_type_;
    159   int8_t  last_received_payload_type_;
    160   int8_t  last_received_media_payload_type_;
    161   bool rtx_;
    162   int8_t payload_type_rtx_;
    163   uint32_t ssrc_rtx_;
    164 };
    165 
    166 }  // namespace webrtc
    167 
    168 #endif  // WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_PAYLOAD_REGISTRY_H_
    169