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
18namespace webrtc {
19
20// This strategy deals with the audio/video-specific aspects
21// of payload handling.
22class RTPPayloadStrategy {
23 public:
24  virtual ~RTPPayloadStrategy() {}
25
26  virtual bool CodecsMustBeUnique() const = 0;
27
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;
32
33  virtual void UpdatePayloadRate(RtpUtility::Payload* payload,
34                                 const uint32_t rate) const = 0;
35
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;
42
43  virtual int GetPayloadTypeFrequency(
44      const RtpUtility::Payload& payload) const = 0;
45
46  static RTPPayloadStrategy* CreateStrategy(const bool handling_audio);
47
48 protected:
49  RTPPayloadStrategy() {}
50};
51
52class RTPPayloadRegistry {
53 public:
54  // The registry takes ownership of the strategy.
55  RTPPayloadRegistry(RTPPayloadStrategy* rtp_payload_strategy);
56  ~RTPPayloadRegistry();
57
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,
63      const uint32_t rate,
64      bool* created_new_payload_type);
65
66  int32_t DeRegisterReceivePayload(
67      const int8_t payload_type);
68
69  int32_t ReceivePayloadType(
70      const char payload_name[RTP_PAYLOAD_NAME_SIZE],
71      const uint32_t frequency,
72      const uint8_t channels,
73      const uint32_t rate,
74      int8_t* payload_type) const;
75
76  bool RtxEnabled() const;
77
78  void SetRtxSsrc(uint32_t ssrc);
79
80  void SetRtxPayloadType(int payload_type);
81
82  bool IsRtx(const RTPHeader& header) const;
83
84  bool RestoreOriginalPacket(uint8_t** restored_packet,
85                             const uint8_t* packet,
86                             int* packet_length,
87                             uint32_t original_ssrc,
88                             const RTPHeader& header) const;
89
90  bool IsRed(const RTPHeader& header) const;
91
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;
95
96  bool GetPayloadSpecifics(uint8_t payload_type, PayloadUnion* payload) const;
97
98  int GetPayloadTypeFrequency(uint8_t payload_type) const;
99
100  bool PayloadTypeToPayload(const uint8_t payload_type,
101                            RtpUtility::Payload*& payload) const;
102
103  void ResetLastReceivedPayloadTypes() {
104    CriticalSectionScoped cs(crit_sect_.get());
105    last_received_payload_type_ = -1;
106    last_received_media_payload_type_ = -1;
107  }
108
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);
113
114  // Returns true if the new media payload type has not changed.
115  bool ReportMediaPayloadType(uint8_t media_payload_type);
116
117  int8_t red_payload_type() const {
118    CriticalSectionScoped cs(crit_sect_.get());
119    return red_payload_type_;
120  }
121  int8_t ulpfec_payload_type() const {
122    CriticalSectionScoped cs(crit_sect_.get());
123    return ulpfec_payload_type_;
124  }
125  int8_t last_received_payload_type() const {
126    CriticalSectionScoped cs(crit_sect_.get());
127    return last_received_payload_type_;
128  }
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;
132  }
133
134  int8_t last_received_media_payload_type() const {
135    CriticalSectionScoped cs(crit_sect_.get());
136    return last_received_media_payload_type_;
137  };
138
139 private:
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);
147
148  bool IsRtxInternal(const RTPHeader& header) const;
149
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_;
158  bool rtx_;
159  int8_t payload_type_rtx_;
160  uint32_t ssrc_rtx_;
161};
162
163}  // namespace webrtc
164
165#endif  // WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_PAYLOAD_REGISTRY_H_
166