JRTPLIB  3.11.1
rtpfaketransmitter.h
1 /*
2 
3  This class allows for jrtp to process packets without sending them out
4  anywhere.
5  The incoming messages are handed in to jrtp through the TransmissionParams
6  and can be retreived from jrtp through the normal polling mecanisms.
7  The outgoing RTP/RTCP packets are given to jrtp through the normal
8  session->SendPacket() and those packets are handed back out to the
9  client through a callback function (packet_ready_cb).
10 
11  example usage : Allows for integration of RTP into gstreamer.
12 
13  Copyright (c) 2005 Philippe Khalaf <burger@speedy.org>
14 
15  This file is a part of JRTPLIB
16  Copyright (c) 1999-2004 Jori Liesenborgs
17 
18  Contact: jori.liesenborgs@gmail.com
19 
20  This library was developed at the "Expertisecentrum Digitale Media"
21  (http://www.edm.luc.ac.be), a research center of the "Limburgs Universitair
22  Centrum" (http://www.luc.ac.be). The library is based upon work done for
23  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
24 
25  Permission is hereby granted, free of charge, to any person obtaining a
26  copy of this software and associated documentation files (the "Software"),
27  to deal in the Software without restriction, including without limitation
28  the rights to use, copy, modify, merge, publish, distribute, sublicense,
29  and/or sell copies of the Software, and to permit persons to whom the
30  Software is furnished to do so, subject to the following conditions:
31 
32  The above copyright notice and this permission notice shall be included
33  in all copies or substantial portions of the Software.
34 
35  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
36  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
38  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
40  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
41  IN THE SOFTWARE.
42 
43 */
44 
45 #ifndef RTPFAKETRANSMITTER_H
46 
47 #define RTPFAKETRANSMITTER_H
48 
49 #include "rtpconfig.h"
50 
51 #include "rtptransmitter.h"
52 #include "rtpipv4destination.h"
53 #include "rtphashtable.h"
54 #include "rtpkeyhashtable.h"
55 #include <list>
56 
57 #ifdef RTP_SUPPORT_THREAD
58  #include <jthread/jmutex.h>
59 #endif // RTP_SUPPORT_THREAD
60 
61 #define RTPFAKETRANS_HASHSIZE 8317
62 #define RTPFAKETRANS_DEFAULTPORTBASE 5000
63 
64 namespace jrtplib
65 {
66 
67 // Definition of a callback that is called when a packet is ready for sending
68 // params (*data, data_len, dest_addr, dest_port, rtp [1 if rtp, 0 if rtcp])
69 typedef void(*packet_ready_cb)(void*, uint8_t*, uint16_t, uint32_t, uint16_t, int rtp);
70 
71 class RTPFakeTransmissionParams : public RTPTransmissionParams
72 {
73 public:
74  RTPFakeTransmissionParams():RTPTransmissionParams(RTPTransmitter::UserDefinedProto) { portbase = RTPFAKETRANS_DEFAULTPORTBASE; bindIP = 0; multicastTTL = 1; currentdata = NULL;}
75  void SetBindIP(uint32_t ip) { bindIP = ip; }
76  void SetPortbase(uint16_t pbase) { portbase = pbase; }
77  void SetMulticastTTL(uint8_t mcastTTL) { multicastTTL = mcastTTL; }
78  void SetLocalIPList(std::list<uint32_t> &iplist) { localIPs = iplist; }
79  void ClearLocalIPList() { localIPs.clear(); }
80  void SetCurrentData(uint8_t *data) { currentdata = data; }
81  void SetCurrentDataLen(uint16_t len) { currentdatalen = len; }
82  void SetCurrentDataAddr(uint32_t addr) { currentdataaddr = addr; }
83  void SetCurrentDataPort(uint16_t port) { currentdataport = port; }
84  void SetCurrentDataType(bool type) { currentdatatype = type; }
85  void SetPacketReadyCB(packet_ready_cb cb) { packetreadycb = cb; };
86  void SetPacketReadyCBData(void *data) { packetreadycbdata = data; };
87  uint32_t GetBindIP() const { return bindIP; }
88  uint16_t GetPortbase() const { return portbase; }
89  uint8_t GetMulticastTTL() const { return multicastTTL; }
90  const std::list<uint32_t> &GetLocalIPList() const { return localIPs; }
91  uint8_t* GetCurrentData() const { return currentdata; }
92  uint16_t GetCurrentDataLen() const { return currentdatalen; }
93  uint32_t GetCurrentDataAddr() const { return currentdataaddr; }
94  uint16_t GetCurrentDataPort() const { return currentdataport; }
95  bool GetCurrentDataType() const { return currentdatatype; }
96  packet_ready_cb GetPacketReadyCB() const { return packetreadycb; }
97  void* GetPacketReadyCBData() const { return packetreadycbdata; }
98 private:
99  uint16_t portbase;
100  uint32_t bindIP;
101  std::list<uint32_t> localIPs;
102  uint8_t multicastTTL;
103  uint8_t* currentdata;
104  uint16_t currentdatalen;
105  uint32_t currentdataaddr;
106  uint16_t currentdataport;
107  bool currentdatatype;
108  packet_ready_cb packetreadycb;
109  void *packetreadycbdata;
110 };
111 
112 class RTPFakeTransmissionInfo : public RTPTransmissionInfo
113 {
114 public:
115  RTPFakeTransmissionInfo(std::list<uint32_t> iplist,
116  RTPFakeTransmissionParams *transparams) :
117  RTPTransmissionInfo(RTPTransmitter::UserDefinedProto)
118  { localIPlist = iplist; params = transparams; }
119 
120  ~RTPFakeTransmissionInfo() { }
121  std::list<uint32_t> GetLocalIPList() const { return localIPlist; }
122  RTPFakeTransmissionParams* GetTransParams() { return params; }
123 private:
124  std::list<uint32_t> localIPlist;
125  RTPFakeTransmissionParams *params;
126 };
127 
128 class RTPFakeTrans_GetHashIndex_IPv4Dest
129 {
130 public:
131  static int GetIndex(const RTPIPv4Destination &d) { return d.GetIP()%RTPFAKETRANS_HASHSIZE; }
132 };
133 
134 class RTPFakeTrans_GetHashIndex_uint32_t
135 {
136 public:
137  static int GetIndex(const uint32_t &k) { return k%RTPFAKETRANS_HASHSIZE; }
138 };
139 
140 #define RTPFAKETRANS_HEADERSIZE (20+8)
141 
142 class RTPFakeTransmitter : public RTPTransmitter
143 {
144 public:
145  RTPFakeTransmitter(RTPMemoryManager *mgr);
146  ~RTPFakeTransmitter();
147 
148  int Init(bool treadsafe);
149  int Create(size_t maxpacksize,const RTPTransmissionParams *transparams);
150  void Destroy();
151  RTPTransmissionInfo *GetTransmissionInfo();
152  void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
153 
154  int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
155  bool ComesFromThisTransmitter(const RTPAddress *addr);
156  size_t GetHeaderOverhead() { return RTPFAKETRANS_HEADERSIZE; }
157 
158  int Poll();
159  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
160  int AbortWait();
161 
162  int SendRTPData(const void *data,size_t len);
163  int SendRTCPData(const void *data,size_t len);
164 
165  int AddDestination(const RTPAddress &addr);
166  int DeleteDestination(const RTPAddress &addr);
167  void ClearDestinations();
168 
169  bool SupportsMulticasting();
170  int JoinMulticastGroup(const RTPAddress &addr);
171  int LeaveMulticastGroup(const RTPAddress &addr);
172  void LeaveAllMulticastGroups();
173 
174  int SetReceiveMode(RTPTransmitter::ReceiveMode m);
175  int AddToIgnoreList(const RTPAddress &addr);
176  int DeleteFromIgnoreList(const RTPAddress &addr);
177  void ClearIgnoreList();
178  int AddToAcceptList(const RTPAddress &addr);
179  int DeleteFromAcceptList(const RTPAddress &addr);
180  void ClearAcceptList();
181  int SetMaximumPacketSize(size_t s);
182 
183  bool NewDataAvailable();
184  RTPRawPacket *GetNextPacket();
185 #ifdef RTPDEBUG
186  void Dump();
187 #endif // RTPDEBUG
188 private:
189  int CreateLocalIPList();
190  bool GetLocalIPList_Interfaces();
191  void GetLocalIPList_DNS();
192  void AddLoopbackAddress();
193  void FlushPackets();
194  int FakePoll();
195  int ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port);
196  int ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port);
197 #ifdef RTP_SUPPORT_IPV4MULTICAST
198  bool SetMulticastTTL(uint8_t ttl);
199 #endif // RTP_SUPPORT_IPV4MULTICAST
200  bool ShouldAcceptData(uint32_t srcip,uint16_t srcport);
201  void ClearAcceptIgnoreInfo();
202 
203  RTPFakeTransmissionParams *params;
204  bool init;
205  bool created;
206  bool waitingfordata;
207  std::list<uint32_t> localIPs;
208  uint16_t portbase;
209  uint8_t multicastTTL;
210  RTPTransmitter::ReceiveMode receivemode;
211 
212  uint8_t *localhostname;
213  size_t localhostnamelength;
214 
215  RTPHashTable<const RTPIPv4Destination,RTPFakeTrans_GetHashIndex_IPv4Dest,RTPFAKETRANS_HASHSIZE> destinations;
216 #ifdef RTP_SUPPORT_IPV4MULTICAST
217 // RTPHashTable<const uint32_t,RTPFakeTrans_GetHashIndex_uint32_t,RTPFAKETRANS_HASHSIZE> multicastgroups;
218 #endif // RTP_SUPPORT_IPV4MULTICAST
219  std::list<RTPRawPacket*> rawpacketlist;
220 
221  bool supportsmulticasting;
222  size_t maxpacksize;
223 
224  class PortInfo
225  {
226  public:
227  PortInfo() { all = false; }
228 
229  bool all;
230  std::list<uint16_t> portlist;
231  };
232 
233  RTPKeyHashTable<const uint32_t,PortInfo*,RTPFakeTrans_GetHashIndex_uint32_t,RTPFAKETRANS_HASHSIZE> acceptignoreinfo;
234 
235  int CreateAbortDescriptors();
236  void DestroyAbortDescriptors();
237  void AbortWaitInternal();
238 #ifdef RTP_SUPPORT_THREAD
239  jthread::JMutex mainmutex,waitmutex;
240  int threadsafe;
241 #endif // RTP_SUPPORT_THREAD
242 };
243 
244 } // end namespace
245 
246 #endif // RTPFAKETRANSMITTER_H
247 
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:80
Definition: rtpfaketransmitter.h:64