ServerMediaSession.hh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 *streamers* - i.e., servers.
  19. // For media receivers, use "MediaSession" instead.)
  20. // C++ header
  21. #ifndef _SERVER_MEDIA_SESSION_HH
  22. #define _SERVER_MEDIA_SESSION_HH
  23. #ifndef _RTCP_HH
  24. #include "RTCP.hh"
  25. #endif
  26. class ServerMediaSubsession; // forward
  27. class ServerMediaSession: public Medium {
  28. public:
  29. static ServerMediaSession* createNew(UsageEnvironment& env,
  30. char const* streamName = NULL,
  31. char const* info = NULL,
  32. char const* description = NULL,
  33. Boolean isSSM = False,
  34. char const* miscSDPLines = NULL);
  35. static Boolean lookupByName(UsageEnvironment& env,
  36. char const* mediumName,
  37. ServerMediaSession*& resultSession);
  38. char* generateSDPDescription(); // based on the entire session
  39. // Note: The caller is responsible for freeing the returned string
  40. char const* streamName() const { return fStreamName; }
  41. Boolean addSubsession(ServerMediaSubsession* subsession);
  42. unsigned numSubsessions() const { return fSubsessionCounter; }
  43. void testScaleFactor(float& scale); // sets "scale" to the actual supported scale
  44. float duration() const;
  45. // a result == 0 means an unbounded session (the default)
  46. // a result < 0 means: subsession durations differ; the result is -(the largest).
  47. // a result > 0 means: this is the duration of a bounded session
  48. virtual void noteLiveness();
  49. // called whenever a client - accessing this media - notes liveness.
  50. // The default implementation does nothing, but subclasses can redefine this - e.g., if you
  51. // want to remove long-unused "ServerMediaSession"s from the server.
  52. unsigned referenceCount() const { return fReferenceCount; }
  53. void incrementReferenceCount() { ++fReferenceCount; }
  54. void decrementReferenceCount() { if (fReferenceCount > 0) --fReferenceCount; }
  55. Boolean& deleteWhenUnreferenced() { return fDeleteWhenUnreferenced; }
  56. void deleteAllSubsessions();
  57. // Removes and deletes all subsessions added by "addSubsession()", returning us to an 'empty' state
  58. // Note: If you have already added this "ServerMediaSession" to a "RTSPServer" then, before calling this function,
  59. // you must first close any client connections that use it,
  60. // by calling "RTSPServer::closeAllClientSessionsForServerMediaSession()".
  61. protected:
  62. ServerMediaSession(UsageEnvironment& env, char const* streamName,
  63. char const* info, char const* description,
  64. Boolean isSSM, char const* miscSDPLines);
  65. // called only by "createNew()"
  66. virtual ~ServerMediaSession();
  67. private: // redefined virtual functions
  68. virtual Boolean isServerMediaSession() const;
  69. private:
  70. Boolean fIsSSM;
  71. // Linkage fields:
  72. friend class ServerMediaSubsessionIterator;
  73. ServerMediaSubsession* fSubsessionsHead;
  74. ServerMediaSubsession* fSubsessionsTail;
  75. unsigned fSubsessionCounter;
  76. char* fStreamName;
  77. char* fInfoSDPString;
  78. char* fDescriptionSDPString;
  79. char* fMiscSDPLines;
  80. struct timeval fCreationTime;
  81. unsigned fReferenceCount;
  82. Boolean fDeleteWhenUnreferenced;
  83. };
  84. class ServerMediaSubsessionIterator {
  85. public:
  86. ServerMediaSubsessionIterator(ServerMediaSession& session);
  87. virtual ~ServerMediaSubsessionIterator();
  88. ServerMediaSubsession* next(); // NULL if none
  89. void reset();
  90. private:
  91. ServerMediaSession& fOurSession;
  92. ServerMediaSubsession* fNextPtr;
  93. };
  94. class ServerMediaSubsession: public Medium {
  95. public:
  96. unsigned trackNumber() const { return fTrackNumber; }
  97. char const* trackId();
  98. virtual char const* sdpLines() = 0;
  99. virtual void getStreamParameters(unsigned clientSessionId, // in
  100. netAddressBits clientAddress, // in
  101. Port const& clientRTPPort, // in
  102. Port const& clientRTCPPort, // in
  103. int tcpSocketNum, // in (-1 means use UDP, not TCP)
  104. unsigned char rtpChannelId, // in (used if TCP)
  105. unsigned char rtcpChannelId, // in (used if TCP)
  106. netAddressBits& destinationAddress, // in out
  107. u_int8_t& destinationTTL, // in out
  108. Boolean& isMulticast, // out
  109. Port& serverRTPPort, // out
  110. Port& serverRTCPPort, // out
  111. void*& streamToken // out
  112. ) = 0;
  113. virtual void startStream(unsigned clientSessionId, void* streamToken,
  114. TaskFunc* rtcpRRHandler,
  115. void* rtcpRRHandlerClientData,
  116. unsigned short& rtpSeqNum,
  117. unsigned& rtpTimestamp,
  118. ServerRequestAlternativeByteHandler* serverRequestAlternativeByteHandler,
  119. void* serverRequestAlternativeByteHandlerClientData) = 0;
  120. virtual void pauseStream(unsigned clientSessionId, void* streamToken);
  121. virtual void seekStream(unsigned clientSessionId, void* streamToken, double& seekNPT,
  122. double streamDuration, u_int64_t& numBytes);
  123. // This routine is used to seek by relative (i.e., NPT) time.
  124. // "streamDuration", if >0.0, specifies how much data to stream, past "seekNPT". (If <=0.0, all remaining data is streamed.)
  125. // "numBytes" returns the size (in bytes) of the data to be streamed, or 0 if unknown or unlimited.
  126. virtual void seekStream(unsigned clientSessionId, void* streamToken, char*& absStart, char*& absEnd);
  127. // This routine is used to seek by 'absolute' time.
  128. // "absStart" should be a string of the form "YYYYMMDDTHHMMSSZ" or "YYYYMMDDTHHMMSS.<frac>Z".
  129. // "absEnd" should be either NULL (for no end time), or a string of the same form as "absStart".
  130. // These strings may be modified in-place, or can be reassigned to a newly-allocated value (after delete[]ing the original).
  131. virtual void nullSeekStream(unsigned clientSessionId, void* streamToken,
  132. double streamEndTime, u_int64_t& numBytes);
  133. // Called whenever we're handling a "PLAY" command without a specified start time.
  134. virtual void setStreamScale(unsigned clientSessionId, void* streamToken, float scale);
  135. virtual float getCurrentNPT(void* streamToken);
  136. virtual FramedSource* getStreamSource(void* streamToken);
  137. virtual void getRTPSinkandRTCP(void* streamToken,
  138. RTPSink const*& rtpSink, RTCPInstance const*& rtcp) = 0;
  139. // Returns pointers to the "RTPSink" and "RTCPInstance" objects for "streamToken".
  140. // (This can be useful if you want to get the associated 'Groupsock' objects, for example.)
  141. // You must not delete these objects, or start/stop playing them; instead, that is done
  142. // using the "startStream()" and "deleteStream()" functions.
  143. virtual void deleteStream(unsigned clientSessionId, void*& streamToken);
  144. virtual void testScaleFactor(float& scale); // sets "scale" to the actual supported scale
  145. virtual float duration() const;
  146. // returns 0 for an unbounded session (the default)
  147. // returns > 0 for a bounded session
  148. virtual void getAbsoluteTimeRange(char*& absStartTime, char*& absEndTime) const;
  149. // Subclasses can reimplement this iff they support seeking by 'absolute' time.
  150. // The following may be called by (e.g.) SIP servers, for which the
  151. // address and port number fields in SDP descriptions need to be non-zero:
  152. void setServerAddressAndPortForSDP(netAddressBits addressBits,
  153. portNumBits portBits);
  154. protected: // we're a virtual base class
  155. ServerMediaSubsession(UsageEnvironment& env);
  156. virtual ~ServerMediaSubsession();
  157. char const* rangeSDPLine() const;
  158. // returns a string to be delete[]d
  159. ServerMediaSession* fParentSession;
  160. netAddressBits fServerAddressForSDP;
  161. portNumBits fPortNumForSDP;
  162. private:
  163. friend class ServerMediaSession;
  164. friend class ServerMediaSubsessionIterator;
  165. ServerMediaSubsession* fNext;
  166. unsigned fTrackNumber; // within an enclosing ServerMediaSession
  167. char const* fTrackId;
  168. };
  169. #endif