3 * example program for the ipcpipelinesrc/ipcpipelinesink elements
5 * Copyright (C) 2013-2014 Tim-Philipp Müller <tim centricular net>
6 * Copyright (C) 2013 Collabora Ltd.
7 * Copyright (C) 2015 Centricular Ltd
8 * Copyright (C) 2015-2017 YouView TV Ltd
9 * Author: George Kiagiadakis <george.kiagiadakis@collabora.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
28 * Based on gst-play and ipcpipeline1. This program will play any URI
29 * while splitting the pipeline in two processes, running the source & demuxer
30 * on the master process and the decoders & sinks on the slave.
31 * See keyboard_cb() for the various keyboard shortcuts you can use to
32 * interract with it while the video window is focused.
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <gst/video/navigation.h>
47 static GMainLoop *loop;
48 static int pipes[2] = { -1, -1 };
50 static const char *arg_video_sink = "autovideosink";
51 static const char *arg_audio_sink = "autoaudiosink";
53 /******* MASTER *******/
55 #define GST_PLAY_KB_ARROW_UP "\033[A"
56 #define GST_PLAY_KB_ARROW_DOWN "\033[B"
57 #define GST_PLAY_KB_ARROW_RIGHT "\033[C"
58 #define GST_PLAY_KB_ARROW_LEFT "\033[D"
62 GST_PLAY_TRICK_MODE_NONE = 0,
63 GST_PLAY_TRICK_MODE_DEFAULT,
64 GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO,
65 GST_PLAY_TRICK_MODE_KEY_UNITS,
66 GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO,
67 GST_PLAY_TRICK_MODE_LAST
70 static GstPlayTrickMode trick_mode = GST_PLAY_TRICK_MODE_NONE;
71 static gdouble cur_rate = 1.0;
72 static gboolean buffering = FALSE;
73 static GstState desired_state = GST_STATE_PLAYING;
75 static gboolean play_do_seek (GstElement * pipeline, gint64 pos, gdouble rate,
76 GstPlayTrickMode mode);
79 toggle_paused (GstElement * pipeline)
81 if (desired_state == GST_STATE_PLAYING)
82 desired_state = GST_STATE_PAUSED;
84 desired_state = GST_STATE_PLAYING;
87 gst_element_set_state (pipeline, desired_state);
88 } else if (desired_state == GST_STATE_PLAYING) {
89 g_print ("\nWill play as soon as buffering finishes)\n");
94 relative_seek (GstElement * pipeline, gdouble percent)
97 gboolean seekable = FALSE;
98 gint64 dur = -1, pos = -1, step;
100 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
102 if (!gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos))
105 query = gst_query_new_seeking (GST_FORMAT_TIME);
106 if (!gst_element_query (pipeline, query)) {
107 gst_query_unref (query);
111 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
112 gst_query_unref (query);
114 if (!seekable || dur <= 0)
117 step = dur * percent;
118 if (ABS (step) < GST_SECOND)
119 step = (percent < 0) ? -GST_SECOND : GST_SECOND;
123 g_print ("\nReached end of play list.\n");
124 g_main_loop_quit (loop);
129 play_do_seek (pipeline, pos, cur_rate, trick_mode);
136 g_print ("\nCould not seek.\n");
141 play_set_rate_and_trick_mode (GstElement * pipeline, gdouble rate,
142 GstPlayTrickMode mode)
146 g_return_val_if_fail (rate != 0, FALSE);
148 if (!gst_element_query_position (pipeline, GST_FORMAT_TIME, &pos))
151 return play_do_seek (pipeline, pos, rate, mode);
155 play_do_seek (GstElement * pipeline, gint64 pos, gdouble rate,
156 GstPlayTrickMode mode)
158 GstSeekFlags seek_flags;
161 gboolean seekable = FALSE;
163 query = gst_query_new_seeking (GST_FORMAT_TIME);
164 if (!gst_element_query (pipeline, query)) {
165 gst_query_unref (query);
169 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
170 gst_query_unref (query);
175 seek_flags = GST_SEEK_FLAG_FLUSH;
178 case GST_PLAY_TRICK_MODE_DEFAULT:
179 seek_flags |= GST_SEEK_FLAG_TRICKMODE;
181 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
182 seek_flags |= GST_SEEK_FLAG_TRICKMODE | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
184 case GST_PLAY_TRICK_MODE_KEY_UNITS:
185 seek_flags |= GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
187 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
189 GST_SEEK_FLAG_TRICKMODE_KEY_UNITS | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
191 case GST_PLAY_TRICK_MODE_NONE:
197 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
198 seek_flags | GST_SEEK_FLAG_ACCURATE,
199 /* start */ GST_SEEK_TYPE_SET, pos,
200 /* stop */ GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
202 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
203 seek_flags | GST_SEEK_FLAG_ACCURATE,
204 /* start */ GST_SEEK_TYPE_SET, 0,
205 /* stop */ GST_SEEK_TYPE_SET, pos);
207 if (!gst_element_send_event (pipeline, seek))
216 play_set_playback_rate (GstElement * pipeline, gdouble rate)
218 if (play_set_rate_and_trick_mode (pipeline, rate, trick_mode)) {
219 g_print ("Playback rate: %.2f", rate);
223 g_print ("Could not change playback rate to %.2f", rate);
229 play_set_relative_playback_rate (GstElement * pipeline, gdouble rate_step,
230 gboolean reverse_direction)
232 gdouble new_rate = cur_rate + rate_step;
234 if (reverse_direction)
237 play_set_playback_rate (pipeline, new_rate);
241 trick_mode_get_description (GstPlayTrickMode mode)
244 case GST_PLAY_TRICK_MODE_NONE:
245 return "normal playback, trick modes disabled";
246 case GST_PLAY_TRICK_MODE_DEFAULT:
247 return "trick mode: default";
248 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
249 return "trick mode: default, no audio";
250 case GST_PLAY_TRICK_MODE_KEY_UNITS:
251 return "trick mode: key frames only";
252 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
253 return "trick mode: key frames only, no audio";
257 return "unknown trick mode";
261 play_switch_trick_mode (GstElement * pipeline)
263 GstPlayTrickMode new_mode = ++trick_mode;
264 const gchar *mode_desc;
266 if (new_mode == GST_PLAY_TRICK_MODE_LAST)
267 new_mode = GST_PLAY_TRICK_MODE_NONE;
269 mode_desc = trick_mode_get_description (new_mode);
271 if (play_set_rate_and_trick_mode (pipeline, cur_rate, new_mode)) {
272 g_print ("Rate: %.2f (%s) \n", cur_rate, mode_desc);
274 g_print ("\nCould not change trick mode to %s.\n", mode_desc);
279 keyboard_cb (const gchar * key_input, GstElement * pipeline)
283 /* only want to switch/case on single char, not first char of string */
284 if (key_input[0] != '\0' && key_input[1] == '\0')
285 key = g_ascii_tolower (key_input[0]);
289 toggle_paused (pipeline);
293 g_main_loop_quit (loop);
296 if (cur_rate > -0.2 && cur_rate < 0.0)
297 play_set_relative_playback_rate (pipeline, 0.0, TRUE);
298 else if (ABS (cur_rate) < 2.0)
299 play_set_relative_playback_rate (pipeline, 0.1, FALSE);
300 else if (ABS (cur_rate) < 4.0)
301 play_set_relative_playback_rate (pipeline, 0.5, FALSE);
303 play_set_relative_playback_rate (pipeline, 1.0, FALSE);
306 if (cur_rate > 0.0 && cur_rate < 0.20)
307 play_set_relative_playback_rate (pipeline, 0.0, TRUE);
308 else if (ABS (cur_rate) <= 2.0)
309 play_set_relative_playback_rate (pipeline, -0.1, FALSE);
310 else if (ABS (cur_rate) <= 4.0)
311 play_set_relative_playback_rate (pipeline, -0.5, FALSE);
313 play_set_relative_playback_rate (pipeline, -1.0, FALSE);
316 play_set_relative_playback_rate (pipeline, 0.0, TRUE);
319 play_switch_trick_mode (pipeline);
322 if (key_input[1] == '\0') {
323 g_main_loop_quit (loop);
327 play_do_seek (pipeline, 0, cur_rate, trick_mode);
330 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
331 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.master.requested");
334 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
335 relative_seek (pipeline, +0.08);
336 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
337 relative_seek (pipeline, -0.01);
339 GST_INFO ("keyboard input:");
340 for (; *key_input != '\0'; ++key_input)
341 GST_INFO (" code %3d", *key_input);
348 master_bus_msg (GstBus * bus, GstMessage * msg, gpointer data)
350 GstPipeline *pipeline = data;
352 switch (GST_MESSAGE_TYPE (msg)) {
353 case GST_MESSAGE_ERROR:{
357 gst_message_parse_error (msg, &err, &dbg);
358 g_printerr ("MASTER: ERROR: %s\n", err->message);
360 g_printerr ("MASTER: ERROR debug information: %s\n", dbg);
364 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
365 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.master.error");
367 g_main_loop_quit (loop);
370 case GST_MESSAGE_WARNING:{
374 gst_message_parse_warning (msg, &err, &dbg);
375 g_printerr ("MASTER: WARNING: %s\n", err->message);
377 g_printerr ("MASTER: WARNING debug information: %s\n", dbg);
381 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
382 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.master.warning");
385 case GST_MESSAGE_ASYNC_DONE:
386 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
387 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.master.async-done");
389 case GST_MESSAGE_EOS:
390 g_print ("EOS on master\n");
391 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
392 g_main_loop_quit (loop);
394 case GST_MESSAGE_BUFFERING:{
396 GstBufferingMode bufmode;
401 gst_message_parse_buffering (msg, &percent);
402 g_print ("%s %d%% \r", "Buffering...", percent);
404 gst_message_parse_buffering_stats (msg, &bufmode, NULL, NULL, NULL);
406 /* no state management needed for live pipelines */
407 if (bufmode != GST_BUFFERING_LIVE) {
408 if (percent == 100) {
409 /* a 100% message means buffering is done */
412 gst_element_set_state (GST_ELEMENT (pipeline), desired_state);
413 g_print ("\n%s\n", gst_element_state_get_name (desired_state));
418 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
425 case GST_MESSAGE_CLOCK_LOST:{
426 g_print ("Clock lost, selecting a new one\n");
427 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
428 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
431 case GST_MESSAGE_LATENCY:
433 gst_bin_recalculate_latency (GST_BIN (pipeline));
436 case GST_MESSAGE_REQUEST_STATE:{
440 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
442 gst_message_parse_request_state (msg, &state);
444 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
445 GST_DEBUG_GRAPH_SHOW_VERBOSE, "ipc.slave.reqstate");
447 g_print ("Setting state to %s as requested by %s...\n",
448 gst_element_state_get_name (state), name);
450 gst_element_set_state (GST_ELEMENT (pipeline), state);
454 case GST_MESSAGE_ELEMENT:
456 GstNavigationMessageType mtype = gst_navigation_message_get_type (msg);
457 if (mtype == GST_NAVIGATION_MESSAGE_EVENT) {
460 if (gst_navigation_message_parse_event (msg, &ev)) {
461 GstNavigationEventType e_type = gst_navigation_event_get_type (ev);
463 case GST_NAVIGATION_EVENT_KEY_PRESS:
467 if (gst_navigation_event_parse_key_event (ev, &key)) {
468 GST_INFO ("Key press: %s", key);
470 if (strcmp (key, "Left") == 0)
471 key = GST_PLAY_KB_ARROW_LEFT;
472 else if (strcmp (key, "Right") == 0)
473 key = GST_PLAY_KB_ARROW_RIGHT;
474 else if (strcmp (key, "Up") == 0)
475 key = GST_PLAY_KB_ARROW_UP;
476 else if (strcmp (key, "Down") == 0)
477 key = GST_PLAY_KB_ARROW_DOWN;
478 else if (strcmp (key, "space") == 0)
480 else if (strlen (key) > 1)
483 keyboard_cb (key, GST_ELEMENT (pipeline));
487 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
490 if (gst_navigation_event_parse_mouse_button_event (ev, &button,
494 relative_seek (GST_ELEMENT (pipeline), +0.08);
495 } else if (button == 5) {
497 relative_seek (GST_ELEMENT (pipeline), -0.01);
507 gst_event_unref (ev);
518 sendfd (int s, int fd)
523 struct cmsghdr *cmsg;
525 char cms[CMSG_SPACE (sizeof (int))];
531 memset (&msg, 0, sizeof msg);
534 msg.msg_control = (caddr_t) cms;
535 msg.msg_controllen = CMSG_LEN (sizeof (int));
537 cmsg = CMSG_FIRSTHDR (&msg);
538 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
539 cmsg->cmsg_level = SOL_SOCKET;
540 cmsg->cmsg_type = SCM_RIGHTS;
541 memmove (CMSG_DATA (cmsg), &fd, sizeof (int));
543 if ((n = sendmsg (s, &msg, 0)) != iov.iov_len)
549 find_ipcpipelinesink (gconstpointer e, gconstpointer c)
551 const GValue *elem = e;
552 const gchar *caps_name = c;
553 const gchar *n = g_object_get_data (g_value_get_object (elem),
554 "ipcpipelinesink-caps-name");
555 return g_strcmp0 (caps_name, n);
558 /* in HLS the decodebin pads are destroyed and re-created every time
559 * the stream changes bitrate. This trick here ensures that the new
560 * pads that will appear will go and link to the same ipcpipelinesinks,
561 * avoiding the creation of new pipelines in the slave. */
563 on_pad_unlinked (GstPad * pad, GstPad * peer, GstElement * pipeline)
566 const GstStructure *structure;
568 caps = gst_pad_get_current_caps (pad);
569 structure = gst_caps_get_structure (caps, 0);
571 g_object_set_data_full (G_OBJECT (GST_OBJECT_PARENT (peer)),
572 "ipcpipelinesink-caps-name",
573 g_strdup (gst_structure_get_name (structure)), g_free);
577 on_pad_added (GstElement * element, GstPad * pad, GstElement * pipeline)
579 GstElement *ipcpipelinesink;
582 const GstStructure *structure;
584 GValue elem = G_VALUE_INIT;
586 gboolean create_sockets;
588 caps = gst_pad_get_current_caps (pad);
589 structure = gst_caps_get_structure (caps, 0);
591 it = gst_bin_iterate_sinks (GST_BIN (pipeline));
592 if (gst_iterator_find_custom (it, find_ipcpipelinesink, &elem,
593 (gpointer) gst_structure_get_name (structure))) {
594 ipcpipelinesink = g_value_get_object (&elem);
595 create_sockets = FALSE;
596 g_value_reset (&elem);
598 ipcpipelinesink = gst_element_factory_make ("ipcpipelinesink", NULL);
599 gst_bin_add (GST_BIN (pipeline), ipcpipelinesink);
600 create_sockets = TRUE;
603 sinkpad = gst_element_get_static_pad (ipcpipelinesink, "sink");
604 if (gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK) {
605 fprintf (stderr, "Failed to link ipcpipelinesink\n");
608 gst_object_unref (sinkpad);
610 g_signal_connect (pad, "unlinked", (GCallback) on_pad_unlinked, pipeline);
612 if (create_sockets) {
613 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets)) {
614 fprintf (stderr, "Error creating sockets: %s\n", strerror (errno));
617 if (fcntl (sockets[0], F_SETFL, O_NONBLOCK) < 0 ||
618 fcntl (sockets[1], F_SETFL, O_NONBLOCK) < 0) {
619 fprintf (stderr, "Error setting O_NONBLOCK on sockets: %s\n",
623 g_object_set (ipcpipelinesink, "fdin", sockets[0], "fdout", sockets[0],
626 printf ("new socket %d\n", sockets[1]);
627 sendfd (pipes[1], sockets[1]);
630 gst_element_set_state (ipcpipelinesink, GST_STATE_PLAYING);
632 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
633 GST_DEBUG_GRAPH_SHOW_ALL, "pad.added");
638 GST_AUTOPLUG_SELECT_TRY,
639 GST_AUTOPLUG_SELECT_EXPOSE,
640 GST_AUTOPLUG_SELECT_SKIP
641 } GstAutoplugSelectResult;
643 static GstAutoplugSelectResult
644 on_autoplug_select (GstElement * uridecodebin, GstPad * pad, GstCaps * caps,
645 GstElementFactory * factory, GstElement * pipeline)
647 /* if decodebin is about to plug a decoder,
648 * stop it right there and expose the pad;
649 * the slave's decodebin will take it from there... */
650 if (gst_element_factory_list_is_type (factory,
651 GST_ELEMENT_FACTORY_TYPE_DECODER)) {
652 gchar *capsstr = gst_caps_to_string (caps);
653 g_print (" exposing to slave: %s\n", capsstr);
655 return GST_AUTOPLUG_SELECT_EXPOSE;
657 return GST_AUTOPLUG_SELECT_TRY;
661 start_source (const gchar * uri)
663 GstElement *pipeline;
664 GstElement *uridecodebin;
667 pipeline = gst_pipeline_new (NULL);
669 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
670 gst_bus_add_watch (bus, master_bus_msg, pipeline);
671 gst_object_unref (bus);
673 uridecodebin = gst_element_factory_make ("uridecodebin", NULL);
674 g_object_set (uridecodebin, "uri", uri, NULL);
675 g_signal_connect (uridecodebin, "pad-added", G_CALLBACK (on_pad_added),
677 g_signal_connect (uridecodebin, "autoplug-select",
678 G_CALLBACK (on_autoplug_select), pipeline);
680 gst_bin_add (GST_BIN (pipeline), uridecodebin);
681 gst_element_set_state (pipeline, GST_STATE_PLAYING);
684 /*********** SLAVE ***********/
687 slave_bus_msg (GstBus * bus, GstMessage * msg, gpointer data)
689 GstPipeline *pipeline = data;
691 switch (GST_MESSAGE_TYPE (msg)) {
692 case GST_MESSAGE_ERROR:{
696 gst_message_parse_error (msg, &err, &dbg);
697 g_printerr ("SLAVE: ERROR: %s\n", err->message);
699 g_printerr ("SLAVE: ERROR debug information: %s\n", dbg);
703 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
704 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.slave.error");
707 case GST_MESSAGE_WARNING:{
711 gst_message_parse_warning (msg, &err, &dbg);
712 g_printerr ("SLAVE: WARNING: %s\n", err->message);
714 g_printerr ("SLAVE: WARNING debug information: %s\n", dbg);
718 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
719 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.slave.warning");
722 case GST_MESSAGE_ASYNC_START:
723 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
724 GST_DEBUG_GRAPH_SHOW_VERBOSE, "ipc.slave.async-start");
726 case GST_MESSAGE_ASYNC_DONE:
727 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
728 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.slave.async-done");
737 on_decoded_pad_added (GstElement * element, GstPad * pad, gpointer data)
739 GstBin *pipeline = data;
745 caps = gst_pad_get_current_caps (pad);
746 capsstr = gst_caps_to_string (caps);
747 printf (" caps: %s\n", capsstr);
750 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
751 if (!strcmp (type, "video/x-raw")) {
753 c = gst_element_factory_make ("videoconvert", NULL);
754 s = gst_element_factory_make (arg_video_sink, NULL);
755 gst_bin_add_many (GST_BIN (pipeline), c, s, NULL);
756 gst_element_link_many (c, s, NULL);
757 cpad = gst_element_get_static_pad (c, "sink");
758 gst_pad_link (pad, cpad);
759 gst_object_unref (cpad);
760 gst_element_set_state (s, GST_STATE_PLAYING);
761 gst_element_set_state (c, GST_STATE_PLAYING);
762 } else if (!strcmp (type, "audio/x-raw")) {
764 c = gst_element_factory_make ("audioconvert", NULL);
765 s = gst_element_factory_make (arg_audio_sink, NULL);
766 gst_bin_add_many (GST_BIN (pipeline), c, s, NULL);
767 gst_element_link_many (c, s, NULL);
768 cpad = gst_element_get_static_pad (c, "sink");
769 gst_pad_link (pad, cpad);
770 gst_object_unref (cpad);
771 gst_element_set_state (s, GST_STATE_PLAYING);
772 gst_element_set_state (c, GST_STATE_PLAYING);
775 s = gst_element_factory_make ("fakesink", NULL);
776 g_object_set (s, "sync", TRUE, "async", TRUE, NULL);
777 gst_bin_add_many (GST_BIN (pipeline), s, NULL);
778 cpad = gst_element_get_static_pad (s, "sink");
779 gst_pad_link (pad, cpad);
780 gst_object_unref (cpad);
781 gst_element_set_state (s, GST_STATE_PLAYING);
784 gst_caps_unref (caps);
786 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
787 GST_DEBUG_GRAPH_SHOW_ALL, "decoded.pad.added");
798 struct cmsghdr *cmsg;
799 char cms[CMSG_SPACE (sizeof (int))];
804 memset (&msg, 0, sizeof msg);
810 msg.msg_control = (caddr_t) cms;
811 msg.msg_controllen = sizeof cms;
813 if ((n = recvmsg (s, &msg, 0)) < 0)
816 perror ("unexpected EOF");
819 cmsg = CMSG_FIRSTHDR (&msg);
820 memmove (&fd, CMSG_DATA (cmsg), sizeof (int));
825 pipe_reader (gpointer data)
827 GstElement *pipeline = data;
828 GstElement *ipcpipelinesrc, *mq, *decodebin;
829 GstPad *rpad, *sink_pad;
838 FD_SET (pipes[0], &set);
839 tv.tv_sec = tv.tv_usec = 0;
840 ret = select (pipes[0] + 1, &set, NULL, NULL, &tv);
842 fprintf (stderr, "Failed to select: %s\n", strerror (errno));
845 if (!FD_ISSET (pipes[0], &set))
848 fd = recvfd (pipes[0]);
849 ipcpipelinesrc = gst_element_factory_make ("ipcpipelinesrc", NULL);
850 gst_bin_add (GST_BIN (pipeline), ipcpipelinesrc);
851 g_object_set (ipcpipelinesrc, "fdin", fd, "fdout", fd, NULL);
854 mq = gst_bin_get_by_name (GST_BIN (pipeline), "mq");
856 fprintf (stderr, "Failed to get mq\n");
859 if (!gst_element_link (ipcpipelinesrc, mq)) {
860 fprintf (stderr, "Failed to link ipcpipelinesrc and mq\n");
864 snprintf (name, sizeof (name), "src_%u", idx++);
865 rpad = gst_element_get_static_pad (mq, name);
867 fprintf (stderr, "Failed to get mq request pad\n");
871 decodebin = gst_element_factory_make ("decodebin", NULL);
872 gst_bin_add (GST_BIN (pipeline), decodebin);
873 sink_pad = gst_element_get_static_pad (decodebin, "sink");
874 gst_pad_link (rpad, sink_pad);
875 gst_object_unref (sink_pad);
877 g_signal_connect (decodebin, "pad-added", G_CALLBACK (on_decoded_pad_added),
880 /* dynamically added elements should be synced manually
881 * to the state of the slave pipeline */
882 gst_element_sync_state_with_parent (ipcpipelinesrc);
883 gst_element_sync_state_with_parent (decodebin);
885 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
886 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.slave.added");
887 gst_object_unref (mq);
895 GstElement *pipeline;
896 GstElement *multiqueue;
898 pipeline = gst_element_factory_make ("ipcslavepipeline", NULL);
899 gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), slave_bus_msg, pipeline);
901 multiqueue = gst_element_factory_make ("multiqueue", "mq");
902 gst_bin_add (GST_BIN (pipeline), multiqueue);
904 g_timeout_add (10, &pipe_reader, gst_object_ref (pipeline));
906 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
907 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.sink");
908 /* The state of the slave pipeline will change together with the state
909 * of the master, there is no need to call gst_element_set_state() here */
913 /********** COMMON ***********/
916 init (int *argc, char ***argv)
918 GOptionEntry options[] = {
919 {"audio-sink", 0, 0, G_OPTION_ARG_STRING, &arg_audio_sink,
920 "Audio sink element to use (default autoaudiosink)", NULL},
921 {"video-sink", 0, 0, G_OPTION_ARG_STRING, &arg_video_sink,
922 "Video sink element to use (default autovideosink)", NULL},
928 ctx = g_option_context_new ("");
929 g_option_context_add_main_entries (ctx, options, "");
930 if (!g_option_context_parse (ctx, argc, argv, &err)) {
931 fprintf (stderr, "Error initializing: %s\n", err->message);
934 g_option_context_free (ctx);
940 loop = g_main_loop_new (NULL, FALSE);
941 g_main_loop_run (loop);
947 main (gint argc, gchar ** argv)
949 GError *error = NULL;
956 fprintf (stderr, "usage: %s [av-filename-or-url]\n", argv[0]);
960 if (!g_strstr_len (argv[1], -1, "://")) {
961 uri = gst_filename_to_uri (argv[1], &error);
963 uri = g_strdup (argv[1]);
967 fprintf (stderr, "usage: %s [av-filename-or-url]\n", argv[0]);
968 g_clear_error (&error);
972 if (socketpair (AF_UNIX, SOCK_STREAM, 0, pipes)) {
973 fprintf (stderr, "Error creating pipes: %s\n", strerror (errno));
976 if (fcntl (pipes[0], F_SETFL, O_NONBLOCK) < 0 ||
977 fcntl (pipes[1], F_SETFL, O_NONBLOCK) < 0) {
978 fprintf (stderr, "Error setting O_NONBLOCK on pipes: %s\n",
985 fprintf (stderr, "Error forking: %s\n", strerror (errno));
987 } else if (pid > 0) {
988 setenv ("GST_DEBUG_FILE", "gstsrc.log", 1);
989 gst_init (&argc, &argv);
992 setenv ("GST_DEBUG_FILE", "gstsink.log", 1);
993 gst_init (&argc, &argv);