Add an example program for the matrix plugin
[experiments/gstreamer.git] / c / gst-matrix-example / gst-matrix-example.c
1 /* gstmatrix-example - test program for the GStreamer matrix element
2  *
3  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /* FIXME: suppress warnings for deprecated API such as GValueArray
20  * with newer GLib versions (>= 2.31.0)
21  * 
22  * It is just not possible to switch to GArray yet because the python API in
23  * 1.2.0 still passes iterable properties as GValueArray */
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
25 #include <gst/gst.h>
26
27 static gboolean on_message(GstBus *bus, GstMessage *message, gpointer user_data)
28 {
29         GMainLoop *loop = (GMainLoop *)user_data;
30
31         switch (GST_MESSAGE_TYPE(message)) {
32         case GST_MESSAGE_ERROR:
33                 g_error("Got ERROR");
34                 g_main_loop_quit(loop);
35                 break;
36         case GST_MESSAGE_WARNING:
37                 g_warning("Got WARNING");
38                 g_main_loop_quit(loop);
39                 break;
40         case GST_MESSAGE_EOS:
41                 g_main_loop_quit(loop);
42                 break;
43         default:
44                 break;
45         }
46
47         return TRUE;
48 }
49
50 static void set_matrix(GstElement *element)
51 {
52         GValueArray *va;
53         GValue v = { 0, };
54         gdouble m[9];
55         guint i;
56
57         /* Initialize a 2D perspective matrix, you can use
58          * cvGetPerspectiveTransform() from OpenCV to build it
59          * from a quad-to-quad transformation */
60         m[0] = 1.9999999999999982;
61         m[1] = 0.8333333333333287;
62         m[2] = -399.99999999999926;
63         m[3] = 3.9968028886505525e-15;
64         m[4] = 1.9999999999999978;
65         m[5] = -8.277822871605139e-13;
66         m[6] = 2.428612866367518e-18;
67         m[7] = 0.0020833333333333294;
68         m[8] = 0.9999999999999996;
69
70         va = g_value_array_new(1);
71
72         g_value_init(&v, G_TYPE_DOUBLE);
73         for (i = 0; i < 9; i++) {
74                 g_value_set_double(&v, m[i]);
75                 g_value_array_append(va, &v);
76                 g_value_reset(&v);
77         }
78         g_object_set(G_OBJECT(element), "m", va, NULL);
79         g_value_array_free(va);
80 }
81
82 int main(void)
83 {
84         GstElement *pipeline, *src, *capsfilter, *matrix, *conv, *sink;
85         GstCaps *caps;
86         GstBus *bus;
87         GMainLoop *loop;
88         int ret;
89
90         gst_init(NULL, NULL);
91
92         pipeline = gst_element_factory_make("pipeline", NULL);
93
94         src = gst_element_factory_make("videotestsrc", NULL);
95
96         caps = gst_caps_from_string("video/x-raw,format=AYUV,width=800,height=480");
97
98         capsfilter = gst_element_factory_make("capsfilter", NULL);
99         g_object_set(G_OBJECT(capsfilter), "caps", caps, NULL);
100
101         matrix = gst_element_factory_make("matrix", NULL);
102         set_matrix(matrix);
103
104         conv = gst_element_factory_make("videoconvert", NULL);
105
106         sink = gst_element_factory_make("autovideosink", NULL);
107         g_return_val_if_fail(sink != NULL, -1);
108
109         gst_bin_add_many(GST_BIN(pipeline), src, capsfilter, matrix, conv, sink, NULL);
110         ret = gst_element_link_many(src, capsfilter, matrix, conv, sink, NULL);
111         if (!ret) {
112                 g_error("Failed to link elements");
113                 return -2;
114         }
115
116         loop = g_main_loop_new(NULL, FALSE);
117
118         bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
119         gst_bus_add_signal_watch(bus);
120         g_signal_connect(G_OBJECT(bus), "message", G_CALLBACK(on_message),
121                          loop);
122         gst_object_unref(GST_OBJECT(bus));
123
124         ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
125         if (ret == GST_STATE_CHANGE_FAILURE) {
126                 g_error("Failed to go into PLAYING state");
127                 return -3;
128         }
129
130         g_main_loop_run(loop);
131
132         gst_element_set_state(pipeline, GST_STATE_NULL);
133
134         g_main_loop_unref(loop);
135         gst_object_unref(pipeline);
136
137         return 0;
138 }