2 * Copyright (c) 2015 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.
18 #include "media_streamer_gst.h"
19 #include "media_streamer_gst_webrtc.h"
20 #include "media_streamer_node.h"
22 #define MS_PADS_UNLINK(pad, peer) GST_PAD_IS_SRC(pad) ? \
23 gst_pad_unlink(pad, peer) : \
24 gst_pad_unlink(peer, pad)
25 #define H264_PARSER_CONFIG_INTERVAL 5
26 #define H264_ENCODER_ZEROLATENCY 0x00000004
28 #define MEDIASTREAMER_DECODEBIN_TYPE_DECODER "video_decoder"
31 MEDIA_STREAMER_SINK_BIN_NORMAL,
32 MEDIA_STREAMER_SINK_BIN_RTP_SERVER,
33 MEDIA_STREAMER_SINK_BIN_ADAPTIVE,
34 } media_streamer_sink_bin_type_e;
36 static int __ms_adaptive_sink_prepare(media_streamer_s *ms_streamer);
38 void ms_generate_dots(GstElement *bin, gchar *name_tag)
41 ms_retm_if(bin == NULL, "bin is NULL");
44 dot_name = g_strdup(DOT_FILE_NAME);
46 dot_name = g_strconcat(DOT_FILE_NAME, ".", name_tag, NULL);
48 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(bin), GST_DEBUG_GRAPH_SHOW_ALL, dot_name);
50 MS_SAFE_GFREE(dot_name);
53 int ms_add_no_target_ghostpad(GstElement *gst_bin, const char *ghost_pad_name, GstPadDirection pad_direction)
55 int ret = MEDIA_STREAMER_ERROR_NONE;
56 gchar *bin_name = NULL;
57 GstPad *ghost_pad = NULL;
61 ms_retvm_if(gst_bin == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "gst_bin is NULL");
63 bin_name = gst_element_get_name(gst_bin);
64 ghost_pad = gst_ghost_pad_new_no_target(ghost_pad_name, pad_direction);
65 if (gst_element_add_pad(GST_ELEMENT(gst_bin), ghost_pad)) {
66 gst_pad_set_active(ghost_pad, TRUE);
67 ms_info("Added [%s] empty ghostpad into [%s]", ghost_pad_name, bin_name);
69 ms_info("Error: Failed to add empty [%s] ghostpad into [%s]", ghost_pad_name, bin_name);
70 ret = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
73 MS_SAFE_GFREE(bin_name);
80 static gboolean __ms_add_ghostpad(GstElement *gst_element, const char *pad_name, GstElement *gst_bin, const char *ghost_pad_name)
83 GstPad *ghost_pad = NULL;
84 GstPad *element_pad = NULL;
88 ms_retvm_if(!gst_element, FALSE, "gst_element is NULL");
89 ms_retvm_if(!pad_name, FALSE, "pad_name is NULL");
90 ms_retvm_if(!gst_bin, FALSE, "gst_bin is NULL");
92 element_pad = gst_element_get_static_pad(gst_element, pad_name);
94 /* Check, if pad is not static, get it by request */
95 if (element_pad == NULL)
96 element_pad = gst_element_get_request_pad(gst_element, pad_name);
98 ghost_pad = gst_ghost_pad_new(ghost_pad_name, element_pad);
99 ms_retvm_if(!ghost_pad, FALSE, "ghost_pad is NULL");
101 gst_pad_set_active(ghost_pad, TRUE);
103 ret = gst_element_add_pad(GST_ELEMENT(gst_bin), ghost_pad);
106 ms_info("Added %s ghostpad from [%s] into [%s]", pad_name, GST_ELEMENT_NAME(gst_element), GST_ELEMENT_NAME(gst_bin));
108 ms_error("Error: element [%s] does not have valid [%s] pad for adding into [%s] bin", GST_ELEMENT_NAME(gst_element), pad_name, GST_ELEMENT_NAME(gst_bin));
110 MS_SAFE_UNREF(element_pad);
117 /* This unlinks from its peer and ghostpads on its way */
118 static gboolean __ms_pad_peer_unlink(GstPad *pad)
121 GstPad *peer_pad = NULL;
125 ms_retvm_if(!pad, FALSE, "pad is NULL");
127 if (!gst_pad_is_linked(pad))
130 peer_pad = gst_pad_get_peer(pad);
131 ms_retvm_if(!peer_pad, FALSE, "Fail to get peer pad");
133 if (GST_IS_PROXY_PAD(peer_pad)) {
135 GstObject *ghost_object = gst_pad_get_parent(peer_pad);
136 if (GST_IS_GHOST_PAD(ghost_object)) {
137 GstPad *ghost_pad = GST_PAD(ghost_object);
138 GstPad *target_pad = gst_pad_get_peer(ghost_pad);
140 if (target_pad && GST_IS_GHOST_PAD(target_pad))
141 ret = ret && gst_element_remove_pad(GST_ELEMENT(GST_PAD_PARENT(target_pad)), target_pad);
143 /* This is a usual static pad */
145 ret = ret && MS_PADS_UNLINK(ghost_pad, target_pad);
147 ms_info("Ghost Pad [%s] does not have target.", GST_PAD_NAME(ghost_pad));
149 ret = ret && gst_element_remove_pad(GST_ELEMENT(GST_PAD_PARENT(ghost_pad)), ghost_pad);
151 MS_SAFE_UNREF(target_pad);
152 MS_SAFE_UNREF(ghost_pad);
154 MS_SAFE_UNREF(ghost_object);
156 } else if (GST_IS_GHOST_PAD(peer_pad)) {
157 ret = ret && gst_element_remove_pad(GST_ELEMENT(GST_PAD_PARENT(peer_pad)), peer_pad);
159 /* This is a usual static pad */
160 ret = ret && MS_PADS_UNLINK(pad, peer_pad);
163 MS_SAFE_UNREF(peer_pad);
170 static GstElement *__ms_pad_get_peer_element(GstPad *pad)
172 GstPad *peer_pad = NULL;
173 GstElement *ret = NULL;
177 ms_retvm_if(!pad, NULL, "pad is NULL");
179 if (!gst_pad_is_linked(pad)) {
180 ms_info("Pad [%s:%s] is not linked yet", GST_DEBUG_PAD_NAME(pad));
184 peer_pad = gst_pad_get_peer(pad);
185 ms_retvm_if(!peer_pad, NULL, "Fail to get peer pad");
187 ret = gst_pad_get_parent_element(peer_pad);
189 if (GST_IS_PROXY_PAD(peer_pad)) {
190 GstPad *ghost_pad = GST_PAD(gst_pad_get_parent(peer_pad));
191 GstPad *target_pad = gst_pad_get_peer(ghost_pad);
192 if (GST_GHOST_PAD(target_pad)) {
193 GstPad *element_pad = gst_ghost_pad_get_target(GST_GHOST_PAD(target_pad));
194 ret = gst_pad_get_parent_element(element_pad);
195 MS_SAFE_UNREF(element_pad);
197 /* This is a usual static pad */
198 ret = gst_pad_get_parent_element(target_pad);
201 MS_SAFE_UNREF(target_pad);
202 MS_SAFE_UNREF(ghost_pad);
203 } else if (GST_IS_GHOST_PAD(peer_pad)) {
204 GstPad *element_pad = gst_ghost_pad_get_target(GST_GHOST_PAD(peer_pad));
205 ret = gst_pad_get_parent_element(element_pad);
206 MS_SAFE_UNREF(element_pad);
208 /* This is a usual static pad */
209 ret = gst_pad_get_parent_element(peer_pad);
212 MS_SAFE_UNREF(peer_pad);
219 gboolean ms_element_unlink(GstElement *element)
223 GValue elem = G_VALUE_INIT;
224 GstIterator *pad_iterator = NULL;
228 ms_retvm_if(!element, FALSE, "element is NULL");
230 pad_iterator = gst_element_iterate_pads(element);
231 while (GST_ITERATOR_OK == gst_iterator_next(pad_iterator, &elem)) {
232 pad = (GstPad *) g_value_get_object(&elem);
233 ret = ret && __ms_pad_peer_unlink(pad);
234 g_value_reset(&elem);
236 g_value_unset(&elem);
237 gst_iterator_free(pad_iterator);
244 gboolean ms_bin_remove_element(GstElement *element)
246 GstElement *parent = NULL;
247 gboolean ret = FALSE;
251 ms_retvm_if(!element, FALSE, "element is NULL");
253 parent = (GstElement *) gst_element_get_parent(element);
255 /* Remove node's element from bin that decreases ref count */
256 if (parent != NULL) {
257 ret = gst_bin_remove(GST_BIN(parent), element);
259 ms_debug("Element [%s] removed from [%s] bin", GST_ELEMENT_NAME(element), GST_ELEMENT_NAME(parent));
262 MS_SAFE_UNREF(parent);
269 gboolean ms_bin_add_element(GstElement *bin, GstElement *element, gboolean do_ref)
271 GstElement *parent = NULL;
272 gboolean ret = FALSE;
276 ms_retvm_if(!bin, FALSE, "bin is NULL");
277 ms_retvm_if(!element, FALSE, "element is NULL");
279 parent = (GstElement *) gst_element_get_parent(element);
281 /* Add node's element into bin and increases ref count if needed */
282 if (parent == NULL) {
283 ret = gst_bin_add(GST_BIN(bin), element);
285 ms_debug("Element [%s] added into [%s] bin", GST_ELEMENT_NAME(element), GST_ELEMENT_NAME(bin));
286 gst_object_ref(element);
287 gst_element_sync_state_with_parent(element);
290 ms_error("Element [%s] is already added into the [%s] bin", GST_ELEMENT_NAME(element), GST_ELEMENT_NAME(parent));
291 g_object_unref(parent);
299 const gchar *ms_get_pad_type(GstPad *element_pad)
301 const gchar *pad_type = NULL;
302 GstCaps *pad_caps = NULL;
306 ms_retvm_if(!element_pad, NULL, "element_pad is NULL");
308 pad_caps = gst_pad_query_caps(element_pad, 0);
309 MS_GET_CAPS_TYPE(pad_caps, pad_type);
310 gst_caps_unref(pad_caps);
317 static GstElement *__ms_find_peer_element_by_type(GstElement *previous_element, GstPad *prev_elem_src_pad, node_info_s *node_klass_type)
319 GstElement *peer_element = NULL;
320 GstIterator *src_pad_iterator = NULL;
321 GValue src_pad_value = G_VALUE_INIT;
322 const gchar *found_klass = NULL;
326 ms_retvm_if(!previous_element, NULL, "previous_element is NULL");
327 ms_retvm_if(!node_klass_type, NULL, "node_klass_type is NULL");
328 ms_retvm_if(!node_klass_type->klass_name, NULL, "node_klass_type->klass_name is NULL");
329 ms_retvm_if(!node_klass_type->default_name, NULL, "node_klass_type->default_name is NULL");
331 if (prev_elem_src_pad) {
333 /* Check if previous element`s source pad is connected with element */
334 peer_element = __ms_pad_get_peer_element(prev_elem_src_pad);
336 found_klass = gst_element_factory_get_klass(gst_element_get_factory(peer_element));
337 if (g_strrstr(found_klass, node_klass_type->klass_name) || g_strrstr(GST_ELEMENT_NAME(peer_element), node_klass_type->default_name))
338 ms_info(" Found peer element [%s] ", GST_ELEMENT_NAME(peer_element));
340 MS_SAFE_UNREF(peer_element);
344 /* Check if any of previous element`s source pads is connected with element */
345 src_pad_iterator = gst_element_iterate_src_pads(previous_element);
346 while (GST_ITERATOR_OK == gst_iterator_next(src_pad_iterator, &src_pad_value)) {
347 GstPad *src_pad = (GstPad *) g_value_get_object(&src_pad_value);
348 peer_element = __ms_pad_get_peer_element(src_pad);
350 found_klass = gst_element_factory_get_klass(gst_element_get_factory(peer_element));
351 if (g_strrstr(found_klass, node_klass_type->klass_name) || g_strrstr(GST_ELEMENT_NAME(peer_element), node_klass_type->default_name)) {
352 ms_info(" Found peer element [%s] ", GST_ELEMENT_NAME(peer_element));
353 g_value_reset(&src_pad_value);
356 MS_SAFE_UNREF(peer_element);
358 g_value_reset(&src_pad_value);
360 g_value_unset(&src_pad_value);
361 gst_iterator_free(src_pad_iterator);
365 ms_info(" Element [%s] is not connected", GST_ELEMENT_NAME(previous_element));
372 static gboolean __ms_link_two_elements(GstElement *previous_element, GstPad *prev_elem_src_pad, GstElement *found_element)
374 GValue src_pad_value = G_VALUE_INIT;
375 gboolean elements_linked = FALSE;
376 GstPad * found_sink_pad = NULL;
377 GstElement *peer_element = NULL;
378 GstIterator *src_pad_iterator = NULL;
379 GstPad *src_pad = NULL;
383 ms_retvm_if(!previous_element, FALSE, "previous_element is NULL");
384 ms_retvm_if(!found_element, FALSE, "found_element is NULL");
386 if (prev_elem_src_pad) {
387 peer_element = __ms_pad_get_peer_element(prev_elem_src_pad);
388 if (!gst_pad_is_linked(prev_elem_src_pad)) {
390 /* Check compatibility of previous element and unlinked found element */
391 found_sink_pad = gst_element_get_compatible_pad(found_element, prev_elem_src_pad, NULL);
393 elements_linked = gst_element_link_pads(previous_element, GST_PAD_NAME(prev_elem_src_pad), found_element, GST_PAD_NAME(found_sink_pad));
394 } else if (peer_element == found_element) {
395 elements_linked = TRUE;
397 MS_SAFE_UNREF(peer_element);
400 /* Check if previous element has any unlinked src pad */
401 src_pad_iterator = gst_element_iterate_src_pads(previous_element);
402 while (GST_ITERATOR_OK == gst_iterator_next(src_pad_iterator, &src_pad_value)) {
403 src_pad = (GstPad *) g_value_get_object(&src_pad_value);
404 GstElement *peer_element = __ms_pad_get_peer_element(src_pad);
405 if (!gst_pad_is_linked(src_pad)) {
407 /* Check compatibility of previous element and unlinked found element */
408 found_sink_pad = gst_element_get_compatible_pad(found_element, src_pad, NULL);
409 if (found_sink_pad) {
410 elements_linked = gst_element_link_pads(previous_element, GST_PAD_NAME(src_pad), found_element, GST_PAD_NAME(found_sink_pad));
411 if (elements_linked) {
412 g_value_reset(&src_pad_value);
413 MS_SAFE_UNREF(peer_element);
417 } else if (peer_element == found_element) {
418 elements_linked = TRUE;
420 MS_SAFE_UNREF(peer_element);
421 g_value_reset(&src_pad_value);
423 g_value_unset(&src_pad_value);
424 gst_iterator_free(src_pad_iterator);
427 if (found_sink_pad) {
429 ms_info("Succeeded to link [%s] -> [%s]", GST_ELEMENT_NAME(previous_element), GST_ELEMENT_NAME(found_element));
431 ms_debug("Failed to link [%s] and [%s]", GST_ELEMENT_NAME(previous_element), GST_ELEMENT_NAME(found_element));
433 ms_info("Element [%s] has no free compatible pad to be connected with [%s] or linked", GST_ELEMENT_NAME(found_element), GST_ELEMENT_NAME(previous_element));
435 MS_SAFE_UNREF(found_sink_pad);
439 return elements_linked;
442 static GstElement *__ms_bin_find_element_and_link_by_type(GstElement *previous_element, GstPad *prev_elem_src_pad, GstElement *search_bin, node_info_s *node_klass_type)
444 GValue element_value = G_VALUE_INIT;
445 GstElement *found_element = NULL;
446 gboolean elements_linked = FALSE;
447 const gchar *found_klass = NULL;
448 GstIterator *bin_iterator = NULL;
452 ms_retvm_if(!previous_element, NULL, "previous_element is NULL");
453 ms_retvm_if(!search_bin, NULL, "search_bin is NULL");
454 ms_retvm_if(!node_klass_type, NULL, "node_klass_type is NULL");
455 ms_retvm_if(!node_klass_type->default_name, NULL, "node_klass_type->default_name is NULL");
457 bin_iterator = gst_bin_iterate_sorted(GST_BIN(search_bin));
459 while (GST_ITERATOR_OK == gst_iterator_next(bin_iterator, &element_value)) {
460 found_element = (GstElement *) g_value_get_object(&element_value);
461 found_klass = gst_element_factory_get_klass(gst_element_get_factory(found_element));
463 /* Check if found element is of appropriate needed plugin class */
464 if (g_strrstr(found_klass, node_klass_type->klass_name) ||
465 g_strrstr(GST_ELEMENT_NAME(found_element), node_klass_type->default_name)) {
466 ms_info("Found element by type [%s]", GST_ELEMENT_NAME(found_element));
467 if (__ms_link_two_elements(previous_element, prev_elem_src_pad, found_element)) {
468 elements_linked = TRUE;
469 g_value_reset(&element_value);
470 g_object_ref(found_element);
474 g_value_reset(&element_value);
477 g_value_unset(&element_value);
478 gst_iterator_free(bin_iterator);
482 return elements_linked ? found_element : NULL;
485 GstElement *ms_find_element_in_bin_by_type(GstElement *bin, node_info_s *node_klass_type)
487 GValue element_value = G_VALUE_INIT;
488 GstElement *found_element = NULL;
489 GstElement *next_element = NULL;
490 const gchar *found_klass = NULL;
491 GstIterator *bin_iterator = NULL;
495 ms_retvm_if(!bin, NULL, "bin is NULL");
496 ms_retvm_if(!node_klass_type, NULL, "node_klass_type is NULL");
497 ms_retvm_if(!node_klass_type->default_name, NULL, "node_klass_type->default_name is NULL");
499 bin_iterator = gst_bin_iterate_sorted(GST_BIN(bin));
501 while (GST_ITERATOR_OK == gst_iterator_next(bin_iterator, &element_value)) {
502 next_element = (GstElement *) g_value_get_object(&element_value);
503 found_klass = gst_element_factory_get_klass(gst_element_get_factory(next_element));
505 if (g_strrstr(found_klass, node_klass_type->klass_name) ||
506 g_strrstr(GST_ELEMENT_NAME(next_element), node_klass_type->default_name)) {
507 ms_info("Found element by type [%s]", GST_ELEMENT_NAME(next_element));
508 found_element = next_element;
512 g_value_reset(&element_value);
515 gst_iterator_free(bin_iterator);
519 return found_element;
522 GstElement *ms_find_element_in_bin_by_name(GstElement *bin, const gchar *name)
524 GValue element_value = G_VALUE_INIT;
525 GstElement *found_element = NULL;
526 GstElement *next_element = NULL;
527 GstIterator *bin_iterator = NULL;
531 ms_retvm_if(!bin, NULL, "bin is NULL");
532 ms_retvm_if(!name, NULL, "name is NULL");
534 bin_iterator = gst_bin_iterate_sorted(GST_BIN(bin));
536 while (GST_ITERATOR_OK == gst_iterator_next(bin_iterator, &element_value)) {
537 next_element = (GstElement *) g_value_get_object(&element_value);
539 if (g_strrstr(GST_ELEMENT_NAME(next_element), name)) {
540 ms_info("Found element by name [%s]", GST_ELEMENT_NAME(next_element));
541 found_element = next_element;
545 g_value_reset(&element_value);
548 gst_iterator_free(bin_iterator);
552 return found_element;
555 static int __ms_factory_rank_compare(GstPluginFeature *first_feature, GstPluginFeature *second_feature)
558 guint first_rank = 0, second_rank = 0;
560 first_rank = gst_plugin_feature_get_rank(first_feature);
561 second_rank = gst_plugin_feature_get_rank(second_feature);
562 ms_debug("second[%s]_rank(%d) - first[%s]_rank(%d) = (%d)",
563 GST_OBJECT_NAME(GST_ELEMENT_FACTORY(second_feature)), second_rank,
564 GST_OBJECT_NAME(GST_ELEMENT_FACTORY(first_feature)), first_rank, second_rank - first_rank);
567 return second_rank - first_rank;
570 GstElement *ms_combine_next_element(GstElement *previous_element, GstPad *prev_elem_src_pad, GstElement *bin_to_find_in, media_streamer_node_type_e node_type)
572 GstCaps *prev_caps = NULL;
573 GstElement *found_element = NULL;
574 node_info_s *node_klass_type = NULL;
576 ms_retvm_if(!previous_element, NULL, "previous_element is NULL");
577 ms_retvm_if(!bin_to_find_in, NULL, "bin_to_find_in is NULL");
581 node_klass_type = ms_node_get_klass_by_its_type(node_type);
583 /* - 1 - If previous element is linked - check for peer element */
584 found_element = __ms_find_peer_element_by_type(GST_ELEMENT(previous_element), prev_elem_src_pad, node_klass_type);
586 /* - 2 - If previous element is not linked - find in bin by type */
588 found_element = __ms_bin_find_element_and_link_by_type(previous_element, prev_elem_src_pad, bin_to_find_in, node_klass_type);
590 /* - 3 - If element by type is not found in bin - create element by type */
591 if (!found_element) {
593 /* Create Node type information for a New element */
594 if (prev_elem_src_pad)
595 prev_caps = gst_pad_query_caps(prev_elem_src_pad, 0);
597 GstPad *src_pad = gst_element_get_static_pad(previous_element, "src");
599 prev_caps = gst_pad_query_caps(src_pad, 0);
600 MS_SAFE_UNREF(src_pad);
602 node_plug_s plug_info = {node_klass_type, NULL, prev_caps, NULL};
604 /* Create Node by ini or registry */
605 found_element = ms_node_element_create(&plug_info, node_type);
607 ms_info("New Element [%s] is created ", GST_ELEMENT_NAME(found_element));
609 /* Add created element */
610 if (ms_bin_add_element(bin_to_find_in, found_element, TRUE)) {
611 gst_element_sync_state_with_parent(found_element);
612 __ms_link_two_elements(previous_element, prev_elem_src_pad, found_element);
613 ms_generate_dots(bin_to_find_in, GST_ELEMENT_NAME(found_element));
615 ms_error("Element [%s] was not added into [%s] bin", GST_ELEMENT_NAME(found_element), GST_ELEMENT_NAME(bin_to_find_in));
616 MS_SAFE_UNREF(found_element);
619 ms_info("New Element is not created ");
621 ms_info("Next element is not combined");
623 MS_SAFE_UNREF(previous_element);
627 return found_element;
630 static gboolean __need_to_skip_hw_video_decoder_klass(const gchar *klass)
632 gchar **zerocopy_node_names = NULL;
633 gboolean found = false;
635 if (g_strcmp0(klass, "Codec/Decoder/Video/Hardware") == 0) {
636 if (!ms_ini_get_boolean("general:use zerocopy", DEFAULT_USE_ZEROCOPY)) {
637 ms_debug("Skipping [%s] because use zerocopy is false", MEDIASTREAMER_DECODEBIN_TYPE_DECODER);
640 ms_ini_read_list("general:zerocopy node names", &zerocopy_node_names);
641 found = (zerocopy_node_names && g_strv_contains((const gchar * const *)zerocopy_node_names, MEDIASTREAMER_DECODEBIN_TYPE_DECODER));
642 g_strfreev(zerocopy_node_names);
644 ms_debug("Skipping [%s] which is not specified in zerocopy node names", MEDIASTREAMER_DECODEBIN_TYPE_DECODER);
647 ms_debug("[%s] can be used", MEDIASTREAMER_DECODEBIN_TYPE_DECODER);
653 static gint __decodebin_autoplug_select_cb(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *factory, gpointer data)
655 /* NOTE : GstAutoplugSelectResult is defined in gstplay-enum.h but not exposed */
657 GST_AUTOPLUG_SELECT_TRY,
658 GST_AUTOPLUG_SELECT_EXPOSE,
659 GST_AUTOPLUG_SELECT_SKIP
660 } GstAutoplugSelectResult;
663 gchar *factory_name = NULL;
664 const gchar *klass = NULL;
665 GstAutoplugSelectResult result = GST_AUTOPLUG_SELECT_TRY;
666 media_streamer_s *ms_streamer = (media_streamer_s *) data;
670 ms_retvm_if(!factory, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "factory is NULL");
671 ms_retvm_if(!ms_streamer, GST_AUTOPLUG_SELECT_TRY, "data is NULL");
673 factory_name = GST_OBJECT_NAME(factory);
674 klass = gst_element_factory_get_metadata(factory, GST_ELEMENT_METADATA_KLASS);
676 ms_debug("Found new element [%s] to link [%s]", factory_name, klass);
678 if (ms_streamer->ini.exclude_elem_names) {
679 for (index = 0; ms_streamer->ini.exclude_elem_names[index]; ++index) {
680 if (g_strrstr(factory_name, ms_streamer->ini.exclude_elem_names[index])) {
681 ms_debug("Skipping [%s] as excluded", factory_name);
682 return GST_AUTOPLUG_SELECT_SKIP;
687 if (__need_to_skip_hw_video_decoder_klass(klass)) {
688 ms_debug("Skipping [%s]", factory_name);
689 return GST_AUTOPLUG_SELECT_SKIP;
692 if (ms_streamer->ini.resource_required_elem_names) {
693 /* Try to acquire resources before adding element */
694 for (index = 0; ms_streamer->ini.resource_required_elem_names[index]; ++index) {
695 if (g_strrstr(factory_name, ms_streamer->ini.resource_required_elem_names[index])) {
696 ms_debug("Trying to acquire resource for [%s] element", factory_name);
698 if (MM_RESOURCE_MANAGER_ERROR_NONE !=
699 mm_resource_manager_mark_for_acquire(ms_streamer->resource_manager,
700 MM_RESOURCE_MANAGER_RES_TYPE_VIDEO_DECODER,
701 MM_RESOURCE_MANAGER_RES_VOLUME_FULL,
702 &ms_streamer->video_decoder_resource)) {
703 ms_error("Failed to mark resources for acquire in [%s] element", factory_name);
704 return GST_AUTOPLUG_SELECT_SKIP;
707 if (MM_RESOURCE_MANAGER_ERROR_NONE !=
708 mm_resource_manager_commit(ms_streamer->resource_manager)) {
709 ms_error("Failed to acquire resources for [%s] element", factory_name);
711 if (MM_RESOURCE_MANAGER_ERROR_NONE !=
712 mm_resource_manager_mark_for_release(
713 ms_streamer->resource_manager,
714 ms_streamer->video_decoder_resource))
715 ms_error("Failed to mark resources for release in [%s] element", factory_name);
717 return GST_AUTOPLUG_SELECT_SKIP;
728 static gint __pad_type_compare(gconstpointer a, gconstpointer b)
730 GstPad *a_pad = NULL;
731 GstPad *b_pad = NULL;
732 const gchar *a_pad_type = NULL;
733 const gchar *b_pad_type = NULL;
737 ms_retvm_if(!a, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "a is NULL");
738 ms_retvm_if(!b, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "b is NULL");
743 a_pad_type = ms_get_pad_type(a_pad);
744 b_pad_type = ms_get_pad_type(b_pad);
748 if (MS_ELEMENT_IS_TEXT(a_pad_type))
750 else if (MS_ELEMENT_IS_TEXT(b_pad_type))
756 static void __decodebin_pad_added_cb(GstElement *element, GstPad *new_pad, gpointer user_data)
758 media_streamer_s *ms_streamer = (media_streamer_s *) user_data;
762 ms_retm_if(new_pad == NULL, "new_pad is NULL");
763 ms_retm_if(ms_streamer == NULL, "user_data is NULL");
765 g_mutex_lock(&ms_streamer->mutex_lock);
767 g_object_ref(new_pad);
769 ms_streamer->pads_types_list = g_list_insert_sorted(ms_streamer->pads_types_list, new_pad, __pad_type_compare);
771 g_mutex_unlock(&ms_streamer->mutex_lock);
776 static void __decodebin_nomore_pads_combine(GstPad *src_pad, media_streamer_s *ms_streamer, media_streamer_sink_bin_type_e sink_bin_type)
778 GstElement *found_element = NULL;
779 const gchar *new_pad_type = NULL;
780 GstCaps *caps = NULL;
781 gchar *caps_str = NULL;
785 ms_retm_if(src_pad == NULL, "src_pad is NULL");
786 ms_retm_if(ms_streamer == NULL, "ms_streamer is NULL");
788 found_element = gst_pad_get_parent_element(src_pad);
789 new_pad_type = ms_get_pad_type(src_pad);
791 if (MS_ELEMENT_IS_VIDEO(new_pad_type)) {
792 if (__ms_bin_find_element_and_link_by_type(found_element, src_pad, ms_streamer->transform_bin,
793 ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_TEXT_OVERLAY))) {
794 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_TEXT_OVERLAY);
797 if (sink_bin_type == MEDIA_STREAMER_SINK_BIN_RTP_SERVER) {
798 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER);
799 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_VIDEO_PAY);
800 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_RTP);
801 } else if (sink_bin_type == MEDIA_STREAMER_SINK_BIN_ADAPTIVE) {
802 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER);
803 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_MUXER);
804 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
806 caps = gst_pad_query_caps(src_pad, NULL);
807 caps_str = gst_caps_to_string(caps);
808 if (caps_str && (g_strrstr(caps_str, "ST12") || g_strrstr(caps_str, "SN12") ||
809 g_strrstr(caps_str, "SN21") || g_strrstr(caps_str, "S420") || g_strrstr(caps_str, "SR32"))) {
810 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
812 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_VIDEO_CONVERTER);
813 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
815 MS_SAFE_GFREE(caps_str);
816 gst_caps_unref(caps);
818 } else if (MS_ELEMENT_IS_AUDIO(new_pad_type)) {
819 if (sink_bin_type == MEDIA_STREAMER_SINK_BIN_RTP_SERVER) {
820 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER);
821 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_AUDIO_PAY);
822 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_RTP);
823 } else if (sink_bin_type == MEDIA_STREAMER_SINK_BIN_ADAPTIVE) {
824 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER);
825 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_MUXER);
826 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
828 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_AUDIO_CONVERTER);
829 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
831 } else if (MS_ELEMENT_IS_TEXT(new_pad_type)) {
832 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_TEXT_OVERLAY);
834 ms_error("Unsupported pad type [%s]!", new_pad_type);
837 ms_generate_dots(ms_streamer->pipeline, "after_sink_linked");
838 gst_object_unref(found_element);
843 static void __decodebin_nomore_pads_cb(GstElement *decodebin, gpointer user_data)
845 media_streamer_s *ms_streamer = (media_streamer_s *) user_data;
846 media_streamer_sink_bin_type_e sink_bin_type = MEDIA_STREAMER_SINK_BIN_NORMAL;
847 media_streamer_node_s *rtp_node = NULL;
848 media_streamer_node_s *adaptive_sink = NULL;
849 char *decodebin_name = NULL;
850 char *param_value = NULL;
851 GList *iterator = NULL;
853 GstPad *src_pad = NULL;
857 ms_retm_if(decodebin == NULL, "decodebin is NULL");
858 ms_retm_if(ms_streamer == NULL, "user_data is NULL");
860 g_mutex_lock(&ms_streamer->mutex_lock);
862 rtp_node = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "rtp_container");
863 adaptive_sink = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "adaptive_sink");
866 g_object_get(G_OBJECT(decodebin), "name", &decodebin_name, NULL);
868 /* FIXME: This case may be not general */
869 if (g_strrstr(decodebin_name, MEDIASTREAMER_DECODEBIN_TYPE_DECODER)) {
870 sink_bin_type = MEDIA_STREAMER_SINK_BIN_NORMAL;
871 MS_SAFE_FREE(decodebin_name);
873 /* It`s server part of Streaming Scenario*/
874 media_streamer_node_get_param(rtp_node, MEDIA_STREAMER_PARAM_VIDEO_OUT_PORT, ¶m_value);
875 if (param_value && ms_get_number_from_string(param_value) > 0)
876 sink_bin_type = MEDIA_STREAMER_SINK_BIN_RTP_SERVER;
878 MS_SAFE_FREE(param_value);
880 } else if (adaptive_sink) {
881 __ms_adaptive_sink_prepare(ms_streamer);
882 sink_bin_type = MEDIA_STREAMER_SINK_BIN_ADAPTIVE;
886 list = ms_streamer->pads_types_list;
887 for (iterator = list; iterator; iterator = iterator->next) {
888 src_pad = GST_PAD(iterator->data);
889 __decodebin_nomore_pads_combine(src_pad, ms_streamer, sink_bin_type);
892 g_mutex_unlock(&ms_streamer->mutex_lock);
897 static GstElement *__ms_decodebin_create(media_streamer_s *ms_streamer, char *name)
899 GstElement *decodebin = NULL;
903 ms_retvm_if(!ms_streamer, NULL, "ms_streamer is NULL");
905 decodebin = ms_element_create(DEFAULT_DECODEBIN, name);
906 ms_bin_add_element(ms_streamer->transform_bin, decodebin, TRUE);
907 gst_element_sync_state_with_parent(decodebin);
909 ms_signal_create(&ms_streamer->autoplug_sig_list, decodebin, "pad-added", G_CALLBACK(__decodebin_pad_added_cb), ms_streamer);
910 ms_signal_create(&ms_streamer->autoplug_sig_list, decodebin, "autoplug-select", G_CALLBACK(__decodebin_autoplug_select_cb), ms_streamer);
911 ms_signal_create(&ms_streamer->autoplug_sig_list, decodebin, "no-more-pads", G_CALLBACK(__decodebin_nomore_pads_cb), ms_streamer);
919 static gboolean __ms_sink_bin_prepare(media_streamer_s *ms_streamer, GstPad *src_pad, const gchar *src_pad_type)
921 GstElement *decoder_element = NULL;
922 GstElement *found_element = NULL;
923 GstElement *parent_rtp_element = NULL;
927 ms_retvm_if(!ms_streamer, FALSE, "ms_streamer is NULL");
928 ms_retvm_if(!src_pad, FALSE, "src_pad is NULL");
930 /* Getting Depayloader */
931 parent_rtp_element = gst_pad_get_parent_element(src_pad);
933 if (MS_ELEMENT_IS_VIDEO(src_pad_type)) {
934 gst_object_ref(parent_rtp_element);
935 found_element = ms_combine_next_element(parent_rtp_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_VIDEO_DEPAY);
936 decoder_element = __ms_bin_find_element_and_link_by_type(found_element, NULL, ms_streamer->transform_bin,
937 ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER));
939 if (!decoder_element) {
940 if (ms_streamer->ini.use_decodebin) {
941 decoder_element = __ms_decodebin_create(ms_streamer, MEDIASTREAMER_DECODEBIN_TYPE_DECODER);
942 gst_object_ref(found_element);
943 __ms_link_two_elements(found_element, NULL, decoder_element);
944 MS_SAFE_UNREF(decoder_element);
950 gst_object_ref(found_element);
951 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER);
952 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
955 __ms_link_two_elements(found_element, NULL, decoder_element);
956 found_element = ms_combine_next_element(decoder_element, NULL, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
958 } else if (MS_ELEMENT_IS_AUDIO(src_pad_type)) {
959 gst_object_ref(parent_rtp_element);
960 found_element = ms_combine_next_element(parent_rtp_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_AUDIO_DEPAY);
961 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_AUDIO_DECODER);
962 found_element = ms_combine_next_element(found_element, NULL, ms_streamer->sink_bin, MEDIA_STREAMER_NODE_TYPE_SINK);
964 ms_info("Unknown media type received from rtp element!");
966 MS_SAFE_UNREF(found_element);
967 MS_SAFE_UNREF(parent_rtp_element);
974 void ms_rtpbin_pad_added_cb(GstElement *src, GstPad *new_pad, gpointer user_data)
976 media_streamer_s *ms_streamer = NULL;
977 GstPad *target_pad = NULL;
978 GstCaps *src_pad_caps = NULL;
979 gchar *src_pad_name = NULL;
980 GstStructure *src_pad_struct = NULL;
981 const gchar *src_pad_type = NULL;
982 GstPad *src_pad = NULL;
983 media_streamer_node_s *ms_node = (media_streamer_node_s *) user_data;
987 ms_retm_if(new_pad == NULL, "new_pad is NULL");
988 ms_retm_if(ms_node == NULL, "ms_node is NULL");
989 ms_debug("Pad [%s] added on [%s]", GST_PAD_NAME(new_pad), GST_ELEMENT_NAME(src));
991 if (g_str_has_prefix(GST_PAD_NAME(new_pad), "recv_rtp_src")) {
992 ms_streamer = ms_node->parent_streamer;
993 ms_retm_if(ms_streamer == NULL, "Node's parent streamer handle is NULL");
994 g_mutex_lock(&ms_streamer->mutex_lock);
996 target_pad = gst_ghost_pad_get_target(GST_GHOST_PAD(new_pad));
997 src_pad_caps = gst_pad_query_caps(target_pad, NULL);
998 src_pad_struct = gst_caps_get_structure(src_pad_caps, 0);
999 src_pad_type = gst_structure_get_string(src_pad_struct, "media");
1000 ms_debug("type is [%s]", src_pad_type);
1002 if (MS_ELEMENT_IS_VIDEO(src_pad_type))
1003 src_pad_name = g_strdup_printf("%s_out", "video");
1004 else if (MS_ELEMENT_IS_AUDIO(src_pad_type))
1005 src_pad_name = g_strdup_printf("%s_out", "audio");
1007 if (src_pad_name != NULL) {
1009 src_pad = gst_element_get_static_pad(ms_node->gst_element, src_pad_name);
1012 gst_ghost_pad_set_target(GST_GHOST_PAD(src_pad), new_pad);
1014 if (src_pad && __ms_sink_bin_prepare(ms_streamer, src_pad, src_pad_type)) {
1015 ms_element_set_state(ms_node->gst_element, GST_STATE_PLAYING);
1016 ms_generate_dots(ms_streamer->pipeline, "rtpbin_playing");
1018 ms_error("Failed to prepare sink_bin for pad type [%s]", src_pad_type);
1021 MS_SAFE_GFREE(src_pad_name);
1023 g_mutex_unlock(&ms_node->parent_streamer->mutex_lock);
1025 gst_caps_unref(src_pad_caps);
1026 MS_SAFE_UNREF(target_pad);
1033 void ms_demux_pad_added_cb(GstElement *element, GstPad *new_pad, gpointer user_data)
1035 media_streamer_s *ms_streamer = (media_streamer_s *)user_data;
1039 ms_retm_if(ms_streamer == NULL, "user_data is NULL");
1040 ms_retm_if(new_pad == NULL, "new_pad is NULL");
1042 g_mutex_lock(&ms_streamer->mutex_lock);
1044 g_object_ref(new_pad);
1046 ms_streamer->pads_types_list = g_list_insert_sorted(ms_streamer->pads_types_list, new_pad, __pad_type_compare);
1048 g_mutex_unlock(&ms_streamer->mutex_lock);
1053 void ms_hlsdemux_pad_added_cb(GstElement *element, GstPad *new_pad, gpointer user_data)
1059 gp = GST_PAD(user_data);
1060 gst_ghost_pad_set_target(GST_GHOST_PAD(gp), new_pad);
1065 static void __demux_nomore_pads_combine(GstPad *src_pad, media_streamer_s *ms_streamer)
1067 GstElement *found_element = NULL;
1068 const gchar *new_pad_type = NULL;
1072 found_element = gst_pad_get_parent_element(src_pad);
1073 new_pad_type = ms_get_pad_type(src_pad);
1075 if (MS_ELEMENT_IS_VIDEO(new_pad_type))
1076 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER);
1077 else if (MS_ELEMENT_IS_AUDIO(new_pad_type))
1078 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_AUDIO_DECODER);
1079 else if (MS_ELEMENT_IS_TEXT(new_pad_type))
1080 found_element = ms_combine_next_element(found_element, src_pad, ms_streamer->transform_bin, MEDIA_STREAMER_NODE_TYPE_TEXT_OVERLAY);
1082 ms_error("Unsupported pad type [%s]!", new_pad_type);
1084 ms_generate_dots(ms_streamer->pipeline, "after_demux_linked");
1085 gst_object_unref(found_element);
1090 void ms_demux_nomore_pads_cb(GstElement *element, gpointer user_data)
1092 media_streamer_s *ms_streamer = (media_streamer_s *) user_data;
1093 GList *iterator = NULL;
1098 ms_retm_if(ms_streamer == NULL, "Handle is NULL");
1100 g_mutex_lock(&ms_streamer->mutex_lock);
1102 list = ms_streamer->pads_types_list;
1103 for (iterator = list; iterator; iterator = iterator->next) {
1104 GstPad *src_pad = GST_PAD(iterator->data);
1105 __demux_nomore_pads_combine(src_pad, ms_streamer);
1108 g_mutex_unlock(&ms_streamer->mutex_lock);
1113 int ms_element_set_state(GstElement *element, GstState state)
1115 GstStateChangeReturn ret_state;
1119 ms_retvm_if(element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "element is NULL");
1121 ret_state = gst_element_set_state(element, state);
1123 if (ret_state == GST_STATE_CHANGE_FAILURE) {
1124 ms_error("Failed to set element [%s] into %s state", GST_ELEMENT_NAME(element), gst_element_state_get_name(state));
1125 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
1130 return MEDIA_STREAMER_ERROR_NONE;
1133 GstElement *ms_element_create(const char *plugin_name, const char *name)
1135 GstElement *plugin_elem = NULL;
1139 ms_retvm_if(plugin_name == NULL, (GstElement *) NULL, "Error empty plugin name");
1141 ms_info("Creating [%s] element", plugin_name);
1143 plugin_elem = gst_element_factory_make(plugin_name, name);
1144 ms_retvm_if(plugin_elem == NULL, (GstElement *) NULL, "Error creating element [%s]", plugin_name);
1151 static int __ms_adaptive_sink_prepare(media_streamer_s *ms_streamer)
1153 static node_info_s nodes_info[] = {
1154 {"Codec/Encoder/Video", "video_encoder"}, /* MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER */
1155 {"Codec/Encoder/Audio", "audio_encoder"}, /* MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER */
1156 {"Codec/Muxer", "mpegtsmux"}, /* MEDIA_STREAMER_NODE_TYPE_MUXER */
1159 GstCaps *video_enc_src_caps = NULL;
1160 GstCaps *video_enc_sink_caps = NULL;
1161 GstCaps *audio_enc_src_caps = NULL;
1162 GstCaps *audio_enc_sink_caps = NULL;
1163 GstElement *audio_enc = NULL;
1164 GstElement *video_enc = NULL;
1165 GstCaps *muxer_src_caps = NULL;
1166 GstElement *muxer = NULL;
1167 node_plug_s video_enc_plug_info = {0, };
1168 node_plug_s audio_enc_plug_info = {0, };
1169 node_plug_s mux_plug_info = {0, };
1173 ms_retvm_if(ms_streamer == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer is NULL");
1175 video_enc_src_caps = gst_caps_new_simple("video/mpeg", "mpegversion", G_TYPE_INT, 4, NULL);
1176 video_enc_sink_caps = gst_caps_new_empty_simple("video/x-raw");
1177 video_enc_plug_info.info = &(nodes_info[0]);
1178 video_enc_plug_info.src_caps = video_enc_src_caps;
1179 video_enc_plug_info.sink_caps = video_enc_sink_caps;
1180 video_enc = ms_node_element_create(&video_enc_plug_info, MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER);
1182 audio_enc_src_caps = gst_caps_new_simple("audio/mpeg", "mpegversion", G_TYPE_INT, 4, NULL);
1183 audio_enc_sink_caps = gst_caps_new_empty_simple("audio/x-raw");
1184 audio_enc_plug_info.info = &(nodes_info[1]);
1185 audio_enc_plug_info.src_caps = audio_enc_src_caps;
1186 audio_enc_plug_info.sink_caps = audio_enc_sink_caps;
1187 audio_enc = ms_node_element_create(&audio_enc_plug_info, MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER);
1189 muxer_src_caps = gst_caps_new_empty_simple("video/mpegts");
1190 mux_plug_info.info = &(nodes_info[2]);
1191 mux_plug_info.src_caps = muxer_src_caps;
1192 muxer = ms_node_element_create(&mux_plug_info, MEDIA_STREAMER_NODE_TYPE_MUXER);
1194 ms_bin_add_element(ms_streamer->transform_bin, muxer, FALSE);
1195 gst_element_sync_state_with_parent(muxer);
1196 ms_bin_add_element(ms_streamer->transform_bin, video_enc, FALSE);
1197 gst_element_sync_state_with_parent(video_enc);
1198 ms_bin_add_element(ms_streamer->transform_bin, audio_enc, FALSE);
1199 gst_element_sync_state_with_parent(audio_enc);
1203 return MEDIA_STREAMER_ERROR_NONE;
1206 GstElement *ms_adaptive_element_create(void)
1208 GstElement *adaptive_bin = NULL;
1212 adaptive_bin = gst_bin_new("adaptive_src");
1213 ms_retvm_if(!adaptive_bin, (GstElement *) NULL, "Error: creating elements for adaptive source");
1215 ms_add_no_target_ghostpad(adaptive_bin, "src", GST_PAD_SRC);
1217 /* Add adaptive node parameters as GObject data with destroy function */
1218 MS_SET_INT_STATIC_STRING_PARAM(adaptive_bin, MEDIA_STREAMER_PARAM_URI, "http://localhost");
1222 return adaptive_bin;
1226 static gboolean __ms_feature_node_filter(GstPluginFeature *feature, gpointer data)
1228 node_plug_s *plug_info = (node_plug_s*)data;
1229 gboolean can_accept = FALSE;
1230 gboolean src_can_accept = FALSE;
1231 gboolean sink_can_accept = FALSE;
1232 GstElementFactory *factory = NULL;
1233 const gchar *factory_klass = NULL;
1235 if (!GST_IS_ELEMENT_FACTORY(feature))
1238 factory = GST_ELEMENT_FACTORY(feature);
1239 factory_klass = gst_element_factory_get_klass(factory);
1241 if (plug_info && g_strrstr(factory_klass, plug_info->info->klass_name)) {
1242 if (GST_IS_CAPS(plug_info->src_caps))
1243 src_can_accept = gst_element_factory_can_src_any_caps(factory, plug_info->src_caps);
1245 if (GST_IS_CAPS(plug_info->sink_caps))
1246 sink_can_accept = gst_element_factory_can_sink_any_caps(factory, plug_info->sink_caps);
1248 if (GST_IS_CAPS(plug_info->src_caps) && GST_IS_CAPS(plug_info->sink_caps)) {
1249 if (src_can_accept && sink_can_accept)
1251 } else if (src_can_accept || sink_can_accept)
1256 for ( ; plug_info->exclude_names && plug_info->exclude_names[index]; ++index) {
1257 if (g_strrstr(GST_OBJECT_NAME(factory), plug_info->exclude_names[index])) {
1258 ms_debug("Skipping compatible factory [%s] as excluded.", GST_OBJECT_NAME(factory));
1263 if (__need_to_skip_hw_video_decoder_klass(factory_klass)) {
1264 ms_debug("Skipping [%s]", GST_OBJECT_NAME(factory));
1268 ms_info("[INFO] Found compatible factory [%s] for klass [%s]",
1269 GST_OBJECT_NAME(factory), factory_klass);
1278 static GstElement *ms_element_create_from_ini(node_plug_s *plug_info, media_streamer_node_type_e type)
1280 const gchar *src_type, *sink_type;
1281 const gchar *format_type;
1282 GstElement *gst_element = NULL;
1283 gchar conf_key[INI_MAX_STRLEN] = {0,};
1284 gchar *plugin_name = NULL;
1288 ms_retvm_if(plug_info == NULL, NULL, "plug_info is NULL");
1290 MS_GET_CAPS_TYPE(plug_info->src_caps, src_type);
1291 MS_GET_CAPS_TYPE(plug_info->sink_caps, sink_type);
1293 ms_info("Specified node formats types: in[%s] - out[%s]", sink_type, src_type);
1295 if (type == MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER) {
1296 format_type = src_type;
1298 MS_GET_CAPS_TYPE(gst_caps_from_string(MEDIA_STREAMER_DEFAULT_VIDEO_FORMAT), format_type);
1299 } else if (type == MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER) {
1300 format_type = src_type;
1302 MS_GET_CAPS_TYPE(gst_caps_from_string(MEDIA_STREAMER_DEFAULT_AUDIO_FORMAT), format_type);
1303 } else if (type == MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER) {
1304 format_type = sink_type;
1306 MS_GET_CAPS_TYPE(gst_caps_from_string(MEDIA_STREAMER_DEFAULT_VIDEO_FORMAT), format_type);
1307 } else if (type == MEDIA_STREAMER_NODE_TYPE_AUDIO_DECODER) {
1308 format_type = sink_type;
1310 MS_GET_CAPS_TYPE(gst_caps_from_string(MEDIA_STREAMER_DEFAULT_AUDIO_FORMAT), format_type);
1312 format_type = sink_type ? sink_type : src_type;
1314 if (snprintf(conf_key, INI_MAX_STRLEN, "node type %d:%s", type, format_type) >= INI_MAX_STRLEN) {
1315 ms_error("Failed to generate config field name, size >= %d", INI_MAX_STRLEN);
1319 plugin_name = ms_ini_get_string(conf_key, NULL);
1322 gst_element = ms_element_create(plugin_name, NULL);
1323 MS_SAFE_GFREE(plugin_name);
1331 static GstElement *ms_element_create_by_registry(node_plug_s *plug_info, media_streamer_node_type_e type)
1333 GstElement *gst_element = NULL;
1334 const gchar *src_type = NULL;
1335 const gchar *sink_type = NULL;
1336 GList *factories = NULL;
1337 GstElementFactory *factory = NULL;
1341 ms_retvm_if(plug_info == NULL, NULL, "plug_info is NULL");
1343 MS_GET_CAPS_TYPE(plug_info->src_caps, src_type);
1344 MS_GET_CAPS_TYPE(plug_info->sink_caps, sink_type);
1346 ms_ini_read_list("general:exclude elements", &plug_info->exclude_names);
1348 factories = gst_registry_feature_filter(gst_registry_get(),
1349 __ms_feature_node_filter, FALSE, plug_info);
1350 factories = g_list_sort(factories, (GCompareFunc) __ms_factory_rank_compare);
1353 factory = GST_ELEMENT_FACTORY(factories->data);
1354 ms_info("Sorted result element is [%s]", GST_OBJECT_NAME(factory));
1355 gst_element = ms_element_create(GST_OBJECT_NAME(factory), NULL);
1357 ms_debug("Could not find any compatible element for node [%d]: in[%s] - out[%s]",
1358 type, sink_type, src_type);
1361 g_strfreev(plug_info->exclude_names);
1362 gst_plugin_list_free(factories);
1369 static GstElement *__ms_video_encoder_element_create(node_plug_s *plug_info)
1371 GstElement *video_scale = NULL;
1372 GstElement *video_convert = NULL;
1373 GstElement *encoder_elem = NULL;
1374 GstElement *encoder_parser = NULL;
1375 GstElement *encoder_bin = NULL;
1376 gboolean gst_ret = FALSE;
1377 gboolean use_zerocopy = FALSE;
1378 const gchar *src_type = NULL;
1379 node_plug_s encoder_info = {0,};
1380 node_plug_s parser_info = {0,};
1381 node_info_s node_info = {MEDIA_STREAMER_PARSER_KLASS, DEFAULT_VIDEO_PARSER};
1382 media_format_mimetype_e encoder_type = MEDIA_FORMAT_MAX;
1383 GstCaps *enc_caps = NULL;
1387 ms_retvm_if(plug_info == NULL, NULL, "plug_info is NULL");
1389 enc_caps = plug_info->src_caps;
1391 enc_caps = gst_caps_from_string(MEDIA_STREAMER_DEFAULT_VIDEO_FORMAT);
1392 ms_debug("No Video encoding format is set! Default will be: [%s]", MEDIA_STREAMER_DEFAULT_VIDEO_FORMAT);
1396 /* Creating Video Encoder */
1397 encoder_info.info = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER);
1398 encoder_info.src_caps = enc_caps;
1399 encoder_info.sink_caps = plug_info->sink_caps;
1400 encoder_info.exclude_names = NULL;
1402 encoder_elem = ms_element_create_from_ini(&encoder_info, MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER);
1404 encoder_elem = ms_element_create_by_registry(&encoder_info, MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER);
1406 /* Creating bin - Video Encoder */
1407 encoder_bin = gst_bin_new("video_encoder");
1409 if (!encoder_elem || !encoder_bin)
1412 use_zerocopy = ms_ini_get_boolean("general:use zerocopy", DEFAULT_USE_ZEROCOPY);
1414 ms_info("Video encoder uses zerocopy");
1415 gst_bin_add(GST_BIN(encoder_bin), encoder_elem);
1416 __ms_add_ghostpad(encoder_elem, "src", encoder_bin, "src");
1417 __ms_add_ghostpad(encoder_elem, "sink", encoder_bin, "sink");
1423 /* Creating Scaler, Converter */
1424 video_scale = ms_element_create(DEFAULT_VIDEO_SCALE, NULL);
1425 video_convert = ms_element_create(DEFAULT_VIDEO_CONVERT, NULL);
1427 if (!video_convert || !video_scale) {
1431 MS_GET_CAPS_TYPE(enc_caps, src_type);
1432 encoder_type = ms_convert_video_string_format_to_media_format(src_type);
1434 if (encoder_type == MEDIA_FORMAT_VP8) {
1435 /* VP8 does not have parser */
1436 g_object_set(G_OBJECT(encoder_elem), "deadline", 1, NULL);
1438 /* Adding elements to bin Video Encoder */
1439 gst_bin_add_many(GST_BIN(encoder_bin), video_convert, video_scale, encoder_elem, NULL);
1440 gst_ret = gst_element_link_many(video_convert, video_scale, encoder_elem, NULL);
1441 if (gst_ret != TRUE) {
1442 ms_error("Failed to link elements into encoder_bin");
1443 MS_SAFE_UNREF(encoder_bin);
1446 __ms_add_ghostpad(encoder_elem, "src", encoder_bin, "src");
1448 /* Creating Video Parser */
1449 parser_info.info = &node_info;
1450 parser_info.src_caps = enc_caps;
1451 parser_info.sink_caps = enc_caps;
1452 parser_info.exclude_names = NULL;
1454 encoder_parser = ms_element_create_from_ini(&parser_info, MEDIA_STREAMER_NODE_TYPE_PARSER);
1455 if (!encoder_parser)
1456 encoder_parser = ms_element_create_by_registry(&parser_info, MEDIA_STREAMER_NODE_TYPE_PARSER);
1457 if (!encoder_parser)
1460 /* Settings if H264 format is set*/
1461 if (encoder_type == MEDIA_FORMAT_H264_SP) {
1462 g_object_set(GST_OBJECT(encoder_parser), "config-interval", H264_PARSER_CONFIG_INTERVAL, NULL);
1463 g_object_set(G_OBJECT(encoder_elem), "tune", H264_ENCODER_ZEROLATENCY, NULL);
1464 g_object_set(G_OBJECT(encoder_elem), "byte-stream", TRUE, NULL);
1465 g_object_set(G_OBJECT(encoder_elem), "bitrate", 3000, NULL);
1466 g_object_set(G_OBJECT(encoder_elem), "threads", 2, NULL);
1469 /* Adding elements to bin Video Encoder */
1470 gst_bin_add_many(GST_BIN(encoder_bin), video_convert, video_scale, encoder_elem, encoder_parser, NULL);
1471 gst_ret = gst_element_link_many(video_convert, video_scale, encoder_elem, encoder_parser, NULL);
1472 if (gst_ret != TRUE) {
1473 ms_error("Failed to link elements into encoder_bin");
1474 MS_SAFE_UNREF(encoder_bin);
1477 __ms_add_ghostpad(encoder_parser, "src", encoder_bin, "src");
1479 __ms_add_ghostpad(video_convert, "sink", encoder_bin, "sink");
1486 ms_error("Error: creating elements for video encoder bin");
1487 MS_SAFE_UNREF(video_convert);
1488 MS_SAFE_UNREF(video_scale);
1489 MS_SAFE_UNREF(encoder_elem);
1490 MS_SAFE_UNREF(encoder_parser);
1491 MS_SAFE_UNREF(encoder_bin);
1498 static GstElement *__ms_video_decoder_element_create(node_plug_s *plug_info)
1500 gboolean is_hw_codec = FALSE;
1501 GstElement *last_elem = NULL;
1502 GstElement *decoder_elem = NULL;
1503 GstElement *decoder_parser = NULL;
1504 GstElement *decoder_queue = NULL;
1505 GstElement *video_conv = NULL;
1506 GstElement *video_scale = NULL;
1507 GstElement *decoder_bin = NULL;
1508 node_plug_s decoder_info = {0,};
1509 node_plug_s parser_info = {0,};
1510 node_info_s nodes_info = {MEDIA_STREAMER_PARSER_KLASS, DEFAULT_VIDEO_PARSER};
1511 media_format_mimetype_e decoder_type = MEDIA_FORMAT_MAX;
1512 gboolean gst_ret = FALSE;
1513 const gchar *sink_type = NULL;
1514 GstCaps *dec_caps = NULL;
1515 gchar *codec_name = NULL;
1519 ms_retvm_if(plug_info == NULL, NULL, "plug_info is NULL");
1521 dec_caps = plug_info->sink_caps;
1523 dec_caps = gst_caps_from_string(MEDIA_STREAMER_DEFAULT_VIDEO_FORMAT);
1524 ms_debug("No Video decoding format is set! Default will be: [%s]", MEDIA_STREAMER_DEFAULT_VIDEO_FORMAT);
1527 /* Creating Video Decoder */
1528 decoder_info.info = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER);
1529 decoder_info.src_caps = plug_info->src_caps;
1530 decoder_info.sink_caps = dec_caps;
1531 decoder_info.exclude_names = NULL;
1533 decoder_elem = ms_element_create_from_ini(&decoder_info, MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER);
1535 decoder_elem = ms_element_create_by_registry(&decoder_info, MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER);
1537 /* Creating Video Parser */
1538 parser_info.info = &nodes_info;
1539 parser_info.src_caps = dec_caps;
1540 parser_info.sink_caps = dec_caps;
1541 parser_info.exclude_names = NULL;
1543 decoder_parser = ms_element_create_from_ini(&parser_info, MEDIA_STREAMER_NODE_TYPE_PARSER);
1544 if (!decoder_parser)
1545 decoder_parser = ms_element_create_by_registry(&parser_info, MEDIA_STREAMER_NODE_TYPE_PARSER);
1547 /* Settings if H264 format is set*/
1548 MS_GET_CAPS_TYPE(dec_caps, sink_type);
1549 decoder_type = ms_convert_video_string_format_to_media_format(sink_type);
1550 if (decoder_type == MEDIA_FORMAT_H264_SP)
1551 g_object_set(G_OBJECT(decoder_parser), "config-interval", H264_PARSER_CONFIG_INTERVAL, NULL);
1553 /* Creating bin - Video Decoder */
1554 decoder_queue = ms_element_create("queue", NULL);
1555 decoder_bin = gst_bin_new("video_decoder");
1557 if (!decoder_elem || !decoder_queue || !decoder_parser || !decoder_bin) {
1558 ms_error("Error: creating elements for video decoder bin");
1562 codec_name = GST_OBJECT_NAME(decoder_elem);
1563 if (g_strrstr(codec_name, "omx") || g_strrstr(codec_name, "sprd"))
1566 /* Adding elements to bin Audio Encoder */
1567 gst_bin_add_many(GST_BIN(decoder_bin), decoder_queue, decoder_elem, decoder_parser, NULL);
1568 gst_ret = gst_element_link_many(decoder_queue, decoder_parser, decoder_elem, NULL);
1569 if (gst_ret != TRUE) {
1570 ms_error("Failed to link elements into decoder_bin");
1571 MS_SAFE_UNREF(decoder_bin);
1575 last_elem = decoder_elem;
1578 video_conv = ms_element_create("videoconvert", NULL);
1579 video_scale = ms_element_create("videoscale", NULL);
1580 if (!video_conv || !video_scale) {
1581 ms_error("Error: creating elements for video decoder bin");
1585 gst_bin_add_many(GST_BIN(decoder_bin), video_conv, video_scale, NULL);
1586 gst_ret = gst_element_link_many(decoder_elem, video_conv, video_scale, NULL);
1587 if (gst_ret != TRUE) {
1588 ms_error("Failed to link elements into decoder_bin");
1589 MS_SAFE_UNREF(decoder_bin);
1592 last_elem = video_scale;
1595 __ms_add_ghostpad(last_elem, "src", decoder_bin, "src");
1596 __ms_add_ghostpad(decoder_queue, "sink", decoder_bin, "sink");
1604 MS_SAFE_UNREF(decoder_elem);
1605 MS_SAFE_UNREF(decoder_queue);
1606 MS_SAFE_UNREF(decoder_parser);
1607 MS_SAFE_UNREF(video_conv);
1608 MS_SAFE_UNREF(video_scale);
1609 MS_SAFE_UNREF(decoder_bin);
1616 static GstElement *__ms_audio_encoder_element_create(node_plug_s *plug_info)
1618 gboolean gst_ret = FALSE;
1619 GstElement *audio_convert = NULL;
1620 GstElement *audio_resample = NULL;
1621 GstElement *audio_filter = NULL;
1622 GstElement *audio_postenc_convert = NULL;
1623 GstElement *audio_encoder = NULL;
1624 GstElement *audio_enc_bin = NULL;
1625 node_plug_s plug_info_encoder = {0,};
1626 gchar *encoder_name = NULL;
1627 GstCaps *audioCaps = NULL;
1628 GstCaps *enc_caps = NULL;
1632 ms_retvm_if(plug_info == NULL, NULL, "plug_info is NULL");
1634 enc_caps = plug_info->src_caps;
1636 enc_caps = gst_caps_from_string(MEDIA_STREAMER_DEFAULT_AUDIO_FORMAT);
1637 ms_debug("No Audio encoding format is set! Default will be: [%s]", MEDIA_STREAMER_DEFAULT_AUDIO_FORMAT);
1640 /* Creating Converter, Resampler, Filter */
1641 audio_convert = ms_element_create(DEFAULT_AUDIO_CONVERT, NULL);
1642 audio_resample = ms_element_create(DEFAULT_AUDIO_RESAMPLE, NULL);
1643 audio_filter = ms_element_create(DEFAULT_FILTER, NULL);
1644 audio_postenc_convert = ms_element_create(DEFAULT_AUDIO_CONVERT, NULL);
1646 /* Creating Audio Encoder */
1647 plug_info_encoder.info = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER);
1648 plug_info_encoder.src_caps = enc_caps;
1649 plug_info_encoder.sink_caps = plug_info->sink_caps;
1650 plug_info_encoder.exclude_names = NULL;
1652 audio_encoder = ms_element_create_from_ini(&plug_info_encoder, MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER);
1654 audio_encoder = ms_element_create_by_registry(&plug_info_encoder, MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER);
1656 encoder_name = gst_element_get_name(audio_encoder);
1657 if (encoder_name && g_strrstr(encoder_name, "aac"))
1658 g_object_set(audio_encoder, "compliance", -2, NULL);
1659 MS_SAFE_GFREE(encoder_name);
1661 /* Creating bin - Audio Encoder */
1662 audio_enc_bin = gst_bin_new("audio_encoder");
1663 if (!audio_convert || !audio_postenc_convert || !audio_filter || !audio_resample || !audio_encoder || !audio_enc_bin) {
1664 ms_error("Error: creating elements for audio encoder bin");
1668 audioCaps = gst_caps_from_string(MEDIA_STREAMER_DEFAULT_AUDIO_RAW_FORMAT);
1669 g_object_set(G_OBJECT(audio_filter), "caps", audioCaps, NULL);
1670 gst_caps_unref(audioCaps);
1672 /* Adding elements to bin Audio Encoder */
1673 gst_bin_add_many(GST_BIN(audio_enc_bin), audio_convert, audio_postenc_convert, audio_filter, audio_resample, audio_encoder, NULL);
1674 gst_ret = gst_element_link_many(audio_convert, audio_resample, audio_filter, audio_postenc_convert, audio_encoder, NULL);
1675 if (gst_ret != TRUE) {
1676 ms_error("Failed to link elements into decoder_bin");
1677 MS_SAFE_UNREF(audio_enc_bin);
1680 __ms_add_ghostpad(audio_encoder, "src", audio_enc_bin, "src");
1681 __ms_add_ghostpad(audio_convert, "sink", audio_enc_bin, "sink");
1685 return audio_enc_bin;
1689 MS_SAFE_UNREF(audio_convert);
1690 MS_SAFE_UNREF(audio_resample);
1691 MS_SAFE_UNREF(audio_postenc_convert);
1692 MS_SAFE_UNREF(audio_filter);
1693 MS_SAFE_UNREF(audio_encoder);
1694 MS_SAFE_UNREF(audio_enc_bin);
1702 static GstElement *__ms_audio_decoder_element_create(node_plug_s *plug_info)
1704 gboolean gst_ret = FALSE;
1705 GstElement *decoder_bin = NULL;
1706 GstElement *decoder_elem = NULL;
1707 GstElement *decoder_parser = NULL;
1708 GstElement *decoder_queue = NULL;
1709 GstElement *audio_conv = NULL;
1710 GstElement *audio_resample = NULL;
1711 node_plug_s decoder_info = {0,};
1712 node_plug_s parser_info = {0,};
1713 node_info_s nodes_info = {MEDIA_STREAMER_PARSER_KLASS, DEFAULT_AUDIO_PARSER};
1714 GstCaps *dec_caps = NULL;
1718 dec_caps = plug_info->sink_caps;
1720 dec_caps = gst_caps_from_string(MEDIA_STREAMER_DEFAULT_AUDIO_FORMAT);
1721 ms_debug("No Audio decoding format is set! Default will be: [%s]", MEDIA_STREAMER_DEFAULT_AUDIO_FORMAT);
1724 /* Creating Audio Decoder */
1725 decoder_info.info = ms_node_get_klass_by_its_type(MEDIA_STREAMER_NODE_TYPE_AUDIO_DECODER);
1726 decoder_info.src_caps = plug_info->src_caps;
1727 decoder_info.sink_caps = dec_caps;
1728 decoder_info.exclude_names = NULL;
1730 decoder_elem = ms_element_create_from_ini(&decoder_info, MEDIA_STREAMER_NODE_TYPE_AUDIO_DECODER);
1732 decoder_elem = ms_element_create_by_registry(&decoder_info, MEDIA_STREAMER_NODE_TYPE_AUDIO_DECODER);
1734 /* Creating Audio Parser */
1735 parser_info.info = &nodes_info;
1736 parser_info.src_caps = dec_caps;
1737 parser_info.sink_caps = dec_caps;
1738 parser_info.exclude_names = NULL;
1740 decoder_parser = ms_element_create_from_ini(&parser_info, MEDIA_STREAMER_NODE_TYPE_PARSER);
1741 if (!decoder_parser)
1742 decoder_parser = ms_element_create_by_registry(&parser_info, MEDIA_STREAMER_NODE_TYPE_PARSER);
1744 /* Creating bin - Audio Decoder */
1745 decoder_bin = gst_bin_new("audio_decoder");
1746 decoder_queue = ms_element_create("queue", NULL);
1747 if (!decoder_elem || !decoder_queue || !decoder_parser || !decoder_bin) {
1748 ms_error("Error: creating elements for audio decoder bin");
1752 /* Adding elements to bin Audio Encoder */
1753 gst_bin_add_many(GST_BIN(decoder_bin), decoder_queue, decoder_elem, decoder_parser, NULL);
1754 gst_ret = gst_element_link_many(decoder_queue, decoder_parser, decoder_elem, NULL);
1755 if (gst_ret != TRUE) {
1756 ms_error("Failed to link elements into decoder_bin");
1757 MS_SAFE_UNREF(decoder_bin);
1761 audio_conv = ms_element_create("audioconvert", NULL);
1762 audio_resample = ms_element_create("audioresample", NULL);
1763 if (!audio_conv || !audio_resample) {
1764 ms_error("Error: creating elements for audio decoder bin");
1768 gst_bin_add_many(GST_BIN(decoder_bin), audio_conv, audio_resample, NULL);
1769 gst_ret = gst_element_link_many(decoder_elem, audio_conv, audio_resample, NULL);
1770 if (gst_ret != TRUE) {
1771 ms_error("Failed to link elements into decoder_bin");
1772 MS_SAFE_UNREF(decoder_bin);
1776 __ms_add_ghostpad(audio_resample, "src", decoder_bin, "src");
1777 __ms_add_ghostpad(decoder_queue, "sink", decoder_bin, "sink");
1785 MS_SAFE_UNREF(decoder_elem);
1786 MS_SAFE_UNREF(decoder_queue);
1787 MS_SAFE_UNREF(decoder_parser);
1788 MS_SAFE_UNREF(audio_conv);
1789 MS_SAFE_UNREF(audio_resample);
1790 MS_SAFE_UNREF(decoder_bin);
1797 GstElement *ms_node_element_create(node_plug_s *plug_info, media_streamer_node_type_e type)
1799 GstElement *gst_element = NULL;
1800 const gchar *src_type = NULL;
1801 const gchar *sink_type = NULL;
1805 ms_retvm_if(plug_info == NULL, NULL, "plug_info is NULL");
1807 MS_GET_CAPS_TYPE(plug_info->src_caps, src_type);
1808 MS_GET_CAPS_TYPE(plug_info->sink_caps, sink_type);
1810 /* 1. Main priority:
1811 * If Node klass defined as MEDIA_STREAMER_STRICT or ENCODER/DECODER or CONVERTER types,
1812 * element will be created immediately by format or name */
1813 if (type == MEDIA_STREAMER_NODE_TYPE_AUDIO_ENCODER)
1814 gst_element = __ms_audio_encoder_element_create(plug_info);
1815 else if (type == MEDIA_STREAMER_NODE_TYPE_AUDIO_DECODER)
1816 gst_element = __ms_audio_decoder_element_create(plug_info);
1817 else if (type == MEDIA_STREAMER_NODE_TYPE_VIDEO_ENCODER)
1818 gst_element = __ms_video_encoder_element_create(plug_info);
1819 else if (type == MEDIA_STREAMER_NODE_TYPE_VIDEO_DECODER)
1820 gst_element = __ms_video_decoder_element_create(plug_info);
1821 else if (type == MEDIA_STREAMER_NODE_TYPE_AUDIO_CONVERTER || type == MEDIA_STREAMER_NODE_TYPE_VIDEO_CONVERTER)
1822 gst_element = ms_element_create(plug_info->info->default_name, NULL);
1823 else if (g_strrstr(MEDIA_STREAMER_STRICT, plug_info->info->klass_name) || (!src_type && !sink_type)) {
1824 if (type == MEDIA_STREAMER_NODE_TYPE_RTP)
1825 gst_element = ms_rtp_element_create();
1826 else if (type == MEDIA_STREAMER_NODE_TYPE_WEBRTC)
1827 gst_element = ms_webrtc_element_create();
1829 gst_element = ms_element_create(plug_info->info->default_name, NULL);
1832 /* 2. Second priority:
1833 * Try to get plugin name that defined in ini file
1834 * according with node type and specified format. */
1835 gst_element = ms_element_create_from_ini(plug_info, type);
1838 /* 3. Third priority:
1839 * If previous cases did not create a valid gst_element,
1840 * try to find compatible plugin in gstreamer registry.
1841 * Elements that are compatible but defined as excluded will be skipped*/
1843 /* Read exclude elements list */
1844 gst_element = ms_element_create_by_registry(plug_info, type);
1847 if (type == MEDIA_STREAMER_NODE_TYPE_FILTER) {
1848 if (plug_info->src_caps)
1849 g_object_set(G_OBJECT(gst_element), "caps", plug_info->src_caps, NULL);
1857 GstElement *ms_rtp_element_create(void)
1859 GstElement *rtp_container = NULL;
1863 rtp_container = gst_bin_new("rtp_container");
1864 ms_retvm_if(!rtp_container, (GstElement *) NULL, "Error: creating elements for rtp container");
1866 ms_add_no_target_ghostpad(rtp_container, MS_PAD_VIDEO_OUT, GST_PAD_SRC);
1867 ms_add_no_target_ghostpad(rtp_container, MS_PAD_AUDIO_OUT, GST_PAD_SRC);
1868 ms_add_no_target_ghostpad(rtp_container, MS_PAD_VIDEO_IN, GST_PAD_SINK);
1869 ms_add_no_target_ghostpad(rtp_container, MS_PAD_AUDIO_IN, GST_PAD_SINK);
1871 ms_add_no_target_ghostpad(rtp_container, MS_PAD_VIDEO_IN"-rtp", GST_PAD_SINK);
1872 ms_add_no_target_ghostpad(rtp_container, MS_PAD_AUDIO_IN"-rtp", GST_PAD_SINK);
1874 /* Add RTP node parameters as GObject data with destroy function */
1875 MS_SET_INT_PARAM(rtp_container, MEDIA_STREAMER_PARAM_VIDEO_IN_PORT, RTP_STREAM_DISABLED);
1876 MS_SET_INT_PARAM(rtp_container, MEDIA_STREAMER_PARAM_AUDIO_IN_PORT, RTP_STREAM_DISABLED);
1877 MS_SET_INT_PARAM(rtp_container, MEDIA_STREAMER_PARAM_VIDEO_OUT_PORT, RTP_STREAM_DISABLED);
1878 MS_SET_INT_PARAM(rtp_container, MEDIA_STREAMER_PARAM_AUDIO_OUT_PORT, RTP_STREAM_DISABLED);
1879 MS_SET_INT_STATIC_STRING_PARAM(rtp_container, MEDIA_STREAMER_PARAM_HOST, "localhost");
1880 MS_SET_INT_CAPS_PARAM(rtp_container, MS_PARAM_VIDEO_IN_FORMAT, gst_caps_new_any());
1881 MS_SET_INT_CAPS_PARAM(rtp_container, MS_PARAM_AUDIO_IN_FORMAT, gst_caps_new_any());
1885 return rtp_container;
1889 static gboolean __ms_parse_gst_error(media_streamer_s *ms_streamer, GstMessage *message, GError *error)
1891 media_streamer_error_e ret_error = MEDIA_STREAMER_ERROR_NONE;
1895 ms_retvm_if(!ms_streamer, FALSE, "Error: invalid Media Streamer handle.");
1896 ms_retvm_if(!message, FALSE, "Error: invalid bus message handle.");
1897 ms_retvm_if(!error, FALSE, "Error: invalid error handle.");
1899 if (error->domain == GST_CORE_ERROR)
1900 ret_error = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
1901 else if (error->domain == GST_LIBRARY_ERROR)
1902 ret_error = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
1903 else if (error->domain == GST_RESOURCE_ERROR)
1904 ret_error = MEDIA_STREAMER_ERROR_RESOURCE_CONFLICT;
1905 else if (error->domain == GST_STREAM_ERROR)
1906 ret_error = MEDIA_STREAMER_ERROR_CONNECTION_FAILED;
1908 ret_error = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
1910 /* post error to application */
1911 if (ms_streamer->error_cb.callback) {
1912 media_streamer_error_cb error_cb = (media_streamer_error_cb) ms_streamer->error_cb.callback;
1913 error_cb((media_streamer_h) ms_streamer, ret_error, ms_streamer->error_cb.user_data);
1922 static GstPadProbeReturn __ms_element_event_probe(GstPad * pad, GstPadProbeInfo *info, gpointer user_data)
1924 GstElement *parent_element = NULL;
1925 GstEvent *event = NULL;
1927 parent_element = gst_pad_get_parent_element(pad);
1928 if (!parent_element) {
1929 ms_error("filed to get parent_elem");
1930 return GST_PAD_PROBE_PASS;
1933 event = GST_PAD_PROBE_INFO_EVENT(info);
1935 if (GST_PAD_PROBE_INFO_TYPE(info) & GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM) {
1936 if (GST_EVENT_TYPE(event) == GST_EVENT_BUFFERSIZE) {
1937 GValue *val_ = (GValue *) g_object_get_data(G_OBJECT(parent_element), "pad_sink");
1938 ms_info("Set locking probe [%d] on [%s] pad of [%s] element", g_value_get_int(val_), GST_PAD_NAME(pad), GST_ELEMENT_NAME(parent_element));
1939 MS_SAFE_UNREF(parent_element);
1940 return GST_PAD_PROBE_OK;
1944 MS_SAFE_UNREF(parent_element);
1946 return GST_PAD_PROBE_PASS;
1949 gboolean ms_element_lock_state(const GValue *item, GValue *ret, gpointer user_data)
1951 GstElement *sink_element = NULL;
1952 GstPad *sink_pad = NULL;
1957 sink_element = GST_ELEMENT(g_value_get_object(item));
1958 ms_retvm_if(!sink_element, FALSE, "Handle is NULL");
1960 g_value_set_boolean(ret, FALSE);
1962 sink_pad = gst_element_get_static_pad(sink_element, "sink");
1964 ms_info("Failed to get static pad of element [%s]", GST_ELEMENT_NAME(sink_element));
1967 if (!gst_pad_is_blocked(sink_pad)) {
1968 probe_id = gst_pad_add_probe(sink_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, __ms_element_event_probe, NULL, NULL);
1969 MS_SET_INT_PARAM(sink_element, "pad_sink", probe_id);
1970 ms_info("Added locking probe [%d] on pad [%s] of element [%s]", probe_id, GST_PAD_NAME(sink_pad), GST_ELEMENT_NAME(sink_element));
1972 ms_info("Pad [%s] of element [%s] is already locked", GST_PAD_NAME(sink_pad), GST_ELEMENT_NAME(sink_element));
1974 MS_SAFE_UNREF(sink_pad);
1976 g_value_set_boolean(ret, TRUE);
1983 gboolean ms_element_unlock_state(const GValue *item, GValue *ret, gpointer user_data)
1985 GstElement *sink_element = NULL;
1987 GstPad *sink_pad = NULL;
1991 g_value_set_boolean(ret, FALSE);
1992 sink_element = GST_ELEMENT(g_value_get_object(item));
1993 ms_retvm_if(!sink_element, FALSE, "Handle is NULL");
1995 val = (GValue *) g_object_get_data(G_OBJECT(sink_element), "pad_sink");
1997 sink_pad = gst_element_get_static_pad(sink_element, "sink");
1999 ms_info("Failed to get static pad of element [%s]", GST_ELEMENT_NAME(sink_element));
2002 if (gst_pad_is_blocked(sink_pad)) {
2003 ms_info("Removing locking probe [%d] ID on [%s] pad of [%s] element", g_value_get_int(val), GST_PAD_NAME(sink_pad), GST_ELEMENT_NAME(sink_element));
2004 gst_pad_remove_probe(sink_pad, g_value_get_int(val));
2006 ms_info("No locking Probe on pad [%s] of element [%s]", GST_PAD_NAME(sink_pad), GST_ELEMENT_NAME(sink_element));
2008 MS_SAFE_UNREF(sink_pad);
2011 g_value_set_boolean(ret, TRUE);
2018 static gboolean __ms_skip_set_state(media_streamer_s *ms_streamer)
2020 media_streamer_node_s *webrtc = NULL;
2022 ms_retvm_if(ms_streamer == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer is NULL");
2023 ms_retvm_if(ms_streamer->nodes_table == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "nodes_table is NULL");
2027 webrtc = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "webrtc_container");
2028 if (webrtc && ms_streamer->pend_state == MEDIA_STREAMER_STATE_READY) {
2036 static gboolean __ms_bus_cb(GstBus *bus, GstMessage *message, gpointer userdata)
2038 int ret = MEDIA_STREAMER_ERROR_NONE;
2039 media_streamer_s *ms_streamer = NULL;
2041 gchar *debug = NULL;
2042 gchar *state_transition_name = NULL;
2043 GstState gst_state_old = GST_STATE_VOID_PENDING;
2044 GstState gst_state_new = GST_STATE_VOID_PENDING;
2045 GstState gst_state_pending = GST_STATE_VOID_PENDING;
2047 ms_streamer = (media_streamer_s *) userdata;
2048 ms_retvm_if(ms_streamer == NULL, FALSE, "Handle is NULL");
2049 ms_retvm_if(ms_streamer->pipeline == NULL, FALSE, "Pipeline doesn`t exist");
2052 if (message == NULL) {
2053 ms_debug("message is null");
2057 switch (GST_MESSAGE_TYPE(message)) {
2058 case GST_MESSAGE_ERROR:
2059 gst_message_parse_error(message, &err, &debug);
2061 /* Transform gst error code to media streamer error code.
2062 * then post it to application if needed */
2063 __ms_parse_gst_error(ms_streamer, message, err);
2065 ms_error("Error from [%s]: %s", GST_OBJECT_NAME(GST_OBJECT_CAST(GST_ELEMENT(GST_MESSAGE_SRC(message)))), err->message);
2068 MS_SAFE_GFREE(debug);
2071 case GST_MESSAGE_STATE_CHANGED:
2072 if (GST_MESSAGE_SRC(message) != GST_OBJECT(ms_streamer->pipeline))
2075 gst_message_parse_state_changed(message, &gst_state_old, &gst_state_new, &gst_state_pending);
2076 state_transition_name = g_strdup_printf("Old[GST_STATE_%s] New[GST_STATE_%s] Pending[GST_STATE_%s]",
2077 gst_element_state_get_name(gst_state_old), gst_element_state_get_name(gst_state_new),
2078 gst_element_state_get_name(gst_state_pending));
2079 ms_info("GST_MESSAGE_STATE_CHANGED: %s", state_transition_name);
2080 ms_generate_dots(ms_streamer->pipeline, state_transition_name);
2081 MS_SAFE_GFREE(state_transition_name);
2083 if (gst_state_new < GST_STATE_PAUSED)
2086 if (ms_streamer->pend_state == ms_streamer->state) {
2087 ms_info("pend_state(%d) is same with current state(%d), skip triggering callback.",
2088 ms_streamer->pend_state, ms_streamer->state);
2092 if(__ms_skip_set_state(ms_streamer)) {
2093 ms_info("Skip set state, state is set after connecting ICE connection.");
2097 ms_update_state_from_pend_state(ms_streamer);
2100 case GST_MESSAGE_ASYNC_DONE:
2101 ms_debug("GST_MESSAGE_ASYNC_DONE");
2103 if (GST_MESSAGE_SRC(message) != GST_OBJECT(ms_streamer->pipeline))
2106 if (ms_streamer->is_seeking) {
2108 g_mutex_lock(&ms_streamer->mutex_lock);
2109 ms_streamer->pend_state = MEDIA_STREAMER_STATE_SEEKING;
2110 g_mutex_unlock(&ms_streamer->mutex_lock);
2112 if (ms_streamer->seek_done_cb.callback) {
2113 media_streamer_position_changed_cb cb = (media_streamer_position_changed_cb) ms_streamer->seek_done_cb.callback;
2114 cb(ms_streamer->seek_done_cb.user_data);
2117 g_mutex_lock(&ms_streamer->mutex_lock);
2118 ms_streamer->is_seeking = FALSE;
2119 ms_streamer->pend_state = MEDIA_STREAMER_STATE_PLAYING;
2120 ms_streamer->seek_done_cb.callback = NULL;
2121 ms_streamer->seek_done_cb.user_data = NULL;
2122 g_mutex_unlock(&ms_streamer->mutex_lock);
2126 case GST_MESSAGE_EOS:
2127 ms_info("GST_MESSAGE_EOS end-of-stream");
2128 ret = ms_element_set_state(ms_streamer->pipeline, GST_STATE_PAUSED);
2129 ms_retvm_if(ret != MEDIA_STREAMER_ERROR_NONE, FALSE, "Failed to pause pipeline");
2139 static int __ms_gstreamer_init(media_streamer_s *ms_streamer)
2141 int ret = MEDIA_STREAMER_ERROR_NONE;
2145 gboolean gst_ret = 0;
2150 ms_retvm_if(ms_streamer == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Handle is NULL");
2152 argc = (int *)malloc(sizeof(int));
2154 ms_error("Error allocation memory");
2155 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2158 /* get argc(number of command line option), argc is always one without option */
2160 if (ms_streamer->ini.gst_args)
2161 (*argc) += g_strv_length(ms_streamer->ini.gst_args); /* default is "--gst-debug = 2 */
2163 argv = (char **)calloc(*argc, sizeof(char*));
2166 ms_error("Error allocation memory");
2167 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2170 argv[0] = g_strdup("MediaStreamer");
2172 if (ms_streamer->ini.gst_args) {
2173 for ( ; ms_streamer->ini.gst_args[i]; ++i) {
2175 ms_error("need to check, prevent overrun");
2178 argv[i+1] = ms_streamer->ini.gst_args[i];
2179 ms_debug("Add [%s] gstreamer parameter.", argv[i+1]);
2183 gst_ret = gst_init_check(argc, &argv, &err);
2185 for (i = 1; i < *argc; i++)
2188 MS_SAFE_FREE(argv[0]);
2193 ms_error("Error: Failed to initialize GStreamer [%s].", err->message);
2194 g_clear_error(&err);
2195 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2203 int ms_pipeline_create(media_streamer_s *ms_streamer)
2205 int ret = MEDIA_STREAMER_ERROR_NONE;
2209 ms_retvm_if(ms_streamer == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer is NULL");
2211 /* initialize gstreamer with configured parameter */
2212 ret = __ms_gstreamer_init(ms_streamer);
2213 if (ret != MEDIA_STREAMER_ERROR_NONE)
2216 ms_streamer->pipeline = gst_pipeline_new(MEDIA_STREAMER_PIPELINE_NAME);
2217 ms_retvm_if(ms_streamer->pipeline == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "Error creating pipeline");
2219 ms_streamer->bus = gst_pipeline_get_bus(GST_PIPELINE(ms_streamer->pipeline));
2220 ms_retvm_if(ms_streamer->bus == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "Error getting the bus of the pipeline");
2222 ms_streamer->bus_watcher = gst_bus_add_watch(ms_streamer->bus, (GstBusFunc) __ms_bus_cb, ms_streamer);
2224 ms_streamer->src_bin = gst_bin_new(MEDIA_STREAMER_SRC_BIN_NAME);
2225 ms_retvm_if(ms_streamer->src_bin == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "Error creating Src bin");
2227 ms_streamer->sink_bin = gst_bin_new(MEDIA_STREAMER_SINK_BIN_NAME);
2228 ms_retvm_if(ms_streamer->sink_bin == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "Error creating Sink bin");
2230 ms_streamer->transform_bin = gst_bin_new(MEDIA_STREAMER_TRANSFORM_BIN_NAME);
2231 ms_retvm_if(ms_streamer->transform_bin == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "Error creating Transform bin");
2233 gst_bin_add_many(GST_BIN(ms_streamer->pipeline), ms_streamer->src_bin, ms_streamer->sink_bin, ms_streamer->transform_bin, NULL);
2234 ms_info("Media streamer pipeline created successfully.");
2241 static gboolean __find_node(gpointer key, gpointer value, gpointer user_data)
2243 gchar *node_name = (gchar *)user_data;
2245 return g_strrstr((char *)key, node_name) != NULL;
2248 static void __ms_pending_pads_remove(void *data)
2254 ms_retm_if(data == NULL, "data is NULL");
2256 pad = GST_PAD(data);
2262 static gboolean __ms_bin_unprepare(media_streamer_s *ms_streamer, GstElement *bin)
2264 GValue element = G_VALUE_INIT;
2265 GstIterator *bin_iterator = NULL;
2266 gboolean ret = TRUE; /* If the bin doesn't have any elements, it returns TRUE */
2267 GstElement *found_element = NULL;
2268 GstIteratorResult it_res = GST_ITERATOR_ERROR;
2269 media_streamer_node_s *found_node = NULL;
2273 ms_retvm_if(ms_streamer == NULL, FALSE, "ms_streamer is NULL");
2274 ms_retvm_if(bin == NULL, FALSE, "bin is NULL");
2275 ms_retvm_if(ms_streamer->nodes_table == NULL, FALSE, "ms_streamer->nodes_table is NULL");
2277 bin_iterator = gst_bin_iterate_elements(GST_BIN(bin));
2278 it_res = gst_iterator_next(bin_iterator, &element);
2280 while (GST_ITERATOR_OK == it_res) {
2281 found_element = (GstElement *) g_value_get_object(&element);
2283 /* Get node of this element if it appears as node */
2284 found_node = (media_streamer_node_s *) g_hash_table_lookup(ms_streamer->nodes_table, GST_ELEMENT_NAME(found_element));
2286 if (!found_node->linked_by_user)
2287 ret = ret && ms_element_unlink(found_element);
2289 ms_info("Unprepare skipped user-linked node [%s]", found_node->name);
2290 ms_generate_dots(ms_streamer->pipeline, GST_ELEMENT_NAME(found_element));
2292 ret = ret && ms_bin_remove_element(found_element);
2295 g_value_reset(&element);
2297 it_res = gst_iterator_next(bin_iterator, &element);
2298 if (GST_ITERATOR_RESYNC == it_res) {
2299 gst_iterator_resync(bin_iterator);
2300 it_res = gst_iterator_next(bin_iterator, &element);
2304 g_value_unset(&element);
2305 gst_iterator_free(bin_iterator);
2312 int ms_pipeline_prepare(media_streamer_s *ms_streamer)
2314 int ret = MEDIA_STREAMER_ERROR_NONE;
2315 media_streamer_node_s *rtp = NULL;
2316 media_streamer_node_s *webrtc = NULL;
2317 media_streamer_node_s *demux = NULL;
2318 media_streamer_node_s *adaptive_src = NULL;
2319 media_streamer_node_s *adaptive_sink = NULL;
2320 gchar *find_node_name = "demux";
2324 ms_retvm_if(ms_streamer == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer is NULL");
2325 ms_retvm_if(ms_streamer->nodes_table == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer->nodes_table is NULL");
2326 ms_retvm_if(ms_streamer->src_bin == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer->src_bin is NULL");
2327 ms_retvm_if(ms_streamer->transform_bin == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer->transform_bin is NULL");
2329 rtp = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "rtp_container");
2330 webrtc = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "webrtc_container");
2331 demux = (media_streamer_node_s *)g_hash_table_find(ms_streamer->nodes_table, (GHRFunc)__find_node, find_node_name);
2332 adaptive_src = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "adaptive_src");
2333 adaptive_sink = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "adaptive_sink");
2336 ret = ms_rtp_node_prepare(rtp);
2337 } else if (webrtc) {
2338 ret = ms_webrtc_node_prepare(ms_streamer, webrtc);
2340 ret = ms_demux_node_prepare(ms_streamer, demux);
2341 if (MEDIA_STREAMER_ERROR_NONE != ret)
2342 ms_error("Failed to prepare demux element");
2344 GstBin *nodes_bin = GST_BIN(ms_streamer->src_bin);
2345 if (nodes_bin->numchildren == 0) {
2346 ms_debug(" No any node is added to [%s]", GST_ELEMENT_NAME(ms_streamer->src_bin));
2347 return MEDIA_STREAMER_ERROR_INVALID_PARAMETER;
2349 nodes_bin = GST_BIN(ms_streamer->sink_bin);
2350 if (nodes_bin->numchildren == 0) {
2351 ms_debug(" No any node is added to [%s]", GST_ELEMENT_NAME(ms_streamer->sink_bin));
2352 return MEDIA_STREAMER_ERROR_INVALID_PARAMETER;
2357 if (GST_BIN(ms_streamer->transform_bin)->numchildren == 0)
2358 ret = ms_adaptive_src_node_prepare(adaptive_src, true);
2360 ret = ms_adaptive_src_node_prepare(adaptive_src, false);
2364 ret = ms_adaptive_sink_node_prepare(ms_streamer, adaptive_sink);
2366 if (ret != MEDIA_STREAMER_ERROR_NONE)
2369 ret = ms_bin_foreach_elements(GST_BIN(ms_streamer->sink_bin), ms_sink_node_prepare_iter, ms_streamer);
2370 if (MEDIA_STREAMER_ERROR_NONE != ret) {
2371 ms_error("Failed to prepare nodes within sink bin");
2375 ret = ms_bin_foreach_elements(GST_BIN(ms_streamer->src_bin), ms_src_node_prepare_iter, ms_streamer);
2376 if (MEDIA_STREAMER_ERROR_NONE != ret) {
2377 ms_error("Failed to prepare nodes within src bin");
2381 ret = ms_set_state(ms_streamer, MEDIA_STREAMER_STATE_READY);
2382 if (ret != MEDIA_STREAMER_ERROR_NONE)
2390 ms_pipeline_unprepare(ms_streamer);
2394 int ms_pipeline_unprepare(media_streamer_s *ms_streamer)
2396 int ret = MEDIA_STREAMER_ERROR_NONE;
2400 ms_retvm_if(ms_streamer == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Handle is NULL");
2401 ms_retvm_if(ms_streamer->nodes_table == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer->nodes_table is NULL");
2403 ret = ms_set_state(ms_streamer, MEDIA_STREAMER_STATE_IDLE);
2404 if (ret != MEDIA_STREAMER_ERROR_NONE)
2405 ms_error("Failed to unprepare pipeline");
2407 if (!ms_streamer->is_interrupted) {
2408 /* FIXME: it seems possible to move codes below to ms_set_state(), need to check the history. */
2409 /* Unprepare resources in case of failure */
2410 if (ms_streamer->video_decoder_resource != NULL) {
2411 ret = mm_resource_manager_mark_for_release(ms_streamer->resource_manager,
2412 ms_streamer->video_decoder_resource);
2413 if (ret != MM_RESOURCE_MANAGER_ERROR_NONE)
2414 ms_error("Failed to mark resources for release");
2416 ret = mm_resource_manager_commit(ms_streamer->resource_manager);
2417 if (ret != MEDIA_STREAMER_ERROR_NONE)
2418 ms_error("Failed to release resources");
2420 ms_streamer->video_decoder_resource = NULL;
2424 /* Disconnects and clean all autoplug signals */
2425 g_list_free_full(ms_streamer->autoplug_sig_list, ms_signal_destroy);
2426 ms_streamer->autoplug_sig_list = NULL;
2428 /* Removes all pending pads according to list */
2429 g_list_free_full(ms_streamer->pads_types_list, __ms_pending_pads_remove);
2430 ms_streamer->pads_types_list = NULL;
2432 media_streamer_node_s *rtp_node = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "rtp_container");
2434 g_list_free_full(rtp_node->sig_list, ms_signal_destroy);
2435 rtp_node->sig_list = NULL;
2436 __ms_bin_unprepare(ms_streamer, rtp_node->gst_element);
2439 __ms_bin_unprepare(ms_streamer, ms_streamer->src_bin);
2440 __ms_bin_unprepare(ms_streamer, ms_streamer->transform_bin);
2441 __ms_bin_unprepare(ms_streamer, ms_streamer->sink_bin);
2448 static const char *g_gst_element_get_state_ret_str_arr[] = {
2449 "GST_STATE_CHANGE_FAILURE",
2450 "GST_STATE_CHANGE_SUCCESS",
2451 "GST_STATE_CHANGE_ASYNC",
2452 "GST_STATE_CHANGE_NO_PREROLL"
2455 void ms_pipeline_get_state(media_streamer_s *ms_streamer)
2457 GstState state_old = GST_STATE_NULL, state_new = GST_STATE_NULL;
2458 GstStateChangeReturn ret_state = -1;
2460 ms_retm_if(ms_streamer == NULL, "ms_streamer is NULL");
2461 ms_retm_if(ms_streamer->pipeline == NULL, "ms_streamer->pipeline is NULL");
2463 ret_state = gst_element_get_state(ms_streamer->pipeline, &state_old, &state_new, GST_CLOCK_TIME_NONE);
2464 if (ret_state == GST_STATE_CHANGE_FAILURE)
2465 ms_error("Failed to gst_element_get_state()");
2467 ms_info("[%s] Got state: old [%s], new [%s]", g_gst_element_get_state_ret_str_arr[ret_state],
2468 gst_element_state_get_name(state_old), gst_element_state_get_name(state_new));
2471 gboolean ms_pipeline_is_get_state_with_no_preroll(media_streamer_s *ms_streamer)
2473 GstState state_old = GST_STATE_NULL, state_new = GST_STATE_NULL;
2474 GstStateChangeReturn ret_state = -1;
2476 ms_retvm_if(ms_streamer == NULL, FALSE, "ms_streamer is NULL");
2477 ms_retvm_if(ms_streamer->pipeline == NULL, FALSE, "ms_streamer->pipeline is NULL");
2479 ret_state = gst_element_get_state(ms_streamer->pipeline, &state_old, &state_new, GST_CLOCK_TIME_NONE);
2480 if (ret_state == GST_STATE_CHANGE_NO_PREROLL) {
2481 ms_info("[%s] Got state: old [%s], new [%s]", g_gst_element_get_state_ret_str_arr[ret_state],
2482 gst_element_state_get_name(state_old), gst_element_state_get_name(state_new));
2489 GstCaps *ms_create_caps_from_fmt(media_format_h fmt, const char *node_name)
2491 GstCaps *caps = NULL;
2492 gchar *caps_name = NULL;
2493 media_format_mimetype_e mime;
2494 int width, height, avg_bps, max_bps, channel, samplerate, bit;
2498 if (!media_format_get_audio_info(fmt, &mime, &channel, &samplerate, &bit, &avg_bps)) {
2499 if (MEDIA_FORMAT_RAW == (mime & MEDIA_FORMAT_RAW)) {
2500 caps = gst_caps_new_simple("audio/x-raw", "format", G_TYPE_STRING, ms_convert_audio_mime_to_string_format(mime), NULL);
2501 } else if (MEDIA_FORMAT_ENCODED == (mime & MEDIA_FORMAT_ENCODED)) {
2502 if (mime == MEDIA_FORMAT_AAC)
2503 caps = gst_caps_new_simple(ms_convert_audio_mime_to_string_format(mime), "mpegversion", G_TYPE_INT, 4, NULL);
2504 else if (mime == MEDIA_FORMAT_MP3)
2505 caps = gst_caps_new_simple(ms_convert_audio_mime_to_string_format(mime), "mpegversion", G_TYPE_INT, 1, NULL);
2507 caps = gst_caps_new_simple(ms_convert_audio_mime_to_string_format(mime), "channels", G_TYPE_INT, channel, "rate", G_TYPE_INT, samplerate, NULL);
2509 caps_name = gst_caps_to_string(caps);
2510 ms_info("Creating Audio Caps from media format [%s]", caps_name);
2512 } else if (!media_format_get_video_info(fmt, &mime, &width, &height, &avg_bps, &max_bps)) {
2513 if (MEDIA_FORMAT_RAW == (mime & MEDIA_FORMAT_RAW))
2514 caps = gst_caps_new_simple("video/x-raw", "framerate", GST_TYPE_FRACTION, max_bps,
2515 avg_bps, "format", G_TYPE_STRING, ms_convert_video_mime_to_string_format(mime, node_name),
2516 "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL);
2517 else if (MEDIA_FORMAT_ENCODED == (mime & MEDIA_FORMAT_ENCODED))
2518 caps = gst_caps_new_simple(ms_convert_video_mime_to_string_format(mime, node_name), "framerate", GST_TYPE_FRACTION, max_bps,
2519 avg_bps, "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL);
2520 caps_name = gst_caps_to_string(caps);
2521 ms_info("Creating Video Caps from media format [%s]", caps_name);
2523 } else if (!media_format_get_container_mime(fmt, &mime)) {
2524 caps = gst_caps_new_empty_simple(ms_convert_container_mime_to_string_format(mime));
2525 caps_name = gst_caps_to_string(caps);
2526 ms_info("Creating Container Caps from media format [%s]", caps_name);
2529 ms_error("Error getting media format information");
2531 MS_SAFE_GFREE(caps_name);
2538 static media_format_h __ms_create_fmt_from_caps(GstCaps *caps)
2541 GstStructure *pad_struct;
2542 int fmt_ret = MEDIA_FORMAT_ERROR_NONE;
2543 const gchar *pad_type = NULL;
2544 const gchar *pad_format = NULL;
2545 int channels = 0, bps = 0;
2546 int width = 0, height = 0, avg_bps = 0, max_bps = 0;
2550 ms_retvm_if(caps == NULL, NULL, "Error: empty caps!");
2552 if (gst_caps_is_any(caps)) {
2553 ms_debug("Can not get format info from any caps!");
2557 fmt_ret = media_format_create(&fmt);
2558 ms_retvm_if(fmt_ret != MEDIA_FORMAT_ERROR_NONE, NULL, "Error: while creating media format object, err[%d]!", fmt_ret);
2560 pad_struct = gst_caps_get_structure(caps, 0);
2561 pad_type = gst_structure_get_name(pad_struct);
2562 pad_format = pad_type;
2564 /* Got raw format type if needed */
2565 if (g_strrstr(pad_type, "/x-raw"))
2566 pad_format = gst_structure_get_string(pad_struct, "format");
2568 ms_debug("Pad type is [%s], format: [%s]", pad_type, pad_format);
2570 if (MS_ELEMENT_IS_VIDEO(pad_type)) {
2571 gst_structure_get_int(pad_struct, "width", &width);
2572 gst_structure_get_fraction(pad_struct, "framerate", &max_bps, &avg_bps);
2573 gst_structure_get_int(pad_struct, "height", &height);
2575 media_format_set_video_mime(fmt, ms_convert_video_string_format_to_media_format(pad_format));
2576 media_format_set_video_width(fmt, width);
2577 media_format_set_video_height(fmt, height);
2578 media_format_set_video_avg_bps(fmt, avg_bps);
2579 media_format_set_video_max_bps(fmt, max_bps);
2580 } else if (MS_ELEMENT_IS_AUDIO(pad_type)) {
2581 media_format_set_audio_mime(fmt, ms_convert_audio_string_format_to_media_format(pad_format));
2582 gst_structure_get_int(pad_struct, "channels", &channels);
2583 media_format_set_audio_channel(fmt, channels);
2584 gst_structure_get_int(pad_struct, "rate", &bps);
2585 media_format_set_audio_avg_bps(fmt, bps);
2593 int ms_element_pad_names(GstElement *gst_element, GstPadDirection pad_type, char ***pad_name_array, int *pads_count)
2595 int ret = MEDIA_STREAMER_ERROR_NONE;
2597 GValue elem = G_VALUE_INIT;
2599 char **pad_names = NULL;
2600 GstIterator *pad_iterator = NULL;
2601 gchar *pad_name = NULL;
2605 ms_retvm_if(gst_element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "gst_element is NULL");
2606 ms_retvm_if(pad_name_array == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "pad_name_array is NULL");
2607 ms_retvm_if(pads_count == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "pads_count is NULL");
2609 if (pad_type == GST_PAD_SRC) {
2610 pad_iterator = gst_element_iterate_src_pads(gst_element);
2611 } else if (pad_type == GST_PAD_SINK) {
2612 pad_iterator = gst_element_iterate_sink_pads(gst_element);
2614 pad_iterator = gst_element_iterate_pads(gst_element);
2615 ms_info("Iterating all pads of the Gst element");
2618 while (GST_ITERATOR_OK == gst_iterator_next(pad_iterator, &elem)) {
2619 pad = (GstPad *) g_value_get_object(&elem);
2621 pad_names = (char **)realloc(pad_names, sizeof(char *) * (pad_number + 1));
2623 ms_error("Error allocation memory");
2624 ret = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2626 MS_SAFE_FREE(pad_names);
2630 pad_name = gst_pad_get_name(pad);
2631 pad_names[pad_number++] = strdup(pad_name);
2634 g_value_reset(&elem);
2637 g_value_unset(&elem);
2638 gst_iterator_free(pad_iterator);
2640 *pad_name_array = pad_names;
2641 *pads_count = pad_number;
2648 int ms_element_get_pad_fmt(GstElement *gst_element, const char *pad_name, media_format_h *fmt)
2650 int ret = MEDIA_STREAMER_ERROR_NONE;
2651 GstCaps *allowed_caps = NULL;
2652 GstCaps *format_caps = NULL;
2654 GValue *value = NULL;
2658 ms_retvm_if(gst_element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "gst_element is NULL");
2659 ms_retvm_if(pad_name == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "pad_name is NULL");
2660 ms_retvm_if(fmt == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "fmt is NULL");
2663 pad = gst_element_get_static_pad(gst_element, pad_name);
2664 ms_retvm_if(pad == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "Fail to get pad [%s] from element [%s].", pad_name, GST_ELEMENT_NAME(gst_element));
2666 value = (GValue *) g_object_get_data(G_OBJECT(gst_element), pad_name);
2668 format_caps = GST_CAPS(gst_value_get_caps(value));
2670 ms_info(" No any format is set for pad [%s]", pad_name);
2672 allowed_caps = gst_pad_get_allowed_caps(pad);
2674 if (gst_caps_is_empty(allowed_caps) || gst_caps_is_any(allowed_caps)) {
2676 *fmt = __ms_create_fmt_from_caps(format_caps);
2678 ret = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2680 *fmt = __ms_create_fmt_from_caps(allowed_caps);
2684 *fmt = __ms_create_fmt_from_caps(format_caps);
2686 ret = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2690 gst_caps_unref(allowed_caps);
2699 gboolean ms_gst_seek(GstElement *element, gint64 g_time, GstSeekFlags seek_flag)
2701 gboolean result = FALSE;
2702 GstEvent *event = NULL;
2706 ms_retvm_if(element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Element is NULL");
2708 event = gst_event_new_seek(1.0, GST_FORMAT_TIME, seek_flag,
2709 GST_SEEK_TYPE_SET, g_time,
2710 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
2712 result = gst_element_send_event(element, event);
2719 int ms_element_push_packet(GstElement *src_element, media_packet_h packet)
2721 GstBuffer *buffer = NULL;
2722 GstFlowReturn gst_ret = GST_FLOW_OK;
2723 guchar *buffer_data = NULL;
2727 ms_retvm_if(src_element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Handle is NULL");
2729 if (packet == NULL) {
2730 g_signal_emit_by_name(G_OBJECT(src_element), "end-of-stream", &gst_ret, NULL);
2731 return MEDIA_STREAMER_ERROR_NONE;
2734 media_packet_get_buffer_data_ptr(packet, (void **)&buffer_data);
2736 if (buffer_data != NULL) {
2737 GstMapInfo buff_info = GST_MAP_INFO_INIT;
2739 guint64 duration = 0;
2742 media_packet_get_buffer_size(packet, &size);
2744 buffer = gst_buffer_new_and_alloc(size);
2746 ms_error("Failed to allocate memory for push buffer");
2747 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2750 if (gst_buffer_map(buffer, &buff_info, GST_MAP_READWRITE)) {
2751 memcpy(buff_info.data, buffer_data, size);
2752 buff_info.size = size;
2753 gst_buffer_unmap(buffer, &buff_info);
2756 media_packet_get_pts(packet, &pts);
2757 GST_BUFFER_PTS(buffer) = pts;
2759 media_packet_get_duration(packet, &duration);
2760 GST_BUFFER_DURATION(buffer) = duration;
2762 g_signal_emit_by_name(G_OBJECT(src_element), "push-buffer", buffer, &gst_ret, NULL);
2763 gst_buffer_unref(buffer);
2766 g_signal_emit_by_name(G_OBJECT(src_element), "end-of-stream", &gst_ret, NULL);
2769 if (gst_ret != GST_FLOW_OK)
2770 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2774 return MEDIA_STREAMER_ERROR_NONE;
2777 int ms_element_pull_packet(GstElement *sink_element, media_packet_h *packet)
2779 int ret = MEDIA_STREAMER_ERROR_NONE;
2780 GstSample *sample = NULL;
2781 media_format_h fmt = NULL;
2782 guint8 *buffer_res = NULL;
2783 GstMapInfo map = {0,};
2784 GstBuffer *buffer = NULL;
2788 ms_retvm_if(sink_element == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Handle is NULL");
2789 ms_retvm_if(packet == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "Handle is NULL");
2791 /* Retrieve the buffer */
2792 g_signal_emit_by_name(sink_element, "pull-sample", &sample, NULL);
2793 ms_retvm_if(sample == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION, "Pull sample failed!");
2795 ret = ms_element_get_pad_fmt(sink_element, "sink", &fmt);
2796 if (ret == MEDIA_STREAMER_ERROR_NONE) {
2797 buffer = gst_sample_get_buffer(sample);
2799 ms_error("Failed to get buffer from sample");
2800 media_format_unref(fmt);
2801 gst_sample_unref(sample);
2802 return MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2804 gst_buffer_map(buffer, &map, GST_MAP_READ);
2806 buffer_res = (guint8 *) malloc(map.size * sizeof(guint8));
2807 if (buffer_res != NULL) {
2808 memcpy(buffer_res, map.data, map.size);
2810 media_packet_create_from_external_memory(fmt, (void *)buffer_res, map.size, NULL, NULL, packet);
2811 media_packet_set_pts(*packet, GST_BUFFER_PTS(buffer));
2812 media_packet_set_dts(*packet, GST_BUFFER_DTS(buffer));
2813 media_packet_set_pts(*packet, GST_BUFFER_DURATION(buffer));
2815 ms_error("Error allocation memory for packet data");
2816 ret = MEDIA_STREAMER_ERROR_INVALID_OPERATION;
2818 gst_buffer_unmap(buffer, &map);
2821 media_format_unref(fmt);
2822 gst_sample_unref(sample);
2829 static void __ms_typefound_cb(GstElement *typefind, guint probability, GstCaps *caps, gpointer data)
2831 media_streamer_s *ms_streamer = (media_streamer_s *) data;
2832 GstElement *decodebin = NULL;
2833 media_streamer_node_s *adaptive_sink = NULL;
2835 GstPad *src_pad = NULL;
2839 ms_retm_if(!typefind, "typefind is NULL");
2840 ms_retm_if(!ms_streamer, "data is NULL");
2842 adaptive_sink = (media_streamer_node_s *)g_hash_table_lookup(ms_streamer->nodes_table, "adaptive_sink");
2843 src_pad = gst_element_get_static_pad(typefind, "src");
2845 type = gst_caps_to_string(caps);
2846 if (g_strrstr(type, "video/mpegts") && adaptive_sink) {
2847 __ms_link_two_elements(typefind, src_pad, adaptive_sink->gst_element);
2849 decodebin = __ms_decodebin_create(ms_streamer, NULL);
2850 __ms_link_two_elements(typefind, src_pad, decodebin);
2853 MS_SAFE_UNREF(src_pad);
2859 int ms_find_type(media_streamer_s *ms_streamer, GstElement *src_element)
2861 GstElement *typefind = NULL;
2862 GstPad *src_pad = NULL;
2866 ms_retvm_if(!ms_streamer, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ms_streamer is NULL");
2867 ms_retvm_if(!src_element, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "src_element is NULL");
2869 src_pad = gst_element_get_static_pad(src_element, "src");
2870 ms_retvm_if(src_pad == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION,
2871 "Error getting static_pad [src_pad]");
2873 typefind = gst_element_factory_make("typefind", "typefinder");
2874 ms_retvm_if(typefind == NULL, MEDIA_STREAMER_ERROR_INVALID_OPERATION,
2875 "Error creating element [typefind]");
2877 ms_bin_add_element(ms_streamer->transform_bin, typefind, TRUE);
2879 gst_element_sync_state_with_parent(typefind);
2881 g_signal_connect(typefind, "have-type", G_CALLBACK(__ms_typefound_cb), ms_streamer);
2883 __ms_link_two_elements(src_element, src_pad, typefind);
2885 MS_SAFE_UNREF(src_pad);
2889 return MEDIA_STREAMER_ERROR_NONE;