Move files from gst-examples into the "subprojects/gst-examples/" subdir
[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  * 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
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 #define GST_USE_UNSTABLE_API
14 #include <gst/webrtc/webrtc.h>
15
16 /* For signalling */
17 #include <libsoup/soup.h>
18 #include <json-glib/json-glib.h>
19
20 #include <string.h>
21
22 enum AppState
23 {
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,
35   PEER_CONNECTED,
36   PEER_CALL_NEGOTIATING = 4000,
37   PEER_CALL_STARTED,
38   PEER_CALL_STOPPING,
39   PEER_CALL_STOPPED,
40   PEER_CALL_ERROR,
41 };
42
43 #define GST_CAT_DEFAULT webrtc_sendrecv_debug
44 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
45
46 static GMainLoop *loop;
47 static GstElement *pipe1, *webrtc1 = NULL;
48 static GObject *send_channel, *receive_channel;
49
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;
57
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},
68   {NULL},
69 };
70
71 static gboolean
72 cleanup_and_quit_loop (const gchar * msg, enum AppState state)
73 {
74   if (msg)
75     gst_printerr ("%s\n", msg);
76   if (state > 0)
77     app_state = state;
78
79   if (ws_conn) {
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, "");
84     else
85       g_clear_object (&ws_conn);
86   }
87
88   if (loop) {
89     g_main_loop_quit (loop);
90     g_clear_pointer (&loop, g_main_loop_unref);
91   }
92
93   /* To allow usage as a GSourceFunc */
94   return G_SOURCE_REMOVE;
95 }
96
97 static gchar *
98 get_string_from_json_object (JsonObject * object)
99 {
100   JsonNode *root;
101   JsonGenerator *generator;
102   gchar *text;
103
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);
109
110   /* Release everything */
111   g_object_unref (generator);
112   json_node_free (root);
113   return text;
114 }
115
116 static void
117 handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name,
118     const char *sink_name)
119 {
120   GstPad *qpad;
121   GstElement *q, *conv, *resample, *sink;
122   GstPadLinkReturn ret;
123
124   gst_println ("Trying to handle stream with %s ! %s", convert_name, sink_name);
125
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);
132
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);
144   } else {
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);
150   }
151
152   qpad = gst_element_get_static_pad (q, "sink");
153
154   ret = gst_pad_link (pad, qpad);
155   g_assert_cmphex (ret, ==, GST_PAD_LINK_OK);
156 }
157
158 static void
159 on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad,
160     GstElement * pipe)
161 {
162   GstCaps *caps;
163   const gchar *name;
164
165   if (!gst_pad_has_current_caps (pad)) {
166     gst_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n",
167         GST_PAD_NAME (pad));
168     return;
169   }
170
171   caps = gst_pad_get_current_caps (pad);
172   name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
173
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");
178   } else {
179     gst_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad));
180   }
181 }
182
183 static void
184 on_incoming_stream (GstElement * webrtc, GstPad * pad, GstElement * pipe)
185 {
186   GstElement *decodebin;
187   GstPad *sinkpad;
188
189   if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC)
190     return;
191
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);
197
198   sinkpad = gst_element_get_static_pad (decodebin, "sink");
199   gst_pad_link (pad, sinkpad);
200   gst_object_unref (sinkpad);
201 }
202
203 static void
204 send_ice_candidate_message (GstElement * webrtc G_GNUC_UNUSED, guint mlineindex,
205     gchar * candidate, gpointer user_data G_GNUC_UNUSED)
206 {
207   gchar *text;
208   JsonObject *ice, *msg;
209
210   if (app_state < PEER_CALL_NEGOTIATING) {
211     cleanup_and_quit_loop ("Can't send ICE, not in call", APP_STATE_ERROR);
212     return;
213   }
214
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);
222
223   soup_websocket_connection_send_text (ws_conn, text);
224   g_free (text);
225 }
226
227 static void
228 send_sdp_to_peer (GstWebRTCSessionDescription * desc)
229 {
230   gchar *text;
231   JsonObject *msg, *sdp;
232
233   if (app_state < PEER_CALL_NEGOTIATING) {
234     cleanup_and_quit_loop ("Can't send SDP to peer, not in call",
235         APP_STATE_ERROR);
236     return;
237   }
238
239   text = gst_sdp_message_as_text (desc->sdp);
240   sdp = json_object_new ();
241
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");
248   } else {
249     g_assert_not_reached ();
250   }
251
252   json_object_set_string_member (sdp, "sdp", text);
253   g_free (text);
254
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);
259
260   soup_websocket_connection_send_text (ws_conn, text);
261   g_free (text);
262 }
263
264 /* Offer created by our pipeline, to be sent to the peer */
265 static void
266 on_offer_created (GstPromise * promise, gpointer user_data)
267 {
268   GstWebRTCSessionDescription *offer = NULL;
269   const GstStructure *reply;
270
271   g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
272
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);
278
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);
283
284   /* Send offer to peer */
285   send_sdp_to_peer (offer);
286   gst_webrtc_session_description_free (offer);
287 }
288
289 static void
290 on_negotiation_needed (GstElement * element, gpointer user_data)
291 {
292   gboolean create_offer = GPOINTER_TO_INT (user_data);
293   app_state = PEER_CALL_NEGOTIATING;
294
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);
301   }
302 }
303
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="
307
308 static void
309 data_channel_on_error (GObject * dc, gpointer user_data)
310 {
311   cleanup_and_quit_loop ("Data channel error", 0);
312 }
313
314 static void
315 data_channel_on_open (GObject * dc, gpointer user_data)
316 {
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);
322 }
323
324 static void
325 data_channel_on_close (GObject * dc, gpointer user_data)
326 {
327   cleanup_and_quit_loop ("Data channel closed", 0);
328 }
329
330 static void
331 data_channel_on_message_string (GObject * dc, gchar * str, gpointer user_data)
332 {
333   gst_print ("Received data channel message: %s\n", str);
334 }
335
336 static void
337 connect_data_channel_signals (GObject * data_channel)
338 {
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),
342       NULL);
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);
347 }
348
349 static void
350 on_data_channel (GstElement * webrtc, GObject * data_channel,
351     gpointer user_data)
352 {
353   connect_data_channel_signals (data_channel);
354   receive_channel = data_channel;
355 }
356
357 static void
358 on_ice_gathering_state_notify (GstElement * webrtcbin, GParamSpec * pspec,
359     gpointer user_data)
360 {
361   GstWebRTCICEGatheringState ice_gather_state;
362   const gchar *new_state = "unknown";
363
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:
367       new_state = "new";
368       break;
369     case GST_WEBRTC_ICE_GATHERING_STATE_GATHERING:
370       new_state = "gathering";
371       break;
372     case GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE:
373       new_state = "complete";
374       break;
375   }
376   gst_print ("ICE gathering state changed to %s\n", new_state);
377 }
378
379 static gboolean webrtcbin_get_stats (GstElement * webrtcbin);
380
381 static gboolean
382 on_webrtcbin_stat (GQuark field_id, const GValue * value, gpointer unused)
383 {
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));
387   } else {
388     GST_FIXME ("unknown field \'%s\' value type: \'%s\'",
389         g_quark_to_string (field_id), g_type_name (G_VALUE_TYPE (value)));
390   }
391
392   return TRUE;
393 }
394
395 static void
396 on_webrtcbin_get_stats (GstPromise * promise, GstElement * webrtcbin)
397 {
398   const GstStructure *stats;
399
400   g_return_if_fail (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED);
401
402   stats = gst_promise_get_reply (promise);
403   gst_structure_foreach (stats, on_webrtcbin_stat, NULL);
404
405   g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtcbin);
406 }
407
408 static gboolean
409 webrtcbin_get_stats (GstElement * webrtcbin)
410 {
411   GstPromise *promise;
412
413   promise =
414       gst_promise_new_with_change_func (
415       (GstPromiseChangeFunc) on_webrtcbin_get_stats, webrtcbin, NULL);
416
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);
420
421   return G_SOURCE_REMOVE;
422 }
423
424 #define RTP_TWCC_URI "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
425
426 static gboolean
427 start_pipeline (gboolean create_offer)
428 {
429   GstStateChangeReturn ret;
430   GError *error = NULL;
431
432   pipe1 =
433       gst_parse_launch ("webrtcbin bundle-policy=max-bundle name=sendrecv "
434       STUN_SERVER
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.
439        */
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);
446
447   if (error) {
448     gst_printerr ("Failed to parse launch: %s\n", error->message);
449     g_error_free (error);
450     goto err;
451   }
452
453   webrtc1 = gst_bin_get_by_name (GST_BIN (pipe1), "sendrecv");
454   g_assert_nonnull (webrtc1);
455
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.
459      */
460     GST_FIXME ("Need to implement header extension negotiation when "
461         "reciving a remote offers");
462   } else {
463     GstElement *videopay, *audiopay;
464     GstRTPHeaderExtension *video_twcc, *audio_twcc;
465
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);
474
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);
483   }
484
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);
496
497   gst_element_set_state (pipe1, GST_STATE_READY);
498
499   g_signal_emit_by_name (webrtc1, "create-data-channel", "channel", NULL,
500       &send_channel);
501   if (send_channel) {
502     gst_print ("Created data channel\n");
503     connect_data_channel_signals (send_channel);
504   } else {
505     gst_print ("Could not create data channel, is usrsctp available?\n");
506   }
507
508   g_signal_connect (webrtc1, "on-data-channel", G_CALLBACK (on_data_channel),
509       NULL);
510   /* Incoming streams will be exposed via this signal */
511   g_signal_connect (webrtc1, "pad-added", G_CALLBACK (on_incoming_stream),
512       pipe1);
513   /* Lifetime is the same as the pipeline itself */
514   gst_object_unref (webrtc1);
515
516   g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtc1);
517
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)
521     goto err;
522
523   return TRUE;
524
525 err:
526   if (pipe1)
527     g_clear_object (&pipe1);
528   if (webrtc1)
529     webrtc1 = NULL;
530   return FALSE;
531 }
532
533 static gboolean
534 setup_call (void)
535 {
536   gchar *msg;
537
538   if (soup_websocket_connection_get_state (ws_conn) !=
539       SOUP_WEBSOCKET_STATE_OPEN)
540     return FALSE;
541
542   if (!peer_id)
543     return FALSE;
544
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);
549   g_free (msg);
550   return TRUE;
551 }
552
553 static gboolean
554 register_with_server (void)
555 {
556   gchar *hello;
557
558   if (soup_websocket_connection_get_state (ws_conn) !=
559       SOUP_WEBSOCKET_STATE_OPEN)
560     return FALSE;
561
562   if (!our_id) {
563     gint32 id;
564
565     id = g_random_int_range (10, 10000);
566     gst_print ("Registering id %i with server\n", id);
567
568     hello = g_strdup_printf ("HELLO %i", id);
569   } else {
570     gst_print ("Registering id %s with server\n", our_id);
571
572     hello = g_strdup_printf ("HELLO %s", our_id);
573   }
574
575   app_state = SERVER_REGISTERING;
576
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);
580   g_free (hello);
581
582   return TRUE;
583 }
584
585 static void
586 on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED,
587     gpointer user_data G_GNUC_UNUSED)
588 {
589   app_state = SERVER_CLOSED;
590   cleanup_and_quit_loop ("Server connection closed", 0);
591 }
592
593 /* Answer created by our pipeline, to be sent to the peer */
594 static void
595 on_answer_created (GstPromise * promise, gpointer user_data)
596 {
597   GstWebRTCSessionDescription *answer = NULL;
598   const GstStructure *reply;
599
600   g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
601
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);
607
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);
612
613   /* Send answer to peer */
614   send_sdp_to_peer (answer);
615   gst_webrtc_session_description_free (answer);
616 }
617
618 static void
619 on_offer_set (GstPromise * promise, gpointer user_data)
620 {
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);
624 }
625
626 static void
627 on_offer_received (GstSDPMessage * sdp)
628 {
629   GstWebRTCSessionDescription *offer = NULL;
630   GstPromise *promise;
631
632   offer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_OFFER, sdp);
633   g_assert_nonnull (offer);
634
635   /* Set remote description on our pipeline */
636   {
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);
639   }
640   gst_webrtc_session_description_free (offer);
641 }
642
643 /* One mega message handler for our asynchronous calling mechanism */
644 static void
645 on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type,
646     GBytes * message, gpointer user_data)
647 {
648   gchar *text;
649
650   switch (type) {
651     case SOUP_WEBSOCKET_DATA_BINARY:
652       gst_printerr ("Received unknown binary message, ignoring\n");
653       return;
654     case SOUP_WEBSOCKET_DATA_TEXT:{
655       gsize size;
656       const gchar *data = g_bytes_get_data (message, &size);
657       /* Convert to NULL-terminated string */
658       text = g_strndup (data, size);
659       break;
660     }
661     default:
662       g_assert_not_reached ();
663   }
664
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",
669           APP_STATE_ERROR);
670       goto out;
671     }
672     app_state = SERVER_REGISTERED;
673     gst_print ("Registered with server\n");
674     if (!our_id) {
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);
678         goto out;
679       }
680     } else {
681       gst_println ("Waiting for connection from peer (our-id: %s)", our_id);
682     }
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
685      * negotiation */
686     if (app_state != PEER_CONNECTING) {
687       cleanup_and_quit_loop ("ERROR: Received SESSION_OK when not calling",
688           PEER_CONNECTION_ERROR);
689       goto out;
690     }
691
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",
696           PEER_CALL_ERROR);
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");
700       goto out;
701     }
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",
706           PEER_CALL_ERROR);
707   } else if (g_str_has_prefix (text, "ERROR")) {
708     /* Handle errors */
709     switch (app_state) {
710       case SERVER_CONNECTING:
711         app_state = SERVER_CONNECTION_ERROR;
712         break;
713       case SERVER_REGISTERING:
714         app_state = SERVER_REGISTRATION_ERROR;
715         break;
716       case PEER_CONNECTING:
717         app_state = PEER_CONNECTION_ERROR;
718         break;
719       case PEER_CONNECTED:
720       case PEER_CALL_NEGOTIATING:
721         app_state = PEER_CALL_ERROR;
722         break;
723       default:
724         app_state = APP_STATE_ERROR;
725     }
726     cleanup_and_quit_loop (text, 0);
727   } else {
728     /* Look for JSON messages containing SDP and ICE candidates */
729     JsonNode *root;
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);
735       goto out;
736     }
737
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);
742       goto out;
743     }
744
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",
750             PEER_CALL_ERROR);
751       }
752
753       app_state = PEER_CALL_NEGOTIATING;
754     }
755
756     object = json_node_get_object (root);
757     /* Check type of JSON message */
758     if (json_object_has_member (object, "sdp")) {
759       int ret;
760       GstSDPMessage *sdp;
761       const gchar *text, *sdptype;
762       GstWebRTCSessionDescription *answer;
763
764       g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
765
766       child = json_object_get_object_member (object, "sdp");
767
768       if (!json_object_has_member (child, "type")) {
769         cleanup_and_quit_loop ("ERROR: received SDP without 'type'",
770             PEER_CALL_ERROR);
771         goto out;
772       }
773
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.
778        *
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);
786
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,
790             sdp);
791         g_assert_nonnull (answer);
792
793         /* Set remote description on our pipeline */
794         {
795           GstPromise *promise = gst_promise_new ();
796           g_signal_emit_by_name (webrtc1, "set-remote-description", answer,
797               promise);
798           gst_promise_interrupt (promise);
799           gst_promise_unref (promise);
800         }
801         app_state = PEER_CALL_STARTED;
802       } else {
803         gst_print ("Received offer:\n%s\n", text);
804         on_offer_received (sdp);
805       }
806
807     } else if (json_object_has_member (object, "ice")) {
808       const gchar *candidate;
809       gint sdpmlineindex;
810
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");
814
815       /* Add ice candidate sent by remote peer */
816       g_signal_emit_by_name (webrtc1, "add-ice-candidate", sdpmlineindex,
817           candidate);
818     } else {
819       gst_printerr ("Ignoring unknown JSON message:\n%s\n", text);
820     }
821     g_object_unref (parser);
822   }
823
824 out:
825   g_free (text);
826 }
827
828 static void
829 on_server_connected (SoupSession * session, GAsyncResult * res,
830     SoupMessage * msg)
831 {
832   GError *error = NULL;
833
834   ws_conn = soup_session_websocket_connect_finish (session, res, &error);
835   if (error) {
836     cleanup_and_quit_loop (error->message, SERVER_CONNECTION_ERROR);
837     g_error_free (error);
838     return;
839   }
840
841   g_assert_nonnull (ws_conn);
842
843   app_state = SERVER_CONNECTED;
844   gst_print ("Connected to signalling server\n");
845
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);
848
849   /* Register with the server so it knows about us and can accept commands */
850   register_with_server ();
851 }
852
853 /*
854  * Connect to the signalling server. This is the entrypoint for everything else.
855  */
856 static void
857 connect_to_websocket_server_async (void)
858 {
859   SoupLogger *logger;
860   SoupMessage *message;
861   SoupSession *session;
862   const char *https_aliases[] = { "wss", NULL };
863
864   session =
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);
869
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);
873
874   message = soup_message_new (SOUP_METHOD_GET, server_url);
875
876   gst_print ("Connecting to server...\n");
877
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;
882 }
883
884 static gboolean
885 check_plugins (void)
886 {
887   int i;
888   gboolean ret;
889   GstPlugin *plugin;
890   GstRegistry *registry;
891   const gchar *needed[] = { "opus", "vpx", "nice", "webrtc", "dtls", "srtp",
892     "rtpmanager", "videotestsrc", "audiotestsrc", NULL
893   };
894
895   registry = gst_registry_get ();
896   ret = TRUE;
897   for (i = 0; i < g_strv_length ((gchar **) needed); i++) {
898     plugin = gst_registry_find_plugin (registry, needed[i]);
899     if (!plugin) {
900       gst_print ("Required gstreamer plugin '%s' not found\n", needed[i]);
901       ret = FALSE;
902       continue;
903     }
904     gst_object_unref (plugin);
905   }
906   return ret;
907 }
908
909 int
910 main (int argc, char *argv[])
911 {
912   GOptionContext *context;
913   GError *error = NULL;
914   int ret_code = -1;
915
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);
921     return -1;
922   }
923
924   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "webrtc-sendrecv", 0,
925       "WebRTC Sending and Receiving example");
926
927   if (!check_plugins ()) {
928     goto out;
929   }
930
931   if (!peer_id && !our_id) {
932     gst_printerr ("--peer-id or --our-id is a required argument\n");
933     goto out;
934   }
935
936   if (peer_id && our_id) {
937     gst_printerr ("specify only --peer-id or --our-id\n");
938     goto out;
939   }
940
941   ret_code = 0;
942
943   /* Disable ssl when running a localhost server, because
944    * it's probably a test server with a self-signed certificate */
945   {
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)
949       disable_ssl = TRUE;
950     gst_uri_unref (uri);
951   }
952
953   loop = g_main_loop_new (NULL, FALSE);
954
955   connect_to_websocket_server_async ();
956
957   g_main_loop_run (loop);
958
959   if (loop)
960     g_main_loop_unref (loop);
961
962   if (pipe1) {
963     gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_NULL);
964     gst_print ("Pipeline stopped\n");
965     gst_object_unref (pipe1);
966   }
967
968 out:
969   g_free (peer_id);
970   g_free (our_id);
971
972   return ret_code;
973 }