JRTPLIB  3.10.0
rtpudpv4transmitter.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2016 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the Expertise Centre for Digital Media
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31 */
32 
37 #ifndef RTPUDPV4TRANSMITTER_H
38 
39 #define RTPUDPV4TRANSMITTER_H
40 
41 #include "rtpconfig.h"
42 #include "rtptransmitter.h"
43 #include "rtpipv4destination.h"
44 #include "rtphashtable.h"
45 #include "rtpkeyhashtable.h"
46 #include "rtpsocketutil.h"
47 #include <list>
48 
49 #ifdef RTP_SUPPORT_THREAD
50  #include <jthread/jmutex.h>
51 #endif // RTP_SUPPORT_THREAD
52 
53 #define RTPUDPV4TRANS_HASHSIZE 8317
54 #define RTPUDPV4TRANS_DEFAULTPORTBASE 5000
55 
56 #define RTPUDPV4TRANS_RTPRECEIVEBUFFER 32768
57 #define RTPUDPV4TRANS_RTCPRECEIVEBUFFER 32768
58 #define RTPUDPV4TRANS_RTPTRANSMITBUFFER 32768
59 #define RTPUDPV4TRANS_RTCPTRANSMITBUFFER 32768
60 
61 namespace jrtplib
62 {
63 
65 class JRTPLIB_IMPORTEXPORT RTPUDPv4TransmissionParams : public RTPTransmissionParams
66 {
67 public:
69 
71  void SetBindIP(uint32_t ip) { bindIP = ip; }
72 
74  void SetMulticastInterfaceIP(uint32_t ip) { mcastifaceIP = ip; }
75 
79  void SetPortbase(uint16_t pbase) { portbase = pbase; }
80 
82  void SetMulticastTTL(uint8_t mcastTTL) { multicastTTL = mcastTTL; }
83 
85  void SetLocalIPList(std::list<uint32_t> &iplist) { localIPs = iplist; }
86 
91  void ClearLocalIPList() { localIPs.clear(); }
92 
94  uint32_t GetBindIP() const { return bindIP; }
95 
97  uint32_t GetMulticastInterfaceIP() const { return mcastifaceIP; }
98 
100  uint16_t GetPortbase() const { return portbase; }
101 
103  uint8_t GetMulticastTTL() const { return multicastTTL; }
104 
106  const std::list<uint32_t> &GetLocalIPList() const { return localIPs; }
107 
109  void SetRTPSendBuffer(int s) { rtpsendbuf = s; }
110 
112  void SetRTPReceiveBuffer(int s) { rtprecvbuf = s; }
113 
115  void SetRTCPSendBuffer(int s) { rtcpsendbuf = s; }
116 
118  void SetRTCPReceiveBuffer(int s) { rtcprecvbuf = s; }
119 
121  void SetRTCPMultiplexing(bool f) { rtcpmux = f; }
122 
124  void SetAllowOddPortbase(bool f) { allowoddportbase = f; }
125 
128  void SetForcedRTCPPort(uint16_t rtcpport) { forcedrtcpport = rtcpport; }
129 
133  void SetUseExistingSockets(SocketType rtpsocket, SocketType rtcpsocket) { rtpsock = rtpsocket; rtcpsock = rtcpsocket; useexistingsockets = true; }
134 
136  int GetRTPSendBuffer() const { return rtpsendbuf; }
137 
139  int GetRTPReceiveBuffer() const { return rtprecvbuf; }
140 
142  int GetRTCPSendBuffer() const { return rtcpsendbuf; }
143 
145  int GetRTCPReceiveBuffer() const { return rtcprecvbuf; }
146 
148  bool GetRTCPMultiplexing() const { return rtcpmux; }
149 
151  bool GetAllowOddPortbase() const { return allowoddportbase; }
152 
154  uint16_t GetForcedRTCPPort() const { return forcedrtcpport; }
155 
158  bool GetUseExistingSockets(SocketType &rtpsocket, SocketType &rtcpsocket) const { if (!useexistingsockets) return false; rtpsocket = rtpsock; rtcpsocket = rtcpsock; return true; }
159 private:
160  uint16_t portbase;
161  uint32_t bindIP, mcastifaceIP;
162  std::list<uint32_t> localIPs;
163  uint8_t multicastTTL;
164  int rtpsendbuf, rtprecvbuf;
165  int rtcpsendbuf, rtcprecvbuf;
166  bool rtcpmux;
167  bool allowoddportbase;
168  uint16_t forcedrtcpport;
169 
170  SocketType rtpsock, rtcpsock;
171  bool useexistingsockets;
172 };
173 
174 inline RTPUDPv4TransmissionParams::RTPUDPv4TransmissionParams() : RTPTransmissionParams(RTPTransmitter::IPv4UDPProto)
175 {
176  portbase = RTPUDPV4TRANS_DEFAULTPORTBASE;
177  bindIP = 0;
178  multicastTTL = 1;
179  mcastifaceIP = 0;
180  rtpsendbuf = RTPUDPV4TRANS_RTPTRANSMITBUFFER;
181  rtprecvbuf = RTPUDPV4TRANS_RTPRECEIVEBUFFER;
182  rtcpsendbuf = RTPUDPV4TRANS_RTCPTRANSMITBUFFER;
183  rtcprecvbuf = RTPUDPV4TRANS_RTCPRECEIVEBUFFER;
184  rtcpmux = false;
185  allowoddportbase = false;
186  forcedrtcpport = 0;
187  useexistingsockets = false;
188  rtpsock = 0;
189  rtcpsock = 0;
190 }
191 
193 class JRTPLIB_IMPORTEXPORT RTPUDPv4TransmissionInfo : public RTPTransmissionInfo
194 {
195 public:
196  RTPUDPv4TransmissionInfo(std::list<uint32_t> iplist,SocketType rtpsock,SocketType rtcpsock,
197  uint16_t rtpport, uint16_t rtcpport) : RTPTransmissionInfo(RTPTransmitter::IPv4UDPProto)
198  { localIPlist = iplist; rtpsocket = rtpsock; rtcpsocket = rtcpsock; m_rtpPort = rtpport; m_rtcpPort = rtcpport; }
199 
201 
203  std::list<uint32_t> GetLocalIPList() const { return localIPlist; }
204 
206  SocketType GetRTPSocket() const { return rtpsocket; }
207 
209  SocketType GetRTCPSocket() const { return rtcpsocket; }
210 
212  uint16_t GetRTPPort() const { return m_rtpPort; }
213 
215  uint16_t GetRTCPPort() const { return m_rtcpPort; }
216 private:
217  std::list<uint32_t> localIPlist;
218  SocketType rtpsocket,rtcpsocket;
219  uint16_t m_rtpPort, m_rtcpPort;
220 };
221 
222 class JRTPLIB_IMPORTEXPORT RTPUDPv4Trans_GetHashIndex_IPv4Dest
223 {
224 public:
225  static int GetIndex(const RTPIPv4Destination &d) { return d.GetIP()%RTPUDPV4TRANS_HASHSIZE; }
226 };
227 
228 class JRTPLIB_IMPORTEXPORT RTPUDPv4Trans_GetHashIndex_uint32_t
229 {
230 public:
231  static int GetIndex(const uint32_t &k) { return k%RTPUDPV4TRANS_HASHSIZE; }
232 };
233 
234 #define RTPUDPV4TRANS_HEADERSIZE (20+8)
235 
243 class JRTPLIB_IMPORTEXPORT RTPUDPv4Transmitter : public RTPTransmitter
244 {
245 public:
248 
249  int Init(bool treadsafe);
250  int Create(size_t maxpacksize,const RTPTransmissionParams *transparams);
251  void Destroy();
252  RTPTransmissionInfo *GetTransmissionInfo();
253  void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
254 
255  int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
256  bool ComesFromThisTransmitter(const RTPAddress *addr);
257  size_t GetHeaderOverhead() { return RTPUDPV4TRANS_HEADERSIZE; }
258 
259  int Poll();
260  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
261  int AbortWait();
262 
263  int SendRTPData(const void *data,size_t len);
264  int SendRTCPData(const void *data,size_t len);
265 
266  int AddDestination(const RTPAddress &addr);
267  int DeleteDestination(const RTPAddress &addr);
268  void ClearDestinations();
269 
270  bool SupportsMulticasting();
271  int JoinMulticastGroup(const RTPAddress &addr);
272  int LeaveMulticastGroup(const RTPAddress &addr);
273  void LeaveAllMulticastGroups();
274 
275  int SetReceiveMode(RTPTransmitter::ReceiveMode m);
276  int AddToIgnoreList(const RTPAddress &addr);
277  int DeleteFromIgnoreList(const RTPAddress &addr);
278  void ClearIgnoreList();
279  int AddToAcceptList(const RTPAddress &addr);
280  int DeleteFromAcceptList(const RTPAddress &addr);
281  void ClearAcceptList();
282  int SetMaximumPacketSize(size_t s);
283 
284  bool NewDataAvailable();
285  RTPRawPacket *GetNextPacket();
286 #ifdef RTPDEBUG
287  void Dump();
288 #endif // RTPDEBUG
289 private:
290  int CreateLocalIPList();
291  bool GetLocalIPList_Interfaces();
292  void GetLocalIPList_DNS();
293  void AddLoopbackAddress();
294  void FlushPackets();
295  int PollSocket(bool rtp);
296  int ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port);
297  int ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port);
298 #ifdef RTP_SUPPORT_IPV4MULTICAST
299  bool SetMulticastTTL(uint8_t ttl);
300 #endif // RTP_SUPPORT_IPV4MULTICAST
301  bool ShouldAcceptData(uint32_t srcip,uint16_t srcport);
302  void ClearAcceptIgnoreInfo();
303 
304  bool init;
305  bool created;
306  bool waitingfordata;
307  SocketType rtpsock,rtcpsock;
308  uint32_t mcastifaceIP;
309  std::list<uint32_t> localIPs;
310  uint16_t m_rtpPort, m_rtcpPort;
311  uint8_t multicastTTL;
312  RTPTransmitter::ReceiveMode receivemode;
313 
314  uint8_t *localhostname;
315  size_t localhostnamelength;
316 
317  RTPHashTable<const RTPIPv4Destination,RTPUDPv4Trans_GetHashIndex_IPv4Dest,RTPUDPV4TRANS_HASHSIZE> destinations;
318 #ifdef RTP_SUPPORT_IPV4MULTICAST
319  RTPHashTable<const uint32_t,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> multicastgroups;
320 #endif // RTP_SUPPORT_IPV4MULTICAST
321  std::list<RTPRawPacket*> rawpacketlist;
322 
323  bool supportsmulticasting;
324  size_t maxpacksize;
325 
326  class PortInfo
327  {
328  public:
329  PortInfo() { all = false; }
330 
331  bool all;
332  std::list<uint16_t> portlist;
333  };
334 
335  RTPKeyHashTable<const uint32_t,PortInfo*,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> acceptignoreinfo;
336 
337  // notification descriptors for AbortWait (0 is for reading, 1 for writing)
338  SocketType abortdesc[2];
339  bool closesocketswhendone;
340 
341  int CreateAbortDescriptors();
342  void DestroyAbortDescriptors();
343  void AbortWaitInternal();
344 #ifdef RTP_SUPPORT_THREAD
345  jthread::JMutex mainmutex,waitmutex;
346  int threadsafe;
347 #endif // RTP_SUPPORT_THREAD
348 };
349 
350 } // end namespace
351 
352 #endif // RTPUDPV4TRANSMITTER_H
353 
bool GetUseExistingSockets(SocketType &rtpsocket, SocketType &rtcpsocket) const
Returns true and fills in sockets if existing sockets were set using RTPUDPv4TransmissionParams::SetU...
Definition: rtpudpv4transmitter.h:158
void SetLocalIPList(std::list< uint32_t > &iplist)
Passes a list of IP addresses which will be used as the local IP addresses.
Definition: rtpudpv4transmitter.h:85
int GetRTPSendBuffer() const
Returns the RTP socket's send buffer size.
Definition: rtpudpv4transmitter.h:136
uint16_t GetRTCPPort() const
Returns the port number that the RTCP socket receives packets on.
Definition: rtpudpv4transmitter.h:215
This class is used by the transmission component to store the incoming RTP and RTCP data in...
Definition: rtprawpacket.h:51
Specifies the internal UDP over IPv4 transmitter.
Definition: rtptransmitter.h:72
Base class for transmission parameters.
Definition: rtptransmitter.h:228
SocketType GetRTCPSocket() const
Returns the socket descriptor used for receiving and transmitting RTCP packets.
Definition: rtpudpv4transmitter.h:209
int GetRTPReceiveBuffer() const
Returns the RTP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:139
Additional information about the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:193
void SetForcedRTCPPort(uint16_t rtcpport)
Force the RTCP socket to use a specific port, not necessarily one more than the RTP port (set this to...
Definition: rtpudpv4transmitter.h:128
std::list< uint32_t > GetLocalIPList() const
Returns the list of IPv4 addresses the transmitter considers to be the local IP addresses.
Definition: rtpudpv4transmitter.h:203
int GetRTCPSendBuffer() const
Returns the RTCP socket's send buffer size.
Definition: rtpudpv4transmitter.h:142
void ClearLocalIPList()
Clears the list of local IP addresses.
Definition: rtpudpv4transmitter.h:91
Parameters for the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:65
uint8_t GetMulticastTTL() const
Returns the multicast TTL which will be used (default is 1).
Definition: rtpudpv4transmitter.h:103
void SetRTPSendBuffer(int s)
Sets the RTP socket's send buffer size.
Definition: rtpudpv4transmitter.h:109
This class is an abstract class which is used to specify destinations, multicast groups etc...
Definition: rtpaddress.h:50
void SetMulticastInterfaceIP(uint32_t ip)
Sets the multicast interface IP address.
Definition: rtpudpv4transmitter.h:74
uint32_t GetBindIP() const
Returns the IP address which will be used to bind the sockets.
Definition: rtpudpv4transmitter.h:94
SocketType GetRTPSocket() const
Returns the socket descriptor used for receiving and transmitting RTP packets.
Definition: rtpudpv4transmitter.h:206
uint16_t GetRTPPort() const
Returns the port number that the RTP socket receives packets on.
Definition: rtpudpv4transmitter.h:212
uint32_t GetMulticastInterfaceIP() const
Returns the multicast interface IP address.
Definition: rtpudpv4transmitter.h:97
uint16_t GetForcedRTCPPort() const
If non-zero, the specified port will be used to receive RTCP traffic.
Definition: rtpudpv4transmitter.h:154
An UDP over IPv4 transmission component.
Definition: rtpudpv4transmitter.h:243
Abstract class from which actual transmission components should be derived.
Definition: rtptransmitter.h:62
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:79
void SetAllowOddPortbase(bool f)
Can be used to allow the RTP port base to be any number, not just even numbers.
Definition: rtpudpv4transmitter.h:124
void SetRTCPReceiveBuffer(int s)
Sets the RTCP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:118
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:84
void SetUseExistingSockets(SocketType rtpsocket, SocketType rtcpsocket)
Use sockets that have already been created, no checks on port numbers will be done, and no buffer sizes will be set; you'll need to close the sockets yourself when done, it will not be done automatically.
Definition: rtpudpv4transmitter.h:133
void SetRTCPMultiplexing(bool f)
Enables or disables multiplexing RTCP traffic over the RTP channel, so that only a single port is use...
Definition: rtpudpv4transmitter.h:121
A memory manager.
Definition: rtpmemorymanager.h:150
void SetRTCPSendBuffer(int s)
Sets the RTCP socket's send buffer size.
Definition: rtpudpv4transmitter.h:115
void SetMulticastTTL(uint8_t mcastTTL)
Sets the multicast TTL to be used to mcastTTL.
Definition: rtpudpv4transmitter.h:82
void SetPortbase(uint16_t pbase)
Sets the RTP portbase to pbase, which has to be an even number unless RTPUDPv4TransmissionParams::Set...
Definition: rtpudpv4transmitter.h:79
int GetRTCPReceiveBuffer() const
Returns the RTCP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:145
const std::list< uint32_t > & GetLocalIPList() const
Returns the list of local IP addresses.
Definition: rtpudpv4transmitter.h:106
bool GetAllowOddPortbase() const
If true, any RTP portbase will be allowed, not just even numbers.
Definition: rtpudpv4transmitter.h:151
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:247
void SetBindIP(uint32_t ip)
Sets the IP address which is used to bind the sockets to ip.
Definition: rtpudpv4transmitter.h:71
bool GetRTCPMultiplexing() const
Returns a flag indicating if RTCP traffic will be multiplexed over the RTP channel.
Definition: rtpudpv4transmitter.h:148
uint16_t GetPortbase() const
Returns the RTP portbase which will be used (default is 5000).
Definition: rtpudpv4transmitter.h:100
void SetRTPReceiveBuffer(int s)
Sets the RTP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:112