houxiao
2017-07-13 b022b91c0c6fa807424b6c12cc92ac5946838083
RtspFace/live555/testProgs/testRTSPClient.hpp
@@ -20,8 +20,9 @@
// client application.  For a full-featured RTSP client application - with much more functionality, and many options - see
// "openRTSP": http://www.live555.com/openRTSP/
#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
#include <liveMedia/liveMedia.hh>
#include <BasicUsageEnvironment/BasicUsageEnvironment.hh>
#include <groupsock/GroupsockHelper.hh>
#include <iostream>
@@ -29,7 +30,7 @@
// By default, we request that the server stream its data using RTP/UDP.
// If, instead, you want to request that the server stream via RTP-over-TCP, change the following to True:
#define REQUEST_STREAMING_OVER_TCP True
//#define REQUEST_STREAMING_OVER_TCP True
// Even though we're not going to be doing anything with the incoming data, we still need to receive it.
// Define the size of the buffer that we'll use:
@@ -75,8 +76,8 @@
void usage(UsageEnvironment& env, char const* progName)
{
   LOG_DEBUG << "Usage: " << progName << " <rtsp-url-1> ... <rtsp-url-N>" << std::endl;
   LOG_DEBUG << "\t(where each <rtsp-url-i> is a \"rtsp://\" URL)" << std::endl;
   LOG_DEBUG << "Usage: " << progName << " <rtsp-url-1> ... <rtsp-url-N>" << LOG_ENDL;
   LOG_DEBUG << "\t(where each <rtsp-url-i> is a \"rtsp://\" URL)" << LOG_ENDL;
}
char eventLoopWatchVariable = 0;
@@ -158,6 +159,7 @@
public:
   StreamClientState scs;
   const PL_RTSPClient_Config& rtspConfig;
    int desiredPortNum;
};
// Define a data sink (a subclass of "MediaSink") to receive the data for each subsession (i.e., each audio or video 'substream').
@@ -202,12 +204,38 @@
void openURL(UsageEnvironment& env, const PL_RTSPClient_Config& _rtspConfig)
{
   if (!_rtspConfig.receivingInterfaceAddr.empty())
   {
      NetAddressList addresses(_rtspConfig.receivingInterfaceAddr.c_str());
      if (addresses.numAddresses() == 0)
      {
         LOG_ERROR << "Failed to find network address for " << _rtspConfig.receivingInterfaceAddr << LOG_ENDL;
         return;
      }
      else
      {
         ReceivingInterfaceAddr = *(unsigned*)(addresses.firstAddress()->data()); // declared in live555
         LOG_INFO << "Use receiving interface addr " << _rtspConfig.receivingInterfaceAddr << LOG_ENDL;
      }
   }
   if (_rtspConfig.desiredPortNum != 0)
   {
      if (_rtspConfig.desiredPortNum <= 0 || _rtspConfig.desiredPortNum >= 65536 || _rtspConfig.desiredPortNum&1)
      {
         LOG_ERROR << "bad port number: " << _rtspConfig.desiredPortNum << " (must be even, and in the range (0,65536))" << LOG_ENDL;
         return;
      }
      else
         LOG_INFO << "Use desired port num " << _rtspConfig.desiredPortNum << LOG_ENDL;
   }
   // Begin by creating a "RTSPClient" object.  Note that there is a separate "RTSPClient" object for each stream that we wish
   // to receive (even if more than stream uses the same "rtsp://" URL).
   RTSPClient* rtspClient = ourRTSPClient::createNew(env, _rtspConfig);
   if (rtspClient == NULL)
      {
         LOG_ERROR << "Failed to create a RTSP client for URL \"" << _rtspConfig.rtspURL.c_str() << "\": " << env.getResultMsg() << std::endl;
         LOG_ERROR << "Failed to create a RTSP client for URL \"" << _rtspConfig.rtspURL.c_str() << "\": " << env.getResultMsg() << LOG_ENDL;
         return;
      }
@@ -231,25 +259,25 @@
         if (resultCode != 0)
            {
               LOG_WARN << *rtspClient << "Failed to get a SDP description: " << resultString << std::endl;
               LOG_WARN << *rtspClient << "Failed to get a SDP description: " << resultString << LOG_ENDL;
               delete[] resultString;
               break;
            }
         char* const sdpDescription = resultString;
         LOG_INFO << *rtspClient << "Got a SDP description:\n" << sdpDescription << std::endl;
         LOG_INFO << *rtspClient << "Got a SDP description:\n" << sdpDescription << LOG_ENDL;
         // Create a media session object from this SDP description:
         scs.session = MediaSession::createNew(env, sdpDescription);
         delete[] sdpDescription; // because we don't need it anymore
         if (scs.session == NULL)
            {
               LOG_ERROR << *rtspClient << "Failed to create a MediaSession object from the SDP description: " << env.getResultMsg() << std::endl;
               LOG_ERROR << *rtspClient << "Failed to create a MediaSession object from the SDP description: " << env.getResultMsg() << LOG_ENDL;
               break;
            }
         else if (!scs.session->hasSubsessions())
            {
               LOG_WARN << *rtspClient << "This session has no media subsessions (i.e., no \"m=\" lines)" << std::endl;
               LOG_WARN << *rtspClient << "This session has no media subsessions (i.e., no \"m=\" lines)" << LOG_ENDL;
               break;
            }
@@ -269,27 +297,34 @@
void setupNextSubsession(RTSPClient* rtspClient)
{
   UsageEnvironment& env = rtspClient->envir(); // alias
   StreamClientState& scs = ((ourRTSPClient*)rtspClient)->scs; // alias
   ourRTSPClient* _ourRTSPClient = (ourRTSPClient*)rtspClient;
   StreamClientState& scs = _ourRTSPClient->scs; // alias
   scs.subsession = scs.iter->next();
   if (scs.subsession != NULL)
      {
         if (_ourRTSPClient->desiredPortNum != 0)
         {
            scs.subsession->setClientPortNum(_ourRTSPClient->desiredPortNum);
                _ourRTSPClient->desiredPortNum += 2;
         }
         if (!scs.subsession->initiate())
            {
               LOG_ERROR << *rtspClient << "Failed to initiate the \"" << *scs.subsession << "\" subsession: " << env.getResultMsg() << std::endl;
               LOG_ERROR << *rtspClient << "Failed to initiate the \"" << *scs.subsession << "\" subsession: " << env.getResultMsg() << LOG_ENDL;
               setupNextSubsession(rtspClient); // give up on this subsession; go to the next one
            }
         else
            {
               LOG_INFO <<  *rtspClient << "Initiated the \"" << *scs.subsession << "\" subsession (" << std::endl;
               LOG_INFO <<  *rtspClient << "Initiated the \"" << *scs.subsession << "\" subsession (" << LOG_ENDL;
               if (scs.subsession->rtcpIsMuxed())
                  LOG_INFO <<  "client port " << scs.subsession->clientPortNum() << std::endl;
                  LOG_INFO <<  "client port " << scs.subsession->clientPortNum() << LOG_ENDL;
               else
                  LOG_INFO <<  "client ports " << scs.subsession->clientPortNum() << "-" << scs.subsession->clientPortNum()+1 << std::endl;
               LOG_INFO <<  ")" << std::endl;
                  LOG_INFO <<  "client ports " << scs.subsession->clientPortNum() << "-" << scs.subsession->clientPortNum()+1 << LOG_ENDL;
               LOG_INFO <<  ")" << LOG_ENDL;
               // Continue setting up this subsession, by sending a RTSP "SETUP" command:
               rtspClient->sendSetupCommand(*scs.subsession, continueAfterSETUP, False, REQUEST_STREAMING_OVER_TCP);
               rtspClient->sendSetupCommand(*scs.subsession, continueAfterSETUP, False, _ourRTSPClient->rtspConfig.requestStreamingOverTcp);
            }
         return;
      }
@@ -316,44 +351,48 @@
         if (resultCode != 0)
            {
               LOG_ERROR << *rtspClient << "Failed to set up the \"" << *scs.subsession << "\" subsession: " << resultString << std::endl;
               LOG_ERROR << *rtspClient << "Failed to set up the \"" << *scs.subsession << "\" subsession: " << resultString << LOG_ENDL;
               break;
            }
         LOG_INFO << *rtspClient << "Set up the \"" << *scs.subsession << "\" subsession (" << std::endl;
            //#todo temp usage
         std::string sess_mime(scs.subsession->mediumName());
            if (sess_mime != "video")
                break;
         LOG_INFO << *rtspClient << "Set up the \"" << *scs.subsession << "\" subsession (" << LOG_ENDL;
         if (scs.subsession->rtcpIsMuxed())
            {
               LOG_INFO << "client port " << scs.subsession->clientPortNum() << std::endl;
            }
            {
                LOG_INFO << "client port " << scs.subsession->clientPortNum() << LOG_ENDL;
            }
         else
            {
               LOG_INFO << "client ports " << scs.subsession->clientPortNum() << "-" << scs.subsession->clientPortNum()+1 << std::endl;
            }
         LOG_INFO << ")" << std::endl;
            {
                LOG_INFO << "client ports " << scs.subsession->clientPortNum() << "-" << scs.subsession->clientPortNum()+1 << LOG_ENDL;
            }
         LOG_INFO << ")" << LOG_ENDL;
         // Having successfully setup the subsession, create a data sink for it, and call "startPlaying()" on it.
         // (This will prepare the data sink to receive data; the actual flow of data from the client won't start happening until later,
         // after we've sent a RTSP "PLAY" command.)
         scs.subsession->sink = DummySink::createNew(env, ((ourRTSPClient*)rtspClient)->rtspConfig,
                                *scs.subsession, rtspClient->url());
         scs.subsession->sink = DummySink::createNew(env, ((ourRTSPClient*)rtspClient)->rtspConfig, *scs.subsession, rtspClient->url());
         // perhaps use your own custom "MediaSink" subclass instead
         if (scs.subsession->sink == NULL)
            {
               LOG_ERROR << *rtspClient << "Failed to create a data sink for the \"" << *scs.subsession
                   << "\" subsession: " << env.getResultMsg() << std::endl;
               break;
            }
            {
                LOG_ERROR << *rtspClient << "Failed to create a data sink for the \"" << *scs.subsession
                    << "\" subsession: " << env.getResultMsg() << LOG_ENDL;
                break;
            }
         LOG_INFO << *rtspClient << "Created a data sink for the \"" << *scs.subsession << "\" subsession" << std::endl;
         LOG_INFO << *rtspClient << "Created a data sink for the \"" << *scs.subsession << "\" subsession" << LOG_ENDL;
         scs.subsession->miscPtr = rtspClient; // a hack to let subsession handler functions get the "RTSPClient" from the subsession
         scs.subsession->sink->startPlaying(*(scs.subsession->readSource()),
                                            subsessionAfterPlaying, scs.subsession);
         Boolean startPlayingRet = scs.subsession->sink->startPlaying(*(scs.subsession->readSource()), subsessionAfterPlaying, scs.subsession);
         LOG_INFO << "startPlayingRet=" << (bool)startPlayingRet << LOG_ENDL;
         // Also set a handler to be called if a RTCP "BYE" arrives for this subsession:
         if (scs.subsession->rtcpInstance() != NULL)
            {
               scs.subsession->rtcpInstance()->setByeHandler(subsessionByeHandler, scs.subsession);
            }
            {
                scs.subsession->rtcpInstance()->setByeHandler(subsessionByeHandler, scs.subsession);
            }
      }
   while (0);
   delete[] resultString;
@@ -373,7 +412,7 @@
         if (resultCode != 0)
            {
               LOG_ERROR << *rtspClient << "Failed to start playing session: " << resultString << std::endl;
               LOG_ERROR << *rtspClient << "Failed to start playing session: " << resultString << LOG_ENDL;
               break;
            }
@@ -389,12 +428,12 @@
               scs.streamTimerTask = env.taskScheduler().scheduleDelayedTask(uSecsToDelay, (TaskFunc*)streamTimerHandler, rtspClient);
            }
         LOG_INFO << *rtspClient << "Started playing session" << std::endl;
         LOG_INFO << *rtspClient << "Started playing session" << LOG_ENDL;
         if (scs.duration > 0)
            {
               LOG_INFO << " (for up to " << scs.duration << " seconds)" << std::endl;
               LOG_INFO << " (for up to " << scs.duration << " seconds)" << LOG_ENDL;
            }
         LOG_INFO << "..." << std::endl;
         LOG_INFO << "..." << LOG_ENDL;
         success = True;
      }
