Update theme submodule
[platform/upstream/gstreamer.git] / examples / tutorials / xcode iOS / Tutorial 2 / GStreamerBackend.m
1 #import "GStreamerBackend.h"
2
3 #include <gst/gst.h>
4
5 GST_DEBUG_CATEGORY_STATIC (debug_category);
6 #define GST_CAT_DEFAULT debug_category
7
8 @interface GStreamerBackend()
9 -(void)setUIMessage:(gchar*) message;
10 -(void)app_function;
11 -(void)check_initialization_complete;
12 @end
13
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 */
20 }
21
22 /*
23  * Interface methods
24  */
25
26 -(id) init:(id) uiDelegate
27 {
28     if (self = [super init])
29     {
30         self->ui_delegate = uiDelegate;
31
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);
34
35         /* Start the bus monitoring task */
36         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
37             [self app_function];
38         });
39     }
40
41     return self;
42 }
43
44 -(void) dealloc
45 {
46     if (pipeline) {
47         GST_DEBUG("Setting the pipeline to NULL");
48         gst_element_set_state(pipeline, GST_STATE_NULL);
49         gst_object_unref(pipeline);
50         pipeline = NULL;
51     }
52 }
53
54 -(void) play
55 {
56     if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
57         [self setUIMessage:"Failed to set pipeline to playing"];
58     }
59 }
60
61 -(void) pause
62 {
63     if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
64         [self setUIMessage:"Failed to set pipeline to paused"];
65     }
66 }
67
68 /*
69  * Private methods
70  */
71
72 /* Change the message on the UI through the UI delegate */
73 -(void)setUIMessage:(gchar*) message
74 {
75     NSString *string = [NSString stringWithUTF8String:message];
76     if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)])
77     {
78         [ui_delegate gstreamerSetUIMessage:string];
79     }
80 }
81
82 /* Retrieve errors from the bus and show them on the UI */
83 static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
84 {
85     GError *err;
86     gchar *debug_info;
87     gchar *message_string;
88
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);
91     g_clear_error (&err);
92     g_free (debug_info);
93     [self setUIMessage:message_string];
94     g_free (message_string);
95     gst_element_set_state (self->pipeline, GST_STATE_NULL);
96 }
97
98 /* Notify UI about pipeline state changes */
99 static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
100 {
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];
107         g_free (message);
108     }
109 }
110
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
114 {
115     if (!initialized && main_loop) {
116         GST_DEBUG ("Initialization complete, notifying application.");
117         if (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)])
118         {
119             [ui_delegate gstreamerInitialized];
120         }
121         initialized = TRUE;
122     }
123 }
124
125 /* Main method for the bus monitoring code */
126 -(void) app_function
127 {
128     GstBus *bus;
129     GSource *bus_source;
130     GError *error = NULL;
131
132     GST_DEBUG ("Creating pipeline");
133
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);
137
138     /* Build pipeline */
139     pipeline = gst_parse_launch("audiotestsrc ! audioconvert ! audioresample ! autoaudiosink", &error);
140     if (error) {
141         gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message);
142         g_clear_error (&error);
143         [self setUIMessage:message];
144         g_free (message);
145         return;
146     }
147
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);
157
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);
165     main_loop = NULL;
166
167     /* Free resources */
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);
172
173     return;
174 }
175
176 @end