1 /*
2 * GStreamer
3 * Copyright (C) 2017 Thibault Saunier <thibault.saunier@osg-samsung.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * Simple crossfade example using the compositor element.
23 *
24 * Takes two video files and crossfades them for 10 seconds and returns.
25 */
26
27 #include <stdlib.h>
28 #include <gst/gst.h>
29 #include <gst/controller/gstdirectcontrolbinding.h>
30 #include <gst/controller/gstinterpolationcontrolsource.h>
31
32 typedef struct
33 {
34 GstElement *compositor;
35 guint z_order;
36 } VideoInfo;
37
38 static gchar *
ensure_uri(const gchar * location)39 ensure_uri (const gchar * location)
40 {
41 if (gst_uri_is_valid (location))
42 return g_strdup (location);
43 else
44 return gst_filename_to_uri (location, NULL);
45 }
46
47 static void
_pad_added_cb(GstElement * decodebin,GstPad * pad,VideoInfo * info)48 _pad_added_cb (GstElement * decodebin, GstPad * pad, VideoInfo * info)
49 {
50 GstPad *sinkpad =
51 gst_element_get_request_pad (GST_ELEMENT (info->compositor), "sink_%u");
52 GstControlSource *control_source;
53 gboolean is_last = info->z_order == 1;
54
55 control_source = gst_interpolation_control_source_new ();
56
57 gst_util_set_object_arg (G_OBJECT (sinkpad), "operator",
58 info->z_order == 0 ? "source" : "add");
59 gst_object_add_control_binding (GST_OBJECT (sinkpad),
60 gst_direct_control_binding_new_absolute (GST_OBJECT (sinkpad), "alpha",
61 control_source));
62
63 g_object_set (control_source, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
64
65 gst_timed_value_control_source_set (GST_TIMED_VALUE_CONTROL_SOURCE
66 (control_source), 0, is_last ? 0.0 : 1.0);
67 gst_timed_value_control_source_set (GST_TIMED_VALUE_CONTROL_SOURCE
68 (control_source), 10 * GST_SECOND, is_last ? 1.0 : 0.0);
69 g_object_set (sinkpad, "zorder", info->z_order, NULL);
70
71 gst_pad_link (pad, sinkpad);
72
73 g_free (info);
74 }
75
76 int
main(int argc,char * argv[])77 main (int argc, char *argv[])
78 {
79 gint i;
80 GstMessage *message;
81 GstElement *compositor, *sink, *pipeline;
82 GstBus *bus;
83
84 if (argc != 3) {
85 g_error ("Need to provide 2 input videos");
86 return -1;
87 }
88
89 gst_init (&argc, &argv);
90 pipeline = gst_element_factory_make ("pipeline", NULL);
91 compositor = gst_element_factory_make ("compositor", NULL);
92 sink =
93 gst_parse_bin_from_description ("videoconvert ! autovideosink", TRUE,
94 NULL);
95
96 gst_util_set_object_arg (G_OBJECT (compositor), "background", "black");
97
98 gst_bin_add_many (GST_BIN (pipeline), compositor, sink, NULL);
99 g_assert (gst_element_link (compositor, sink));
100
101 for (i = 1; i < 3; i++) {
102 gchar *uri = ensure_uri (argv[i]);
103 VideoInfo *info = g_malloc0 (sizeof (VideoInfo));
104 GstElement *uridecodebin = gst_element_factory_make ("uridecodebin", NULL);
105
106 g_object_set (uridecodebin, "uri", uri, "expose-all-streams", FALSE,
107 "caps", gst_caps_from_string ("video/x-raw(ANY)"), NULL);
108
109 info->compositor = compositor;
110 info->z_order = i - 1;
111 g_signal_connect (uridecodebin, "pad-added", (GCallback) _pad_added_cb,
112 info);
113
114 gst_bin_add (GST_BIN (pipeline), uridecodebin);
115 }
116
117 bus = gst_element_get_bus (pipeline);
118 gst_element_set_state (pipeline, GST_STATE_PLAYING);
119
120 message =
121 gst_bus_timed_pop_filtered (bus, 11 * GST_SECOND,
122 GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
123 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
124 GST_DEBUG_GRAPH_SHOW_ALL, "go");
125 if (message)
126 gst_print ("%" GST_PTR_FORMAT "\n", message);
127 else
128 gst_print ("Timeout\n");
129 gst_element_set_state (pipeline, GST_STATE_NULL);
130 gst_object_unref (pipeline);
131
132 return 0;
133 }
134