ccRTP
channel.h
Go to the documentation of this file.
1// Copyright (C) 2001-2015 Federico Montesino Pouzols <fedemp@altern.org>
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU Lesser General Public License
14// along with GNU ccRTP. If not, see <http://www.gnu.org/licenses/>.
15//
16// As a special exception, you may use this file as part of a free software
17// library without restriction. Specifically, if other files instantiate
18// templates or use macros or inline functions from this file, or you compile
19// this file and link it with other files to produce an executable, this
20// file does not by itself cause the resulting executable to be covered by
21// the GNU General Public License. This exception does not however
22// invalidate any other reasons why the executable file might be covered by
23// the GNU General Public License.
24//
25// This exception applies only to the code released under the name GNU
26// ccRTP. If you copy code from other releases into a copy of GNU
27// ccRTP, as the General Public License permits, the exception does
28// not apply to the code that you add in this way. To avoid misleading
29// anyone as to the status of such modified files, you must delete
30// this exception notice from them.
31//
32// If you write modifications of your own for GNU ccRTP, it is your choice
33// whether to permit this exception to apply to your modifications.
34// If you do not wish that, delete this exception notice.
35//
36
37#ifndef CCRTP_CHANNEL_H_
38#define CCRTP_CHANNEL_H_
39
40#include <ccrtp/base.h>
41#include <commoncpp/socket.h>
42
43#ifndef _MSWINDOWS_
44#include <sys/ioctl.h>
45inline size_t ccioctl(int so, int request, size_t& len)
46 { return ioctl(so,request,&len); }
47#else
48inline size_t ccioctl(SOCKET so, int request, size_t& len )
49{
50 unsigned long l;
51 size_t result = 0;
52 ::ioctlsocket(so,request,&l);
53 len = l;
54 return result;
55}
56#endif
57
58NAMESPACE_COMMONCPP
59
94class RTPBaseUDPIPv4Socket : private UDPSocket
95{
96public:
100 RTPBaseUDPIPv4Socket(const InetAddress& ia, tpport_t port) :
101 UDPSocket(ia,port)
102 { }
103
105 { endSocket(); }
106
107 inline bool
109 { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
110
111 inline InetHostAddress
112 getSender(tpport_t& port) const
113 { return UDPSocket::getSender(&port); }
114
115 inline size_t
116 recv(unsigned char* buffer, size_t len)
117 { return UDPSocket::receive(buffer, len); }
118
122 inline size_t
124 { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
125
126 Socket::Error
127 setMulticast(bool enable)
128 { return UDPSocket::setMulticast(enable); }
129
130 inline Socket::Error
131 join(const InetMcastAddress& ia, uint32 iface)
132 { return UDPSocket::join(ia,iface); }
133
134 inline Socket::Error
135 drop(const InetMcastAddress& ia)
136 { return UDPSocket::drop(ia); }
137
138 inline Socket::Error
139 setTimeToLive(unsigned char ttl)
140 { return UDPSocket::setTimeToLive(ttl); }
141
146 UDPSocket()
147 { }
148
149 inline void
150 setPeer(const InetAddress &ia, tpport_t port)
151 {UDPSocket::setPeer((InetHostAddress&)ia, port);}
152
153 inline size_t
154 send(const unsigned char* const buffer, size_t len)
155 { return UDPSocket::send(buffer, len); }
156
157 inline SOCKET getRecvSocket() const
158 { return UDPSocket::so; }
159
160 // common
161 inline void
163 { UDPSocket::endSocket(); }
164};
165
186template<class BaseSocket>
188{
189public:
190 DualRTPChannel(const InetAddress& ia, tpport_t port)
191 {
192 recvSocket = new BaseSocket(ia,port);
193 sendSocket = new BaseSocket;
194 }
195
197 { delete sendSocket; delete recvSocket; }
198
199 inline bool
201 { return recvSocket->isPendingRecv(timeout); }
202
203 inline InetHostAddress
204 getSender(tpport_t& port) const
205 { return recvSocket->getSender(port); }
206
207 inline size_t
208 recv(unsigned char* buffer, size_t len)
209 { return recvSocket->recv(buffer, len); }
210
211 inline size_t
213 { return recvSocket->getNextPacketSize(); }
214
215 inline Socket::Error
216 setMulticast(bool enable)
217 { Socket::Error error = recvSocket->setMulticast(enable);
218 if (error) return error;
219 return sendSocket->setMulticast(enable); }
220
221 inline Socket::Error
222 join(const InetMcastAddress& ia, uint32 iface)
223 { return recvSocket->join(ia,iface); }
224
225 inline Socket::Error
226 drop(const InetMcastAddress& ia)
227 { return recvSocket->drop(ia); }
228
229 inline Socket::Error
230 setTimeToLive(unsigned char ttl)
231 { return sendSocket->setTimeToLive(ttl); }
232
233 inline void
234 setPeer(const InetAddress& host, tpport_t port)
235 { sendSocket->setPeer(host,port); }
236
237 inline size_t
238 send(const unsigned char* const buffer, size_t len)
239 { return sendSocket->send(buffer, len); }
240
241 inline SOCKET getRecvSocket() const
242 { return recvSocket->getRecvSocket(); }
243
244 // common.
245 inline void
247 { sendSocket->endSocket(); recvSocket->endSocket(); }
248
249private:
250 BaseSocket* sendSocket;
251 BaseSocket* recvSocket;
252};
253
254#ifdef CCXX_IPV6
255
277class RTPBaseUDPIPv6Socket : private UDPSocket
278{
279public:
283 RTPBaseUDPIPv6Socket(const IPV6Address& ia, tpport_t port) :
284 UDPSocket(ia,port)
285 { }
286
287 inline ~RTPBaseUDPIPv6Socket()
288 { endSocket(); }
289
290 inline bool
291 isPendingRecv(microtimeout_t timeout)
292 { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
293
294 inline IPV6Host
295 getSender(tpport_t& port) const
296 { return UDPSocket::getIPV6Sender(&port); }
297
298 inline size_t
299 recv(unsigned char* buffer, size_t len)
300 { return UDPSocket::receive(buffer, len); }
301
305 inline size_t
306 getNextPacketSize() const
307 { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
308
309 Socket::Error
310 setMulticast(bool enable)
311 { return UDPSocket::setMulticast(enable); }
312
313 inline Socket::Error
314 join(const IPV6Multicast& ia, uint32 iface)
315 { return Socket::join(ia); }
316
317 inline Socket::Error
318 drop(const IPV6Multicast& ia)
319 { return UDPSocket::drop(ia); }
320
321 inline Socket::Error
322 setTimeToLive(unsigned char ttl)
323 { return UDPSocket::setTimeToLive(ttl); }
324
328 RTPBaseUDPIPv6Socket() :
329 UDPSocket()
330 { }
331
332 inline void
333 setPeer(const IPV6Host &ia, tpport_t port)
334 {UDPSocket::setPeer(ia, port);}
335
336 inline size_t
337 send(const unsigned char* const buffer, size_t len)
338 { return UDPSocket::send(buffer, len); }
339
340 inline SOCKET getRecvSocket() const
341 { return UDPSocket::so; }
342
343 // common
344 inline void
345 endSocket()
346 { UDPSocket::endSocket(); }
347};
348
369template<class BaseSocket>
370class DualRTPChannelIPV6
371{
372public:
373 DualRTPChannelIPV6(const IPV6Host& ia, tpport_t port)
374 {
375 recvSocket = new BaseSocket(ia,port);
376 sendSocket = new BaseSocket;
377 }
378
379 inline ~DualRTPChannelIPV6()
380 { delete sendSocket; delete recvSocket; }
381
382 inline bool
383 isPendingRecv(microtimeout_t timeout) const
384 { return recvSocket->isPendingRecv(timeout); }
385
386 inline IPV6Host
387 getSender(tpport_t& port) const
388 { return recvSocket->getIPV6Sender(port); }
389
390 inline size_t
391 recv(unsigned char* buffer, size_t len)
392 { return recvSocket->recv(buffer, len); }
393
394 inline size_t
395 getNextPacketSize() const
396 { return recvSocket->getNextPacketSize(); }
397
398 inline Socket::Error
399 setMulticast(bool enable)
400 { Socket::Error error = recvSocket->setMulticast(enable);
401 if (error) return error;
402 return sendSocket->setMulticast(enable); }
403
404 inline Socket::Error
405 join(const IPV6Multicast& ia, uint32 iface)
406 { return recvSocket->join(ia,iface); }
407
408 inline Socket::Error
409 drop(const IPV6Multicast& ia)
410 { return recvSocket->drop(ia); }
411
412 inline Socket::Error
413 setTimeToLive(unsigned char ttl)
414 { return sendSocket->setTimeToLive(ttl); }
415
416 inline void
417 setPeer(const IPV6Host& host, tpport_t port)
418 { sendSocket->setPeer(host,port); }
419
420 inline size_t
421 send(const unsigned char* const buffer, size_t len)
422 { return sendSocket->send(buffer, len); }
423
424 inline SOCKET getRecvSocket() const
425 { return recvSocket->getRecvSocket(); }
426
427 // common.
428 inline void
429 endSocket()
430 { sendSocket->endSocket(); recvSocket->endSocket(); }
431
432private:
433 BaseSocket* sendSocket;
434 BaseSocket* recvSocket;
435};
436
437
438typedef DualRTPChannelIPV6<RTPBaseUDPIPv6Socket> DualRTPUDPIPv6Channel;
439typedef RTPBaseUDPIPv6Socket SingleRTPChannelIPV6;
440typedef SingleRTPChannelIPV6 SymmetricRTPChannelIPV6;
441
442#endif
443
445
451
456 // sockets
458
459END_NAMESPACE
460
461#endif //CCRTP_CHANNEL_H_
462
Base elements for RTP stacks: constants, types and global functions.
uint32 microtimeout_t
Time interval expressed in microseconds.
Definition base.h:67
size_t ccioctl(int so, int request, size_t &len)
Definition channel.h:45
Definition channel.h:188
Socket::Error drop(const InetMcastAddress &ia)
Definition channel.h:226
DualRTPChannel(const InetAddress &ia, tpport_t port)
Definition channel.h:190
SOCKET getRecvSocket() const
Definition channel.h:241
void endSocket()
Definition channel.h:246
size_t send(const unsigned char *const buffer, size_t len)
Definition channel.h:238
bool isPendingRecv(microtimeout_t timeout) const
Definition channel.h:200
size_t getNextPacketSize() const
Definition channel.h:212
Socket::Error setTimeToLive(unsigned char ttl)
Definition channel.h:230
InetHostAddress getSender(tpport_t &port) const
Definition channel.h:204
~DualRTPChannel()
Definition channel.h:196
size_t recv(unsigned char *buffer, size_t len)
Definition channel.h:208
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
Definition channel.h:222
void setPeer(const InetAddress &host, tpport_t port)
Definition channel.h:234
Socket::Error setMulticast(bool enable)
Definition channel.h:216
A UDP/IPv4 socket class targetted at RTP stacks.
Definition channel.h:95
size_t getNextPacketSize() const
Get size of next datagram waiting to be read.
Definition channel.h:123
size_t recv(unsigned char *buffer, size_t len)
Definition channel.h:116
SOCKET getRecvSocket() const
Definition channel.h:157
RTPBaseUDPIPv4Socket()
Constructor for transmitter.
Definition channel.h:145
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
Definition channel.h:131
bool isPendingRecv(microtimeout_t timeout)
Definition channel.h:108
void endSocket()
Definition channel.h:162
Socket::Error drop(const InetMcastAddress &ia)
Definition channel.h:135
~RTPBaseUDPIPv4Socket()
Definition channel.h:104
size_t send(const unsigned char *const buffer, size_t len)
Definition channel.h:154
RTPBaseUDPIPv4Socket(const InetAddress &ia, tpport_t port)
Constructor for receiver.
Definition channel.h:100
Socket::Error setTimeToLive(unsigned char ttl)
Definition channel.h:139
InetHostAddress getSender(tpport_t &port) const
Definition channel.h:112
Socket::Error setMulticast(bool enable)
Definition channel.h:127
void setPeer(const InetAddress &ia, tpport_t port)
Definition channel.h:150
SingleRTPChannel SymmetricRTPChannel
Actually, RTP with a single channel can be called 'Symmetric RTP'.
Definition channel.h:455
RTPBaseUDPIPv4Socket SingleRTPChannel
May be used in applications where using the same socket for both sending and receiving is not a limit...
Definition channel.h:450
DualRTPChannel< RTPBaseUDPIPv4Socket > DualRTPUDPIPv4Channel
Definition channel.h:444