examples: webrtc: Add bus handling to the Android and C sendrecv examples
[platform/upstream/gstreamer.git] / subprojects / gst-examples / webrtc / sendrecv / gst / webrtc-sendrecv.c
1 /*
2  * Demo gstreamer app for negotiating and streaming a sendrecv webrtc stream
3  * with a browser JS app.
4  *
5  * Build by running: `make webrtc-sendrecv`, or build the gstreamer monorepo.
6  *
7  * Author: Nirbheek Chauhan <nirbheek@centricular.com>
8  */
9 #include <gst/gst.h>
10 #include <gst/sdp/sdp.h>
11 #include <gst/rtp/rtp.h>
12
13 #include <gst/webrtc/webrtc.h>
14 #include <gst/webrtc/nice/nice.h>
15
16 #include "custom_agent.h"
17
18 /* For signalling */
19 #include <libsoup/soup.h>
20 #include <json-glib/json-glib.h>
21
22 #include <string.h>
23
24 enum AppState
25 {
26   APP_STATE_UNKNOWN = 0,
27   APP_STATE_ERROR = 1,          /* generic error */
28   SERVER_CONNECTING = 1000,
29   SERVER_CONNECTION_ERROR,
30   SERVER_CONNECTED,             /* Ready to register */
31   SERVER_REGISTERING = 2000,
32   SERVER_REGISTRATION_ERROR,
33   SERVER_REGISTERED,            /* Ready to call a peer */
34   SERVER_CLOSED,                /* server connection closed by us or the server */
35   PEER_CONNECTING = 3000,
36   PEER_CONNECTION_ERROR,
37   PEER_CONNECTED,
38   PEER_CALL_NEGOTIATING = 4000,
39   PEER_CALL_STARTED,
40   PEER_CALL_STOPPING,
41   PEER_CALL_STOPPED,
42   PEER_CALL_ERROR,
43 };
44
45 #define GST_CAT_DEFAULT webrtc_sendrecv_debug
46 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
47
48 static GMainLoop *loop;
49 static GstElement *pipe1, *webrtc1, *audio_bin, *video_bin = NULL;
50 static GObject *send_channel, *receive_channel;
51
52 static SoupWebsocketConnection *ws_conn = NULL;
53 static enum AppState app_state = 0;
54 static gchar *peer_id = NULL;
55 static gchar *our_id = NULL;
56 static const gchar *server_url = "wss://webrtc.nirbheek.in:8443";
57 static gboolean disable_ssl = FALSE;
58 static gboolean remote_is_offerer = FALSE;
59 static gboolean custom_ice = FALSE;
60
61 static GOptionEntry entries[] = {
62   {"peer-id", 0, 0, G_OPTION_ARG_STRING, &peer_id,
63       "String ID of the peer to connect to", "ID"},
64   {"our-id", 0, 0, G_OPTION_ARG_STRING, &our_id,
65       "String ID that the peer can use to connect to us", "ID"},
66   {"server", 0, 0, G_OPTION_ARG_STRING, &server_url,
67       "Signalling server to connect to", "URL"},
68   {"disable-ssl", 0, 0, G_OPTION_ARG_NONE, &disable_ssl, "Disable ssl", NULL},
69   {"remote-offerer", 0, 0, G_OPTION_ARG_NONE, &remote_is_offerer,
70       "Request that the peer generate the offer and we'll answer", NULL},
71   {"custom-ice", 0, 0, G_OPTION_ARG_NONE, &custom_ice,
72       "Use a custom ice agent", NULL},
73   {NULL},
74 };
75
76 static gboolean
77 cleanup_and_quit_loop (const gchar * msg, enum AppState state)
78 {
79   if (msg)
80     gst_printerr ("%s\n", msg);
81   if (state > 0)
82     app_state = state;
83
84   if (ws_conn) {
85     if (soup_websocket_connection_get_state (ws_conn) ==
86         SOUP_WEBSOCKET_STATE_OPEN)
87       /* This will call us again */
88       soup_websocket_connection_close (ws_conn, 1000, "");
89     else
90       g_clear_object (&ws_conn);
91   }
92
93   if (loop) {
94     g_main_loop_quit (loop);
95     g_clear_pointer (&loop, g_main_loop_unref);
96   }
97
98   /* To allow usage as a GSourceFunc */
99   return G_SOURCE_REMOVE;
100 }
101
102 static gchar *
103 get_string_from_json_object (JsonObject * object)
104 {
105   JsonNode *root;
106   JsonGenerator *generator;
107   gchar *text;
108
109   /* Make it the root node */
110   root = json_node_init_object (json_node_alloc (), object);
111   generator = json_generator_new ();
112   json_generator_set_root (generator, root);
113   text = json_generator_to_data (generator, NULL);
114
115   /* Release everything */
116   g_object_unref (generator);
117   json_node_free (root);
118   return text;
119 }
120
121 static void
122 handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name,
123     const char *sink_name)
124 {
125   GstPad *qpad;
126   GstElement *q, *conv, *resample, *sink;
127   GstPadLinkReturn ret;
128
129   gst_println ("Trying to handle stream with %s ! %s", convert_name, sink_name);
130
131   q = gst_element_factory_make ("queue", NULL);
132   g_assert_nonnull (q);
133   conv = gst_element_factory_make (convert_name, NULL);
134   g_assert_nonnull (conv);
135   sink = gst_element_factory_make (sink_name, NULL);
136   g_assert_nonnull (sink);
137
138   if (g_strcmp0 (convert_name, "audioconvert") == 0) {
139     /* Might also need to resample, so add it just in case.
140      * Will be a no-op if it's not required. */
141     resample = gst_element_factory_make ("audioresample", NULL);
142     g_assert_nonnull (resample);
143     gst_bin_add_many (GST_BIN (pipe), q, conv, resample, sink, NULL);
144     gst_element_sync_state_with_parent (q);
145     gst_element_sync_state_with_parent (conv);
146     gst_element_sync_state_with_parent (resample);
147     gst_element_sync_state_with_parent (sink);
148     gst_element_link_many (q, conv, resample, sink, NULL);
149   } else {
150     gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL);
151     gst_element_sync_state_with_parent (q);
152     gst_element_sync_state_with_parent (conv);
153     gst_element_sync_state_with_parent (sink);
154     gst_element_link_many (q, conv, sink, NULL);
155   }
156
157   qpad = gst_element_get_static_pad (q, "sink");
158
159   ret = gst_pad_link (pad, qpad);
160   g_assert_cmphex (ret, ==, GST_PAD_LINK_OK);
161 }
162
163 static void
164 on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad,
165     GstElement * pipe)
166 {
167   GstCaps *caps;
168   const gchar *name;
169
170   if (!gst_pad_has_current_caps (pad)) {
171     gst_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n",
172         GST_PAD_NAME (pad));
173     return;
174   }
175
176   caps = gst_pad_get_current_caps (pad);
177   name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
178
179   if (g_str_has_prefix (name, "video")) {
180     handle_media_stream (pad, pipe, "videoconvert", "autovideosink");
181   } else if (g_str_has_prefix (name, "audio")) {
182     handle_media_stream (pad, pipe, "audioconvert", "autoaudiosink");
183   } else {
184     gst_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad));
185   }
186 }
187
188 static void
189 on_incoming_stream (GstElement * webrtc, GstPad * pad, GstElement * pipe)
190 {
191   GstElement *decodebin;
192   GstPad *sinkpad;
193
194   if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC)
195     return;
196
197   decodebin = gst_element_factory_make ("decodebin", NULL);
198   g_signal_connect (decodebin, "pad-added",
199       G_CALLBACK (on_incoming_decodebin_stream), pipe);
200   gst_bin_add (GST_BIN (pipe), decodebin);
201   gst_element_sync_state_with_parent (decodebin);
202
203   sinkpad = gst_element_get_static_pad (decodebin, "sink");
204   gst_pad_link (pad, sinkpad);
205   gst_object_unref (sinkpad);
206 }
207
208 static void
209 send_ice_candidate_message (GstElement * webrtc G_GNUC_UNUSED, guint mlineindex,
210     gchar * candidate, gpointer user_data G_GNUC_UNUSED)
211 {
212   gchar *text;
213   JsonObject *ice, *msg;
214
215   if (app_state < PEER_CALL_NEGOTIATING) {
216     cleanup_and_quit_loop ("Can't send ICE, not in call", APP_STATE_ERROR);
217     return;
218   }
219
220   ice = json_object_new ();
221   json_object_set_string_member (ice, "candidate", candidate);
222   json_object_set_int_member (ice, "sdpMLineIndex", mlineindex);
223   msg = json_object_new ();
224   json_object_set_object_member (msg, "ice", ice);
225   text = get_string_from_json_object (msg);
226   json_object_unref (msg);
227
228   soup_websocket_connection_send_text (ws_conn, text);
229   g_free (text);
230 }
231
232 static void
233 send_sdp_to_peer (GstWebRTCSessionDescription * desc)
234 {
235   gchar *text;
236   JsonObject *msg, *sdp;
237
238   if (app_state < PEER_CALL_NEGOTIATING) {
239     cleanup_and_quit_loop ("Can't send SDP to peer, not in call",
240         APP_STATE_ERROR);
241     return;
242   }
243
244   text = gst_sdp_message_as_text (desc->sdp);
245   sdp = json_object_new ();
246
247   if (desc->type == GST_WEBRTC_SDP_TYPE_OFFER) {
248     gst_print ("Sending offer:\n%s\n", text);
249     json_object_set_string_member (sdp, "type", "offer");
250   } else if (desc->type == GST_WEBRTC_SDP_TYPE_ANSWER) {
251     gst_print ("Sending answer:\n%s\n", text);
252     json_object_set_string_member (sdp, "type", "answer");
253   } else {
254     g_assert_not_reached ();
255   }
256
257   json_object_set_string_member (sdp, "sdp", text);
258   g_free (text);
259
260   msg = json_object_new ();
261   json_object_set_object_member (msg, "sdp", sdp);
262   text = get_string_from_json_object (msg);
263   json_object_unref (msg);
264
265   soup_websocket_connection_send_text (ws_conn, text);
266   g_free (text);
267 }
268
269 /* Offer created by our pipeline, to be sent to the peer */
270 static void
271 on_offer_created (GstPromise * promise, gpointer user_data)
272 {
273   GstWebRTCSessionDescription *offer = NULL;
274   const GstStructure *reply;
275
276   g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
277
278   g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED);
279   reply = gst_promise_get_reply (promise);
280   gst_structure_get (reply, "offer",
281       GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
282   gst_promise_unref (promise);
283
284   promise = gst_promise_new ();
285   g_signal_emit_by_name (webrtc1, "set-local-description", offer, promise);
286   gst_promise_interrupt (promise);
287   gst_promise_unref (promise);
288
289   /* Send offer to peer */
290   send_sdp_to_peer (offer);
291   gst_webrtc_session_description_free (offer);
292 }
293
294 static void
295 on_negotiation_needed (GstElement * element, gpointer user_data)
296 {
297   gboolean create_offer = GPOINTER_TO_INT (user_data);
298   app_state = PEER_CALL_NEGOTIATING;
299
300   if (remote_is_offerer) {
301     soup_websocket_connection_send_text (ws_conn, "OFFER_REQUEST");
302   } else if (create_offer) {
303     GstPromise *promise =
304         gst_promise_new_with_change_func (on_offer_created, NULL, NULL);
305     g_signal_emit_by_name (webrtc1, "create-offer", NULL, promise);
306   }
307 }
308
309 static void
310 data_channel_on_error (GObject * dc, gpointer user_data)
311 {
312   cleanup_and_quit_loop ("Data channel error", 0);
313 }
314
315 static void
316 data_channel_on_open (GObject * dc, gpointer user_data)
317 {
318   GBytes *bytes = g_bytes_new ("data", strlen ("data"));
319   gst_print ("data channel opened\n");
320   g_signal_emit_by_name (dc, "send-string", "Hi! from GStreamer");
321   g_signal_emit_by_name (dc, "send-data", bytes);
322   g_bytes_unref (bytes);
323 }
324
325 static void
326 data_channel_on_close (GObject * dc, gpointer user_data)
327 {
328   cleanup_and_quit_loop ("Data channel closed", 0);
329 }
330
331 static void
332 data_channel_on_message_string (GObject * dc, gchar * str, gpointer user_data)
333 {
334   gst_print ("Received data channel message: %s\n", str);
335 }
336
337 static void
338 connect_data_channel_signals (GObject * data_channel)
339 {
340   g_signal_connect (data_channel, "on-error",
341       G_CALLBACK (data_channel_on_error), NULL);
342   g_signal_connect (data_channel, "on-open", G_CALLBACK (data_channel_on_open),
343       NULL);
344   g_signal_connect (data_channel, "on-close",
345       G_CALLBACK (data_channel_on_close), NULL);
346   g_signal_connect (data_channel, "on-message-string",
347       G_CALLBACK (data_channel_on_message_string), NULL);
348 }
349
350 static void
351 on_data_channel (GstElement * webrtc, GObject * data_channel,
352     gpointer user_data)
353 {
354   connect_data_channel_signals (data_channel);
355   receive_channel = data_channel;
356 }
357
358 static void
359 on_ice_gathering_state_notify (GstElement * webrtcbin, GParamSpec * pspec,
360     gpointer user_data)
361 {
362   GstWebRTCICEGatheringState ice_gather_state;
363   const gchar *new_state = "unknown";
364
365   g_object_get (webrtcbin, "ice-gathering-state", &ice_gather_state, NULL);
366   switch (ice_gather_state) {
367     case GST_WEBRTC_ICE_GATHERING_STATE_NEW:
368       new_state = "new";
369       break;
370     case GST_WEBRTC_ICE_GATHERING_STATE_GATHERING:
371       new_state = "gathering";
372       break;
373     case GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE:
374       new_state = "complete";
375       break;
376   }
377   gst_print ("ICE gathering state changed to %s\n", new_state);
378 }
379
380 static gboolean webrtcbin_get_stats (GstElement * webrtcbin);
381
382 static gboolean
383 on_webrtcbin_stat (GQuark field_id, const GValue * value, gpointer unused)
384 {
385   if (GST_VALUE_HOLDS_STRUCTURE (value)) {
386     GST_DEBUG ("stat: \'%s\': %" GST_PTR_FORMAT, g_quark_to_string (field_id),
387         gst_value_get_structure (value));
388   } else {
389     GST_FIXME ("unknown field \'%s\' value type: \'%s\'",
390         g_quark_to_string (field_id), g_type_name (G_VALUE_TYPE (value)));
391   }
392
393   return TRUE;
394 }
395
396 static void
397 on_webrtcbin_get_stats (GstPromise * promise, GstElement * webrtcbin)
398 {
399   const GstStructure *stats;
400
401   g_return_if_fail (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED);
402
403   stats = gst_promise_get_reply (promise);
404   gst_structure_foreach (stats, on_webrtcbin_stat, NULL);
405
406   g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtcbin);
407 }
408
409 static gboolean
410 webrtcbin_get_stats (GstElement * webrtcbin)
411 {
412   GstPromise *promise;
413
414   promise =
415       gst_promise_new_with_change_func (
416       (GstPromiseChangeFunc) on_webrtcbin_get_stats, webrtcbin, NULL);
417
418   GST_TRACE ("emitting get-stats on %" GST_PTR_FORMAT, webrtcbin);
419   g_signal_emit_by_name (webrtcbin, "get-stats", NULL, promise);
420   gst_promise_unref (promise);
421
422   return G_SOURCE_REMOVE;
423 }
424
425 static gboolean
426 bus_watch_cb (GstBus * bus, GstMessage * message, gpointer user_data)
427 {
428   GstPipeline *pipeline = user_data;
429
430   switch (GST_MESSAGE_TYPE (message)) {
431     case GST_MESSAGE_ERROR:
432     {
433       GError *error = NULL;
434       gchar *debug = NULL;
435
436       gst_message_parse_error (message, &error, &debug);
437       cleanup_and_quit_loop ("ERROR: Error on bus", APP_STATE_ERROR);
438       g_warning ("Error on bus: %s (debug: %s)", error->message, debug);
439       g_error_free (error);
440       g_free (debug);
441       break;
442     }
443     case GST_MESSAGE_WARNING:
444     {
445       GError *error = NULL;
446       gchar *debug = NULL;
447
448       gst_message_parse_warning (message, &error, &debug);
449       g_warning ("Warning on bus: %s (debug: %s)", error->message, debug);
450       g_error_free (error);
451       g_free (debug);
452       break;
453     }
454     case GST_MESSAGE_LATENCY:
455       gst_bin_recalculate_latency (GST_BIN (pipeline));
456       break;
457     default:
458       break;
459   }
460
461   return G_SOURCE_CONTINUE;
462 }
463
464 #define STUN_SERVER "stun://stun.l.google.com:19302"
465 #define RTP_TWCC_URI "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
466 #define RTP_OPUS_DEFAULT_PT 97
467 #define RTP_VP8_DEFAULT_PT 96
468
469 static gboolean
470 start_pipeline (gboolean create_offer, guint opus_pt, guint vp8_pt)
471 {
472   GstBus *bus;
473   char *audio_desc, *video_desc;
474   GstStateChangeReturn ret;
475   GstWebRTCICE *custom_agent;
476   GError *audio_error = NULL;
477   GError *video_error = NULL;
478
479   pipe1 = gst_pipeline_new ("webrtc-pipeline");
480
481   audio_desc =
482       g_strdup_printf
483       ("audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample"
484       "! queue ! opusenc ! rtpopuspay name=audiopay pt=%u ! queue", opus_pt);
485   audio_bin = gst_parse_bin_from_description (audio_desc, TRUE, &audio_error);
486   g_free (audio_desc);
487   if (audio_error) {
488     gst_printerr ("Failed to parse audio_bin: %s\n", audio_error->message);
489     g_error_free (audio_error);
490     goto err;
491   }
492
493   video_desc =
494       g_strdup_printf
495       ("videotestsrc is-live=true pattern=ball ! videoconvert ! queue ! "
496       /* increase the default keyframe distance, browsers have really long
497        * periods between keyframes and rely on PLI events on packet loss to
498        * fix corrupted video.
499        */
500       "vp8enc deadline=1 keyframe-max-dist=2000 ! "
501       /* picture-id-mode=15-bit seems to make TWCC stats behave better, and
502        * fixes stuttery video playback in Chrome */
503       "rtpvp8pay name=videopay picture-id-mode=15-bit pt=%u ! queue", vp8_pt);
504   video_bin = gst_parse_bin_from_description (video_desc, TRUE, &video_error);
505   g_free (video_desc);
506   if (video_error) {
507     gst_printerr ("Failed to parse video_bin: %s\n", video_error->message);
508     g_error_free (video_error);
509     goto err;
510   }
511
512   if (custom_ice) {
513     custom_agent = GST_WEBRTC_ICE (customice_agent_new ("custom"));
514     webrtc1 = gst_element_factory_make_full ("webrtcbin", "name", "sendrecv",
515         "stun-server", STUN_SERVER, "ice-agent", custom_agent, NULL);
516   } else {
517     webrtc1 = gst_element_factory_make_full ("webrtcbin", "name", "sendrecv",
518         "stun-server", STUN_SERVER, NULL);
519   }
520   g_assert_nonnull (webrtc1);
521   gst_util_set_object_arg (G_OBJECT (webrtc1), "bundle-policy", "max-bundle");
522
523   /* Takes ownership of each: */
524   gst_bin_add_many (GST_BIN (pipe1), audio_bin, video_bin, webrtc1, NULL);
525
526   if (!gst_element_link (audio_bin, webrtc1)) {
527     gst_printerr ("Failed to link audio_bin \n");
528   }
529   if (!gst_element_link (video_bin, webrtc1)) {
530     gst_printerr ("Failed to link video_bin \n");
531   }
532
533   if (!create_offer) {
534     /* XXX: this will fail when the remote offers twcc as the extension id
535      * cannot currently be negotiated when receiving an offer.
536      */
537     GST_FIXME ("Need to implement header extension negotiation when "
538         "reciving a remote offers");
539   } else {
540     GstElement *videopay, *audiopay;
541     GstRTPHeaderExtension *video_twcc, *audio_twcc;
542
543     videopay = gst_bin_get_by_name (GST_BIN (pipe1), "videopay");
544     g_assert_nonnull (videopay);
545     video_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI);
546     g_assert_nonnull (video_twcc);
547     gst_rtp_header_extension_set_id (video_twcc, 1);
548     g_signal_emit_by_name (videopay, "add-extension", video_twcc);
549     g_clear_object (&video_twcc);
550     g_clear_object (&videopay);
551
552     audiopay = gst_bin_get_by_name (GST_BIN (pipe1), "audiopay");
553     g_assert_nonnull (audiopay);
554     audio_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI);
555     g_assert_nonnull (audio_twcc);
556     gst_rtp_header_extension_set_id (audio_twcc, 1);
557     g_signal_emit_by_name (audiopay, "add-extension", audio_twcc);
558     g_clear_object (&audio_twcc);
559     g_clear_object (&audiopay);
560   }
561
562   /* This is the gstwebrtc entry point where we create the offer and so on. It
563    * will be called when the pipeline goes to PLAYING. */
564   g_signal_connect (webrtc1, "on-negotiation-needed",
565       G_CALLBACK (on_negotiation_needed), GINT_TO_POINTER (create_offer));
566   /* We need to transmit this ICE candidate to the browser via the websockets
567    * signalling server. Incoming ice candidates from the browser need to be
568    * added by us too, see on_server_message() */
569   g_signal_connect (webrtc1, "on-ice-candidate",
570       G_CALLBACK (send_ice_candidate_message), NULL);
571   g_signal_connect (webrtc1, "notify::ice-gathering-state",
572       G_CALLBACK (on_ice_gathering_state_notify), NULL);
573
574   bus = gst_pipeline_get_bus (GST_PIPELINE (pipe1));
575   gst_bus_add_watch (bus, bus_watch_cb, pipe1);
576   gst_object_unref (bus);
577
578   gst_element_set_state (pipe1, GST_STATE_READY);
579
580   g_signal_emit_by_name (webrtc1, "create-data-channel", "channel", NULL,
581       &send_channel);
582   if (send_channel) {
583     gst_print ("Created data channel\n");
584     connect_data_channel_signals (send_channel);
585   } else {
586     gst_print ("Could not create data channel, is usrsctp available?\n");
587   }
588
589   g_signal_connect (webrtc1, "on-data-channel", G_CALLBACK (on_data_channel),
590       NULL);
591   /* Incoming streams will be exposed via this signal */
592   g_signal_connect (webrtc1, "pad-added", G_CALLBACK (on_incoming_stream),
593       pipe1);
594
595   g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtc1);
596
597   gst_print ("Starting pipeline\n");
598   ret = gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_PLAYING);
599   if (ret == GST_STATE_CHANGE_FAILURE)
600     goto err;
601
602   return TRUE;
603
604 err:
605   if (pipe1)
606     g_clear_object (&pipe1);
607   if (webrtc1)
608     webrtc1 = NULL;
609   return FALSE;
610 }
611
612 static gboolean
613 setup_call (void)
614 {
615   gchar *msg;
616
617   if (soup_websocket_connection_get_state (ws_conn) !=
618       SOUP_WEBSOCKET_STATE_OPEN)
619     return FALSE;
620
621   if (!peer_id)
622     return FALSE;
623
624   gst_print ("Setting up signalling server call with %s\n", peer_id);
625   app_state = PEER_CONNECTING;
626   msg = g_strdup_printf ("SESSION %s", peer_id);
627   soup_websocket_connection_send_text (ws_conn, msg);
628   g_free (msg);
629   return TRUE;
630 }
631
632 static gboolean
633 register_with_server (void)
634 {
635   gchar *hello;
636
637   if (soup_websocket_connection_get_state (ws_conn) !=
638       SOUP_WEBSOCKET_STATE_OPEN)
639     return FALSE;
640
641   if (!our_id) {
642     gint32 id;
643
644     id = g_random_int_range (10, 10000);
645     gst_print ("Registering id %i with server\n", id);
646
647     hello = g_strdup_printf ("HELLO %i", id);
648   } else {
649     gst_print ("Registering id %s with server\n", our_id);
650
651     hello = g_strdup_printf ("HELLO %s", our_id);
652   }
653
654   app_state = SERVER_REGISTERING;
655
656   /* Register with the server with a random integer id. Reply will be received
657    * by on_server_message() */
658   soup_websocket_connection_send_text (ws_conn, hello);
659   g_free (hello);
660
661   return TRUE;
662 }
663
664 static void
665 on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED,
666     gpointer user_data G_GNUC_UNUSED)
667 {
668   app_state = SERVER_CLOSED;
669   cleanup_and_quit_loop ("Server connection closed", 0);
670 }
671
672 /* Answer created by our pipeline, to be sent to the peer */
673 static void
674 on_answer_created (GstPromise * promise, gpointer user_data)
675 {
676   GstWebRTCSessionDescription *answer = NULL;
677   const GstStructure *reply;
678
679   g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
680
681   g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED);
682   reply = gst_promise_get_reply (promise);
683   gst_structure_get (reply, "answer",
684       GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL);
685   gst_promise_unref (promise);
686
687   promise = gst_promise_new ();
688   g_signal_emit_by_name (webrtc1, "set-local-description", answer, promise);
689   gst_promise_interrupt (promise);
690   gst_promise_unref (promise);
691
692   /* Send answer to peer */
693   send_sdp_to_peer (answer);
694   gst_webrtc_session_description_free (answer);
695 }
696
697 static void
698 on_offer_set (GstPromise * promise, gpointer user_data)
699 {
700   gst_promise_unref (promise);
701   promise = gst_promise_new_with_change_func (on_answer_created, NULL, NULL);
702   g_signal_emit_by_name (webrtc1, "create-answer", NULL, promise);
703 }
704
705 static void
706 on_offer_received (GstSDPMessage * sdp)
707 {
708   GstWebRTCSessionDescription *offer = NULL;
709   GstPromise *promise;
710
711   /* If we got an offer and we have no webrtcbin, we need to parse the SDP,
712    * get the payload types, then start the pipeline */
713   if (!webrtc1 && our_id) {
714     guint medias_len, formats_len;
715     guint opus_pt = 0, vp8_pt = 0;
716
717     gst_println ("Parsing offer to find payload types");
718
719     medias_len = gst_sdp_message_medias_len (sdp);
720     for (int i = 0; i < medias_len; i++) {
721       const GstSDPMedia *media = gst_sdp_message_get_media (sdp, i);
722       formats_len = gst_sdp_media_formats_len (media);
723       for (int j = 0; j < formats_len; j++) {
724         guint pt;
725         GstCaps *caps;
726         GstStructure *s;
727         const char *fmt, *encoding_name;
728
729         fmt = gst_sdp_media_get_format (media, j);
730         if (g_strcmp0 (fmt, "webrtc-datachannel") == 0)
731           continue;
732         pt = atoi (fmt);
733         caps = gst_sdp_media_get_caps_from_media (media, pt);
734         s = gst_caps_get_structure (caps, 0);
735         encoding_name = gst_structure_get_string (s, "encoding-name");
736         if (vp8_pt == 0 && g_strcmp0 (encoding_name, "VP8") == 0)
737           vp8_pt = pt;
738         if (opus_pt == 0 && g_strcmp0 (encoding_name, "OPUS") == 0)
739           opus_pt = pt;
740       }
741     }
742
743     g_assert_cmpint (opus_pt, !=, 0);
744     g_assert_cmpint (vp8_pt, !=, 0);
745
746     gst_println ("Starting pipeline with opus pt: %u vp8 pt: %u", opus_pt,
747         vp8_pt);
748
749     if (!start_pipeline (FALSE, opus_pt, vp8_pt)) {
750       cleanup_and_quit_loop ("ERROR: failed to start pipeline",
751           PEER_CALL_ERROR);
752     }
753   }
754
755   offer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_OFFER, sdp);
756   g_assert_nonnull (offer);
757
758   /* Set remote description on our pipeline */
759   {
760     promise = gst_promise_new_with_change_func (on_offer_set, NULL, NULL);
761     g_signal_emit_by_name (webrtc1, "set-remote-description", offer, promise);
762   }
763   gst_webrtc_session_description_free (offer);
764 }
765
766 /* One mega message handler for our asynchronous calling mechanism */
767 static void
768 on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type,
769     GBytes * message, gpointer user_data)
770 {
771   gchar *text;
772
773   switch (type) {
774     case SOUP_WEBSOCKET_DATA_BINARY:
775       gst_printerr ("Received unknown binary message, ignoring\n");
776       return;
777     case SOUP_WEBSOCKET_DATA_TEXT:{
778       gsize size;
779       const gchar *data = g_bytes_get_data (message, &size);
780       /* Convert to NULL-terminated string */
781       text = g_strndup (data, size);
782       break;
783     }
784     default:
785       g_assert_not_reached ();
786   }
787
788   if (g_strcmp0 (text, "HELLO") == 0) {
789     /* Server has accepted our registration, we are ready to send commands */
790     if (app_state != SERVER_REGISTERING) {
791       cleanup_and_quit_loop ("ERROR: Received HELLO when not registering",
792           APP_STATE_ERROR);
793       goto out;
794     }
795     app_state = SERVER_REGISTERED;
796     gst_print ("Registered with server\n");
797     if (!our_id) {
798       /* Ask signalling server to connect us with a specific peer */
799       if (!setup_call ()) {
800         cleanup_and_quit_loop ("ERROR: Failed to setup call", PEER_CALL_ERROR);
801         goto out;
802       }
803     } else {
804       gst_println ("Waiting for connection from peer (our-id: %s)", our_id);
805     }
806   } else if (g_strcmp0 (text, "SESSION_OK") == 0) {
807     /* The call initiated by us has been setup by the server; now we can start
808      * negotiation */
809     if (app_state != PEER_CONNECTING) {
810       cleanup_and_quit_loop ("ERROR: Received SESSION_OK when not calling",
811           PEER_CONNECTION_ERROR);
812       goto out;
813     }
814
815     app_state = PEER_CONNECTED;
816     /* Start negotiation (exchange SDP and ICE candidates) */
817     if (!start_pipeline (TRUE, RTP_OPUS_DEFAULT_PT, RTP_VP8_DEFAULT_PT))
818       cleanup_and_quit_loop ("ERROR: failed to start pipeline",
819           PEER_CALL_ERROR);
820   } else if (g_strcmp0 (text, "OFFER_REQUEST") == 0) {
821     if (app_state != SERVER_REGISTERED) {
822       gst_printerr ("Received OFFER_REQUEST at a strange time, ignoring\n");
823       goto out;
824     }
825     gst_print ("Received OFFER_REQUEST, sending offer\n");
826     /* Peer wants us to start negotiation (exchange SDP and ICE candidates) */
827     if (!start_pipeline (TRUE, RTP_OPUS_DEFAULT_PT, RTP_VP8_DEFAULT_PT))
828       cleanup_and_quit_loop ("ERROR: failed to start pipeline",
829           PEER_CALL_ERROR);
830   } else if (g_str_has_prefix (text, "ERROR")) {
831     /* Handle errors */
832     switch (app_state) {
833       case SERVER_CONNECTING:
834         app_state = SERVER_CONNECTION_ERROR;
835         break;
836       case SERVER_REGISTERING:
837         app_state = SERVER_REGISTRATION_ERROR;
838         break;
839       case PEER_CONNECTING:
840         app_state = PEER_CONNECTION_ERROR;
841         break;
842       case PEER_CONNECTED:
843       case PEER_CALL_NEGOTIATING:
844         app_state = PEER_CALL_ERROR;
845         break;
846       default:
847         app_state = APP_STATE_ERROR;
848     }
849     cleanup_and_quit_loop (text, 0);
850   } else {
851     /* Look for JSON messages containing SDP and ICE candidates */
852     JsonNode *root;
853     JsonObject *object, *child;
854     JsonParser *parser = json_parser_new ();
855     if (!json_parser_load_from_data (parser, text, -1, NULL)) {
856       gst_printerr ("Unknown message '%s', ignoring\n", text);
857       g_object_unref (parser);
858       goto out;
859     }
860
861     root = json_parser_get_root (parser);
862     if (!JSON_NODE_HOLDS_OBJECT (root)) {
863       gst_printerr ("Unknown json message '%s', ignoring\n", text);
864       g_object_unref (parser);
865       goto out;
866     }
867
868     object = json_node_get_object (root);
869     /* Check type of JSON message */
870     if (json_object_has_member (object, "sdp")) {
871       int ret;
872       GstSDPMessage *sdp;
873       const gchar *text, *sdptype;
874       GstWebRTCSessionDescription *answer;
875
876       app_state = PEER_CALL_NEGOTIATING;
877
878       child = json_object_get_object_member (object, "sdp");
879
880       if (!json_object_has_member (child, "type")) {
881         cleanup_and_quit_loop ("ERROR: received SDP without 'type'",
882             PEER_CALL_ERROR);
883         goto out;
884       }
885
886       sdptype = json_object_get_string_member (child, "type");
887       /* In this example, we create the offer and receive one answer by default,
888        * but it's possible to comment out the offer creation and wait for an offer
889        * instead, so we handle either here.
890        *
891        * See tests/examples/webrtcbidirectional.c in gst-plugins-bad for another
892        * example how to handle offers from peers and reply with answers using webrtcbin. */
893       text = json_object_get_string_member (child, "sdp");
894       ret = gst_sdp_message_new (&sdp);
895       g_assert_cmphex (ret, ==, GST_SDP_OK);
896       ret = gst_sdp_message_parse_buffer ((guint8 *) text, strlen (text), sdp);
897       g_assert_cmphex (ret, ==, GST_SDP_OK);
898
899       if (g_str_equal (sdptype, "answer")) {
900         gst_print ("Received answer:\n%s\n", text);
901         answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER,
902             sdp);
903         g_assert_nonnull (answer);
904
905         /* Set remote description on our pipeline */
906         {
907           GstPromise *promise = gst_promise_new ();
908           g_signal_emit_by_name (webrtc1, "set-remote-description", answer,
909               promise);
910           gst_promise_interrupt (promise);
911           gst_promise_unref (promise);
912         }
913         app_state = PEER_CALL_STARTED;
914       } else {
915         gst_print ("Received offer:\n%s\n", text);
916         on_offer_received (sdp);
917       }
918
919     } else if (json_object_has_member (object, "ice")) {
920       const gchar *candidate;
921       gint sdpmlineindex;
922
923       child = json_object_get_object_member (object, "ice");
924       candidate = json_object_get_string_member (child, "candidate");
925       sdpmlineindex = json_object_get_int_member (child, "sdpMLineIndex");
926
927       /* Add ice candidate sent by remote peer */
928       g_signal_emit_by_name (webrtc1, "add-ice-candidate", sdpmlineindex,
929           candidate);
930     } else {
931       gst_printerr ("Ignoring unknown JSON message:\n%s\n", text);
932     }
933     g_object_unref (parser);
934   }
935
936 out:
937   g_free (text);
938 }
939
940 static void
941 on_server_connected (SoupSession * session, GAsyncResult * res,
942     SoupMessage * msg)
943 {
944   GError *error = NULL;
945
946   ws_conn = soup_session_websocket_connect_finish (session, res, &error);
947   if (error) {
948     cleanup_and_quit_loop (error->message, SERVER_CONNECTION_ERROR);
949     g_error_free (error);
950     return;
951   }
952
953   g_assert_nonnull (ws_conn);
954
955   app_state = SERVER_CONNECTED;
956   gst_print ("Connected to signalling server\n");
957
958   g_signal_connect (ws_conn, "closed", G_CALLBACK (on_server_closed), NULL);
959   g_signal_connect (ws_conn, "message", G_CALLBACK (on_server_message), NULL);
960
961   /* Register with the server so it knows about us and can accept commands */
962   register_with_server ();
963 }
964
965 /*
966  * Connect to the signalling server. This is the entrypoint for everything else.
967  */
968 static void
969 connect_to_websocket_server_async (void)
970 {
971   SoupLogger *logger;
972   SoupMessage *message;
973   SoupSession *session;
974   const char *https_aliases[] = { "wss", NULL };
975
976   session =
977       soup_session_new_with_options (SOUP_SESSION_SSL_STRICT, !disable_ssl,
978       SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE,
979       //SOUP_SESSION_SSL_CA_FILE, "/etc/ssl/certs/ca-bundle.crt",
980       SOUP_SESSION_HTTPS_ALIASES, https_aliases, NULL);
981
982   logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
983   soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
984   g_object_unref (logger);
985
986   message = soup_message_new (SOUP_METHOD_GET, server_url);
987
988   gst_print ("Connecting to server...\n");
989
990   /* Once connected, we will register */
991   soup_session_websocket_connect_async (session, message, NULL, NULL, NULL,
992       (GAsyncReadyCallback) on_server_connected, message);
993   app_state = SERVER_CONNECTING;
994 }
995
996 static gboolean
997 check_plugins (void)
998 {
999   int i;
1000   gboolean ret;
1001   GstPlugin *plugin;
1002   GstRegistry *registry;
1003   const gchar *needed[] = { "opus", "vpx", "nice", "webrtc", "dtls", "srtp",
1004     "rtpmanager", "videotestsrc", "audiotestsrc", NULL
1005   };
1006
1007   registry = gst_registry_get ();
1008   ret = TRUE;
1009   for (i = 0; i < g_strv_length ((gchar **) needed); i++) {
1010     plugin = gst_registry_find_plugin (registry, needed[i]);
1011     if (!plugin) {
1012       gst_print ("Required gstreamer plugin '%s' not found\n", needed[i]);
1013       ret = FALSE;
1014       continue;
1015     }
1016     gst_object_unref (plugin);
1017   }
1018   return ret;
1019 }
1020
1021 int
1022 main (int argc, char *argv[])
1023 {
1024   GOptionContext *context;
1025   GError *error = NULL;
1026   int ret_code = -1;
1027
1028   context = g_option_context_new ("- gstreamer webrtc sendrecv demo");
1029   g_option_context_add_main_entries (context, entries, NULL);
1030   g_option_context_add_group (context, gst_init_get_option_group ());
1031   if (!g_option_context_parse (context, &argc, &argv, &error)) {
1032     gst_printerr ("Error initializing: %s\n", error->message);
1033     return -1;
1034   }
1035
1036   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "webrtc-sendrecv", 0,
1037       "WebRTC Sending and Receiving example");
1038
1039   if (!check_plugins ()) {
1040     goto out;
1041   }
1042
1043   if (!peer_id && !our_id) {
1044     gst_printerr ("--peer-id or --our-id is a required argument\n");
1045     goto out;
1046   }
1047
1048   if (peer_id && our_id) {
1049     gst_printerr ("specify only --peer-id or --our-id\n");
1050     goto out;
1051   }
1052
1053   ret_code = 0;
1054
1055   /* Disable ssl when running a localhost server, because
1056    * it's probably a test server with a self-signed certificate */
1057   {
1058     GstUri *uri = gst_uri_from_string (server_url);
1059     if (g_strcmp0 ("localhost", gst_uri_get_host (uri)) == 0 ||
1060         g_strcmp0 ("127.0.0.1", gst_uri_get_host (uri)) == 0)
1061       disable_ssl = TRUE;
1062     gst_uri_unref (uri);
1063   }
1064
1065   loop = g_main_loop_new (NULL, FALSE);
1066
1067   connect_to_websocket_server_async ();
1068
1069   g_main_loop_run (loop);
1070
1071   if (loop)
1072     g_main_loop_unref (loop);
1073
1074   if (pipe1) {
1075     GstBus *bus;
1076
1077     gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_NULL);
1078     gst_print ("Pipeline stopped\n");
1079
1080     bus = gst_pipeline_get_bus (GST_PIPELINE (pipe1));
1081     gst_bus_remove_watch (bus);
1082     gst_object_unref (bus);
1083
1084     gst_object_unref (pipe1);
1085   }
1086
1087 out:
1088   g_free (peer_id);
1089   g_free (our_id);
1090
1091   return ret_code;
1092 }