From c7caa54d8d58af6159f2c758ad5d6687d461a333 Mon Sep 17 00:00:00 2001 From: sujinwen <sujinwen@454eff88-639b-444f-9e54-f578c98de674> Date: 星期二, 25 七月 2017 10:53:23 +0800 Subject: [PATCH] --- RtspFace/live555/testProgs/testRTSPClient.hpp | 229 +++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 141 insertions(+), 88 deletions(-) diff --git a/RtspFace/live555/testProgs/testRTSPClient.hpp b/RtspFace/live555/testProgs/testRTSPClient.hpp index f346165..47dccd2 100644 --- a/RtspFace/live555/testProgs/testRTSPClient.hpp +++ b/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,11 +30,13 @@ // 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 +//#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: #define DUMMY_SINK_RECEIVE_BUFFER_SIZE 1920*1080*3//#todo + +#define INCREASE_RECEIVE_BUFFER_TO 8000000 // 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 @@ -62,21 +65,21 @@ void shutdownStream(RTSPClient* rtspClient, int exitCode = 1); // A function that outputs a string that identifies each stream (for debugging output). Modify this if you wish: -log4cpp::CategoryStream& operator<<(log4cpp::CategoryStream& logRoot, const RTSPClient& rtspClient) +Logger& operator<<(Logger& logRoot, const RTSPClient& rtspClient) { return logRoot << "[URL:\"" << rtspClient.url() << "\"]: "; } // A function that outputs a string that identifies each subsession (for debugging output). Modify this if you wish: -log4cpp::CategoryStream& operator<<(log4cpp::CategoryStream& logRoot, const MediaSubsession& subsession) +Logger& operator<<(Logger& logRoot, const MediaSubsession& subsession) { return logRoot << subsession.mediumName() << "/" << subsession.codecName(); } 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 +161,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 +206,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 +261,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,42 +299,55 @@ 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) { - if (!scs.subsession->initiate()) - { - LOG_ERROR << *rtspClient << "Failed to initiate the \"" << *scs.subsession << "\" subsession: " << env.getResultMsg() << std::endl; - setupNextSubsession(rtspClient); // give up on this subsession; go to the next one - } - else - { - LOG_INFO << *rtspClient << "Initiated the \"" << *scs.subsession << "\" subsession (" << std::endl; - if (scs.subsession->rtcpIsMuxed()) - LOG_INFO << "client port " << scs.subsession->clientPortNum() << std::endl; - else - LOG_INFO << "client ports " << scs.subsession->clientPortNum() << "-" << scs.subsession->clientPortNum()+1 << std::endl; - LOG_INFO << ")" << std::endl; - - // Continue setting up this subsession, by sending a RTSP "SETUP" command: - rtspClient->sendSetupCommand(*scs.subsession, continueAfterSETUP, False, REQUEST_STREAMING_OVER_TCP); - } - return; + scs.subsession->setClientPortNum(_ourRTSPClient->desiredPortNum); + _ourRTSPClient->desiredPortNum += 2; } + + if (!scs.subsession->initiate()) + { + 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 (" << LOG_ENDL; + if (scs.subsession->rtcpIsMuxed()) + LOG_INFO << "client port " << scs.subsession->clientPortNum() << LOG_ENDL; + else + LOG_INFO << "client ports " << scs.subsession->clientPortNum() << "-" << scs.subsession->clientPortNum()+1 << LOG_ENDL; + LOG_INFO << ")" << LOG_ENDL; + +#ifdef INCREASE_RECEIVE_BUFFER_TO + //sysctl net.core.rmem_max=40000000 + if (INCREASE_RECEIVE_BUFFER_TO > 0) + increaseReceiveBufferTo(env, scs.subsession->rtpSource()->RTPgs()->socketNum(), INCREASE_RECEIVE_BUFFER_TO); +#endif + + // Continue setting up this subsession, by sending a RTSP "SETUP" command: + rtspClient->sendSetupCommand(*scs.subsession, continueAfterSETUP, False, _ourRTSPClient->rtspConfig.requestStreamingOverTcp); + } + return; + } // We've finished setting up all of the subsessions. Now, send a RTSP "PLAY" command to start the streaming: if (scs.session->absStartTime() != NULL) - { - // Special case: The stream is indexed by 'absolute' time, so send an appropriate "PLAY" command: - rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY, scs.session->absStartTime(), scs.session->absEndTime()); - } + { + // Special case: The stream is indexed by 'absolute' time, so send an appropriate "PLAY" command: + rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY, scs.session->absStartTime(), scs.session->absEndTime()); + } else - { - scs.duration = scs.session->playEndTime() - scs.session->playStartTime(); - rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY); - } + { + scs.duration = scs.session->playEndTime() - scs.session->playStartTime(); + rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY); + } } void continueAfterSETUP(RTSPClient* rtspClient, int resultCode, char* resultString) @@ -316,44 +359,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 +420,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 +436,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 +485,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 +538,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 +547,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 +562,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 +574,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 +606,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 +655,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 -- Gitblit v1.8.0