GStreamer教程01

参考来自 <http://blog.csdn.net/sakulafly/article/details/19398257>,并整理。

gst_parse_launch

      GStreamer是设计来处理多媒体流的框架。媒体流经过一系列的中间element,从source element流到sink element。这些相互作用的element构成了一整个的pipeline。

      使用GStreamer时你常常需要使用独立的elements来手动搭建一个pipeline,但是,在比较简单的情况下,我们也可以使用gst_parse_launch()。这个函数原本是描述一个pipeline的,但也可以很方便的用来建立一个pipeline。

 

来自 <http://blog.csdn.net/sakulafly/article/details/19398257>

 

playbin2

 

playbin2是一个特殊的element,它既是一个source也是一个sink,同时也能处理整个pipeline的事务。在内部,他创建和链接了所有播放你的媒体所必须的elements,你完全不必担心。

 

1.  /* Build the pipeline */  

2.  pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);  

//仅解析了playbin2得一个参数——我们希望播放的URI。试试其他的地址,比如http://或者file://开头的URI,playbin2都能良好的工作。

 

#include<gst/gst.h>

intmain(int argc, char *argv[]) {

  GstElement *pipeline, *source, *sink;

  GstBus *bus;

  GstMessage *msg;

  GstStateChangeReturn ret;

 

  /* Initialize GStreamer */

  gst_init (&argc, &argv);

  

  /*Create the elements */

//gst_element_factory_make参数(element类型,element名字)

  source = gst_element_factory_make("videotestsrc", "source");

  sink = gst_element_factory_make("autovideosink", "sink");

  

  /* Create the emptypipeline */

  pipeline = gst_pipeline_new("test-pipeline");

  

  if (!pipeline || !source || !sink) {

    g_printerr ("Not all elements could becreated.\n");

    return -1;

  }

 

  /* Build the pipeline */

//一个pipeline就是一个特定类型的可以包含其他element的bin,而且所以可     以用在bin上的方法也都可以用在pipeline上。

//向pipeline添加element,由null终止,【gst_bin_add增加单个】

  gst_bin_add_many (GST_BIN (pipeline),source, sink, NULL);

 

//gst_element_link连接bin里面的element(源,目标)

  if (gst_element_link (source, sink) !=TRUE) {

      g_printerr ("Elements could not belinked.\n");

      gst_object_unref (pipeline);

      return-1;

  }

 

  /* Modify the source's properties */

//g_object_set()方法接受一个用NULL结束的属性名称/属性值的组成的对,所以可以一次同时修改多项属性。

  g_object_set (source, "pattern", 0,NULL);

 

  /* Start playing */

  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);

  if (ret == GST_STATE_CHANGE_FAILURE) {

    g_printerr ("Unable to set thepipeline to the playing state.\n");

    gst_object_unref (pipeline);

    return -1;

  }

 

  /* Wait until error or EOS */

//gst_bus_timed_pop_filtered会阻塞知道获得一个消息

  bus = gst_element_get_bus (pipeline);

  msg = gst_bus_timed_pop_filtered (bus,GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

 

  /* Parse message */

  if (msg != NULL) {

    GError *err;

    gchar *debug_info;

   

    switch (GST_MESSAGE_TYPE (msg)) {

      case GST_MESSAGE_ERROR:

        gst_message_parse_error (msg, &err,&debug_info);

        g_printerr ("Error received from element%s: %s\n", GST_OBJECT_NAME (msg->src), err->message);

        g_printerr ("Debugginginformation: %s\n", debug_info ? debug_info : "none");

        g_clear_error (&err);

        g_free (debug_info);

        break;

      case GST_MESSAGE_EOS:

        g_print ("End-Of-Streamreached.\n");

        break;

      default:

        /* We should not reach here because weonly asked for ERRORs and EOS */

        g_printerr ("Unexpected messagereceived.\n");

        break;

    }

    gst_message_unref (msg);

  }

 

  /* Free resources */

  gst_object_unref (bus);

  gst_element_set_state (pipeline,GST_STATE_NULL);

  gst_object_unref (pipeline);

  return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_30042269/article/details/80539436