d0bc4a670c8eec94da348e4b7b7c1fb61c226e01
[platform/upstream/gst-plugins-good.git] / tests / old / 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",
50                 &active_source, NULL);
51   
52   active_source ++;
53   
54   if (active_source > nb_sources - 1)
55     active_source = 0;
56   
57   g_object_set (G_OBJECT (video_switch), "active_source",
58                 active_source, NULL);
59   
60   g_message ("current number of sources : %d, active source %d",
61              nb_sources, active_source);
62   
63   return (GST_STATE (GST_ELEMENT (video_switch)) == GST_STATE_PLAYING);
64 }
65
66 int
67 main (int argc, char *argv[])
68 {
69   GstElement *pipeline, *src1, *src2, *video_switch, *video_sink;
70
71   /* Initing GStreamer library */
72   gst_init (&argc, &argv);
73
74   loop = g_main_loop_new (NULL, FALSE);
75   
76   pipeline = gst_pipeline_new ("pipeline");
77   src1 = gst_element_factory_make ("videotestsrc", "src1");
78   g_object_set (G_OBJECT (src1), "pattern", 0, NULL);
79   src2 = gst_element_factory_make ("videotestsrc", "src2");
80   g_object_set (G_OBJECT (src2), "pattern", 1, NULL);
81   video_switch = gst_element_factory_make ("switch", "video_switch");
82   video_sink = gst_element_factory_make ("ximagesink", "video_sink");
83   
84   gst_bin_add_many (GST_BIN (pipeline), src1, src2, video_switch,
85                     video_sink, NULL);
86   
87   gst_element_link (src1, video_switch);
88   gst_element_link (src2, video_switch);
89   gst_element_link (video_switch, video_sink);
90   
91   g_signal_connect (G_OBJECT (pipeline), "eos",
92                     G_CALLBACK (got_eos), NULL);
93
94   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
95   
96   g_idle_add ((GSourceFunc) idle_iterate, pipeline);
97   g_timeout_add (2000, (GSourceFunc) switch_timer, video_switch);
98   
99   g_main_loop_run (loop);
100
101   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
102   
103   /* unref */
104   gst_object_unref (GST_OBJECT (pipeline));
105
106   exit (0);
107 }