• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_request_pad_simple (GST_ELEMENT (info->compositor),
52       "sink_%u");
53   GstControlSource *control_source;
54   gboolean is_last = info->z_order == 1;
55 
56   control_source = gst_interpolation_control_source_new ();
57 
58   gst_util_set_object_arg (G_OBJECT (sinkpad), "operator",
59       info->z_order == 0 ? "source" : "add");
60   gst_object_add_control_binding (GST_OBJECT (sinkpad),
61       gst_direct_control_binding_new_absolute (GST_OBJECT (sinkpad), "alpha",
62           control_source));
63 
64   g_object_set (control_source, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
65 
66   gst_timed_value_control_source_set (GST_TIMED_VALUE_CONTROL_SOURCE
67       (control_source), 0, is_last ? 0.0 : 1.0);
68   gst_timed_value_control_source_set (GST_TIMED_VALUE_CONTROL_SOURCE
69       (control_source), 10 * GST_SECOND, is_last ? 1.0 : 0.0);
70   g_object_set (sinkpad, "zorder", info->z_order, NULL);
71 
72   gst_pad_link (pad, sinkpad);
73 
74   g_free (info);
75 }
76 
77 int
main(int argc,char * argv[])78 main (int argc, char *argv[])
79 {
80   gint i;
81   GstMessage *message;
82   GstElement *compositor, *sink, *pipeline;
83   GstBus *bus;
84 
85   if (argc != 3) {
86     g_error ("Need to provide 2 input videos");
87     return -1;
88   }
89 
90   gst_init (&argc, &argv);
91   pipeline = gst_element_factory_make ("pipeline", NULL);
92   compositor = gst_element_factory_make ("compositor", NULL);
93   sink =
94       gst_parse_bin_from_description ("videoconvert ! autovideosink", TRUE,
95       NULL);
96 
97   gst_util_set_object_arg (G_OBJECT (compositor), "background", "black");
98 
99   gst_bin_add_many (GST_BIN (pipeline), compositor, sink, NULL);
100   g_assert (gst_element_link (compositor, sink));
101 
102   for (i = 1; i < 3; i++) {
103     gchar *uri = ensure_uri (argv[i]);
104     VideoInfo *info = g_malloc0 (sizeof (VideoInfo));
105     GstElement *uridecodebin = gst_element_factory_make ("uridecodebin", NULL);
106 
107     g_object_set (uridecodebin, "uri", uri, "expose-all-streams", FALSE,
108         "caps", gst_caps_from_string ("video/x-raw(ANY)"), NULL);
109 
110     info->compositor = compositor;
111     info->z_order = i - 1;
112     g_signal_connect (uridecodebin, "pad-added", (GCallback) _pad_added_cb,
113         info);
114 
115     gst_bin_add (GST_BIN (pipeline), uridecodebin);
116   }
117 
118   bus = gst_element_get_bus (pipeline);
119   gst_element_set_state (pipeline, GST_STATE_PLAYING);
120 
121   message =
122       gst_bus_timed_pop_filtered (bus, 11 * GST_SECOND,
123       GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
124   GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
125       GST_DEBUG_GRAPH_SHOW_ALL, "go");
126   if (message)
127     gst_print ("%" GST_PTR_FORMAT "\n", message);
128   else
129     gst_print ("Timeout\n");
130   gst_element_set_state (pipeline, GST_STATE_NULL);
131   gst_object_unref (pipeline);
132 
133   return 0;
134 }
135