1. 程式人生 > >GStreamer教程01

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;

}