@@ -438,7 +477,7 @@
   RTSPClient* rtspClient = (RTSPClient*)subsession->miscPtr;
   UsageEnvironment& env = rtspClient->envir(); // alias
   LOG_INFO << *rtspClient << "Received RTCP \"BYE\" on \"" << *subsession << "\" subsession" << std::endl;
   LOG_INFO << *rtspClient << "Received RTCP \"BYE\" on \"" << *subsession << "\" subsession" << LOG_ENDL;
   // Now act as if the subsession had closed:
   subsessionAfterPlaying(subsession);
@@ -491,7 +530,7 @@
            }
      }
   LOG_NOTICE << *rtspClient << "Closing the stream." << std::endl;
   LOG_NOTICE << *rtspClient << "Closing the stream." << LOG_ENDL;
   Medium::close(rtspClient);
   // Note that this will also cause this stream's "StreamClientState" structure to get reclaimed.
@@ -500,7 +539,8 @@
         // The final stream has ended, so exit the application now.
         // (Of course, if you're embedding this code into your own application, you might want to comment this out,
         // and replace it with "eventLoopWatchVariable = 1;", so that we leave the LIVE555 event loop, and continue running "main()".)
         exit(exitCode);
         //exit(exitCode);
         eventLoopWatchVariable = 1;
      }
}
@@ -514,7 +554,7 @@
ourRTSPClient::ourRTSPClient(UsageEnvironment& env, const PL_RTSPClient_Config& _rtspConfig)
   : RTSPClient(env, _rtspConfig.rtspURL.c_str(), _rtspConfig.verbosityLevel, _rtspConfig.progName.c_str(),
                _rtspConfig.tunnelOverHTTPPortNum, -1), rtspConfig(_rtspConfig)
                _rtspConfig.tunnelOverHTTPPortNum, -1), scs(), rtspConfig(_rtspConfig), desiredPortNum(_rtspConfig.desiredPortNum)
{
}
@@ -526,7 +566,7 @@
// Implementation of "StreamClientState":
StreamClientState::StreamClientState()
   : iter(NULL), session(NULL), subsession(NULL), streamTimerTask(NULL), duration(0.0)
   : iter(NULL), session(NULL), subsession(NULL), streamTimerTask(), duration(0.0)
{
}
@@ -558,20 +598,25 @@
   // ffmpeg need AUX header
   if (rtspConfig.aux)
      {
         fReceiveBuffer[0]=0x00;
         fReceiveBuffer[1]=0x00;
         fReceiveBuffer[2]=0x00;
         fReceiveBuffer[3]=0x01;
      }
   {
      fReceiveBuffer[0]=0x00;
      fReceiveBuffer[1]=0x00;
      fReceiveBuffer[2]=0x00;
      fReceiveBuffer[3]=0x01;
   }
   RtspClientParam param;
   //parse sdp
   const char* strSDP = fSubsession.savedSDPLines();
   rtsp_client_sdp_callback(rtspConfig.args, strSDP);
   const char* strFmtp = fSubsession.fmtp_spropparametersets();
   rtsp_client_fmtp_callback(rtspConfig.args, strFmtp);
   //std::cout << strFmtp << std::endl;
   param.sdp = fSubsession.savedSDPLines();
   param.fmtp = fSubsession.fmtp_spropparametersets();
   param.width = fSubsession.videoWidth();
   param.height = fSubsession.videoHeight();
   param.fps = fSubsession.videoFPS();
   param.codecName = fSubsession.codecName();
   param.bandwidth = fSubsession.bandwidth();
   rtsp_client_set_param_callback(rtspConfig.args, param);
}
DummySink::~DummySink()
@@ -602,20 +647,20 @@
   // We've just received a frame of data.  (Optionally) print out information about it:
