gstreamer学习(1)——运行第一个hello word

此博客是在gstreamer官网学习并总结的学习概要,具体参考gstreamer官网教程:基础教程1:Hello world!

环境

  1. host使用Ubuntu 22.04
    使用ubuntu 20.04安装组件失败,导致无法编译,所以建议直接升级22.04

  2. 首先需要安装gstreamer组件:

sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
  1. 下载gstreamer代码:
git clone https://gitlab.freedesktop.org/gstreamer/gstreamer
  1. 编译第一个hello word程序:
    代码路径在:subprojects/gst-docs/examples/tutorials下,编译命令:
gcc basic-tutorial-1.c -o basic-tutorial-1 `pkg-config --cflags --libs gstreamer-1.0`

这是基础的编译命令,如果你需要其他的gstreamer组件,可以在后面加上所需组件,比如video组件:gstreamer-video-1.0

  1. 运行结果:
    在这里插入图片描述

代码

源代码如下:
(在官网或者gstreamer安装包也可以找到)

#include <gst/gst.h>

#ifdef __APPLE__
#include <TargetConditionals.h>
#endif

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

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

  /* Build the pipeline */
  pipeline =
      gst_parse_launch
      ("playbin uri=https://gstreamer.freedesktop.org/data/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);

  /* See next tutorial for proper error message handling/parsing */
  if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
    g_error ("An error occurred! Re-run with the GST_DEBUG=*:WARN environment "
        "variable set for more details.");
  }

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

int
main (int argc, char *argv[])
{
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
  return gst_macos_main (tutorial_main, argc, argv, NULL);
#else
  return tutorial_main (argc, argv);
#endif
}

分析(来自官网)

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

这条语句必须作为第一条gstreamer语句,在这个函数中,会做如下事情:

  1. 初始化所有的内部数据结构
  2. 检查哪些插件可用
  3. 执行命令传入的option
  pipeline =
      gst_parse_launch
      ("playbin uri=https://gstreamer.freedesktop.org/data/media/sintel_trailer-480p.webm",
      NULL);

通常构建pipeline是通过手动指定内部element完成的,但是如果pipeline比较简单,直接调用gst_parse_launch()函数构建也可以。调用gst_parse_launch()函数构建只有一个元素的pipeline是通过playbin实现的,playbin是一个特殊的元素,它即是sink又是source,并且包含所有必须的元素。

不止是网址,也可以是 http:// or file://也可以当作playbin的参数

  /* 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);

先获取当前pipeline的bus,然后从bus中获取通知消息(此处获取的ERROR和EOS),这个接口是阻塞式的

  /* Free resources */
  gst_message_unref (msg);
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;

需要清理的东西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值