00001 00034 #include "RtspSnifferSession.h" 00035 00036 // LiveMedia 00037 #include "BasicUsageEnvironment.hh" 00038 00039 // RTVC 00040 #include "RtspHelper.h" 00041 #include "RtvcRtpSink.h" 00042 00043 RtspSnifferSession::RtspSnifferSession() 00044 :m_pRtspClient(NULL), 00045 m_pSession(NULL), 00046 m_bStreamUsingTCP(true), 00047 m_watchVariable(0), 00048 m_sLastError("") 00049 {;} 00050 00051 RtspSnifferSession::~RtspSnifferSession() 00052 { 00053 if (m_pRtspClient && m_pSession) 00054 // Cleanup RTSP Media session 00055 RtspHelper::shutdownMediaSession(m_pRtspClient, m_pSession); 00056 } 00057 00058 bool RtspSnifferSession::getMediaSessionInformation(const std::string sUrl, int nTimeOut) 00059 { 00060 const char* szUrl = sUrl.c_str(); 00061 00062 // Setup environment 00063 TaskScheduler* pScheduler = BasicTaskScheduler::createNew(); 00064 UsageEnvironment* pEnv = BasicUsageEnvironment::createNew(*pScheduler); 00065 00066 bool bSuccess = true; 00067 00068 do 00069 { 00070 // Create client 00071 m_pRtspClient = RTSPClient::createNew(*pEnv); 00072 00073 if (!RtspHelper::createMediaSession(sUrl, m_pRtspClient, &m_pSession, m_sLastError)) 00074 { 00075 bSuccess = false; 00076 break; 00077 } 00078 00079 // Iterate over session and add subsessions to vectors 00080 MediaSubsession* pSubsession = NULL; 00081 MediaSubsessionIterator it(*m_pSession); 00082 while ((pSubsession = it.next()) != NULL) 00083 { 00084 m_vSubsessions.push_back(pSubsession); 00085 } 00086 // Create our mappings 00087 /*if (!mapLiveMediaToRtvc(m_pSession)) 00088 { 00089 bSuccess = false; 00090 break; 00091 }*/ 00092 00093 //bSuccess = playStreamsIfNecessary(pEnv, m_pSession); 00094 break; 00095 }while (true); 00096 return bSuccess; 00097 } 00098 00099 void RtspSnifferSession::shutdown() 00100 { 00101 // Cleanup RTSP Media session 00102 RtspHelper::shutdownMediaSession(m_pRtspClient, m_pSession); 00103 } 00104 00105 //bool RtspSnifferSession::playStreamsIfNecessary(UsageEnvironment* pEnv, MediaSession* pSession) 00106 //{ 00107 // if (needToPlayStreams(pSession)) 00108 // { 00109 // // Setup and play the streams 00110 // if (!RtspHelper::createRtpSources(pEnv, pSession)) 00111 // { 00112 // m_sLastError = "Failed to create RTP sources"; 00113 // return false; 00114 // } 00115 // 00116 // // Setup RTP source 00117 // if (!RtspHelper::setupStreams(m_pRtspClient, pSession, m_bStreamUsingTCP)) 00118 // { 00119 // m_sLastError = "Failed to setup RTP streams"; 00120 // return false; 00121 // } 00122 // 00123 // if (!createReceivers(pSession)) 00124 // { 00125 // m_sLastError = "Failed to create RTP receivers: " + std::string(pEnv->getResultMsg()); 00126 // return false; 00127 // } 00128 // 00129 // // Play streams 00130 // if (!m_pRtspClient->playMediaSession(*pSession)) 00131 // { 00132 // m_sLastError = "Failed to start playing session: " + std::string(pEnv->getResultMsg()); 00133 // return false; 00134 // } 00135 // 00136 // // Start the liveMedia eventloop 00137 // pEnv->taskScheduler().doEventLoop(&m_watchVariable); // does not return until watch variable is set 00138 // } 00139 // return true; 00140 //} 00141 00142 //bool RtspSnifferSession::createReceivers(MediaSession* pSession) 00143 //{ 00144 // // Create and start media sinks for each subsession: 00145 // bool madeProgress = False; 00146 // MediaSubsessionIterator iter(*m_pSession); 00147 // MediaSubsession *pSubsession; 00148 // 00149 // while ((pSubsession = iter.next()) != NULL) 00150 // { 00151 // // Create our RTPSinks that will receive the data 00152 // if (pSubsession->readSource() == NULL) continue;// was not initiated 00153 // 00154 // if ( strcmp(pSubsession->mediumName(), "video") == 0 && 00155 // ((strcmp(pSubsession->codecName(), "H263-1998") == 0)|| 00156 // (strcmp(pSubsession->codecName(), "H263-2000") == 0)) 00157 // ) 00158 // { 00159 // int nSinkBufferSize = 20000; 00160 // // Create our H263Sniffer 00161 // m_pFormatSniffer = new H263FormatSniffer(&(m_subsessionMap[pSubsession]), m_watchVariable); 00162 // RtvcRtpSink<H263FormatSniffer>* pSink = new RtvcRtpSink<H263FormatSniffer>( 00163 // pSession->envir(), 00164 // 0 /*Not interested in source id in sniffing session*/, 00165 // nSinkBufferSize, 00166 // m_pFormatSniffer 00167 // ); 00168 // pSubsession->sink = pSink; 00169 // } 00170 // else 00171 // { 00172 // // Don't create any sinks since there's no data to sniff 00173 // continue; 00174 // } 00175 // 00176 // // TODO Set after playing pointers 00177 // pSubsession->sink->startPlaying(*(pSubsession->readSource()), NULL/*subsessionAfterPlaying*/, NULL/*subsession*/); 00178 // 00179 // // Also set a handler to be called if a RTCP "BYE" arrives 00180 // // for this subsession: 00181 // if (pSubsession->rtcpInstance() != NULL) 00182 // { 00183 // pSubsession->rtcpInstance()->setByeHandler(&RtspSnifferSession::subsessionByeHandler, this/*subsession*/); 00184 // } 00185 // madeProgress = True; 00186 // } 00187 // return madeProgress; 00188 //} 00189 00190 //bool RtspSnifferSession::mapLiveMediaToRtvc(MediaSession* pSession) 00191 //{ 00192 // // Check if all subsession could get mapped 00193 // return ((nLiveMediaSubsessionCount == m_subsessionMap.size())? true : false); 00194 //} 00195 00196 00197 bool RtspSnifferSession::needToPlayStreams(MediaSession* pSession) 00198 { 00199 MediaSubsessionIterator iter(*pSession); 00200 MediaSubsession *pSubsession = NULL; 00201 bool bFailed = false; 00202 while ((pSubsession = iter.next()) != NULL && !bFailed) 00203 { 00204 const char* szMedium = pSubsession->mediumName(); 00205 const char* szCodec = pSubsession->codecName(); 00206 if ((strcmp(szMedium, "video")==0) && 00207 ((strcmp(szCodec, "H263-1998")==0)||(strcmp(szCodec, "H263-2000")==0)) 00208 ) 00209 { 00210 return true; 00211 } 00212 } 00213 return false; 00214 }