2 * Demo gstreamer app for negotiating and streaming a sendrecv webrtc stream
3 * with a browser JS app.
5 * gcc webrtc-sendrecv.c $(pkg-config --cflags --libs gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-2.4 json-glib-1.0) -o webrtc-sendrecv
7 * Author: Nirbheek Chauhan <nirbheek@centricular.com>
10 #include <gst/sdp/sdp.h>
11 #include <gst/rtp/rtp.h>
13 #define GST_USE_UNSTABLE_API
14 #include <gst/webrtc/webrtc.h>
17 #include <libsoup/soup.h>
18 #include <json-glib/json-glib.h>
24 APP_STATE_UNKNOWN = 0,
25 APP_STATE_ERROR = 1, /* generic error */
26 SERVER_CONNECTING = 1000,
27 SERVER_CONNECTION_ERROR,
28 SERVER_CONNECTED, /* Ready to register */
29 SERVER_REGISTERING = 2000,
30 SERVER_REGISTRATION_ERROR,
31 SERVER_REGISTERED, /* Ready to call a peer */
32 SERVER_CLOSED, /* server connection closed by us or the server */
33 PEER_CONNECTING = 3000,
34 PEER_CONNECTION_ERROR,
36 PEER_CALL_NEGOTIATING = 4000,
43 #define GST_CAT_DEFAULT webrtc_sendrecv_debug
44 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
46 static GMainLoop *loop;
47 static GstElement *pipe1, *webrtc1 = NULL;
48 static GObject *send_channel, *receive_channel;
50 static SoupWebsocketConnection *ws_conn = NULL;
51 static enum AppState app_state = 0;
52 static gchar *peer_id = NULL;
53 static gchar *our_id = NULL;
54 static const gchar *server_url = "wss://webrtc.nirbheek.in:8443";
55 static gboolean disable_ssl = FALSE;
56 static gboolean remote_is_offerer = FALSE;
58 static GOptionEntry entries[] = {
59 {"peer-id", 0, 0, G_OPTION_ARG_STRING, &peer_id,
60 "String ID of the peer to connect to", "ID"},
61 {"our-id", 0, 0, G_OPTION_ARG_STRING, &our_id,
62 "String ID of the session that peer can connect to us", "ID"},
63 {"server", 0, 0, G_OPTION_ARG_STRING, &server_url,
64 "Signalling server to connect to", "URL"},
65 {"disable-ssl", 0, 0, G_OPTION_ARG_NONE, &disable_ssl, "Disable ssl", NULL},
66 {"remote-offerer", 0, 0, G_OPTION_ARG_NONE, &remote_is_offerer,
67 "Request that the peer generate the offer and we'll answer", NULL},
72 cleanup_and_quit_loop (const gchar * msg, enum AppState state)
75 gst_printerr ("%s\n", msg);
80 if (soup_websocket_connection_get_state (ws_conn) ==
81 SOUP_WEBSOCKET_STATE_OPEN)
82 /* This will call us again */
83 soup_websocket_connection_close (ws_conn, 1000, "");
85 g_clear_object (&ws_conn);
89 g_main_loop_quit (loop);
90 g_clear_pointer (&loop, g_main_loop_unref);
93 /* To allow usage as a GSourceFunc */
94 return G_SOURCE_REMOVE;
98 get_string_from_json_object (JsonObject * object)
101 JsonGenerator *generator;
104 /* Make it the root node */
105 root = json_node_init_object (json_node_alloc (), object);
106 generator = json_generator_new ();
107 json_generator_set_root (generator, root);
108 text = json_generator_to_data (generator, NULL);
110 /* Release everything */
111 g_object_unref (generator);
112 json_node_free (root);
117 handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name,
118 const char *sink_name)
121 GstElement *q, *conv, *resample, *sink;
122 GstPadLinkReturn ret;
124 gst_println ("Trying to handle stream with %s ! %s", convert_name, sink_name);
126 q = gst_element_factory_make ("queue", NULL);
127 g_assert_nonnull (q);
128 conv = gst_element_factory_make (convert_name, NULL);
129 g_assert_nonnull (conv);
130 sink = gst_element_factory_make (sink_name, NULL);
131 g_assert_nonnull (sink);
133 if (g_strcmp0 (convert_name, "audioconvert") == 0) {
134 /* Might also need to resample, so add it just in case.
135 * Will be a no-op if it's not required. */
136 resample = gst_element_factory_make ("audioresample", NULL);
137 g_assert_nonnull (resample);
138 gst_bin_add_many (GST_BIN (pipe), q, conv, resample, sink, NULL);
139 gst_element_sync_state_with_parent (q);
140 gst_element_sync_state_with_parent (conv);
141 gst_element_sync_state_with_parent (resample);
142 gst_element_sync_state_with_parent (sink);
143 gst_element_link_many (q, conv, resample, sink, NULL);
145 gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL);
146 gst_element_sync_state_with_parent (q);
147 gst_element_sync_state_with_parent (conv);
148 gst_element_sync_state_with_parent (sink);
149 gst_element_link_many (q, conv, sink, NULL);
152 qpad = gst_element_get_static_pad (q, "sink");
154 ret = gst_pad_link (pad, qpad);
155 g_assert_cmphex (ret, ==, GST_PAD_LINK_OK);
159 on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad,
165 if (!gst_pad_has_current_caps (pad)) {
166 gst_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n",
171 caps = gst_pad_get_current_caps (pad);
172 name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
174 if (g_str_has_prefix (name, "video")) {
175 handle_media_stream (pad, pipe, "videoconvert", "autovideosink");
176 } else if (g_str_has_prefix (name, "audio")) {
177 handle_media_stream (pad, pipe, "audioconvert", "autoaudiosink");
179 gst_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad));
184 on_incoming_stream (GstElement * webrtc, GstPad * pad, GstElement * pipe)
186 GstElement *decodebin;
189 if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC)
192 decodebin = gst_element_factory_make ("decodebin", NULL);
193 g_signal_connect (decodebin, "pad-added",
194 G_CALLBACK (on_incoming_decodebin_stream), pipe);
195 gst_bin_add (GST_BIN (pipe), decodebin);
196 gst_element_sync_state_with_parent (decodebin);
198 sinkpad = gst_element_get_static_pad (decodebin, "sink");
199 gst_pad_link (pad, sinkpad);
200 gst_object_unref (sinkpad);
204 send_ice_candidate_message (GstElement * webrtc G_GNUC_UNUSED, guint mlineindex,
205 gchar * candidate, gpointer user_data G_GNUC_UNUSED)
208 JsonObject *ice, *msg;
210 if (app_state < PEER_CALL_NEGOTIATING) {
211 cleanup_and_quit_loop ("Can't send ICE, not in call", APP_STATE_ERROR);
215 ice = json_object_new ();
216 json_object_set_string_member (ice, "candidate", candidate);
217 json_object_set_int_member (ice, "sdpMLineIndex", mlineindex);
218 msg = json_object_new ();
219 json_object_set_object_member (msg, "ice", ice);
220 text = get_string_from_json_object (msg);
221 json_object_unref (msg);
223 soup_websocket_connection_send_text (ws_conn, text);
228 send_sdp_to_peer (GstWebRTCSessionDescription * desc)
231 JsonObject *msg, *sdp;
233 if (app_state < PEER_CALL_NEGOTIATING) {
234 cleanup_and_quit_loop ("Can't send SDP to peer, not in call",
239 text = gst_sdp_message_as_text (desc->sdp);
240 sdp = json_object_new ();
242 if (desc->type == GST_WEBRTC_SDP_TYPE_OFFER) {
243 gst_print ("Sending offer:\n%s\n", text);
244 json_object_set_string_member (sdp, "type", "offer");
245 } else if (desc->type == GST_WEBRTC_SDP_TYPE_ANSWER) {
246 gst_print ("Sending answer:\n%s\n", text);
247 json_object_set_string_member (sdp, "type", "answer");
249 g_assert_not_reached ();
252 json_object_set_string_member (sdp, "sdp", text);
255 msg = json_object_new ();
256 json_object_set_object_member (msg, "sdp", sdp);
257 text = get_string_from_json_object (msg);
258 json_object_unref (msg);
260 soup_websocket_connection_send_text (ws_conn, text);
264 /* Offer created by our pipeline, to be sent to the peer */
266 on_offer_created (GstPromise * promise, gpointer user_data)
268 GstWebRTCSessionDescription *offer = NULL;
269 const GstStructure *reply;
271 g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
273 g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED);
274 reply = gst_promise_get_reply (promise);
275 gst_structure_get (reply, "offer",
276 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
277 gst_promise_unref (promise);
279 promise = gst_promise_new ();
280 g_signal_emit_by_name (webrtc1, "set-local-description", offer, promise);
281 gst_promise_interrupt (promise);
282 gst_promise_unref (promise);
284 /* Send offer to peer */
285 send_sdp_to_peer (offer);
286 gst_webrtc_session_description_free (offer);
290 on_negotiation_needed (GstElement * element, gpointer user_data)
292 gboolean create_offer = GPOINTER_TO_INT (user_data);
293 app_state = PEER_CALL_NEGOTIATING;
295 if (remote_is_offerer) {
296 soup_websocket_connection_send_text (ws_conn, "OFFER_REQUEST");
297 } else if (create_offer) {
298 GstPromise *promise =
299 gst_promise_new_with_change_func (on_offer_created, NULL, NULL);
300 g_signal_emit_by_name (webrtc1, "create-offer", NULL, promise);
304 #define STUN_SERVER " stun-server=stun://stun.l.google.com:19302 "
305 #define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload="
306 #define RTP_CAPS_VP8 "application/x-rtp,media=video,encoding-name=VP8,payload="
309 data_channel_on_error (GObject * dc, gpointer user_data)
311 cleanup_and_quit_loop ("Data channel error", 0);
315 data_channel_on_open (GObject * dc, gpointer user_data)
317 GBytes *bytes = g_bytes_new ("data", strlen ("data"));
318 gst_print ("data channel opened\n");
319 g_signal_emit_by_name (dc, "send-string", "Hi! from GStreamer");
320 g_signal_emit_by_name (dc, "send-data", bytes);
321 g_bytes_unref (bytes);
325 data_channel_on_close (GObject * dc, gpointer user_data)
327 cleanup_and_quit_loop ("Data channel closed", 0);
331 data_channel_on_message_string (GObject * dc, gchar * str, gpointer user_data)
333 gst_print ("Received data channel message: %s\n", str);
337 connect_data_channel_signals (GObject * data_channel)
339 g_signal_connect (data_channel, "on-error",
340 G_CALLBACK (data_channel_on_error), NULL);
341 g_signal_connect (data_channel, "on-open", G_CALLBACK (data_channel_on_open),
343 g_signal_connect (data_channel, "on-close",
344 G_CALLBACK (data_channel_on_close), NULL);
345 g_signal_connect (data_channel, "on-message-string",
346 G_CALLBACK (data_channel_on_message_string), NULL);
350 on_data_channel (GstElement * webrtc, GObject * data_channel,
353 connect_data_channel_signals (data_channel);
354 receive_channel = data_channel;
358 on_ice_gathering_state_notify (GstElement * webrtcbin, GParamSpec * pspec,
361 GstWebRTCICEGatheringState ice_gather_state;
362 const gchar *new_state = "unknown";
364 g_object_get (webrtcbin, "ice-gathering-state", &ice_gather_state, NULL);
365 switch (ice_gather_state) {
366 case GST_WEBRTC_ICE_GATHERING_STATE_NEW:
369 case GST_WEBRTC_ICE_GATHERING_STATE_GATHERING:
370 new_state = "gathering";
372 case GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE:
373 new_state = "complete";
376 gst_print ("ICE gathering state changed to %s\n", new_state);
379 static gboolean webrtcbin_get_stats (GstElement * webrtcbin);
382 on_webrtcbin_stat (GQuark field_id, const GValue * value, gpointer unused)
384 if (GST_VALUE_HOLDS_STRUCTURE (value)) {
385 GST_DEBUG ("stat: \'%s\': %" GST_PTR_FORMAT, g_quark_to_string (field_id),
386 gst_value_get_structure (value));
388 GST_FIXME ("unknown field \'%s\' value type: \'%s\'",
389 g_quark_to_string (field_id), g_type_name (G_VALUE_TYPE (value)));
396 on_webrtcbin_get_stats (GstPromise * promise, GstElement * webrtcbin)
398 const GstStructure *stats;
400 g_return_if_fail (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED);
402 stats = gst_promise_get_reply (promise);
403 gst_structure_foreach (stats, on_webrtcbin_stat, NULL);
405 g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtcbin);
409 webrtcbin_get_stats (GstElement * webrtcbin)
414 gst_promise_new_with_change_func (
415 (GstPromiseChangeFunc) on_webrtcbin_get_stats, webrtcbin, NULL);
417 GST_TRACE ("emitting get-stats on %" GST_PTR_FORMAT, webrtcbin);
418 g_signal_emit_by_name (webrtcbin, "get-stats", NULL, promise);
419 gst_promise_unref (promise);
421 return G_SOURCE_REMOVE;
424 #define RTP_TWCC_URI "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
427 start_pipeline (gboolean create_offer)
429 GstStateChangeReturn ret;
430 GError *error = NULL;
433 gst_parse_launch ("webrtcbin bundle-policy=max-bundle name=sendrecv "
435 "videotestsrc is-live=true pattern=ball ! videoconvert ! queue ! "
436 /* increase the default keyframe distance, browsers have really long
437 * periods between keyframes and rely on PLI events on packet loss to
438 * fix corrupted video.
440 "vp8enc deadline=1 keyframe-max-dist=2000 ! "
441 /* picture-id-mode=15-bit seems to make TWCC stats behave better */
442 "rtpvp8pay name=videopay picture-id-mode=15-bit ! "
443 "queue ! " RTP_CAPS_VP8 "96 ! sendrecv. "
444 "audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay name=audiopay ! "
445 "queue ! " RTP_CAPS_OPUS "97 ! sendrecv. ", &error);
448 gst_printerr ("Failed to parse launch: %s\n", error->message);
449 g_error_free (error);
453 webrtc1 = gst_bin_get_by_name (GST_BIN (pipe1), "sendrecv");
454 g_assert_nonnull (webrtc1);
456 if (remote_is_offerer) {
457 /* XXX: this will fail when the remote offers twcc as the extension id
458 * cannot currently be negotiated when receiving an offer.
460 GST_FIXME ("Need to implement header extension negotiation when "
461 "reciving a remote offers");
463 GstElement *videopay, *audiopay;
464 GstRTPHeaderExtension *video_twcc, *audio_twcc;
466 videopay = gst_bin_get_by_name (GST_BIN (pipe1), "videopay");
467 g_assert_nonnull (videopay);
468 video_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI);
469 g_assert_nonnull (video_twcc);
470 gst_rtp_header_extension_set_id (video_twcc, 1);
471 g_signal_emit_by_name (videopay, "add-extension", video_twcc);
472 g_clear_object (&video_twcc);
473 g_clear_object (&videopay);
475 audiopay = gst_bin_get_by_name (GST_BIN (pipe1), "audiopay");
476 g_assert_nonnull (audiopay);
477 audio_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI);
478 g_assert_nonnull (audio_twcc);
479 gst_rtp_header_extension_set_id (audio_twcc, 1);
480 g_signal_emit_by_name (audiopay, "add-extension", audio_twcc);
481 g_clear_object (&audio_twcc);
482 g_clear_object (&audiopay);
485 /* This is the gstwebrtc entry point where we create the offer and so on. It
486 * will be called when the pipeline goes to PLAYING. */
487 g_signal_connect (webrtc1, "on-negotiation-needed",
488 G_CALLBACK (on_negotiation_needed), GINT_TO_POINTER (create_offer));
489 /* We need to transmit this ICE candidate to the browser via the websockets
490 * signalling server. Incoming ice candidates from the browser need to be
491 * added by us too, see on_server_message() */
492 g_signal_connect (webrtc1, "on-ice-candidate",
493 G_CALLBACK (send_ice_candidate_message), NULL);
494 g_signal_connect (webrtc1, "notify::ice-gathering-state",
495 G_CALLBACK (on_ice_gathering_state_notify), NULL);
497 gst_element_set_state (pipe1, GST_STATE_READY);
499 g_signal_emit_by_name (webrtc1, "create-data-channel", "channel", NULL,
502 gst_print ("Created data channel\n");
503 connect_data_channel_signals (send_channel);
505 gst_print ("Could not create data channel, is usrsctp available?\n");
508 g_signal_connect (webrtc1, "on-data-channel", G_CALLBACK (on_data_channel),
510 /* Incoming streams will be exposed via this signal */
511 g_signal_connect (webrtc1, "pad-added", G_CALLBACK (on_incoming_stream),
513 /* Lifetime is the same as the pipeline itself */
514 gst_object_unref (webrtc1);
516 g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtc1);
518 gst_print ("Starting pipeline\n");
519 ret = gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_PLAYING);
520 if (ret == GST_STATE_CHANGE_FAILURE)
527 g_clear_object (&pipe1);
538 if (soup_websocket_connection_get_state (ws_conn) !=
539 SOUP_WEBSOCKET_STATE_OPEN)
545 gst_print ("Setting up signalling server call with %s\n", peer_id);
546 app_state = PEER_CONNECTING;
547 msg = g_strdup_printf ("SESSION %s", peer_id);
548 soup_websocket_connection_send_text (ws_conn, msg);
554 register_with_server (void)
558 if (soup_websocket_connection_get_state (ws_conn) !=
559 SOUP_WEBSOCKET_STATE_OPEN)
565 id = g_random_int_range (10, 10000);
566 gst_print ("Registering id %i with server\n", id);
568 hello = g_strdup_printf ("HELLO %i", id);
570 gst_print ("Registering id %s with server\n", our_id);
572 hello = g_strdup_printf ("HELLO %s", our_id);
575 app_state = SERVER_REGISTERING;
577 /* Register with the server with a random integer id. Reply will be received
578 * by on_server_message() */
579 soup_websocket_connection_send_text (ws_conn, hello);
586 on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED,
587 gpointer user_data G_GNUC_UNUSED)
589 app_state = SERVER_CLOSED;
590 cleanup_and_quit_loop ("Server connection closed", 0);
593 /* Answer created by our pipeline, to be sent to the peer */
595 on_answer_created (GstPromise * promise, gpointer user_data)
597 GstWebRTCSessionDescription *answer = NULL;
598 const GstStructure *reply;
600 g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
602 g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED);
603 reply = gst_promise_get_reply (promise);
604 gst_structure_get (reply, "answer",
605 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL);
606 gst_promise_unref (promise);
608 promise = gst_promise_new ();
609 g_signal_emit_by_name (webrtc1, "set-local-description", answer, promise);
610 gst_promise_interrupt (promise);
611 gst_promise_unref (promise);
613 /* Send answer to peer */
614 send_sdp_to_peer (answer);
615 gst_webrtc_session_description_free (answer);
619 on_offer_set (GstPromise * promise, gpointer user_data)
621 gst_promise_unref (promise);
622 promise = gst_promise_new_with_change_func (on_answer_created, NULL, NULL);
623 g_signal_emit_by_name (webrtc1, "create-answer", NULL, promise);
627 on_offer_received (GstSDPMessage * sdp)
629 GstWebRTCSessionDescription *offer = NULL;
632 offer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_OFFER, sdp);
633 g_assert_nonnull (offer);
635 /* Set remote description on our pipeline */
637 promise = gst_promise_new_with_change_func (on_offer_set, NULL, NULL);
638 g_signal_emit_by_name (webrtc1, "set-remote-description", offer, promise);
640 gst_webrtc_session_description_free (offer);
643 /* One mega message handler for our asynchronous calling mechanism */
645 on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type,
646 GBytes * message, gpointer user_data)
651 case SOUP_WEBSOCKET_DATA_BINARY:
652 gst_printerr ("Received unknown binary message, ignoring\n");
654 case SOUP_WEBSOCKET_DATA_TEXT:{
656 const gchar *data = g_bytes_get_data (message, &size);
657 /* Convert to NULL-terminated string */
658 text = g_strndup (data, size);
662 g_assert_not_reached ();
665 if (g_strcmp0 (text, "HELLO") == 0) {
666 /* Server has accepted our registration, we are ready to send commands */
667 if (app_state != SERVER_REGISTERING) {
668 cleanup_and_quit_loop ("ERROR: Received HELLO when not registering",
672 app_state = SERVER_REGISTERED;
673 gst_print ("Registered with server\n");
675 /* Ask signalling server to connect us with a specific peer */
676 if (!setup_call ()) {
677 cleanup_and_quit_loop ("ERROR: Failed to setup call", PEER_CALL_ERROR);
681 gst_println ("Waiting for connection from peer (our-id: %s)", our_id);
683 } else if (g_strcmp0 (text, "SESSION_OK") == 0) {
684 /* The call initiated by us has been setup by the server; now we can start
686 if (app_state != PEER_CONNECTING) {
687 cleanup_and_quit_loop ("ERROR: Received SESSION_OK when not calling",
688 PEER_CONNECTION_ERROR);
692 app_state = PEER_CONNECTED;
693 /* Start negotiation (exchange SDP and ICE candidates) */
694 if (!start_pipeline (TRUE))
695 cleanup_and_quit_loop ("ERROR: failed to start pipeline",
697 } else if (g_strcmp0 (text, "OFFER_REQUEST") == 0) {
698 if (app_state != SERVER_REGISTERED) {
699 gst_printerr ("Received OFFER_REQUEST at a strange time, ignoring\n");
702 gst_print ("Received OFFER_REQUEST, sending offer\n");
703 /* Peer wants us to start negotiation (exchange SDP and ICE candidates) */
704 if (!start_pipeline (TRUE))
705 cleanup_and_quit_loop ("ERROR: failed to start pipeline",
707 } else if (g_str_has_prefix (text, "ERROR")) {
710 case SERVER_CONNECTING:
711 app_state = SERVER_CONNECTION_ERROR;
713 case SERVER_REGISTERING:
714 app_state = SERVER_REGISTRATION_ERROR;
716 case PEER_CONNECTING:
717 app_state = PEER_CONNECTION_ERROR;
720 case PEER_CALL_NEGOTIATING:
721 app_state = PEER_CALL_ERROR;
724 app_state = APP_STATE_ERROR;
726 cleanup_and_quit_loop (text, 0);
728 /* Look for JSON messages containing SDP and ICE candidates */
730 JsonObject *object, *child;
731 JsonParser *parser = json_parser_new ();
732 if (!json_parser_load_from_data (parser, text, -1, NULL)) {
733 gst_printerr ("Unknown message '%s', ignoring\n", text);
734 g_object_unref (parser);
738 root = json_parser_get_root (parser);
739 if (!JSON_NODE_HOLDS_OBJECT (root)) {
740 gst_printerr ("Unknown json message '%s', ignoring\n", text);
741 g_object_unref (parser);
745 /* If peer connection wasn't made yet and we are expecting peer will
746 * connect to us, launch pipeline at this moment */
747 if (!webrtc1 && our_id) {
748 if (!start_pipeline (FALSE)) {
749 cleanup_and_quit_loop ("ERROR: failed to start pipeline",
753 app_state = PEER_CALL_NEGOTIATING;
756 object = json_node_get_object (root);
757 /* Check type of JSON message */
758 if (json_object_has_member (object, "sdp")) {
761 const gchar *text, *sdptype;
762 GstWebRTCSessionDescription *answer;
764 g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
766 child = json_object_get_object_member (object, "sdp");
768 if (!json_object_has_member (child, "type")) {
769 cleanup_and_quit_loop ("ERROR: received SDP without 'type'",
774 sdptype = json_object_get_string_member (child, "type");
775 /* In this example, we create the offer and receive one answer by default,
776 * but it's possible to comment out the offer creation and wait for an offer
777 * instead, so we handle either here.
779 * See tests/examples/webrtcbidirectional.c in gst-plugins-bad for another
780 * example how to handle offers from peers and reply with answers using webrtcbin. */
781 text = json_object_get_string_member (child, "sdp");
782 ret = gst_sdp_message_new (&sdp);
783 g_assert_cmphex (ret, ==, GST_SDP_OK);
784 ret = gst_sdp_message_parse_buffer ((guint8 *) text, strlen (text), sdp);
785 g_assert_cmphex (ret, ==, GST_SDP_OK);
787 if (g_str_equal (sdptype, "answer")) {
788 gst_print ("Received answer:\n%s\n", text);
789 answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER,
791 g_assert_nonnull (answer);
793 /* Set remote description on our pipeline */
795 GstPromise *promise = gst_promise_new ();
796 g_signal_emit_by_name (webrtc1, "set-remote-description", answer,
798 gst_promise_interrupt (promise);
799 gst_promise_unref (promise);
801 app_state = PEER_CALL_STARTED;
803 gst_print ("Received offer:\n%s\n", text);
804 on_offer_received (sdp);
807 } else if (json_object_has_member (object, "ice")) {
808 const gchar *candidate;
811 child = json_object_get_object_member (object, "ice");
812 candidate = json_object_get_string_member (child, "candidate");
813 sdpmlineindex = json_object_get_int_member (child, "sdpMLineIndex");
815 /* Add ice candidate sent by remote peer */
816 g_signal_emit_by_name (webrtc1, "add-ice-candidate", sdpmlineindex,
819 gst_printerr ("Ignoring unknown JSON message:\n%s\n", text);
821 g_object_unref (parser);
829 on_server_connected (SoupSession * session, GAsyncResult * res,
832 GError *error = NULL;
834 ws_conn = soup_session_websocket_connect_finish (session, res, &error);
836 cleanup_and_quit_loop (error->message, SERVER_CONNECTION_ERROR);
837 g_error_free (error);
841 g_assert_nonnull (ws_conn);
843 app_state = SERVER_CONNECTED;
844 gst_print ("Connected to signalling server\n");
846 g_signal_connect (ws_conn, "closed", G_CALLBACK (on_server_closed), NULL);
847 g_signal_connect (ws_conn, "message", G_CALLBACK (on_server_message), NULL);
849 /* Register with the server so it knows about us and can accept commands */
850 register_with_server ();
854 * Connect to the signalling server. This is the entrypoint for everything else.
857 connect_to_websocket_server_async (void)
860 SoupMessage *message;
861 SoupSession *session;
862 const char *https_aliases[] = { "wss", NULL };
865 soup_session_new_with_options (SOUP_SESSION_SSL_STRICT, !disable_ssl,
866 SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE,
867 //SOUP_SESSION_SSL_CA_FILE, "/etc/ssl/certs/ca-bundle.crt",
868 SOUP_SESSION_HTTPS_ALIASES, https_aliases, NULL);
870 logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
871 soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
872 g_object_unref (logger);
874 message = soup_message_new (SOUP_METHOD_GET, server_url);
876 gst_print ("Connecting to server...\n");
878 /* Once connected, we will register */
879 soup_session_websocket_connect_async (session, message, NULL, NULL, NULL,
880 (GAsyncReadyCallback) on_server_connected, message);
881 app_state = SERVER_CONNECTING;
890 GstRegistry *registry;
891 const gchar *needed[] = { "opus", "vpx", "nice", "webrtc", "dtls", "srtp",
892 "rtpmanager", "videotestsrc", "audiotestsrc", NULL
895 registry = gst_registry_get ();
897 for (i = 0; i < g_strv_length ((gchar **) needed); i++) {
898 plugin = gst_registry_find_plugin (registry, needed[i]);
900 gst_print ("Required gstreamer plugin '%s' not found\n", needed[i]);
904 gst_object_unref (plugin);
910 main (int argc, char *argv[])
912 GOptionContext *context;
913 GError *error = NULL;
916 context = g_option_context_new ("- gstreamer webrtc sendrecv demo");
917 g_option_context_add_main_entries (context, entries, NULL);
918 g_option_context_add_group (context, gst_init_get_option_group ());
919 if (!g_option_context_parse (context, &argc, &argv, &error)) {
920 gst_printerr ("Error initializing: %s\n", error->message);
924 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "webrtc-sendrecv", 0,
925 "WebRTC Sending and Receiving example");
927 if (!check_plugins ()) {
931 if (!peer_id && !our_id) {
932 gst_printerr ("--peer-id or --our-id is a required argument\n");
936 if (peer_id && our_id) {
937 gst_printerr ("specify only --peer-id or --our-id\n");
943 /* Disable ssl when running a localhost server, because
944 * it's probably a test server with a self-signed certificate */
946 GstUri *uri = gst_uri_from_string (server_url);
947 if (g_strcmp0 ("localhost", gst_uri_get_host (uri)) == 0 ||
948 g_strcmp0 ("127.0.0.1", gst_uri_get_host (uri)) == 0)
953 loop = g_main_loop_new (NULL, FALSE);
955 connect_to_websocket_server_async ();
957 g_main_loop_run (loop);
960 g_main_loop_unref (loop);
963 gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_NULL);
964 gst_print ("Pipeline stopped\n");
965 gst_object_unref (pipe1);