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