main.c 8.32 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
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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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
93 94

  connect_webrtc_to_tee (webrtcbin);
Jakub Adam's avatar
Jakub Adam committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

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
112
  GstCaps *caps;
Jakub Adam's avatar
Jakub Adam committed
113 114 115 116 117 118 119 120 121 122 123

  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);

Olivier Crête's avatar
Olivier Crête committed
124 125 126 127 128 129
  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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147


  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
148
  if (gst_sdp_message_new_from_text (sdp, &sdp_msg) != GST_SDP_OK) {
Jakub Adam's avatar
Jakub Adam committed
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
    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);
  }
}

211 212
int main (int argc, char *argv[])
{
Jakub Adam's avatar
Jakub Adam committed
213
  GOptionContext *option_context;
214 215 216 217
  GMainLoop *loop;
  gchar *pipeline_str;
  GstElement *pipeline;
  GError *error = NULL;
Jakub Adam's avatar
Jakub Adam committed
218 219 220 221 222 223 224 225 226 227 228 229 230
  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);
  }
231 232 233
  if (!rist_addresses) {
    rist_addresses = g_strdup (DEFAULT_RIST_ADDRESSES);
  }
234 235 236

  http_server = mss_http_server_new ();

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

  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
254 255 256 257
  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
258 259 260 261 262 263 264 265 266
  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);

267 268 269
  loop = g_main_loop_new (NULL, FALSE);
  g_unix_signal_add (SIGINT, sigint_handler, loop);

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

  g_main_loop_run (loop);
  g_main_loop_unref (loop);

281 282
  gst_element_set_state (pipeline, GST_STATE_NULL);

283 284
  gst_clear_object (&pipeline);
  g_clear_object (&http_server);
285 286
  g_clear_pointer (&srt_uri, g_free);
  g_clear_pointer (&rist_addresses, g_free);
287
}