main.c 9.17 KB
Newer Older
1
2
3
4
5
#include "mss-http-server.h"

#include <glib-unix.h>
#include <gst/gst.h>

Jakub Adam's avatar
Jakub Adam committed
6
7
8
#define GST_USE_UNSTABLE_API
#include <gst/webrtc/rtcsessiondescription.h>

9
#define DEFAULT_SRT_URI "srt://127.0.0.1:7001"
10
#define DEFAULT_RIST_ADDRESSES "127.0.0.1:5004"
Jakub Adam's avatar
Jakub Adam committed
11
12

static gchar *srt_uri = NULL;
13
static gchar *rist_addresses = NULL;
Jakub Adam's avatar
Jakub Adam committed
14
15
16

static GOptionEntry options[] = {
    { "srt-uri", 'u', 0, G_OPTION_ARG_STRING, &srt_uri,
Jakub Adam's avatar
Jakub Adam committed
17
      "SRT stream URI. Default: " DEFAULT_SRT_URI, "srt://address:port" },
18
    { "rist-addresses", 'r', 0, G_OPTION_ARG_STRING, &rist_addresses,
Jakub Adam's avatar
Jakub Adam committed
19
      "Comma-separated list of addresses to send RIST packets to. Default: " DEFAULT_RIST_ADDRESSES, "address:port,address:port" },
Jakub Adam's avatar
Jakub Adam committed
20
21
22
    { NULL }
};

Jakub Adam's avatar
Jakub Adam committed
23
24
MssHttpServer *http_server;

25
26
27
28
29
30
31
static gboolean
sigint_handler (gpointer user_data)
{
  g_main_loop_quit (user_data);
  return G_SOURCE_REMOVE;
}

Jakub Adam's avatar
Jakub Adam committed
32
33
34
static gboolean
gst_bus_cb (GstBus * bus, GstMessage * message, gpointer data)
{
Olivier Crête's avatar
Olivier Crête committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  switch (GST_MESSAGE_TYPE(message)) {
  case GST_MESSAGE_ERROR:
    {
      GError *gerr;
      gchar *debug_msg;
      gst_message_parse_error (message, &gerr, &debug_msg);
      g_error ("Error: %s (%s)", gerr->message, debug_msg);
      g_error_free (gerr);
      g_free (debug_msg);
    }
    break;
  case GST_MESSAGE_WARNING:
    {
      GError *gerr;
      gchar *debug_msg;
      gst_message_parse_error (message, &gerr, &debug_msg);
      g_warning ("Error: %s (%s)", gerr->message, debug_msg);
      g_error_free (gerr);
      g_free (debug_msg);
    }
    break;
  case GST_MESSAGE_EOS:
    {
      g_error ("Got EOS!!");
    }
    break;
  default:
    break;
  }
Jakub Adam's avatar
Jakub Adam committed
64
65
66
  return TRUE;
}

Jakub Adam's avatar
Jakub Adam committed
67
68
69
70
71
72
73
74
75
76
77
78
79
static GstElement *
get_webrtcbin_for_client (GstBin *pipeline, MssClientId client_id)
{
  gchar *name;
  GstElement *webrtcbin;

  name = g_strdup_printf ("webrtcbin_%p", client_id);
  webrtcbin = gst_bin_get_by_name (pipeline, name);
  g_free (name);

  return webrtcbin;
}

Olivier Crête's avatar
Olivier Crête committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
static void
connect_webrtc_to_tee (GstElement *webrtcbin)
{
  GstElement *pipeline;
  GstElement *tee;
  GstPad *srcpad;
  GstPad *sinkpad;
  GstPadLinkReturn ret;

  pipeline = GST_ELEMENT (gst_element_get_parent (webrtcbin));
  if (pipeline == NULL)
    return;
  tee = gst_bin_get_by_name (GST_BIN (pipeline), "webrtctee");
  srcpad = gst_element_get_request_pad (tee, "src_%u");
  sinkpad = gst_element_get_request_pad (webrtcbin, "sink_0");
  ret = gst_pad_link (srcpad, sinkpad);
  g_assert (ret == GST_PAD_LINK_OK);
  gst_object_unref (srcpad);
  gst_object_unref (sinkpad);
  gst_object_unref (tee);
  gst_object_unref (pipeline);
}

