1 /*
2 * GStreamer
3 * Copyright (C) 2017 Thibault Saunier <thibault.saunier@osg-samsung.com>
4 * Copyright (C) 2020 Jan Schmidt <jan@centricular.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * Simple example using the compositor element.
24 *
25 * Takes two video files and display them side-by-side as a mosaic
26 */
27
28 #include <stdlib.h>
29 #include <gst/gst.h>
30 #include <gst/video/video.h>
31
32 typedef struct
33 {
34 GstElement *compositor;
35 gint x, y, w, h;
36 gint zorder;
37 } VideoInfo;
38
39 static gchar *
ensure_uri(const gchar * location)40 ensure_uri (const gchar * location)
41 {
42 if (gst_uri_is_valid (location))
43 return g_strdup (location);
44 else
45 return gst_filename_to_uri (location, NULL);
46 }
47
48 static void
_pad_added_cb(GstElement * decodebin,GstPad * pad,VideoInfo * info)49 _pad_added_cb (GstElement * decodebin, GstPad * pad, VideoInfo * info)
50 {
51 GstStructure *converter_config;
52 GstPad *sinkpad =
53 gst_element_request_pad_simple (GST_ELEMENT (info->compositor),
54 "sink_%u");
55
56 converter_config = gst_structure_new ("GstVideoConverter",
57 GST_VIDEO_CONVERTER_OPT_THREADS, G_TYPE_UINT, 0,
58 GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD, GST_TYPE_VIDEO_RESAMPLER_METHOD,
59 GST_VIDEO_RESAMPLER_METHOD_NEAREST, GST_VIDEO_CONVERTER_OPT_DEST_X,
60 G_TYPE_INT, 0, GST_VIDEO_CONVERTER_OPT_DEST_Y, G_TYPE_INT, 0,
61 GST_VIDEO_CONVERTER_OPT_DEST_WIDTH, G_TYPE_INT, info->w,
62 GST_VIDEO_CONVERTER_OPT_DEST_HEIGHT, G_TYPE_INT, info->h, NULL);
63
64 g_object_set (sinkpad, "xpos", info->x, "ypos", info->y, "width", info->w,
65 "height", info->h, "converter-config", converter_config, NULL);
66
67 gst_structure_free (converter_config);
68
69 gst_pad_link (pad, sinkpad);
70
71 g_free (info);
72 }
73
74 int
main(int argc,char * argv[])75 main (int argc, char *argv[])
76 {
77 gint i;
78 GstMessage *message;
79 GstElement *compositor, *pipeline;
80 GstBus *bus;
81
82 if (argc != 3) {
83 g_error ("Need to provide 2 input videos");
84 return -1;
85 }
86
87 gst_init (&argc, &argv);
88 pipeline =
89 gst_parse_launch
90 ("videotestsrc pattern=black is-live=true ! video/x-raw,width=1,height=1,format=AYUV ! compositor name=comp start-time-selection=first ! video/x-raw,format=AYUV,width=1275,height=833,framerate=25/1 ! videoconvert ! autovideosink",
91 NULL);
92
93 g_assert (pipeline != NULL);
94 compositor = gst_bin_get_by_name (GST_BIN (pipeline), "comp");
95
96 gst_util_set_object_arg (G_OBJECT (compositor), "background", "black");
97
98 for (i = 1; i < 3; i++) {
99 gchar *uri = ensure_uri (argv[i]);
100 VideoInfo *info = g_malloc0 (sizeof (VideoInfo));
101 GstElement *uridecodebin = gst_element_factory_make ("uridecodebin", NULL);
102
103 g_object_set (uridecodebin, "uri", uri, "expose-all-streams", FALSE,
104 "caps", gst_caps_from_string ("video/x-raw(ANY)"), NULL);
105
106 info->compositor = compositor;
107 if (i == 1) {
108 info->x = 326;
109 info->y = 155;
110 info->w = 930;
111 info->h = 523;
112 info->zorder = 2;
113 } else {
114 info->x = 19;
115 info->y = 155;
116 info->w = 288;
117 info->h = 162;
118 info->zorder = 3;
119 }
120 g_signal_connect (uridecodebin, "pad-added", (GCallback) _pad_added_cb,
121 info);
122
123 gst_bin_add (GST_BIN (pipeline), uridecodebin);
124 }
125
126 bus = gst_element_get_bus (pipeline);
127 gst_element_set_state (pipeline, GST_STATE_PLAYING);
128
129 message =
130 gst_bus_timed_pop_filtered (bus, 60 * GST_SECOND,
131 GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
132 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
133 GST_DEBUG_GRAPH_SHOW_ALL | GST_DEBUG_GRAPH_SHOW_VERBOSE, "go");
134 if (message)
135 gst_print ("%" GST_PTR_FORMAT "\n", message);
136 else
137 gst_print ("Timeout\n");
138 gst_element_set_state (pipeline, GST_STATE_NULL);
139 gst_object_unref (pipeline);
140
141 return 0;
142 }
143