jrtplib  3.6.0
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 <jmutex.h>
59 #endif // RTP_SUPPORT_THREAD
60 
61 #define RTPFAKETRANS_HASHSIZE 8317
62 #define RTPFAKETRANS_DEFAULTPORTBASE 5000
63 
64 // Definition of a callback that is called when a packet is ready for sending
65 // params (*data, data_len, dest_addr, dest_port, rtp [1 if rtp, 0 if rtcp])
66 typedef void(*packet_ready_cb)(void*, uint8_t*, uint16_t, uint32_t, uint16_t, int rtp);
67 
68 class RTPFakeTransmissionParams : public RTPTransmissionParams
69 {
70 public:
71  RTPFakeTransmissionParams():RTPTransmissionParams(RTPTransmitter::UserDefinedProto) { portbase = RTPFAKETRANS_DEFAULTPORTBASE; bindIP = 0; multicastTTL = 1; currentdata = NULL;}
72  void SetBindIP(uint32_t ip) { bindIP = ip; }
73  void SetPortbase(uint16_t pbase) { portbase = pbase; }
74  void SetMulticastTTL(uint8_t mcastTTL) { multicastTTL = mcastTTL; }
75  void SetLocalIPList(std::list<uint32_t> &iplist) { localIPs = iplist; }
76  void ClearLocalIPList() { localIPs.clear(); }
77  void SetCurrentData(uint8_t *data) { currentdata = data; }
78  void SetCurrentDataLen(uint16_t len) { currentdatalen = len; }
79  void SetCurrentDataAddr(uint32_t addr) { currentdataaddr = addr; }
80  void SetCurrentDataPort(uint16_t port) { currentdataport = port; }
81  void SetCurrentDataType(bool type) { currentdatatype = type; }
82  void SetPacketReadyCB(packet_ready_cb cb) { packetreadycb = cb; };
83  void SetPacketReadyCBData(void *data) { packetreadycbdata = data; };
84  uint32_t GetBindIP() const { return bindIP; }
85  uint16_t GetPortbase() const { return portbase; }
86  uint8_t GetMulticastTTL() const { return multicastTTL; }
87  const std::list<uint32_t> &GetLocalIPList() const { return localIPs; }
88  uint8_t* GetCurrentData() const { return currentdata; }
89  uint16_t GetCurrentDataLen() const { return currentdatalen; }
90  uint32_t GetCurrentDataAddr() const { return currentdataaddr; }
91  uint16_t GetCurrentDataPort() const { return currentdataport; }
92  bool GetCurrentDataType() const { return currentdatatype; }
93  packet_ready_cb GetPacketReadyCB() const { return packetreadycb; }
94  void* GetPacketReadyCBData() const { return packetreadycbdata; }
95 private:
96  uint16_t portbase;
97  uint32_t bindIP;
98  std::list<uint32_t> localIPs;
99  uint8_t multicastTTL;
100  uint8_t* currentdata;
101  uint16_t currentdatalen;
102  uint32_t currentdataaddr;
103  uint16_t currentdataport;
104  bool currentdatatype;
105  packet_ready_cb packetreadycb;
106  void *packetreadycbdata;
107 };
108 
109 class RTPFakeTransmissionInfo : public RTPTransmissionInfo
110 {
111 public:
112  RTPFakeTransmissionInfo(std::list<uint32_t> iplist,
113  RTPFakeTransmissionParams *transparams) :
114  RTPTransmissionInfo(RTPTransmitter::UserDefinedProto)
115  { localIPlist = iplist; params = transparams; }
116 
117  ~RTPFakeTransmissionInfo() { }
118  std::list<uint32_t> GetLocalIPList() const { return localIPlist; }
119  RTPFakeTransmissionParams* GetTransParams() { return params; }
120 private:
121  std::list<uint32_t> localIPlist;
122  RTPFakeTransmissionParams *params;
123 };
124 
125 class RTPFakeTrans_GetHashIndex_IPv4Dest
126 {
127 public:
128  static int GetIndex(const RTPIPv4Destination &d) { return d.GetIP()%RTPFAKETRANS_HASHSIZE; }
129 };
130 
131 class RTPFakeTrans_GetHashIndex_uint32_t
132 {
133 public:
134  static int GetIndex(const uint32_t &k) { return k%RTPFAKETRANS_HASHSIZE; }
135 };
136 
137 #define RTPFAKETRANS_HEADERSIZE (20+8)
138 
139 class RTPFakeTransmitter : public RTPTransmitter
140 {
141 public:
142  RTPFakeTransmitter(RTPMemoryManager *mgr);
143  ~RTPFakeTransmitter();
144 
145  int Init(bool treadsafe);
146  int Create(size_t maxpacksize,const RTPTransmissionParams *transparams);
147  void Destroy();
149 
150  int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
151  bool ComesFromThisTransmitter(const RTPAddress *addr);
152  size_t GetHeaderOverhead() { return RTPFAKETRANS_HEADERSIZE; }
153 
154  int Poll();
155  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
156  int AbortWait();
157 
158  int SendRTPData(const void *data,size_t len);
159  int SendRTCPData(const void *data,size_t len);
160 
161  int AddDestination(const RTPAddress &addr);
162  int DeleteDestination(const RTPAddress &addr);
163  void ClearDestinations();
164 
165  bool SupportsMulticasting();
166  int JoinMulticastGroup(const RTPAddress &addr);
167  int LeaveMulticastGroup(const RTPAddress &addr);
169 
171  int AddToIgnoreList(const RTPAddress &addr);
172  int DeleteFromIgnoreList(const RTPAddress &addr);
173  void ClearIgnoreList();
174  int AddToAcceptList(const RTPAddress &addr);
175  int DeleteFromAcceptList(const RTPAddress &addr);
176  void ClearAcceptList();
177  int SetMaximumPacketSize(size_t s);
178 
179  bool NewDataAvailable();
181 #ifdef RTPDEBUG
182  void Dump();
183 #endif // RTPDEBUG
184 private:
185  int CreateLocalIPList();
186  bool GetLocalIPList_Interfaces();
187  void GetLocalIPList_DNS();
188  void AddLoopbackAddress();
189  void FlushPackets();
190  int FakePoll();
191  int ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port);
192  int ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port);
193 #ifdef RTP_SUPPORT_IPV4MULTICAST
194  bool SetMulticastTTL(uint8_t ttl);
195 #endif // RTP_SUPPORT_IPV4MULTICAST
196  bool ShouldAcceptData(uint32_t srcip,uint16_t srcport);
197  void ClearAcceptIgnoreInfo();
198 
199  RTPFakeTransmissionParams *params;
200  bool init;
201  bool created;
202  bool waitingfordata;
203  std::list<uint32_t> localIPs;
204  uint16_t portbase;
205  uint8_t multicastTTL;
206  RTPTransmitter::ReceiveMode receivemode;
207 
208  uint8_t *localhostname;
209  size_t localhostnamelength;
210 
211  RTPHashTable<const RTPIPv4Destination,RTPFakeTrans_GetHashIndex_IPv4Dest,RTPFAKETRANS_HASHSIZE> destinations;
212 #ifdef RTP_SUPPORT_IPV4MULTICAST
213 // RTPHashTable<const uint32_t,RTPFakeTrans_GetHashIndex_uint32_t,RTPFAKETRANS_HASHSIZE> multicastgroups;
214 #endif // RTP_SUPPORT_IPV4MULTICAST
215  std::list<RTPRawPacket*> rawpacketlist;
216 
217  bool supportsmulticasting;
218  size_t maxpacksize;
219 
220  class PortInfo
221  {
222  public:
223  PortInfo() { all = false; }
224 
225  bool all;
226  std::list<uint16_t> portlist;
227  };
228 
229  RTPKeyHashTable<const uint32_t,PortInfo*,RTPFakeTrans_GetHashIndex_uint32_t,RTPFAKETRANS_HASHSIZE> acceptignoreinfo;
230 
231  int CreateAbortDescriptors();
232  void DestroyAbortDescriptors();
233  void AbortWaitInternal();
234 #ifdef RTP_SUPPORT_THREAD
235  JMutex mainmutex,waitmutex;
236  int threadsafe;
237 #endif // RTP_SUPPORT_THREAD
238 };
239 
240 #endif // RTPFakeTRANSMITTER_H
241 
virtual int WaitForIncomingData(const RTPTime &delay, bool *dataavailable=0)=0
Waits until incoming data is detected.
virtual int DeleteDestination(const RTPAddress &addr)=0
Deletes the address specified by addr from the list of destinations.
virtual int AddToAcceptList(const RTPAddress &addr)=0
Adds addr to the list of addresses to accept.
virtual int LeaveMulticastGroup(const RTPAddress &addr)=0
Leaves the multicast group specified by addr.
virtual RTPTransmissionInfo * GetTransmissionInfo()=0
Returns additional information about the transmitter.
A memory manager.
Definition: rtpmemorymanager.h:144
virtual int SendRTPData(const void *data, size_t len)=0
Send a packet with length len containing data to all RTP addresses of the current destination list...
virtual bool SupportsMulticasting()=0
Returns true if the transmission component supports multicasting.
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:77
virtual int Init(bool threadsafe)=0
This function must be called before the transmission component can be used.
virtual int GetLocalHostName(uint8_t *buffer, size_t *bufferlength)=0
Looks up the local host name.
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:236
virtual void ClearAcceptList()=0
Clears the list of addresses to accept.
virtual void Destroy()=0
By calling this function, buffers are cleared and the component cannot be used anymore.
virtual int Poll()=0
Checks for incoming data and stores it.
This class is used by the transmission component to store the incoming RTP and RTCP data in...
Definition: rtprawpacket.h:48
virtual int AbortWait()=0
If the previous function has been called, this one aborts the waiting.
virtual int AddDestination(const RTPAddress &addr)=0
Adds the address specified by addr to the list of destinations.
virtual void ClearDestinations()=0
Clears the list of destinations.
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:73
virtual size_t GetHeaderOverhead()=0
Returns the amount of bytes that will be added to the RTP packet by the underlying layers (excluding ...
Abstract class from which actual transmission components should be derived.
Definition: rtptransmitter.h:57
virtual RTPRawPacket * GetNextPacket()=0
Returns the raw data of a received RTP packet (received during the Poll function) in an RTPRawPacket ...
This class is an abstract class which is used to specify destinations, multicast groups etc...
Definition: rtpaddress.h:47
virtual int DeleteFromAcceptList(const RTPAddress &addr)=0
Deletes addr from the list of addresses to accept.
virtual void ClearIgnoreList()=0
Clears the list of addresses to ignore.
virtual int JoinMulticastGroup(const RTPAddress &addr)=0
Joins the multicast group specified by addr.
virtual int DeleteFromIgnoreList(const RTPAddress &addr)=0
Deletes addr from the list of addresses to accept.
virtual int Create(size_t maxpacksize, const RTPTransmissionParams *transparams)=0
Prepares the component to be used.
virtual bool NewDataAvailable()=0
Returns true if packets can be obtained using the GetNextPacket member function.
virtual int SetMaximumPacketSize(size_t s)=0
Sets the maximum packet size which the transmitter should allow to s.
virtual void LeaveAllMulticastGroups()=0
Leaves all the multicast groups that have been joined.
virtual int SendRTCPData(const void *data, size_t len)=0
Send a packet with length len containing data to all RTCP addresses of the current destination list...
virtual bool ComesFromThisTransmitter(const RTPAddress *addr)=0
Returns true if the address specified by addr is one of the addresses of the transmitter.
Base class for transmission parameters.
Definition: rtptransmitter.h:217
virtual int AddToIgnoreList(const RTPAddress &addr)=0
Adds addr to the list of addresses to ignore.
virtual int SetReceiveMode(RTPTransmitter::ReceiveMode m)=0
Sets the receive mode.