2 #include <gst/sdp/sdp.h>
3 #include <gst/webrtc/webrtc.h>
7 static GMainLoop *loop;
8 static GstElement *pipe1, *webrtc1, *webrtc2;
12 _bus_watch (GstBus * bus, GstMessage * msg, GstElement * pipe)
14 switch (GST_MESSAGE_TYPE (msg)) {
15 case GST_MESSAGE_STATE_CHANGED:
16 if (GST_ELEMENT (msg->src) == pipe) {
17 GstState old, new, pending;
19 gst_message_parse_state_changed (msg, &old, &new, &pending);
22 gchar *dump_name = g_strconcat ("state_changed-",
23 gst_element_state_get_name (old), "_",
24 gst_element_state_get_name (new), NULL);
25 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (msg->src),
26 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
31 case GST_MESSAGE_ERROR:{
33 gchar *dbg_info = NULL;
35 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipe),
36 GST_DEBUG_GRAPH_SHOW_ALL, "error");
38 gst_message_parse_error (msg, &err, &dbg_info);
39 g_printerr ("ERROR from element %s: %s\n",
40 GST_OBJECT_NAME (msg->src), err->message);
41 g_printerr ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
44 g_main_loop_quit (loop);
47 case GST_MESSAGE_EOS:{
48 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipe),
49 GST_DEBUG_GRAPH_SHOW_ALL, "eos");
50 g_print ("EOS received\n");
51 g_main_loop_quit (loop);
62 _webrtc_pad_added (GstElement * webrtc, GstPad * new_pad, GstElement * pipe)
67 if (GST_PAD_DIRECTION (new_pad) != GST_PAD_SRC)
70 out = gst_parse_bin_from_description ("rtpvp8depay ! vp8dec ! "
71 "videoconvert ! queue ! xvimagesink", TRUE, NULL);
72 gst_bin_add (GST_BIN (pipe), out);
73 gst_element_sync_state_with_parent (out);
75 sink = out->sinkpads->data;
77 gst_pad_link (new_pad, sink);
81 _on_answer_received (GstPromise * promise, gpointer user_data)
83 GstWebRTCSessionDescription *answer = NULL;
84 const GstStructure *reply;
87 g_assert (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED);
88 reply = gst_promise_get_reply (promise);
89 gst_structure_get (reply, "answer",
90 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL);
91 gst_promise_unref (promise);
92 desc = gst_sdp_message_as_text (answer->sdp);
93 g_print ("Created answer:\n%s\n", desc);
96 /* this is one way to tell webrtcbin that we don't want to be notified when
97 * this task is complete: set a NULL promise */
98 g_signal_emit_by_name (webrtc1, "set-remote-description", answer, NULL);
99 /* this is another way to tell webrtcbin that we don't want to be notified
100 * when this task is complete: interrupt the promise */
101 promise = gst_promise_new ();
102 g_signal_emit_by_name (webrtc2, "set-local-description", answer, NULL);
103 gst_promise_interrupt (promise);
104 gst_promise_unref (promise);
106 gst_webrtc_session_description_free (answer);
110 _on_offer_received (GstPromise * promise, gpointer user_data)
112 GstWebRTCSessionDescription *offer = NULL;
113 const GstStructure *reply;
116 g_assert (gst_promise_wait (promise) == GST_PROMISE_RESULT_REPLIED);
117 reply = gst_promise_get_reply (promise);
118 gst_structure_get (reply, "offer",
119 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
120 gst_promise_unref (promise);
121 desc = gst_sdp_message_as_text (offer->sdp);
122 g_print ("Created offer:\n%s\n", desc);
125 g_signal_emit_by_name (webrtc1, "set-local-description", offer, NULL);
126 g_signal_emit_by_name (webrtc2, "set-remote-description", offer, NULL);
128 promise = gst_promise_new_with_change_func (_on_answer_received, user_data,
130 g_signal_emit_by_name (webrtc2, "create-answer", NULL, promise);
132 gst_webrtc_session_description_free (offer);
136 _on_negotiation_needed (GstElement * element, gpointer user_data)
140 promise = gst_promise_new_with_change_func (_on_offer_received, user_data,
142 g_signal_emit_by_name (webrtc1, "create-offer", NULL, promise);
146 _on_ice_candidate (GstElement * webrtc, guint mlineindex, gchar * candidate,
149 g_signal_emit_by_name (other, "add-ice-candidate", mlineindex, candidate);
153 main (int argc, char *argv[])
155 gst_init (&argc, &argv);
157 loop = g_main_loop_new (NULL, FALSE);
159 gst_parse_launch ("videotestsrc ! queue ! vp8enc ! rtpvp8pay ! queue ! "
160 "application/x-rtp,media=video,payload=96,encoding-name=VP8 ! "
161 "webrtcbin name=smpte videotestsrc pattern=ball ! queue ! vp8enc ! rtpvp8pay ! queue ! "
162 "application/x-rtp,media=video,payload=96,encoding-name=VP8 ! webrtcbin name=ball",
164 bus1 = gst_pipeline_get_bus (GST_PIPELINE (pipe1));
165 gst_bus_add_watch (bus1, (GstBusFunc) _bus_watch, pipe1);
167 webrtc1 = gst_bin_get_by_name (GST_BIN (pipe1), "smpte");
168 g_signal_connect (webrtc1, "on-negotiation-needed",
169 G_CALLBACK (_on_negotiation_needed), NULL);
170 g_signal_connect (webrtc1, "pad-added", G_CALLBACK (_webrtc_pad_added),
172 webrtc2 = gst_bin_get_by_name (GST_BIN (pipe1), "ball");
173 g_signal_connect (webrtc2, "pad-added", G_CALLBACK (_webrtc_pad_added),
175 g_signal_connect (webrtc1, "on-ice-candidate",
176 G_CALLBACK (_on_ice_candidate), webrtc2);
177 g_signal_connect (webrtc2, "on-ice-candidate",
178 G_CALLBACK (_on_ice_candidate), webrtc1);
180 g_print ("Starting pipeline\n");
181 gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_PLAYING);
183 g_main_loop_run (loop);
185 gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_NULL);
186 g_print ("Pipeline stopped\n");
188 gst_object_unref (webrtc1);
189 gst_object_unref (webrtc2);
190 gst_bus_remove_watch (bus1);
191 gst_object_unref (bus1);
192 gst_object_unref (pipe1);