oqueue.h

Go to the documentation of this file.
00001 // Copyright (C) 2001,2002,2004,2005 Federico Montesino Pouzols <fedemp@altern.org>.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 //
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // ccRTP.  If you copy code from other releases into a copy of GNU
00028 // ccRTP, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU ccRTP, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00044 #ifndef CCXX_RTP_OQUEUE_H_
00045 #define CCXX_RTP_OQUEUE_H_
00046 
00047 #include <ccrtp/queuebase.h>
00048 #include <ccrtp/CryptoContext.h>
00049 #include <list>
00050 
00051 #ifdef  CCXX_NAMESPACES
00052 namespace ost {
00053 #endif
00054 
00068 class __EXPORT DestinationListHandler
00069 {
00070 protected:
00071         struct TransportAddress;
00072         std::list<TransportAddress*> destList;
00073 
00074 public:
00075         DestinationListHandler();
00076 
00077         ~DestinationListHandler();
00078 
00082         inline bool isSingleDestination() const
00083         { return (1 == destList.size()); }
00084 
00085         inline TransportAddress* getFirstDestination() const
00086         { return destList.front(); }
00087 
00088         inline void lockDestinationList() const
00089         { destinationLock.readLock(); }
00090 
00091         inline void unlockDestinationList() const
00092         { destinationLock.unlock(); }
00093 
00094 protected:
00095         inline void writeLockDestinationList() const
00096         { destinationLock.writeLock(); }
00097 
00101         bool
00102         addDestinationToList(const InetAddress& ia, tpport_t data,
00103                              tpport_t control);
00104 
00108         bool removeDestinationFromList(const InetAddress& ia,
00109                                        tpport_t dataPort,
00110                                        tpport_t controlPort);
00111 
00112         struct TransportAddress
00113         {
00114                 TransportAddress(InetAddress na, tpport_t dtp, tpport_t ctp) :
00115                         networkAddress(na), dataTransportPort(dtp),
00116                         controlTransportPort(ctp)
00117                 {  }
00118 
00119                 inline const InetAddress& getNetworkAddress() const
00120                 { return networkAddress; }
00121 
00122                 inline tpport_t getDataTransportPort() const
00123                 { return dataTransportPort; }
00124 
00125                 inline tpport_t getControlTransportPort() const
00126                 { return controlTransportPort; }
00127 
00128                 InetAddress networkAddress;
00129                 tpport_t dataTransportPort, controlTransportPort;
00130         };
00131 
00132 private:
00133         mutable ThreadLock destinationLock;
00134 };
00135 
00136 #ifdef  CCXX_IPV6
00137 
00145 class __EXPORT DestinationListHandlerIPV6
00146 {
00147 protected:
00148         struct TransportAddressIPV6;
00149         std::list<TransportAddressIPV6*> destListIPV6;
00150 
00151 public:
00152         DestinationListHandlerIPV6();
00153 
00154         ~DestinationListHandlerIPV6();
00155 
00159         inline bool isSingleDestinationIPV6() const
00160         { return (1 == destListIPV6.size()); }
00161 
00162         inline TransportAddressIPV6* getFirstDestinationIPV6() const
00163         { return destListIPV6.front(); }
00164 
00165         inline void lockDestinationListIPV6() const
00166         { destinationLock.readLock(); }
00167 
00168         inline void unlockDestinationListIPV6() const
00169         { destinationLock.unlock(); }
00170 
00171 protected:
00172         inline void writeLockDestinationListIPV6() const
00173         { destinationLock.writeLock(); }
00174 
00178         bool
00179         addDestinationToListIPV6(const IPV6Address& ia, tpport_t data,
00180                              tpport_t control);
00181 
00185         bool removeDestinationFromListIPV6(const IPV6Address& ia,
00186                                        tpport_t dataPort,
00187                                        tpport_t controlPort);
00188 
00189         struct TransportAddressIPV6
00190         {
00191                 TransportAddressIPV6(IPV6Address na, tpport_t dtp, tpport_t ctp) :
00192                         networkAddress(na), dataTransportPort(dtp),
00193                         controlTransportPort(ctp)
00194                 {  }
00195 
00196                 inline const IPV6Address& getNetworkAddress() const
00197                 { return networkAddress; }
00198 
00199                 inline tpport_t getDataTransportPort() const
00200                 { return dataTransportPort; }
00201 
00202                 inline tpport_t getControlTransportPort() const
00203                 { return controlTransportPort; }
00204 
00205                 IPV6Address networkAddress;
00206                 tpport_t dataTransportPort, controlTransportPort;
00207         };
00208 
00209 private:
00210         mutable ThreadLock destinationLock;
00211 };
00212 
00213 #endif
00214 
00222 class __EXPORT OutgoingDataQueue:
00223         public OutgoingDataQueueBase,
00224 #ifdef  CCXX_IPV6
00225         protected DestinationListHandlerIPV6,
00226 #endif
00227         protected DestinationListHandler
00228 {
00229 public:
00230 #ifdef  CCXX_IPV6
00231         bool
00232         addDestination(const IPV6Address& ia,
00233                        tpport_t dataPort = DefaultRTPDataPort,
00234                        tpport_t controlPort = 0);
00235 
00236         bool
00237         forgetDestination(const IPV6Address& ia,
00238                           tpport_t dataPort = DefaultRTPDataPort,
00239                           tpport_t controlPort = 0);
00240 
00241 #endif
00242 
00243         bool
00244         addDestination(const InetHostAddress& ia,
00245                        tpport_t dataPort = DefaultRTPDataPort,
00246                        tpport_t controlPort = 0);
00247 
00248         bool
00249         addDestination(const InetMcastAddress& ia,
00250                        tpport_t dataPort = DefaultRTPDataPort,
00251                        tpport_t controlPort = 0);
00252 
00253         bool
00254         forgetDestination(const InetHostAddress& ia,
00255                           tpport_t dataPort = DefaultRTPDataPort,
00256                           tpport_t controlPort = 0);
00257 
00258         bool
00259         forgetDestination(const InetMcastAddress& ia,
00260                           tpport_t dataPort = DefaultRTPDataPort,
00261                           tpport_t controlPort = 0);
00262 
00268         void
00269         addContributor(uint32 csrc);
00270 
00274         bool
00275         removeContributor(uint32 csrc);
00276 
00282         bool
00283         isSending() const;
00284 
00285 
00298         void
00299         putData(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
00300 
00313         void
00314         sendImmediate(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
00315 
00316 
00323         void setPadding(uint8 paddinglen)
00324         { sendInfo.paddinglen = paddinglen; }
00325 
00334         void setMark(bool mark)
00335         { sendInfo.marked = mark; }
00336 
00340         inline bool getMark() const
00341         { return sendInfo.marked; }
00342 
00353         size_t
00354         setPartial(uint32 timestamp, unsigned char* data, size_t offset, size_t max);
00355 
00356         inline microtimeout_t
00357         getDefaultSchedulingTimeout() const
00358         { return defaultSchedulingTimeout; }
00359 
00366         inline void
00367         setSchedulingTimeout(microtimeout_t to)
00368         { schedulingTimeout = to; }
00369 
00370         inline microtimeout_t
00371         getDefaultExpireTimeout() const
00372         { return defaultExpireTimeout; }
00373 
00381         inline void
00382         setExpireTimeout(microtimeout_t to)
00383         { expireTimeout = to; }
00384 
00385         inline microtimeout_t getExpireTimeout() const
00386         { return expireTimeout; }
00387 
00393         inline uint32
00394         getSendPacketCount() const
00395         { return sendInfo.packetCount; }
00396 
00402         inline uint32
00403         getSendOctetCount() const
00404         { return sendInfo.octetCount; }
00405 
00411         inline uint16
00412         getSequenceNumber() const
00413         { return sendInfo.sendSeq; }
00414 
00423         void
00424         setOutQueueCryptoContext(CryptoContext* cc);
00425 
00434         void
00435         removeOutQueueCryptoContext(CryptoContext* cc);
00436 
00444         CryptoContext*
00445         getOutQueueCryptoContext(uint32 ssrc);
00446 
00447 
00448 protected:
00449         OutgoingDataQueue();
00450 
00451         virtual ~OutgoingDataQueue()
00452         { }
00453 
00454         struct OutgoingRTPPktLink
00455         {
00456                 OutgoingRTPPktLink(OutgoingRTPPkt* pkt,
00457                                    OutgoingRTPPktLink* p,
00458                                    OutgoingRTPPktLink* n) :
00459                         packet(pkt), prev(p), next(n) { }
00460 
00461                 ~OutgoingRTPPktLink() { delete packet; }
00462 
00463                 inline OutgoingRTPPkt* getPacket() { return packet; }
00464 
00465                 inline void setPacket(OutgoingRTPPkt* pkt) { packet = pkt; }
00466 
00467                 inline OutgoingRTPPktLink* getPrev() { return prev; }
00468 
00469                 inline void setPrev(OutgoingRTPPktLink* p) { prev = p; }
00470 
00471                 inline OutgoingRTPPktLink* getNext() { return next; }
00472 
00473                 inline void setNext(OutgoingRTPPktLink* n) { next = n; }
00474 
00475                 // the packet this link refers to.
00476                 OutgoingRTPPkt* packet;
00477                 // global outgoing packets queue.
00478                 OutgoingRTPPktLink * prev, * next;
00479         };
00480 
00488         void
00489         dispatchImmediate(OutgoingRTPPkt *packet);
00490 
00500         microtimeout_t
00501         getSchedulingTimeout();
00502 
00509         size_t
00510         dispatchDataPacket();
00511 
00520         inline void
00521         setNextSeqNum(uint32 seqNum)
00522         { sendInfo.sendSeq = seqNum; }
00523 
00524         inline uint32
00525         getCurrentSeqNum(void)
00526         { return sendInfo.sendSeq; }
00527 
00530         inline void
00531         setInitialTimestamp(uint32 ts)
00532         { initialTimestamp = ts; }
00533 
00536         inline uint32
00537         getInitialTimestamp()
00538         { return initialTimestamp; }
00539 
00540         void purgeOutgoingQueue();
00541 
00542         virtual void
00543         setControlPeer(const InetAddress &host, tpport_t port) {};
00544 
00545 #ifdef  CCXX_IPV6
00546         virtual void
00547         setControlPeerIPV6(const IPV6Address &host, tpport_t port) {};
00548 #endif
00549 
00550         // The crypto contexts for outgoing SRTP sessions.
00551         mutable Mutex cryptoMutex;
00552         std::list<CryptoContext *> cryptoContexts;
00553 
00554 private:
00560         inline virtual void onExpireSend(OutgoingRTPPkt&)
00561         { }
00562 
00563         virtual void
00564         setDataPeer(const InetAddress &host, tpport_t port) {};
00565 
00566 #ifdef  CCXX_IPV6
00567         virtual void
00568         setDataPeerIPV6(const IPV6Address &host, tpport_t port) {};
00569 #endif
00570 
00580         virtual size_t
00581         sendData(const unsigned char* const buffer, size_t len) {return 0;};
00582 
00583 #ifdef  CCXX_IPV6
00584         virtual size_t
00585         sendDataIPV6(const unsigned char* const buffer, size_t len) {return 0;};
00586 #endif
00587 
00588         static const microtimeout_t defaultSchedulingTimeout;
00589         static const microtimeout_t defaultExpireTimeout;
00590         mutable ThreadLock sendLock;
00591         // outgoing data packets queue
00592         OutgoingRTPPktLink* sendFirst, * sendLast;
00593         uint32 initialTimestamp;
00594         // transmission scheduling timeout for the service thread
00595         microtimeout_t schedulingTimeout;
00596         // how old a packet can reach in the sending queue before deletetion
00597         microtimeout_t expireTimeout;
00598 
00599 
00600         struct {
00601                 // number of packets sent from the beginning
00602                 uint32 packetCount;
00603                 // number of payload octets sent from the beginning
00604                 uint32 octetCount;
00605                 // the sequence number of the next packet to sent
00606                 uint16 sendSeq;
00607                 // contributing sources
00608                 uint32 sendSources[16];
00609                 // how many CSRCs to send.
00610                 uint16 sendCC;
00611                 // pad packets to a paddinglen multiple
00612                 uint8 paddinglen;
00613                 // This flags tells whether to set the bit M in the
00614                 // RTP fixed header of the packet in which the next
00615                 // provided data will be sent.
00616                 bool marked;
00617                 // whether there was not loss.
00618                 bool complete;
00619                 // ramdonly generated offset for the timestamp of sent packets
00620                 uint32 initialTimestamp;
00621                 // elapsed time accumulated through successive overflows of
00622                 // the local timestamp field
00623                 timeval overflowTime;
00624         } sendInfo;
00625 };
00626  // oqueue
00628 
00629 #ifdef  CCXX_NAMESPACES
00630 }
00631 #endif
00632 
00633 #endif  //CCXX_RTP_OQUEUE_H_
00634 

Generated on Fri Jan 18 21:34:51 2008 for ccRTP by  doxygen 1.5.4