• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
24 #include <stdlib.h>
25 
26 
27 #include <gst/gst.h>
28 
29 static GMainLoop *loop = NULL;
30 
31 static gboolean
my_bus_callback(GstBus * bus,GstMessage * message,gpointer data)32 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
33 {
34   g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));
35 
36   switch (GST_MESSAGE_TYPE (message)) {
37     case GST_MESSAGE_ERROR:{
38       GError *err;
39       gchar *debug;
40 
41       gst_message_parse_error (message, &err, &debug);
42       g_print ("Error: %s\n", err->message);
43       g_error_free (err);
44       g_free (debug);
45 
46       g_main_loop_quit (loop);
47       break;
48     }
49     case GST_MESSAGE_EOS:
50       /* end-of-stream */
51       g_main_loop_quit (loop);
52       break;
53     default:
54       /* unhandled message */
55       break;
56   }
57 
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)
61    */
62   return TRUE;
63 }
64 
65 
66 
67 static gboolean
switch_timer(GstElement * video_switch)68 switch_timer (GstElement * video_switch)
69 {
70   gint nb_sources;
71   GstPad *active_pad, *new_pad;
72   gchar *active_name;
73 
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);
77 
78   active_name = gst_pad_get_name (active_pad);
79   if (strcmp (active_name, "sink_0") == 0) {
80     new_pad = gst_element_get_static_pad (video_switch, "sink_1");
81   } else {
82     new_pad = gst_element_get_static_pad (video_switch, "sink_0");
83   }
84   g_object_set (G_OBJECT (video_switch), "active-pad", new_pad, NULL);
85   g_free (active_name);
86   gst_object_unref (new_pad);
87 
88   g_message ("current number of sources : %d, active source %s",
89       nb_sources, gst_pad_get_name (active_pad));
90 
91   return (GST_STATE (GST_ELEMENT (video_switch)) == GST_STATE_PLAYING);
92 }
93 
94 static void
last_message_received(GObject * segment)95 last_message_received (GObject * segment)
96 {
97   gchar *last_message;
98 
99   g_object_get (segment, "last_message", &last_message, NULL);
100   g_print ("last-message: %s\n", last_message);
101   g_free (last_message);
102 }
103 
104 int
main(int argc,char * argv[])105 main (int argc, char *argv[])
106 {
107   GstElement *pipeline, *src1, *src2, *video_switch, *video_sink, *segment;
108   GstElement *sink1_sync, *sink2_sync, *capsfilter;
109   GstBus *bus;
110 
111   /* Initing GStreamer library */
112   gst_init (&argc, &argv);
113 
114   loop = g_main_loop_new (NULL, FALSE);
115 
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,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);
146 
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);
151 
152   g_timeout_add (200, (GSourceFunc) switch_timer, video_switch);
153 
154   g_main_loop_run (loop);
155 
156   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
157 
158   /* unref */
159   gst_object_unref (GST_OBJECT (pipeline));
160 
161   exit (0);
162 }
163