MediaSession.hh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "liveMedia"
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // A data structure that represents a session that consists of
  17. // potentially multiple (audio and/or video) sub-sessions
  18. // (This data structure is used for media *receivers* - i.e., clients.
  19. // For media streamers, use "ServerMediaSession" instead.)
  20. // C++ header
  21. /* NOTE: To support receiving your own custom RTP payload format, you must first define a new
  22. subclass of "MultiFramedRTPSource" (or "BasicUDPSource") that implements it.
  23. Then define your own subclass of "MediaSession" and "MediaSubsession", as follows:
  24. - In your subclass of "MediaSession" (named, for example, "myMediaSession"):
  25. - Define and implement your own static member function
  26. static myMediaSession* createNew(UsageEnvironment& env, char const* sdpDescription);
  27. and call this - instead of "MediaSession::createNew()" - in your application,
  28. when you create a new "MediaSession" object.
  29. - Reimplement the "createNewMediaSubsession()" virtual function, as follows:
  30. MediaSubsession* myMediaSession::createNewMediaSubsession() { return new myMediaSubsession(*this); }
  31. - In your subclass of "MediaSubsession" (named, for example, "myMediaSubsession"):
  32. - Reimplement the "createSourceObjects()" virtual function, perhaps similar to this:
  33. Boolean myMediaSubsession::createSourceObjects(int useSpecialRTPoffset) {
  34. if (strcmp(fCodecName, "X-MY-RTP-PAYLOAD-FORMAT") == 0) {
  35. // This subsession uses our custom RTP payload format:
  36. fReadSource = fRTPSource = myRTPPayloadFormatRTPSource::createNew( <parameters> );
  37. return True;
  38. } else {
  39. // This subsession uses some other RTP payload format - perhaps one that we already implement:
  40. return ::createSourceObjects(useSpecialRTPoffset);
  41. }
  42. }
  43. */
  44. #ifndef _MEDIA_SESSION_HH
  45. #define _MEDIA_SESSION_HH
  46. #ifndef _RTCP_HH
  47. #include "RTCP.hh"
  48. #endif
  49. #ifndef _FRAMED_FILTER_HH
  50. #include "FramedFilter.hh"
  51. #endif
  52. class MediaSubsession; // forward
  53. class MediaSession: public Medium {
  54. public:
  55. static MediaSession* createNew(UsageEnvironment& env,
  56. char const* sdpDescription);
  57. static Boolean lookupByName(UsageEnvironment& env, char const* sourceName,
  58. MediaSession*& resultSession);
  59. Boolean hasSubsessions() const { return fSubsessionsHead != NULL; }
  60. char* connectionEndpointName() const { return fConnectionEndpointName; }
  61. char const* CNAME() const { return fCNAME; }
  62. struct in_addr const& sourceFilterAddr() const { return fSourceFilterAddr; }
  63. float& scale() { return fScale; }
  64. float& speed() { return fSpeed; }
  65. char* mediaSessionType() const { return fMediaSessionType; }
  66. char* sessionName() const { return fSessionName; }
  67. char* sessionDescription() const { return fSessionDescription; }
  68. char const* controlPath() const { return fControlPath; }
  69. double& playStartTime() { return fMaxPlayStartTime; }
  70. double& playEndTime() { return fMaxPlayEndTime; }
  71. char* absStartTime() const;
  72. char* absEndTime() const;
  73. // Used only to set the local fields:
  74. char*& _absStartTime() { return fAbsStartTime; }
  75. char*& _absEndTime() { return fAbsEndTime; }
  76. Boolean initiateByMediaType(char const* mimeType,
  77. MediaSubsession*& resultSubsession,
  78. int useSpecialRTPoffset = -1);
  79. // Initiates the first subsession with the specified MIME type
  80. // Returns the resulting subsession, or 'multi source' (not both)
  81. protected: // redefined virtual functions
  82. virtual Boolean isMediaSession() const;
  83. protected:
  84. MediaSession(UsageEnvironment& env);
  85. // called only by createNew();
  86. virtual ~MediaSession();
  87. virtual MediaSubsession* createNewMediaSubsession();
  88. Boolean initializeWithSDP(char const* sdpDescription);
  89. Boolean parseSDPLine(char const* input, char const*& nextLine);
  90. Boolean parseSDPLine_s(char const* sdpLine);
  91. Boolean parseSDPLine_i(char const* sdpLine);
  92. Boolean parseSDPLine_c(char const* sdpLine);
  93. Boolean parseSDPAttribute_type(char const* sdpLine);
  94. Boolean parseSDPAttribute_control(char const* sdpLine);
  95. Boolean parseSDPAttribute_range(char const* sdpLine);
  96. Boolean parseSDPAttribute_source_filter(char const* sdpLine);
  97. static char* lookupPayloadFormat(unsigned char rtpPayloadType,
  98. unsigned& rtpTimestampFrequency,
  99. unsigned& numChannels);
  100. static unsigned guessRTPTimestampFrequency(char const* mediumName,
  101. char const* codecName);
  102. protected:
  103. friend class MediaSubsessionIterator;
  104. char* fCNAME; // used for RTCP
  105. // Linkage fields:
  106. MediaSubsession* fSubsessionsHead;
  107. MediaSubsession* fSubsessionsTail;
  108. // Fields set from a SDP description:
  109. char* fConnectionEndpointName;
  110. double fMaxPlayStartTime;
  111. double fMaxPlayEndTime;
  112. char* fAbsStartTime;
  113. char* fAbsEndTime;
  114. struct in_addr fSourceFilterAddr; // used for SSM
  115. float fScale; // set from a RTSP "Scale:" header
  116. float fSpeed;
  117. char* fMediaSessionType; // holds a=type value
  118. char* fSessionName; // holds s=<session name> value
  119. char* fSessionDescription; // holds i=<session description> value
  120. char* fControlPath; // holds optional a=control: string
  121. };
  122. class MediaSubsessionIterator {
  123. public:
  124. MediaSubsessionIterator(MediaSession const& session);
  125. virtual ~MediaSubsessionIterator();
  126. MediaSubsession* next(); // NULL if none
  127. void reset();
  128. private:
  129. MediaSession const& fOurSession;
  130. MediaSubsession* fNextPtr;
  131. };
  132. class MediaSubsession {
  133. public:
  134. MediaSession& parentSession() { return fParent; }
  135. MediaSession const& parentSession() const { return fParent; }
  136. unsigned short clientPortNum() const { return fClientPortNum; }
  137. unsigned char rtpPayloadFormat() const { return fRTPPayloadFormat; }
  138. char const* savedSDPLines() const { return fSavedSDPLines; }
  139. char const* mediumName() const { return fMediumName; }
  140. char const* codecName() const { return fCodecName; }
  141. char const* protocolName() const { return fProtocolName; }
  142. char const* controlPath() const { return fControlPath; }
  143. Boolean isSSM() const { return fSourceFilterAddr.s_addr != 0; }
  144. unsigned short videoWidth() const { return fVideoWidth; }
  145. unsigned short videoHeight() const { return fVideoHeight; }
  146. unsigned videoFPS() const { return fVideoFPS; }
  147. unsigned numChannels() const { return fNumChannels; }
  148. float& scale() { return fScale; }
  149. float& speed() { return fSpeed; }
  150. RTPSource* rtpSource() { return fRTPSource; }
  151. RTCPInstance* rtcpInstance() { return fRTCPInstance; }
  152. unsigned rtpTimestampFrequency() const { return fRTPTimestampFrequency; }
  153. Boolean rtcpIsMuxed() const { return fMultiplexRTCPWithRTP; }
  154. FramedSource* readSource() { return fReadSource; }
  155. // This is the source that client sinks read from. It is usually
  156. // (but not necessarily) the same as "rtpSource()"
  157. void addFilter(FramedFilter* filter);
  158. // Changes "readSource()" to "filter" (which must have just been created with "readSource()" as its input)
  159. double playStartTime() const;
  160. double playEndTime() const;
  161. char* absStartTime() const;
  162. char* absEndTime() const;
  163. // Used only to set the local fields:
  164. double& _playStartTime() { return fPlayStartTime; }
  165. double& _playEndTime() { return fPlayEndTime; }
  166. char*& _absStartTime() { return fAbsStartTime; }
  167. char*& _absEndTime() { return fAbsEndTime; }
  168. Boolean initiate(int useSpecialRTPoffset = -1);
  169. // Creates a "RTPSource" for this subsession. (Has no effect if it's
  170. // already been created.) Returns True iff this succeeds.
  171. void deInitiate(); // Destroys any previously created RTPSource
  172. Boolean setClientPortNum(unsigned short portNum);
  173. // Sets the preferred client port number that any "RTPSource" for
  174. // this subsession would use. (By default, the client port number
  175. // is gotten from the original SDP description, or - if the SDP
  176. // description does not specfy a client port number - an ephemeral
  177. // (even) port number is chosen.) This routine must *not* be
  178. // called after initiate().
  179. void receiveRawMP3ADUs() { fReceiveRawMP3ADUs = True; } // optional hack for audio/MPA-ROBUST; must not be called after initiate()
  180. void receiveRawJPEGFrames() { fReceiveRawJPEGFrames = True; } // optional hack for video/JPEG; must not be called after initiate()
  181. char*& connectionEndpointName() { return fConnectionEndpointName; }
  182. char const* connectionEndpointName() const {
  183. return fConnectionEndpointName;
  184. }
  185. // 'Bandwidth' parameter, set in the "b=" SDP line:
  186. unsigned bandwidth() const { return fBandwidth; }
  187. // General SDP attribute accessor functions:
  188. char const* attrVal_str(char const* attrName) const;
  189. // returns "" if attribute doesn't exist (and has no default value), or is not a string
  190. char const* attrVal_strToLower(char const* attrName) const;
  191. // returns "" if attribute doesn't exist (and has no default value), or is not a string
  192. unsigned attrVal_int(char const* attrName) const;
  193. // also returns 0 if attribute doesn't exist (and has no default value)
  194. unsigned attrVal_unsigned(char const* attrName) const { return (unsigned)attrVal_int(attrName); }
  195. Boolean attrVal_bool(char const* attrName) const { return attrVal_int(attrName) != 0; }
  196. // Old, now-deprecated SDP attribute accessor functions, kept here for backwards-compatibility:
  197. char const* fmtp_config() const;
  198. char const* fmtp_configuration() const { return fmtp_config(); }
  199. char const* fmtp_spropparametersets() const { return attrVal_str("sprop-parameter-sets"); }
  200. char const* fmtp_spropvps() const { return attrVal_str("sprop-vps"); }
  201. char const* fmtp_spropsps() const { return attrVal_str("sprop-sps"); }
  202. char const* fmtp_sproppps() const { return attrVal_str("sprop-pps"); }
  203. netAddressBits connectionEndpointAddress() const;
  204. // Converts "fConnectionEndpointName" to an address (or 0 if unknown)
  205. void setDestinations(netAddressBits defaultDestAddress);
  206. // Uses "fConnectionEndpointName" and "serverPortNum" to set
  207. // the destination address and port of the RTP and RTCP objects.
  208. // This is typically called by RTSP clients after doing "SETUP".
  209. char const* sessionId() const { return fSessionId; }
  210. void setSessionId(char const* sessionId);
  211. // Public fields that external callers can use to keep state.
  212. // (They are responsible for all storage management on these fields)
  213. unsigned short serverPortNum; // in host byte order (used by RTSP)
  214. unsigned char rtpChannelId, rtcpChannelId; // used by RTSP (for RTP/TCP)
  215. MediaSink* sink; // callers can use this to keep track of who's playing us
  216. void* miscPtr; // callers can use this for whatever they want
  217. // Parameters set from a RTSP "RTP-Info:" header:
  218. struct {
  219. u_int16_t seqNum;
  220. u_int32_t timestamp;
  221. Boolean infoIsNew; // not part of the RTSP header; instead, set whenever this struct is filled in
  222. } rtpInfo;
  223. double getNormalPlayTime(struct timeval const& presentationTime);
  224. // Computes the stream's "Normal Play Time" (NPT) from the given "presentationTime".
  225. // (For the definition of "Normal Play Time", see RFC 2326, section 3.6.)
  226. // This function is useful only if the "rtpInfo" structure was previously filled in
  227. // (e.g., by a "RTP-Info:" header in a RTSP response).
  228. // Also, for this function to work properly, the RTP stream's presentation times must (eventually) be
  229. // synchronized via RTCP.
  230. // (Note: If this function returns a negative number, then the result should be ignored by the caller.)
  231. protected:
  232. friend class MediaSession;
  233. friend class MediaSubsessionIterator;
  234. MediaSubsession(MediaSession& parent);
  235. virtual ~MediaSubsession();
  236. UsageEnvironment& env() { return fParent.envir(); }
  237. void setNext(MediaSubsession* next) { fNext = next; }
  238. void setAttribute(char const* name, char const* value = NULL, Boolean valueIsHexadecimal = False);
  239. Boolean parseSDPLine_c(char const* sdpLine);
  240. Boolean parseSDPLine_b(char const* sdpLine);
  241. Boolean parseSDPAttribute_rtpmap(char const* sdpLine);
  242. Boolean parseSDPAttribute_rtcpmux(char const* sdpLine);
  243. Boolean parseSDPAttribute_control(char const* sdpLine);
  244. Boolean parseSDPAttribute_range(char const* sdpLine);
  245. Boolean parseSDPAttribute_fmtp(char const* sdpLine);
  246. Boolean parseSDPAttribute_source_filter(char const* sdpLine);
  247. Boolean parseSDPAttribute_x_dimensions(char const* sdpLine);
  248. Boolean parseSDPAttribute_framerate(char const* sdpLine);
  249. virtual Boolean createSourceObjects(int useSpecialRTPoffset);
  250. // create "fRTPSource" and "fReadSource" member objects, after we've been initialized via SDP
  251. protected:
  252. // Linkage fields:
  253. MediaSession& fParent;
  254. MediaSubsession* fNext;
  255. // Fields set from a SDP description:
  256. char* fConnectionEndpointName; // may also be set by RTSP SETUP response
  257. unsigned short fClientPortNum; // in host byte order
  258. // This field is also set by initiate()
  259. unsigned char fRTPPayloadFormat;
  260. char* fSavedSDPLines;
  261. char* fMediumName;
  262. char* fCodecName;
  263. char* fProtocolName;
  264. unsigned fRTPTimestampFrequency;
  265. Boolean fMultiplexRTCPWithRTP;
  266. char* fControlPath; // holds optional a=control: string
  267. struct in_addr fSourceFilterAddr; // used for SSM
  268. unsigned fBandwidth; // in kilobits-per-second, from b= line
  269. double fPlayStartTime;
  270. double fPlayEndTime;
  271. char* fAbsStartTime;
  272. char* fAbsEndTime;
  273. unsigned short fVideoWidth, fVideoHeight;
  274. // screen dimensions (set by an optional a=x-dimensions: <w>,<h> line)
  275. unsigned fVideoFPS;
  276. // frame rate (set by an optional "a=framerate: <fps>" or "a=x-framerate: <fps>" line)
  277. unsigned fNumChannels;
  278. // optionally set by "a=rtpmap:" lines for audio sessions. Default: 1
  279. float fScale; // set from a RTSP "Scale:" header
  280. float fSpeed;
  281. double fNPT_PTS_Offset; // set by "getNormalPlayTime()"; add this to a PTS to get NPT
  282. HashTable* fAttributeTable; // for "a=fmtp:" attributes. (Later an array by payload type #####)
  283. // Fields set or used by initiate():
  284. Groupsock* fRTPSocket; Groupsock* fRTCPSocket; // works even for unicast
  285. RTPSource* fRTPSource; RTCPInstance* fRTCPInstance;
  286. FramedSource* fReadSource;
  287. Boolean fReceiveRawMP3ADUs, fReceiveRawJPEGFrames;
  288. // Other fields:
  289. char* fSessionId; // used by RTSP
  290. };
  291. #endif