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