GenericMediaServer.hh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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-2020 Live Networks, Inc. All rights reserved.
  16. // A generic media server class, used to implement a RTSP server, and any other server that uses
  17. // "ServerMediaSession" objects to describe media to be served.
  18. // C++ header
  19. #ifndef _GENERIC_MEDIA_SERVER_HH
  20. #define _GENERIC_MEDIA_SERVER_HH
  21. #ifndef _MEDIA_HH
  22. #include "Media.hh"
  23. #endif
  24. #ifndef _SERVER_MEDIA_SESSION_HH
  25. #include "ServerMediaSession.hh"
  26. #endif
  27. #ifndef REQUEST_BUFFER_SIZE
  28. #define REQUEST_BUFFER_SIZE 20000 // for incoming requests
  29. #endif
  30. #ifndef RESPONSE_BUFFER_SIZE
  31. #define RESPONSE_BUFFER_SIZE 20000
  32. #endif
  33. class GenericMediaServer: public Medium {
  34. public:
  35. void addServerMediaSession(ServerMediaSession* serverMediaSession);
  36. virtual ServerMediaSession*
  37. lookupServerMediaSession(char const* streamName, Boolean isFirstLookupInSession = True);
  38. void removeServerMediaSession(ServerMediaSession* serverMediaSession);
  39. // Removes the "ServerMediaSession" object from our lookup table, so it will no longer be accessible by new clients.
  40. // (However, any *existing* client sessions that use this "ServerMediaSession" object will continue streaming.
  41. // The "ServerMediaSession" object will not get deleted until all of these client sessions have closed.)
  42. // (To both delete the "ServerMediaSession" object *and* close all client sessions that use it,
  43. // call "deleteServerMediaSession(serverMediaSession)" instead.)
  44. virtual void removeServerMediaSession(char const* streamName);
  45. // ditto
  46. void closeAllClientSessionsForServerMediaSession(ServerMediaSession* serverMediaSession);
  47. // Closes (from the server) all client sessions that are currently using this "ServerMediaSession" object.
  48. // Note, however, that the "ServerMediaSession" object remains accessible by new clients.
  49. virtual void closeAllClientSessionsForServerMediaSession(char const* streamName);
  50. // ditto
  51. void deleteServerMediaSession(ServerMediaSession* serverMediaSession);
  52. // Equivalent to:
  53. // "closeAllClientSessionsForServerMediaSession(serverMediaSession); removeServerMediaSession(serverMediaSession);"
  54. virtual void deleteServerMediaSession(char const* streamName);
  55. // Equivalent to:
  56. // "closeAllClientSessionsForServerMediaSession(streamName); removeServerMediaSession(streamName);
  57. unsigned numClientSessions() const { return fClientSessions->numEntries(); }
  58. protected:
  59. GenericMediaServer(UsageEnvironment& env, int ourSocket, Port ourPort,
  60. unsigned reclamationSeconds);
  61. // If "reclamationSeconds" > 0, then the "ClientSession" state for each client will get
  62. // reclaimed if no activity from the client is detected in at least "reclamationSeconds".
  63. // we're an abstract base class
  64. virtual ~GenericMediaServer();
  65. void cleanup(); // MUST be called in the destructor of any subclass of us
  66. static int setUpOurSocket(UsageEnvironment& env, Port& ourPort);
  67. static void incomingConnectionHandler(void*, int /*mask*/);
  68. void incomingConnectionHandler();
  69. void incomingConnectionHandlerOnSocket(int serverSocket);
  70. public: // should be protected, but some old compilers complain otherwise
  71. // The state of a TCP connection used by a client:
  72. class ClientConnection {
  73. protected:
  74. ClientConnection(GenericMediaServer& ourServer, int clientSocket, struct sockaddr_in clientAddr);
  75. virtual ~ClientConnection();
  76. UsageEnvironment& envir() { return fOurServer.envir(); }
  77. void closeSockets();
  78. static void incomingRequestHandler(void*, int /*mask*/);
  79. void incomingRequestHandler();
  80. virtual void handleRequestBytes(int newBytesRead) = 0;
  81. void resetRequestBuffer();
  82. protected:
  83. friend class GenericMediaServer;
  84. friend class ClientSession;
  85. friend class RTSPServer; // needed to make some broken Windows compilers work; remove this in the future when we end support for Windows
  86. GenericMediaServer& fOurServer;
  87. int fOurSocket;
  88. struct sockaddr_in fClientAddr;
  89. unsigned char fRequestBuffer[REQUEST_BUFFER_SIZE];
  90. unsigned char fResponseBuffer[RESPONSE_BUFFER_SIZE];
  91. unsigned fRequestBytesAlreadySeen, fRequestBufferBytesLeft;
  92. };
  93. // The state of an individual client session (using one or more sequential TCP connections) handled by a server:
  94. class ClientSession {
  95. protected:
  96. ClientSession(GenericMediaServer& ourServer, u_int32_t sessionId);
  97. virtual ~ClientSession();
  98. UsageEnvironment& envir() { return fOurServer.envir(); }
  99. void noteLiveness();
  100. static void noteClientLiveness(ClientSession* clientSession);
  101. static void livenessTimeoutTask(ClientSession* clientSession);
  102. protected:
  103. friend class GenericMediaServer;
  104. friend class ClientConnection;
  105. GenericMediaServer& fOurServer;
  106. u_int32_t fOurSessionId;
  107. ServerMediaSession* fOurServerMediaSession;
  108. TaskToken fLivenessCheckTask;
  109. };
  110. protected:
  111. virtual ClientConnection* createNewClientConnection(int clientSocket, struct sockaddr_in clientAddr) = 0;
  112. virtual ClientSession* createNewClientSession(u_int32_t sessionId) = 0;
  113. ClientSession* createNewClientSessionWithId();
  114. // Generates a new (unused) random session id, and calls the "createNewClientSession()"
  115. // virtual function with this session id as parameter.
  116. // Lookup a "ClientSession" object by sessionId (integer, and string):
  117. ClientSession* lookupClientSession(u_int32_t sessionId);
  118. ClientSession* lookupClientSession(char const* sessionIdStr);
  119. // An iterator over our "ServerMediaSession" objects:
  120. class ServerMediaSessionIterator {
  121. public:
  122. ServerMediaSessionIterator(GenericMediaServer& server);
  123. virtual ~ServerMediaSessionIterator();
  124. ServerMediaSession* next();
  125. private:
  126. HashTable::Iterator* fOurIterator;
  127. };
  128. protected:
  129. friend class ClientConnection;
  130. friend class ClientSession;
  131. friend class ServerMediaSessionIterator;
  132. int fServerSocket;
  133. Port fServerPort;
  134. unsigned fReclamationSeconds;
  135. private:
  136. HashTable* fServerMediaSessions; // maps 'stream name' strings to "ServerMediaSession" objects
  137. HashTable* fClientConnections; // the "ClientConnection" objects that we're using
  138. HashTable* fClientSessions; // maps 'session id' strings to "ClientSession" objects
  139. u_int32_t fPreviousClientSessionId;
  140. };
  141. // A data structure used for optional user/password authentication:
  142. class UserAuthenticationDatabase {
  143. public:
  144. UserAuthenticationDatabase(char const* realm = NULL,
  145. Boolean passwordsAreMD5 = False);
  146. // If "passwordsAreMD5" is True, then each password stored into, or removed from,
  147. // the database is actually the value computed
  148. // by md5(<username>:<realm>:<actual-password>)
  149. virtual ~UserAuthenticationDatabase();
  150. virtual void addUserRecord(char const* username, char const* password);
  151. virtual void removeUserRecord(char const* username);
  152. virtual char const* lookupPassword(char const* username);
  153. // returns NULL if the user name was not present
  154. char const* realm() { return fRealm; }
  155. Boolean passwordsAreMD5() { return fPasswordsAreMD5; }
  156. protected:
  157. HashTable* fTable;
  158. char* fRealm;
  159. Boolean fPasswordsAreMD5;
  160. };
  161. #endif