mss-http-server.c 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * This file is a part of MultiStreamServer
 *
 * (C) 2019, Collabora Ltd
 */

#include "mss-http-server.h"
#include "gresource-multistream-server.h"

#include <glib/gstdio.h>

Jakub Adam's avatar
Jakub Adam committed
12
13
#include <json-glib/json-glib.h>

14
15
16
17
18
19
20
21
#include <libsoup/soup-message.h>
#include <libsoup/soup-server.h>

struct _MssHttpServer
{
  GObject parent;

  SoupServer *soup_server;
Jakub Adam's avatar
Jakub Adam committed
22

23
  gchar *hls_dir;
Jakub Adam's avatar
Jakub Adam committed
24
25

  GSList *websocket_connections;
26
27
28
29
};

G_DEFINE_TYPE (MssHttpServer, mss_http_server, G_TYPE_OBJECT)

Jakub Adam's avatar
Jakub Adam committed
30
31
32
33
34
35
36
37
38
39
40
enum
{
  SIGNAL_WS_CLIENT_CONNECTED,
  SIGNAL_WS_CLIENT_DISCONNECTED,
  SIGNAL_SDP_ANSWER,
  SIGNAL_CANDIDATE,
  N_SIGNALS
};

guint signals[N_SIGNALS];

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
MssHttpServer *
mss_http_server_new ()
{
  return MSS_HTTP_SERVER (g_object_new (MSS_TYPE_HTTP_SERVER, NULL));
}

