RtspDataSession.cpp

Go to the documentation of this file.
00001 
00034 #include "RtspDataSession.h"
00035 
00036 // LiveMedia
00037 #include "BasicUsageEnvironment.hh"
00038 
00039 // RTVC
00040 #include "RtspHelper.h"
00041 #include "RtvcRtpSink.h"
00042 #include "RtpPacketManager.h"
00043 
00044 RtspDataSession::RtspDataSession(RtpPacketManager* pRtpPacketManager)
00045 :m_pRtpPacketManager(pRtpPacketManager),
00046         m_pRtspClient(NULL),
00047         m_pSession(NULL),
00048         m_bStreamUsingTCP(true),
00049         m_watchVariable(0),
00050         m_sLastError("")
00051 {;}
00052 
00053 bool RtspDataSession::startRetrievingData(const std::string sUrl, int nTimeOut)
00054 {
00055         const char* szUrl = sUrl.c_str();
00056 
00057         // Setup environment
00058         TaskScheduler* pScheduler = BasicTaskScheduler::createNew();
00059         UsageEnvironment* pEnv = BasicUsageEnvironment::createNew(*pScheduler);
00060 
00061         bool bSuccess = true;
00062 
00063         do
00064         {
00065                 // Create client
00066                 m_pRtspClient = RTSPClient::createNew(*pEnv);
00067 
00068                 if (!RtspHelper::createMediaSession(sUrl, m_pRtspClient, &m_pSession, m_sLastError))
00069                 {
00070                         bSuccess = false;
00071                         break;
00072                 }
00073                 
00074                 bSuccess = playStreams(pEnv, m_pSession);
00075                 break;
00076         }while (true);
00077 
00078         // Cleanup RTSP Media session
00079         RtspHelper::shutdownMediaSession(m_pRtspClient, m_pSession);
00080         return bSuccess;
00081 }
00082 
00083 void RtspDataSession::shutdown()
00084 {
00085         // Cleanup RTSP Media session
00086         RtspHelper::shutdownMediaSession(m_pRtspClient, m_pSession);
00087 }
00088 
00089 bool RtspDataSession::playStreams(UsageEnvironment* pEnv, MediaSession* pSession)
00090 {
00091         // Setup and play the streams 
00092         if (!RtspHelper::createRtpSources(pEnv, pSession))
00093         {
00094                 m_sLastError = "Failed to create RTP sources";
00095                 return false;
00096         }
00097 
00098         // Setup RTP source
00099         if (!RtspHelper::setupStreams(m_pRtspClient, pSession, m_bStreamUsingTCP))
00100         {
00101                 m_sLastError = "Failed to setup RTP streams";
00102                 return false;
00103         }
00104 
00105         if (!createReceivers(pSession))
00106         {
00107                 m_sLastError =  "Failed to create RTP receivers: " +  std::string(pEnv->getResultMsg());
00108                 return false;
00109         }
00110 
00111         // Play streams
00112         if (!m_pRtspClient->playMediaSession(*pSession)) 
00113         {
00114                 m_sLastError =  "Failed to start playing session: " +  std::string(pEnv->getResultMsg());
00115                 return false;
00116         }
00117 
00118         // Start the liveMedia eventloop
00119         pEnv->taskScheduler().doEventLoop(&m_watchVariable); // does not return until watch variable is set
00120         return true;
00121 }
00122 
00123 bool RtspDataSession::createReceivers(MediaSession* pSession)
00124 {
00125         // Create and start media sinks for each subsession:
00126         bool madeProgress = False;
00127         MediaSubsessionIterator iter(*m_pSession);
00128         MediaSubsession *pSubsession;
00129         int nSinkID = -1;
00130         while ((pSubsession = iter.next()) != NULL) 
00131         {
00132                 ++nSinkID;
00133 
00134                 // Create our RTPSinks that will receive the data
00135                 if (pSubsession->readSource() == NULL) continue;// was not initiated
00136 
00137                 int nSinkBufferSize = 20000;
00138 
00139                 // Create a new queue in the packet manager
00140                 m_pRtpPacketManager->createQueue(nSinkID);
00141                 // Link the Rtp sink to the ID of the queue in the packet manager
00142                 RtvcRtpSink<RtpPacketManager>* pSink = new RtvcRtpSink<RtpPacketManager>(pSession->envir(), nSinkID, nSinkBufferSize, m_pRtpPacketManager);
00143                 pSubsession->sink = pSink;
00144 
00145                 // TODO Set after playing pointers
00146                 pSubsession->sink->startPlaying(*(pSubsession->readSource()), NULL, NULL);
00147 
00148                 // Also set a handler to be called if a RTCP "BYE" arrives
00149                 // for this subsession:
00150                 if (pSubsession->rtcpInstance() != NULL) 
00151                 {
00152                         pSubsession->rtcpInstance()->setByeHandler(&RtspDataSession::subsessionByeHandler, this/*subsession*/);
00153                 }
00154                 madeProgress = True;
00155         }
00156         return madeProgress;
00157 }
00158 

Generated on Fri Mar 13 14:12:38 2009 for RTVC by  doxygen 1.5.3