liveMedia/include/MediaSession.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 data structure that represents a session that consists of
00019 // potentially multiple (audio and/or video) sub-sessions
00020 // (This data structure is used for media *receivers* - i.e., clients.
00021 //  For media streamers, use "ServerMediaSession" instead.)
00022 // C++ header
00023 
00024 /* NOTE: To support receiving your own custom RTP payload format, you must first define a new subclass of "MultiFramedRTPSource"
00025    (or "BasicUDPSource") that implements it.  Then define your own subclass of "MediaSession" and "MediaSubsession", as follows:
00026    - In your subclass of "MediaSession" (named, for example, "myMediaSession"):
00027        - Define and implement your own static member function
00028            static myMediaSession* createNew(UsageEnvironment& env, char const* sdpDescription);
00029          and call this - instead of "MediaSession::createNew()" - in your application, when you create a new "MediaSession" object.
00030        - Reimplement the "createNewMediaSubsession()" virtual function, as follows:
00031            MediaSubsession* myMediaSession::createNewMediaSubsession() { return new myMediaSubsession(*this); }
00032    - In your subclass of "MediaSubsession" (named, for example, "myMediaSubsession"):
00033        - Reimplement the "createSourceObjects()" virtual function, perhaps similar to this:
00034            Boolean myMediaSubsession::createSourceObjects(int useSpecialRTPoffset) {
00035              if (strcmp(fCodecName, "X-MY-RTP-PAYLOAD-FORMAT") == 0) {
00036                // This subsession uses our custom RTP payload format:
00037                fReadSource = fRTPSource = myRTPPayloadFormatRTPSource::createNew( <parameters> );
00038                return True;
00039              } else {
00040                // This subsession uses some other RTP payload format - perhaps one that we already implement:
00041                return ::createSourceObjects(useSpecialRTPoffset);
00042              }
00043            }  
00044 */
00045 
00046 #ifndef _MEDIA_SESSION_HH
00047 #define _MEDIA_SESSION_HH
00048 
00049 #ifndef _RTCP_HH
00050 #include "RTCP.hh"
00051 #endif
00052 
00053 class MediaSubsession; // forward
00054 
00055 class MediaSession: public Medium {
00056 public:
00057   static MediaSession* createNew(UsageEnvironment& env,
00058                                  char const* sdpDescription);
00059 
00060   static Boolean lookupByName(UsageEnvironment& env, char const* sourceName,
00061                               MediaSession*& resultSession);
00062 
00063   Boolean hasSubsessions() const { return fSubsessionsHead != NULL; }
00064   double& playStartTime() { return fMaxPlayStartTime; }
00065   double& playEndTime() { return fMaxPlayEndTime; }
00066   char* connectionEndpointName() const { return fConnectionEndpointName; }
00067   char const* CNAME() const { return fCNAME; }
00068   struct in_addr const& sourceFilterAddr() const { return fSourceFilterAddr; }
00069   float& scale() { return fScale; }
00070   char* mediaSessionType() const { return fMediaSessionType; }
00071   char* sessionName() const { return fSessionName; }
00072   char* sessionDescription() const { return fSessionDescription; }
00073   char const* controlPath() const { return fControlPath; }
00074 
00075   Boolean initiateByMediaType(char const* mimeType,
00076                               MediaSubsession*& resultSubsession,
00077                               int useSpecialRTPoffset = -1);
00078       // Initiates the first subsession with the specified MIME type
00079       // Returns the resulting subsession, or 'multi source' (not both)
00080 
00081 protected: // redefined virtual functions
00082   virtual Boolean isMediaSession() const;
00083 
00084 protected:
00085   MediaSession(UsageEnvironment& env);
00086       // called only by createNew();
00087   virtual ~MediaSession();
00088 
00089   virtual MediaSubsession* createNewMediaSubsession();
00090 
00091   Boolean initializeWithSDP(char const* sdpDescription);
00092   Boolean parseSDPLine(char const* input, char const*& nextLine);
00093   Boolean parseSDPLine_s(char const* sdpLine);
00094   Boolean parseSDPLine_i(char const* sdpLine);
00095   Boolean parseSDPLine_c(char const* sdpLine);
00096   Boolean parseSDPAttribute_type(char const* sdpLine);
00097   Boolean parseSDPAttribute_control(char const* sdpLine);
00098   Boolean parseSDPAttribute_range(char const* sdpLine);
00099   Boolean parseSDPAttribute_source_filter(char const* sdpLine);
00100 
00101   static char* lookupPayloadFormat(unsigned char rtpPayloadType,
00102                                    unsigned& rtpTimestampFrequency,
00103                                    unsigned& numChannels);
00104   static unsigned guessRTPTimestampFrequency(char const* mediumName,
00105                                              char const* codecName);
00106 
00107 protected:
00108   friend class MediaSubsessionIterator;
00109   char* fCNAME; // used for RTCP
00110 
00111   // Linkage fields:
00112   MediaSubsession* fSubsessionsHead;
00113   MediaSubsession* fSubsessionsTail;
00114 
00115   // Fields set from a SDP description:
00116   char* fConnectionEndpointName;
00117   double fMaxPlayStartTime;
00118   double fMaxPlayEndTime;
00119   struct in_addr fSourceFilterAddr; // used for SSM
00120   float fScale; // set from a RTSP "Scale:" header
00121   char* fMediaSessionType; // holds a=type value
00122   char* fSessionName; // holds s=<session name> value
00123   char* fSessionDescription; // holds i=<session description> value
00124   char* fControlPath; // holds optional a=control: string
00125 };
00126 
00127 
00128 class MediaSubsessionIterator {
00129 public:
00130   MediaSubsessionIterator(MediaSession& session);
00131   virtual ~MediaSubsessionIterator();
00132 
00133   MediaSubsession* next(); // NULL if none
00134   void reset();
00135 
00136 private:
00137   MediaSession& fOurSession;
00138   MediaSubsession* fNextPtr;
00139 };
00140 
00141 
00142 class MediaSubsession {
00143 public:
00144   MediaSession& parentSession() { return fParent; }
00145   MediaSession const& parentSession() const { return fParent; }
00146 
00147   unsigned short clientPortNum() const { return fClientPortNum; }
00148   unsigned char rtpPayloadFormat() const { return fRTPPayloadFormat; }
00149   char const* savedSDPLines() const { return fSavedSDPLines; }
00150   char const* mediumName() const { return fMediumName; }
00151   char const* codecName() const { return fCodecName; }
00152   char const* protocolName() const { return fProtocolName; }
00153   char const* controlPath() const { return fControlPath; }
00154   Boolean isSSM() const { return fSourceFilterAddr.s_addr != 0; }
00155 
00156   unsigned short videoWidth() const { return fVideoWidth; }
00157   unsigned short videoHeight() const { return fVideoHeight; }
00158   unsigned videoFPS() const { return fVideoFPS; }
00159   unsigned numChannels() const { return fNumChannels; }
00160   float& scale() { return fScale; }
00161 
00162   RTPSource* rtpSource() { return fRTPSource; }
00163   RTCPInstance* rtcpInstance() { return fRTCPInstance; }
00164   unsigned rtpTimestampFrequency() const { return fRTPTimestampFrequency; }
00165   FramedSource* readSource() { return fReadSource; }
00166     // This is the source that client sinks read from.  It is usually
00167     // (but not necessarily) the same as "rtpSource()"
00168 
00169   double playStartTime() const;
00170   double playEndTime() const;
00171   // Used only to set the local fields:
00172   double& _playStartTime() { return fPlayStartTime; }
00173   double& _playEndTime() { return fPlayEndTime; }
00174 
00175   Boolean initiate(int useSpecialRTPoffset = -1);
00176       // Creates a "RTPSource" for this subsession. (Has no effect if it's
00177       // already been created.)  Returns True iff this succeeds.
00178   void deInitiate(); // Destroys any previously created RTPSource
00179   Boolean setClientPortNum(unsigned short portNum);
00180       // Sets the preferred client port number that any "RTPSource" for
00181       // this subsession would use.  (By default, the client port number
00182       // is gotten from the original SDP description, or - if the SDP
00183       // description does not specfy a client port number - an ephemeral
00184       // (even) port number is chosen.)  This routine should *not* be
00185       // called after initiate().
00186   char*& connectionEndpointName() { return fConnectionEndpointName; }
00187   char const* connectionEndpointName() const {
00188     return fConnectionEndpointName;
00189   }
00190 
00191   // Various parameters set in "a=fmtp:" SDP lines:
00192   unsigned fmtp_auxiliarydatasizelength() const { return fAuxiliarydatasizelength; }
00193   unsigned fmtp_constantduration() const { return fConstantduration; }
00194   unsigned fmtp_constantsize() const { return fConstantsize; }
00195   unsigned fmtp_crc() const { return fCRC; }
00196   unsigned fmtp_ctsdeltalength() const { return fCtsdeltalength; }
00197   unsigned fmtp_de_interleavebuffersize() const { return fDe_interleavebuffersize; }
00198   unsigned fmtp_dtsdeltalength() const { return fDtsdeltalength; }
00199   unsigned fmtp_indexdeltalength() const { return fIndexdeltalength; }
00200   unsigned fmtp_indexlength() const { return fIndexlength; }
00201   unsigned fmtp_interleaving() const { return fInterleaving; }
00202   unsigned fmtp_maxdisplacement() const { return fMaxdisplacement; }
00203   unsigned fmtp_objecttype() const { return fObjecttype; }
00204   unsigned fmtp_octetalign() const { return fOctetalign; }
00205   unsigned fmtp_profile_level_id() const { return fProfile_level_id; }
00206   unsigned fmtp_robustsorting() const { return fRobustsorting; }
00207   unsigned fmtp_sizelength() const { return fSizelength; }
00208   unsigned fmtp_streamstateindication() const { return fStreamstateindication; }
00209   unsigned fmtp_streamtype() const { return fStreamtype; }
00210   Boolean fmtp_cpresent() const { return fCpresent; }
00211   Boolean fmtp_randomaccessindication() const { return fRandomaccessindication; }
00212   char const* fmtp_config() const { return fConfig; }
00213   char const* fmtp_configuration() const { return fmtp_config(); }
00214   char const* fmtp_mode() const { return fMode; }
00215   char const* fmtp_spropparametersets() const { return fSpropParameterSets; }
00216   char const* fmtp_emphasis() const { return fEmphasis; }
00217   char const* fmtp_channelorder() const { return fChannelOrder; }
00218 
00219   netAddressBits connectionEndpointAddress() const;
00220       // Converts "fConnectionEndpointName" to an address (or 0 if unknown)
00221   void setDestinations(netAddressBits defaultDestAddress);
00222       // Uses "fConnectionEndpointName" and "serverPortNum" to set
00223       // the destination address and port of the RTP and RTCP objects.
00224       // This is typically called by RTSP clients after doing "SETUP".
00225 
00226   char const* sessionId() const { return fSessionId; }
00227   void setSessionId(char const* sessionId);
00228 
00229   // Public fields that external callers can use to keep state.
00230   // (They are responsible for all storage management on these fields)
00231   unsigned short serverPortNum; // in host byte order (used by RTSP)
00232   unsigned char rtpChannelId, rtcpChannelId; // used by RTSP (for RTP/TCP)
00233   MediaSink* sink; // callers can use this to keep track of who's playing us
00234   void* miscPtr; // callers can use this for whatever they want
00235 
00236   // Parameters set from a RTSP "RTP-Info:" header:
00237   struct {
00238     u_int16_t seqNum;
00239     u_int32_t timestamp;
00240     Boolean infoIsNew; // not part of the RTSP header; instead, set whenever this struct is filled in
00241   } rtpInfo;
00242 
00243   double getNormalPlayTime(struct timeval const& presentationTime);
00244   // Computes the stream's "Normal Play Time" (NPT) from the given "presentationTime".
00245   // (For the definition of "Normal Play Time", see RFC 2326, section 3.6.)
00246   // This function is useful only if the "rtpInfo" structure was previously filled in
00247   // (e.g., by a "RTP-Info:" header in a RTSP response).
00248   // Also, for this function to work properly, the RTP stream's presentation times must (eventually) be
00249   // synchronized via RTCP.
00250   // (Note: If this function returns a negative number, then the result should be ignored by the caller.)
00251 
00252 protected:
00253   friend class MediaSession;
00254   friend class MediaSubsessionIterator;
00255   MediaSubsession(MediaSession& parent);
00256   virtual ~MediaSubsession();
00257 
00258   UsageEnvironment& env() { return fParent.envir(); }
00259   void setNext(MediaSubsession* next) { fNext = next; }
00260 
00261   Boolean parseSDPLine_c(char const* sdpLine);
00262   Boolean parseSDPLine_b(char const* sdpLine);
00263   Boolean parseSDPAttribute_rtpmap(char const* sdpLine);
00264   Boolean parseSDPAttribute_control(char const* sdpLine);
00265   Boolean parseSDPAttribute_range(char const* sdpLine);
00266   Boolean parseSDPAttribute_fmtp(char const* sdpLine);
00267   Boolean parseSDPAttribute_source_filter(char const* sdpLine);
00268   Boolean parseSDPAttribute_x_dimensions(char const* sdpLine);
00269   Boolean parseSDPAttribute_framerate(char const* sdpLine);
00270 
00271   virtual Boolean createSourceObjects(int useSpecialRTPoffset);
00272     // create "fRTPSource" and "fReadSource" member objects, after we've been initialized via SDP
00273 
00274 protected:
00275   // Linkage fields:
00276   MediaSession& fParent;
00277   MediaSubsession* fNext;
00278 
00279   // Fields set from a SDP description:
00280   char* fConnectionEndpointName; // may also be set by RTSP SETUP response
00281   unsigned short fClientPortNum; // in host byte order
00282       // This field is also set by initiate()
00283   unsigned char fRTPPayloadFormat;
00284   char* fSavedSDPLines;
00285   char* fMediumName;
00286   char* fCodecName;
00287   char* fProtocolName;
00288   unsigned fRTPTimestampFrequency;
00289   char* fControlPath; // holds optional a=control: string
00290   struct in_addr fSourceFilterAddr; // used for SSM
00291   unsigned fBandwidth; // in kilobits-per-second, from b= line
00292 
00293   // Parameters set by "a=fmtp:" SDP lines:
00294   unsigned fAuxiliarydatasizelength, fConstantduration, fConstantsize;
00295   unsigned fCRC, fCtsdeltalength, fDe_interleavebuffersize, fDtsdeltalength;
00296   unsigned fIndexdeltalength, fIndexlength, fInterleaving;
00297   unsigned fMaxdisplacement, fObjecttype;
00298   unsigned fOctetalign, fProfile_level_id, fRobustsorting;
00299   unsigned fSizelength, fStreamstateindication, fStreamtype;
00300   Boolean fCpresent, fRandomaccessindication;
00301   char *fConfig, *fMode, *fSpropParameterSets, *fEmphasis, *fChannelOrder;
00302 
00303   double fPlayStartTime;
00304   double fPlayEndTime;
00305   unsigned short fVideoWidth, fVideoHeight;
00306      // screen dimensions (set by an optional a=x-dimensions: <w>,<h> line)
00307   unsigned fVideoFPS;
00308      // frame rate (set by an optional "a=framerate: <fps>" or "a=x-framerate: <fps>" line)
00309   unsigned fNumChannels;
00310      // optionally set by "a=rtpmap:" lines for audio sessions.  Default: 1
00311   float fScale; // set from a RTSP "Scale:" header
00312   double fNPT_PTS_Offset; // set by "getNormalPlayTime()"; add this to a PTS to get NPT
00313 
00314   // Fields set by initiate():
00315   Groupsock* fRTPSocket; Groupsock* fRTCPSocket; // works even for unicast
00316   RTPSource* fRTPSource; RTCPInstance* fRTCPInstance;
00317   FramedSource* fReadSource;
00318 
00319   // Other fields:
00320   char* fSessionId; // used by RTSP
00321 };
00322 
00323 #endif

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