Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 2001,2002,2006 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 | |
| 38 | /** |
| 39 | * @file pool.h |
| 40 | * @short Pools of RTP sessions. |
| 41 | **/ |
| 42 | |
| 43 | #ifndef CCXX_RTP_POOL_H |
| 44 | #define CCXX_RTP_POOL_H |
| 45 | |
| 46 | #include <list> |
| 47 | #include <ccrtp/rtp.h> |
| 48 | |
| 49 | #ifdef CCXX_NAMESPACES |
| 50 | namespace ost { |
| 51 | using std::list; |
| 52 | #endif |
| 53 | |
| 54 | typedef TRTPSessionBase<> RTPSessionBase; |
| 55 | |
| 56 | class RTPSessionBaseHandler |
| 57 | { |
| 58 | public: |
| 59 | inline microtimeout_t getSchedulingTimeout(RTPSessionBase& s) |
| 60 | { return s.getSchedulingTimeout(); } |
| 61 | |
| 62 | inline timeval getRTCPCheckInterval(RTPSessionBase& s) |
| 63 | { return s.getRTCPCheckInterval(); } |
| 64 | |
| 65 | size_t |
| 66 | takeInDataPacket(RTPSessionBase& s) |
| 67 | { return s.takeInDataPacket(); } |
| 68 | |
| 69 | size_t |
| 70 | dispatchDataPacket(RTPSessionBase& s) |
| 71 | { return s.dispatchDataPacket(); } |
| 72 | |
| 73 | void |
| 74 | controlReceptionService(RTPSessionBase& s) |
| 75 | { s.controlReceptionService(); } |
| 76 | |
| 77 | void |
| 78 | controlTransmissionService(RTPSessionBase& s) |
| 79 | { s.controlTransmissionService(); } |
| 80 | |
| 81 | inline SOCKET getDataRecvSocket(RTPSessionBase& s) const |
| 82 | { return s.getDataRecvSocket(); } |
| 83 | |
| 84 | inline SOCKET getControlRecvSocket(RTPSessionBase& s) const |
| 85 | { return s.getControlRecvSocket(); } |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * Class for tracking session status. Session pools arrange sessions |
| 90 | * in lists of SessionListElement objects. |
| 91 | * |
| 92 | * @author Jorgen Terner |
| 93 | **/ |
| 94 | |
| 95 | class SessionListElement { |
| 96 | private: |
| 97 | RTPSessionBase* elem; |
| 98 | bool cleared; |
| 99 | |
| 100 | public: |
| 101 | SessionListElement(RTPSessionBase* e); |
| 102 | void clear(); |
| 103 | bool isCleared(); |
| 104 | RTPSessionBase* get(); |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | inline SessionListElement::SessionListElement(RTPSessionBase* e) |
| 109 | : elem(e), cleared(false) { |
| 110 | } |
| 111 | |
| 112 | inline void SessionListElement::clear() { |
| 113 | cleared = true; |
| 114 | delete elem; |
| 115 | elem = 0; |
| 116 | } |
| 117 | |
| 118 | inline bool SessionListElement::isCleared() { |
| 119 | return cleared; |
| 120 | } |
| 121 | |
| 122 | inline RTPSessionBase* SessionListElement::get() { |
| 123 | return elem; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * std equality for SessionListElement objects. |
| 128 | * |
| 129 | * @author Jorgen Terner |
| 130 | **/ |
| 131 | class PredEquals |
| 132 | { |
| 133 | protected: |
| 134 | RTPSessionBase* elem; |
| 135 | public: |
| 136 | PredEquals(RTPSessionBase* e) : elem(e) {} |
| 137 | |
| 138 | bool operator() (SessionListElement* e) |
| 139 | { |
| 140 | return e->get() == elem; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * This class is a base class for classes that define a group of RTP |
| 146 | * sessions that will be served by one or more execution |
| 147 | * threads. Derived classes are responsible for serving each RTP |
| 148 | * session with a thread at least. |
| 149 | * |
| 150 | * In order to use the RTP session "pool" you just have to build |
| 151 | * RTPSessionBase objects for each RTP session (instead of RTPSession |
| 152 | * objects). Then, add the RTPSessionBase objects to an RTP session |
| 153 | * "pool" and call startRunning() method of the session pool. |
| 154 | * |
| 155 | * @author Federico Montesino Pouzols <fedemp@altern.org> |
| 156 | **/ |
| 157 | class __EXPORT RTPSessionPool: public RTPSessionBaseHandler |
| 158 | { |
| 159 | public: |
| 160 | RTPSessionPool(); |
| 161 | |
| 162 | inline virtual ~RTPSessionPool() |
| 163 | { } |
| 164 | |
| 165 | bool |
| 166 | addSession(RTPSessionBase& session); |
| 167 | |
| 168 | bool |
| 169 | removeSession(RTPSessionBase& session); |
| 170 | |
| 171 | size_t |
| 172 | getPoolLength() const; |
| 173 | |
| 174 | virtual void startRunning() = 0; |
| 175 | |
| 176 | inline bool isActive() |
| 177 | { return poolActive; } |
| 178 | |
| 179 | protected: |
| 180 | inline void setActive() |
| 181 | { poolActive = true; } |
| 182 | |
| 183 | inline timeval getPoolTimeout() |
| 184 | { return poolTimeout; } |
| 185 | |
| 186 | inline void setPoolTimeout(int sec, int usec) |
| 187 | { poolTimeout.tv_sec = sec; poolTimeout.tv_usec = usec; } |
| 188 | |
| 189 | inline void setPoolTimeout(struct timeval to) |
| 190 | { poolTimeout = to; } |
| 191 | |
| 192 | std::list<SessionListElement*> sessionList; |
| 193 | typedef std::list<SessionListElement*>::iterator PoolIterator; |
| 194 | |
| 195 | mutable ThreadLock poolLock; |
| 196 | |
| 197 | #ifndef WIN32 |
| 198 | fd_set recvSocketSet; |
| 199 | SOCKET highestSocket; // highest socket number + 1 |
| 200 | #endif |
| 201 | |
| 202 | private: |
| 203 | timeval poolTimeout; |
| 204 | mutable bool poolActive; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | class __EXPORT SingleRTPSessionPool : |
| 209 | public RTPSessionPool, |
| 210 | public Thread |
| 211 | { |
| 212 | public: |
| 213 | /** |
| 214 | * @param pri optional thread priority value. |
| 215 | **/ |
| 216 | SingleRTPSessionPool(int pri = 0) : |
| 217 | RTPSessionPool(), |
| 218 | Thread(pri) |
| 219 | { } |
| 220 | |
| 221 | ~SingleRTPSessionPool() |
| 222 | { } |
| 223 | |
| 224 | void startRunning() |
| 225 | { setActive(); Thread::start(); } |
| 226 | |
| 227 | protected: |
| 228 | /** |
| 229 | * Runnable method for the thread. This thread serves all the |
| 230 | * RTP sessions.added to this pool. |
| 231 | */ |
| 232 | void run(); |
| 233 | }; |
| 234 | |
| 235 | #ifdef CCXX_NAMESPACES |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | #endif //CCXX_RTP_POOL_H |
| 240 | |
| 241 | /** EMACS ** |
| 242 | * Local variables: |
| 243 | * mode: c++ |
| 244 | * c-basic-offset: 8 |
| 245 | * End: |
| 246 | */ |