2 * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
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.
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.
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.
29 static GMainLoop *loop = NULL;
32 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
34 g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));
36 switch (GST_MESSAGE_TYPE (message)) {
37 case GST_MESSAGE_ERROR:{
41 gst_message_parse_error (message, &err, &debug);
42 g_print ("Error: %s\n", err->message);
46 g_main_loop_quit (loop);
51 g_main_loop_quit (loop);
54 /* unhandled message */
58 /* we want to be notified again the next time there is a message
59 * on the bus, so returning TRUE (FALSE means we want to stop watching
60 * for messages on the bus and our callback should not be called again)
68 switch_timer (GstElement * video_switch)
71 GstPad *active_pad, *new_pad;
74 g_message ("switching");
75 g_object_get (G_OBJECT (video_switch), "n-pads", &nb_sources, NULL);
76 g_object_get (G_OBJECT (video_switch), "active-pad", &active_pad, NULL);
78 active_name = gst_pad_get_name (active_pad);
79 if (strcmp (active_name, "sink0") == 0) {
80 new_pad = gst_element_get_static_pad (video_switch, "sink1");
82 new_pad = gst_element_get_static_pad (video_switch, "sink0");
84 g_object_set (G_OBJECT (video_switch), "active-pad", new_pad, NULL);
86 gst_object_unref (new_pad);
88 g_message ("current number of sources : %d, active source %s",
89 nb_sources, gst_pad_get_name (active_pad));
91 return (GST_STATE (GST_ELEMENT (video_switch)) == GST_STATE_PLAYING);
95 last_message_received (GObject * segment)
99 g_object_get (segment, "last_message", &last_message, NULL);
100 g_print ("last-message: %s\n", last_message);
101 g_free (last_message);
105 main (int argc, char *argv[])
107 GstElement *pipeline, *src1, *src2, *video_switch, *video_sink, *segment;
108 GstElement *sink1_sync, *sink2_sync, *capsfilter;
111 /* Initing GStreamer library */
112 gst_init (&argc, &argv);
114 loop = g_main_loop_new (NULL, FALSE);
116 pipeline = gst_pipeline_new ("pipeline");
117 src1 = gst_element_factory_make ("videotestsrc", "src1");
118 g_object_set (G_OBJECT (src1), "pattern", 0, NULL);
119 src2 = gst_element_factory_make ("videotestsrc", "src2");
120 g_object_set (G_OBJECT (src2), "pattern", 1, NULL);
121 capsfilter = gst_element_factory_make ("capsfilter", "caps0");
122 g_object_set (G_OBJECT (capsfilter), "caps",
123 gst_caps_from_string ("video/x-raw-rgb,width=640,height=480"), NULL);
124 video_switch = gst_element_factory_make ("input-selector", "video_switch");
125 segment = gst_element_factory_make ("identity", "identity-segment");
126 g_object_set (G_OBJECT (segment), "silent", TRUE, NULL);
127 g_signal_connect (G_OBJECT (segment), "notify::last-message",
128 G_CALLBACK (last_message_received), segment);
129 g_object_set (G_OBJECT (segment), "single-segment", TRUE, NULL);
130 video_sink = gst_element_factory_make ("ximagesink", "video_sink");
131 g_object_set (G_OBJECT (video_sink), "sync", FALSE, NULL);
132 sink1_sync = gst_element_factory_make ("identity", "sink0_sync");
133 g_object_set (G_OBJECT (sink1_sync), "sync", TRUE, NULL);
134 sink2_sync = gst_element_factory_make ("identity", "sink1_sync");
135 g_object_set (G_OBJECT (sink2_sync), "sync", TRUE, NULL);
136 gst_bin_add_many (GST_BIN (pipeline), src1, src2, segment, video_switch,
137 video_sink, sink1_sync, sink2_sync, capsfilter, NULL);
138 gst_element_link (src1, sink1_sync);
139 gst_element_link (sink1_sync, video_switch);
140 gst_element_link (src2, capsfilter);
141 gst_element_link (capsfilter, sink2_sync);
142 gst_element_link (sink2_sync, video_switch);
143 gst_element_link (video_switch, segment);
144 gst_element_link (segment, /*scaler);
145 gst_element_link (scaler, */ video_sink);
147 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
148 gst_bus_add_watch (bus, my_bus_callback, NULL);
149 gst_object_unref (bus);
150 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
152 g_timeout_add (200, (GSourceFunc) switch_timer, video_switch);
154 g_main_loop_run (loop);
156 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
159 gst_object_unref (GST_OBJECT (pipeline));