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

Generated on Thu May 17 07:11:45 2012 for live by  doxygen 1.5.2