1 #include <stdlib.h>
2 #include <gst/gst.h>
3
4 static void
event_loop(GstBus * bus,GstElement * pipe)5 event_loop (GstBus * bus, GstElement * pipe)
6 {
7 GstMessage *message = NULL;
8
9 while (TRUE) {
10 message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
11
12 g_assert (message != NULL);
13
14 switch (message->type) {
15 case GST_MESSAGE_EOS:
16 g_message ("received EOS");
17 gst_message_unref (message);
18 return;
19 case GST_MESSAGE_WARNING:{
20 GError *gerror;
21 gchar *debug;
22
23 gst_message_parse_warning (message, &gerror, &debug);
24 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
25 gst_message_unref (message);
26 g_error_free (gerror);
27 g_free (debug);
28 break;
29 }
30 case GST_MESSAGE_ERROR:{
31 GError *gerror;
32 gchar *debug;
33
34 gst_message_parse_error (message, &gerror, &debug);
35 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
36 gst_message_unref (message);
37 g_error_free (gerror);
38 g_free (debug);
39 return;
40 }
41 default:
42 gst_message_unref (message);
43 break;
44 }
45 }
46 }
47
48 static GstBusSyncReply
sync_bus_handler(GstBus * bus,GstMessage * message,GstElement * bin)49 sync_bus_handler (GstBus * bus, GstMessage * message, GstElement * bin)
50 {
51 switch (GST_MESSAGE_TYPE (message)) {
52 case GST_MESSAGE_STREAM_STATUS:
53 {
54 GstStreamStatusType type;
55 GstElement *owner;
56 const GValue *val;
57 gchar *path;
58 GstTask *task = NULL;
59
60 g_message ("received STREAM_STATUS");
61 gst_message_parse_stream_status (message, &type, &owner);
62
63 val = gst_message_get_stream_status_object (message);
64
65 g_message ("type: %d", type);
66 path = gst_object_get_path_string (GST_MESSAGE_SRC (message));
67 g_message ("source: %s", path);
68 g_free (path);
69 path = gst_object_get_path_string (GST_OBJECT (owner));
70 g_message ("owner: %s", path);
71 g_free (path);
72 g_message ("object: type %s, value %p", G_VALUE_TYPE_NAME (val),
73 g_value_get_object (val));
74
75 /* see if we know how to deal with this object */
76 if (G_VALUE_TYPE (val) == GST_TYPE_TASK) {
77 task = g_value_get_object (val);
78 }
79
80 switch (type) {
81 case GST_STREAM_STATUS_TYPE_CREATE:
82 g_message ("created task %p", task);
83 break;
84 case GST_STREAM_STATUS_TYPE_ENTER:
85 /* g_message ("raising task priority"); */
86 /* setpriority (PRIO_PROCESS, 0, -10); */
87 break;
88 case GST_STREAM_STATUS_TYPE_LEAVE:
89 break;
90 default:
91 break;
92 }
93 break;
94 }
95 default:
96 break;
97 }
98 /* pass all messages on the async queue */
99 return GST_BUS_PASS;
100 }
101
102 int
main(int argc,char * argv[])103 main (int argc, char *argv[])
104 {
105 GstElement *bin, *fakesrc, *fakesink;
106 GstBus *bus;
107
108 gst_init (&argc, &argv);
109
110 /* create a new bin to hold the elements */
111 bin = gst_pipeline_new ("pipeline");
112 g_assert (bin);
113
114 /* create a source */
115 fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
116 g_assert (fakesrc);
117 g_object_set (fakesrc, "num-buffers", 50, NULL);
118
119 /* and a sink */
120 fakesink = gst_element_factory_make ("fakesink", "fakesink");
121 g_assert (fakesink);
122
123 /* add objects to the main pipeline */
124 gst_bin_add_many (GST_BIN (bin), fakesrc, fakesink, NULL);
125
126 /* link the elements */
127 gst_element_link (fakesrc, fakesink);
128
129 /* get the bus, we need to install a sync handler */
130 bus = gst_pipeline_get_bus (GST_PIPELINE (bin));
131 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) sync_bus_handler, bin,
132 NULL);
133
134 /* start playing */
135 gst_element_set_state (bin, GST_STATE_PLAYING);
136
137 /* Run event loop listening for bus messages until EOS or ERROR */
138 event_loop (bus, bin);
139
140 /* stop the bin */
141 gst_element_set_state (bin, GST_STATE_NULL);
142 gst_object_unref (bus);
143
144 exit (0);
145 }
146