houxiao
2016-12-26 993a7850d6cffb341fefabb68fbb97168c4a461c
RtspFace/live555/testProgs/testRTSPClient.hpp
@@ -25,6 +25,19 @@
#include <iostream>
#define RTSP_CLIENT_VERBOSITY_LEVEL 1 // by default, print verbose output from each "RTSPClient"
// 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 False
// 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:
#define DUMMY_SINK_RECEIVE_BUFFER_SIZE 1920*1080*3
// If you don't want to see debugging output for each received frame, then comment out the following line:
#define DEBUG_PRINT_EACH_RECEIVED_FRAME 1
// Forward function definitions:
// RTSP 'response handlers':
@@ -39,7 +52,7 @@
  // called at the end of a stream's expected duration (if the stream has not already signaled its end using a RTCP "BYE")
// The main streaming routine (for each "rtsp://" URL):
void openURL(UsageEnvironment& env, void* args, char const* progName, char const* rtspURL);
void openURL(UsageEnvironment& env, const RTSPConfig& _rtspConfig);
// Used to iterate through each stream's 'subsessions', setting up each one:
void setupNextSubsession(RTSPClient* rtspClient);
@@ -75,9 +88,18 @@
    return 1;
  }
   RTSPConfig rtspConfig;
   rtspConfig.progName = argv[0];
   rtspConfig.rtspURL = "";
   rtspConfig.aux = false;
   rtspConfig.verbosityLevel = RTSP_CLIENT_VERBOSITY_LEVEL;
   rtspConfig.tunnelOverHTTPPortNum = 0;
   rtspConfig.args = nullptr;
  // There are argc-1 URLs: argv[1] through argv[argc-1].  Open and start streaming each one:
  for (int i = 1; i <= argc-1; ++i) {
    openURL(*env, NULL, argv[0], argv[i]);
   rtspConfig.rtspURL = argv[i];
   openURL(*env, rtspConfig);
  }
  // All subsequent activity takes place within the event loop:
@@ -117,20 +139,16 @@
class ourRTSPClient: public RTSPClient {
public:
  static ourRTSPClient* createNew(UsageEnvironment& env, char const* rtspURL,
              int verbosityLevel = 0,
              char const* applicationName = NULL,
              portNumBits tunnelOverHTTPPortNum = 0);
  static ourRTSPClient* createNew(UsageEnvironment& env, const RTSPConfig& _rtspConfig);
protected:
  ourRTSPClient(UsageEnvironment& env, char const* rtspURL,
      int verbosityLevel, char const* applicationName, portNumBits tunnelOverHTTPPortNum);
  ourRTSPClient(UsageEnvironment& env, const RTSPConfig& _rtspConfig);
    // called only by createNew();
  virtual ~ourRTSPClient();
public:
  StreamClientState scs;
  void* args;
  const RTSPConfig& rtspConfig;
};
// Define a data sink (a subclass of "MediaSink") to receive the data for each subsession (i.e., each audio or video 'substream').
@@ -142,12 +160,12 @@
{
public:
   static DummySink* createNew(UsageEnvironment& env, 
               void* _args,
              const RTSPConfig& _rtspConfig,
              MediaSubsession& subsession, // identifies the kind of data that's being received
              char const* streamId = NULL); // identifies the stream itself (optional)
private:
   DummySink(UsageEnvironment& env, void* _args, MediaSubsession& subsession, char const* streamId);
   DummySink(UsageEnvironment& env, const RTSPConfig& _rtspConfig, MediaSubsession& subsession, char const* streamId);
   // called only by "createNew()"
   virtual ~DummySink();
@@ -159,7 +177,7 @@
          struct timeval presentationTime, unsigned durationInMicroseconds);
public:
   void* args;
   const RTSPConfig& rtspConfig;
private:
   // redefined virtual functions:
