From c27858631d29736590b51b25524ec6a2f825b37f Mon Sep 17 00:00:00 2001 From: Xavi Artigas Date: Mon, 6 May 2013 16:49:03 +0200 Subject: [PATCH] Large revamp to bring it closer to the Android tutorial 2. --- .../xcode iOS/Tutorial 2/GStreamerBackend.h | 7 +- .../xcode iOS/Tutorial 2/GStreamerBackend.m | 172 +++++++++++------- .../Tutorial 2/GStreamerBackendDelegate.h | 4 +- .../xcode iOS/Tutorial 2/ViewController.h | 7 +- .../xcode iOS/Tutorial 2/ViewController.m | 32 +--- .../en.lproj/MainStoryboard_iPad.storyboard | 64 +++++-- .../en.lproj/MainStoryboard_iPhone.storyboard | 83 +++++---- 7 files changed, 218 insertions(+), 151 deletions(-) diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.h b/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.h index 0b0d67a8f7..bcd804ce51 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.h +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.h @@ -3,12 +3,7 @@ @interface GStreamerBackend : NSObject -@property (nonatomic,assign) id delegate; - --(NSString*) getGStreamerVersion; - --(BOOL) initializePipeline; - +-(id) init:(id) uiDelegate; -(void) play; -(void) pause; -(void) stop; diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.m b/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.m index 27bfaa08a6..0ab205e868 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.m +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackend.m @@ -3,16 +3,33 @@ #include @interface GStreamerBackend() --(void)notifyError:(gchar*) message; --(void)notifyEos; --(void) _poll_gst_bus; +-(void)setUIMessage:(gchar*) message; +-(void)app_function; +-(void)check_initialization_complete; @end @implementation GStreamerBackend { - GstElement *pipeline; + id delegate; /* Class that we use to interact with the user interface */ + GstElement *pipeline; /* The running pipeline */ + GMainContext *context; /* GLib context used to run the main loop */ + GMainLoop *main_loop; /* GLib main loop */ + gboolean initialized; /* To avoid informing the UI multiple times about the initialization */ } -@synthesize delegate; +-(id) init:(id) uiDelegate +{ + if (self = [super init]) + { + self->delegate = uiDelegate; + + /* Start the bus monitoring task */ + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [self app_function]; + }); + } + + return self; +} -(void) dealloc { @@ -23,95 +40,120 @@ } } --(void)notifyError:(gchar*) message +-(void)setUIMessage:(gchar*) message { NSString *string = [NSString stringWithUTF8String:message]; - if(delegate && [delegate respondsToSelector:@selector(gstreamerError:from:)]) + if(delegate && [delegate respondsToSelector:@selector(gstreamerSetUIMessage:)]) { - [delegate gstreamerError:string from:self]; + [delegate gstreamerSetUIMessage:string]; } } --(void)notifyEos +/* Retrieve errors from the bus and show them on the UI */ +static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) { - if(delegate && [delegate respondsToSelector:@selector(gstreamerEosFrom)]) - { - [delegate gstreamerEosFrom:self]; + GError *err; + gchar *debug_info; + gchar *message_string; + + gst_message_parse_error (msg, &err, &debug_info); + message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message); + g_clear_error (&err); + g_free (debug_info); + [self setUIMessage:message_string]; + g_free (message_string); + gst_element_set_state (self->pipeline, GST_STATE_NULL); +} + +/* Notify UI about pipeline state changes */ +static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) +{ + GstState old_state, new_state, pending_state; + gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); + /* Only pay attention to messages coming from the pipeline, not its children */ + if (GST_MESSAGE_SRC (msg) == GST_OBJECT (self->pipeline)) { + gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state)); + [self setUIMessage:message]; + g_free (message); } } --(void) _poll_gst_bus +/* Check if all conditions are met to report GStreamer as initialized. + * These conditions will change depending on the application */ +-(void) check_initialization_complete +{ + if (!initialized && main_loop) { + GST_DEBUG ("Initialization complete, notifying application."); + if (delegate && [delegate respondsToSelector:@selector(gstreamerInitialized)]) + { + [delegate gstreamerInitialized]; + } + initialized = TRUE; + } +} + +/* Main method for the native code. This is executed on its own thread. */ +-(void) app_function { GstBus *bus; - GstMessage *msg; - - /* Wait until error or EOS */ - bus = gst_element_get_bus (self->pipeline); - msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, - (GstMessageType) (GST_MESSAGE_ERROR | GST_MESSAGE_EOS)); - gst_object_unref(bus); - - switch (GST_MESSAGE_TYPE(msg)) { - case GST_MESSAGE_EOS: - [self stop]; - [self notifyEos]; - NSLog(@"EOS"); - break; - case GST_MESSAGE_ERROR: { - GError *gerr = NULL; - gchar *debug; - - gst_message_parse_error(msg, &gerr, &debug); - - [self stop]; - NSLog(@"Error %s - %s", gerr->message, debug, nil); - [self notifyError:gerr->message]; - g_free(debug); - g_error_free(gerr); - } - break; - default: - break; - } -} - --(BOOL) initializePipeline -{ + GSource *bus_source; GError *error = NULL; + + GST_DEBUG ("Creating pipeline"); + + /* Create our own GLib Main Context and make it the default one */ + context = g_main_context_new (); + g_main_context_push_thread_default(context); - if (pipeline) - return YES; - + /* Build pipeline */ pipeline = gst_parse_launch("audiotestsrc ! audioconvert ! audioresample ! autoaudiosink", &error); if (error) { gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message); g_clear_error (&error); - [self notifyError:message]; + [self setUIMessage:message]; g_free (message); - return NO; + return; } - /* start the bus polling. This will the bus being continuously polled */ - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - while (1) { - [self _poll_gst_bus]; - } - }); + /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ + bus = gst_element_get_bus (pipeline); + bus_source = gst_bus_create_watch (bus); + g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL); + g_source_attach (bus_source, context); + g_source_unref (bus_source); + g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, (__bridge void *)self); + g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, (__bridge void *)self); + gst_object_unref (bus); - return YES; + /* Create a GLib Main Loop and set it to run */ + GST_DEBUG ("Entering main loop..."); + main_loop = g_main_loop_new (context, FALSE); + [self check_initialization_complete]; + g_main_loop_run (main_loop); + GST_DEBUG ("Exited main loop"); + g_main_loop_unref (main_loop); + main_loop = NULL; + + /* Free resources */ + g_main_context_pop_thread_default(context); + g_main_context_unref (context); + gst_element_set_state (pipeline, GST_STATE_NULL); + gst_object_unref (pipeline); + + return; } -(void) play { if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) { - [self notifyError:"Failed to set pipeline to playing"]; + [self setUIMessage:"Failed to set pipeline to playing"]; } } -(void) pause { if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) { - [self notifyError:"Failed to set pipeline to paused"]; + [self setUIMessage:"Failed to set pipeline to paused"]; } } @@ -121,13 +163,5 @@ gst_element_set_state(pipeline, GST_STATE_NULL); } --(NSString*) getGStreamerVersion -{ - char *str = gst_version_string(); - NSString *version = [NSString stringWithUTF8String:str]; - g_free(str); - return version; -} - @end diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackendDelegate.h b/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackendDelegate.h index 2414408f7a..b9095918c7 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackendDelegate.h +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 2/GStreamerBackendDelegate.h @@ -3,7 +3,7 @@ @protocol GStreamerBackendDelegate @optional --(void) gstreamerError:(NSString *)message from:(id)sender; --(void) gstreamerEosFrom:(id)sender; +-(void) gstreamerInitialized; +-(void) gstreamerSetUIMessage:(NSString *)message; @end diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.h b/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.h index df3a6f7bba..38fbe1c8e7 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.h +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.h @@ -2,13 +2,16 @@ #import "GStreamerBackendDelegate.h" @interface ViewController : UIViewController { + IBOutlet UILabel *message_label; + IBOutlet UIButton *play_button; + IBOutlet UIButton *pause_button; } -(IBAction) play:(id)sender; -(IBAction) pause:(id)sender; /* From GStreamerBackendDelegate */ --(void) gstreamerError:(NSString *)message from:(id)sender; --(void) gstreamerEosFrom:(id)sender; +-(void) gstreamerInitialized; +-(void) gstreamerSetUIMessage:(NSString *)message; @end diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.m b/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.m index f477537ec0..99c2b1c96e 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.m +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 2/ViewController.m @@ -13,14 +13,11 @@ - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. - gst_backend = [[GStreamerBackend alloc] init]; - - if (![gst_backend initializePipeline]) { - - } - gst_backend.delegate = self; + play_button.enabled = FALSE; + pause_button.enabled = FALSE; + + gst_backend = [[GStreamerBackend alloc] init:self]; } - (void)didReceiveMemoryWarning @@ -39,28 +36,19 @@ [gst_backend pause]; } --(void) gstreamerError:(NSString *)message from:(id)sender +-(void) gstreamerInitialized { - NSLog(@"Error %@", message, nil); - - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GStreamer error" - message:message - delegate:nil - cancelButtonTitle:@"OK" - otherButtonTitles:nil]; dispatch_async(dispatch_get_main_queue(), ^{ - /* make sure it runs from the main thread */ - [alert show]; + play_button.enabled = TRUE; + pause_button.enabled = TRUE; + message_label.text = @"Ready"; }); } --(void) gstreamerEosFrom:(id)sender +-(void) gstreamerSetUIMessage:(NSString *)message { - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"EOS" message:@"End of stream" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; - dispatch_async(dispatch_get_main_queue(), ^{ - /* make sure it runs from the main thread */ - [alert show]; + message_label.text = message; }); } diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPad.storyboard b/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPad.storyboard index 54dcba87f3..9c834f2289 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPad.storyboard +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPad.storyboard @@ -12,17 +12,26 @@ - + - - - + + + + - - - + + + - - + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPhone.storyboard b/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPhone.storyboard index fc46a36e59..1ce5fdb8bf 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPhone.storyboard +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 2/en.lproj/MainStoryboard_iPhone.storyboard @@ -1,89 +1,98 @@ - + - + - - + + - + - - - + - - - - - + + + + + + + + - - - + + + + + + + + + + - + - - - - - - - - - - - - - -