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