@@ -171,20 +189,18 @@
   char* fStreamId;
};
#define RTSP_CLIENT_VERBOSITY_LEVEL 1 // by default, print verbose output from each "RTSPClient"
static unsigned rtspClientCount = 0; // Counts how many streams (i.e., "RTSPClient"s) are currently in use.
void openURL(UsageEnvironment& env, void* args, char const* progName, char const* rtspURL) {
void openURL(UsageEnvironment& env, const RTSPConfig& _rtspConfig)
{
  // 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, rtspURL, RTSP_CLIENT_VERBOSITY_LEVEL, progName);
  if (rtspClient == NULL) {
    env << "Failed to create a RTSP client for URL \"" << rtspURL << "\": " << env.getResultMsg() << "\n";
   RTSPClient* rtspClient = ourRTSPClient::createNew(env, _rtspConfig);
   if (rtspClient == NULL)
   {
      env << "Failed to create a RTSP client for URL \"" << _rtspConfig.rtspURL.c_str() << "\": " << env.getResultMsg() << "\n";
    return;
  }
  ((ourRTSPClient*)rtspClient)->args = args;
  ++rtspClientCount;
@@ -197,12 +213,15 @@
// Implementation of the RTSP 'response handlers':
void continueAfterDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString) {
  do {
void continueAfterDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString)
{
   do
   {
    UsageEnvironment& env = rtspClient->envir(); // alias
    StreamClientState& scs = ((ourRTSPClient*)rtspClient)->scs; // alias
    if (resultCode != 0) {
      if (resultCode != 0)
      {
      env << *rtspClient << "Failed to get a SDP description: " << resultString << "\n";
      delete[] resultString;
      break;
@@ -214,10 +233,13 @@
    // 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) {
      if (scs.session == NULL)
      {
      env << *rtspClient << "Failed to create a MediaSession object from the SDP description: " << env.getResultMsg() << "\n";
      break;
    } else if (!scs.session->hasSubsessions()) {
      }
      else if (!scs.session->hasSubsessions())
      {
      env << *rtspClient << "This session has no media subsessions (i.e., no \"m=\" lines)\n";
      break;
    }
@@ -234,11 +256,8 @@
  shutdownStream(rtspClient);
}
// 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 False
void setupNextSubsession(RTSPClient* rtspClient) {
void setupNextSubsession(RTSPClient* rtspClient)
{
  UsageEnvironment& env = rtspClient->envir(); // alias
  StreamClientState& scs = ((ourRTSPClient*)rtspClient)->scs; // alias
  
@@ -294,8 +313,7 @@
    // (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.)
   DummySink* mySink;
    scs.subsession->sink = mySink = DummySink::createNew(env, ((ourRTSPClient*)rtspClient)->args,
    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) {
@@ -446,15 +464,14 @@
// Implementation of "ourRTSPClient":
ourRTSPClient* ourRTSPClient::createNew(UsageEnvironment& env, char const* rtspURL,
               int verbosityLevel, char const* applicationName, portNumBits tunnelOverHTTPPortNum) {
  return new ourRTSPClient(env, rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum);
ourRTSPClient* ourRTSPClient::createNew(UsageEnvironment& env, const RTSPConfig& _rtspConfig)
{
  return new ourRTSPClient(env, _rtspConfig);
}
ourRTSPClient::ourRTSPClient(UsageEnvironment& env, char const* rtspURL,
              int verbosityLevel, char const* applicationName, portNumBits tunnelOverHTTPPortNum)
  : RTSPClient(env,rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum, -1),
   args(nullptr)
ourRTSPClient::ourRTSPClient(UsageEnvironment& env, const RTSPConfig& _rtspConfig)
  : RTSPClient(env, _rtspConfig.rtspURL.c_str(), _rtspConfig.verbosityLevel, _rtspConfig.progName.c_str(),
            _rtspConfig.tunnelOverHTTPPortNum, -1), rtspConfig(_rtspConfig)
{
}
@@ -481,30 +498,29 @@
// Implementation of "DummySink":
// 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:
#define DUMMY_SINK_RECEIVE_BUFFER_SIZE 1920*1080*3
DummySink* DummySink::createNew(UsageEnvironment& env, void* _args, MediaSubsession& subsession, char const* streamId)
DummySink* DummySink::createNew(UsageEnvironment& env, const RTSPConfig& _rtspConfig, MediaSubsession& subsession, char const* streamId)
{
  return new DummySink(env, _args, subsession, streamId);
  return new DummySink(env, _rtspConfig, subsession, streamId);
}
DummySink::DummySink(UsageEnvironment& env, void* _args, MediaSubsession& subsession, char const* streamId)
  : MediaSink(env), args(_args), fSubsession(subsession)
DummySink::DummySink(UsageEnvironment& env, const RTSPConfig& _rtspConfig, MediaSubsession& subsession, char const* streamId)
  : MediaSink(env), rtspConfig(_rtspConfig), fSubsession(subsession)
{
   fStreamId = strDup(streamId);
   fReceiveBuffer = new u_int8_t[DUMMY_SINK_RECEIVE_BUFFER_SIZE];
   // ffmpeg need AUX header
   if (rtspConfig.aux)
   {
   fReceiveBuffer[0]=0x00; fReceiveBuffer[1]=0x00; fReceiveBuffer[2]=0x00; fReceiveBuffer[3]=0x01;
   }
   //parse sdp
   const char* strSDP = fSubsession.savedSDPLines();
   rtsp_client_sdp_callback(args, strSDP);
   rtsp_client_sdp_callback(rtspConfig.args, strSDP);
   
   const char* strFmtp = fSubsession.fmtp_spropparametersets();
   rtsp_client_fmtp_callback(args, strFmtp);
   rtsp_client_fmtp_callback(rtspConfig.args, strFmtp);
   //std::cout << strFmtp << std::endl;
}
@@ -514,17 +530,20 @@
}
void DummySink::afterGettingFrame(void* clientData, unsigned frameSize, unsigned numTruncatedBytes,
              struct timeval presentationTime, unsigned durationInMicroseconds) {
              struct timeval presentationTime, unsigned durationInMicroseconds)
{
  DummySink* sink = (DummySink*)clientData;
  if (frameSize > 0)
   rtsp_client_frame_callback(sink->args, sink->fReceiveBuffer, frameSize + 4);
   {
      unsigned s = frameSize;
      if (sink->rtspConfig.aux)
         s += 4;
      rtsp_client_frame_callback(sink->rtspConfig.args, sink->fReceiveBuffer, s);
   }
  
  sink->afterGettingFrame(frameSize, numTruncatedBytes, presentationTime, durationInMicroseconds);
}
// If you don't want to see debugging output for each received frame, then comment out the following line:
#define DEBUG_PRINT_EACH_RECEIVED_FRAME 1
void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
              struct timeval presentationTime, unsigned /*durationInMicroseconds*/) {
@@ -549,14 +568,20 @@
  continuePlaying();
}
Boolean DummySink::continuePlaying() {
Boolean DummySink::continuePlaying()
{
  if (fSource == NULL) return False; // sanity check (should not happen)
     rtsp_client_continue_callback(args);
   rtsp_client_continue_callback(rtspConfig.args);
   u_int8_t* b = fReceiveBuffer;
   if (rtspConfig.aux)
      b += 4;
     
  // Request the next frame of data from our input source.  "afterGettingFrame()" will get called later, when it arrives:
  fSource->getNextFrame(fReceiveBuffer + 4, DUMMY_SINK_RECEIVE_BUFFER_SIZE,
   fSource->getNextFrame(b, DUMMY_SINK_RECEIVE_BUFFER_SIZE,
                        afterGettingFrame, this,
                        onSourceClosure, this);
  return True;
}