Jakub Adam's avatar
Jakub Adam committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
static void
on_offer_created (GstPromise *promise, GstElement *webrtcbin)
{
  GstWebRTCSessionDescription *offer = NULL;
  gchar *sdp;

  gst_structure_get (gst_promise_get_reply (promise),
      "offer", GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
  gst_promise_unref (promise);

  g_signal_emit_by_name (webrtcbin, "set-local-description", offer, NULL);

  sdp = gst_sdp_message_as_text (offer->sdp);
  mss_http_server_send_sdp_offer (http_server,
      g_object_get_data (G_OBJECT (webrtcbin), "client_id"),
      sdp);
  g_free (sdp);

  gst_webrtc_session_description_free (offer);
Olivier Crête's avatar
Olivier Crête committed
122
123

  connect_webrtc_to_tee (webrtcbin);
Jakub Adam's avatar
Jakub Adam committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
}

static void
webrtc_on_ice_candidate_cb (GstElement *webrtcbin, guint mlineindex,
  gchar *candidate)
{
  mss_http_server_send_candidate (http_server,
      g_object_get_data (G_OBJECT (webrtcbin), "client_id"),
      mlineindex, candidate);
}

static void
webrtc_client_connected_cb (MssHttpServer *server, MssClientId client_id,
    GstBin *pipeline)
{
  gchar *name;
  GstElement *webrtcbin;
Olivier Crête's avatar
Olivier Crête committed
141
  GstCaps *caps;
142
  GstStateChangeReturn ret;
Jakub Adam's avatar
Jakub Adam committed
143
144
145
146
147
148

  name = g_strdup_printf ("webrtcbin_%p", client_id);

  webrtcbin = gst_element_factory_make ("webrtcbin", name);
  g_object_set_data (G_OBJECT (webrtcbin), "client_id", client_id);
  gst_bin_add (pipeline, webrtcbin);
149
150
  ret = gst_element_set_state (webrtcbin, GST_STATE_PLAYING);
  g_assert (ret != GST_STATE_CHANGE_FAILURE);
Jakub Adam's avatar
Jakub Adam committed
151
152
153
154

  g_signal_connect (webrtcbin, "on-ice-candidate",
      G_CALLBACK (webrtc_on_ice_candidate_cb), NULL);

Olivier Crête's avatar
Olivier Crête committed
155
156
157
158
159
160
  caps = gst_caps_from_string ("application/x-rtp, payload=96,encoding-name=H264,clock-rate=90000,media=video,packetization-mode=(string)1,profile-level-id=(string)42e01f");
  g_signal_emit_by_name (webrtcbin, "add-transceiver",
      GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY,
      caps);

  gst_caps_unref (caps);
Jakub Adam's avatar
Jakub Adam committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178


  g_signal_emit_by_name (webrtcbin, "create-offer", NULL,
      gst_promise_new_with_change_func (
          (GstPromiseChangeFunc) on_offer_created, webrtcbin,NULL));

  GST_DEBUG_BIN_TO_DOT_FILE (pipeline, GST_DEBUG_GRAPH_SHOW_ALL, "rtcbin");

  g_free (name);
}

static void
webrtc_sdp_answer_cb (MssHttpServer *server, MssClientId client_id,
    const gchar *sdp, GstBin *pipeline)
{
  GstSDPMessage *sdp_msg = NULL;
  GstWebRTCSessionDescription *desc = NULL;

Olivier Crête's avatar
Olivier Crête committed
179
  if (gst_sdp_message_new_from_text (sdp, &sdp_msg) != GST_SDP_OK) {
Jakub Adam's avatar
Jakub Adam committed
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    g_debug ("Error parsing SDP description");
    goto out;
  }

  desc = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER,
      sdp_msg);
  if (desc) {
    GstElement *webrtcbin;
    GstPromise *promise;

    webrtcbin = get_webrtcbin_for_client (pipeline, client_id);
    if (!webrtcbin) {
      goto out;
    }
    promise = gst_promise_new();

    g_signal_emit_by_name (webrtcbin, "set-remote-description", desc, promise);

    gst_promise_wait (promise);
    gst_promise_unref (promise);

    gst_object_unref (webrtcbin);
  } else {
    gst_sdp_message_free (sdp_msg);
  }

out:
  g_clear_pointer (&desc, gst_webrtc_session_description_free);
}

static void
webrtc_candidate_cb (MssHttpServer *server, MssClientId client_id,
    guint mlineindex, const gchar *candidate, GstBin *pipeline)
{
  if (strlen (candidate)) {
    GstElement *webrtcbin;

    webrtcbin = get_webrtcbin_for_client (pipeline, client_id);
    if (webrtcbin) {
      g_signal_emit_by_name (webrtcbin, "add-ice-candidate", mlineindex,
          candidate);
      gst_object_unref (webrtcbin);
    }
  }

  g_debug ("Remote candidate: %s", candidate);
}

static void
webrtc_client_disconnected_cb (MssHttpServer *server, MssClientId client_id,
    GstBin *pipeline)
{
  GstElement *webrtcbin;

  webrtcbin = get_webrtcbin_for_client (pipeline, client_id);
  if (webrtcbin) {
    gst_bin_remove (GST_BIN (GST_ELEMENT_PARENT (webrtcbin)), webrtcbin);
    gst_element_set_state (webrtcbin, GST_STATE_NULL);
    gst_object_unref (webrtcbin);
  }
}

