ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oqueue.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 
43 #ifndef CCXX_RTP_OQUEUE_H_
44 #define CCXX_RTP_OQUEUE_H_
45 
46 #include <ccrtp/queuebase.h>
47 #include <ccrtp/CryptoContext.h>
48 #include <list>
49 
50 NAMESPACE_COMMONCPP
51 
66 {
67 protected:
69  std::list<TransportAddress*> destList;
70 
71 public:
73 
75 
79  inline bool isSingleDestination() const
80  { return (1 == destList.size()); }
81 
83  { return destList.front(); }
84 
85  inline void lockDestinationList() const
86  { destinationLock.readLock(); }
87 
88  inline void unlockDestinationList() const
89  { destinationLock.unlock(); }
90 
91 protected:
92  inline void writeLockDestinationList() const
93  { destinationLock.writeLock(); }
94 
98  bool
99  addDestinationToList(const InetAddress& ia, tpport_t data,
100  tpport_t control);
101 
105  bool removeDestinationFromList(const InetAddress& ia,
106  tpport_t dataPort,
107  tpport_t controlPort);
108 
110  {
111  TransportAddress(InetAddress na, tpport_t dtp, tpport_t ctp) :
112  networkAddress(na), dataTransportPort(dtp),
113  controlTransportPort(ctp)
114  { }
115 
116  inline const InetAddress& getNetworkAddress() const
117  { return networkAddress; }
118 
119  inline tpport_t getDataTransportPort() const
120  { return dataTransportPort; }
121 
122  inline tpport_t getControlTransportPort() const
123  { return controlTransportPort; }
124 
125  InetAddress networkAddress;
126  tpport_t dataTransportPort, controlTransportPort;
127  };
128 
129 private:
130  mutable ThreadLock destinationLock;
131 };
132 
133 #ifdef CCXX_IPV6
134 
142 class __EXPORT DestinationListHandlerIPV6
143 {
144 protected:
145  struct TransportAddressIPV6;
146  std::list<TransportAddressIPV6*> destListIPV6;
147 
148 public:
149  DestinationListHandlerIPV6();
150 
151  ~DestinationListHandlerIPV6();
152 
156  inline bool isSingleDestinationIPV6() const
157  { return (1 == destListIPV6.size()); }
158 
159  inline TransportAddressIPV6* getFirstDestinationIPV6() const
160  { return destListIPV6.front(); }
161 
162  inline void lockDestinationListIPV6() const
163  { destinationLock.readLock(); }
164 
165  inline void unlockDestinationListIPV6() const
166  { destinationLock.unlock(); }
167 
168 protected:
169  inline void writeLockDestinationListIPV6() const
170  { destinationLock.writeLock(); }
171 
175  bool
176  addDestinationToListIPV6(const IPV6Address& ia, tpport_t data,
177  tpport_t control);
178 
182  bool removeDestinationFromListIPV6(const IPV6Address& ia,
183  tpport_t dataPort,
184  tpport_t controlPort);
185 
186  struct TransportAddressIPV6
187  {
188  TransportAddressIPV6(IPV6Address na, tpport_t dtp, tpport_t ctp) :
189  networkAddress(na), dataTransportPort(dtp),
190  controlTransportPort(ctp)
191  { }
192 
193  inline const IPV6Address& getNetworkAddress() const
194  { return networkAddress; }
195 
196  inline tpport_t getDataTransportPort() const
197  { return dataTransportPort; }
198 
199  inline tpport_t getControlTransportPort() const
200  { return controlTransportPort; }
201 
202  IPV6Address networkAddress;
203  tpport_t dataTransportPort, controlTransportPort;
204  };
205 
206 private:
207  mutable ThreadLock destinationLock;
208 };
209 
210 #endif
211 
220  public OutgoingDataQueueBase,
221 #ifdef CCXX_IPV6
222  protected DestinationListHandlerIPV6,
223 #endif
224  protected DestinationListHandler
225 {
226 public:
227 #ifdef CCXX_IPV6
228  bool
229  addDestination(const IPV6Address& ia,
230  tpport_t dataPort = DefaultRTPDataPort,
231  tpport_t controlPort = 0);
232 
233  bool
234  forgetDestination(const IPV6Address& ia,
235  tpport_t dataPort = DefaultRTPDataPort,
236  tpport_t controlPort = 0);
237 
238 #endif
239 
240  bool
241  addDestination(const InetHostAddress& ia,
242  tpport_t dataPort = DefaultRTPDataPort,
243  tpport_t controlPort = 0);
244 
245  bool
246  addDestination(const InetMcastAddress& ia,
247  tpport_t dataPort = DefaultRTPDataPort,
248  tpport_t controlPort = 0);
249 
250  bool
251  forgetDestination(const InetHostAddress& ia,
252  tpport_t dataPort = DefaultRTPDataPort,
253  tpport_t controlPort = 0);
254 
255  bool
256  forgetDestination(const InetMcastAddress& ia,
257  tpport_t dataPort = DefaultRTPDataPort,
258  tpport_t controlPort = 0);
259 
265  void
266  addContributor(uint32 csrc);
267 
271  bool
272  removeContributor(uint32 csrc);
273 
279  bool
280  isSending() const;
281 
282 
295  void
296  putData(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
297 
310  void
311  sendImmediate(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
312 
313 
320  void setPadding(uint8 paddinglen)
321  { sendInfo.paddinglen = paddinglen; }
322 
331  void setMark(bool mark)
332  { sendInfo.marked = mark; }
333 
337  inline bool getMark() const
338  { return sendInfo.marked; }
339 
350  size_t
351  setPartial(uint32 timestamp, unsigned char* data, size_t offset, size_t max);
352 
353  inline microtimeout_t
355  { return defaultSchedulingTimeout; }
356 
363  inline void
365  { schedulingTimeout = to; }
366 
367  inline microtimeout_t
369  { return defaultExpireTimeout; }
370 
378  inline void
380  { expireTimeout = to; }
381 
383  { return expireTimeout; }
384 
390  inline uint32
392  { return sendInfo.packetCount; }
393 
399  inline uint32
401  { return sendInfo.octetCount; }
402 
408  inline uint16
410  { return sendInfo.sendSeq; }
411 
420  void
421  setOutQueueCryptoContext(CryptoContext* cc);
422 
431  void
432  removeOutQueueCryptoContext(CryptoContext* cc);
433 
442  getOutQueueCryptoContext(uint32 ssrc);
443 
444 
445 protected:
447 
449  { }
450 
452  {
455  OutgoingRTPPktLink* n) :
456  packet(pkt), prev(p), next(n) { }
457 
458  ~OutgoingRTPPktLink() { delete packet; }
459 
460  inline OutgoingRTPPkt* getPacket() { return packet; }
461 
462  inline void setPacket(OutgoingRTPPkt* pkt) { packet = pkt; }
463 
464  inline OutgoingRTPPktLink* getPrev() { return prev; }
465 
466  inline void setPrev(OutgoingRTPPktLink* p) { prev = p; }
467 
468  inline OutgoingRTPPktLink* getNext() { return next; }
469 
470  inline void setNext(OutgoingRTPPktLink* n) { next = n; }
471 
472  // the packet this link refers to.
474  // global outgoing packets queue.
476  };
477 
485  void
486  dispatchImmediate(OutgoingRTPPkt *packet);
487 
498  getSchedulingTimeout();
499 
506  size_t
507  dispatchDataPacket();
508 
517  inline void
518  setNextSeqNum(uint32 seqNum)
519  { sendInfo.sendSeq = seqNum; }
520 
521  inline uint32
523  { return sendInfo.sendSeq; }
524 
527  inline void
529  { initialTimestamp = ts; }
530 
533  inline uint32
535  { return initialTimestamp; }
536 
537  void purgeOutgoingQueue();
538 
539  virtual void
540  setControlPeer(const InetAddress &host, tpport_t port) {}
541 
542 #ifdef CCXX_IPV6
543  virtual void
544  setControlPeerIPV6(const IPV6Address &host, tpport_t port) {}
545 #endif
546 
547  // The crypto contexts for outgoing SRTP sessions.
548  mutable Mutex cryptoMutex;
549  std::list<CryptoContext *> cryptoContexts;
550 
551 private:
557  inline virtual void onExpireSend(OutgoingRTPPkt&)
558  { }
559 
560  virtual void
561  setDataPeer(const InetAddress &host, tpport_t port) {}
562 
563 #ifdef CCXX_IPV6
564  virtual void
565  setDataPeerIPV6(const IPV6Address &host, tpport_t port) {}
566 #endif
567 
577  virtual size_t
578  sendData(const unsigned char* const buffer, size_t len) {return 0;}
579 
580 #ifdef CCXX_IPV6
581  virtual size_t
582  sendDataIPV6(const unsigned char* const buffer, size_t len) {return 0;}
583 #endif
584 
587  mutable ThreadLock sendLock;
588  // outgoing data packets queue
591  // transmission scheduling timeout for the service thread
593  // how old a packet can reach in the sending queue before deletetion
595 
596 
597  struct {
598  // number of packets sent from the beginning
599  uint32 packetCount;
600  // number of payload octets sent from the beginning
601  uint32 octetCount;
602  // the sequence number of the next packet to sent
603  uint16 sendSeq;
604  // contributing sources
605  uint32 sendSources[16];
606  // how many CSRCs to send.
607  uint16 sendCC;
608  // pad packets to a paddinglen multiple
609  uint8 paddinglen;
610  // This flags tells whether to set the bit M in the
611  // RTP fixed header of the packet in which the next
612  // provided data will be sent.
613  bool marked;
614  // whether there was not loss.
615  bool complete;
616  // ramdonly generated offset for the timestamp of sent packets
617  uint32 initialTimestamp;
618  // elapsed time accumulated through successive overflows of
619  // the local timestamp field
620  timeval overflowTime;
621  } sendInfo;
622 };
623  // oqueue
625 
626 END_NAMESPACE
627 
628 #endif //CCXX_RTP_OQUEUE_H_
629 
uint32 getSendPacketCount() const
Get the total number of packets sent so far.
Definition: oqueue.h:391
uint32 getSendOctetCount() const
Get the total number of octets (payload only) sent so far.
Definition: oqueue.h:400
void setMark(bool mark)
Set marker bit for the packet in which the next data provided will be send.
Definition: oqueue.h:331
virtual size_t sendData(const unsigned char *const buffer, size_t len)
This function performs the physical I/O for writing a packet to the destination.
Definition: oqueue.h:578
void setSchedulingTimeout(microtimeout_t to)
Set the default scheduling timeout to use when no data packets are waiting to be sent.
Definition: oqueue.h:364
bool getMark() const
Get wheter the mark bit will be set in the next packet.
Definition: oqueue.h:337
The implementation for a SRTP cryptographic context.
Definition: CryptoContext.h:82
ThreadLock destinationLock
Definition: oqueue.h:130
timeval overflowTime
Definition: oqueue.h:620
void writeLockDestinationList() const
Definition: oqueue.h:92
uint32 microtimeout_t
Time interval expressed in microseconds.
Definition: base.h:67
uint8 paddinglen
Definition: oqueue.h:609
uint32 getCurrentSeqNum(void)
Definition: oqueue.h:522
std::list< TransportAddress * > destList
Definition: oqueue.h:68
microtimeout_t expireTimeout
Definition: oqueue.h:594
This class handles a list of destination addresses.
Definition: oqueue.h:65
void lockDestinationList() const
Definition: oqueue.h:85
microtimeout_t getDefaultExpireTimeout() const
Definition: oqueue.h:368
uint32 packetCount
Definition: oqueue.h:599
void setNextSeqNum(uint32 seqNum)
For thoses cases in which the application requires a method to set the sequence number for the outgoi...
Definition: oqueue.h:518
std::list< CryptoContext * > cryptoContexts
Definition: oqueue.h:549
TransportAddress * getFirstDestination() const
Definition: oqueue.h:82
uint32 getInitialTimestamp()
Definition: oqueue.h:534
virtual void onExpireSend(OutgoingRTPPkt &)
A hook to filter packets being sent that have been expired.
Definition: oqueue.h:557
void setPadding(uint8 paddinglen)
Set padding.
Definition: oqueue.h:320
uint32 initialTimestamp
Definition: oqueue.h:590
OutgoingRTPPktLink * sendLast
Definition: oqueue.h:589
Base classes for RTP queues.
microtimeout_t getDefaultSchedulingTimeout() const
Definition: oqueue.h:354
#define __EXPORT
Definition: ZrtpCallback.h:40
virtual void setControlPeer(const InetAddress &host, tpport_t port)
Definition: oqueue.h:540
static const microtimeout_t defaultSchedulingTimeout
Schedule at 8 ms.
Definition: oqueue.h:585
const tpport_t DefaultRTPDataPort
registered default RTP data transport port
Definition: base.h:109
microtimeout_t schedulingTimeout
Definition: oqueue.h:592
void setInitialTimestamp(uint32 ts)
Definition: oqueue.h:528
tpport_t getControlTransportPort() const
Definition: oqueue.h:122
const InetAddress & getNetworkAddress() const
Definition: oqueue.h:116
uint16 getSequenceNumber() const
Get the sequence number of the next outgoing packet.
Definition: oqueue.h:409
tpport_t getDataTransportPort() const
Definition: oqueue.h:119
uint32 octetCount
Definition: oqueue.h:601
virtual ~OutgoingDataQueue()
Definition: oqueue.h:448
ThreadLock sendLock
Definition: oqueue.h:587
uint16 sendCC
Definition: oqueue.h:607
Mutex cryptoMutex
Definition: oqueue.h:548
static const microtimeout_t defaultExpireTimeout
Packets unsent will expire after 40 ms.
Definition: oqueue.h:586
TransportAddress(InetAddress na, tpport_t dtp, tpport_t ctp)
Definition: oqueue.h:111
void setExpireTimeout(microtimeout_t to)
Set the "expired" timer for expiring packets pending in the send queue which have gone unsent and are...
Definition: oqueue.h:379
virtual void setDataPeer(const InetAddress &host, tpport_t port)
Definition: oqueue.h:561
bool isSingleDestination() const
Get whether there is only a destination in the list.
Definition: oqueue.h:79
A generic outgoing RTP data queue supporting multiple destinations.
Definition: oqueue.h:219
microtimeout_t getExpireTimeout() const
Definition: oqueue.h:382
RTP packets being sent.
Definition: rtppkt.h:507
void unlockDestinationList() const
Definition: oqueue.h:88
uint16 sendSeq
Definition: oqueue.h:603