欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GStreamer 代码详解:连接 IP 网络摄像机

程序员文章站 2024-03-02 20:36:52
...

下面是本文讨论的代码。我试了一下,扩展名必须是 *.c,改成 *.cpp 的话无法编译。这个原因我回头再研究。

#include <gst/gst.h>
int main (int argc, char *argv[])
{
  GstElement *pipeline;
  GstBus *bus;
  GstMessage *msg;

  gst_init (&argc, &argv);

  pipeline = gst_parse_launch("playbin uri=rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0", NULL);
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
  bus = gst_element_get_bus (pipeline);
  msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

  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;
}

上面的程序可简化为下面伪代码的形式:

	pipeline = gst_parse_launch("playbin uri= ...");
	gst_element_set_state (pipeline, ...);
	bus = gst_element_get_bus (pipeline);
	msg = gst_bus_timed_pop_filtered (bus, ...);
  • pipeline
    GStreamer 能够帮助我们建立一个视频数据流架构,这个架构由 “元素” 构成。本例子建立的这个数据流只有一个元素,实现从数据源获取数据,并在窗口显示出来。
    变量 pipeline 就是这个唯一的元素,函数 gst_parse_launch(…) 则是创建这个元素最简单的方法。

  • playbin
    我们正在构建一个由一个名为playbin的元素组成的管道。playbin是一个特殊的元素,它充当(可产生视频流的端口)和(可接收视频流的端口),是一个完整的管道。在内部,它创建并连接所有必要的元素来播放您的媒体,因此您不必担心它。
    它不允许手动管道那样的控制粒度,但是,它仍然允许足够的定制以满足广泛的应用程序。

  • bus
    数据流中的元素之间的通讯,需要一种可靠的机制 ,GStreamer 采用了总线来解决这个问题。