JRTPLIB  3.11.1
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 public:
262 
263  int Init(bool treadsafe);
264  int Create(size_t maxpacksize,const RTPTransmissionParams *transparams);
265  void Destroy();
266  RTPTransmissionInfo *GetTransmissionInfo();
267  void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
268 
269  int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
270  bool ComesFromThisTransmitter(const RTPAddress *addr);
271  size_t GetHeaderOverhead() { return RTPUDPV4TRANS_HEADERSIZE; }
272 
273  int Poll();
274  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
275  int AbortWait();
276 
277  int SendRTPData(const void *data,size_t len);
278  int SendRTCPData(const void *data,size_t len);
279 
280  int AddDestination(const RTPAddress &addr);
281  int DeleteDestination(const RTPAddress &addr);
282  void ClearDestinations();
283 
284  bool SupportsMulticasting();
285  int JoinMulticastGroup(const RTPAddress &addr);
286  int LeaveMulticastGroup(const RTPAddress &addr);
287  void LeaveAllMulticastGroups();
288 
289  int SetReceiveMode(RTPTransmitter::ReceiveMode m);
290  int AddToIgnoreList(const RTPAddress &addr);
291  int DeleteFromIgnoreList(const RTPAddress &addr);
292  void ClearIgnoreList();
293  int AddToAcceptList(const RTPAddress &addr);
294  int DeleteFromAcceptList(const RTPAddress &addr);
295  void ClearAcceptList();
296  int SetMaximumPacketSize(size_t s);
297 
298  bool NewDataAvailable();
299  RTPRawPacket *GetNextPacket();
300 #ifdef RTPDEBUG
301  void Dump();
302 #endif // RTPDEBUG
303 private:
304  int CreateLocalIPList();
305  bool GetLocalIPList_Interfaces();
306  void GetLocalIPList_DNS();
307  void AddLoopbackAddress();
308  void FlushPackets();
309  int PollSocket(bool rtp);
310  int ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port);
311  int ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port);
312 #ifdef RTP_SUPPORT_IPV4MULTICAST
313  bool SetMulticastTTL(uint8_t ttl);
314 #endif // RTP_SUPPORT_IPV4MULTICAST
315  bool ShouldAcceptData(uint32_t srcip,uint16_t srcport);
316  void ClearAcceptIgnoreInfo();
317 
318  bool init;
319  bool created;
320  bool waitingfordata;
321  SocketType rtpsock,rtcpsock;
322  uint32_t mcastifaceIP;
323  std::list<uint32_t> localIPs;
324  uint16_t m_rtpPort, m_rtcpPort;
325  uint8_t multicastTTL;
326  RTPTransmitter::ReceiveMode receivemode;
327 
328  uint8_t *localhostname;
329  size_t localhostnamelength;
330 
331  RTPHashTable<const RTPIPv4Destination,RTPUDPv4Trans_GetHashIndex_IPv4Dest,RTPUDPV4TRANS_HASHSIZE> destinations;
332 #ifdef RTP_SUPPORT_IPV4MULTICAST
333  RTPHashTable<const uint32_t,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> multicastgroups;
334 #endif // RTP_SUPPORT_IPV4MULTICAST
335  std::list<RTPRawPacket*> rawpacketlist;
336 
337  bool supportsmulticasting;
338  size_t maxpacksize;
339 
340  class PortInfo
341  {
342  public:
343  PortInfo() { all = false; }
344 
345  bool all;
346  std::list<uint16_t> portlist;
347  };
348 
349  RTPKeyHashTable<const uint32_t,PortInfo*,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> acceptignoreinfo;
350 
351  bool closesocketswhendone;
352  RTPAbortDescriptors m_abortDesc;
353  RTPAbortDescriptors *m_pAbortDesc; // in case an external one was specified
354 
355 #ifdef RTP_SUPPORT_THREAD
356  jthread::JMutex mainmutex,waitmutex;
357  int threadsafe;
358 #endif // RTP_SUPPORT_THREAD
359 };
360 
361 } // end namespace
362 
363 #endif // RTPUDPV4TRANSMITTER_H
364 
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 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
int GetRTPSendBuffer() const
Returns the RTP socket&#39;s send buffer size.
Definition: rtpudpv4transmitter.h:142
uint16_t GetRTCPPort() const
Returns the port number that the RTCP socket receives packets on.
Definition: rtpudpv4transmitter.h:229
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
RTPAbortDescriptors * GetCreatedAbortDescriptors() const
If non-null, this RTPAbortDescriptors instance will be used internally, which can be useful when crea...
Definition: rtpudpv4transmitter.h:169
Base class for transmission parameters.
Definition: rtptransmitter.h:229
SocketType GetRTCPSocket() const
Returns the socket descriptor used for receiving and transmitting RTCP packets.
Definition: rtpudpv4transmitter.h:223
int GetRTPReceiveBuffer() const
Returns the RTP socket&#39;s receive buffer size.
Definition: rtpudpv4transmitter.h:145
Additional information about the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:207
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
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
int GetRTCPSendBuffer() const
Returns the RTCP socket&#39;s send buffer size.
Definition: rtpudpv4transmitter.h:148
void ClearLocalIPList()
Clears the list of local IP addresses.
Definition: rtpudpv4transmitter.h:92
Parameters for the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:66
uint8_t GetMulticastTTL() const
Returns the multicast TTL which will be used (default is 1).
Definition: rtpudpv4transmitter.h:104
void SetRTPSendBuffer(int s)
Sets the RTP socket&#39;s send buffer size.
Definition: rtpudpv4transmitter.h:110
void SetCreatedAbortDescriptors(RTPAbortDescriptors *desc)
If non null, the specified abort descriptors will be used to cancel the function that&#39;s waiting for p...
Definition: rtpudpv4transmitter.h:139
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:75
uint32_t GetBindIP() const
Returns the IP address which will be used to bind the sockets.
Definition: rtpudpv4transmitter.h:95
SocketType GetRTPSocket() const
Returns the socket descriptor used for receiving and transmitting RTP packets.
Definition: rtpudpv4transmitter.h:220
uint16_t GetRTPPort() const
Returns the port number that the RTP socket receives packets on.
Definition: rtpudpv4transmitter.h:226
uint32_t GetMulticastInterfaceIP() const
Returns the multicast interface IP address.
Definition: rtpudpv4transmitter.h:98
uint16_t GetForcedRTCPPort() const
If non-zero, the specified port will be used to receive RTCP traffic.
Definition: rtpudpv4transmitter.h:160
An UDP over IPv4 transmission component.
Definition: rtpudpv4transmitter.h:257
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:80
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 SetRTCPReceiveBuffer(int s)
Sets the RTCP socket&#39;s receive buffer size.
Definition: rtpudpv4transmitter.h:119
Definition: rtpfaketransmitter.h:64
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&#39;ll need to close the sockets yourself when done, it will not be done automatically.
Definition: rtpudpv4transmitter.h:134
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
A memory manager.
Definition: rtpmemorymanager.h:150
void SetRTCPSendBuffer(int s)
Sets the RTCP socket&#39;s send buffer size.
Definition: rtpudpv4transmitter.h:116
void SetMulticastTTL(uint8_t mcastTTL)
Sets the multicast TTL to be used to mcastTTL.
Definition: rtpudpv4transmitter.h:83
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
int GetRTCPReceiveBuffer() const
Returns the RTCP socket&#39;s receive buffer size.
Definition: rtpudpv4transmitter.h:151
const std::list< uint32_t > & GetLocalIPList() const
Returns the list of local IP addresses.
Definition: rtpudpv4transmitter.h:107
bool GetAllowOddPortbase() const
If true, any RTP portbase will be allowed, not just even numbers.
Definition: rtpudpv4transmitter.h:157
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:248
Helper class for several RTPTransmitter instances, to be able to cancel a call to &#39;select&#39;...
Definition: rtpabortdescriptors.h:64
void SetBindIP(uint32_t ip)
Sets the IP address which is used to bind the sockets to ip.
Definition: rtpudpv4transmitter.h:72
bool GetRTCPMultiplexing() const
Returns a flag indicating if RTCP traffic will be multiplexed over the RTP channel.
Definition: rtpudpv4transmitter.h:154
uint16_t GetPortbase() const
Returns the RTP portbase which will be used (default is 5000).
Definition: rtpudpv4transmitter.h:101
void SetRTPReceiveBuffer(int s)
Sets the RTP socket&#39;s receive buffer size.
Definition: rtpudpv4transmitter.h:113