92bf501ec9b0c60b6f37f37d6f8d9dc37981dada
[platform/upstream/gstreamer.git] / examples / switch / switcher.c
1 /* GStreamer
2  * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25
26 static GMainLoop *loop = NULL;
27
28
29
30 static void
31 got_eos (GstElement * pipeline)
32 {
33   g_main_loop_quit (loop);
34 }
35
36 static gboolean
37 idle_iterate (GstElement * pipeline)
38 {
39   gst_bin_iterate (GST_BIN (pipeline));
40   return (GST_STATE (GST_ELEMENT (pipeline)) == GST_STATE_PLAYING);
41 }
42
43 static gboolean
44 switch_timer (GstElement * video_switch)
45 {
46   gint nb_sources, active_source;
47
48   g_object_get (G_OBJECT (video_switch), "nb_sources", &nb_sources, NULL);
49   g_object_get (G_OBJECT (video_switch), "active_source", &active_source, NULL);
50
51   active_source++;
52
53   if (active_source > nb_sources - 1)
54     active_source = 0;
55
56   g_object_set (G_OBJECT (video_switch), "active_source", active_source, NULL);
57
58   g_message ("current number of sources : %d, active source %d",
59       nb_sources, active_source);
60
61   return (GST_STATE (GST_ELEMENT (video_switch)) == GST_STATE_PLAYING);
62 }
63
64 int
65 main (int argc, char *argv[])
66 {
67   GstElement *pipeline, *src1, *src2, *video_switch, *video_sink;
68
69   /* Initing GStreamer library */
70   gst_init (&argc, &argv);
71
72   loop = g_main_loop_new (NULL, FALSE);
73
74   pipeline = gst_pipeline_new ("pipeline");
75   src1 = gst_element_factory_make ("videotestsrc", "src1");
76   g_object_set (G_OBJECT (src1), "pattern", 0, NULL);
77   src2 = gst_element_factory_make ("videotestsrc", "src2");
78   g_object_set (G_OBJECT (src2), "pattern", 1, NULL);
79   video_switch = gst_element_factory_make ("switch", "video_switch");
80   video_sink = gst_element_factory_make (DEFAULT_VIDEOSINK, "video_sink");
81
82   gst_bin_add_many (GST_BIN (pipeline), src1, src2, video_switch,
83       video_sink, NULL);
84
85   gst_element_link (src1, video_switch);
86   gst_element_link (src2, video_switch);
87   gst_element_link (video_switch, video_sink);
88
89   g_signal_connect (G_OBJECT (pipeline), "eos", G_CALLBACK (got_eos), NULL);
90
91   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
92
93   g_idle_add ((GSourceFunc) idle_iterate, pipeline);
94   g_timeout_add (2000, (GSourceFunc) switch_timer, video_switch);
95
96   g_main_loop_run (loop);
97
98   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
99
100   /* unref */
101   gst_object_unref (GST_OBJECT (pipeline));
102
103   exit (0);
104 }