ccRTP
sources.h
Go to the documentation of this file.
00001 // Copyright (C) 2001,2002,2003,2004 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_SOURCES_H_
00045 #define CCXX_RTP_SOURCES_H_
00046 
00047 #include <string>
00048 #include <ccrtp/rtcppkt.h>
00049 #include <cstddef>
00050 
00051 NAMESPACE_COMMONCPP
00052 
00066 class __EXPORT SDESItemsHolder
00067 {
00068 public:
00069     const std::string&
00070     getItem(SDESItemType type) const;
00071 
00072     inline const std::string&
00073     getPRIVPrefix() const
00074     { return sdesItems[SDESItemTypeEND]; }
00075 
00076     void
00077     setItem(SDESItemType item, const std::string& val);
00078 
00079     inline void
00080     setPRIVPrefix(const std::string& val)
00081     { sdesItems[SDESItemTypeEND] = val; }
00082 
00083 protected:
00084     SDESItemsHolder()
00085     { }
00086 
00087     inline virtual ~SDESItemsHolder()
00088     { }
00089 
00090 private:
00091     // SDES items for a participant.
00092     // sdesItems[0] (== sdesItems[SDESItemTypeEND]) holds the prefix
00093     // value for the PRIV item. The rest of entries hold the
00094     // correponding SDES item value.
00095     std::string sdesItems[SDESItemTypeLast + 1];
00096 };
00097 
00126 class __EXPORT Participant : private SDESItemsHolder
00127 {
00128 public:
00141     const std::string&
00142     getSDESItem(SDESItemType type) const
00143     { return SDESItemsHolder::getItem(type); }
00144 
00152     inline const std::string&
00153     getPRIVPrefix() const
00154     { return SDESItemsHolder::getPRIVPrefix(); }
00155 
00161     Participant(const std::string& cname);
00162 
00163     ~Participant();
00164 
00165 private:
00166     friend class ParticipantHandler;
00167 
00171     inline void
00172     setSDESItem(SDESItemType item, const std::string& val)
00173     { SDESItemsHolder::setItem(item,val); }
00174 
00178     inline void
00179     setPRIVPrefix(const std::string val)
00180     { SDESItemsHolder::setPRIVPrefix(val); }
00181 };
00182 
00194 class __EXPORT SyncSource
00195 {
00196 public:
00227     typedef enum {
00228         stateUnknown,     
00229         statePrevalid,    
00230 
00231 
00232         stateActive,      
00233 
00234         stateInactive,    
00235 
00236 
00237         stateLeaving      
00238 
00239     }       State;
00240 
00245     SyncSource(uint32 ssrc);
00246 
00247     ~SyncSource();
00248 
00249     State
00250     getState() const
00251     { return state; }
00252 
00256     bool isSender() const
00257     { return activeSender; }
00258 
00259     uint32 getID() const
00260     { return SSRC; }
00261 
00269     inline Participant*
00270     getParticipant() const
00271     { return participant; }
00272 
00273     tpport_t getDataTransportPort() const
00274     { return dataTransportPort; }
00275 
00276     tpport_t getControlTransportPort() const
00277     { return controlTransportPort; }
00278 
00279     const InetAddress& getNetworkAddress() const
00280     { return networkAddress; }
00281 
00282 protected:
00286     SyncSource(const SyncSource& source);
00287 
00288     SyncSource&
00289     operator=(const SyncSource& source);
00290 
00291 private:
00292     friend class SyncSourceHandler;
00293 
00294     inline void
00295     setState(State st)
00296     { state = st; }
00297 
00301     inline void
00302     setSender(bool active)
00303     { activeSender = active; }
00304 
00305     inline void
00306     setParticipant(Participant& p)
00307     { participant = &p; }
00308 
00309     void setDataTransportPort(tpport_t p)
00310     { dataTransportPort = p; }
00311 
00312     void setControlTransportPort(tpport_t p)
00313     { controlTransportPort = p; }
00314 
00315     void setNetworkAddress(InetAddress addr)
00316     { networkAddress = addr; }
00317 
00318     inline void
00319     setLink(void *l)
00320     { link = l; }
00321 
00322     void *getLink() const
00323     { return link; }
00324 
00325     // validity state of this source
00326     State state;
00327     // 32-bit SSRC identifier.
00328     uint32 SSRC;
00329     // A valid source not always is active
00330         bool activeSender;
00331     // The corresponding participant.
00332     Participant* participant;
00333 
00334     // Network protocol address for data and control connection
00335     // (both are assumed to be the same).
00336     InetAddress networkAddress;
00337     tpport_t dataTransportPort;
00338     tpport_t controlTransportPort;
00339 
00340     // Pointer to the SyncSourceLink or similar object in the
00341     // service queue. Saves a lot of searches in the membership
00342     // table.
00343     void* link;
00344 };
00345 
00366 class __EXPORT RTPApplication : private SDESItemsHolder
00367 {
00368 private:
00369     struct ParticipantLink;
00370 
00371 public:
00379     RTPApplication(const std::string& cname);
00380 
00381     ~RTPApplication();
00382 
00383     inline void
00384     setSDESItem(SDESItemType item, const std::string& val)
00385     { SDESItemsHolder::setItem(item,val); }
00386 
00387     inline void
00388     setPRIVPrefix(const std::string& val)
00389     { SDESItemsHolder::setPRIVPrefix(val); }
00390 
00391     const std::string&
00392     getSDESItem(SDESItemType item) const
00393     { return SDESItemsHolder::getItem(item); }
00394 
00395     inline const std::string&
00396     getPRIVPrefix() const
00397     { return SDESItemsHolder::getPRIVPrefix(); }
00398 
00403     class ParticipantsIterator
00404     {
00405     public:
00406         typedef std::forward_iterator_tag iterator_category;
00407         typedef Participant value_type;
00408         typedef std::ptrdiff_t difference_type;
00409         typedef const Participant* pointer;
00410         typedef const Participant& reference;
00411 
00412         ParticipantsIterator(ParticipantLink* p = NULL) :
00413             link(p)
00414         { }
00415 
00416         ParticipantsIterator(const ParticipantsIterator& pi) :
00417             link(pi.link)
00418         { }
00419 
00420         reference operator*() const
00421         { return *(link->getParticipant()); }
00422 
00423         pointer operator->() const
00424         { return link->getParticipant(); }
00425 
00426         ParticipantsIterator& operator++() {
00427             link = link->getNext();
00428             return *this;
00429         }
00430 
00431         ParticipantsIterator operator++(int) {
00432             ParticipantsIterator result(*this);
00433             ++(*this);
00434             return result;
00435         }
00436         friend bool operator==(const ParticipantsIterator& l,
00437                        const ParticipantsIterator& r)
00438         { return l.link == r.link; }
00439 
00440         friend bool operator!=(const ParticipantsIterator& l,
00441                        const ParticipantsIterator& r)
00442         { return l.link != r.link; }
00443     private:
00444         ParticipantLink *link;
00445     };
00446 
00447     ParticipantsIterator begin()
00448     { return ParticipantsIterator(firstPart); }
00449 
00450     ParticipantsIterator end()
00451     { return ParticipantsIterator(NULL); }
00452 
00453     const Participant*
00454     getParticipant(const std::string& cname) const;
00455 
00456 private:
00457     friend class ApplicationHandler;
00458 
00459     struct ParticipantLink {
00460         ParticipantLink(Participant& p,
00461                 ParticipantLink* l) :
00462             participant(&p), next(l)
00463         { }
00464         inline ~ParticipantLink() { delete participant; }
00465         inline Participant* getParticipant() { return participant; }
00466         inline ParticipantLink* getPrev() { return prev; }
00467         inline ParticipantLink* getNext() { return next; }
00468         inline void setPrev(ParticipantLink* l) { prev = l; }
00469         inline void setNext(ParticipantLink* l) { next = l; }
00470         Participant* participant;
00471         ParticipantLink* next, *prev;
00472     };
00473 
00474     void
00475     addParticipant(Participant& part);
00476 
00477     void
00478     removeParticipant(ParticipantLink* part);
00479 
00484     void
00485     findCNAME();
00486 
00488     static const size_t defaultParticipantsNum;
00489     Participant** participants;
00491     ParticipantLink* firstPart, * lastPart;
00492 };
00493 
00503 __EXPORT RTPApplication& defaultApplication();
00504  // sources
00506 
00507 END_NAMESPACE
00508 
00509 #endif //CCXX_RTP_SOURCES_H_
00510