#ifdef DEBUG_PRINT_EACH_RECEIVED_FRAME
   if (fStreamId != NULL)
      LOG_DEBUG << "Stream \"" << fStreamId << "\"; " << std::endl;
   LOG_DEBUG << "\t" << fSubsession.mediumName() << "/" << fSubsession.codecName() << ":\tReceived " << frameSize << " bytes" << std::endl;
      LOG_DEBUG << "Stream \"" << fStreamId << "\"; " << LOG_ENDL;
   LOG_DEBUG << "\t" << fSubsession.mediumName() << "/" << fSubsession.codecName() << ":\tReceived " << frameSize << " bytes" << LOG_ENDL;
   if (numTruncatedBytes > 0)
      LOG_DEBUG << " (with " << numTruncatedBytes << " bytes truncated)" << std::endl;
      LOG_DEBUG << " (with " << numTruncatedBytes << " bytes truncated)" << LOG_ENDL;
   
   char uSecsStr[6+1]; // used to output the 'microseconds' part of the presentation time
   sprintf(uSecsStr, "%06u", (unsigned)presentationTime.tv_usec);
   LOG_DEBUG << "\tPresentation time: " << (int)presentationTime.tv_sec << "." << uSecsStr << std::endl;
   LOG_DEBUG << "\tPresentation time: " << (int)presentationTime.tv_sec << "." << uSecsStr << LOG_ENDL;
   if (fSubsession.rtpSource() != NULL && !fSubsession.rtpSource()->hasBeenSynchronizedUsingRTCP())
      {
         LOG_DEBUG << "\tPTS not RTCP-synchronized" << std::endl; // mark the debugging output to indicate that this presentation time is not RTCP-synchronized
         LOG_DEBUG << "\tPTS not RTCP-synchronized" << LOG_ENDL; // mark the debugging output to indicate that this presentation time is not RTCP-synchronized
      }
#ifdef DEBUG_PRINT_NPT
   LOG_DEBUG << "\tNPT: " << fSubsession.getNormalPlayTime(presentationTime) << std::endl;
   LOG_DEBUG << "\tNPT: " << fSubsession.getNormalPlayTime(presentationTime) << LOG_ENDL;
#endif
#endif