live
GenericMediaServer.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 generic media server class, used to implement a RTSP server, and any other server that uses
19 // "ServerMediaSession" objects to describe media to be served.
20 // C++ header
21 
22 #ifndef _GENERIC_MEDIA_SERVER_HH
23 #define _GENERIC_MEDIA_SERVER_HH
24 
25 #ifndef _MEDIA_HH
26 #include "Media.hh"
27 #endif
28 #ifndef _SERVER_MEDIA_SESSION_HH
29 #include "ServerMediaSession.hh"
30 #endif
31 
32 #ifndef REQUEST_BUFFER_SIZE
33 #define REQUEST_BUFFER_SIZE 20000 // for incoming requests
34 #endif
35 #ifndef RESPONSE_BUFFER_SIZE
36 #define RESPONSE_BUFFER_SIZE 20000
37 #endif
38 
39 // Typedef for a handler function that gets called when "lookupServerMediaSession()"
40 // (defined below) completes:
41 typedef void lookupServerMediaSessionCompletionFunc(void* clientData,
42  ServerMediaSession* sessionLookedUp);
43 
44 class GenericMediaServer: public Medium {
45 public:
46  void addServerMediaSession(ServerMediaSession* serverMediaSession);
47 
48  virtual void lookupServerMediaSession(char const* streamName,
50  void* completionClientData,
51  Boolean isFirstLookupInSession = True);
52  // Note: This is a virtual function, so can be reimplemented by subclasses.
53  void lookupServerMediaSession(char const* streamName,
54  void (GenericMediaServer::*memberFunc)(ServerMediaSession*));
55  // Special case of "lookupServerMediaSession()" where the 'completion function' is a
56  // member function of "GenericMediaServer" (and the 'completion client data' is "this".)
57 
58  void removeServerMediaSession(ServerMediaSession* serverMediaSession);
59  // Removes the "ServerMediaSession" object from our lookup table, so it will no longer be accessible by new clients.
60  // (However, any *existing* client sessions that use this "ServerMediaSession" object will continue streaming.
61  // The "ServerMediaSession" object will not get deleted until all of these client sessions have closed.)
62  // (To both delete the "ServerMediaSession" object *and* close all client sessions that use it,
63  // call "deleteServerMediaSession(serverMediaSession)" instead.)
64  virtual void removeServerMediaSession(char const* streamName);
65  // ditto
66 
68  // Closes (from the server) all client sessions that are currently using this "ServerMediaSession" object.
69  // Note, however, that the "ServerMediaSession" object remains accessible by new clients.
70  virtual void closeAllClientSessionsForServerMediaSession(char const* streamName);
71  // ditto
72 
73  void deleteServerMediaSession(ServerMediaSession* serverMediaSession);
74  // Equivalent to:
75  // "closeAllClientSessionsForServerMediaSession(serverMediaSession); removeServerMediaSession(serverMediaSession);"
76  virtual void deleteServerMediaSession(char const* streamName);
77  // Equivalent to:
78  // "closeAllClientSessionsForServerMediaSession(streamName); removeServerMediaSession(streamName);
79 
80  unsigned numClientSessions() const { return fClientSessions->numEntries(); }
81 
82 protected:
83  GenericMediaServer(UsageEnvironment& env, int ourSocketIPv4, int ourSocketIPv6, Port ourPort,
84  unsigned reclamationSeconds);
85  // If "reclamationSeconds" > 0, then the "ClientSession" state for each client will get
86  // reclaimed if no activity from the client is detected in at least "reclamationSeconds".
87  // we're an abstract base class
89  void cleanup(); // MUST be called in the destructor of any subclass of us
90 
91  static int setUpOurSocket(UsageEnvironment& env, Port& ourPort, int domain);
92 
93  static void incomingConnectionHandlerIPv4(void*, int /*mask*/);
94  static void incomingConnectionHandlerIPv6(void*, int /*mask*/);
97  void incomingConnectionHandlerOnSocket(int serverSocket);
98 
99  void setTLSFileNames(char const* certFileName, char const* privKeyFileName);
100 
101 public: // should be protected, but some old compilers complain otherwise
102  // The state of a TCP connection used by a client:
104  protected:
106  int clientSocket, struct sockaddr_storage const& clientAddr,
107  Boolean useTLS);
108  virtual ~ClientConnection();
109 
111  void closeSockets();
112 
113  static void incomingRequestHandler(void*, int /*mask*/);
115  virtual void handleRequestBytes(int newBytesRead) = 0;
117 
118  protected:
119  friend class GenericMediaServer;
120  friend class ClientSession;
121  friend class RTSPServer; // needed to make some broken Windows compilers work; remove this in the future when we end support for Windows
124  struct sockaddr_storage fClientAddr;
128 
129  // Optional support for TLS:
131  };
132 
133  // The state of an individual client session (using one or more sequential TCP connections) handled by a server:
135  protected:
136  ClientSession(GenericMediaServer& ourServer, u_int32_t sessionId);
137  virtual ~ClientSession();
138 
140  void noteLiveness();
141  static void noteClientLiveness(ClientSession* clientSession);
142  static void livenessTimeoutTask(ClientSession* clientSession);
143 
144  protected:
145  friend class GenericMediaServer;
146  friend class ClientConnection;
148  u_int32_t fOurSessionId;
151  };
152 
153 protected:
154  virtual ClientConnection* createNewClientConnection(int clientSocket, struct sockaddr_storage const& clientAddr) = 0;
155  virtual ClientSession* createNewClientSession(u_int32_t sessionId) = 0;
156 
158  // Generates a new (unused) random session id, and calls the "createNewClientSession()"
159  // virtual function with this session id as parameter.
160 
161  // Lookup a "ClientSession" object by sessionId (integer, and string):
162  ClientSession* lookupClientSession(u_int32_t sessionId);
163  ClientSession* lookupClientSession(char const* sessionIdStr);
164 
165  // An iterator over our "ServerMediaSession" objects:
167  public:
171  private:
173  };
174 
175  // The basic, synchronous "ServerMediaSession" lookup operation; only for subclasses:
176  ServerMediaSession* getServerMediaSession(char const* streamName);
177 
178 protected:
179  friend class ClientConnection;
180  friend class ClientSession;
185 
186 private:
187  HashTable* fServerMediaSessions; // maps 'stream name' strings to "ServerMediaSession" objects
188  HashTable* fClientConnections; // the "ClientConnection" objects that we're using
189  HashTable* fClientSessions; // maps 'session id' strings to "ClientSession" objects
191 
194 };
195 
196 // A data structure used for optional user/password authentication:
197 
199 public:
202  // If "passwordsAreMD5" is True, then each password stored into, or removed from,
203  // the database is actually the value computed
204  // by md5(<username>:<realm>:<actual-password>)
206 
207  virtual void addUserRecord(char const* username, char const* password);
208  virtual void removeUserRecord(char const* username);
209 
210  virtual char const* lookupPassword(char const* username);
211  // returns NULL if the user name was not present
212 
213  char const* realm() { return fRealm; }
215 
216 protected:
218  char* fRealm;
220 };
221 
222 #endif
const Boolean False
Definition: Boolean.hh:28
const Boolean True
Definition: Boolean.hh:31
unsigned char Boolean
Definition: Boolean.hh:25
void lookupServerMediaSessionCompletionFunc(void *clientData, ServerMediaSession *sessionLookedUp)
#define RESPONSE_BUFFER_SIZE
#define REQUEST_BUFFER_SIZE
#define NULL
void * TaskToken
virtual void handleRequestBytes(int newBytesRead)=0
static void incomingRequestHandler(void *, int)
unsigned char fRequestBuffer[REQUEST_BUFFER_SIZE]
unsigned char fResponseBuffer[RESPONSE_BUFFER_SIZE]
ClientConnection(GenericMediaServer &ourServer, int clientSocket, struct sockaddr_storage const &clientAddr, Boolean useTLS)
ClientSession(GenericMediaServer &ourServer, u_int32_t sessionId)
static void livenessTimeoutTask(ClientSession *clientSession)
ServerMediaSession * fOurServerMediaSession
static void noteClientLiveness(ClientSession *clientSession)
ServerMediaSessionIterator(GenericMediaServer &server)
unsigned numClientSessions() const
virtual ClientConnection * createNewClientConnection(int clientSocket, struct sockaddr_storage const &clientAddr)=0
void lookupServerMediaSession(char const *streamName, void(GenericMediaServer::*memberFunc)(ServerMediaSession *))
void deleteServerMediaSession(ServerMediaSession *serverMediaSession)
char const * fTLSCertificateFileName
char const * fTLSPrivateKeyFileName
u_int32_t fPreviousClientSessionId
virtual void deleteServerMediaSession(char const *streamName)
void addServerMediaSession(ServerMediaSession *serverMediaSession)
virtual void closeAllClientSessionsForServerMediaSession(char const *streamName)
void incomingConnectionHandlerIPv6()
ClientSession * lookupClientSession(u_int32_t sessionId)
void removeServerMediaSession(ServerMediaSession *serverMediaSession)
static void incomingConnectionHandlerIPv4(void *, int)
static void incomingConnectionHandlerIPv6(void *, int)
HashTable * fClientConnections
HashTable * fServerMediaSessions
void setTLSFileNames(char const *certFileName, char const *privKeyFileName)
void incomingConnectionHandlerOnSocket(int serverSocket)
ClientSession * createNewClientSessionWithId()
virtual void lookupServerMediaSession(char const *streamName, lookupServerMediaSessionCompletionFunc *completionFunc, void *completionClientData, Boolean isFirstLookupInSession=True)
GenericMediaServer(UsageEnvironment &env, int ourSocketIPv4, int ourSocketIPv6, Port ourPort, unsigned reclamationSeconds)
ServerMediaSession * getServerMediaSession(char const *streamName)
virtual ~GenericMediaServer()
static int setUpOurSocket(UsageEnvironment &env, Port &ourPort, int domain)
virtual ClientSession * createNewClientSession(u_int32_t sessionId)=0
void incomingConnectionHandlerIPv4()
ClientSession * lookupClientSession(char const *sessionIdStr)
virtual void removeServerMediaSession(char const *streamName)
void closeAllClientSessionsForServerMediaSession(ServerMediaSession *serverMediaSession)
virtual unsigned numEntries() const =0
Definition: Media.hh:50
UsageEnvironment & envir() const
Definition: Media.hh:59
virtual void removeUserRecord(char const *username)
virtual char const * lookupPassword(char const *username)
virtual void addUserRecord(char const *username, char const *password)
UserAuthenticationDatabase(char const *realm=NULL, Boolean passwordsAreMD5=False)
virtual ~UserAuthenticationDatabase()