1 #import "GStreamerBackend.h"
5 GST_DEBUG_CATEGORY_STATIC (debug_category);
6 #define GST_CAT_DEFAULT debug_category
8 @interface GStreamerBackend()
9 -(void)setUIMessage:(gchar*) message;
11 -(void)check_initialization_complete;
14 @implementation GStreamerBackend {
15 id ui_delegate; /* Class that we use to interact with the user interface */
16 GstElement *pipeline; /* The running pipeline */
17 GMainContext *context; /* GLib context used to run the main loop */
18 GMainLoop *main_loop; /* GLib main loop */
19 gboolean initialized; /* To avoid informing the UI multiple times about the initialization */
26 -(id) init:(id) uiDelegate
28 if (self = [super init])
30 self->ui_delegate = uiDelegate;
32 GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-2", 0, "iOS tutorial 2");
33 gst_debug_set_threshold_for_name("tutorial-2", GST_LEVEL_DEBUG);
35 /* Start the bus monitoring task */
36 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
47 GST_DEBUG("Setting the pipeline to NULL");
48 gst_element_set_state(pipeline, GST_STATE_NULL);
49 gst_object_unref(pipeline);
56 if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
57 [self setUIMessage:"Failed to set pipeline to playing"];
63 if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
64 [self setUIMessage:"Failed to set pipeline to paused"];
72 /* Change the message on the UI through the UI delegate */
73 -(void)setUIMessage:(gchar*) message
75 NSString *string = [NSString stringWithUTF8String:message];
76 if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)])
78 [ui_delegate gstreamerSetUIMessage:string];
82 /* Retrieve errors from the bus and show them on the UI */
83 static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
87 gchar *message_string;
89 gst_message_parse_error (msg, &err, &debug_info);
90 message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message);
93 [self setUIMessage:message_string];
94 g_free (message_string);
95 gst_element_set_state (self->pipeline, GST_STATE_NULL);
98 /* Notify UI about pipeline state changes */
99 static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
101 GstState old_state, new_state, pending_state;
102 gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
103 /* Only pay attention to messages coming from the pipeline, not its children */
104 if (GST_MESSAGE_SRC (msg) == GST_OBJECT (self->pipeline)) {
105 gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state));
106 [self setUIMessage:message];
111 /* Check if all conditions are met to report GStreamer as initialized.
112 * These conditions will change depending on the application */
113 -(void) check_initialization_complete
115 if (!initialized && main_loop) {
116 GST_DEBUG ("Initialization complete, notifying application.");
117 if (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)])
119 [ui_delegate gstreamerInitialized];
125 /* Main method for the bus monitoring code */
130 GError *error = NULL;
132 GST_DEBUG ("Creating pipeline");
134 /* Create our own GLib Main Context and make it the default one */
135 context = g_main_context_new ();
136 g_main_context_push_thread_default(context);
139 pipeline = gst_parse_launch("audiotestsrc ! audioconvert ! audioresample ! autoaudiosink", &error);
141 gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message);
142 g_clear_error (&error);
143 [self setUIMessage:message];
148 /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
149 bus = gst_element_get_bus (pipeline);
150 bus_source = gst_bus_create_watch (bus);
151 g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL);
152 g_source_attach (bus_source, context);
153 g_source_unref (bus_source);
154 g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, (__bridge void *)self);
155 g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, (__bridge void *)self);
156 gst_object_unref (bus);
158 /* Create a GLib Main Loop and set it to run */
159 GST_DEBUG ("Entering main loop...");
160 main_loop = g_main_loop_new (context, FALSE);
161 [self check_initialization_complete];
162 g_main_loop_run (main_loop);
163 GST_DEBUG ("Exited main loop");
164 g_main_loop_unref (main_loop);
168 g_main_context_pop_thread_default(context);
169 g_main_context_unref (context);
170 gst_element_set_state (pipeline, GST_STATE_NULL);
171 gst_object_unref (pipeline);