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