docs/html/WebRTCServer_8h_source.html
| | 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
62 enum WebRTCFlags
63 {
64// route/stream flags
65WEBRTC_PRIVATE = 0, // hidden (default)
66WEBRTC_PUBLIC = (1 << 1), // allow discoverability
67WEBRTC_AUDIO = (1 << 0), // produces/accepts video
68WEBRTC_VIDEO = (1 << 1), // produces/accepts audio
69WEBRTC_SEND = (1 << 2), // server sends to the client (outgoing)
70WEBRTC_RECEIVE = (1 << 3), // server receives from the client (incoming)
71WEBRTC_MULTI_CLIENT = (1 << 4), // can connect with multiple clients simultaneously
72
73// state flags
74WEBRTC_PEER_CONNECTING = (1 << 5), // the peer is connecting
75WEBRTC_PEER_CONNECTED = (1 << 6), // the peer is ready
76WEBRTC_PEER_STREAMING = (1 << 7), // the peer is streaming
77WEBRTC_PEER_CLOSED = (1 << 8), // the peer has closed
78 };
79
80
85 struct WebRTCPeer
86 {
87 SoupWebsocketConnection* connection;
88 SoupClientContext* client_context;
90
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
97void* user_data; // private data for the route handler
98 };
99
100
116 class WebRTCServer
117 {
118 public:
123static WebRTCServer* Create( uint16_t port=WEBRTC_DEFAULT_PORT,
124const char* stun_server=WEBRTC_DEFAULT_STUN_SERVER,
125const char* ssl_cert=NULL, const char* ssl_key=NULL,
126bool threaded=true );
127
132void Release();
133
138typedef void (*WebsocketListener)( WebRTCPeer* peer, const char* message, size_t message_size, void* user_data );
139
144typedef SoupServerCallback HttpListener;
145
153void AddRoute( const char* path, HttpListener callback, void* user_data=NULL, uint32_t flags=0 );
154
170void AddRoute( const char* path, WebsocketListener callback, void* user_data=NULL, uint32_t flags=0 );
171
177inline const char* GetSTUNServer() const { return mStunServer.c_str(); }
178
183inline bool HasHTTPS() const { return mHasHTTPS; }
184
189inline bool IsThreaded() const { return (mThread != NULL); }
190
196bool ProcessRequests( bool blocking=false );
197
198 protected:
199
200WebRTCServer( uint16_t port, const char* stun_server, const char* ssl_cert_file, const char* ssl_key_file, bool threaded );
201~WebRTCServer();
202
203bool init();
204
205static void* runThread( void* user_data );
206
207static void onHttpRequest( SoupServer* soup_server, SoupMessage* message, const char* path, GHashTable* query, SoupClientContext* client_context, void* user_data );
208static void onHttpDefault( SoupServer* soup_server, SoupMessage* message, const char* path, GHashTable* query, SoupClientContext* client_context, void* user_data );
209
210static void onWebsocketOpened( SoupServer* server, SoupWebsocketConnection* connection, const char *path, SoupClientContext* client_context, void* user_data );
211static void onWebsocketMessage( SoupWebsocketConnection* connection, SoupWebsocketDataType data_type, GBytes* message, void* user_data );
212static void onWebsocketClosed( SoupWebsocketConnection* connection, void* user_data );
213
215 {
220 };
221
222struct WebsocketRoute
223 {
225WebsocketListener callback;
228 std::vector<WebRTCPeer*> peers;
229 };
230
231HttpRoute* findHttpRoute( const char* path ) const;
232WebsocketRoute* findWebsocketRoute( const char* path ) const;
233
234void freeRoute( HttpRoute* route );
235void 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
250
253 uint32_t mPeerCount;
254
256 };
257
258 #endif
const char * GetSTUNServer() const
Get the STUN server being used.
Definition: WebRTCServer.h:177
std::string printRouteInfo(WebsocketRoute *route) const
WebRTCServer * server
Definition: WebRTCServer.h:89
~WebRTCServer()
std::string mStunServer
Definition: WebRTCServer.h:244
std::string mSSLKeyFile
Definition: WebRTCServer.h:247
uint32_t mPeerCount
Definition: WebRTCServer.h:253
std::string path
Definition: WebRTCServer.h:216
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.
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
std::string ip_address
Definition: WebRTCServer.h:95
uint32_t mRefCount
Definition: WebRTCServer.h:252
WebRTC signalling server for establishing and negotiating connections with peers for bi-directional m...
Definition: WebRTCServer.h:116
SoupClientContext * client_context
Definition: WebRTCServer.h:88
std::string path
Definition: WebRTCServer.h:94
static void onHttpRequest(SoupServer *soup_server, SoupMessage *message, const char *path, GHashTable *query, SoupClientContext *client_context, void *user_data)
static void onHttpDefault(SoupServer *soup_server, SoupMessage *message, const char *path, GHashTable *query, SoupClientContext *client_context, void *user_data)
uint32_t ID
Definition: WebRTCServer.h:91
WebRTCServer::HttpRoute::flags
uint32_t flags
Definition: WebRTCServer.h:219
@ WEBRTC_PUBLIC
Definition: WebRTCServer.h:66
uint32_t flags
Definition: WebRTCServer.h:92
@ WEBRTC_PRIVATE
Definition: WebRTCServer.h:65
@ WEBRTC_PEER_CLOSED
Definition: WebRTCServer.h:77
WebRTCServer::findWebsocketRoute
WebsocketRoute * findWebsocketRoute(const char *path) const
WebRTCFlags
Flags for route decorators or peer state.
Definition: WebRTCServer.h:62
@ WEBRTC_PEER_STREAMING
Definition: WebRTCServer.h:76
uint16_t mPort
Definition: WebRTCServer.h:251
void Release()
Release a reference to the server instance.
@ WEBRTC_MULTI_CLIENT
Definition: WebRTCServer.h:71
Definition: WebRTCServer.h:214
@ WEBRTC_PEER_CONNECTING
Definition: WebRTCServer.h:74
@ WEBRTC_RECEIVE
Definition: WebRTCServer.h:70
WebRTCServer::WebsocketRoute::peers
std::vector< WebRTCPeer * > peers
Definition: WebRTCServer.h:228
@ WEBRTC_SEND
Definition: WebRTCServer.h:69
SoupServerCallback HttpListener
Function pointer to a callback for handling HTTP requests.
Definition: WebRTCServer.h:144
SoupWebsocketConnection * connection
Definition: WebRTCServer.h:87
bool mHasHTTPS
Definition: WebRTCServer.h:249
bool IsThreaded() const
Return true if the server is running in it's own thread.
Definition: WebRTCServer.h:189
Definition: WebRTCServer.h:222
WebRTCServer::WebsocketRoute::path
std::string path
Definition: WebRTCServer.h:224
@ WEBRTC_AUDIO
Definition: WebRTCServer.h:67
SoupServer * mSoupServer
Definition: WebRTCServer.h:243
bool HasHTTPS() const
Return true if the server is using HTTPS.
Definition: WebRTCServer.h:183
HttpRoute * findHttpRoute(const char *path) const
WebRTCServer::HttpRoute::callback
HttpListener callback
Definition: WebRTCServer.h:217
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.
Thread * mThread
Definition: WebRTCServer.h:255
std::vector< HttpRoute * > mHttpRoutes
Definition: WebRTCServer.h:240
void freeRoute(HttpRoute *route)
bool ProcessRequests(bool blocking=false)
Process incoming requests on the server.
bool init()
std::string mSSLCertFile
Definition: WebRTCServer.h:246
@ WEBRTC_VIDEO
Definition: WebRTCServer.h:68
@ WEBRTC_PEER_CONNECTED
Definition: WebRTCServer.h:75
static void * runThread(void *user_data)
#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 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
Remote peer that has connected.
Definition: WebRTCServer.h:85
WebRTCServer(uint16_t port, const char *stun_server, const char *ssl_cert_file, const char *ssl_key_file, bool threaded)
#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)