1 /* GStreamer
2 *
3 * example program for the ipcpipelinesrc/ipcpipelinesink elements
4 *
5 * Copyright (C) 2015-2017 YouView TV Ltd
6 * Author: Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 /*
25 * This program shows a moving ball on a video sink, with the video sink
26 * running in a different process than videotestsrc.
27 */
28
29 #define _GNU_SOURCE
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <gst/gst.h>
39
40 static GMainLoop *loop = NULL;
41
42 static gboolean
master_bus_msg(GstBus * bus,GstMessage * msg,gpointer data)43 master_bus_msg (GstBus * bus, GstMessage * msg, gpointer data)
44 {
45 GstPipeline *pipeline = data;
46
47 switch (GST_MESSAGE_TYPE (msg)) {
48 case GST_MESSAGE_ERROR:{
49 GError *err;
50 gchar *dbg;
51
52 gst_message_parse_error (msg, &err, &dbg);
53 g_printerr ("ERROR: %s\n", err->message);
54 if (dbg != NULL)
55 g_printerr ("ERROR debug information: %s\n", dbg);
56 g_error_free (err);
57 g_free (dbg);
58
59 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
60 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.error");
61
62 g_main_loop_quit (loop);
63 break;
64 }
65 case GST_MESSAGE_WARNING:{
66 GError *err;
67 gchar *dbg;
68
69 gst_message_parse_warning (msg, &err, &dbg);
70 g_printerr ("WARNING: %s\n", err->message);
71 if (dbg != NULL)
72 g_printerr ("WARNING debug information: %s\n", dbg);
73 g_error_free (err);
74 g_free (dbg);
75
76 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
77 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.warning");
78 break;
79 }
80 case GST_MESSAGE_ASYNC_DONE:
81 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
82 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.async-done");
83 break;
84 case GST_MESSAGE_EOS:
85 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
86 g_main_loop_quit (loop);
87 break;
88 default:
89 break;
90 }
91 return TRUE;
92 }
93
94 static void
start_source(int fdin,int fdout)95 start_source (int fdin, int fdout)
96 {
97 GstElement *pipeline;
98 GstElement *source, *ipcpipelinesink, *capsfilter;
99 GstCaps *caps;
100
101 pipeline = gst_pipeline_new (NULL);
102 gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), master_bus_msg, pipeline);
103
104 source = gst_element_factory_make ("videotestsrc", NULL);
105 g_object_set (source, "pattern", 18, "num-buffers", 50, NULL);
106
107 capsfilter = gst_element_factory_make ("capsfilter", NULL);
108 caps =
109 gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT, 640, "height",
110 G_TYPE_INT, 480, NULL);
111 g_object_set (capsfilter, "caps", caps, NULL);
112 gst_caps_unref (caps);
113
114 ipcpipelinesink = gst_element_factory_make ("ipcpipelinesink", NULL);
115 g_object_set (ipcpipelinesink, "fdin", fdin, "fdout", fdout, NULL);
116
117 gst_bin_add_many (GST_BIN (pipeline), source, capsfilter, ipcpipelinesink,
118 NULL);
119 gst_element_link_many (source, capsfilter, ipcpipelinesink, NULL);
120
121 gst_element_set_state (pipeline, GST_STATE_PLAYING);
122 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
123 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.src");
124 }
125
126 static void
start_sink(int fdin,int fdout)127 start_sink (int fdin, int fdout)
128 {
129 GstElement *pipeline;
130 GstElement *ipcpipelinesrc, *navseek, *sink;
131
132 pipeline = gst_element_factory_make ("ipcslavepipeline", NULL);
133 ipcpipelinesrc = gst_element_factory_make ("ipcpipelinesrc", NULL);
134 navseek = gst_element_factory_make ("navseek", NULL);
135 g_object_set (navseek, "seek-offset", 1.0, NULL);
136 sink = gst_element_factory_make ("autovideosink", NULL);
137 g_object_set (ipcpipelinesrc, "fdin", fdin, "fdout", fdout, NULL);
138 gst_bin_add_many (GST_BIN (pipeline), ipcpipelinesrc, navseek, sink, NULL);
139 gst_element_link_many (ipcpipelinesrc, navseek, sink, NULL);
140
141 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
142 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.sink");
143 /* The state of the slave pipeline will change together with the state
144 * of the master, there is no need to call gst_element_set_state() here */
145 }
146
147 static void
run(pid_t pid)148 run (pid_t pid)
149 {
150 loop = g_main_loop_new (NULL, FALSE);
151 g_main_loop_run (loop);
152 if (pid > 0)
153 kill (pid, SIGTERM);
154 }
155
156 int
main(int argc,char ** argv)157 main (int argc, char **argv)
158 {
159 int sockets[2];
160 pid_t pid;
161
162 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets)) {
163 fprintf (stderr, "Error creating sockets: %s\n", strerror (errno));
164 return 1;
165 }
166 if (fcntl (sockets[0], F_SETFL, O_NONBLOCK) < 0 ||
167 fcntl (sockets[1], F_SETFL, O_NONBLOCK) < 0) {
168 fprintf (stderr, "Error setting O_NONBLOCK on sockets: %s\n",
169 strerror (errno));
170 return 1;
171 }
172
173 pid = fork ();
174 if (pid < 0) {
175 fprintf (stderr, "Error forking: %s\n", strerror (errno));
176 return 1;
177 } else if (pid > 0) {
178 setenv ("GST_DEBUG_FILE", "gstsrc.log", 1);
179 gst_init (&argc, &argv);
180 start_source (sockets[0], sockets[0]);
181 } else {
182 setenv ("GST_DEBUG_FILE", "gstsink.log", 1);
183 gst_init (&argc, &argv);
184 start_sink (sockets[1], sockets[1]);
185 }
186
187 run (pid);
188
189 return 0;
190 }
191