JRTPLIB  3.9.0
rtpudpv4transmitter.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2011 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 <list>
47 
48 #ifdef RTP_SUPPORT_THREAD
49  #include <jthread/jmutex.h>
50 #endif // RTP_SUPPORT_THREAD
51 
52 #define RTPUDPV4TRANS_HASHSIZE 8317
53 #define RTPUDPV4TRANS_DEFAULTPORTBASE 5000
54 
55 #define RTPUDPV4TRANS_RTPRECEIVEBUFFER 32768
56 #define RTPUDPV4TRANS_RTCPRECEIVEBUFFER 32768
57 #define RTPUDPV4TRANS_RTPTRANSMITBUFFER 32768
58 #define RTPUDPV4TRANS_RTCPTRANSMITBUFFER 32768
59 
60 namespace jrtplib
61 {
62 
65 {
66 public:
67  RTPUDPv4TransmissionParams():RTPTransmissionParams(RTPTransmitter::IPv4UDPProto) { portbase = RTPUDPV4TRANS_DEFAULTPORTBASE; bindIP = 0; multicastTTL = 1; mcastifaceIP = 0; rtpsendbuf = RTPUDPV4TRANS_RTPTRANSMITBUFFER; rtprecvbuf= RTPUDPV4TRANS_RTPRECEIVEBUFFER; rtcpsendbuf = RTPUDPV4TRANS_RTCPTRANSMITBUFFER; rtcprecvbuf = RTPUDPV4TRANS_RTCPRECEIVEBUFFER; }
68 
70  void SetBindIP(uint32_t ip) { bindIP = ip; }
71 
73  void SetMulticastInterfaceIP(uint32_t ip) { mcastifaceIP = ip; }
74 
76  void SetPortbase(uint16_t pbase) { portbase = pbase; }
77 
79  void SetMulticastTTL(uint8_t mcastTTL) { multicastTTL = mcastTTL; }
80 
82  void SetLocalIPList(std::list<uint32_t> &iplist) { localIPs = iplist; }
83 
88  void ClearLocalIPList() { localIPs.clear(); }
89 
91  uint32_t GetBindIP() const { return bindIP; }
92 
94  uint32_t GetMulticastInterfaceIP() const { return mcastifaceIP; }
95 
97  uint16_t GetPortbase() const { return portbase; }
98 
100  uint8_t GetMulticastTTL() const { return multicastTTL; }
101 
103  const std::list<uint32_t> &GetLocalIPList() const { return localIPs; }
104 
106  void SetRTPSendBuffer(int s) { rtpsendbuf = s; }
107 
109  void SetRTPReceiveBuffer(int s) { rtprecvbuf = s; }
110 
112  void SetRTCPSendBuffer(int s) { rtcpsendbuf = s; }
113 
115  void SetRTCPReceiveBuffer(int s) { rtcprecvbuf = s; }
116 
118  int GetRTPSendBuffer() const { return rtpsendbuf; }
119 
121  int GetRTPReceiveBuffer() const { return rtprecvbuf; }
122 
124  int GetRTCPSendBuffer() const { return rtcpsendbuf; }
125 
127  int GetRTCPReceiveBuffer() const { return rtcprecvbuf; }
128 private:
129  uint16_t portbase;
130  uint32_t bindIP, mcastifaceIP;
131  std::list<uint32_t> localIPs;
132  uint8_t multicastTTL;
133  int rtpsendbuf, rtprecvbuf;
134  int rtcpsendbuf, rtcprecvbuf;
135 };
136 
139 {
140 public:
141 #if ! (defined(WIN32) || defined(_WIN32_WCE))
142  RTPUDPv4TransmissionInfo(std::list<uint32_t> iplist,int rtpsock,int rtcpsock) : RTPTransmissionInfo(RTPTransmitter::IPv4UDPProto)
143 #else
144  RTPUDPv4TransmissionInfo(std::list<uint32_t> iplist,SOCKET rtpsock,SOCKET rtcpsock) : RTPTransmissionInfo(RTPTransmitter::IPv4UDPProto)
145 #endif // WIN32
146  { localIPlist = iplist; rtpsocket = rtpsock; rtcpsocket = rtcpsock; }
147 
148  ~RTPUDPv4TransmissionInfo() { }
149 
151  std::list<uint32_t> GetLocalIPList() const { return localIPlist; }
152 #if ! (defined(WIN32) || defined(_WIN32_WCE))
153 
154  int GetRTPSocket() const { return rtpsocket; }
155 
157  int GetRTCPSocket() const { return rtcpsocket; }
158 #else
159  SOCKET GetRTPSocket() const { return rtpsocket; }
160  SOCKET GetRTCPSocket() const { return rtcpsocket; }
161 #endif // WIN32
162 private:
163  std::list<uint32_t> localIPlist;
164 #if ! (defined(WIN32) || defined(_WIN32_WCE))
165  int rtpsocket,rtcpsocket;
166 #else
167  SOCKET rtpsocket,rtcpsocket;
168 #endif // WIN32
169 };
170 
171 class RTPUDPv4Trans_GetHashIndex_IPv4Dest
172 {
173 public:
174  static int GetIndex(const RTPIPv4Destination &d) { return d.GetIP()%RTPUDPV4TRANS_HASHSIZE; }
175 };
176 
177 class RTPUDPv4Trans_GetHashIndex_uint32_t
178 {
179 public:
180  static int GetIndex(const uint32_t &k) { return k%RTPUDPV4TRANS_HASHSIZE; }
181 };
182 
183 #define RTPUDPV4TRANS_HEADERSIZE (20+8)
184 
193 {
194 public:
197 
198  int Init(bool treadsafe);
199  int Create(size_t maxpacksize,const RTPTransmissionParams *transparams);
200  void Destroy();
201  RTPTransmissionInfo *GetTransmissionInfo();
202  void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
203 
204  int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
205  bool ComesFromThisTransmitter(const RTPAddress *addr);
206  size_t GetHeaderOverhead() { return RTPUDPV4TRANS_HEADERSIZE; }
207 
208  int Poll();
209  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
210  int AbortWait();
211 
212  int SendRTPData(const void *data,size_t len);
213  int SendRTCPData(const void *data,size_t len);
214 
215  int AddDestination(const RTPAddress &addr);
216  int DeleteDestination(const RTPAddress &addr);
217  void ClearDestinations();
218 
219  bool SupportsMulticasting();
220  int JoinMulticastGroup(const RTPAddress &addr);
221  int LeaveMulticastGroup(const RTPAddress &addr);
222  void LeaveAllMulticastGroups();
223 
224  int SetReceiveMode(RTPTransmitter::ReceiveMode m);
225  int AddToIgnoreList(const RTPAddress &addr);
226  int DeleteFromIgnoreList(const RTPAddress &addr);
227  void ClearIgnoreList();
228  int AddToAcceptList(const RTPAddress &addr);
229  int DeleteFromAcceptList(const RTPAddress &addr);
230  void ClearAcceptList();
231  int SetMaximumPacketSize(size_t s);
232 
233  bool NewDataAvailable();
234  RTPRawPacket *GetNextPacket();
235 #ifdef RTPDEBUG
236  void Dump();
237 #endif // RTPDEBUG
238 private:
239  int CreateLocalIPList();
240  bool GetLocalIPList_Interfaces();
241  void GetLocalIPList_DNS();
242  void AddLoopbackAddress();
243  void FlushPackets();
244  int PollSocket(bool rtp);
245  int ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port);
246  int ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port);
247 #ifdef RTP_SUPPORT_IPV4MULTICAST
248  bool SetMulticastTTL(uint8_t ttl);
249 #endif // RTP_SUPPORT_IPV4MULTICAST
250  bool ShouldAcceptData(uint32_t srcip,uint16_t srcport);
251  void ClearAcceptIgnoreInfo();
252 
253  bool init;
254  bool created;
255  bool waitingfordata;
256 #if (defined(WIN32) || defined(_WIN32_WCE))
257  SOCKET rtpsock,rtcpsock;
258 #else // not using winsock
259  int rtpsock,rtcpsock;
260 #endif // WIN32
261  uint32_t bindIP, mcastifaceIP;
262  std::list<uint32_t> localIPs;
263  uint16_t portbase;
264  uint8_t multicastTTL;
265  RTPTransmitter::ReceiveMode receivemode;
266 
267  uint8_t *localhostname;
268  size_t localhostnamelength;
269 
270  RTPHashTable<const RTPIPv4Destination,RTPUDPv4Trans_GetHashIndex_IPv4Dest,RTPUDPV4TRANS_HASHSIZE> destinations;
271 #ifdef RTP_SUPPORT_IPV4MULTICAST
272  RTPHashTable<const uint32_t,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> multicastgroups;
273 #endif // RTP_SUPPORT_IPV4MULTICAST
274  std::list<RTPRawPacket*> rawpacketlist;
275 
276  bool supportsmulticasting;
277  size_t maxpacksize;
278 
279  class PortInfo
280  {
281  public:
282  PortInfo() { all = false; }
283 
284  bool all;
285  std::list<uint16_t> portlist;
286  };
287 
288  RTPKeyHashTable<const uint32_t,PortInfo*,RTPUDPv4Trans_GetHashIndex_uint32_t,RTPUDPV4TRANS_HASHSIZE> acceptignoreinfo;
289 
290  // notification descriptors for AbortWait (0 is for reading, 1 for writing)
291 #if (defined(WIN32) || defined(_WIN32_WCE))
292  SOCKET abortdesc[2];
293 #else
294  int abortdesc[2];
295 #endif // WIN32
296  int CreateAbortDescriptors();
297  void DestroyAbortDescriptors();
298  void AbortWaitInternal();
299 #ifdef RTP_SUPPORT_THREAD
300  jthread::JMutex mainmutex,waitmutex;
301  int threadsafe;
302 #endif // RTP_SUPPORT_THREAD
303 };
304 
305 } // end namespace
306 
307 #endif // RTPUDPV4TRANSMITTER_H
308 
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:82
int GetRTPSendBuffer() const
Returns the RTP socket's send buffer size.
Definition: rtpudpv4transmitter.h:118
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:71
Base class for transmission parameters.
Definition: rtptransmitter.h:227
int GetRTPReceiveBuffer() const
Returns the RTP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:121
Additional information about the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:138
std::list< uint32_t > GetLocalIPList() const
Returns the list of IPv4 addresses the transmitter considers to be the local IP addresses.
Definition: rtpudpv4transmitter.h:151
int GetRTCPSendBuffer() const
Returns the RTCP socket's send buffer size.
Definition: rtpudpv4transmitter.h:124
void ClearLocalIPList()
Clears the list of local IP addresses.
Definition: rtpudpv4transmitter.h:88
Parameters for the UDP over IPv4 transmitter.
Definition: rtpudpv4transmitter.h:64
uint8_t GetMulticastTTL() const
Returns the multicast TTL which will be used (default is 1).
Definition: rtpudpv4transmitter.h:100
void SetRTPSendBuffer(int s)
Sets the RTP socket's send buffer size.
Definition: rtpudpv4transmitter.h:106
int GetRTPSocket() const
Returns the socket descriptor used for receiving and transmitting RTP packets.
Definition: rtpudpv4transmitter.h:154
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:73
uint32_t GetBindIP() const
Returns the IP address which will be used to bind the sockets.
Definition: rtpudpv4transmitter.h:91
uint32_t GetMulticastInterfaceIP() const
Returns the multicast interface IP address.
Definition: rtpudpv4transmitter.h:94
An UDP over IPv4 transmission component.
Definition: rtpudpv4transmitter.h:192
int GetRTCPSocket() const
Returns the socket descriptor used for receiving and transmitting RTCP packets.
Definition: rtpudpv4transmitter.h:157
Abstract class from which actual transmission components should be derived.
Definition: rtptransmitter.h:61
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:78
void SetRTCPReceiveBuffer(int s)
Sets the RTCP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:115
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:80
A memory manager.
Definition: rtpmemorymanager.h:147
void SetRTCPSendBuffer(int s)
Sets the RTCP socket's send buffer size.
Definition: rtpudpv4transmitter.h:112
void SetMulticastTTL(uint8_t mcastTTL)
Sets the multicast TTL to be used to mcastTTL.
Definition: rtpudpv4transmitter.h:79
void SetPortbase(uint16_t pbase)
Sets the RTP portbase to pbase.
Definition: rtpudpv4transmitter.h:76
int GetRTCPReceiveBuffer() const
Returns the RTCP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:127
const std::list< uint32_t > & GetLocalIPList() const
Returns the list of local IP addresses.
Definition: rtpudpv4transmitter.h:103
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:246
void SetBindIP(uint32_t ip)
Sets the IP address which is used to bind the sockets to ip.
Definition: rtpudpv4transmitter.h:70
uint16_t GetPortbase() const
Returns the RTP portbase which will be used (default is 5000).
Definition: rtpudpv4transmitter.h:97
void SetRTPReceiveBuffer(int s)
Sets the RTP socket's receive buffer size.
Definition: rtpudpv4transmitter.h:109