在iOS中使用Gstreamer进行RTSP流式传输时,objc_autoreleaseNoPool和低内存警告崩溃

时间:2013-09-12 13:48:20

标签: ios objective-c video-streaming gstreamer rtsp

我正在使用iOS的gstreamer库从本地网络中的ip camera进行RTSP流式传输。我正在使用教程本身提供的代码 -

#import <gst/gst.h>
#include <gst/interfaces/xoverlay.h>
#include <gst/video/video.h>


@interface GStreamerBackend()
//these are all structs, so no need to give them as properties for memory handling.
{
    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 */
    GstState target_state;       /* Desired pipeline state, to be set once buffering is complete */
}

@property(nonatomic, weak)id<GstreamerDelegate> delegate;
@property(nonatomic, weak)UIView *ui_video_view;
@property(nonatomic)BOOL is_live;

@end

@implementation GStreamerBackend


-(id)initWithDelegate:(id)delegate videoView:(UIView *)videoView
{
    if(self = [super init])
    {
        self.delegate = delegate;
        self.ui_video_view = videoView;

        /* Start the bus monitoring task */
        @autoreleasepool
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [self app_function];
            });
        }

    }
    return self;
}


#pragma mark Pipeline Setup and Running
-(void) setUri:(NSString*)uri
{
    const char *char_uri = [uri UTF8String];
    g_object_set(pipeline, "uri", char_uri, NULL);
}


/* Main method for the bus monitoring code */
-(void) app_function
{
    GstBus *bus;
    //GSource *timeout_source;
    GSource *bus_source;
    GError *error = NULL;

    /* 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 */
    pipeline = gst_parse_launch("playbin2", &error);
    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) {
        NSLog(@"Could not retrieve video sink");
        return;
    }
    gst_x_overlay_set_window_handle(GST_X_OVERLAY(video_sink), (guintptr) (id) self.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::eos", (GCallback)eos_cb, (__bridge void *)self);
    g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, (__bridge void *)self);
    g_signal_connect (G_OBJECT (bus), "message::buffering", (GCallback)buffering_cb, (__bridge void *)self);
    g_signal_connect (G_OBJECT (bus), "message::clock-lost", (GCallback)clock_lost_cb, (__bridge void *)self);
    gst_object_unref (bus);

    /* Register a function that GLib will call 4 times per second */
   /* timeout_source = g_timeout_source_new (250);
    g_source_set_callback (timeout_source, (GSourceFunc)refresh_ui, (__bridge void *)self, NULL);
    g_source_attach (timeout_source, context);
    g_source_unref (timeout_source);
    */

    /* Create a GLib Main Loop and set it to run */
    NSLog(@"Entering main loop...");

    @autoreleasepool {
        main_loop = g_main_loop_new (context, FALSE);
        [self check_initialization_complete];
        g_main_loop_run (main_loop);
        NSLog(@"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);
    pipeline = NULL;

    self.ui_video_view = NULL;
    self.delegate = NULL;

    return;

}


/* 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)
    {
        [self.delegate gstreamerInitialized];
        initialized = TRUE;
    }
}



/* 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);
    }
}


/* 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);
}


#pragma mark for Network Reselience
/* Called when buffering messages are received. We inform the UI about the current buffering level and
 * keep the pipeline paused until 100% buffering is reached. At that point, set the desired state. */
static void buffering_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) {
    gint percent;

    if (self.is_live)
        return;

    gst_message_parse_buffering (msg, &percent);
    if (percent < 100 && self->target_state >= GST_STATE_PAUSED)
    {
        gchar * message_string = g_strdup_printf ("Buffering %d%%", percent);
        gst_element_set_state (self->pipeline, GST_STATE_PAUSED);
        [self setUIMessage:message_string];
        g_free (message_string);
    }
    else if (self->target_state >= GST_STATE_PLAYING)
    {
        gst_element_set_state (self->pipeline, GST_STATE_PLAYING);
    }
    else if (self->target_state >= GST_STATE_PAUSED)
    {
        [self setUIMessage:"Buffering complete"];
    }
}


/* Called when the clock is lost */
static void clock_lost_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) {
    if (self->target_state >= GST_STATE_PLAYING) {
        gst_element_set_state (self->pipeline, GST_STATE_PAUSED);
        gst_element_set_state (self->pipeline, GST_STATE_PLAYING);
    }
}


#pragma mark Functionality related
/* Retrieve the video sink's Caps and tell the application about the media size */
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);

    /* Do nothing if there is no video sink (this might be an audio-only clip */
    if (!video_sink) return;

    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;
        }
        NSLog(@"Media size is %dx%d, notifying application", width, height);

        [self.delegate mediaSizeChanged:width height:height];
    }

    gst_caps_unref(caps);
    gst_object_unref (video_sink_pad);
    gst_object_unref(video_sink);
}


-(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)setUIMessage:(gchar*)message
{
    NSString *string = [NSString stringWithUTF8String:message];
    [self.delegate gstreamerSetUIMessage:string];
}


-(void) deinit
{
    if (main_loop) {
        g_main_loop_quit(main_loop);
    }
}


-(void) dealloc
{
    if (pipeline)
    {
        gst_element_set_state(pipeline, GST_STATE_NULL);
        gst_object_unref(pipeline);
        pipeline = NULL;
    }
}

@end

我在iOS 5 上收到以下警告,但在iOS 6及更高版本上没有。 -

objc[1512]: Object 0x39bfff0 of class __NSCFDictionary autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[1512]: Object 0x39bfd50 of class __NSCFDictionary autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[1512]: Object 0x2d57c00 of class __NSCFString autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

在搜索到这些错误后,我在 [self app_function]; 周围添加了 @autoreleasepool 块,但我仍然遇到相同的错误。随后我收到了内存警告和崩溃。

我还通过转到BreakPoint Navigator(⌘+ 6)点击底部的 + 符号 - &gt;来设置 objc_autoreleaseNoPool 断点。 添加异常断点,如此 -

enter image description here

关于“类__NSCFDictionary的对象0x39bfff0”的第一个警告,这是我在调试导航器中看到的 -

enter image description here

显然问题出在 - VideoStreaming gst_egl_choose_config at gstegladaptation_eagl.m:233:但是我无法在任何地方找到这个文件。我搜索了~/Library/Developer/GStreamer路径中安装的Gstreamer sdk,但无法找到该文件。

iOS 5上发生内存警告和崩溃。

在iOS 6上 - 日志上不显示警告。没有内存警告或崩溃。 虽然“objc_autoreleaseNoPool”的断点仍然受到影响。

任何人都可以给出解决方案或解释这个问题吗?

0 个答案:

没有答案