2013-05-09 16:07:30 +00:00
|
|
|
#import "GStreamerBackend.h"
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <gst/interfaces/xoverlay.h>
|
2013-05-14 16:11:04 +00:00
|
|
|
#include <gst/video/video.h>
|
2013-05-09 16:07:30 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (debug_category);
|
|
|
|
#define GST_CAT_DEFAULT debug_category
|
|
|
|
|
|
|
|
@interface GStreamerBackend()
|
|
|
|
-(void)setUIMessage:(gchar*) message;
|
|
|
|
-(void)app_function;
|
|
|
|
-(void)check_initialization_complete;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GStreamerBackend {
|
|
|
|
id ui_delegate; /* Class that we use to interact with the user interface */
|
|
|
|
GstElement *pipeline; /* The running pipeline */
|
|
|
|
GstElement *video_sink;/* The video sink element which receives XOverlay commands */
|
|
|
|
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 */
|
|
|
|
UIView *ui_video_view; /* UIView that holds the video */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
-(id) init:(id) uiDelegate videoView:(UIView *)video_view
|
|
|
|
{
|
|
|
|
if (self = [super init])
|
|
|
|
{
|
|
|
|
self->ui_delegate = uiDelegate;
|
|
|
|
self->ui_video_view = video_view;
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-4", 0, "iOS tutorial 4");
|
|
|
|
gst_debug_set_threshold_for_name("tutorial-4", GST_LEVEL_DEBUG);
|
|
|
|
|
|
|
|
/* Start the bus monitoring task */
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
[self app_function];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-05-14 15:11:05 +00:00
|
|
|
-(void) deinit
|
2013-05-09 16:07:30 +00:00
|
|
|
{
|
2013-05-14 15:11:05 +00:00
|
|
|
if (main_loop) {
|
|
|
|
g_main_loop_quit(main_loop);
|
2013-05-09 16:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) play
|
|
|
|
{
|
2013-05-14 15:11:05 +00:00
|
|
|
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
2013-05-09 16:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) pause
|
|
|
|
{
|
2013-05-14 15:11:05 +00:00
|
|
|
gst_element_set_state(pipeline, GST_STATE_PAUSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) setUri:(NSString*)uri
|
|
|
|
{
|
|
|
|
const char *char_uri = [uri UTF8String];
|
|
|
|
g_object_set(pipeline, "uri", char_uri, NULL);
|
|
|
|
GST_DEBUG ("URI set to %s", char_uri);
|
2013-05-09 16:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Change the message on the UI through the UI delegate */
|
|
|
|
-(void)setUIMessage:(gchar*) message
|
|
|
|
{
|
|
|
|
NSString *string = [NSString stringWithUTF8String:message];
|
|
|
|
if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)])
|
|
|
|
{
|
|
|
|
[ui_delegate gstreamerSetUIMessage:string];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 16:11:04 +00:00
|
|
|
static void check_media_size (GStreamerBackend *self) {
|
|
|
|
GstElement *video_sink;
|
|
|
|
GstPad *video_sink_pad;
|
|
|
|
GstCaps *caps;
|
|
|
|
GstVideoFormat fmt;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
|
|
|
|
/* Retrieve the Caps at the entrance of the video sink */
|
|
|
|
g_object_get (self->pipeline, "video-sink", &video_sink, NULL);
|
|
|
|
video_sink_pad = gst_element_get_static_pad (video_sink, "sink");
|
|
|
|
caps = gst_pad_get_negotiated_caps (video_sink_pad);
|
|
|
|
|
|
|
|
if (gst_video_format_parse_caps(caps, &fmt, &width, &height)) {
|
|
|
|
int par_n, par_d;
|
|
|
|
if (gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d)) {
|
|
|
|
width = width * par_n / par_d;
|
|
|
|
}
|
|
|
|
GST_DEBUG ("Media size is %dx%d, notifying application", width, height);
|
|
|
|
|
|
|
|
if (self->ui_delegate && [self->ui_delegate respondsToSelector:@selector(mediaSizeChanged:height:)])
|
|
|
|
{
|
|
|
|
[self->ui_delegate mediaSizeChanged:width height:height];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_caps_unref(caps);
|
|
|
|
gst_object_unref (video_sink_pad);
|
|
|
|
gst_object_unref(video_sink);
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:07:30 +00:00
|
|
|
/* Retrieve errors from the bus and show them on the UI */
|
|
|
|
static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *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);
|
2013-05-14 16:11:04 +00:00
|
|
|
|
|
|
|
if (old_state == GST_STATE_READY && new_state == GST_STATE_PAUSED)
|
|
|
|
{
|
|
|
|
check_media_size(self);
|
|
|
|
}
|
2013-05-09 16:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)])
|
|
|
|
{
|
|
|
|
[ui_delegate gstreamerInitialized];
|
|
|
|
}
|
|
|
|
initialized = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main method for the bus monitoring code */
|
|
|
|
-(void) app_function
|
|
|
|
{
|
|
|
|
GstBus *bus;
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Build pipeline */
|
2013-05-14 15:11:05 +00:00
|
|
|
pipeline = gst_parse_launch("playbin2", &error);
|
2013-05-09 16:07:30 +00:00
|
|
|
if (error) {
|
|
|
|
gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message);
|
|
|
|
g_clear_error (&error);
|
|
|
|
[self setUIMessage:message];
|
|
|
|
g_free (message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the pipeline to READY, so it can already accept a window handle */
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_READY);
|
|
|
|
|
|
|
|
video_sink = gst_bin_get_by_interface(GST_BIN(pipeline), GST_TYPE_X_OVERLAY);
|
|
|
|
if (!video_sink) {
|
|
|
|
GST_ERROR ("Could not retrieve video sink");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gst_x_overlay_set_window_handle(GST_X_OVERLAY(video_sink), (guintptr) (id) ui_video_view);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* 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);
|
2013-05-14 15:11:05 +00:00
|
|
|
pipeline = NULL;
|
2013-05-09 16:07:30 +00:00
|
|
|
|
2013-05-14 15:11:05 +00:00
|
|
|
ui_delegate = NULL;
|
|
|
|
ui_video_view = NULL;
|
|
|
|
|
2013-05-09 16:07:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|