ccRTP
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 NAMESPACE_COMMONCPP
00052 
00066 class __EXPORT DestinationListHandler
00067 {
00068 protected:
00069     struct TransportAddress;
00070     std::list<TransportAddress*> destList;
00071 
00072 public:
00073     DestinationListHandler();
00074 
00075     ~DestinationListHandler();
00076 
00080     inline bool isSingleDestination() const
00081     { return (1 == destList.size()); }
00082 
00083     inline TransportAddress* getFirstDestination() const
00084     { return destList.front(); }
00085 
00086     inline void lockDestinationList() const
00087     { destinationLock.readLock(); }
00088 
00089     inline void unlockDestinationList() const
00090     { destinationLock.unlock(); }
00091 
00092 protected:
00093     inline void writeLockDestinationList() const
00094     { destinationLock.writeLock(); }
00095 
00099     bool
00100     addDestinationToList(const InetAddress& ia, tpport_t data,
00101                  tpport_t control);
00102 
00106     bool removeDestinationFromList(const InetAddress& ia,
00107                        tpport_t dataPort,
00108                        tpport_t controlPort);
00109 
00110     struct TransportAddress
00111     {
00112         TransportAddress(InetAddress na, tpport_t dtp, tpport_t ctp) :
00113             networkAddress(na), dataTransportPort(dtp),
00114             controlTransportPort(ctp)
00115         {  }
00116 
00117         inline const InetAddress& getNetworkAddress() const
00118         { return networkAddress; }
00119 
00120         inline tpport_t getDataTransportPort() const
00121         { return dataTransportPort; }
00122 
00123         inline tpport_t getControlTransportPort() const
00124         { return controlTransportPort; }
00125 
00126         InetAddress networkAddress;
00127         tpport_t dataTransportPort, controlTransportPort;
00128     };
00129 
00130 private:
00131     mutable ThreadLock destinationLock;
00132 };
00133 
00134 #ifdef  CCXX_IPV6
00135 
00143 class __EXPORT DestinationListHandlerIPV6
00144 {
00145 protected:
00146     struct TransportAddressIPV6;
00147     std::list<TransportAddressIPV6*> destListIPV6;
00148 
00149 public:
00150     DestinationListHandlerIPV6();
00151 
00152     ~DestinationListHandlerIPV6();
00153 
00157     inline bool isSingleDestinationIPV6() const
00158     { return (1 == destListIPV6.size()); }
00159 
00160     inline TransportAddressIPV6* getFirstDestinationIPV6() const
00161     { return destListIPV6.front(); }
00162 
00163     inline void lockDestinationListIPV6() const
00164     { destinationLock.readLock(); }
00165 
00166     inline void unlockDestinationListIPV6() const
00167     { destinationLock.unlock(); }
00168 
00169 protected:
00170     inline void writeLockDestinationListIPV6() const
00171     { destinationLock.writeLock(); }
00172 
00176     bool
00177     addDestinationToListIPV6(const IPV6Address& ia, tpport_t data,
00178                  tpport_t control);
00179 
00183     bool removeDestinationFromListIPV6(const IPV6Address& ia,
00184                        tpport_t dataPort,
00185                        tpport_t controlPort);
00186 
00187     struct TransportAddressIPV6
00188     {
00189         TransportAddressIPV6(IPV6Address na, tpport_t dtp, tpport_t ctp) :
00190             networkAddress(na), dataTransportPort(dtp),
00191             controlTransportPort(ctp)
00192         {  }
00193 
00194         inline const IPV6Address& getNetworkAddress() const
00195         { return networkAddress; }
00196 
00197         inline tpport_t getDataTransportPort() const
00198         { return dataTransportPort; }
00199 
00200         inline tpport_t getControlTransportPort() const
00201         { return controlTransportPort; }
00202 
00203         IPV6Address networkAddress;
00204         tpport_t dataTransportPort, controlTransportPort;
00205     };
00206 
00207 private:
00208     mutable ThreadLock destinationLock;
00209 };
00210 
00211 #endif
00212 
00220 class __EXPORT OutgoingDataQueue:
00221     public OutgoingDataQueueBase,
00222 #ifdef  CCXX_IPV6
00223     protected DestinationListHandlerIPV6,
00224 #endif
00225     protected DestinationListHandler
00226 {
00227 public:
00228 #ifdef  CCXX_IPV6
00229     bool
00230     addDestination(const IPV6Address& ia,
00231                tpport_t dataPort = DefaultRTPDataPort,
00232                tpport_t controlPort = 0);
00233 
00234     bool
00235     forgetDestination(const IPV6Address& ia,
00236               tpport_t dataPort = DefaultRTPDataPort,
00237               tpport_t controlPort = 0);
00238 
00239 #endif
00240 
00241     bool
00242     addDestination(const InetHostAddress& ia,
00243                tpport_t dataPort = DefaultRTPDataPort,
00244                tpport_t controlPort = 0);
00245 
00246     bool
00247     addDestination(const InetMcastAddress& ia,
00248                tpport_t dataPort = DefaultRTPDataPort,
00249                tpport_t controlPort = 0);
00250 
00251     bool
00252     forgetDestination(const InetHostAddress& ia,
00253               tpport_t dataPort = DefaultRTPDataPort,
00254               tpport_t controlPort = 0);
00255 
00256     bool
00257     forgetDestination(const InetMcastAddress& ia,
00258               tpport_t dataPort = DefaultRTPDataPort,
00259               tpport_t controlPort = 0);
00260 
00266     void
00267     addContributor(uint32 csrc);
00268 
00272     bool
00273     removeContributor(uint32 csrc);
00274 
00280     bool
00281     isSending() const;
00282 
00283 
00296     void
00297     putData(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
00298 
00311         void
00312         sendImmediate(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
00313 
00314 
00321     void setPadding(uint8 paddinglen)
00322     { sendInfo.paddinglen = paddinglen; }
00323 
00332     void setMark(bool mark)
00333     { sendInfo.marked = mark; }
00334 
00338     inline bool getMark() const
00339     { return sendInfo.marked; }
00340 
00351     size_t
00352     setPartial(uint32 timestamp, unsigned char* data, size_t offset, size_t max);
00353 
00354     inline microtimeout_t
00355     getDefaultSchedulingTimeout() const
00356     { return defaultSchedulingTimeout; }
00357 
00364     inline void
00365     setSchedulingTimeout(microtimeout_t to)
00366     { schedulingTimeout = to; }
00367 
00368     inline microtimeout_t
00369     getDefaultExpireTimeout() const
00370     { return defaultExpireTimeout; }
00371 
00379     inline void
00380     setExpireTimeout(microtimeout_t to)
00381     { expireTimeout = to; }
00382 
00383     inline microtimeout_t getExpireTimeout() const
00384     { return expireTimeout; }
00385 
00391     inline uint32
00392     getSendPacketCount() const
00393     { return sendInfo.packetCount; }
00394 
00400     inline uint32
00401     getSendOctetCount() const
00402     { return sendInfo.octetCount; }
00403 
00409         inline uint16
00410         getSequenceNumber() const
00411         { return sendInfo.sendSeq; }
00412 
00421         void
00422         setOutQueueCryptoContext(CryptoContext* cc);
00423 
00432         void
00433         removeOutQueueCryptoContext(CryptoContext* cc);
00434 
00442         CryptoContext*
00443         getOutQueueCryptoContext(uint32 ssrc);
00444 
00445 
00446 protected:
00447     OutgoingDataQueue();
00448 
00449     virtual ~OutgoingDataQueue()
00450     { }
00451 
00452     struct OutgoingRTPPktLink
00453     {
00454         OutgoingRTPPktLink(OutgoingRTPPkt* pkt,
00455                    OutgoingRTPPktLink* p,
00456                    OutgoingRTPPktLink* n) :
00457             packet(pkt), prev(p), next(n) { }
00458 
00459         ~OutgoingRTPPktLink() { delete packet; }
00460 
00461         inline OutgoingRTPPkt* getPacket() { return packet; }
00462 
00463         inline void setPacket(OutgoingRTPPkt* pkt) { packet = pkt; }
00464 
00465         inline OutgoingRTPPktLink* getPrev() { return prev; }
00466 
00467         inline void setPrev(OutgoingRTPPktLink* p) { prev = p; }
00468 
00469         inline OutgoingRTPPktLink* getNext() { return next; }
00470 
00471         inline void setNext(OutgoingRTPPktLink* n) { next = n; }
00472 
00473         // the packet this link refers to.
00474         OutgoingRTPPkt* packet;
00475         // global outgoing packets queue.
00476         OutgoingRTPPktLink * prev, * next;
00477     };
00478 
00486     void
00487     dispatchImmediate(OutgoingRTPPkt *packet);
00488 
00498     microtimeout_t
00499     getSchedulingTimeout();
00500 
00507     size_t
00508     dispatchDataPacket();
00509 
00518     inline void
00519     setNextSeqNum(uint32 seqNum)
00520     { sendInfo.sendSeq = seqNum; }
00521 
00522     inline uint32
00523     getCurrentSeqNum(void)
00524     { return sendInfo.sendSeq; }
00525 
00528     inline void
00529     setInitialTimestamp(uint32 ts)
00530     { initialTimestamp = ts; }
00531 
00534     inline uint32
00535     getInitialTimestamp()
00536     { return initialTimestamp; }
00537 
00538     void purgeOutgoingQueue();
00539 
00540         virtual void
00541         setControlPeer(const InetAddress &host, tpport_t port) {}
00542 
00543 #ifdef  CCXX_IPV6
00544     virtual void
00545     setControlPeerIPV6(const IPV6Address &host, tpport_t port) {}
00546 #endif
00547 
00548         // The crypto contexts for outgoing SRTP sessions.
00549     mutable Mutex cryptoMutex;
00550         std::list<CryptoContext *> cryptoContexts;
00551 
00552 private:
00558     inline virtual void onExpireSend(OutgoingRTPPkt&)
00559     { }
00560 
00561     virtual void
00562         setDataPeer(const InetAddress &host, tpport_t port) {}
00563 
00564 #ifdef  CCXX_IPV6
00565     virtual void
00566     setDataPeerIPV6(const IPV6Address &host, tpport_t port) {}
00567 #endif
00568 
00578     virtual size_t
00579     sendData(const unsigned char* const buffer, size_t len) {return 0;}
00580 
00581 #ifdef  CCXX_IPV6
00582     virtual size_t
00583     sendDataIPV6(const unsigned char* const buffer, size_t len) {return 0;}
00584 #endif
00585 
00586     static const microtimeout_t defaultSchedulingTimeout;
00587     static const microtimeout_t defaultExpireTimeout;
00588     mutable ThreadLock sendLock;
00589     // outgoing data packets queue
00590     OutgoingRTPPktLink* sendFirst, * sendLast;
00591     uint32 initialTimestamp;
00592     // transmission scheduling timeout for the service thread
00593     microtimeout_t schedulingTimeout;
00594     // how old a packet can reach in the sending queue before deletetion
00595     microtimeout_t expireTimeout;
00596 
00597 
00598     struct {
00599         // number of packets sent from the beginning
00600         uint32 packetCount;
00601         // number of payload octets sent from the beginning
00602         uint32 octetCount;
00603         // the sequence number of the next packet to sent
00604         uint16 sendSeq;
00605         // contributing sources
00606         uint32 sendSources[16];
00607         // how many CSRCs to send.
00608         uint16 sendCC;
00609         // pad packets to a paddinglen multiple
00610         uint8 paddinglen;
00611         // This flags tells whether to set the bit M in the
00612         // RTP fixed header of the packet in which the next
00613         // provided data will be sent.
00614         bool marked;
00615         // whether there was not loss.
00616         bool complete;
00617         // ramdonly generated offset for the timestamp of sent packets
00618         uint32 initialTimestamp;
00619         // elapsed time accumulated through successive overflows of
00620         // the local timestamp field
00621         timeval overflowTime;
00622     } sendInfo;
00623 };
00624  // oqueue
00626 
00627 END_NAMESPACE
00628 
00629 #endif  //CCXX_RTP_OQUEUE_H_
00630