如何使用gstreamer编程,播放视频。

一,播放本地视频

#include 

static gboolean bus_call (GstBus *bus,GstMessage *msg, gpointer data)
{
    gint percent = 0;
    
    switch (GST_MESSAGE_TYPE (msg)) 
    {
    case GST_MESSAGE_ASYNC_DONE:
            g_print ("ASYNC_DONE msg\n");
            break;

    case GST_MESSAGE_BUFFERING:
            gst_message_parse_buffering (msg, &percent);
            g_print ("%s %d%%  \r", "Buffering...", percent);
            break;

    case GST_MESSAGE_CLOCK_LOST:
            g_print ("Clock lost, selecting a new one\n");
            break;
    case GST_MESSAGE_LATENCY:
            g_print ("Redistribute latency...\n");
            break;
    case GST_MESSAGE_REQUEST_STATE:
            g_print ("REQUEST STATE msg\n");
            break;
    case GST_MESSAGE_WARNING:
            g_print ("WARNING  msg\n");
            break;
    case GST_MESSAGE_STREAM_START:
            g_print ("Signals the start of a new stream\n");
            break;
    case GST_MESSAGE_EOS:
            g_print ("End of stream\n"); 
            //add code , replay or switch to next
            break;
    case GST_MESSAGE_ERROR:
            {
                gchar *debug;
                GError *error;
    
                gst_message_parse_error (msg, &error, &debug);
                g_free (debug);
    
                g_printerr ("Error: %s\n", error->message);
                g_error_free (error);
    
                //add code,handle error
                break;
            }
    default:
            printf("other msg : %d\n", GST_MESSAGE_TYPE (msg));
            break;
    }

    return TRUE;
}

int main(int argc,char **argv)
{
    /* init GStreamer */
    gst_init (&argc, &argv);
    
    /* create loop */
    GMainLoop *loop = g_main_loop_new (NULL, FALSE);
    if (NULL == loop)
    {
        goto err0;
    }

    /* create playbin */
    GstElement *playbin_em = gst_element_factory_make ("playbin", "play");
    if (NULL == playbin_em)
    {
        goto err1;
    }

    /* set uri */
    gchar *uri_str = "uri=file:///app/kang.ts";
    g_object_set (G_OBJECT (playbin_em), "uri", uri_str, NULL);
    
    /* set video sink */
    GstElement *videosink = gst_element_factory_make ("overlaysink", NULL);
    if (videosink != NULL)
    {    
        g_object_set (G_OBJECT (playbin_em), "video-sink", videosink, NULL);
    }
    else
    {
        goto err2;
    }
    g_object_set(G_OBJECT(videosink), "overlay-left", 0, NULL);
    g_object_set(G_OBJECT(videosink), "overlay-top", 0, NULL);
    g_object_set(G_OBJECT(videosink), "overlay-width", 960, NULL);
    g_object_set(G_OBJECT(videosink), "overlay-height", 540, NULL);
		
    /* set synchronisation offset between audio and video */
    gint64 tmp_64 = 200000000;   /* 200ms */
    g_object_set(G_OBJECT(playbin_em), "av-offset", tmp_64, NULL);
	
    /*add bus*/
    GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (playbin_em));
    gst_bus_add_watch (bus, bus_call, NULL);
    gst_object_unref (bus);
    			
    /* play */
    gst_element_set_state(playbin_em, GST_STATE_PLAYING);

    g_main_loop_run(loop);
    
err2:    
    gst_object_unref(GST_OBJECT (playbin_em));
err1:
    g_main_loop_unref(loop);
err0:
    return 0;
}
 

二,播放在线视频

uri_str改为协议地址即可,比如

1、接收udp组播数据,udp://@225.0.0.3:2003

2、rtsp请求,rtsp://172.16.50.188:8554/kang.mkv

你可能感兴趣的:(Freescale,i.MX6)