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

Generated on Tue Oct 7 15:38:08 2008 for live by  doxygen 1.5.2