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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #define _GNU_SOURCE
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <gst/gst.h>
43
44 static GMainLoop *loop = NULL;
45
46 static gboolean
master_bus_msg(GstBus * bus,GstMessage * msg,gpointer data)47 master_bus_msg (GstBus * bus, GstMessage * msg, gpointer data)
48 {
49 GstPipeline *pipeline = data;
50
51 switch (GST_MESSAGE_TYPE (msg)) {
52 case GST_MESSAGE_ERROR:{
53 GError *err;
54 gchar *dbg;
55
56 gst_message_parse_error (msg, &err, &dbg);
57 g_printerr ("ERROR: %s\n", err->message);
58 if (dbg != NULL)
59 g_printerr ("ERROR debug information: %s\n", dbg);
60 g_error_free (err);
61 g_free (dbg);
62
63 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
64 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.error");
65
66 g_main_loop_quit (loop);
67 break;
68 }
69 case GST_MESSAGE_WARNING:{
70 GError *err;
71 gchar *dbg;
72
73 gst_message_parse_warning (msg, &err, &dbg);
74 g_printerr ("WARNING: %s\n", err->message);
75 if (dbg != NULL)
76 g_printerr ("WARNING debug information: %s\n", dbg);
77 g_error_free (err);
78 g_free (dbg);
79
80 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
81 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.warning");
82 break;
83 }
84 case GST_MESSAGE_ASYNC_DONE:
85 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
86 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.async-done");
87 break;
88 case GST_MESSAGE_EOS:
89 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
90 g_main_loop_quit (loop);
91 break;
92 default:
93 break;
94 }
95 return TRUE;
96 }
97
98 static void
start_source(int fdin,int fdout)99 start_source (int fdin, int fdout)
100 {
101 GstElement *pipeline;
102 GstElement *source, *ipcpipelinesink, *capsfilter;
103 GstCaps *caps;
104
105 pipeline = gst_pipeline_new (NULL);
106 gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), master_bus_msg, pipeline);
107
108 source = gst_element_factory_make ("videotestsrc", NULL);
109 g_object_set (source, "pattern", 18, "num-buffers", 50, NULL);
110
111 capsfilter = gst_element_factory_make ("capsfilter", NULL);
112 caps =
113 gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT, 640, "height",
114 G_TYPE_INT, 480, NULL);
115 g_object_set (capsfilter, "caps", caps, NULL);
116 gst_caps_unref (caps);
117
118 ipcpipelinesink = gst_element_factory_make ("ipcpipelinesink", NULL);
119 g_object_set (ipcpipelinesink, "fdin", fdin, "fdout", fdout, NULL);
120
121 gst_bin_add_many (GST_BIN (pipeline), source, capsfilter, ipcpipelinesink,
122 NULL);
123 gst_element_link_many (source, capsfilter, ipcpipelinesink, NULL);
124
125 gst_element_set_state (pipeline, GST_STATE_PLAYING);
126 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
127 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.src");
128 }
129
130 static void
start_sink(int fdin,int fdout)131 start_sink (int fdin, int fdout)
132 {
133 GstElement *pipeline;
134 GstElement *ipcpipelinesrc, *navseek, *sink;
135
136 pipeline = gst_element_factory_make ("ipcslavepipeline", NULL);
137 ipcpipelinesrc = gst_element_factory_make ("ipcpipelinesrc", NULL);
138 navseek = gst_element_factory_make ("navseek", NULL);
139 g_object_set (navseek, "seek-offset", 1.0, NULL);
140 sink = gst_element_factory_make ("autovideosink", NULL);
141 g_object_set (ipcpipelinesrc, "fdin", fdin, "fdout", fdout, NULL);
142 gst_bin_add_many (GST_BIN (pipeline), ipcpipelinesrc, navseek, sink, NULL);
143 gst_element_link_many (ipcpipelinesrc, navseek, sink, NULL);
144
145 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
146 GST_DEBUG_GRAPH_SHOW_ALL, "ipc.sink");
147 /* The state of the slave pipeline will change together with the state
148 * of the master, there is no need to call gst_element_set_state() here */
149 }
150
151 static void
run(pid_t pid)152 run (pid_t pid)
153 {
154 loop = g_main_loop_new (NULL, FALSE);
155 g_main_loop_run (loop);
156 if (pid > 0)
157 kill (pid, SIGTERM);
158 }
159
160 int
main(int argc,char ** argv)161 main (int argc, char **argv)
162 {
163 int sockets[2];
164 pid_t pid;
165
166 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets)) {
167 fprintf (stderr, "Error creating sockets: %s\n", strerror (errno));
168 return 1;
169 }
170 if (fcntl (sockets[0], F_SETFL, O_NONBLOCK) < 0 ||
171 fcntl (sockets[1], F_SETFL, O_NONBLOCK) < 0) {
172 fprintf (stderr, "Error setting O_NONBLOCK on sockets: %s\n",
173 strerror (errno));
174 return 1;
175 }
176
177 pid = fork ();
178 if (pid < 0) {
179 fprintf (stderr, "Error forking: %s\n", strerror (errno));
180 return 1;
181 } else if (pid > 0) {
182 setenv ("GST_DEBUG_FILE", "gstsrc.log", 1);
183 gst_init (&argc, &argv);
184 start_source (sockets[0], sockets[0]);
185 } else {
186 setenv ("GST_DEBUG_FILE", "gstsink.log", 1);
187 gst_init (&argc, &argv);
188 start_sink (sockets[1], sockets[1]);
189 }
190
191 run (pid);
192
193 return 0;
194 }
195