Session.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: Session.cpp * 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 #include "Session.hpp" 00045 #include "ContainerInfo.hpp" 00046 #include "Adaptor.hpp" 00047 #include "RTSP.hpp" 00048 #include "DataChannel.hpp" 00049 00050 #include "../io/Rtp.hpp" 00051 #include "../metadata/TerminalCapabilities.hpp" 00052 00053 Session::Session() 00054 { 00055 globalTimer=NULL; 00056 gtRefCount=NULL; 00057 gtSharedAdapt=NULL; 00058 00059 sessionControlChannel=-1; 00060 state=SESSION_ERR; 00061 termCap=NULL; 00062 userPref=NULL; 00063 mp4Stream=NULL; 00064 suggestedAdaptor=NULL; 00065 client=NULL; 00066 prot=NULL; 00067 00068 url=NULL; 00069 serverID=NULL; 00070 canAdmitRequest=false;; 00071 }; 00072 00073 Session::~Session() { 00074 } 00075 00077 bool Session::sendResponse(const char *str,int bytes) 00078 { 00079 assert(str && bytes>0); 00080 00081 if(!str || bytes<=0) 00082 return false; 00083 00084 int n=0; 00085 int total=0; 00086 bool ret=false; 00087 00088 dprintf_full("Session::sendResponse: Writing %i bytes: X%sX\r\n",bytes,str); 00089 00090 while(bytes>0) { 00091 #ifdef WIN32 00092 n=send(sessionControlChannel,str+total, bytes,0); 00093 #else 00094 n=send(sessionControlChannel,str+total, bytes,MSG_NOSIGNAL); 00095 #endif 00096 if(n==-1) 00097 break; 00098 bytes-=n; 00099 total+=n; 00100 } 00101 if(bytes==0) 00102 ret=true; 00103 dprintf_full("Session::sendResponse exit, sending %s\r\n",ret?"was ok":"FAILED!"); 00104 return ret; 00105 }; 00106 00113 int Session::readRequest(char* str, const int MAX_BYTES) 00114 { 00115 assert(str && MAX_BYTES>0); 00116 00117 if(!str || MAX_BYTES<=0) 00118 return -1; 00119 00120 int n=0; 00121 int total=0; 00122 memset(str,0,MAX_BYTES); 00123 00124 do { 00125 // n == 0 if connection closed 00126 // n < 0 if error 00127 n=recv(sessionControlChannel, str+total, MAX_BYTES-1-total,0); 00128 dprintf_full("Session::readRequest: partial %i total %i\r\n",n,total); 00129 dprintf_full("Session::readRequest:X%sX\r\n",str+total); 00130 if(n<=0) 00131 break; 00132 total+=n; 00133 } 00134 while(n>0 && !(total>3 && str[total-4]=='\r' && str[total-3]=='\n' && 00135 str[total-2]=='\r' && str[total-1]=='\n')); 00136 00137 dprintf_full("Session::readRequest: %i\r\n",total);fflush(stdout); 00138 return total; 00139 }; 00140 00141 void Session::setTerminalCapabilities(TerminalCapabilities* tc) { 00142 termCap=tc; 00143 }; 00144 00145 const TerminalCapabilities* Session::getTerminalCapabilities() const { 00146 return termCap; 00147 }; 00148 00149 int Session::getControlSocket() const { 00150 return sessionControlChannel; 00151 }; 00152 00153 ContainerInfo* Session::getContainerInfo() const { 00154 return mp4Stream; 00155 }; 00156 00158 bool Session::setRtxInfoForES(u32 esId, rtx_info* rtx) 00159 { 00160 const ESInfo* es=this->mp4Stream->getES(esId); 00161 if(!es) { 00162 dprintf("ClientSession::setRtxInfoForES: invalid esId as input\r\n"); 00163 return false; 00164 } 00165 00166 rtx_group* grp=new rtx_group(ACK,3000); 00167 grp->es=es; 00168 grp->rtx=rtx; 00169 rtx->mId=rtxInfo.size()+1; 00170 grp->midESInfo=esId; 00171 rtxInfo.push_back(grp); 00172 return true; 00173 }; 00174 00175 00177 rtx_group* Session::getRtxGroup(u32 esId) 00178 { 00179 00180 for( list<rtx_group*>::iterator li=rtxInfo.begin(); 00181 li!=rtxInfo.end(); li++) { 00182 if(esId==(*li)->es->getStreamId()) 00183 return *li; 00184 } 00185 return NULL; 00186 00187 } 00188 00189 list < rtx_group* >* Session::getAllRtxGroups() { 00190 return &rtxInfo; 00191 }; 00192 00193 KnownProtocols Session::determineProtocol(const char *request) 00194 { 00195 if (strstr(request, "RTSP/1.0")) { 00196 assert(!prot); 00197 prot = new RTSP(); 00198 return PROTO_RTSP; 00199 } 00200 if (strstr(request, "HTTP/1.")) { 00201 assert(!prot); 00202 // prot=new HTTP(); 00203 return PROTO_HTTP; 00204 } 00205 return PROTO_UNKNOWN; 00206 } 00207 00208 void Session::setDataChannel(DataChannel * dc) 00209 { 00210 channels.push_back(dc); 00211 }; 00212 00213 00214 DataChannel *Session::getDataChannel(int esId) 00215 { 00216 list < DataChannel * >::const_iterator dcI; 00217 if (!channels.empty()) { 00218 dcI = channels.begin(); 00219 while (dcI != channels.end()) { 00220 if ((*dcI)->getESInfo()->getStreamId() == (u32) esId) { 00221 return (*dcI); 00222 } 00223 dcI++; 00224 } 00225 } 00226 return NULL; 00227 }; 00228 00229 const list < DataChannel * >& Session::getDataChannels() const { 00230 return channels; 00231 }; 00232 00233 Adaptor* Session::getSuggestedAdaptor() { 00234 if(suggestedAdaptor) 00235 return suggestedAdaptor->clone(); 00236 return NULL; 00237 }; 00238 00239 const sockaddr_in* Session::getDataSinkInfo() const { 00240 return client; 00241 }; 00242 00243 const Url* Session::getUrl() const { 00244 return url; 00245 };