liveMedia/include/RTSPClient.hh

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2012 Live Networks, Inc.  All rights reserved.
00018 // A generic RTSP client - for a single "rtsp://" URL
00019 // C++ header
00020 
00021 #ifndef _RTSP_CLIENT_HH
00022 #define _RTSP_CLIENT_HH
00023 
00024 #ifndef _MEDIA_SESSION_HH
00025 #include "MediaSession.hh"
00026 #endif
00027 #ifndef _NET_ADDRESS_HH
00028 #include "NetAddress.hh"
00029 #endif
00030 #ifndef _DIGEST_AUTHENTICATION_HH
00031 #include "DigestAuthentication.hh"
00032 #endif
00033 
00034 #define RTSPCLIENT_SYNCHRONOUS_INTERFACE 1 // For now, continue to support the old synchronous interface as well
00035 
00036 class RTSPClient: public Medium {
00037 public:
00038   static RTSPClient* createNew(UsageEnvironment& env, char const* rtspURL,
00039                                int verbosityLevel = 0,
00040                                char const* applicationName = NULL,
00041                                portNumBits tunnelOverHTTPPortNum = 0);
00042   // If "tunnelOverHTTPPortNum" is non-zero, we tunnel RTSP (and RTP)
00043   // over a HTTP connection with the given port number, using the technique
00044   // described in Apple's document <http://developer.apple.com/documentation/QuickTime/QTSS/Concepts/chapter_2_section_14.html>
00045 
00046   typedef void (responseHandler)(RTSPClient* rtspClient,
00047                                  int resultCode, char* resultString);
00048       // A function that is called in response to a RTSP command.  The parameters are as follows:
00049       //     "rtspClient": The "RTSPClient" object on which the original command was issued.
00050       //     "resultCode": If zero, then the command completed successfully.  If non-zero, then the command did not complete
00051       //         successfully, and "resultCode" indicates the error, as follows:
00052       //             A positive "resultCode" is a RTSP error code (for example, 404 means "not found")
00053       //             A negative "resultCode" indicates a socket/network error; 0-"resultCode" is the standard "errno" code.
00054       //     "resultString": A ('\0'-terminated) string returned along with the response, or else NULL.
00055       //         In particular:
00056       //             "resultString" for a successful "DESCRIBE" command will be the media session's SDP description.
00057       //             "resultString" for a successful "OPTIONS" command will be a list of allowed commands.
00058       //         Note that this string can be present (i.e., not NULL) even if "resultCode" is non-zero - i.e., an error message.
00059       //         Also, "resultString" can be NULL, even if "resultCode" is zero (e.g., if the RTSP command succeeded, but without
00060       //             including an appropriate result header).
00061       //         Note also that this string is dynamically allocated, and must be freed by the handler (or the caller)
00062       //             - using "delete[]".
00063 
00064   unsigned sendDescribeCommand(responseHandler* responseHandler, Authenticator* authenticator = NULL);
00065       // Issues a RTSP "DESCRIBE" command, then returns the "CSeq" sequence number that was used in the command.
00066       // The (programmer-supplied) "responseHandler" function is called later to handle the response
00067       //     (or is called immediately - with an error code - if the command cannot be sent).
00068       // "authenticator" (optional) is used for access control.  If you have username and password strings, you can use this by
00069       //     passing an actual parameter that you created by creating an "Authenticator(username, password) object".
00070       //     (Note that if you supply a non-NULL "authenticator" parameter, you need do this only for the first command you send.)
00071 
00072   unsigned sendOptionsCommand(responseHandler* responseHandler, Authenticator* authenticator = NULL);
00073       // Issues a RTSP "OPTIONS" command, then returns the "CSeq" sequence number that was used in the command.
00074       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00075 
00076   unsigned sendAnnounceCommand(char const* sdpDescription, responseHandler* responseHandler, Authenticator* authenticator = NULL);
00077       // Issues a RTSP "ANNOUNCE" command (with "sdpDescription" as parameter),
00078       //     then returns the "CSeq" sequence number that was used in the command.
00079       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00080 
00081   unsigned sendSetupCommand(MediaSubsession& subsession, responseHandler* responseHandler,
00082                             Boolean streamOutgoing = False,
00083                             Boolean streamUsingTCP = False,
00084                             Boolean forceMulticastOnUnspecified = False,
00085                             Authenticator* authenticator = NULL);
00086       // Issues a RTSP "SETUP" command, then returns the "CSeq" sequence number that was used in the command.
00087       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00088 
00089   unsigned sendPlayCommand(MediaSession& session, responseHandler* responseHandler,
00090                            double start = 0.0f, double end = -1.0f, float scale = 1.0f,
00091                            Authenticator* authenticator = NULL);
00092       // Issues an aggregate RTSP "PLAY" command on "session", then returns the "CSeq" sequence number that was used in the command.
00093       // (Note: start=-1 means 'resume'; end=-1 means 'play to end')
00094       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00095   unsigned sendPlayCommand(MediaSubsession& subsession, responseHandler* responseHandler,
00096                            double start = 0.0f, double end = -1.0f, float scale = 1.0f,
00097                            Authenticator* authenticator = NULL);
00098       // Issues a RTSP "PLAY" command on "subsession", then returns the "CSeq" sequence number that was used in the command.
00099       // (Note: start=-1 means 'resume'; end=-1 means 'play to end')
00100       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00101 
00102   unsigned sendPauseCommand(MediaSession& session, responseHandler* responseHandler, Authenticator* authenticator = NULL);
00103       // Issues an aggregate RTSP "PAUSE" command on "session", then returns the "CSeq" sequence number that was used in the command.
00104       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00105   unsigned sendPauseCommand(MediaSubsession& subsession, responseHandler* responseHandler, Authenticator* authenticator = NULL);
00106       // Issues a RTSP "PAUSE" command on "subsession", then returns the "CSeq" sequence number that was used in the command.
00107       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00108 
00109   unsigned sendRecordCommand(MediaSession& session, responseHandler* responseHandler, Authenticator* authenticator = NULL);
00110       // Issues an aggregate RTSP "RECORD" command on "session", then returns the "CSeq" sequence number that was used in the command.
00111       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00112   unsigned sendRecordCommand(MediaSubsession& subsession, responseHandler* responseHandler, Authenticator* authenticator = NULL);
00113       // Issues a RTSP "RECORD" command on "subsession", then returns the "CSeq" sequence number that was used in the command.
00114       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00115 
00116   unsigned sendTeardownCommand(MediaSession& session, responseHandler* responseHandler, Authenticator* authenticator = NULL);
00117       // Issues an aggregate RTSP "TEARDOWN" command on "session", then returns the "CSeq" sequence number that was used in the command.
00118       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00119   unsigned sendTeardownCommand(MediaSubsession& subsession, responseHandler* responseHandler, Authenticator* authenticator = NULL);
00120       // Issues a RTSP "TEARDOWN" command on "subsession", then returns the "CSeq" sequence number that was used in the command.
00121       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00122 
00123   unsigned sendSetParameterCommand(MediaSession& session, responseHandler* responseHandler,
00124                                    char const* parameterName, char const* parameterValue,
00125                                    Authenticator* authenticator = NULL);
00126       // Issues an aggregate RTSP "SET_PARAMETER" command on "session", then returns the "CSeq" sequence number that was used in the command.
00127       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00128 
00129   unsigned sendGetParameterCommand(MediaSession& session, responseHandler* responseHandler, char const* parameterName,
00130                                    Authenticator* authenticator = NULL);
00131       // Issues an aggregate RTSP "GET_PARAMETER" command on "session", then returns the "CSeq" sequence number that was used in the command.
00132       // (The "responseHandler" and "authenticator" parameters are as described for "sendDescribeCommand".)
00133 
00134   Boolean changeResponseHandler(unsigned cseq, responseHandler* newResponseHandler);
00135       // Changes the response handler for the previously-performed command (whose operation returned "cseq").
00136       // (To turn off any response handling for the command, use a "newResponseHandler" value of NULL.  This might be done as part
00137       //  of an implementation of a 'timeout handler' on the command, for example.)
00138       // This function returns True iff "cseq" was for a valid previously-performed command (whose response is still unhandled).
00139 
00140   int socketNum() const { return fInputSocketNum; }
00141 
00142   static Boolean lookupByName(UsageEnvironment& env,
00143                               char const* sourceName,
00144                               RTSPClient*& resultClient);
00145 
00146   static Boolean parseRTSPURL(UsageEnvironment& env, char const* url,
00147                               char*& username, char*& password, NetAddress& address, portNumBits& portNum, char const** urlSuffix = NULL);
00148       // Parses "url" as "rtsp://[<username>[:<password>]@]<server-address-or-name>[:<port>][/<stream-name>]"
00149       // (Note that the returned "username" and "password" are either NULL, or heap-allocated strings that the caller must later delete[].)
00150 
00151   void setUserAgentString(char const* userAgentName);
00152       // sets an alternative string to be used in RTSP "User-Agent:" headers
00153 
00154   unsigned sessionTimeoutParameter() const { return fSessionTimeoutParameter; }
00155 
00156   char const* url() const { return fBaseURL; }
00157 
00158   static unsigned responseBufferSize;
00159 
00160 protected:
00161   RTSPClient(UsageEnvironment& env, char const* rtspURL,
00162              int verbosityLevel, char const* applicationName, portNumBits tunnelOverHTTPPortNum);
00163       // called only by createNew();
00164   virtual ~RTSPClient();
00165 
00166   void setBaseURL(char const* url);
00167 
00168 private: // redefined virtual functions
00169   virtual Boolean isRTSPClient() const;
00170 
00171 public: // Some compilers complain if this is "private:"
00172   // The state of a request-in-progress:
00173   class RequestRecord {
00174   public:
00175     RequestRecord(unsigned cseq, char const* commandName, responseHandler* handler,
00176                   MediaSession* session = NULL, MediaSubsession* subsession = NULL, u_int32_t booleanFlags = 0,
00177                   double start = 0.0f, double end = -1.0f, float scale = 1.0f, char const* contentStr = NULL);
00178     virtual ~RequestRecord();
00179 
00180     RequestRecord*& next() { return fNext; }
00181     unsigned& cseq() { return fCSeq; }
00182     char const* commandName() const { return fCommandName; }
00183     MediaSession* session() const { return fSession; }
00184     MediaSubsession* subsession() const { return fSubsession; }
00185     u_int32_t booleanFlags() const { return fBooleanFlags; }
00186     double start() const { return fStart; }
00187     double end() const { return fEnd; }
00188     float scale() const { return fScale; }
00189     char* contentStr() const { return fContentStr; }
00190     responseHandler*& handler() { return fHandler; }
00191 
00192   private:
00193     RequestRecord* fNext;
00194     unsigned fCSeq;
00195     char const* fCommandName;
00196     MediaSession* fSession;
00197     MediaSubsession* fSubsession;
00198     u_int32_t fBooleanFlags;
00199     double fStart, fEnd;
00200     float fScale;
00201     char* fContentStr;
00202     responseHandler* fHandler;
00203   };
00204 private:
00205   class RequestQueue {
00206   public:
00207     RequestQueue();
00208     virtual ~RequestQueue();
00209 
00210     void enqueue(RequestRecord* request); // "request" must not be NULL
00211     RequestRecord* dequeue();
00212     void putAtHead(RequestRecord* request); // "request" must not be NULL
00213     RequestRecord* findByCSeq(unsigned cseq);
00214     Boolean isEmpty() const { return fHead == NULL; }
00215 
00216   private:
00217     RequestRecord* fHead;
00218     RequestRecord* fTail;
00219   };
00220 
00221   void reset();
00222   void resetTCPSockets();
00223   void resetResponseBuffer();
00224   int openConnection(); // -1: failure; 0: pending; 1: success
00225   int connectToServer(int socketNum, portNumBits remotePortNum); // used to implement "openConnection()"; result values are the same
00226   char* createAuthenticatorString(char const* cmd, char const* url);
00227   unsigned sendRequest(RequestRecord* request);
00228   void handleRequestError(RequestRecord* request);
00229   Boolean parseResponseCode(char const* line, unsigned& responseCode, char const*& responseString);
00230   void handleIncomingRequest();
00231   static Boolean checkForHeader(char const* line, char const* headerName, unsigned headerNameLength, char const*& headerParams);
00232   Boolean parseTransportParams(char const* paramsStr,
00233                                char*& serverAddressStr, portNumBits& serverPortNum,
00234                                unsigned char& rtpChannelId, unsigned char& rtcpChannelId);
00235   Boolean parseScaleParam(char const* paramStr, float& scale);
00236   Boolean parseRTPInfoParams(char const*& paramStr, u_int16_t& seqNum, u_int32_t& timestamp);
00237   Boolean handleSETUPResponse(MediaSubsession& subsession, char const* sessionParamsStr, char const* transportParamsStr,
00238                               Boolean streamUsingTCP);
00239   Boolean handlePLAYResponse(MediaSession& session, MediaSubsession& subsession,
00240                              char const* scaleParamsStr, char const* rangeParamsStr, char const* rtpInfoParamsStr);
00241   Boolean handleTEARDOWNResponse(MediaSession& session, MediaSubsession& subsession);
00242   Boolean handleGET_PARAMETERResponse(char const* parameterName, char*& resultValueString);
00243   Boolean handleAuthenticationFailure(char const* wwwAuthenticateParamsStr);
00244   Boolean resendCommand(RequestRecord* request);
00245   char const* sessionURL(MediaSession const& session) const;
00246   static void handleAlternativeRequestByte(void*, u_int8_t requestByte);
00247   void handleAlternativeRequestByte1(u_int8_t requestByte);
00248   void constructSubsessionURL(MediaSubsession const& subsession,
00249                               char const*& prefix,
00250                               char const*& separator,
00251                               char const*& suffix);
00252 
00253   // Support for tunneling RTSP-over-HTTP:
00254   Boolean setupHTTPTunneling1(); // send the HTTP "GET"
00255   static void responseHandlerForHTTP_GET(RTSPClient* rtspClient, int responseCode, char* responseString);
00256   void responseHandlerForHTTP_GET1(int responseCode, char* responseString);
00257   Boolean setupHTTPTunneling2(); // send the HTTP "POST"
00258 
00259   // Support for asynchronous connections to the server:
00260   static void connectionHandler(void*, int /*mask*/);
00261   void connectionHandler1();
00262 
00263   // Support for handling data sent back by a server:
00264   static void incomingDataHandler(void*, int /*mask*/);
00265   void incomingDataHandler1();
00266   void handleResponseBytes(int newBytesRead);
00267 
00268 private:
00269   int fVerbosityLevel;
00270   portNumBits fTunnelOverHTTPPortNum;
00271   char* fUserAgentHeaderStr;
00272   unsigned fUserAgentHeaderStrLen;
00273   int fInputSocketNum, fOutputSocketNum;
00274   netAddressBits fServerAddress;
00275   unsigned fCSeq; // sequence number, used in consecutive requests
00276   char* fBaseURL;
00277   Authenticator fCurrentAuthenticator;
00278   unsigned char fTCPStreamIdCount; // used for (optional) RTP/TCP
00279   char* fLastSessionId;
00280   unsigned fSessionTimeoutParameter; // optionally set in response "Session:" headers
00281   char* fResponseBuffer;
00282   unsigned fResponseBytesAlreadySeen, fResponseBufferBytesLeft;
00283   RequestQueue fRequestsAwaitingConnection, fRequestsAwaitingHTTPTunneling, fRequestsAwaitingResponse;
00284 
00285   // Support for tunneling RTSP-over-HTTP:
00286   char fSessionCookie[33];
00287   unsigned fSessionCookieCounter;
00288   Boolean fHTTPTunnelingConnectionIsPending;
00289 
00290 #ifdef RTSPCLIENT_SYNCHRONOUS_INTERFACE
00291   // Old "RTSPClient" interface, which performs synchronous (blocking) operations.
00292   // This will eventually go away, so new applications should not use it.
00293 public:
00294   static RTSPClient* createNew(UsageEnvironment& env,
00295                                int verbosityLevel = 0,
00296                                char const* applicationName = NULL,
00297                                portNumBits tunnelOverHTTPPortNum = 0);
00298   char* describeURL(char const* url, Authenticator* authenticator = NULL,
00299                     Boolean allowKasennaProtocol = False, int timeout = -1);
00300   char* describeWithPassword(char const* url,
00301                              char const* username, char const* password,
00302                              Boolean allowKasennaProtocol = False, 
00303                              int timeout = -1);
00304   char* sendOptionsCmd(char const* url,
00305                        char* username = NULL, char* password = NULL,
00306                        Authenticator* authenticator = NULL,
00307                        int timeout = -1);
00308   Boolean announceSDPDescription(char const* url,
00309                                  char const* sdpDescription,
00310                                  Authenticator* authenticator = NULL,
00311                                  int timeout = -1);
00312   Boolean announceWithPassword(char const* url, char const* sdpDescription,
00313                                char const* username, char const* password, int timeout = -1);
00314   Boolean setupMediaSubsession(MediaSubsession& subsession,
00315                                Boolean streamOutgoing = False,
00316                                Boolean streamUsingTCP = False,
00317                                Boolean forceMulticastOnUnspecified = False);
00318   Boolean playMediaSession(MediaSession& session,
00319                            double start = 0.0f, double end = -1.0f,
00320                            float scale = 1.0f);
00321   Boolean playMediaSubsession(MediaSubsession& subsession,
00322                               double start = 0.0f, double end = -1.0f,
00323                               float scale = 1.0f,
00324                               Boolean hackForDSS = False);
00325   Boolean pauseMediaSession(MediaSession& session);
00326   Boolean pauseMediaSubsession(MediaSubsession& subsession);
00327   Boolean recordMediaSubsession(MediaSubsession& subsession);
00328   Boolean setMediaSessionParameter(MediaSession& session,
00329                                    char const* parameterName,
00330                                    char const* parameterValue);
00331   Boolean getMediaSessionParameter(MediaSession& session,
00332                                    char const* parameterName,
00333                                    char*& parameterValue);
00334   Boolean teardownMediaSession(MediaSession& session);
00335   Boolean teardownMediaSubsession(MediaSubsession& subsession);
00336 
00337   static Boolean parseRTSPURLUsernamePassword(char const* url,
00338                                               char*& username, char*& password);
00339 private: // used to implement the old interface:
00340   static void responseHandlerForSyncInterface(RTSPClient* rtspClient,
00341                                               int responseCode, char* responseString);
00342   void responseHandlerForSyncInterface1(int responseCode, char* responseString);
00343   static void timeoutHandlerForSyncInterface(void* rtspClient);
00344   void timeoutHandlerForSyncInterface1();
00345   TaskToken fTimeoutTask;
00346   char fWatchVariableForSyncInterface;
00347   char* fResultString;
00348   int fResultCode;
00349 #endif
00350 };
00351 
00352 #endif

Generated on Thu Feb 2 23:51:30 2012 for live by  doxygen 1.5.2