live
OnDemandServerMediaSubsession.hh
Go to the documentation of this file.
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 
7 This library is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 **********/
16 // "liveMedia"
17 // Copyright (c) 1996-2021 Live Networks, Inc. All rights reserved.
18 // A 'ServerMediaSubsession' object that creates new, unicast, "RTPSink"s
19 // on demand.
20 // C++ header
21 
22 #ifndef _ON_DEMAND_SERVER_MEDIA_SUBSESSION_HH
23 #define _ON_DEMAND_SERVER_MEDIA_SUBSESSION_HH
24 
25 #ifndef _SERVER_MEDIA_SESSION_HH
26 #include "ServerMediaSession.hh"
27 #endif
28 #ifndef _RTP_SINK_HH
29 #include "RTPSink.hh"
30 #endif
31 #ifndef _BASIC_UDP_SINK_HH
32 #include "BasicUDPSink.hh"
33 #endif
34 #ifndef _RTCP_HH
35 #include "RTCP.hh"
36 #endif
37 
39 protected: // we're a virtual base class
41  portNumBits initialPortNum = 6970,
44 
45 protected: // redefined virtual functions
46  virtual char const* sdpLines(int addressFamily);
47  virtual void getStreamParameters(unsigned clientSessionId,
48  struct sockaddr_storage const& clientAddress,
49  Port const& clientRTPPort,
50  Port const& clientRTCPPort,
51  int tcpSocketNum,
52  unsigned char rtpChannelId,
53  unsigned char rtcpChannelId,
54  TLSState* tlsState,
55  struct sockaddr_storage& destinationAddress,
56  u_int8_t& destinationTTL,
57  Boolean& isMulticast,
58  Port& serverRTPPort,
59  Port& serverRTCPPort,
60  void*& streamToken);
61  virtual void startStream(unsigned clientSessionId, void* streamToken,
62  TaskFunc* rtcpRRHandler,
63  void* rtcpRRHandlerClientData,
64  unsigned short& rtpSeqNum,
65  unsigned& rtpTimestamp,
66  ServerRequestAlternativeByteHandler* serverRequestAlternativeByteHandler,
67  void* serverRequestAlternativeByteHandlerClientData);
68  virtual void pauseStream(unsigned clientSessionId, void* streamToken);
69  virtual void seekStream(unsigned clientSessionId, void* streamToken, double& seekNPT, double streamDuration, u_int64_t& numBytes);
70  virtual void seekStream(unsigned clientSessionId, void* streamToken, char*& absStart, char*& absEnd);
71  virtual void nullSeekStream(unsigned clientSessionId, void* streamToken,
72  double streamEndTime, u_int64_t& numBytes);
73  virtual void setStreamScale(unsigned clientSessionId, void* streamToken, float scale);
74  virtual float getCurrentNPT(void* streamToken);
75  virtual FramedSource* getStreamSource(void* streamToken);
76  virtual void getRTPSinkandRTCP(void* streamToken,
77  RTPSink const*& rtpSink, RTCPInstance const*& rtcp);
78  virtual void deleteStream(unsigned clientSessionId, void*& streamToken);
79 
80 protected: // new virtual functions, possibly redefined by subclasses
81  virtual char const* getAuxSDPLine(RTPSink* rtpSink,
82  FramedSource* inputSource);
83  virtual void seekStreamSource(FramedSource* inputSource, double& seekNPT, double streamDuration, u_int64_t& numBytes);
84  // This routine is used to seek by relative (i.e., NPT) time.
85  // "streamDuration", if >0.0, specifies how much data to stream, past "seekNPT". (If <=0.0, all remaining data is streamed.)
86  // "numBytes" returns the size (in bytes) of the data to be streamed, or 0 if unknown or unlimited.
87  virtual void seekStreamSource(FramedSource* inputSource, char*& absStart, char*& absEnd);
88  // This routine is used to seek by 'absolute' time.
89  // "absStart" should be a string of the form "YYYYMMDDTHHMMSSZ" or "YYYYMMDDTHHMMSS.<frac>Z".
90  // "absEnd" should be either NULL (for no end time), or a string of the same form as "absStart".
91  // These strings may be modified in-place, or can be reassigned to a newly-allocated value (after delete[]ing the original).
92  virtual void setStreamSourceScale(FramedSource* inputSource, float scale);
93  virtual void setStreamSourceDuration(FramedSource* inputSource, double streamDuration, u_int64_t& numBytes);
94  virtual void closeStreamSource(FramedSource* inputSource);
95 
96 protected: // new virtual functions, defined by all subclasses
97  virtual FramedSource* createNewStreamSource(unsigned clientSessionId,
98  unsigned& estBitrate) = 0;
99  // "estBitrate" is the stream's estimated bitrate, in kbps
100  virtual RTPSink* createNewRTPSink(Groupsock* rtpGroupsock,
101  unsigned char rtpPayloadTypeIfDynamic,
102  FramedSource* inputSource) = 0;
103 
104 protected: // new virtual functions, may be redefined by a subclass:
105  virtual Groupsock* createGroupsock(struct sockaddr_storage const& addr, Port port);
106  virtual RTCPInstance* createRTCP(Groupsock* RTCPgs, unsigned totSessionBW, /* in kbps */
107  unsigned char const* cname, RTPSink* sink);
108 
109 public:
111  // An alternative to passing the "multiplexRTCPWithRTP" parameter as True in the constructor
112 
113  void setRTCPAppPacketHandler(RTCPAppHandlerFunc* handler, void* clientData);
114  // Sets a handler to be called if a RTCP "APP" packet arrives from any future client.
115  // (Any current clients are not affected; any "APP" packets from them will continue to be
116  // handled by whatever handler existed when the client sent its first RTSP "PLAY" command.)
117  // (Call with (NULL, NULL) to remove an existing handler - for future clients only)
118 
119  void sendRTCPAppPacket(u_int8_t subtype, char const* name,
120  u_int8_t* appDependentData, unsigned appDependentDataSize);
121  // Sends a custom RTCP "APP" packet to the most recent client (if "reuseFirstSource" was False),
122  // or to all current clients (if "reuseFirstSource" was True).
123  // The parameters correspond to their
124  // respective fields as described in the RTP/RTCP definition (RFC 3550).
125  // Note that only the low-order 5 bits of "subtype" are used, and only the first 4 bytes
126  // of "name" are used. (If "name" has fewer than 4 bytes, or is NULL,
127  // then the remaining bytes are '\0'.)
128 
129 protected:
130  void setSDPLinesFromRTPSink(RTPSink* rtpSink, FramedSource* inputSource,
131  unsigned estBitrate);
132  // used to implement "sdpLines()"
133 
134 protected:
135  char* fSDPLines;
136  HashTable* fDestinationsHashTable; // indexed by client session id
137 
138 private:
143  char fCNAME[100]; // for RTCP
146  friend class StreamState;
147 };
148 
149 
150 // A class that represents the state of an ongoing stream. This is used only internally, in the implementation of
151 // "OnDemandServerMediaSubsession", but we expose the definition here, in case subclasses of "OnDemandServerMediaSubsession"
152 // want to access it.
153 
155 public:
156  Destinations(struct sockaddr_storage const& destAddr,
157  Port const& rtpDestPort,
158  Port const& rtcpDestPort)
159  : isTCP(False), addr(destAddr), rtpPort(rtpDestPort), rtcpPort(rtcpDestPort) {
160  }
161  Destinations(int tcpSockNum, unsigned char rtpChanId, unsigned char rtcpChanId,
162  TLSState* tlsSt)
163  : isTCP(True), rtpPort(0) /*dummy*/, rtcpPort(0) /*dummy*/,
164  tcpSocketNum(tcpSockNum), rtpChannelId(rtpChanId), rtcpChannelId(rtcpChanId),
165  tlsState(tlsSt) {
166  }
167 
168 public:
170  struct sockaddr_storage addr;
174  unsigned char rtpChannelId, rtcpChannelId;
176 };
177 
178 class StreamState {
179 public:
181  Port const& serverRTPPort, Port const& serverRTCPPort,
182  RTPSink* rtpSink, BasicUDPSink* udpSink,
183  unsigned totalBW, FramedSource* mediaSource,
184  Groupsock* rtpGS, Groupsock* rtcpGS);
185  virtual ~StreamState();
186 
187  void startPlaying(Destinations* destinations, unsigned clientSessionId,
188  TaskFunc* rtcpRRHandler, void* rtcpRRHandlerClientData,
189  ServerRequestAlternativeByteHandler* serverRequestAlternativeByteHandler,
190  void* serverRequestAlternativeByteHandlerClientData);
191  void pause();
192  void sendRTCPAppPacket(u_int8_t subtype, char const* name,
193  u_int8_t* appDependentData, unsigned appDependentDataSize);
194  void endPlaying(Destinations* destinations, unsigned clientSessionId);
195  void reclaim();
196 
197  unsigned& referenceCount() { return fReferenceCount; }
198 
199  Port const& serverRTPPort() const { return fServerRTPPort; }
200  Port const& serverRTCPPort() const { return fServerRTCPPort; }
201 
202  RTPSink* rtpSink() const { return fRTPSink; }
204 
205  float streamDuration() const { return fStreamDuration; }
206 
207  FramedSource* mediaSource() const { return fMediaSource; }
208  float& startNPT() { return fStartNPT; }
209 
210 private:
213  unsigned fReferenceCount;
214 
216 
219 
221  unsigned fTotalBW;
223 
225  float fStartNPT; // initial 'normal play time'; reset after each seek
226 
229 };
230 
231 #endif
const Boolean False
Definition: Boolean.hh:28
const Boolean True
Definition: Boolean.hh:31
unsigned char Boolean
Definition: Boolean.hh:25
u_int16_t portNumBits
Definition: NetAddress.hh:102
void RTCPAppHandlerFunc(void *clientData, u_int8_t subtype, u_int32_t nameBytes, u_int8_t *appDependentData, unsigned appDependentDataSize)
Definition: RTCP.hh:45
void ServerRequestAlternativeByteHandler(void *instance, u_int8_t requestByte)
Definition: RTPInterface.hh:41
void TaskFunc(void *clientData)
Destinations(int tcpSockNum, unsigned char rtpChanId, unsigned char rtcpChanId, TLSState *tlsSt)
struct sockaddr_storage addr
Destinations(struct sockaddr_storage const &destAddr, Port const &rtpDestPort, Port const &rtcpDestPort)
char const * name() const
Definition: Media.hh:61
virtual char const * sdpLines(int addressFamily)
virtual void seekStreamSource(FramedSource *inputSource, double &seekNPT, double streamDuration, u_int64_t &numBytes)
virtual void seekStream(unsigned clientSessionId, void *streamToken, double &seekNPT, double streamDuration, u_int64_t &numBytes)
void sendRTCPAppPacket(u_int8_t subtype, char const *name, u_int8_t *appDependentData, unsigned appDependentDataSize)
virtual void getStreamParameters(unsigned clientSessionId, struct sockaddr_storage const &clientAddress, Port const &clientRTPPort, Port const &clientRTCPPort, int tcpSocketNum, unsigned char rtpChannelId, unsigned char rtcpChannelId, TLSState *tlsState, struct sockaddr_storage &destinationAddress, u_int8_t &destinationTTL, Boolean &isMulticast, Port &serverRTPPort, Port &serverRTCPPort, void *&streamToken)
virtual FramedSource * createNewStreamSource(unsigned clientSessionId, unsigned &estBitrate)=0
virtual FramedSource * getStreamSource(void *streamToken)
virtual void setStreamScale(unsigned clientSessionId, void *streamToken, float scale)
virtual float getCurrentNPT(void *streamToken)
virtual void startStream(unsigned clientSessionId, void *streamToken, TaskFunc *rtcpRRHandler, void *rtcpRRHandlerClientData, unsigned short &rtpSeqNum, unsigned &rtpTimestamp, ServerRequestAlternativeByteHandler *serverRequestAlternativeByteHandler, void *serverRequestAlternativeByteHandlerClientData)
virtual void setStreamSourceDuration(FramedSource *inputSource, double streamDuration, u_int64_t &numBytes)
virtual Groupsock * createGroupsock(struct sockaddr_storage const &addr, Port port)
virtual void setStreamSourceScale(FramedSource *inputSource, float scale)
virtual void seekStream(unsigned clientSessionId, void *streamToken, char *&absStart, char *&absEnd)
virtual void seekStreamSource(FramedSource *inputSource, char *&absStart, char *&absEnd)
virtual char const * getAuxSDPLine(RTPSink *rtpSink, FramedSource *inputSource)
void setSDPLinesFromRTPSink(RTPSink *rtpSink, FramedSource *inputSource, unsigned estBitrate)
virtual void closeStreamSource(FramedSource *inputSource)
virtual void getRTPSinkandRTCP(void *streamToken, RTPSink const *&rtpSink, RTCPInstance const *&rtcp)
void setRTCPAppPacketHandler(RTCPAppHandlerFunc *handler, void *clientData)
virtual void pauseStream(unsigned clientSessionId, void *streamToken)
virtual RTPSink * createNewRTPSink(Groupsock *rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource *inputSource)=0
virtual RTCPInstance * createRTCP(Groupsock *RTCPgs, unsigned totSessionBW, unsigned char const *cname, RTPSink *sink)
virtual void nullSeekStream(unsigned clientSessionId, void *streamToken, double streamEndTime, u_int64_t &numBytes)
OnDemandServerMediaSubsession(UsageEnvironment &env, Boolean reuseFirstSource, portNumBits initialPortNum=6970, Boolean multiplexRTCPWithRTP=False)
virtual void deleteStream(unsigned clientSessionId, void *&streamToken)
RTCPInstance * rtcpInstance() const
Port const & serverRTCPPort() const
void sendRTCPAppPacket(u_int8_t subtype, char const *name, u_int8_t *appDependentData, unsigned appDependentDataSize)
virtual ~StreamState()
void pause()
StreamState(OnDemandServerMediaSubsession &master, Port const &serverRTPPort, Port const &serverRTCPPort, RTPSink *rtpSink, BasicUDPSink *udpSink, unsigned totalBW, FramedSource *mediaSource, Groupsock *rtpGS, Groupsock *rtcpGS)
void endPlaying(Destinations *destinations, unsigned clientSessionId)
void startPlaying(Destinations *destinations, unsigned clientSessionId, TaskFunc *rtcpRRHandler, void *rtcpRRHandlerClientData, ServerRequestAlternativeByteHandler *serverRequestAlternativeByteHandler, void *serverRequestAlternativeByteHandlerClientData)
FramedSource * mediaSource() const
Port const & serverRTPPort() const
void reclaim()
OnDemandServerMediaSubsession & fMaster