JRTPLIB  3.11.2 (development version)
rtpudpv4transmitter.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2017 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 "rtpabortdescriptors.h"
48 #include <list>
49 
50 #ifdef RTP_SUPPORT_THREAD
51  #include <jthread/jmutex.h>
52 #endif // RTP_SUPPORT_THREAD
53 
54 #define RTPUDPV4TRANS_HASHSIZE 8317
55 #define RTPUDPV4TRANS_DEFAULTPORTBASE 5000
56 
57 #define RTPUDPV4TRANS_RTPRECEIVEBUFFER 32768
58 #define RTPUDPV4TRANS_RTCPRECEIVEBUFFER 32768
59 #define RTPUDPV4TRANS_RTPTRANSMITBUFFER 32768
60 #define RTPUDPV4TRANS_RTCPTRANSMITBUFFER 32768
61 
62 namespace jrtplib
63 {
64 
66 class JRTPLIB_IMPORTEXPORT RTPUDPv4TransmissionParams : public RTPTransmissionParams
67 {
68 public:
70 
72  void SetBindIP(uint32_t ip) { bindIP = ip; }
73 
75  void SetMulticastInterfaceIP(uint32_t ip) { mcastifaceIP = ip; }
76 
80  void SetPortbase(uint16_t pbase) { portbase = pbase; }
81 
83  void SetMulticastTTL(uint8_t mcastTTL) { multicastTTL = mcastTTL; }
84 
86  void SetLocalIPList(std::list<uint32_t> &iplist) { localIPs = iplist; }
87 
92  void ClearLocalIPList() { localIPs.clear(); }
93 
95  uint32_t GetBindIP() const { return bindIP; }
96 
98  uint32_t GetMulticastInterfaceIP() const { return mcastifaceIP; }
99 
101  uint16_t GetPortbase() const { return portbase; }
102 
104  uint8_t GetMulticastTTL() const { return multicastTTL; }
105 
107  const std::list<uint32_t> &GetLocalIPList() const { return localIPs; }
108 
110  void SetRTPSendBuffer(int s) { rtpsendbuf = s; }
111 
113  void SetRTPReceiveBuffer(int s) { rtprecvbuf = s; }
114 
116  void SetRTCPSendBuffer(int s) { rtcpsendbuf = s; }
117 
119  void SetRTCPReceiveBuffer(int s) { rtcprecvbuf = s; }
120 
122  void SetRTCPMultiplexing(bool f) { rtcpmux = f; }
123 
125  void SetAllowOddPortbase(bool f) { allowoddportbase = f; }
126 
129  void SetForcedRTCPPort(uint16_t rtcpport) { forcedrtcpport = rtcpport; }
130 
134  void SetUseExistingSockets(SocketType rtpsocket, SocketType rtcpsocket) { rtpsock = rtpsocket; rtcpsock = rtcpsocket; useexistingsockets = true; }
135 
139  void SetCreatedAbortDescriptors(RTPAbortDescriptors *desc) { m_pAbortDesc = desc; }
140 
142  int GetRTPSendBuffer() const { return rtpsendbuf; }
143 
145  int GetRTPReceiveBuffer() const { return rtprecvbuf; }
146 
148  int GetRTCPSendBuffer() const { return rtcpsendbuf; }
149 
151  int GetRTCPReceiveBuffer() const { return rtcprecvbuf; }
152 
154  bool GetRTCPMultiplexing() const { return rtcpmux; }
155 
157  bool GetAllowOddPortbase() const { return allowoddportbase; }
158 
160  uint16_t GetForcedRTCPPort() const { return forcedrtcpport; }
161 
164  bool GetUseExistingSockets(SocketType &rtpsocket, SocketType &rtcpsocket) const { if (!useexistingsockets) return false; rtpsocket = rtpsock; rtcpsocket = rtcpsock; return true; }
165 
169  RTPAbortDescriptors *GetCreatedAbortDescriptors() const { return m_pAbortDesc; }
170 private:
171  uint16_t portbase;
172  uint32_t bindIP, mcastifaceIP;
173  std::list<uint32_t> localIPs;
174  uint8_t multicastTTL;
175  int rtpsendbuf, rtprecvbuf;
176  int rtcpsendbuf, rtcprecvbuf;
177  bool rtcpmux;
178  bool allowoddportbase;
179  uint16_t forcedrtcpport;
180 
181  SocketType rtpsock, rtcpsock;
182  bool useexistingsockets;
183 
184  RTPAbortDescriptors *m_pAbortDesc;
185 };
186 
187 inline RTPUDPv4TransmissionParams::RTPUDPv4TransmissionParams() : RTPTransmissionParams(RTPTransmitter::IPv4UDPProto)
188 {
189  portbase = RTPUDPV4TRANS_DEFAULTPORTBASE;
190  bindIP = 0;
191  multicastTTL = 1;
192  mcastifaceIP = 0;
193  rtpsendbuf = RTPUDPV4TRANS_RTPTRANSMITBUFFER;
194  rtprecvbuf = RTPUDPV4TRANS_RTPRECEIVEBUFFER;
195  rtcpsendbuf = RTPUDPV4TRANS_RTCPTRANSMITBUFFER;
196  rtcprecvbuf = RTPUDPV4TRANS_RTCPRECEIVEBUFFER;
197  rtcpmux = false;
198  allowoddportbase = false;
199  forcedrtcpport = 0;
200  useexistingsockets = false;
201  rtpsock = 0;
202  rtcpsock = 0;
203  m_pAbortDesc = 0;
204 }
205 
207 class JRTPLIB_IMPORTEXPORT RTPUDPv4TransmissionInfo : public RTPTransmissionInfo
208 {
209 public:
210  RTPUDPv4TransmissionInfo(std::list<uint32_t> iplist,SocketType rtpsock,SocketType rtcpsock,
211  uint16_t rtpport, uint16_t rtcpport) : RTPTransmissionInfo(RTPTransmitter::IPv4UDPProto)
212  { localIPlist = iplist; rtpsocket = rtpsock; rtcpsocket = rtcpsock; m_rtpPort = rtpport; m_rtcpPort = rtcpport; }
213 
215 
217  std::list<uint32_t> GetLocalIPList() const { return localIPlist; }
218 
220  SocketType GetRTPSocket() const { return rtpsocket; }
221 
223  SocketType GetRTCPSocket() const { return rtcpsocket; }
224 
226  uint16_t GetRTPPort() const { return m_rtpPort; }
227 
229  uint16_t GetRTCPPort() const { return m_rtcpPort; }
230 private:
231  std::list<uint32_t> localIPlist;
232  SocketType rtpsocket,rtcpsocket;
233  uint16_t m_rtpPort, m_rtcpPort;
234 };
235 
236 class JRTPLIB_IMPORTEXPORT RTPUDPv4Trans_GetHashIndex_IPv4Dest
237 {
238 public:
239  static int GetIndex(const RTPIPv4Destination &d) { return d.GetIP()%RTPUDPV4TRANS_HASHSIZE; }
240 };
241 
242 class JRTPLIB_IMPORTEXPORT RTPUDPv4Trans_GetHashIndex_uint32_t
243 {
244 public:
245  static int GetIndex(const uint32_t &k) { return k%RTPUDPV4TRANS_HASHSIZE; }
246 };
247 
248 #define RTPUDPV4TRANS_HEADERSIZE (20+8)
249 
257 class JRTPLIB_IMPORTEXPORT RTPUDPv4Transmitter : public RTPTransmitter
258 {
259  JRTPLIB_NO_COPY(RTPUDPv4Transmitter)
260 public:
263 
264  int Init(bool treadsafe);
265  int Create(size_t maxpacksize,const RTPTransmissionParams *transparams);
266  void Destroy();
267  RTPTransmissionInfo *GetTransmissionInfo();
268  void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
269 
270  int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
271  bool ComesFromThisTransmitter(const RTPAddress *addr);
272  size_t GetHeaderOverhead() { return RTPUDPV4TRANS_HEADERSIZE; }
273 
274  int Poll();
275  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
276  int AbortWait();
277 
278  int SendRTPData(const void *data,size_t len);
279  int SendRTCPData(const void *data,size_t len);
280 
281  int AddDestination(const RTPAddress &addr);
282  int DeleteDestination(const RTPAddress &addr);
283  void ClearDestinations();
284 
285  bool SupportsMulticasting();
286  int JoinMulticastGroup(const RTPAddress &addr);
287  int LeaveMulticastGroup(const RTPAddress &addr);
288  void LeaveAllMulticastGroups();
289 
290  int SetReceiveMode(RTPTransmitter::ReceiveMode m);
291  int AddToIgnoreList(const RTPAddress &addr);
292  int DeleteFromIgnoreList(const RTPAddress &addr);
293  void ClearIgnoreList();
294  int AddToAcceptList(const RTPAddress &addr);
295  int DeleteFromAcceptList(const RTPAddress &addr);
296  void ClearAcceptList();
297  int SetMaximumPacketSize(size_t s);
298 
299  bool NewDataAvailable();
300  RTPRawPacket *GetNextPacket();
301 #ifdef RTPDEBUG
302  void Dump();
303 #endif // RTPDEBUG
304 private:
305  int CreateLocalIPList();
306  bool GetLocalIPList_Interfaces();
307  void GetLocalIPList_DNS();
308  void AddLoopbackAddress();
309  void FlushPackets();
310  int PollSocket(bool rtp);
311  int ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port);
312  int ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port);
313 #ifdef RTP_SUPPORT_IPV4MULTICAST
314  bool SetMulticastTTL(uint8_t ttl);
315 #endif // RTP_SUPPORT_IPV4MULTICAST
316  bool ShouldAcceptData(uint32_t srcip,uint16_t srcport);
317  void ClearAcceptIgnoreInfo();
318 
319  bool init;
320  bool created;
321  bool waitingfordata;
322  SocketType rtpsock,rtcpsock;
323  uint32_t mcastifaceIP;
324  std::list<uint32_t> localIPs;
325  uint16_t m_rtpPort, m_rtcpPort;
326  uint8_t multicastTTL;
327  RTPTransmitter::ReceiveMode receivemode;
328 
329  uint8_t *localhostname;
330  size_t localhostnamelength;
331 
332  RTPHashTable<const RTPIPv4Destination,RTPUDPv4Trans_GetHashIndex_IPv4Dest,RTPUDPV4TRANS_HASHSIZE> destinations;
333 #ifdef RTP_SUPPORT_IPV4MULTICAST
334  RTPHashTable<const uint32_t,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> multicastgroups;
335 #endif // RTP_SUPPORT_IPV4MULTICAST
336  std::list<RTPRawPacket*> rawpacketlist;
337 
338  bool supportsmulticasting;
339  size_t maxpacksize;
340 
341  class PortInfo
342  {
343  public:
344  PortInfo() { all = false; }
345 
346  bool all;
347  std::list<uint16_t> portlist;
348  };
349 
350  RTPKeyHashTable<const uint32_t,PortInfo*,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> acceptignoreinfo;
351 
352  bool closesocketswhendone;
353  RTPAbortDescriptors m_abortDesc;
354  RTPAbortDescriptors *m_pAbortDesc; // in case an external one was specified
355 
356 #ifdef RTP_SUPPORT_THREAD
357  jthread::JMutex mainmutex,waitmutex;
358  int threadsafe;
359 #endif // RTP_SUPPORT_THREAD
360 };
361 
362 } // end namespace
363 
364 #endif // RTPUDPV4TRANSMITTER_H
365 
Helper class for several RTPTransmitter instances, to be able to cancel a call to 'select',...
Definition: rtpabortdescriptors.h:65
This class is an abstract class which is used to specify destinations, multicast groups etc.
Definition: rtpaddress.h:51
A memory manager.
Definition: rtpmemorymanager.h:151
This class is used by the transmission component to store the incoming RTP and RTCP data in.
Definition: rtprawpacket.h:53
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:86
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:249
Base class for transmission parameters.
Definition: rtptransmitter.h:230
Abstract class from which actual transmission components should be derived.
Definition: rtptransmitter.h:63
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:81
@ IPv4UDPProto
Specifies the internal UDP over IPv4 transmitter.
Definition: rtptransmitter.h:72
Additional information about the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:208
SocketType GetRTPSocket() const
Returns the socket descriptor used for receiving and transmitting RTP packets.
Definition: rtpudpv4transmitter.h:220
std::list< uint32_t > GetLocalIPList() const
Returns the list of IPv4 addresses the transmitter considers to be the local IP addresses.
Definition: rtpudpv4transmitter.h:217
SocketType GetRTCPSocket() const
Returns the socket descriptor used for receiving and transmitting RTCP packets.
Definition: rtpudpv4transmitter.h:223
uint16_t GetRTPPort() const
Returns the port number that the RTP socket receives packets on.
Definition: rtpudpv4transmitter.h:226
uint16_t GetRTCPPort() const
Returns the port number that the RTCP socket receives packets on.
Definition: rtpudpv4transmitter.h:229
Parameters for the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:67
void SetUseExistingSockets(SocketType rtpsocket, SocketType rtcpsocket)
Use sockets that have already been created, no checks on port numbers will be done,...
Definition: rtpudpv4transmitter.h:134
bool GetAllowOddPortbase() const
If true, any RTP portbase will be allowed, not just even numbers.
Definition: rtpudpv4transmitter.h:157
void ClearLocalIPList()
Clears the list of local IP addresses.
Definition: rtpudpv4transmitter.h:92
int GetRTPReceiveBuffer() const
Returns the RTP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:145
int GetRTPSendBuffer() const
Returns the RTP socket's send buffer size.
Definition: rtpudpv4transmitter.h:142
void SetMulticastInterfaceIP(uint32_t ip)
Sets the multicast interface IP address.
Definition: rtpudpv4transmitter.h:75
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:122
void SetRTCPSendBuffer(int s)
Sets the RTCP socket's send buffer size.
Definition: rtpudpv4transmitter.h:116
void SetRTPSendBuffer(int s)
Sets the RTP socket's send buffer size.
Definition: rtpudpv4transmitter.h:110
const std::list< uint32_t > & GetLocalIPList() const
Returns the list of local IP addresses.
Definition: rtpudpv4transmitter.h:107
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:86
void SetAllowOddPortbase(bool f)
Can be used to allow the RTP port base to be any number, not just even numbers.
Definition: rtpudpv4transmitter.h:125
void SetCreatedAbortDescriptors(RTPAbortDescriptors *desc)
If non null, the specified abort descriptors will be used to cancel the function that's waiting for p...
Definition: rtpudpv4transmitter.h:139
uint16_t GetForcedRTCPPort() const
If non-zero, the specified port will be used to receive RTCP traffic.
Definition: rtpudpv4transmitter.h:160
int GetRTCPReceiveBuffer() const
Returns the RTCP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:151
void SetRTPReceiveBuffer(int s)
Sets the RTP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:113
uint32_t GetMulticastInterfaceIP() const
Returns the multicast interface IP address.
Definition: rtpudpv4transmitter.h:98
int GetRTCPSendBuffer() const
Returns the RTCP socket's send buffer size.
Definition: rtpudpv4transmitter.h:148
void SetBindIP(uint32_t ip)
Sets the IP address which is used to bind the sockets to ip.
Definition: rtpudpv4transmitter.h:72
bool GetUseExistingSockets(SocketType &rtpsocket, SocketType &rtcpsocket) const
Returns true and fills in sockets if existing sockets were set using RTPUDPv4TransmissionParams::SetU...
Definition: rtpudpv4transmitter.h:164
void SetPortbase(uint16_t pbase)
Sets the RTP portbase to pbase, which has to be an even number unless RTPUDPv4TransmissionParams::Set...
Definition: rtpudpv4transmitter.h:80
void SetRTCPReceiveBuffer(int s)
Sets the RTCP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:119
uint16_t GetPortbase() const
Returns the RTP portbase which will be used (default is 5000).
Definition: rtpudpv4transmitter.h:101
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:129
void SetMulticastTTL(uint8_t mcastTTL)
Sets the multicast TTL to be used to mcastTTL.
Definition: rtpudpv4transmitter.h:83
RTPAbortDescriptors * GetCreatedAbortDescriptors() const
If non-null, this RTPAbortDescriptors instance will be used internally, which can be useful when crea...
Definition: rtpudpv4transmitter.h:169
uint32_t GetBindIP() const
Returns the IP address which will be used to bind the sockets.
Definition: rtpudpv4transmitter.h:95
uint8_t GetMulticastTTL() const
Returns the multicast TTL which will be used (default is 1).
Definition: rtpudpv4transmitter.h:104
bool GetRTCPMultiplexing() const
Returns a flag indicating if RTCP traffic will be multiplexed over the RTP channel.
Definition: rtpudpv4transmitter.h:154
An UDP over IPv4 transmission component.
Definition: rtpudpv4transmitter.h:258