242
243
int main (int argc, char *argv[])
{
Jakub Adam's avatar
Jakub Adam committed
244
  GOptionContext *option_context;
245
246
247
248
  GMainLoop *loop;
  gchar *pipeline_str;
  GstElement *pipeline;
  GError *error = NULL;
Jakub Adam's avatar
Jakub Adam committed
249
  GstBus *bus;
250
  GstStateChangeReturn ret;
Jakub Adam's avatar
Jakub Adam committed
251
252
253
254
255
256
257
258
259
260
261
262

  option_context = g_option_context_new (NULL);
  g_option_context_add_main_entries (option_context, options, NULL);

  if (!g_option_context_parse (option_context, &argc, &argv, &error)) {
    g_print ("option parsing failed: %s\n", error->message);
    exit (1);
  }

  if (!srt_uri) {
    srt_uri = g_strdup(DEFAULT_SRT_URI);
  }
263
264
265
  if (!rist_addresses) {
    rist_addresses = g_strdup (DEFAULT_RIST_ADDRESSES);
  }
266
267
268

  http_server = mss_http_server_new ();

Jakub Adam's avatar
Jakub Adam committed
269
  pipeline_str = g_strdup_printf ("srtsrc uri=%s?mode=listener ! tsparse ! tee name=t ! "
Olivier Crête's avatar
Olivier Crête committed
270
      "queue ! tsdemux latency=50 ! decodebin ! videoconvert ! autovideosink "
Olivier Crête's avatar
Olivier Crête committed
271
272
      "t. ! queue leaky=downstream max-size-buffers=400 ! tsdemux ! h264parse ! video/x-h264, stream-format=avc ! h264parse ! mpegtsmux ! hlssink location=%s/segment%%05d.ts playlist-location=%s/playlist.m3u8 target-duration=1 playlist-length=3 "
      "t. ! queue leaky=downstream max-size-buffers=10 ! rtpmp2tpay ! ristsink bonding-addresses=%s "
273
      "t. ! queue leaky=downstream max-size-buffers=10 ! tsdemux ! rtph264pay config-interval=1 ! application/x-rtp,payload=96 ! tee name=webrtctee allow-not-linked=true "
Olivier Crête's avatar
Olivier Crête committed
274
      "t. ! queue leaky=downstream max-size-buffers=10 ! srtsink uri=srt://:7002?mode=listener",
Jakub Adam's avatar
Jakub Adam committed
275
      srt_uri,
276
      mss_http_server_get_hls_dir (http_server),
277
278
      mss_http_server_get_hls_dir (http_server),
      rist_addresses);
279
280
281
282
283
284

  gst_init (&argc, &argv);
  pipeline = gst_parse_launch (pipeline_str, &error);
  g_assert_no_error (error);
  g_free (pipeline_str);

Jakub Adam's avatar
Jakub Adam committed
285
286
287
288
  bus = gst_element_get_bus (pipeline);
  gst_bus_add_watch (bus, gst_bus_cb, NULL);
  gst_object_unref (bus);

Jakub Adam's avatar
Jakub Adam committed
289
290
291
292
293
294
295
  g_signal_connect (http_server, "ws-client-disconnected",
      G_CALLBACK (webrtc_client_disconnected_cb), pipeline);
  g_signal_connect (http_server, "sdp-answer",
      G_CALLBACK (webrtc_sdp_answer_cb), pipeline);
  g_signal_connect (http_server, "candidate",
      G_CALLBACK (webrtc_candidate_cb), pipeline);

296
297
298
  loop = g_main_loop_new (NULL, FALSE);
  g_unix_signal_add (SIGINT, sigint_handler, loop);

299
  g_print ("Input SRT URI is %s\n"
300
      "\nOutput streams:\n"
301
      "\tHLS & WebRTC web player: http://localhost:8080\n"
Jakub Adam's avatar
Jakub Adam committed
302
      "\tRIST: %s\n"
Olivier Crête's avatar
Olivier Crête committed
303
      "\tSRT: srt://127.0.0.1:7002\n",
304
305
      srt_uri,
      rist_addresses);
306

307
308
  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  g_assert (ret != GST_STATE_CHANGE_FAILURE);
309
310
311
312

  g_signal_connect (http_server, "ws-client-connected",
      G_CALLBACK (webrtc_client_connected_cb), pipeline);

313
314
315
  g_main_loop_run (loop);
  g_main_loop_unref (loop);

316
317
  gst_element_set_state (pipeline, GST_STATE_NULL);

318
319
  gst_clear_object (&pipeline);
  g_clear_object (&http_server);
320
321
  g_clear_pointer (&srt_uri, g_free);
  g_clear_pointer (&rist_addresses, g_free);
322
}