2 * Copyright (c) 2013 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 #ifndef WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_PAYLOAD_REGISTRY_H_
12 #define WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_PAYLOAD_REGISTRY_H_
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"
20 // This strategy deals with the audio/video-specific aspects
21 // of payload handling.
22 class RTPPayloadStrategy {
24 virtual ~RTPPayloadStrategy() {}
26 virtual bool CodecsMustBeUnique() const = 0;
28 virtual bool PayloadIsCompatible(const RtpUtility::Payload& payload,
29 const uint32_t frequency,
30 const uint8_t channels,
31 const uint32_t rate) const = 0;
33 virtual void UpdatePayloadRate(RtpUtility::Payload* payload,
34 const uint32_t rate) const = 0;
36 virtual RtpUtility::Payload* CreatePayloadType(
37 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
38 const int8_t payloadType,
39 const uint32_t frequency,
40 const uint8_t channels,
41 const uint32_t rate) const = 0;
43 virtual int GetPayloadTypeFrequency(
44 const RtpUtility::Payload& payload) const = 0;
46 static RTPPayloadStrategy* CreateStrategy(const bool handling_audio);
49 RTPPayloadStrategy() {}
52 class RTPPayloadRegistry {
54 // The registry takes ownership of the strategy.
55 RTPPayloadRegistry(RTPPayloadStrategy* rtp_payload_strategy);
56 ~RTPPayloadRegistry();
58 int32_t RegisterReceivePayload(
59 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
60 const int8_t payload_type,
61 const uint32_t frequency,
62 const uint8_t channels,
64 bool* created_new_payload_type);
66 int32_t DeRegisterReceivePayload(
67 const int8_t payload_type);
69 int32_t ReceivePayloadType(
70 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
71 const uint32_t frequency,
72 const uint8_t channels,
74 int8_t* payload_type) const;
76 bool RtxEnabled() const;
78 void SetRtxSsrc(uint32_t ssrc);
80 void SetRtxPayloadType(int payload_type);
82 bool IsRtx(const RTPHeader& header) const;
84 bool RestoreOriginalPacket(uint8_t** restored_packet,
85 const uint8_t* packet,
87 uint32_t original_ssrc,
88 const RTPHeader& header) const;
90 bool IsRed(const RTPHeader& header) const;
92 // Returns true if the media of this RTP packet is encapsulated within an
93 // extra header, such as RTX or RED.
94 bool IsEncapsulated(const RTPHeader& header) const;
96 bool GetPayloadSpecifics(uint8_t payload_type, PayloadUnion* payload) const;
98 int GetPayloadTypeFrequency(uint8_t payload_type) const;
100 bool PayloadTypeToPayload(const uint8_t payload_type,
101 RtpUtility::Payload*& payload) const;
103 void ResetLastReceivedPayloadTypes() {
104 CriticalSectionScoped cs(crit_sect_.get());
105 last_received_payload_type_ = -1;
106 last_received_media_payload_type_ = -1;
109 // This sets the payload type of the packets being received from the network
110 // on the media SSRC. For instance if packets are encapsulated with RED, this
111 // payload type will be the RED payload type.
112 void SetIncomingPayloadType(const RTPHeader& header);
114 // Returns true if the new media payload type has not changed.
115 bool ReportMediaPayloadType(uint8_t media_payload_type);
117 int8_t red_payload_type() const {
118 CriticalSectionScoped cs(crit_sect_.get());
119 return red_payload_type_;
121 int8_t ulpfec_payload_type() const {
122 CriticalSectionScoped cs(crit_sect_.get());
123 return ulpfec_payload_type_;
125 int8_t last_received_payload_type() const {
126 CriticalSectionScoped cs(crit_sect_.get());
127 return last_received_payload_type_;
129 void set_last_received_payload_type(int8_t last_received_payload_type) {
130 CriticalSectionScoped cs(crit_sect_.get());
131 last_received_payload_type_ = last_received_payload_type;
134 int8_t last_received_media_payload_type() const {
135 CriticalSectionScoped cs(crit_sect_.get());
136 return last_received_media_payload_type_;
140 // Prunes the payload type map of the specific payload type, if it exists.
141 void DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(
142 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
143 const size_t payload_name_length,
144 const uint32_t frequency,
145 const uint8_t channels,
146 const uint32_t rate);
148 bool IsRtxInternal(const RTPHeader& header) const;
150 scoped_ptr<CriticalSectionWrapper> crit_sect_;
151 RtpUtility::PayloadTypeMap payload_type_map_;
152 scoped_ptr<RTPPayloadStrategy> rtp_payload_strategy_;
153 int8_t red_payload_type_;
154 int8_t ulpfec_payload_type_;
155 int8_t incoming_payload_type_;
156 int8_t last_received_payload_type_;
157 int8_t last_received_media_payload_type_;
159 int8_t payload_type_rtx_;
163 } // namespace webrtc
165 #endif // WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_PAYLOAD_REGISTRY_H_