2017-10-21 14:27:29 +00:00
|
|
|
/*
|
|
|
|
* Demo gstreamer app for negotiating and streaming a sendrecv webrtc stream
|
|
|
|
* with a browser JS app.
|
|
|
|
*
|
2022-03-01 14:20:12 +00:00
|
|
|
* Build by running: `make webrtc-sendrecv`, or build the gstreamer monorepo.
|
2017-10-21 14:27:29 +00:00
|
|
|
*
|
|
|
|
* Author: Nirbheek Chauhan <nirbheek@centricular.com>
|
|
|
|
*/
|
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <gst/sdp/sdp.h>
|
2021-04-30 04:15:01 +00:00
|
|
|
#include <gst/rtp/rtp.h>
|
2018-02-02 08:39:04 +00:00
|
|
|
|
|
|
|
#define GST_USE_UNSTABLE_API
|
2017-10-21 14:27:29 +00:00
|
|
|
#include <gst/webrtc/webrtc.h>
|
|
|
|
|
|
|
|
/* For signalling */
|
|
|
|
#include <libsoup/soup.h>
|
|
|
|
#include <json-glib/json-glib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-04-14 10:49:55 +00:00
|
|
|
enum AppState
|
|
|
|
{
|
2017-10-21 14:27:29 +00:00
|
|
|
APP_STATE_UNKNOWN = 0,
|
2020-04-14 10:49:55 +00:00
|
|
|
APP_STATE_ERROR = 1, /* generic error */
|
2017-10-21 14:27:29 +00:00
|
|
|
SERVER_CONNECTING = 1000,
|
|
|
|
SERVER_CONNECTION_ERROR,
|
2020-04-14 10:49:55 +00:00
|
|
|
SERVER_CONNECTED, /* Ready to register */
|
2017-10-21 14:27:29 +00:00
|
|
|
SERVER_REGISTERING = 2000,
|
|
|
|
SERVER_REGISTRATION_ERROR,
|
2020-04-14 10:49:55 +00:00
|
|
|
SERVER_REGISTERED, /* Ready to call a peer */
|
|
|
|
SERVER_CLOSED, /* server connection closed by us or the server */
|
2017-10-21 14:27:29 +00:00
|
|
|
PEER_CONNECTING = 3000,
|
|
|
|
PEER_CONNECTION_ERROR,
|
|
|
|
PEER_CONNECTED,
|
|
|
|
PEER_CALL_NEGOTIATING = 4000,
|
|
|
|
PEER_CALL_STARTED,
|
|
|
|
PEER_CALL_STOPPING,
|
|
|
|
PEER_CALL_STOPPED,
|
|
|
|
PEER_CALL_ERROR,
|
|
|
|
};
|
|
|
|
|
2021-04-30 04:15:01 +00:00
|
|
|
#define GST_CAT_DEFAULT webrtc_sendrecv_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
static GMainLoop *loop;
|
2020-11-25 17:41:53 +00:00
|
|
|
static GstElement *pipe1, *webrtc1 = NULL;
|
2018-09-21 13:03:43 +00:00
|
|
|
static GObject *send_channel, *receive_channel;
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
static SoupWebsocketConnection *ws_conn = NULL;
|
|
|
|
static enum AppState app_state = 0;
|
2020-11-25 17:41:53 +00:00
|
|
|
static gchar *peer_id = NULL;
|
|
|
|
static gchar *our_id = NULL;
|
2017-10-21 14:27:29 +00:00
|
|
|
static const gchar *server_url = "wss://webrtc.nirbheek.in:8443";
|
2018-06-06 16:51:15 +00:00
|
|
|
static gboolean disable_ssl = FALSE;
|
2020-03-04 16:03:17 +00:00
|
|
|
static gboolean remote_is_offerer = FALSE;
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2020-04-14 10:49:55 +00:00
|
|
|
static GOptionEntry entries[] = {
|
|
|
|
{"peer-id", 0, 0, G_OPTION_ARG_STRING, &peer_id,
|
|
|
|
"String ID of the peer to connect to", "ID"},
|
2020-11-25 17:41:53 +00:00
|
|
|
{"our-id", 0, 0, G_OPTION_ARG_STRING, &our_id,
|
2022-03-05 14:36:37 +00:00
|
|
|
"String ID that the peer can use to connect to us", "ID"},
|
2020-04-14 10:49:55 +00:00
|
|
|
{"server", 0, 0, G_OPTION_ARG_STRING, &server_url,
|
|
|
|
"Signalling server to connect to", "URL"},
|
|
|
|
{"disable-ssl", 0, 0, G_OPTION_ARG_NONE, &disable_ssl, "Disable ssl", NULL},
|
|
|
|
{"remote-offerer", 0, 0, G_OPTION_ARG_NONE, &remote_is_offerer,
|
|
|
|
"Request that the peer generate the offer and we'll answer", NULL},
|
|
|
|
{NULL},
|
2017-10-21 14:27:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
cleanup_and_quit_loop (const gchar * msg, enum AppState state)
|
|
|
|
{
|
|
|
|
if (msg)
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_printerr ("%s\n", msg);
|
2017-10-21 14:27:29 +00:00
|
|
|
if (state > 0)
|
|
|
|
app_state = state;
|
|
|
|
|
|
|
|
if (ws_conn) {
|
|
|
|
if (soup_websocket_connection_get_state (ws_conn) ==
|
|
|
|
SOUP_WEBSOCKET_STATE_OPEN)
|
|
|
|
/* This will call us again */
|
|
|
|
soup_websocket_connection_close (ws_conn, 1000, "");
|
|
|
|
else
|
2020-11-25 17:41:53 +00:00
|
|
|
g_clear_object (&ws_conn);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (loop) {
|
|
|
|
g_main_loop_quit (loop);
|
2020-11-25 17:41:53 +00:00
|
|
|
g_clear_pointer (&loop, g_main_loop_unref);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* To allow usage as a GSourceFunc */
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2020-04-14 10:49:55 +00:00
|
|
|
static gchar *
|
2017-10-21 14:27:29 +00:00
|
|
|
get_string_from_json_object (JsonObject * object)
|
|
|
|
{
|
|
|
|
JsonNode *root;
|
|
|
|
JsonGenerator *generator;
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
/* Make it the root node */
|
|
|
|
root = json_node_init_object (json_node_alloc (), object);
|
|
|
|
generator = json_generator_new ();
|
|
|
|
json_generator_set_root (generator, root);
|
|
|
|
text = json_generator_to_data (generator, NULL);
|
|
|
|
|
|
|
|
/* Release everything */
|
|
|
|
g_object_unref (generator);
|
|
|
|
json_node_free (root);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-14 10:49:55 +00:00
|
|
|
handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name,
|
|
|
|
const char *sink_name)
|
2017-10-21 14:27:29 +00:00
|
|
|
{
|
|
|
|
GstPad *qpad;
|
2018-03-21 00:56:49 +00:00
|
|
|
GstElement *q, *conv, *resample, *sink;
|
2017-10-21 14:27:29 +00:00
|
|
|
GstPadLinkReturn ret;
|
|
|
|
|
2020-11-25 17:41:53 +00:00
|
|
|
gst_println ("Trying to handle stream with %s ! %s", convert_name, sink_name);
|
2018-03-08 14:40:55 +00:00
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
q = gst_element_factory_make ("queue", NULL);
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_nonnull (q);
|
2017-10-21 14:27:29 +00:00
|
|
|
conv = gst_element_factory_make (convert_name, NULL);
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_nonnull (conv);
|
2017-10-21 14:27:29 +00:00
|
|
|
sink = gst_element_factory_make (sink_name, NULL);
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_nonnull (sink);
|
2018-03-21 00:56:49 +00:00
|
|
|
|
|
|
|
if (g_strcmp0 (convert_name, "audioconvert") == 0) {
|
2018-03-31 19:45:16 +00:00
|
|
|
/* Might also need to resample, so add it just in case.
|
|
|
|
* Will be a no-op if it's not required. */
|
2018-03-21 00:56:49 +00:00
|
|
|
resample = gst_element_factory_make ("audioresample", NULL);
|
|
|
|
g_assert_nonnull (resample);
|
|
|
|
gst_bin_add_many (GST_BIN (pipe), q, conv, resample, sink, NULL);
|
|
|
|
gst_element_sync_state_with_parent (q);
|
|
|
|
gst_element_sync_state_with_parent (conv);
|
|
|
|
gst_element_sync_state_with_parent (resample);
|
|
|
|
gst_element_sync_state_with_parent (sink);
|
|
|
|
gst_element_link_many (q, conv, resample, sink, NULL);
|
|
|
|
} else {
|
|
|
|
gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL);
|
|
|
|
gst_element_sync_state_with_parent (q);
|
|
|
|
gst_element_sync_state_with_parent (conv);
|
|
|
|
gst_element_sync_state_with_parent (sink);
|
|
|
|
gst_element_link_many (q, conv, sink, NULL);
|
|
|
|
}
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
qpad = gst_element_get_static_pad (q, "sink");
|
|
|
|
|
|
|
|
ret = gst_pad_link (pad, qpad);
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_cmphex (ret, ==, GST_PAD_LINK_OK);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad,
|
|
|
|
GstElement * pipe)
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
const gchar *name;
|
|
|
|
|
|
|
|
if (!gst_pad_has_current_caps (pad)) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n",
|
2017-10-21 14:27:29 +00:00
|
|
|
GST_PAD_NAME (pad));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
caps = gst_pad_get_current_caps (pad);
|
|
|
|
name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
|
|
|
|
|
|
|
|
if (g_str_has_prefix (name, "video")) {
|
|
|
|
handle_media_stream (pad, pipe, "videoconvert", "autovideosink");
|
|
|
|
} else if (g_str_has_prefix (name, "audio")) {
|
|
|
|
handle_media_stream (pad, pipe, "audioconvert", "autoaudiosink");
|
|
|
|
} else {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad));
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_incoming_stream (GstElement * webrtc, GstPad * pad, GstElement * pipe)
|
|
|
|
{
|
|
|
|
GstElement *decodebin;
|
2019-05-09 16:39:28 +00:00
|
|
|
GstPad *sinkpad;
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
decodebin = gst_element_factory_make ("decodebin", NULL);
|
|
|
|
g_signal_connect (decodebin, "pad-added",
|
|
|
|
G_CALLBACK (on_incoming_decodebin_stream), pipe);
|
|
|
|
gst_bin_add (GST_BIN (pipe), decodebin);
|
|
|
|
gst_element_sync_state_with_parent (decodebin);
|
2019-05-09 16:39:28 +00:00
|
|
|
|
|
|
|
sinkpad = gst_element_get_static_pad (decodebin, "sink");
|
|
|
|
gst_pad_link (pad, sinkpad);
|
2019-07-01 10:01:31 +00:00
|
|
|
gst_object_unref (sinkpad);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_ice_candidate_message (GstElement * webrtc G_GNUC_UNUSED, guint mlineindex,
|
|
|
|
gchar * candidate, gpointer user_data G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
gchar *text;
|
|
|
|
JsonObject *ice, *msg;
|
|
|
|
|
|
|
|
if (app_state < PEER_CALL_NEGOTIATING) {
|
|
|
|
cleanup_and_quit_loop ("Can't send ICE, not in call", APP_STATE_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ice = json_object_new ();
|
|
|
|
json_object_set_string_member (ice, "candidate", candidate);
|
|
|
|
json_object_set_int_member (ice, "sdpMLineIndex", mlineindex);
|
|
|
|
msg = json_object_new ();
|
|
|
|
json_object_set_object_member (msg, "ice", ice);
|
|
|
|
text = get_string_from_json_object (msg);
|
|
|
|
json_object_unref (msg);
|
|
|
|
|
|
|
|
soup_websocket_connection_send_text (ws_conn, text);
|
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-14 10:49:55 +00:00
|
|
|
send_sdp_to_peer (GstWebRTCSessionDescription * desc)
|
2017-10-21 14:27:29 +00:00
|
|
|
{
|
|
|
|
gchar *text;
|
|
|
|
JsonObject *msg, *sdp;
|
|
|
|
|
|
|
|
if (app_state < PEER_CALL_NEGOTIATING) {
|
2020-04-14 10:49:55 +00:00
|
|
|
cleanup_and_quit_loop ("Can't send SDP to peer, not in call",
|
|
|
|
APP_STATE_ERROR);
|
2017-10-21 14:27:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
text = gst_sdp_message_as_text (desc->sdp);
|
2017-10-21 14:27:29 +00:00
|
|
|
sdp = json_object_new ();
|
2020-03-04 16:03:17 +00:00
|
|
|
|
|
|
|
if (desc->type == GST_WEBRTC_SDP_TYPE_OFFER) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Sending offer:\n%s\n", text);
|
2020-03-04 16:03:17 +00:00
|
|
|
json_object_set_string_member (sdp, "type", "offer");
|
2020-04-14 10:49:55 +00:00
|
|
|
} else if (desc->type == GST_WEBRTC_SDP_TYPE_ANSWER) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Sending answer:\n%s\n", text);
|
2020-03-04 16:03:17 +00:00
|
|
|
json_object_set_string_member (sdp, "type", "answer");
|
2020-04-14 10:49:55 +00:00
|
|
|
} else {
|
2020-03-04 16:03:17 +00:00
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
json_object_set_string_member (sdp, "sdp", text);
|
|
|
|
g_free (text);
|
|
|
|
|
|
|
|
msg = json_object_new ();
|
|
|
|
json_object_set_object_member (msg, "sdp", sdp);
|
|
|
|
text = get_string_from_json_object (msg);
|
|
|
|
json_object_unref (msg);
|
|
|
|
|
|
|
|
soup_websocket_connection_send_text (ws_conn, text);
|
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Offer created by our pipeline, to be sent to the peer */
|
|
|
|
static void
|
2017-10-30 03:42:06 +00:00
|
|
|
on_offer_created (GstPromise * promise, gpointer user_data)
|
2017-10-21 14:27:29 +00:00
|
|
|
{
|
|
|
|
GstWebRTCSessionDescription *offer = NULL;
|
2017-11-22 12:15:48 +00:00
|
|
|
const GstStructure *reply;
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2020-04-14 10:49:55 +00:00
|
|
|
g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED);
|
2017-11-22 12:15:48 +00:00
|
|
|
reply = gst_promise_get_reply (promise);
|
|
|
|
gst_structure_get (reply, "offer",
|
2017-10-21 14:27:29 +00:00
|
|
|
GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
|
|
|
|
gst_promise_unref (promise);
|
|
|
|
|
|
|
|
promise = gst_promise_new ();
|
|
|
|
g_signal_emit_by_name (webrtc1, "set-local-description", offer, promise);
|
|
|
|
gst_promise_interrupt (promise);
|
|
|
|
gst_promise_unref (promise);
|
|
|
|
|
|
|
|
/* Send offer to peer */
|
2020-03-04 16:03:17 +00:00
|
|
|
send_sdp_to_peer (offer);
|
2017-10-21 14:27:29 +00:00
|
|
|
gst_webrtc_session_description_free (offer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_negotiation_needed (GstElement * element, gpointer user_data)
|
|
|
|
{
|
2021-02-09 09:58:57 +00:00
|
|
|
gboolean create_offer = GPOINTER_TO_INT (user_data);
|
2017-10-21 14:27:29 +00:00
|
|
|
app_state = PEER_CALL_NEGOTIATING;
|
2020-03-04 16:03:17 +00:00
|
|
|
|
2021-02-09 09:58:57 +00:00
|
|
|
if (remote_is_offerer) {
|
2021-02-09 08:57:03 +00:00
|
|
|
soup_websocket_connection_send_text (ws_conn, "OFFER_REQUEST");
|
2021-02-09 09:58:57 +00:00
|
|
|
} else if (create_offer) {
|
2021-02-09 09:58:35 +00:00
|
|
|
GstPromise *promise =
|
|
|
|
gst_promise_new_with_change_func (on_offer_created, NULL, NULL);
|
2020-03-04 16:03:17 +00:00
|
|
|
g_signal_emit_by_name (webrtc1, "create-offer", NULL, promise);
|
|
|
|
}
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 16:10:09 +00:00
|
|
|
#define STUN_SERVER " stun-server=stun://stun.l.google.com:19302 "
|
2017-10-21 14:27:29 +00:00
|
|
|
#define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload="
|
|
|
|
#define RTP_CAPS_VP8 "application/x-rtp,media=video,encoding-name=VP8,payload="
|
|
|
|
|
2018-09-21 13:03:43 +00:00
|
|
|
static void
|
|
|
|
data_channel_on_error (GObject * dc, gpointer user_data)
|
|
|
|
{
|
|
|
|
cleanup_and_quit_loop ("Data channel error", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
data_channel_on_open (GObject * dc, gpointer user_data)
|
|
|
|
{
|
2020-04-14 10:49:55 +00:00
|
|
|
GBytes *bytes = g_bytes_new ("data", strlen ("data"));
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("data channel opened\n");
|
2018-09-21 13:03:43 +00:00
|
|
|
g_signal_emit_by_name (dc, "send-string", "Hi! from GStreamer");
|
|
|
|
g_signal_emit_by_name (dc, "send-data", bytes);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
data_channel_on_close (GObject * dc, gpointer user_data)
|
|
|
|
{
|
|
|
|
cleanup_and_quit_loop ("Data channel closed", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-14 10:49:55 +00:00
|
|
|
data_channel_on_message_string (GObject * dc, gchar * str, gpointer user_data)
|
2018-09-21 13:03:43 +00:00
|
|
|
{
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Received data channel message: %s\n", str);
|
2018-09-21 13:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connect_data_channel_signals (GObject * data_channel)
|
|
|
|
{
|
2020-04-14 10:49:55 +00:00
|
|
|
g_signal_connect (data_channel, "on-error",
|
|
|
|
G_CALLBACK (data_channel_on_error), NULL);
|
2018-09-21 13:03:43 +00:00
|
|
|
g_signal_connect (data_channel, "on-open", G_CALLBACK (data_channel_on_open),
|
|
|
|
NULL);
|
2020-04-14 10:49:55 +00:00
|
|
|
g_signal_connect (data_channel, "on-close",
|
|
|
|
G_CALLBACK (data_channel_on_close), NULL);
|
|
|
|
g_signal_connect (data_channel, "on-message-string",
|
|
|
|
G_CALLBACK (data_channel_on_message_string), NULL);
|
2018-09-21 13:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-14 10:49:55 +00:00
|
|
|
on_data_channel (GstElement * webrtc, GObject * data_channel,
|
|
|
|
gpointer user_data)
|
2018-09-21 13:03:43 +00:00
|
|
|
{
|
|
|
|
connect_data_channel_signals (data_channel);
|
|
|
|
receive_channel = data_channel;
|
|
|
|
}
|
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
static void
|
|
|
|
on_ice_gathering_state_notify (GstElement * webrtcbin, GParamSpec * pspec,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GstWebRTCICEGatheringState ice_gather_state;
|
|
|
|
const gchar *new_state = "unknown";
|
|
|
|
|
2020-04-14 10:49:55 +00:00
|
|
|
g_object_get (webrtcbin, "ice-gathering-state", &ice_gather_state, NULL);
|
2020-03-04 16:03:17 +00:00
|
|
|
switch (ice_gather_state) {
|
|
|
|
case GST_WEBRTC_ICE_GATHERING_STATE_NEW:
|
|
|
|
new_state = "new";
|
|
|
|
break;
|
|
|
|
case GST_WEBRTC_ICE_GATHERING_STATE_GATHERING:
|
|
|
|
new_state = "gathering";
|
|
|
|
break;
|
|
|
|
case GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE:
|
|
|
|
new_state = "complete";
|
|
|
|
break;
|
|
|
|
}
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("ICE gathering state changed to %s\n", new_state);
|
2020-03-04 16:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 04:15:01 +00:00
|
|
|
static gboolean webrtcbin_get_stats (GstElement * webrtcbin);
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_webrtcbin_stat (GQuark field_id, const GValue * value, gpointer unused)
|
|
|
|
{
|
|
|
|
if (GST_VALUE_HOLDS_STRUCTURE (value)) {
|
|
|
|
GST_DEBUG ("stat: \'%s\': %" GST_PTR_FORMAT, g_quark_to_string (field_id),
|
|
|
|
gst_value_get_structure (value));
|
|
|
|
} else {
|
|
|
|
GST_FIXME ("unknown field \'%s\' value type: \'%s\'",
|
|
|
|
g_quark_to_string (field_id), g_type_name (G_VALUE_TYPE (value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_webrtcbin_get_stats (GstPromise * promise, GstElement * webrtcbin)
|
|
|
|
{
|
|
|
|
const GstStructure *stats;
|
|
|
|
|
|
|
|
g_return_if_fail (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED);
|
|
|
|
|
|
|
|
stats = gst_promise_get_reply (promise);
|
|
|
|
gst_structure_foreach (stats, on_webrtcbin_stat, NULL);
|
|
|
|
|
|
|
|
g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtcbin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
webrtcbin_get_stats (GstElement * webrtcbin)
|
|
|
|
{
|
|
|
|
GstPromise *promise;
|
|
|
|
|
|
|
|
promise =
|
|
|
|
gst_promise_new_with_change_func (
|
|
|
|
(GstPromiseChangeFunc) on_webrtcbin_get_stats, webrtcbin, NULL);
|
|
|
|
|
|
|
|
GST_TRACE ("emitting get-stats on %" GST_PTR_FORMAT, webrtcbin);
|
|
|
|
g_signal_emit_by_name (webrtcbin, "get-stats", NULL, promise);
|
|
|
|
gst_promise_unref (promise);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RTP_TWCC_URI "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
static gboolean
|
2021-02-09 09:58:57 +00:00
|
|
|
start_pipeline (gboolean create_offer)
|
2017-10-21 14:27:29 +00:00
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
pipe1 =
|
2020-04-14 10:49:55 +00:00
|
|
|
gst_parse_launch ("webrtcbin bundle-policy=max-bundle name=sendrecv "
|
|
|
|
STUN_SERVER
|
2021-04-30 04:15:01 +00:00
|
|
|
"videotestsrc is-live=true pattern=ball ! videoconvert ! queue ! "
|
|
|
|
/* increase the default keyframe distance, browsers have really long
|
|
|
|
* periods between keyframes and rely on PLI events on packet loss to
|
|
|
|
* fix corrupted video.
|
|
|
|
*/
|
|
|
|
"vp8enc deadline=1 keyframe-max-dist=2000 ! "
|
2022-03-15 08:30:48 +00:00
|
|
|
/* picture-id-mode=15-bit seems to make TWCC stats behave better, and
|
|
|
|
* fixes stuttery video playback in Chrome */
|
2021-04-30 04:15:01 +00:00
|
|
|
"rtpvp8pay name=videopay picture-id-mode=15-bit ! "
|
2017-10-21 14:27:29 +00:00
|
|
|
"queue ! " RTP_CAPS_VP8 "96 ! sendrecv. "
|
2021-04-30 04:15:01 +00:00
|
|
|
"audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay name=audiopay ! "
|
2020-04-14 10:49:55 +00:00
|
|
|
"queue ! " RTP_CAPS_OPUS "97 ! sendrecv. ", &error);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
if (error) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_printerr ("Failed to parse launch: %s\n", error->message);
|
2017-10-21 14:27:29 +00:00
|
|
|
g_error_free (error);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
webrtc1 = gst_bin_get_by_name (GST_BIN (pipe1), "sendrecv");
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_nonnull (webrtc1);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2021-04-30 04:15:01 +00:00
|
|
|
if (remote_is_offerer) {
|
|
|
|
/* XXX: this will fail when the remote offers twcc as the extension id
|
|
|
|
* cannot currently be negotiated when receiving an offer.
|
|
|
|
*/
|
|
|
|
GST_FIXME ("Need to implement header extension negotiation when "
|
|
|
|
"reciving a remote offers");
|
|
|
|
} else {
|
|
|
|
GstElement *videopay, *audiopay;
|
|
|
|
GstRTPHeaderExtension *video_twcc, *audio_twcc;
|
|
|
|
|
|
|
|
videopay = gst_bin_get_by_name (GST_BIN (pipe1), "videopay");
|
|
|
|
g_assert_nonnull (videopay);
|
|
|
|
video_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI);
|
|
|
|
g_assert_nonnull (video_twcc);
|
|
|
|
gst_rtp_header_extension_set_id (video_twcc, 1);
|
|
|
|
g_signal_emit_by_name (videopay, "add-extension", video_twcc);
|
|
|
|
g_clear_object (&video_twcc);
|
|
|
|
g_clear_object (&videopay);
|
|
|
|
|
|
|
|
audiopay = gst_bin_get_by_name (GST_BIN (pipe1), "audiopay");
|
|
|
|
g_assert_nonnull (audiopay);
|
|
|
|
audio_twcc = gst_rtp_header_extension_create_from_uri (RTP_TWCC_URI);
|
|
|
|
g_assert_nonnull (audio_twcc);
|
|
|
|
gst_rtp_header_extension_set_id (audio_twcc, 1);
|
|
|
|
g_signal_emit_by_name (audiopay, "add-extension", audio_twcc);
|
|
|
|
g_clear_object (&audio_twcc);
|
|
|
|
g_clear_object (&audiopay);
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
/* This is the gstwebrtc entry point where we create the offer and so on. It
|
|
|
|
* will be called when the pipeline goes to PLAYING. */
|
|
|
|
g_signal_connect (webrtc1, "on-negotiation-needed",
|
2021-02-09 09:58:57 +00:00
|
|
|
G_CALLBACK (on_negotiation_needed), GINT_TO_POINTER (create_offer));
|
2017-10-21 14:27:29 +00:00
|
|
|
/* We need to transmit this ICE candidate to the browser via the websockets
|
|
|
|
* signalling server. Incoming ice candidates from the browser need to be
|
|
|
|
* added by us too, see on_server_message() */
|
|
|
|
g_signal_connect (webrtc1, "on-ice-candidate",
|
|
|
|
G_CALLBACK (send_ice_candidate_message), NULL);
|
2020-03-04 16:03:17 +00:00
|
|
|
g_signal_connect (webrtc1, "notify::ice-gathering-state",
|
|
|
|
G_CALLBACK (on_ice_gathering_state_notify), NULL);
|
2018-11-06 04:41:28 +00:00
|
|
|
|
|
|
|
gst_element_set_state (pipe1, GST_STATE_READY);
|
|
|
|
|
2018-09-21 13:03:43 +00:00
|
|
|
g_signal_emit_by_name (webrtc1, "create-data-channel", "channel", NULL,
|
|
|
|
&send_channel);
|
|
|
|
if (send_channel) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Created data channel\n");
|
2018-09-21 13:03:43 +00:00
|
|
|
connect_data_channel_signals (send_channel);
|
|
|
|
} else {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Could not create data channel, is usrsctp available?\n");
|
2018-09-21 13:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_connect (webrtc1, "on-data-channel", G_CALLBACK (on_data_channel),
|
|
|
|
NULL);
|
2017-10-21 14:27:29 +00:00
|
|
|
/* Incoming streams will be exposed via this signal */
|
|
|
|
g_signal_connect (webrtc1, "pad-added", G_CALLBACK (on_incoming_stream),
|
|
|
|
pipe1);
|
|
|
|
/* Lifetime is the same as the pipeline itself */
|
|
|
|
gst_object_unref (webrtc1);
|
|
|
|
|
2021-04-30 04:15:01 +00:00
|
|
|
g_timeout_add (100, (GSourceFunc) webrtcbin_get_stats, webrtc1);
|
|
|
|
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Starting pipeline\n");
|
2017-10-21 14:27:29 +00:00
|
|
|
ret = gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_PLAYING);
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (pipe1)
|
|
|
|
g_clear_object (&pipe1);
|
|
|
|
if (webrtc1)
|
|
|
|
webrtc1 = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
setup_call (void)
|
|
|
|
{
|
|
|
|
gchar *msg;
|
|
|
|
|
|
|
|
if (soup_websocket_connection_get_state (ws_conn) !=
|
|
|
|
SOUP_WEBSOCKET_STATE_OPEN)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!peer_id)
|
|
|
|
return FALSE;
|
|
|
|
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Setting up signalling server call with %s\n", peer_id);
|
2017-10-21 14:27:29 +00:00
|
|
|
app_state = PEER_CONNECTING;
|
|
|
|
msg = g_strdup_printf ("SESSION %s", peer_id);
|
|
|
|
soup_websocket_connection_send_text (ws_conn, msg);
|
|
|
|
g_free (msg);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
register_with_server (void)
|
|
|
|
{
|
|
|
|
gchar *hello;
|
|
|
|
|
|
|
|
if (soup_websocket_connection_get_state (ws_conn) !=
|
|
|
|
SOUP_WEBSOCKET_STATE_OPEN)
|
|
|
|
return FALSE;
|
|
|
|
|
2020-11-25 17:41:53 +00:00
|
|
|
if (!our_id) {
|
|
|
|
gint32 id;
|
|
|
|
|
|
|
|
id = g_random_int_range (10, 10000);
|
|
|
|
gst_print ("Registering id %i with server\n", id);
|
|
|
|
|
|
|
|
hello = g_strdup_printf ("HELLO %i", id);
|
|
|
|
} else {
|
|
|
|
gst_print ("Registering id %s with server\n", our_id);
|
|
|
|
|
|
|
|
hello = g_strdup_printf ("HELLO %s", our_id);
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
app_state = SERVER_REGISTERING;
|
|
|
|
|
|
|
|
/* Register with the server with a random integer id. Reply will be received
|
|
|
|
* by on_server_message() */
|
|
|
|
soup_websocket_connection_send_text (ws_conn, hello);
|
|
|
|
g_free (hello);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED,
|
|
|
|
gpointer user_data G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
app_state = SERVER_CLOSED;
|
|
|
|
cleanup_and_quit_loop ("Server connection closed", 0);
|
|
|
|
}
|
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
/* Answer created by our pipeline, to be sent to the peer */
|
|
|
|
static void
|
|
|
|
on_answer_created (GstPromise * promise, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstWebRTCSessionDescription *answer = NULL;
|
|
|
|
const GstStructure *reply;
|
|
|
|
|
|
|
|
g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
|
|
|
|
|
2020-04-14 10:49:55 +00:00
|
|
|
g_assert_cmphex (gst_promise_wait (promise), ==, GST_PROMISE_RESULT_REPLIED);
|
2020-03-04 16:03:17 +00:00
|
|
|
reply = gst_promise_get_reply (promise);
|
|
|
|
gst_structure_get (reply, "answer",
|
|
|
|
GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL);
|
|
|
|
gst_promise_unref (promise);
|
|
|
|
|
|
|
|
promise = gst_promise_new ();
|
|
|
|
g_signal_emit_by_name (webrtc1, "set-local-description", answer, promise);
|
|
|
|
gst_promise_interrupt (promise);
|
|
|
|
gst_promise_unref (promise);
|
|
|
|
|
|
|
|
/* Send answer to peer */
|
|
|
|
send_sdp_to_peer (answer);
|
|
|
|
gst_webrtc_session_description_free (answer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-01 08:52:33 +00:00
|
|
|
on_offer_set (GstPromise * promise, gpointer user_data)
|
|
|
|
{
|
|
|
|
gst_promise_unref (promise);
|
|
|
|
promise = gst_promise_new_with_change_func (on_answer_created, NULL, NULL);
|
|
|
|
g_signal_emit_by_name (webrtc1, "create-answer", NULL, promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-06-19 02:30:23 +00:00
|
|
|
on_offer_received (GstSDPMessage * sdp)
|
2020-03-04 16:03:17 +00:00
|
|
|
{
|
|
|
|
GstWebRTCSessionDescription *offer = NULL;
|
|
|
|
GstPromise *promise;
|
|
|
|
|
|
|
|
offer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_OFFER, sdp);
|
|
|
|
g_assert_nonnull (offer);
|
|
|
|
|
|
|
|
/* Set remote description on our pipeline */
|
|
|
|
{
|
2020-05-01 08:52:33 +00:00
|
|
|
promise = gst_promise_new_with_change_func (on_offer_set, NULL, NULL);
|
2020-06-19 02:30:23 +00:00
|
|
|
g_signal_emit_by_name (webrtc1, "set-remote-description", offer, promise);
|
2020-03-04 16:03:17 +00:00
|
|
|
}
|
|
|
|
gst_webrtc_session_description_free (offer);
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
/* One mega message handler for our asynchronous calling mechanism */
|
|
|
|
static void
|
|
|
|
on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type,
|
|
|
|
GBytes * message, gpointer user_data)
|
|
|
|
{
|
2018-09-20 08:48:06 +00:00
|
|
|
gchar *text;
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case SOUP_WEBSOCKET_DATA_BINARY:
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_printerr ("Received unknown binary message, ignoring\n");
|
2017-10-21 14:27:29 +00:00
|
|
|
return;
|
2020-04-14 10:49:55 +00:00
|
|
|
case SOUP_WEBSOCKET_DATA_TEXT:{
|
2018-09-20 08:48:06 +00:00
|
|
|
gsize size;
|
|
|
|
const gchar *data = g_bytes_get_data (message, &size);
|
2017-10-21 14:27:29 +00:00
|
|
|
/* Convert to NULL-terminated string */
|
|
|
|
text = g_strndup (data, size);
|
|
|
|
break;
|
2018-09-20 08:48:06 +00:00
|
|
|
}
|
2017-10-21 14:27:29 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_strcmp0 (text, "HELLO") == 0) {
|
2021-02-09 09:58:35 +00:00
|
|
|
/* Server has accepted our registration, we are ready to send commands */
|
2017-10-21 14:27:29 +00:00
|
|
|
if (app_state != SERVER_REGISTERING) {
|
|
|
|
cleanup_and_quit_loop ("ERROR: Received HELLO when not registering",
|
|
|
|
APP_STATE_ERROR);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
app_state = SERVER_REGISTERED;
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Registered with server\n");
|
2020-11-25 17:41:53 +00:00
|
|
|
if (!our_id) {
|
|
|
|
/* Ask signalling server to connect us with a specific peer */
|
|
|
|
if (!setup_call ()) {
|
|
|
|
cleanup_and_quit_loop ("ERROR: Failed to setup call", PEER_CALL_ERROR);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gst_println ("Waiting for connection from peer (our-id: %s)", our_id);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
} else if (g_strcmp0 (text, "SESSION_OK") == 0) {
|
2021-02-09 09:58:35 +00:00
|
|
|
/* The call initiated by us has been setup by the server; now we can start
|
|
|
|
* negotiation */
|
2017-10-21 14:27:29 +00:00
|
|
|
if (app_state != PEER_CONNECTING) {
|
|
|
|
cleanup_and_quit_loop ("ERROR: Received SESSION_OK when not calling",
|
|
|
|
PEER_CONNECTION_ERROR);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
app_state = PEER_CONNECTED;
|
|
|
|
/* Start negotiation (exchange SDP and ICE candidates) */
|
2021-02-09 09:58:57 +00:00
|
|
|
if (!start_pipeline (TRUE))
|
|
|
|
cleanup_and_quit_loop ("ERROR: failed to start pipeline",
|
|
|
|
PEER_CALL_ERROR);
|
|
|
|
} else if (g_strcmp0 (text, "OFFER_REQUEST") == 0) {
|
|
|
|
if (app_state != SERVER_REGISTERED) {
|
|
|
|
gst_printerr ("Received OFFER_REQUEST at a strange time, ignoring\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
gst_print ("Received OFFER_REQUEST, sending offer\n");
|
|
|
|
/* Peer wants us to start negotiation (exchange SDP and ICE candidates) */
|
|
|
|
if (!start_pipeline (TRUE))
|
2017-10-21 14:27:29 +00:00
|
|
|
cleanup_and_quit_loop ("ERROR: failed to start pipeline",
|
|
|
|
PEER_CALL_ERROR);
|
|
|
|
} else if (g_str_has_prefix (text, "ERROR")) {
|
2021-02-09 09:58:35 +00:00
|
|
|
/* Handle errors */
|
2017-10-21 14:27:29 +00:00
|
|
|
switch (app_state) {
|
|
|
|
case SERVER_CONNECTING:
|
|
|
|
app_state = SERVER_CONNECTION_ERROR;
|
|
|
|
break;
|
|
|
|
case SERVER_REGISTERING:
|
|
|
|
app_state = SERVER_REGISTRATION_ERROR;
|
|
|
|
break;
|
|
|
|
case PEER_CONNECTING:
|
|
|
|
app_state = PEER_CONNECTION_ERROR;
|
|
|
|
break;
|
|
|
|
case PEER_CONNECTED:
|
|
|
|
case PEER_CALL_NEGOTIATING:
|
|
|
|
app_state = PEER_CALL_ERROR;
|
2020-04-14 17:13:56 +00:00
|
|
|
break;
|
2017-10-21 14:27:29 +00:00
|
|
|
default:
|
|
|
|
app_state = APP_STATE_ERROR;
|
|
|
|
}
|
|
|
|
cleanup_and_quit_loop (text, 0);
|
|
|
|
} else {
|
2021-02-09 09:58:35 +00:00
|
|
|
/* Look for JSON messages containing SDP and ICE candidates */
|
2017-10-21 14:27:29 +00:00
|
|
|
JsonNode *root;
|
2018-03-23 06:06:40 +00:00
|
|
|
JsonObject *object, *child;
|
2017-10-21 14:27:29 +00:00
|
|
|
JsonParser *parser = json_parser_new ();
|
|
|
|
if (!json_parser_load_from_data (parser, text, -1, NULL)) {
|
2021-02-09 09:58:35 +00:00
|
|
|
gst_printerr ("Unknown message '%s', ignoring\n", text);
|
2017-10-21 14:27:29 +00:00
|
|
|
g_object_unref (parser);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
root = json_parser_get_root (parser);
|
|
|
|
if (!JSON_NODE_HOLDS_OBJECT (root)) {
|
2021-02-09 09:58:35 +00:00
|
|
|
gst_printerr ("Unknown json message '%s', ignoring\n", text);
|
2017-10-21 14:27:29 +00:00
|
|
|
g_object_unref (parser);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-11-25 17:41:53 +00:00
|
|
|
/* If peer connection wasn't made yet and we are expecting peer will
|
|
|
|
* connect to us, launch pipeline at this moment */
|
|
|
|
if (!webrtc1 && our_id) {
|
2021-02-09 09:58:57 +00:00
|
|
|
if (!start_pipeline (FALSE)) {
|
2020-11-25 17:41:53 +00:00
|
|
|
cleanup_and_quit_loop ("ERROR: failed to start pipeline",
|
|
|
|
PEER_CALL_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
app_state = PEER_CALL_NEGOTIATING;
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
object = json_node_get_object (root);
|
|
|
|
/* Check type of JSON message */
|
|
|
|
if (json_object_has_member (object, "sdp")) {
|
|
|
|
int ret;
|
|
|
|
GstSDPMessage *sdp;
|
2018-03-23 06:06:40 +00:00
|
|
|
const gchar *text, *sdptype;
|
2017-10-21 14:27:29 +00:00
|
|
|
GstWebRTCSessionDescription *answer;
|
|
|
|
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_cmphex (app_state, ==, PEER_CALL_NEGOTIATING);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2018-03-23 06:06:40 +00:00
|
|
|
child = json_object_get_object_member (object, "sdp");
|
|
|
|
|
|
|
|
if (!json_object_has_member (child, "type")) {
|
|
|
|
cleanup_and_quit_loop ("ERROR: received SDP without 'type'",
|
|
|
|
PEER_CALL_ERROR);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdptype = json_object_get_string_member (child, "type");
|
2020-03-04 16:03:17 +00:00
|
|
|
/* In this example, we create the offer and receive one answer by default,
|
|
|
|
* but it's possible to comment out the offer creation and wait for an offer
|
|
|
|
* instead, so we handle either here.
|
|
|
|
*
|
|
|
|
* See tests/examples/webrtcbidirectional.c in gst-plugins-bad for another
|
|
|
|
* example how to handle offers from peers and reply with answers using webrtcbin. */
|
2018-03-23 06:06:40 +00:00
|
|
|
text = json_object_get_string_member (child, "sdp");
|
2017-10-21 14:27:29 +00:00
|
|
|
ret = gst_sdp_message_new (&sdp);
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_cmphex (ret, ==, GST_SDP_OK);
|
2018-03-23 06:35:09 +00:00
|
|
|
ret = gst_sdp_message_parse_buffer ((guint8 *) text, strlen (text), sdp);
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_cmphex (ret, ==, GST_SDP_OK);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
if (g_str_equal (sdptype, "answer")) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Received answer:\n%s\n", text);
|
2020-03-04 16:03:17 +00:00
|
|
|
answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER,
|
|
|
|
sdp);
|
|
|
|
g_assert_nonnull (answer);
|
2020-04-14 10:49:55 +00:00
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
/* Set remote description on our pipeline */
|
|
|
|
{
|
|
|
|
GstPromise *promise = gst_promise_new ();
|
|
|
|
g_signal_emit_by_name (webrtc1, "set-remote-description", answer,
|
|
|
|
promise);
|
|
|
|
gst_promise_interrupt (promise);
|
|
|
|
gst_promise_unref (promise);
|
|
|
|
}
|
|
|
|
app_state = PEER_CALL_STARTED;
|
2020-04-14 10:49:55 +00:00
|
|
|
} else {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Received offer:\n%s\n", text);
|
2020-03-04 16:03:17 +00:00
|
|
|
on_offer_received (sdp);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (json_object_has_member (object, "ice")) {
|
|
|
|
const gchar *candidate;
|
|
|
|
gint sdpmlineindex;
|
|
|
|
|
2018-03-23 06:06:40 +00:00
|
|
|
child = json_object_get_object_member (object, "ice");
|
|
|
|
candidate = json_object_get_string_member (child, "candidate");
|
|
|
|
sdpmlineindex = json_object_get_int_member (child, "sdpMLineIndex");
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
/* Add ice candidate sent by remote peer */
|
|
|
|
g_signal_emit_by_name (webrtc1, "add-ice-candidate", sdpmlineindex,
|
|
|
|
candidate);
|
|
|
|
} else {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_printerr ("Ignoring unknown JSON message:\n%s\n", text);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
g_object_unref (parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_free (text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_server_connected (SoupSession * session, GAsyncResult * res,
|
2020-04-14 10:49:55 +00:00
|
|
|
SoupMessage * msg)
|
2017-10-21 14:27:29 +00:00
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
ws_conn = soup_session_websocket_connect_finish (session, res, &error);
|
|
|
|
if (error) {
|
|
|
|
cleanup_and_quit_loop (error->message, SERVER_CONNECTION_ERROR);
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-08 14:40:55 +00:00
|
|
|
g_assert_nonnull (ws_conn);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
app_state = SERVER_CONNECTED;
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Connected to signalling server\n");
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
g_signal_connect (ws_conn, "closed", G_CALLBACK (on_server_closed), NULL);
|
|
|
|
g_signal_connect (ws_conn, "message", G_CALLBACK (on_server_message), NULL);
|
|
|
|
|
|
|
|
/* Register with the server so it knows about us and can accept commands */
|
|
|
|
register_with_server ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to the signalling server. This is the entrypoint for everything else.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
connect_to_websocket_server_async (void)
|
|
|
|
{
|
|
|
|
SoupLogger *logger;
|
|
|
|
SoupMessage *message;
|
|
|
|
SoupSession *session;
|
2020-04-14 10:49:55 +00:00
|
|
|
const char *https_aliases[] = { "wss", NULL };
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2020-04-14 10:49:55 +00:00
|
|
|
session =
|
|
|
|
soup_session_new_with_options (SOUP_SESSION_SSL_STRICT, !disable_ssl,
|
2017-10-21 14:27:29 +00:00
|
|
|
SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE,
|
|
|
|
//SOUP_SESSION_SSL_CA_FILE, "/etc/ssl/certs/ca-bundle.crt",
|
|
|
|
SOUP_SESSION_HTTPS_ALIASES, https_aliases, NULL);
|
|
|
|
|
|
|
|
logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
|
|
|
|
soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
|
|
|
|
g_object_unref (logger);
|
|
|
|
|
|
|
|
message = soup_message_new (SOUP_METHOD_GET, server_url);
|
|
|
|
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Connecting to server...\n");
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
/* Once connected, we will register */
|
|
|
|
soup_session_websocket_connect_async (session, message, NULL, NULL, NULL,
|
|
|
|
(GAsyncReadyCallback) on_server_connected, message);
|
|
|
|
app_state = SERVER_CONNECTING;
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:24:48 +00:00
|
|
|
static gboolean
|
|
|
|
check_plugins (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
gboolean ret;
|
|
|
|
GstPlugin *plugin;
|
|
|
|
GstRegistry *registry;
|
|
|
|
const gchar *needed[] = { "opus", "vpx", "nice", "webrtc", "dtls", "srtp",
|
2020-04-14 10:49:55 +00:00
|
|
|
"rtpmanager", "videotestsrc", "audiotestsrc", NULL
|
|
|
|
};
|
2018-03-09 20:24:48 +00:00
|
|
|
|
|
|
|
registry = gst_registry_get ();
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0; i < g_strv_length ((gchar **) needed); i++) {
|
|
|
|
plugin = gst_registry_find_plugin (registry, needed[i]);
|
|
|
|
if (!plugin) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Required gstreamer plugin '%s' not found\n", needed[i]);
|
2018-03-09 20:24:48 +00:00
|
|
|
ret = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
gst_object_unref (plugin);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GOptionContext *context;
|
|
|
|
GError *error = NULL;
|
2020-11-25 17:41:53 +00:00
|
|
|
int ret_code = -1;
|
2017-10-21 14:27:29 +00:00
|
|
|
|
|
|
|
context = g_option_context_new ("- gstreamer webrtc sendrecv demo");
|
|
|
|
g_option_context_add_main_entries (context, entries, NULL);
|
|
|
|
g_option_context_add_group (context, gst_init_get_option_group ());
|
|
|
|
if (!g_option_context_parse (context, &argc, &argv, &error)) {
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_printerr ("Error initializing: %s\n", error->message);
|
2017-10-21 14:27:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-04-30 04:15:01 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "webrtc-sendrecv", 0,
|
|
|
|
"WebRTC Sending and Receiving example");
|
|
|
|
|
2020-11-25 17:41:53 +00:00
|
|
|
if (!check_plugins ()) {
|
|
|
|
goto out;
|
|
|
|
}
|
2018-03-09 20:24:48 +00:00
|
|
|
|
2020-11-25 17:41:53 +00:00
|
|
|
if (!peer_id && !our_id) {
|
|
|
|
gst_printerr ("--peer-id or --our-id is a required argument\n");
|
|
|
|
goto out;
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 17:41:53 +00:00
|
|
|
if (peer_id && our_id) {
|
|
|
|
gst_printerr ("specify only --peer-id or --our-id\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_code = 0;
|
|
|
|
|
2018-06-06 16:51:15 +00:00
|
|
|
/* Disable ssl when running a localhost server, because
|
2018-03-31 04:57:05 +00:00
|
|
|
* it's probably a test server with a self-signed certificate */
|
|
|
|
{
|
|
|
|
GstUri *uri = gst_uri_from_string (server_url);
|
|
|
|
if (g_strcmp0 ("localhost", gst_uri_get_host (uri)) == 0 ||
|
|
|
|
g_strcmp0 ("127.0.0.1", gst_uri_get_host (uri)) == 0)
|
2018-06-06 16:51:15 +00:00
|
|
|
disable_ssl = TRUE;
|
2018-03-31 04:57:05 +00:00
|
|
|
gst_uri_unref (uri);
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
|
|
|
connect_to_websocket_server_async ();
|
|
|
|
|
|
|
|
g_main_loop_run (loop);
|
2020-11-25 17:41:53 +00:00
|
|
|
|
|
|
|
if (loop)
|
|
|
|
g_main_loop_unref (loop);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2018-03-31 04:58:51 +00:00
|
|
|
if (pipe1) {
|
|
|
|
gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_NULL);
|
2020-07-26 17:20:59 +00:00
|
|
|
gst_print ("Pipeline stopped\n");
|
2018-03-31 04:58:51 +00:00
|
|
|
gst_object_unref (pipe1);
|
|
|
|
}
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2020-11-25 17:41:53 +00:00
|
|
|
out:
|
|
|
|
g_free (peer_id);
|
|
|
|
g_free (our_id);
|
|
|
|
|
|
|
|
return ret_code;
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|