ccRTP
oqueue.h
Go to the documentation of this file.
1 // Copyright (C) 2001,2002,2004,2005 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 General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // ccRTP. If you copy code from other releases into a copy of GNU
28 // ccRTP, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU ccRTP, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
44 #ifndef CCXX_RTP_OQUEUE_H_
45 #define CCXX_RTP_OQUEUE_H_
46 
47 #include <ccrtp/queuebase.h>
48 #include <ccrtp/CryptoContext.h>
49 #include <list>
50 
51 NAMESPACE_COMMONCPP
52 
66 class __EXPORT DestinationListHandler
67 {
68 protected:
70  std::list<TransportAddress*> destList;
71 
72 public:
74 
76 
80  inline bool isSingleDestination() const
81  { return (1 == destList.size()); }
82 
83  inline TransportAddress* getFirstDestination() const
84  { return destList.front(); }
85 
86  inline void lockDestinationList() const
87  { destinationLock.readLock(); }
88 
89  inline void unlockDestinationList() const
90  { destinationLock.unlock(); }
91 
92 protected:
93  inline void writeLockDestinationList() const
94  { destinationLock.writeLock(); }
95 
99  bool
100  addDestinationToList(const InetAddress& ia, tpport_t data,
101  tpport_t control);
102 
106  bool removeDestinationFromList(const InetAddress& ia,
107  tpport_t dataPort,
108  tpport_t controlPort);
109 
111  {
112  TransportAddress(InetAddress na, tpport_t dtp, tpport_t ctp) :
113  networkAddress(na), dataTransportPort(dtp),
114  controlTransportPort(ctp)
115  { }
116 
117  inline const InetAddress& getNetworkAddress() const
118  { return networkAddress; }
119 
120  inline tpport_t getDataTransportPort() const
121  { return dataTransportPort; }
122 
123  inline tpport_t getControlTransportPort() const
124  { return controlTransportPort; }
125 
126  InetAddress networkAddress;
127  tpport_t dataTransportPort, controlTransportPort;
128  };
129 
130 private:
131  mutable ThreadLock destinationLock;
132 };
133 
134 #ifdef CCXX_IPV6
135 
143 class __EXPORT DestinationListHandlerIPV6
144 {
145 protected:
146  struct TransportAddressIPV6;
147  std::list<TransportAddressIPV6*> destListIPV6;
148 
149 public:
150  DestinationListHandlerIPV6();
151 
152  ~DestinationListHandlerIPV6();
153 
157  inline bool isSingleDestinationIPV6() const
158  { return (1 == destListIPV6.size()); }
159 
160  inline TransportAddressIPV6* getFirstDestinationIPV6() const
161  { return destListIPV6.front(); }
162 
163  inline void lockDestinationListIPV6() const
164  { destinationLock.readLock(); }
165 
166  inline void unlockDestinationListIPV6() const
167  { destinationLock.unlock(); }
168 
169 protected:
170  inline void writeLockDestinationListIPV6() const
171  { destinationLock.writeLock(); }
172 
176  bool
177  addDestinationToListIPV6(const IPV6Address& ia, tpport_t data,
178  tpport_t control);
179 
183  bool removeDestinationFromListIPV6(const IPV6Address& ia,
184  tpport_t dataPort,
185  tpport_t controlPort);
186 
187  struct TransportAddressIPV6
188  {
189  TransportAddressIPV6(IPV6Address na, tpport_t dtp, tpport_t ctp) :
190  networkAddress(na), dataTransportPort(dtp),
191  controlTransportPort(ctp)
192  { }
193 
194  inline const IPV6Address& getNetworkAddress() const
195  { return networkAddress; }
196 
197  inline tpport_t getDataTransportPort() const
198  { return dataTransportPort; }
199 
200  inline tpport_t getControlTransportPort() const
201  { return controlTransportPort; }
202 
203  IPV6Address networkAddress;
204  tpport_t dataTransportPort, controlTransportPort;
205  };
206 
207 private:
208  mutable ThreadLock destinationLock;
209 };
210 
211 #endif
212 
220 class __EXPORT OutgoingDataQueue:
221  public OutgoingDataQueueBase,
222 #ifdef CCXX_IPV6
223  protected DestinationListHandlerIPV6,
224 #endif
225  protected DestinationListHandler
226 {
227 public:
228 #ifdef CCXX_IPV6
229  bool
230  addDestination(const IPV6Address& ia,
231  tpport_t dataPort = DefaultRTPDataPort,
232  tpport_t controlPort = 0);
233 
234  bool
235  forgetDestination(const IPV6Address& ia,
236  tpport_t dataPort = DefaultRTPDataPort,
237  tpport_t controlPort = 0);
238 
239 #endif
240 
241  bool
242  addDestination(const InetHostAddress& ia,
243  tpport_t dataPort = DefaultRTPDataPort,
244  tpport_t controlPort = 0);
245 
246  bool
247  addDestination(const InetMcastAddress& ia,
248  tpport_t dataPort = DefaultRTPDataPort,
249  tpport_t controlPort = 0);
250 
251  bool
252  forgetDestination(const InetHostAddress& ia,
253  tpport_t dataPort = DefaultRTPDataPort,
254  tpport_t controlPort = 0);
255 
256  bool
257  forgetDestination(const InetMcastAddress& ia,
258  tpport_t dataPort = DefaultRTPDataPort,
259  tpport_t controlPort = 0);
260 
266  void
267  addContributor(uint32 csrc);
268 
272  bool
273  removeContributor(uint32 csrc);
274 
280  bool
281  isSending() const;
282 
283 
296  void
297  putData(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
298 
311  void
312  sendImmediate(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
313 
314 
321  void setPadding(uint8 paddinglen)
322  { sendInfo.paddinglen = paddinglen; }
323 
332  void setMark(bool mark)
333  { sendInfo.marked = mark; }
334 
338  inline bool getMark() const
339  { return sendInfo.marked; }
340 
351  size_t
352  setPartial(uint32 timestamp, unsigned char* data, size_t offset, size_t max);
353 
354  inline microtimeout_t
355  getDefaultSchedulingTimeout() const
356  { return defaultSchedulingTimeout; }
357 
364  inline void
365  setSchedulingTimeout(microtimeout_t to)
366  { schedulingTimeout = to; }
367 
368  inline microtimeout_t
369  getDefaultExpireTimeout() const
370  { return defaultExpireTimeout; }
371 
379  inline void
380  setExpireTimeout(microtimeout_t to)
381  { expireTimeout = to; }
382 
383  inline microtimeout_t getExpireTimeout() const
384  { return expireTimeout; }
385 
391  inline uint32
392  getSendPacketCount() const
393  { return sendInfo.packetCount; }
394 
400  inline uint32
401  getSendOctetCount() const
402  { return sendInfo.octetCount; }
403 
409  inline uint16
410  getSequenceNumber() const
411  { return sendInfo.sendSeq; }
412 
421  void
422  setOutQueueCryptoContext(CryptoContext* cc);
423 
432  void
433  removeOutQueueCryptoContext(CryptoContext* cc);
434 
443  getOutQueueCryptoContext(uint32 ssrc);
444 
445 
446 protected:
448 
449  virtual ~OutgoingDataQueue()
450  { }
451 
453  {
456  OutgoingRTPPktLink* n) :
457  packet(pkt), prev(p), next(n) { }
458 
459  ~OutgoingRTPPktLink() { delete packet; }
460 
461  inline OutgoingRTPPkt* getPacket() { return packet; }
462 
463  inline void setPacket(OutgoingRTPPkt* pkt) { packet = pkt; }
464 
465  inline OutgoingRTPPktLink* getPrev() { return prev; }
466 
467  inline void setPrev(OutgoingRTPPktLink* p) { prev = p; }
468 
469  inline OutgoingRTPPktLink* getNext() { return next; }
470 
471  inline void setNext(OutgoingRTPPktLink* n) { next = n; }
472 
473  // the packet this link refers to.
475  // global outgoing packets queue.
477  };
478 
486  void
487  dispatchImmediate(OutgoingRTPPkt *packet);
488 
499  getSchedulingTimeout();
500 
507  size_t
508  dispatchDataPacket();
509 
518  inline void
519  setNextSeqNum(uint32 seqNum)
520  { sendInfo.sendSeq = seqNum; }
521 
522  inline uint32
523  getCurrentSeqNum(void)
524  { return sendInfo.sendSeq; }
525 
528  inline void
529  setInitialTimestamp(uint32 ts)
530  { initialTimestamp = ts; }
531 
534  inline uint32
535  getInitialTimestamp()
536  { return initialTimestamp; }
537 
538  void purgeOutgoingQueue();
539 
540  virtual void
541  setControlPeer(const InetAddress &host, tpport_t port) {}
542 
543 #ifdef CCXX_IPV6
544  virtual void
545  setControlPeerIPV6(const IPV6Address &host, tpport_t port) {}
546 #endif
547 
548  // The crypto contexts for outgoing SRTP sessions.
549  mutable Mutex cryptoMutex;
550  std::list<CryptoContext *> cryptoContexts;
551 
552 private:
558  inline virtual void onExpireSend(OutgoingRTPPkt&)
559  { }
560 
561  virtual void
562  setDataPeer(const InetAddress &host, tpport_t port) {}
563 
564 #ifdef CCXX_IPV6
565  virtual void
566  setDataPeerIPV6(const IPV6Address &host, tpport_t port) {}
567 #endif
568 
578  virtual size_t
579  sendData(const unsigned char* const buffer, size_t len) {return 0;}
580 
581 #ifdef CCXX_IPV6
582  virtual size_t
583  sendDataIPV6(const unsigned char* const buffer, size_t len) {return 0;}
584 #endif
585 
586  static const microtimeout_t defaultSchedulingTimeout;
587  static const microtimeout_t defaultExpireTimeout;
588  mutable ThreadLock sendLock;
589  // outgoing data packets queue
590  OutgoingRTPPktLink* sendFirst, * sendLast;
591  uint32 initialTimestamp;
592  // transmission scheduling timeout for the service thread
593  microtimeout_t schedulingTimeout;
594  // how old a packet can reach in the sending queue before deletetion
595  microtimeout_t expireTimeout;
596 
597 
598  struct {
599  // number of packets sent from the beginning
600  uint32 packetCount;
601  // number of payload octets sent from the beginning
602  uint32 octetCount;
603  // the sequence number of the next packet to sent
604  uint16 sendSeq;
605  // contributing sources
606  uint32 sendSources[16];
607  // how many CSRCs to send.
608  uint16 sendCC;
609  // pad packets to a paddinglen multiple
610  uint8 paddinglen;
611  // This flags tells whether to set the bit M in the
612  // RTP fixed header of the packet in which the next
613  // provided data will be sent.
614  bool marked;
615  // whether there was not loss.
616  bool complete;
617  // ramdonly generated offset for the timestamp of sent packets
618  uint32 initialTimestamp;
619  // elapsed time accumulated through successive overflows of
620  // the local timestamp field
621  timeval overflowTime;
622  } sendInfo;
623 };
624  // oqueue
626 
627 END_NAMESPACE
628 
629 #endif //CCXX_RTP_OQUEUE_H_
630