2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef GST_USE_UNSTABLE_API
18 #define GST_USE_UNSTABLE_API
19 #include <gst/webrtc/webrtc.h>
21 #include "media_streamer_util.h"
22 #include "media_streamer_priv.h"
23 #include "media_streamer_gst.h"
24 #include "media_streamer_gst_webrtc.h"
25 #include "media_streamer_node.h"
27 static gchar *__make_ice_candidate_message(guint mlineindex, gchar *candidate)
29 JsonObject *ice, *msg;
32 ms_retvm_if(candidate == NULL, NULL, "candidate is NULL");
34 ice = json_object_new();
35 json_object_set_string_member(ice, "candidate", candidate);
36 json_object_set_int_member(ice, "sdpMLineIndex", mlineindex);
38 msg = json_object_new();
39 json_object_set_object_member(msg, "ice", ice);
41 text = ms_get_string_from_json_object(msg);
43 json_object_unref(msg);
48 static gchar* __make_sdp_message(GstWebRTCSessionDescription *desc)
51 JsonObject *msg, *sdp;
53 text = gst_sdp_message_as_text(desc->sdp);
54 sdp = json_object_new();
56 if (desc->type == GST_WEBRTC_SDP_TYPE_OFFER) {
57 ms_info("Making offer message:\n%s", text);
58 json_object_set_string_member(sdp, "type", "offer");
59 } else if (desc->type == GST_WEBRTC_SDP_TYPE_ANSWER) {
60 ms_info("Making answer message:\n%s", text);
61 json_object_set_string_member(sdp, "type", "answer");
63 ms_error("invalid description type");
67 json_object_set_string_member(sdp, "sdp", text);
70 msg = json_object_new();
71 json_object_set_object_member(msg, "sdp", sdp);
73 text = ms_get_string_from_json_object(msg);
75 json_object_unref(msg);
80 static void __trigger_message_callback(media_streamer_node_s *webrtc_node, gchar *message)
82 ms_retm_if(webrtc_node == NULL, "webrtc_node is NULL");
84 ms_debug("message is : \n%s", message);
86 if (webrtc_node->user_cb.callback) {
87 ms_debug("=====> Now trigger user callback(%p)", webrtc_node->user_cb.callback);
88 ((media_streamer_webrtc_message_cb)(webrtc_node->user_cb.callback))(webrtc_node, message, webrtc_node->user_cb.user_data);
89 ms_debug("<===== End of the callback");
91 ms_warning("message callback is NULL");
95 static void __ms_webrtcbin_set_session_description(GstElement *webrtcbin, GstWebRTCSessionDescription *session_description, gboolean is_remote)
98 ms_retm_if(session_description == NULL, "session_description is NULL");
99 ms_retm_if(webrtcbin == NULL, "webrtcbin is NULL");
103 promise = gst_promise_new();
104 g_signal_emit_by_name(webrtcbin, is_remote? "set-remote-description" : "set-local-description", session_description, promise);
105 gst_promise_interrupt(promise);
106 gst_promise_unref(promise);
111 static void __on_offer_created_cb(GstPromise *promise, gpointer user_data)
113 GstWebRTCSessionDescription *offer = NULL;
114 const GstStructure *reply;
115 media_streamer_node_s *webrtc_node = (media_streamer_node_s *)user_data;
116 node_info_s *node_klass_type = NULL;
117 GstElement *webrtcbin = NULL;
120 ms_retm_if(promise == NULL, "promise is NULL");
121 ms_retm_if(webrtc_node == NULL, "webrtc_node is NULL");
122 ms_retm_if(gst_promise_wait(promise) != GST_PROMISE_RESULT_REPLIED, "promise is not for replied result");
126 node_klass_type = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_WEBRTC);
127 if (!(webrtcbin = ms_find_element_in_bin_by_type(webrtc_node->gst_element, node_klass_type))) {
128 ms_error("Could not find webrtcbin by type[%s, %s]", node_klass_type->klass_name, node_klass_type->default_name);
132 reply = gst_promise_get_reply(promise);
133 gst_structure_get(reply, "offer",
134 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
135 gst_promise_unref(promise);
137 __ms_webrtcbin_set_session_description(webrtcbin, offer, FALSE);
139 sdp_msg = __make_sdp_message(offer);
140 gst_webrtc_session_description_free(offer);
142 /* Send local description to peer */
143 __trigger_message_callback(webrtc_node, sdp_msg);
149 static void __on_answer_created_cb(GstPromise * promise, gpointer user_data)
151 GstWebRTCSessionDescription *answer = NULL;
152 const GstStructure *reply;
153 media_streamer_node_s *node = (media_streamer_node_s *)user_data;
154 node_info_s *node_klass_type = NULL;
156 GstElement *webrtcbin;
158 ms_retm_if(promise == NULL, "promise is NULL");
159 ms_retm_if(node == NULL, "node is NULL");
160 ms_retm_if(node->gst_element == NULL, "webrtc_container is NULL");
161 ms_retm_if(gst_promise_wait(promise) != GST_PROMISE_RESULT_REPLIED, "promise is not for replied result");
165 node_klass_type = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_WEBRTC);
166 if (!(webrtcbin = ms_find_element_in_bin_by_type(node->gst_element, node_klass_type))) {
167 ms_error("Could not find webrtcbin by type[%s, %s]", node_klass_type->klass_name, node_klass_type->default_name);
171 reply = gst_promise_get_reply(promise);
172 gst_structure_get(reply, "answer", GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL);
173 gst_promise_unref(promise);
175 __ms_webrtcbin_set_session_description(webrtcbin, answer, FALSE);
177 sdp_msg = __make_sdp_message(answer);
178 gst_webrtc_session_description_free(answer);
180 /* Send local description to peer */
181 __trigger_message_callback(node, sdp_msg);
187 int ms_webrtcbin_set_remote_session_description(media_streamer_node_s *node, const char *sdp_msg)
189 GstSDPMessage *gst_sdp;
192 GstWebRTCSessionDescription *answer, *offer;
193 node_info_s *node_klass_type;
194 GstElement *webrtcbin;
195 int ret = MEDIA_STREAMER_ERROR_NONE;
197 ms_retvm_if(node == NULL || node->gst_element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Error: empty webrtcbin");
198 ms_retvm_if(sdp_msg == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "sdp_msg is NULL");
203 node_klass_type = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_WEBRTC);
204 if (!(webrtcbin = ms_find_element_in_bin_by_type(node->gst_element, node_klass_type))) {
205 ms_error("Could not find webrtcbin by type[%s, %s]", node_klass_type->klass_name, node_klass_type->default_name);
206 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
209 ret = ms_webrtc_get_sdp_from_message(sdp_msg, &sdp, &type);
210 if (ret != MEDIA_STREAMER_ERROR_NONE)
213 ret = gst_sdp_message_new(&gst_sdp);
214 if (ret != GST_SDP_OK) {
215 ret = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
219 ret = gst_sdp_message_parse_buffer((guint8 *)sdp, strlen(sdp), gst_sdp);
220 if (ret != GST_SDP_OK) {
221 ret = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
225 if (g_str_equal(type, "answer")) {
226 answer = gst_webrtc_session_description_new(GST_WEBRTC_SDP_TYPE_ANSWER, gst_sdp);
227 g_assert_nonnull(answer);
229 __ms_webrtcbin_set_session_description(webrtcbin, answer, TRUE);
230 gst_webrtc_session_description_free(answer);
231 } else if (g_str_equal(type, "offer")) {
232 offer = gst_webrtc_session_description_new(GST_WEBRTC_SDP_TYPE_OFFER, gst_sdp);
233 g_assert_nonnull(offer);
235 __ms_webrtcbin_set_session_description(webrtcbin, offer, TRUE);
236 gst_webrtc_session_description_free(offer);
238 ms_webrtcbin_on_negotiation_process_answer(webrtcbin, node);
240 ms_error("type is %s, it is not a answer or offer", type);
252 int ms_webrtcbin_add_ice_candidate(media_streamer_node_s *node, const char *ice_msg)
256 node_info_s *node_klass_type;
257 GstElement *webrtcbin;
260 ms_retvm_if(node == NULL || node->gst_element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Error: empty webrtcbin");
261 ms_retvm_if(ice_msg == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ice_msg is NULL");
265 node_klass_type = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_WEBRTC);
266 if (!(webrtcbin = ms_find_element_in_bin_by_type(node->gst_element, node_klass_type))) {
267 ms_error("Could not find webrtcbin by type[%s, %s]", node_klass_type->klass_name, node_klass_type->default_name);
268 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
271 ret = ms_webrtc_get_ice_candidate_from_message(ice_msg, &candidate, &sdpmlineindex);
272 if (ret != MEDIA_STREAMER_ERROR_NONE)
275 /*Add ice candidate sent by remote peer */
276 g_signal_emit_by_name(webrtcbin, "add-ice-candidate", sdpmlineindex, candidate);
280 return MEDIA_STREAMER_ERROR_NONE;
283 void ms_webrtcbin_on_negotiation_process_answer(GstElement *webrtcbin, media_streamer_node_s *webrtc_node)
287 ms_retm_if(webrtcbin == NULL, "webrtcbin is NULL");
291 promise = gst_promise_new_with_change_func(__on_answer_created_cb, webrtc_node, NULL);
292 g_signal_emit_by_name(G_OBJECT(webrtcbin), "create-answer", NULL, promise);
297 void ms_webrtcbin_on_negotiation_needed_cb(GstElement *webrtcbin, gpointer user_data)
301 ms_retm_if(webrtcbin == NULL, "webrtcbin is NULL");
302 ms_retm_if(user_data == NULL, "user_data is NULL");
306 promise = gst_promise_new_with_change_func(__on_offer_created_cb, user_data, NULL);
307 g_signal_emit_by_name(G_OBJECT(webrtcbin), "create-offer", NULL, promise);
312 void ms_webrtcbin_on_ice_candidate_cb(GstElement *webrtcbin, guint mlineindex, gchar *candidate, gpointer user_data)
314 gchar *ice_candidate_msg = NULL;
315 media_streamer_node_s *webrtc_node = (media_streamer_node_s *)user_data;
317 ms_retm_if(webrtcbin == NULL, "webrtcbin is NULL");
318 ms_retm_if(candidate == NULL, "candidate is NULL");
319 ms_retm_if(webrtc_node == NULL, "webrtc_node is NULL");
321 ice_candidate_msg = __make_ice_candidate_message(mlineindex, candidate);
323 __trigger_message_callback(webrtc_node, ice_candidate_msg);
325 g_free(ice_candidate_msg);
328 void ms_webrtcbin_notify_ice_gathering_state_cb(GstElement *webrtcbin, GParamSpec * pspec, gpointer user_data)
330 GstWebRTCICEGatheringState ice_gather_state;
331 const gchar *new_state = "UNKNOWN";
333 g_object_get(webrtcbin, "ice-gathering-state", &ice_gather_state, NULL);
335 switch (ice_gather_state) {
336 case GST_WEBRTC_ICE_GATHERING_STATE_NEW:
339 case GST_WEBRTC_ICE_GATHERING_STATE_GATHERING:
340 new_state = "GATHERING";
342 case GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE:
343 new_state = "COMPLETE";
347 ms_info("ICE gathering state changed to [%s]", new_state);
350 static void __data_channel_on_error_cb(GObject *data_channel, gpointer user_data)
352 ms_retm_if(data_channel == NULL, "data_channel is NULL");
359 static void __data_channel_on_open_cb(GObject *data_channel, gpointer user_data)
361 GBytes *bytes = NULL;
363 ms_retm_if(data_channel == NULL, "data_channel is NULL");
367 bytes = g_bytes_new("data", strlen("data"));
369 g_signal_emit_by_name(data_channel, "send-string", "Hi! from GStreamer");
370 g_signal_emit_by_name(data_channel, "send-data", bytes);
372 g_bytes_unref(bytes);
377 static void __data_channel_on_close_cb(GObject *data_channel, gpointer user_data)
379 ms_retm_if(data_channel == NULL, "data_channel is NULL");
386 static void __data_channel_on_message_string_cb(GObject *data_channel, gchar *message, gpointer user_data)
388 ms_retm_if(data_channel == NULL, "data_channel is NULL");
389 ms_retm_if(message == NULL, "message is NULL");
391 ms_info("Received message: %s", message);
394 static void __connect_data_channel_signals(GObject *data_channel)
396 ms_retm_if(data_channel == NULL, "data_channel is NULL");
400 g_signal_connect(data_channel, "on-error", G_CALLBACK(__data_channel_on_error_cb), NULL);
401 g_signal_connect(data_channel, "on-open", G_CALLBACK(__data_channel_on_open_cb), NULL);
402 g_signal_connect(data_channel, "on-close", G_CALLBACK(__data_channel_on_close_cb), NULL);
403 g_signal_connect(data_channel, "on-message-string", G_CALLBACK(__data_channel_on_message_string_cb), NULL);
408 void ms_webrtcbin_on_data_channel_cb(GstElement *webrtcbin, GObject *data_channel, gpointer user_data)
410 media_streamer_s *ms_streamer = (media_streamer_s *)user_data;
412 ms_retm_if(ms_streamer == NULL, "ms_streamer is NULL");
413 ms_retm_if(data_channel == NULL, "data_channel is NULL");
417 __connect_data_channel_signals(data_channel);
422 void ms_webrtcbin_pad_added_cb(GstElement *webrtcbin, GstPad *new_pad, gpointer user_data)
424 media_streamer_s *ms_streamer = (media_streamer_s *)user_data;
426 ms_retm_if(new_pad == NULL, "new_pad is NULL");
427 ms_retm_if(ms_streamer == NULL, "ms_streamer is NULL");
428 ms_retm_if(GST_PAD_DIRECTION(new_pad) != GST_PAD_SRC, "new_pad is not for source");
432 ms_debug("Pad [%s] added on [%s]", GST_PAD_NAME(new_pad), GST_ELEMENT_NAME(webrtcbin));
437 GstElement *ms_webrtc_element_create(void)
439 GstElement *webrtc_container;
440 GstElement *webrtcbin;
441 GstGhostPad *ghost_pad_video_in;
445 webrtc_container = gst_bin_new("webrtc_container");
446 ms_retvm_if(!webrtc_container, (GstElement *) NULL, "Error: creating elements for webrtc container");
448 ms_add_no_target_ghostpad(webrtc_container, MS_RTP_PAD_VIDEO_IN, GST_PAD_SINK);
450 MS_SET_INT_STATIC_STRING_PARAM(webrtc_container, MEDIA_STREAMER_PARAM_WEBRTC_PEER_TYPE, DEFAULT_WEBRTC_PEER);
451 MS_SET_INT_STATIC_STRING_PARAM(webrtc_container, MEDIA_STREAMER_PARAM_WEBRTC_REMOTE_SESSION_DESCRIPTION, NULL);
452 MS_SET_INT_STATIC_STRING_PARAM(webrtc_container, MEDIA_STREAMER_PARAM_WEBRTC_ADD_ICE_CANDIDATE, NULL);
454 if (!(webrtcbin = ms_element_create("webrtcbin", NULL))) {
455 ms_error("Failed to create webrtcbin element");
459 /* FIXME: these should be set from user */
460 g_object_set(G_OBJECT(webrtcbin), "bundle-policy", 3, NULL); // 3:max-bundle
461 g_object_set(G_OBJECT(webrtcbin), "stun-server", "stun://stun.l.google.com:19302", NULL);
463 ms_bin_add_element(webrtc_container, webrtcbin, FALSE);
465 if (!(ghost_pad_video_in = (GstGhostPad *)gst_element_get_static_pad(webrtc_container, MS_RTP_PAD_VIDEO_IN))) {
466 ms_error("Failed to get ghost pad for webrtc_container");
470 if (!(gst_ghost_pad_set_target(ghost_pad_video_in, gst_element_get_request_pad(webrtcbin, "sink_%u")))) {
471 ms_info("Failed to gst_ghost_pad_set_target() for %s", MS_RTP_PAD_VIDEO_IN);
472 /* release resources */
478 return webrtc_container;