| | |
| | | |
| | | #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': |
| | |
| | | // 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); |
| | |
| | | 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: |
| | |
| | | |
| | | 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'). |
| | |
| | | { |
| | | 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(); |
| | | |
| | |
| | | struct timeval presentationTime, unsigned durationInMicroseconds); |
| | | |
| | | public: |
| | | void* args; |
| | | const RTSPConfig& rtspConfig; |
| | | |
| | | private: |
| | | // redefined virtual functions: |
| | |
| | | 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; |
| | | |
| | |
| | | |
| | | // 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; |
| | |
| | | // 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; |
| | | } |
| | |
| | | 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 |
| | | |
| | |
| | | // (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) { |
| | |
| | | |
| | | // 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) |
| | | { |
| | | } |
| | | |
| | |
| | | |
| | | // 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; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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*/) { |
| | |
| | | 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; |
| | | } |