981576aed31c7473a9c6f830130a2a78de422359
[platform/upstream/gstreamer.git] / tests / old / examples / helloworld2 / helloworld2.c
1 #include <gst/gst.h>
2
3 static void
4 gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline)
5 {
6   GstElement *osssink;
7   GstElement *new_element;
8   GstAutoplug *autoplug;
9   GstElement *autobin;
10   GstElement *filesrc;
11   GstElement *cache;
12
13   GST_DEBUG ("GstPipeline: play have type");
14
15   gst_element_set_state (pipeline, GST_STATE_PAUSED);
16
17   filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
18   autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
19   cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
20
21   /* unlink_pads the typefind from the pipeline and remove it */
22   gst_element_unlink_pads (cache, "src", typefind, "sink");
23   gst_bin_remove (GST_BIN (autobin), typefind);
24       
25   /* and an audio sink */
26   osssink = gst_element_factory_make("osssink", "play_audio");
27   g_assert(osssink != NULL);
28
29   autoplug = gst_autoplug_factory_make ("staticrender");
30   g_assert (autoplug != NULL);
31
32   new_element = gst_autoplug_to_renderers (autoplug,
33            caps,
34            osssink,
35            NULL);
36
37   if (!new_element) {
38     g_print ("could not autoplug, no suitable codecs found...\n");
39     exit (-1);
40   }
41
42   gst_element_set_name (new_element, "new_element");
43
44   gst_bin_add (GST_BIN (autobin), new_element);
45
46   g_object_set (G_OBJECT (cache), "reset", TRUE, NULL);
47
48   gst_element_link_pads (cache, "src", new_element, "sink");
49
50   gst_element_set_state (pipeline, GST_STATE_PLAYING);
51 }
52
53 static void
54 gst_play_cache_empty (GstElement *element, GstElement *pipeline)
55 {
56   GstElement *autobin;
57   GstElement *filesrc;
58   GstElement *cache;
59   GstElement *new_element;
60
61   fprintf (stderr, "have cache empty\n");
62
63   gst_element_set_state (pipeline, GST_STATE_PAUSED);
64
65   filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
66   autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
67   cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
68   new_element = gst_bin_get_by_name (GST_BIN (autobin), "new_element");
69
70   gst_element_unlink_pads (filesrc, "src", cache, "sink");
71   gst_element_unlink_pads (cache, "src", new_element, "sink");
72   gst_bin_remove (GST_BIN (autobin), cache);
73   gst_element_link_pads (filesrc, "src", new_element, "sink");
74
75   gst_element_set_state (pipeline, GST_STATE_PLAYING);
76
77   fprintf (stderr, "done with cache_empty\n");
78 }
79
80 int 
81 main (int argc, char *argv[]) 
82 {
83   GstElement *filesrc;
84   GstElement *pipeline;
85   GstElement *autobin;
86   GstElement *typefind;
87   GstElement *cache;
88
89   gst_init (&argc, &argv);
90
91   if (argc != 2) {
92     g_print ("usage: %s <filename>\n", argv[0]);
93     exit (-1);
94   }
95
96   /* create a new pipeline to hold the elements */
97   pipeline = gst_pipeline_new ("pipeline");
98   g_assert (pipeline != NULL);
99
100   /* create a disk reader */
101   filesrc = gst_element_factory_make ("filesrc", "disk_source");
102   g_assert (filesrc != NULL);
103   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
104   gst_bin_add (GST_BIN (pipeline), filesrc);
105
106   autobin = gst_bin_new ("autobin");
107   cache = gst_element_factory_make ("autoplugcache", "cache");
108   g_signal_connect (G_OBJECT (cache), "cache_empty", 
109                      G_CALLBACK (gst_play_cache_empty), pipeline);
110
111   typefind = gst_element_factory_make ("typefind", "typefind");
112   g_signal_connect (G_OBJECT (typefind), "have_type", 
113                      G_CALLBACK (gst_play_have_type), pipeline);
114   gst_bin_add (GST_BIN (autobin), cache);
115   gst_bin_add (GST_BIN (autobin), typefind);
116
117   gst_element_link_pads (cache, "src", typefind, "sink");
118   gst_element_add_ghost_pad (autobin, gst_element_get_pad (cache, "sink"), "sink");
119
120   gst_bin_add (GST_BIN( pipeline), autobin);
121   gst_element_link_pads (filesrc, "src", autobin, "sink");
122
123   /* start playing */
124   gst_element_set_state( GST_ELEMENT (pipeline), GST_STATE_PLAYING);
125
126   while (gst_bin_iterate (GST_BIN (pipeline)));
127
128   /* stop the pipeline */
129   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
130
131   gst_object_unref (GST_OBJECT (pipeline));
132
133   exit(0);
134 }
135