您的位置:首页 > 其它

GStreamer基础教程01——Hello World

2017-08-04 23:17 671 查看
目标

      对于一个软件库来说,没有比在屏幕上打印出Hello World更近直观的第一印象了。因为我们是在和一个多媒体的framework打交道,所以我们准备播放一段视频来代替Hello World。不要被下面的代码吓唬住了——真正起作用的也就四行而已。剩下的都是资源管理的代码,C语言嘛,就是有这个麻烦。不多说了,准备你的第一个GStreamer应用吧……

Hello World

把下面的代码copy到一个文本文件,并改名为basic-tutorial-1.c

[objc]
view plain
copy

print?

#include <gst/gst.h>  
    
int main(int argc, charchar *argv[]) {  
  GstElement *pipeline;  
  GstBus *bus;  
  GstMessage *msg;  
    
  /* Initialize GStreamer */  
  gst_init (&argc, &argv);  
    
  /* Build the pipeline */  
  pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);  
    
  /* Start playing */  
  gst_element_set_state (pipeline, GST_STATE_PLAYING);  
    
  /* Wait until error or EOS */  
  bus = gst_element_get_bus (pipeline);  
  msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);  
    
  /* Free resources */  
  if (msg != NULL)  
    gst_message_unref (msg);  
  gst_object_unref (bus);  
  gst_element_set_state (pipeline, GST_STATE_NULL);  
  gst_object_unref (pipeline);  
  return 0;  
}  

#include <gst/gst.h>

int main(int argc, char *argv[]) {
GstElement *pipeline;
GstBus *bus;
GstMessage *msg;

/* Initialize GStreamer */
gst_init (&argc, &argv);

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

/* Start playing */
gst_element_set_state (pipeline, GST_STATE_PLAYING);

/* Wait until error or EOS */
bus = gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

/* Free resources */
if (msg != NULL)
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
return 0;
}

      编译方面根据你的平台也有介绍,Linux平台http://docs.gstreamer.com/display/GstSDK/Installing+on+Linux,Mac平台http://docs.gstr
bc37
eamer.com/display/GstSDK/Installing+on+Mac+OS+X,windows下是http://docs.gstreamer.com/display/GstSDK/Installing+on+Windows。如果编译出错的话请检查一下报错地方的语句,如果编译通过,那么可以运行了,会弹出一个窗口,播放网络上的一个视频。恭喜你,第一步成功了!

代码分析

      我们看一下代码,分析一下工作流程。

[objc]
view plain
copy

print?

/* Initialize GStreamer */  
gst_init (&argc, &argv);  

/* Initialize GStreamer */
gst_init (&argc, &argv);


      这是所有GStreamer应用的第一句,在gst_init里面做了

      +初始化所有内部数据结构

      +检查所有可用的插件

      +运行所有的命令行选项

      如果你把argc和argv传入gst_init,在处理命令行上是由好处的(在GStreamer工具里面还会讲到这个方面)。

[objc]
view plain
copy

print?

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

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


      这一行是这个教程最重要的部分,里面有两个非常重要的点:gst_parse_launch和playbin2

gst_parse_launch

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

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

playbin2

      我们让gst_parse_launch()函数建立了一个怎么样的pipeline呢?这就是playbin2的用处了,我们建立了一个只包含playbin2的element的pipeline。

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

      这个element相对于纯手工搭建的pipeline来说,控制粒度没有那么好,但也有足够的可定制了。

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

      如果你键入了错误的URI,或者URI不存在,或者你漏掉了某个插件,GStreamer提供了一些通知机制,但我们这个例子仅仅实现在出错时退出,所以就不展开了。

[objc]
view plain
copy

print?

/* Start playing */  
gst_element_set_state (pipeline, GST_STATE_PLAYING);  

/* Start playing */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
      这一行代码展示了另一个需要关注的点:状态。每一个GStreamer的element有一个状态,你可以理解成常见的DVD播放器上得播放/暂停按钮。播放器必须设置pipeline为PLAYING状态才能真正开始播放,这一行代码就是做了这件事。

[objc]
view plain
copy

print?

/* Wait until error or EOS */  
bus = gst_element_get_bus (pipeline);  
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);  

/* Wait until error or EOS */
bus = gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
      这几行是在等待发生一个错误或者流已经播放结束。gst_element_get_bus()会得到pipeline的总线,然后gst_bus_timed_pop_filtered()会阻塞直到你遇到一个错误或者流播放结束。这一点在下一讲还会继续介绍,这里先介绍这么多。

      就这样了,GStreamer处理了所有的事情,这个例子会在流播放结束或播放出错时停止,当然,任何时候你都可以用Ctrl+C来终止。

清理

      在应用终止前,我们还有一些事情要做

[objc]
view plain
copy

print?

/* Free resources */  
if (msg != NULL)  
  gst_message_unref (msg);  
gst_object_unref (bus);  
gst_element_set_state (pipeline, GST_STATE_NULL);  
gst_object_unref (pipeline);  

/* Free resources */
if (msg != NULL)
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
      在你使用了一个函数后,一定要记得查阅文档来确定是否需要释放资源。在这个例子中,gst_bus_timed_pop_filtered()会返回一个message,这个需要调用gst_message_unref()来释放(下一讲会继续介绍)。

      gst_element_get_bus()会对总线增加一个引用,所以也需要调用get_object_unref()来释放。设置pipeline为NULL状态会让它释放掉所有的资源,最后,释放掉pipeline自身。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: