python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / c / gst-basic-example / gst-basic-example.c
1 /* gst-basic-example - basic example program for the GStreamer 1,0 API
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 #include <gst/gst.h>
20
21 static gboolean on_message(GstBus *bus, GstMessage *message, gpointer user_data)
22 {
23         GMainLoop *loop = (GMainLoop *)user_data;
24
25         switch (GST_MESSAGE_TYPE(message)) {
26         case GST_MESSAGE_ERROR:
27                 g_error("Got ERROR");
28                 g_main_loop_quit(loop);
29                 break;
30         case GST_MESSAGE_WARNING:
31                 g_warning("Got WARNING");
32                 g_main_loop_quit(loop);
33                 break;
34         case GST_MESSAGE_EOS:
35                 g_main_loop_quit(loop);
36                 break;
37         default:
38                 break;
39         }
40
41         return TRUE;
42 }
43
44 int main(void)
45 {
46         GstElement *pipeline, *src, *capsfilter, *conv, *sink;
47         GstCaps *caps;
48         GstBus *bus;
49         GMainLoop *loop;
50         int ret;
51
52         gst_init(NULL, NULL);
53
54         pipeline = gst_element_factory_make("pipeline", NULL);
55
56         src = gst_element_factory_make("videotestsrc", NULL);
57
58         caps = gst_caps_from_string("video/x-raw,format=AYUV,width=800,height=480");
59
60         capsfilter = gst_element_factory_make("capsfilter", NULL);
61         g_object_set(G_OBJECT(capsfilter), "caps", caps, NULL);
62
63         conv = gst_element_factory_make("videoconvert", NULL);
64
65         sink = gst_element_factory_make("autovideosink", NULL);
66         g_return_val_if_fail(sink != NULL, -1);
67
68         gst_bin_add_many(GST_BIN(pipeline), src, capsfilter, conv, sink, NULL);
69         ret = gst_element_link_many(src, capsfilter, conv, sink, NULL);
70         if (!ret) {
71                 g_error("Failed to link elements");
72                 return -2;
73         }
74
75         loop = g_main_loop_new(NULL, FALSE);
76
77         bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
78         gst_bus_add_signal_watch(bus);
79         g_signal_connect(G_OBJECT(bus), "message", G_CALLBACK(on_message),
80                          loop);
81         gst_object_unref(GST_OBJECT(bus));
82
83         ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
84         if (ret == GST_STATE_CHANGE_FAILURE) {
85                 g_error("Failed to go into PLAYING state");
86                 return -3;
87         }
88
89         g_main_loop_run(loop);
90
91         gst_element_set_state(pipeline, GST_STATE_NULL);
92
93         g_main_loop_unref(loop);
94         gst_object_unref(pipeline);
95
96         return 0;
97 }