1 #import "GStreamerBackend.h"
4 #include <gst/video/video.h>
6 GST_DEBUG_CATEGORY_STATIC (debug_category);
7 #define GST_CAT_DEFAULT debug_category
9 @interface GStreamerBackend()
10 -(void)setUIMessage:(gchar*) message;
12 -(void)check_initialization_complete;
15 @implementation GStreamerBackend {
16 id ui_delegate; /* Class that we use to interact with the user interface */
17 GstElement *pipeline; /* The running pipeline */
18 GstElement *video_sink;/* The video sink element which receives XOverlay commands */
19 GMainContext *context; /* GLib context used to run the main loop */
20 GMainLoop *main_loop; /* GLib main loop */
21 gboolean initialized; /* To avoid informing the UI multiple times about the initialization */
22 UIView *ui_video_view; /* UIView that holds the video */
29 -(id) init:(id) uiDelegate videoView:(UIView *)video_view
31 if (self = [super init])
33 self->ui_delegate = uiDelegate;
34 self->ui_video_view = video_view;
36 GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-3", 0, "iOS tutorial 3");
37 gst_debug_set_threshold_for_name("tutorial-3", GST_LEVEL_DEBUG);
39 /* Start the bus monitoring task */
40 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
51 GST_DEBUG("Setting the pipeline to NULL");
52 gst_element_set_state(pipeline, GST_STATE_NULL);
53 gst_object_unref(pipeline);
60 if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
61 [self setUIMessage:"Failed to set pipeline to playing"];
67 if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
68 [self setUIMessage:"Failed to set pipeline to paused"];
76 /* Change the message on the UI through the UI delegate */
77 -(void)setUIMessage:(gchar*) message
79 NSString *string = [NSString stringWithUTF8String:message];
80 if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)])
82 [ui_delegate gstreamerSetUIMessage:string];
86 /* Retrieve errors from the bus and show them on the UI */
87 static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
91 gchar *message_string;
93 gst_message_parse_error (msg, &err, &debug_info);
94 message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message);
97 [self setUIMessage:message_string];
98 g_free (message_string);
99 gst_element_set_state (self->pipeline, GST_STATE_NULL);
102 /* Notify UI about pipeline state changes */
103 static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
105 GstState old_state, new_state, pending_state;
106 gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
107 /* Only pay attention to messages coming from the pipeline, not its children */
108 if (GST_MESSAGE_SRC (msg) == GST_OBJECT (self->pipeline)) {
109 gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state));
110 [self setUIMessage:message];
115 /* Check if all conditions are met to report GStreamer as initialized.
116 * These conditions will change depending on the application */
117 -(void) check_initialization_complete
119 if (!initialized && main_loop) {
120 GST_DEBUG ("Initialization complete, notifying application.");
121 if (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)])
123 [ui_delegate gstreamerInitialized];
129 /* Main method for the bus monitoring code */
134 GError *error = NULL;
136 GST_DEBUG ("Creating pipeline");
138 /* Create our own GLib Main Context and make it the default one */
139 context = g_main_context_new ();
140 g_main_context_push_thread_default(context);
143 pipeline = gst_parse_launch("videotestsrc ! warptv ! videoconvert ! autovideosink", &error);
145 gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message);
146 g_clear_error (&error);
147 [self setUIMessage:message];
152 /* Set the pipeline to READY, so it can already accept a window handle */
153 gst_element_set_state(pipeline, GST_STATE_READY);
155 video_sink = gst_bin_get_by_interface(GST_BIN(pipeline), GST_TYPE_VIDEO_OVERLAY);
157 GST_ERROR ("Could not retrieve video sink");
160 gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(video_sink), (guintptr) (id) ui_video_view);
162 /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
163 bus = gst_element_get_bus (pipeline);
164 bus_source = gst_bus_create_watch (bus);
165 g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL);
166 g_source_attach (bus_source, context);
167 g_source_unref (bus_source);
168 g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, (__bridge void *)self);
169 g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, (__bridge void *)self);
170 gst_object_unref (bus);
172 /* Create a GLib Main Loop and set it to run */
173 GST_DEBUG ("Entering main loop...");
174 main_loop = g_main_loop_new (context, FALSE);
175 [self check_initialization_complete];
176 g_main_loop_run (main_loop);
177 GST_DEBUG ("Exited main loop");
178 g_main_loop_unref (main_loop);
182 g_main_context_pop_thread_default(context);
183 g_main_context_unref (context);
184 gst_element_set_state (pipeline, GST_STATE_NULL);
185 gst_object_unref (pipeline);