main.c 8.08 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
35
36
37
static gboolean
gst_bus_cb (GstBus * bus, GstMessage * message, gpointer data)
{
  return TRUE;
}

Jakub Adam's avatar
Jakub Adam committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
}

static void
on_offer_created (GstPromise *promise, GstElement *webrtcbin)
{
  GstWebRTCSessionDescription *offer = NULL;
  gchar *sdp;
56
  GstSDPMedia *media;
Jakub Adam's avatar
Jakub Adam committed
57
58
59
60
61

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

62
63
64
65
66
  media = (GstSDPMedia *) gst_sdp_message_get_media (offer->sdp, 0);

  gst_sdp_media_add_attribute (media, "fmtp",
      "96 packetization-mode=1;profile-level-id=42e01f");

Jakub Adam's avatar
Jakub Adam committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
  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);
}

static void
webrtc_on_ice_candidate_cb (GstElement *webrtcbin, guint mlineindex,
  gchar *candidate)
{
  g_debug ("Local candidate: %s", 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 *tee;
  GstElement *webrtcbin;
  GstPad *srcpad;
  GstPad *sinkpad;

  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);
  gst_element_sync_state_with_parent (webrtcbin);

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

  tee = gst_bin_get_by_name (pipeline, "webrtctee");
  srcpad = gst_element_get_request_pad (tee, "src_%u");
  sinkpad = gst_element_get_request_pad (webrtcbin, "sink_%u");
  gst_pad_link (srcpad, sinkpad);
  gst_object_unref (srcpad);
  gst_object_unref (sinkpad);
  gst_object_unref (tee);


  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;
  gchar *sdp_tmp;
  char **split;

  // TODO needed?
  split = g_strsplit(sdp, "a=recvonly", -1);
  sdp_tmp = g_strjoinv("a=sendrecv", split);
  g_strfreev(split);

  g_debug ("ANSWER: %s", sdp_tmp);

  if (gst_sdp_message_new_from_text (sdp_tmp, &sdp_msg) != GST_SDP_OK) {
    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_free (sdp_tmp);
  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);
  }
}

207
208
int main (int argc, char *argv[])
{
Jakub Adam's avatar
Jakub Adam committed
209
  GOptionContext *option_context;
210
211
212
213
  GMainLoop *loop;
  gchar *pipeline_str;
  GstElement *pipeline;
  GError *error = NULL;
Jakub Adam's avatar
Jakub Adam committed
214
215
216
217
218
219
220
221
222
223
224
225
226
  GstBus *bus;

  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);
  }
227
228
229
  if (!rist_addresses) {
    rist_addresses = g_strdup (DEFAULT_RIST_ADDRESSES);
  }
230
231
232

  http_server = mss_http_server_new ();

Jakub Adam's avatar
Jakub Adam committed
233
  pipeline_str = g_strdup_printf ("srtsrc uri=%s?mode=listener ! tsparse ! tee name=t ! "
234
      "queue ! decodebin ! videoconvert ! autovideosink "
235
      "t. ! queue leaky=downstream ! 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 "
236
      "t. ! queue leaky=downstream max-size-buffers=400 ! rtpmp2tpay ! ristsink bonding-addresses=%s "
Jakub Adam's avatar
Jakub Adam committed
237
238
      "t. ! queue leaky=downstream ! tsdemux ! h264parse ! rtph264pay ! application/x-rtp,payload=96 ! tee name=webrtctee allow-not-linked=true "
      "t. ! queue leaky=downstream max-size-buffers=400 ! srtsink uri=srt://:7002?mode=listener",
Jakub Adam's avatar
Jakub Adam committed
239
      srt_uri,
240
      mss_http_server_get_hls_dir (http_server),
241
242
      mss_http_server_get_hls_dir (http_server),
      rist_addresses);
243
244
245
246
247
248
249

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

Jakub Adam's avatar
Jakub Adam committed
250
251
252
253
  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
254
255
256
257
258
259
260
261
262
  g_signal_connect (http_server, "ws-client-connected",
      G_CALLBACK (webrtc_client_connected_cb), pipeline);
  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);

263
264
265
  loop = g_main_loop_new (NULL, FALSE);
  g_unix_signal_add (SIGINT, sigint_handler, loop);

266
  g_print ("Input SRT URI is %s\n"
267
      "\nOutput streams:\n"
268
      "\tHLS & WebRTC web player: http://localhost:8080\n"
Jakub Adam's avatar
Jakub Adam committed
269
270
      "\tRIST: %s\n"
      "\tSRT: srt://127.0.0.1:7002",
271
272
      srt_uri,
      rist_addresses);
273
274
275
276

  g_main_loop_run (loop);
  g_main_loop_unref (loop);

277
278
  gst_element_set_state (pipeline, GST_STATE_NULL);

279
280
  gst_clear_object (&pipeline);
  g_clear_object (&http_server);
281
282
  g_clear_pointer (&srt_uri, g_free);
  g_clear_pointer (&rist_addresses, g_free);
283
}