SemiProxySession.hpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: SemiProxySession.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 /*********************************************************************** 00045 * Video Session Migration System, 2004 * 00046 * Author: Klaus Schoeffmann * 00047 ************************************************************************/ 00048 00049 00050 #ifndef __SemiProxySession_hpp__ 00051 #define __SemiProxySession_hpp__ 00052 00053 #ifdef WIN32 00054 #include <winsock2.h> 00055 #else 00056 #include <unistd.h> 00057 #include <time.h> 00058 #include <sys/time.h> 00059 #include <sys/socket.h> 00060 #include <netdb.h> 00061 #include <netinet/in.h> 00062 #include <unistd.h> 00063 #include <arpa/inet.h> 00064 #endif 00065 00066 #include <string> 00067 //#include <unistd.h> 00068 #include <sys/types.h> 00069 #include <sstream> 00070 00071 #include "SemiGlobals.hpp" 00072 #include "net/Session.hpp" 00073 #include "net/RTSP.hpp" 00074 #include "global.hpp" 00075 00076 #include "metadata/MP21.hpp" 00077 #include "metadata/TerminalCapabilities.hpp" 00078 00079 #include "SemiProxy.hpp" 00080 #include "SSession.hpp" 00081 #include "URIParameters.hpp" 00082 #include "TimeMeasurement.hpp" 00083 #include "Profile.hpp" 00084 #include "TrackList.hpp" 00085 00086 class SemiProxy; 00087 00088 enum PlayerInformation {PI_UNKOWN, PI_MUVIPLAYER, PI_QUICKTIME, PI_REALPLAYER, PI_PVPLAYER}; 00089 00097 class SemiProxySession : public Session { 00098 00099 //exceptions... 00100 class GetHostNameProblem {}; 00101 class ParamEndProblem {}; 00102 00103 public: 00105 SemiProxySession(int fdclient, struct sockaddr_in* clientsock, SemiProxy *sp); 00106 00108 ~SemiProxySession(); 00109 00111 static SemiProxySession* getSemiProxySession(uint RtspSessionId); 00112 00115 void run(); 00116 00122 void parseResponseFromServer(char *buffer, int len); 00123 00129 void forwardToServer(char *buffer, int len); 00130 00133 void forwardToClient(char *buffer, int len); 00134 00136 void handleOptionsResponse(string &buf, int &bytesRead); 00137 00139 void handleDescribeResponse(string &buf, int &bytesRead); 00140 00142 void handleSetupResponse(string &buf, int &bytesRead); 00143 00145 void handlePlayResponse(string &buf, int &bytesRead); 00146 00148 void handlePauseResponse(string &buf, int &bytesRead); 00149 00151 void handleTeardownResponse(string &buf, int &bytesRead); 00152 00154 bool getOptions(const Url* fileName, const char* remaining); 00155 00157 bool setOptions(const Url* fileName, const char* remaining); 00158 00160 bool options(const Url* fileName, const char* remaining); 00161 00163 bool connect(const Url* fileName, const char* remaining); 00164 00168 bool setup(const Url* fileName, const char* remaining); 00169 00176 bool play(const Url* fileName, const char* remaining); 00177 00179 bool pause(const Url* fileName, const char* remaining); 00180 00182 bool tearDown(int sessionKey, bool immediate=false, const Url* fileName=NULL, const char* remaining=NULL); 00183 00185 bool containsSessionID(uint SessionID); 00186 00188 //uint getRtspSessionID(); 00189 00191 uint getServerTimeMs(); 00192 00194 uint getClientTimeMs(); 00195 00196 void testClientOptions(); 00197 00200 uint requestServerTimeMs(); 00201 00204 uint requestClientTimeMs(); 00205 00207 void sendTeardownToClient(); 00208 00211 void closeSession(); 00212 00215 void removeSession(); 00216 00220 void setUrl(const Url *uri, bool makeExactMatch = true); 00221 00223 void setStateClosed(); 00224 00225 PlayerInformation getPlayerInfo() { return playerInfo; }; 00226 00227 private: 00228 00229 PlayerInformation playerInfo; 00230 00232 void initSemiProxySession(); 00233 00235 void resetFlags(); 00236 00238 static vector<SemiProxySession *> sessionList; 00239 00241 int fdserver, fdclient; 00242 00244 class SemiProxy *sp; 00245 00247 struct sockaddr_in* clientsock; 00248 00250 char *buffer; 00251 00253 int bytesRead; 00254 00256 uint lastClientCSeq; 00257 00259 uint lastServerCSeq; 00260 00262 ulong prevDuration; 00263 00265 bool noTermCapsFound; 00266 00267 // --- variables used for active sessions --- 00269 uint lastClientRtspMsg; 00270 00272 URIParameters params; 00273 00275 uint RtspSessionID; 00276 00278 uint lastCRtspSessionID; 00279 00281 TrackList tracks; 00282 00283 00285 char *SessionURI; 00287 SSession *asess; 00288 00290 TimeMeasurement *timemsmnt; 00291 00293 char *requestedHostname; 00294 00296 uint receivedServerPosition; 00297 00299 uint receivedClientPosition; 00300 00302 bool sendServerPositionRequest; 00303 00305 bool sendClientPositionRequest; 00306 00308 bool sessionHasBeenPaused; 00309 00310 00311 void createRtspServerSocket(const char *host, int port); 00312 00314 void readFromSocket(int fdsock, struct timeval &tv, fd_set &rfds); 00315 00318 bool checkServerConnection(const Url* fileName); 00319 00322 char* extractStrParameter(string &strbuf, char *pname, bool removeIt = false); 00323 00326 int extractParameter(string &strbuf, char *pname, bool removeIt = false); 00327 00330 void extractCSeq(char *buffer, uint &lastCSeq); 00331 00333 ulong extractDuration(string &strbuf); 00334 00336 ulong extractRangeFrom(string &strbuf); 00338 ulong extractPrebuffer(string &strbuf); 00339 00341 uint getContentLength(string strbuf); 00342 00344 void replaceCSeq(string &str, uint newCSeq); 00345 00348 void replaceRange(string &strbuf, char *newVal, bool replaceOnlyFromValue = true); 00349 00350 //void replace(string &str, char* val, char* newval, bool all = false); 00352 void changeContentLength(string &strbuf, uint newVal); 00353 00358 bool checkForValidAddress(char *buffer, int &bytesRead); 00359 00362 uint getSmallerStringPos(string str, char *needle1, char *needle2, uint from = 0); 00363 00365 string createTerminalCapabilities(Profile *profile); 00366 00367 }; 00368 00369 #endif