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
88  virtual ~GenericMediaServer();
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 public: // should be protected, but some old compilers complain otherwise
100  // The state of a TCP connection used by a client:
102  protected:
103  ClientConnection(GenericMediaServer& ourServer, int clientSocket, struct sockaddr_storage const& clientAddr);
104  virtual ~ClientConnection();
105 
107  void closeSockets();
108 
109  static void incomingRequestHandler(void*, int /*mask*/);
110  void incomingRequestHandler();
111  virtual void handleRequestBytes(int newBytesRead) = 0;
112  void resetRequestBuffer();
113 
114  protected:
115  friend class GenericMediaServer;
116  friend class ClientSession;
117  friend class RTSPServer; // needed to make some broken Windows compilers work; remove this in the future when we end support for Windows
120  struct sockaddr_storage fClientAddr;
124  };
125 
126  // The state of an individual client session (using one or more sequential TCP connections) handled by a server:
128  protected:
129  ClientSession(GenericMediaServer& ourServer, u_int32_t sessionId);
130  virtual ~ClientSession();
131 
133  void noteLiveness();
134  static void noteClientLiveness(ClientSession* clientSession);
135  static void livenessTimeoutTask(ClientSession* clientSession);
136 
137  protected:
138  friend class GenericMediaServer;
139  friend class ClientConnection;
141  u_int32_t fOurSessionId;
144  };
145 
146 protected:
147  virtual ClientConnection* createNewClientConnection(int clientSocket, struct sockaddr_storage const& clientAddr) = 0;
148  virtual ClientSession* createNewClientSession(u_int32_t sessionId) = 0;
149 
151  // Generates a new (unused) random session id, and calls the "createNewClientSession()"
152  // virtual function with this session id as parameter.
153 
154  // Lookup a "ClientSession" object by sessionId (integer, and string):
155  ClientSession* lookupClientSession(u_int32_t sessionId);
156  ClientSession* lookupClientSession(char const* sessionIdStr);
157 
158  // An iterator over our "ServerMediaSession" objects:
160  public:
162  virtual ~ServerMediaSessionIterator();
164  private:
166  };
167 
168  // The basic, synchronous "ServerMediaSession" lookup operation; only for subclasses:
169  ServerMediaSession* getServerMediaSession(char const* streamName);
170 
171 protected:
172  friend class ClientConnection;
173  friend class ClientSession;
178 
179 private:
180  HashTable* fServerMediaSessions; // maps 'stream name' strings to "ServerMediaSession" objects
181  HashTable* fClientConnections; // the "ClientConnection" objects that we're using
182  HashTable* fClientSessions; // maps 'session id' strings to "ClientSession" objects
184 };
185 
186 // A data structure used for optional user/password authentication:
187 
189 public:
190  UserAuthenticationDatabase(char const* realm = NULL,
192  // If "passwordsAreMD5" is True, then each password stored into, or removed from,
193  // the database is actually the value computed
194  // by md5(<username>:<realm>:<actual-password>)
195  virtual ~UserAuthenticationDatabase();
196 
197  virtual void addUserRecord(char const* username, char const* password);
198  virtual void removeUserRecord(char const* username);
199 
200  virtual char const* lookupPassword(char const* username);
201  // returns NULL if the user name was not present
202 
203  char const* realm() { return fRealm; }
205 
206 protected:
208  char* fRealm;
210 };
211 
212 #endif
unsigned char Boolean
Definition: Boolean.hh:25
virtual void handleRequestBytes(int newBytesRead)=0
GenericMediaServer(UsageEnvironment &env, int ourSocketIPv4, int ourSocketIPv6, Port ourPort, unsigned reclamationSeconds)
void addServerMediaSession(ServerMediaSession *serverMediaSession)
const Boolean False
Definition: Boolean.hh:28
void removeServerMediaSession(ServerMediaSession *serverMediaSession)
static void noteClientLiveness(ClientSession *clientSession)
virtual void removeUserRecord(char const *username)
unsigned numClientSessions() const
virtual char const * lookupPassword(char const *username)
#define NULL
ServerMediaSession * fOurServerMediaSession
ClientSession * createNewClientSessionWithId()
UsageEnvironment & envir() const
Definition: Media.hh:59
u_int32_t fPreviousClientSessionId
virtual unsigned numEntries() const =0
virtual void lookupServerMediaSession(char const *streamName, lookupServerMediaSessionCompletionFunc *completionFunc, void *completionClientData, Boolean isFirstLookupInSession=True)
virtual ClientSession * createNewClientSession(u_int32_t sessionId)=0
static int setUpOurSocket(UsageEnvironment &env, Port &ourPort, int domain)
virtual void addUserRecord(char const *username, char const *password)
void closeAllClientSessionsForServerMediaSession(ServerMediaSession *serverMediaSession)
ClientConnection(GenericMediaServer &ourServer, int clientSocket, struct sockaddr_storage const &clientAddr)
void * TaskToken
HashTable * fClientConnections
ServerMediaSessionIterator(GenericMediaServer &server)
Definition: Media.hh:50
HashTable * fServerMediaSessions
void incomingConnectionHandlerIPv4()
void incomingConnectionHandlerOnSocket(int serverSocket)
UserAuthenticationDatabase(char const *realm=NULL, Boolean passwordsAreMD5=False)
void deleteServerMediaSession(ServerMediaSession *serverMediaSession)
virtual ~GenericMediaServer()
unsigned char fRequestBuffer[REQUEST_BUFFER_SIZE]
ServerMediaSession * getServerMediaSession(char const *streamName)
void lookupServerMediaSessionCompletionFunc(void *clientData, ServerMediaSession *sessionLookedUp)
static void livenessTimeoutTask(ClientSession *clientSession)
ClientSession(GenericMediaServer &ourServer, u_int32_t sessionId)
#define REQUEST_BUFFER_SIZE
const Boolean True
Definition: Boolean.hh:31
ClientSession * lookupClientSession(u_int32_t sessionId)
virtual ClientConnection * createNewClientConnection(int clientSocket, struct sockaddr_storage const &clientAddr)=0
virtual ~UserAuthenticationDatabase()
#define RESPONSE_BUFFER_SIZE
unsigned char fResponseBuffer[RESPONSE_BUFFER_SIZE]
void incomingConnectionHandlerIPv6()