Jetson Inference
DNN Vision Library
WebRTCServer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __WEBRTC_SERVER_H__
24 #define __WEBRTC_SERVER_H__
25 
26 #include <libsoup/soup.h>
27 
28 #include <stdint.h>
29 #include <string>
30 #include <vector>
31 
32 
33 // forward declarations
34 class WebRTCServer;
35 class Thread;
36 
37 
42 #define WEBRTC_DEFAULT_PORT 41567
43 
49 #define WEBRTC_DEFAULT_STUN_SERVER "stun.l.google.com:19302"
50 
55 #define LOG_WEBRTC "[webrtc] "
56 
57 
63 {
64  // route/stream flags
65  WEBRTC_PRIVATE = 0, // hidden (default)
66  WEBRTC_PUBLIC = (1 << 1), // allow discoverability
67  WEBRTC_AUDIO = (1 << 0), // produces/accepts video
68  WEBRTC_VIDEO = (1 << 1), // produces/accepts audio
69  WEBRTC_SEND = (1 << 2), // server sends to the client (outgoing)
70  WEBRTC_RECEIVE = (1 << 3), // server receives from the client (incoming)
71  WEBRTC_MULTI_CLIENT = (1 << 4), // can connect with multiple clients simultaneously
72 
73  // state flags
74  WEBRTC_PEER_CONNECTING = (1 << 5), // the peer is connecting
75  WEBRTC_PEER_CONNECTED = (1 << 6), // the peer is ready
76  WEBRTC_PEER_STREAMING = (1 << 7), // the peer is streaming
77  WEBRTC_PEER_CLOSED = (1 << 8), // the peer has closed
78 };
79 
80 
85 struct WebRTCPeer
86 {
87  SoupWebsocketConnection* connection;
88  SoupClientContext* client_context;
90 
91  uint32_t ID; // unique ID
92  uint32_t flags; // WebRTCFlags
93 
94  std::string path; // the route the peer is connected on
95  std::string ip_address; // IP address of the peer
96 
97  void* user_data; // private data for the route handler
98 };
99 
100 
117 {
118 public:
123  static WebRTCServer* Create( uint16_t port=WEBRTC_DEFAULT_PORT,
124  const char* stun_server=WEBRTC_DEFAULT_STUN_SERVER,
125  const char* ssl_cert=NULL, const char* ssl_key=NULL,
126  bool threaded=true );
127 
132  void Release();
133 
138  typedef void (*WebsocketListener)( WebRTCPeer* peer, const char* message, size_t message_size, void* user_data );
139 
144  typedef SoupServerCallback HttpListener;
145 
153  void AddRoute( const char* path, HttpListener callback, void* user_data=NULL, uint32_t flags=0 );
154 
170  void AddRoute( const char* path, WebsocketListener callback, void* user_data=NULL, uint32_t flags=0 );
171 
177  inline const char* GetSTUNServer() const { return mStunServer.c_str(); }
178 
183  inline bool HasHTTPS() const { return mHasHTTPS; }
184 
189  inline bool IsThreaded() const { return (mThread != NULL); }
190 
196  bool ProcessRequests( bool blocking=false );
197 
198 protected:
199 
200  WebRTCServer( uint16_t port, const char* stun_server, const char* ssl_cert_file, const char* ssl_key_file, bool threaded );
201  ~WebRTCServer();
202 
203  bool init();
204 
205  static void* runThread( void* user_data );
206 
207  static void onHttpRequest( SoupServer* soup_server, SoupMessage* message, const char* path, GHashTable* query, SoupClientContext* client_context, void* user_data );
208  static void onHttpDefault( SoupServer* soup_server, SoupMessage* message, const char* path, GHashTable* query, SoupClientContext* client_context, void* user_data );
209 
210  static void onWebsocketOpened( SoupServer* server, SoupWebsocketConnection* connection, const char *path, SoupClientContext* client_context, void* user_data );
211  static void onWebsocketMessage( SoupWebsocketConnection* connection, SoupWebsocketDataType data_type, GBytes* message, void* user_data );
212  static void onWebsocketClosed( SoupWebsocketConnection* connection, void* user_data );
213 
214  struct HttpRoute
215  {
216  std::string path;
218  void* user_data;
219  uint32_t flags;
220  };
221 
223  {
224  std::string path;
226  void* user_data;
227  uint32_t flags;
228  std::vector<WebRTCPeer*> peers;
229  };
230 
231  HttpRoute* findHttpRoute( const char* path ) const;
232  WebsocketRoute* findWebsocketRoute( const char* path ) const;
233 
234  void freeRoute( HttpRoute* route );
235  void freeRoute( WebsocketRoute* route );
236 
237  std::string printRouteInfo( WebsocketRoute* route ) const;
238  //std::string printRouteList();
239 
240  std::vector<HttpRoute*> mHttpRoutes;
241  std::vector<WebsocketRoute*> mWebsocketRoutes;
242 
243  SoupServer* mSoupServer;
244  std::string mStunServer;
245 
246  std::string mSSLCertFile;
247  std::string mSSLKeyFile;
248 
249  bool mHasHTTPS;
250 
251  uint16_t mPort;
252  uint32_t mRefCount;
253  uint32_t mPeerCount;
254 
256 };
257 
258 #endif
WebRTCServer::GetSTUNServer
const char * GetSTUNServer() const
Get the STUN server being used.
Definition: WebRTCServer.h:177
WebRTCServer::printRouteInfo
std::string printRouteInfo(WebsocketRoute *route) const
WebRTCPeer::server
WebRTCServer * server
Definition: WebRTCServer.h:89
WebRTCServer::~WebRTCServer
~WebRTCServer()
WebRTCServer::mStunServer
std::string mStunServer
Definition: WebRTCServer.h:244
WebRTCServer::mSSLKeyFile
std::string mSSLKeyFile
Definition: WebRTCServer.h:247
WebRTCServer::mPeerCount
uint32_t mPeerCount
Definition: WebRTCServer.h:253
WebRTCServer::HttpRoute::path
std::string path
Definition: WebRTCServer.h:216
WebRTCServer::AddRoute
void AddRoute(const char *path, HttpListener callback, void *user_data=NULL, uint32_t flags=0)
Register a function to handle incoming http/www requests at the specified path.
WebRTCPeer::user_data
void * user_data
Definition: WebRTCServer.h:97
WebRTCServer::WebsocketListener
void(* WebsocketListener)(WebRTCPeer *peer, const char *message, size_t message_size, void *user_data)
Function pointer to a callback for handling websocket requests.
Definition: WebRTCServer.h:138
WebRTCPeer::ip_address
std::string ip_address
Definition: WebRTCServer.h:95
WebRTCServer::mRefCount
uint32_t mRefCount
Definition: WebRTCServer.h:252
WebRTCServer
WebRTC signalling server for establishing and negotiating connections with peers for bi-directional m...
Definition: WebRTCServer.h:116
WebRTCPeer::client_context
SoupClientContext * client_context
Definition: WebRTCServer.h:88
WebRTCPeer::path
std::string path
Definition: WebRTCServer.h:94
WebRTCServer::onHttpRequest
static void onHttpRequest(SoupServer *soup_server, SoupMessage *message, const char *path, GHashTable *query, SoupClientContext *client_context, void *user_data)
WebRTCServer::onHttpDefault
static void onHttpDefault(SoupServer *soup_server, SoupMessage *message, const char *path, GHashTable *query, SoupClientContext *client_context, void *user_data)
WebRTCPeer::ID
uint32_t ID
Definition: WebRTCServer.h:91
WebRTCServer::HttpRoute::flags
uint32_t flags
Definition: WebRTCServer.h:219
WEBRTC_PUBLIC
@ WEBRTC_PUBLIC
Definition: WebRTCServer.h:66
WebRTCPeer::flags
uint32_t flags
Definition: WebRTCServer.h:92
WEBRTC_PRIVATE
@ WEBRTC_PRIVATE
Definition: WebRTCServer.h:65
WEBRTC_PEER_CLOSED
@ WEBRTC_PEER_CLOSED
Definition: WebRTCServer.h:77
WebRTCServer::findWebsocketRoute
WebsocketRoute * findWebsocketRoute(const char *path) const
WebRTCFlags
WebRTCFlags
Flags for route decorators or peer state.
Definition: WebRTCServer.h:62
WEBRTC_PEER_STREAMING
@ WEBRTC_PEER_STREAMING
Definition: WebRTCServer.h:76
WebRTCServer::mPort
uint16_t mPort
Definition: WebRTCServer.h:251
WebRTCServer::Release
void Release()
Release a reference to the server instance.
WEBRTC_MULTI_CLIENT
@ WEBRTC_MULTI_CLIENT
Definition: WebRTCServer.h:71
WebRTCServer::HttpRoute
Definition: WebRTCServer.h:214
WEBRTC_PEER_CONNECTING
@ WEBRTC_PEER_CONNECTING
Definition: WebRTCServer.h:74
WEBRTC_RECEIVE
@ WEBRTC_RECEIVE
Definition: WebRTCServer.h:70
WebRTCServer::WebsocketRoute::peers
std::vector< WebRTCPeer * > peers
Definition: WebRTCServer.h:228
WEBRTC_SEND
@ WEBRTC_SEND
Definition: WebRTCServer.h:69
WebRTCServer::HttpListener
SoupServerCallback HttpListener
Function pointer to a callback for handling HTTP requests.
Definition: WebRTCServer.h:144
WebRTCPeer::connection
SoupWebsocketConnection * connection
Definition: WebRTCServer.h:87
WebRTCServer::mHasHTTPS
bool mHasHTTPS
Definition: WebRTCServer.h:249
WebRTCServer::IsThreaded
bool IsThreaded() const
Return true if the server is running in it's own thread.
Definition: WebRTCServer.h:189
WebRTCServer::WebsocketRoute
Definition: WebRTCServer.h:222
WebRTCServer::WebsocketRoute::path
std::string path
Definition: WebRTCServer.h:224
WEBRTC_AUDIO
@ WEBRTC_AUDIO
Definition: WebRTCServer.h:67
WebRTCServer::mSoupServer
SoupServer * mSoupServer
Definition: WebRTCServer.h:243
WebRTCServer::HasHTTPS
bool HasHTTPS() const
Return true if the server is using HTTPS.
Definition: WebRTCServer.h:183
WebRTCServer::findHttpRoute
HttpRoute * findHttpRoute(const char *path) const
WebRTCServer::HttpRoute::callback
HttpListener callback
Definition: WebRTCServer.h:217
WebRTCServer::Create
static WebRTCServer * Create(uint16_t port=WEBRTC_DEFAULT_PORT, const char *stun_server=WEBRTC_DEFAULT_STUN_SERVER, const char *ssl_cert=NULL, const char *ssl_key=NULL, bool threaded=true)
Create a WebRTC server on this port.
WebRTCServer::mThread
Thread * mThread
Definition: WebRTCServer.h:255
WebRTCServer::mHttpRoutes
std::vector< HttpRoute * > mHttpRoutes
Definition: WebRTCServer.h:240
WebRTCServer::freeRoute
void freeRoute(HttpRoute *route)
WebRTCServer::ProcessRequests
bool ProcessRequests(bool blocking=false)
Process incoming requests on the server.
WebRTCServer::init
bool init()
WebRTCServer::mSSLCertFile
std::string mSSLCertFile
Definition: WebRTCServer.h:246
WEBRTC_VIDEO
@ WEBRTC_VIDEO
Definition: WebRTCServer.h:68
WEBRTC_PEER_CONNECTED
@ WEBRTC_PEER_CONNECTED
Definition: WebRTCServer.h:75
WebRTCServer::runThread
static void * runThread(void *user_data)
WEBRTC_DEFAULT_PORT
#define WEBRTC_DEFAULT_PORT
Default HTTP/websocket port used by the WebRTC server.
Definition: WebRTCServer.h:42
WebRTCServer::WebsocketRoute::flags
uint32_t flags
Definition: WebRTCServer.h:227
WebRTCServer::onWebsocketClosed
static void onWebsocketClosed(SoupWebsocketConnection *connection, void *user_data)
WebRTCServer::onWebsocketMessage
static void onWebsocketMessage(SoupWebsocketConnection *connection, SoupWebsocketDataType data_type, GBytes *message, void *user_data)
Thread
Thread class for launching an asynchronous operating-system dependent thread.
Definition: Thread.h:44
WebRTCServer::WebsocketRoute::callback
WebsocketListener callback
Definition: WebRTCServer.h:225
WebRTCServer::mWebsocketRoutes
std::vector< WebsocketRoute * > mWebsocketRoutes
Definition: WebRTCServer.h:241
WebRTCPeer
Remote peer that has connected.
Definition: WebRTCServer.h:85
WebRTCServer::WebRTCServer
WebRTCServer(uint16_t port, const char *stun_server, const char *ssl_cert_file, const char *ssl_key_file, bool threaded)
WEBRTC_DEFAULT_STUN_SERVER
#define WEBRTC_DEFAULT_STUN_SERVER
Default STUN server used for WebRTC.
Definition: WebRTCServer.h:49
WebRTCServer::HttpRoute::user_data
void * user_data
Definition: WebRTCServer.h:218
WebRTCServer::WebsocketRoute::user_data
void * user_data
Definition: WebRTCServer.h:226
WebRTCServer::onWebsocketOpened
static void onWebsocketOpened(SoupServer *server, SoupWebsocketConnection *connection, const char *path, SoupClientContext *client_context, void *user_data)