Add comments, debug output, a bit of variable renaming and some code reorganization.

This commit is contained in:
Xavi Artigas 2013-05-06 17:13:03 +02:00
parent c27858631d
commit 5d747fb6be
4 changed files with 58 additions and 28 deletions

View file

@ -3,9 +3,14 @@
@interface GStreamerBackend : NSObject
/* Initialization method. Pass the delegate that will take care of the UI.
* This delegate must implement the GStreamerBackendDelegate protocol */
-(id) init:(id) uiDelegate;
/* Set the pipeline to PLAYING */
-(void) play;
/* Set the pipeline to PAUSED */
-(void) pause;
-(void) stop;
@end

View file

@ -2,6 +2,9 @@
#include <gst/gst.h>
GST_DEBUG_CATEGORY_STATIC (debug_category);
#define GST_CAT_DEFAULT debug_category
@interface GStreamerBackend()
-(void)setUIMessage:(gchar*) message;
-(void)app_function;
@ -9,18 +12,25 @@
@end
@implementation GStreamerBackend {
id delegate; /* Class that we use to interact with the user interface */
id ui_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 */
}
/*
* Interface methods
*/
-(id) init:(id) uiDelegate
{
if (self = [super init])
{
self->delegate = uiDelegate;
self->ui_delegate = uiDelegate;
GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-2", 0, "iOS tutorial 2");
gst_debug_set_threshold_for_name("tutorial-2", GST_LEVEL_DEBUG);
/* Start the bus monitoring task */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@ -34,18 +44,38 @@
-(void) dealloc
{
if (pipeline) {
GST_DEBUG("Setting the pipeline to NULL");
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
pipeline = NULL;
}
}
-(void) play
{
if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
[self setUIMessage:"Failed to set pipeline to playing"];
}
}
-(void) pause
{
if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
[self setUIMessage:"Failed to set pipeline to paused"];
}
}
/*
* Private methods
*/
/* Change the message on the UI through the UI delegate */
-(void)setUIMessage:(gchar*) message
{
NSString *string = [NSString stringWithUTF8String:message];
if(delegate && [delegate respondsToSelector:@selector(gstreamerSetUIMessage:)])
if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)])
{
[delegate gstreamerSetUIMessage:string];
[ui_delegate gstreamerSetUIMessage:string];
}
}
@ -84,15 +114,15 @@ static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *se
{
if (!initialized && main_loop) {
GST_DEBUG ("Initialization complete, notifying application.");
if (delegate && [delegate respondsToSelector:@selector(gstreamerInitialized)])
if (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)])
{
[delegate gstreamerInitialized];
[ui_delegate gstreamerInitialized];
}
initialized = TRUE;
}
}
/* Main method for the native code. This is executed on its own thread. */
/* Main method for the bus monitoring code */
-(void) app_function
{
GstBus *bus;
@ -143,25 +173,5 @@ static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *se
return;
}
-(void) play
{
if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
[self setUIMessage:"Failed to set pipeline to playing"];
}
}
-(void) pause
{
if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
[self setUIMessage:"Failed to set pipeline to paused"];
}
}
-(void) stop
{
if(pipeline)
gst_element_set_state(pipeline, GST_STATE_NULL);
}
@end

View file

@ -3,7 +3,12 @@
@protocol GStreamerBackendDelegate <NSObject>
@optional
/* Called when the GStreamer backend has finished initializing
* and is ready to accept orders. */
-(void) gstreamerInitialized;
/* Called when the GStreamer backend wants to output some message
* to the screen. */
-(void) gstreamerSetUIMessage:(NSString *)message;
@end

View file

@ -10,6 +10,10 @@
@implementation ViewController
/*
* Methods from UIViewController
*/
- (void)viewDidLoad
{
[super viewDidLoad];
@ -26,16 +30,22 @@
// Dispose of any resources that can be recreated.
}
/* Called when the Play button is pressed */
-(IBAction) play:(id)sender
{
[gst_backend play];
}
/* Called when the Pause button is pressed */
-(IBAction) pause:(id)sender
{
[gst_backend pause];
}
/*
* Methods from GstreamerBackendDelegate
*/
-(void) gstreamerInitialized
{
dispatch_async(dispatch_get_main_queue(), ^{