static void
http_cb (SoupServer *server, SoupMessage *msg, const char *path,
  GHashTable *query, SoupClientContext *client, gpointer user_data)
{
  GResource *res = multistream_server_get_resource();
  GBytes *bytes;

  if (g_str_equal (path, "/")) {
    path = "/index.html";
  }

  bytes = g_resource_lookup_data(res, path, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
  if (bytes) {
    SoupBuffer *buffer;
    gconstpointer data;
    gsize size;

    data = g_bytes_get_data (bytes, &size);

    buffer = soup_buffer_new_with_owner(data, size, bytes,
        (GDestroyNotify) g_bytes_unref);

    soup_message_body_append_buffer (msg->response_body, buffer);
    soup_buffer_free (buffer);

Jakub Adam's avatar
Jakub Adam committed
72
73
74
    if (g_str_has_suffix(path, ".js")) {
      soup_message_headers_append(msg->response_headers,
          "Content-Type", "text/javascript");
75
76
77
78
79
80
    } else if (g_str_has_suffix(path, ".css")) {
      soup_message_headers_append(msg->response_headers,
          "Content-Type", "text/css");
    } else if (g_str_has_suffix(path, ".svg")) {
      soup_message_headers_append(msg->response_headers,
          "Content-Type", "image/svg+xml");
Jakub Adam's avatar
Jakub Adam committed
81
82
    }

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    soup_message_set_status (msg, SOUP_STATUS_OK);
  } else {
    soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
  }
}

static void
hls_cb (SoupServer *server, SoupMessage *msg, const char *path,
  GHashTable *query, SoupClientContext *client, gpointer user_data)
{
  gchar *file_name;
  gchar *file_path;
  gchar *buf;
  gsize buf_len;
  GError *error = NULL;
  MssHttpServer *self = MSS_HTTP_SERVER(user_data);

  file_name = g_path_get_basename (path);
  file_path = g_build_filename (self->hls_dir, file_name, NULL);

  if (g_file_get_contents (file_path, &buf, &buf_len, &error)) {
    SoupBuffer *buffer;

    buffer = soup_buffer_new_take ((guchar *) buf, buf_len);
    soup_message_body_append_buffer (msg->response_body, buffer);
    soup_buffer_free (buffer);

    if (g_str_has_suffix(path, ".m3u8")) {
      soup_message_headers_append(msg->response_headers,
          "Content-Type", "application/vnd.apple.mpegurl");
    }

    soup_message_set_status (msg, SOUP_STATUS_OK);
  } else {
    g_warning (error->message);
    g_clear_error (&error);
    soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
  }
}

Jakub Adam's avatar
Jakub Adam committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
static void
mss_http_server_handle_message (MssHttpServer *server,
  SoupWebsocketConnection *connection, GBytes *message)
{
  gsize length = 0;
  const gchar *msg_data = g_bytes_get_data (message, &length);
  JsonParser *parser = json_parser_new ();
  GError *error = NULL;

  if (json_parser_load_from_data (parser, msg_data, length, &error)) {
    JsonObject *msg = json_node_get_object (json_parser_get_root (parser));
    const gchar *msg_type;

    if (!json_object_has_member (msg, "msg")) {
      // Invalid message
      goto out;
    }

    msg_type = json_object_get_string_member (msg, "msg");
    if (g_str_equal(msg_type, "answer")) {
Olivier Crête's avatar
Olivier Crête committed
143
144
145
      const gchar *answer_sdp = json_object_get_string_member (msg, "sdp");
      g_debug ("Received answer:\n %s", answer_sdp);

Jakub Adam's avatar
Jakub Adam committed
146
      g_signal_emit (server, signals[SIGNAL_SDP_ANSWER], 0, connection,
Olivier Crête's avatar
Olivier Crête committed
147
          answer_sdp);
Jakub Adam's avatar
Jakub Adam committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    } else if (g_str_equal(msg_type, "candidate")) {
      JsonObject *candidate;

      candidate = json_object_get_object_member (msg, "candidate");

      g_signal_emit (server, signals[SIGNAL_CANDIDATE], 0, connection,
          json_object_get_int_member (candidate, "sdpMLineIndex"),
          json_object_get_string_member (candidate, "candidate"));
    }
  } else {
    g_debug ("Error parsing message: %s", error->message);
    g_clear_error (&error);
  }

out:
  g_object_unref (parser);
}

static void
message_cb (SoupWebsocketConnection *connection, gint type, GBytes *message,
  gpointer user_data)
{
  mss_http_server_handle_message (MSS_HTTP_SERVER (user_data),
      connection, message);
}

static void
mss_http_server_remove_websocket_connection (MssHttpServer *server,
  SoupWebsocketConnection *connection)
{
  MssClientId client_id;

  client_id = g_object_get_data (G_OBJECT (connection), "client_id");

  server->websocket_connections = g_slist_remove (server->websocket_connections,
      client_id);

  g_signal_emit (server, signals [SIGNAL_WS_CLIENT_DISCONNECTED], 0, client_id);
}

static void
closed_cb (SoupWebsocketConnection *connection, gpointer user_data)
{
  g_debug ("Connection closed");

  mss_http_server_remove_websocket_connection (MSS_HTTP_SERVER (user_data),
      connection);
}

static void
mss_http_server_add_websocket_connection (MssHttpServer *server,
  SoupWebsocketConnection *connection)
{
  g_signal_connect (connection, "message", (GCallback) message_cb, server);
  g_signal_connect (connection, "closed", (GCallback) closed_cb, server);

  g_object_ref (connection);

  g_object_set_data (G_OBJECT (connection),
      "client_id", connection);

  server->websocket_connections =
      g_slist_append (server->websocket_connections, connection);

  g_signal_emit (server, signals [SIGNAL_WS_CLIENT_CONNECTED], 0, connection);
}

static void
websocket_cb (SoupServer *server, SoupWebsocketConnection *connection,
  const char *path, SoupClientContext *client, gpointer user_data)
{
  g_debug ("New connection from %s", soup_client_context_get_host (client));

  mss_http_server_add_websocket_connection (MSS_HTTP_SERVER (user_data),
      connection);
}

225
226
227
228
229
230
231
232
233
234
235
static void
mss_http_server_init (MssHttpServer *server)
{
  GError *error = NULL;

  server->soup_server = soup_server_new (NULL, NULL);
  server->hls_dir = g_dir_make_tmp ("mss-hls-XXXXXX", &error);
  g_assert_no_error (error);

  soup_server_add_handler (server->soup_server, NULL, http_cb, server, NULL);
  soup_server_add_handler (server->soup_server, "/hls/", hls_cb, server, NULL);
Jakub Adam's avatar
Jakub Adam committed
236
237
238
  soup_server_add_websocket_handler (server->soup_server, "/ws", NULL, NULL,
      websocket_cb, server, NULL);

239
240
241
242
243
244
245
246
247
248
  soup_server_listen_all (server->soup_server, 8080, 0, &error);
  g_assert_no_error (error);
}

const gchar *
mss_http_server_get_hls_dir (MssHttpServer *server)
{
  return server->hls_dir;
}

Jakub Adam's avatar
Jakub Adam committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
static void
mss_http_server_send_to_websocket_client (MssHttpServer *server,
  MssClientId client_id, JsonNode *msg)
{
  SoupWebsocketConnection *connection = client_id;
  SoupWebsocketState socket_state;

  if (!g_slist_find (server->websocket_connections, connection)) {
    g_warning ("Unknown websocket connection.");
    return;
  }

  socket_state = soup_websocket_connection_get_state (connection);

  if (socket_state == SOUP_WEBSOCKET_STATE_OPEN) {
    gchar *msg_str = json_to_string (msg, TRUE);

    soup_websocket_connection_send_text (connection, msg_str);

    g_free (msg_str);
  } else {
    g_warning ("Trying to send message using websocket that isn't open.");
  }
}

void
mss_http_server_send_sdp_offer (MssHttpServer *server, MssClientId client_id,
  const gchar *sdp)
{
  JsonBuilder *builder;
  JsonNode *root;

  g_debug ("Send offer: %s", sdp);

  builder = json_builder_new ();
  json_builder_begin_object (builder);
  json_builder_set_member_name (builder, "msg");
  json_builder_add_string_value(builder, "offer");

  json_builder_set_member_name (builder, "sdp");
  json_builder_add_string_value(builder, sdp);
  json_builder_end_object (builder);

  root = json_builder_get_root (builder);

  mss_http_server_send_to_websocket_client (server, client_id, root);

  json_node_unref (root);
  g_object_unref (builder);
}

void
mss_http_server_send_candidate (MssHttpServer *server, MssClientId client_id,
  guint mlineindex, const gchar *candidate)
{
  JsonBuilder *builder;
  JsonNode *root;

  g_debug ("Send candidate: %u %s", mlineindex, candidate);

  builder = json_builder_new ();
  json_builder_begin_object (builder);
  json_builder_set_member_name (builder, "msg");
  json_builder_add_string_value(builder, "candidate");

  json_builder_set_member_name (builder, "candidate");
  json_builder_begin_object(builder);
  json_builder_set_member_name (builder, "candidate");
  json_builder_add_string_value(builder, candidate);
  json_builder_set_member_name (builder, "sdpMLineIndex");
  json_builder_add_int_value(builder, mlineindex);
  json_builder_end_object(builder);
  json_builder_end_object(builder);

  root = json_builder_get_root (builder);

  mss_http_server_send_to_websocket_client (server, client_id, root);

  json_node_unref (root);
  g_object_unref (builder);
}

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
static void
mss_http_server_dispose (GObject *object)
{
  MssHttpServer *self = MSS_HTTP_SERVER (object);
  GDir *dir;

  soup_server_disconnect (self->soup_server);
  g_clear_object (&self->soup_server);


  dir = g_dir_open (self->hls_dir, 0, NULL);
  if (dir) {
    const gchar *filename;

    while ((filename = g_dir_read_name (dir))) {
      gchar *path = g_build_filename (self->hls_dir, filename, NULL);
      g_unlink (path);
      g_free (path);
    }
    g_dir_close (dir);
    g_rmdir (self->hls_dir);
  }

  g_clear_pointer (&self->hls_dir, g_free);
}

static void
mss_http_server_class_init (MssHttpServerClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->dispose = mss_http_server_dispose;
Jakub Adam's avatar
Jakub Adam committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

  signals[SIGNAL_WS_CLIENT_CONNECTED] =
      g_signal_new ("ws-client-connected", G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 1, G_TYPE_POINTER);

  signals[SIGNAL_WS_CLIENT_DISCONNECTED] =
      g_signal_new ("ws-client-disconnected", G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 1, G_TYPE_POINTER);

  signals[SIGNAL_SDP_ANSWER] =
      g_signal_new ("sdp-answer", G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_STRING);

  signals[SIGNAL_CANDIDATE] =
      g_signal_new ("candidate", G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_STRING);
387
}