00001
00034 #include "RtspDataSession.h"
00035
00036
00037 #include "BasicUsageEnvironment.hh"
00038
00039
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
00058 TaskScheduler* pScheduler = BasicTaskScheduler::createNew();
00059 UsageEnvironment* pEnv = BasicUsageEnvironment::createNew(*pScheduler);
00060
00061 bool bSuccess = true;
00062
00063 do
00064 {
00065
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
00079 RtspHelper::shutdownMediaSession(m_pRtspClient, m_pSession);
00080 return bSuccess;
00081 }
00082
00083 void RtspDataSession::shutdown()
00084 {
00085
00086 RtspHelper::shutdownMediaSession(m_pRtspClient, m_pSession);
00087 }
00088
00089 bool RtspDataSession::playStreams(UsageEnvironment* pEnv, MediaSession* pSession)
00090 {
00091
00092 if (!RtspHelper::createRtpSources(pEnv, pSession))
00093 {
00094 m_sLastError = "Failed to create RTP sources";
00095 return false;
00096 }
00097
00098
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
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
00119 pEnv->taskScheduler().doEventLoop(&m_watchVariable);
00120 return true;
00121 }
00122
00123 bool RtspDataSession::createReceivers(MediaSession* pSession)
00124 {
00125
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
00135 if (pSubsession->readSource() == NULL) continue;
00136
00137 int nSinkBufferSize = 20000;
00138
00139
00140 m_pRtpPacketManager->createQueue(nSinkID);
00141
00142 RtvcRtpSink<RtpPacketManager>* pSink = new RtvcRtpSink<RtpPacketManager>(pSession->envir(), nSinkID, nSinkBufferSize, m_pRtpPacketManager);
00143 pSubsession->sink = pSink;
00144
00145
00146 pSubsession->sink->startPlaying(*(pSubsession->readSource()), NULL, NULL);
00147
00148
00149
00150 if (pSubsession->rtcpInstance() != NULL)
00151 {
00152 pSubsession->rtcpInstance()->setByeHandler(&RtspDataSession::subsessionByeHandler, this);
00153 }
00154 madeProgress = True;
00155 }
00156 return madeProgress;
00157 }
00158