目录

摄像头原始数据读取——gstreamer(gst_parse_launch)

摄像头原始数据读取------gstreamer(gst_parse_launch)

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <gst/gst.h>

#ifdef __cplusplus
};
#endif

static gboolean bus_msg_timeout(GstBus *bus) 
{
    GstMessage *bus_msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

    if (bus_msg != NULL) 
    {
        GError *err;
        gchar *debug_info;

        switch (GST_MESSAGE_TYPE(bus_msg)) 
        {
            case GST_MESSAGE_ERROR:
                gst_message_parse_error(bus_msg, &err, &debug_info);
                g_printerr("Error received from element %s: %s\n",GST_OBJECT_NAME(bus_msg->src), err->message);
                g_printerr("Debugging information: %s\n",debug_info ? debug_info : "none");
                g_clear_error(&err);
                g_free(debug_info);
                break;
            case GST_MESSAGE_EOS:
                g_print("End-Of-Stream reached.\n");
                break;
            case GST_MESSAGE_STATE_CHANGED:
                break;
            default:
                break;
        }
        gst_message_unref(bus_msg);
    }

    return TRUE;
}

int main(int argc, char *argv[]) 
{
    GMainLoop *main_loop;
    GstElement *element_pipeline;
    GstBus *bus_ptr;
    GstMessage *msg;

    // 初始化GStreamer
    gst_init(&argc, &argv);

    // 使用v4l2src作为视频源,videoconvert进行格式转换,autovideosink自动选择视频输出
    element_pipeline = gst_parse_launch("v4l2src device=/dev/video0 ! videoconvert ! autovideosink", NULL);

    // 创建事件循环
    main_loop = g_main_loop_new(NULL, FALSE);

    // 获取总线以监听消息
    bus_ptr = gst_element_get_bus(element_pipeline);

    // 添加一个定时器来检查总线上的消息
    g_timeout_add_seconds(1, (GSourceFunc)bus_msg_timeout, bus_ptr);

    // 开始播放管道
    gst_element_set_state(element_pipeline, GST_STATE_PLAYING);

    // 运行事件循环
    g_main_loop_run(main_loop);

    // 清理
    gst_element_set_state(element_pipeline, GST_STATE_NULL);
    gst_object_unref(bus_ptr);
    gst_object_unref(element_pipeline);
    g_main_loop_unref(main_loop);

    return 0;
}
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
良许Linux3 分钟前
不想做程序员了,自己又没其他本领,能干什么呢?
linux
zym大哥大9 分钟前
C++多线程函数介绍
c++
良许Linux12 分钟前
程序员加班的真正原因是什么?
linux
良许Linux29 分钟前
C++ 程序员入门需要多久,怎样才能学好?
linux
xujiangyan_33 分钟前
php的高速缓存
linux·服务器·php
阿杜杜不是阿木木40 分钟前
使用ollama部署本地大模型(没有GPU也可以),实现IDEA和VS Code的git commit自动生成
linux·git·vscode·ai·intellij-idea·ollama
红肤色1 小时前
【网络安全基础】CentOS 7超详细安装教程(含镜像)
linux·运维·服务器·安全·网络安全·centos
良许Linux1 小时前
为什么不让程序员直接对接客户,而是通过产品经理?
linux
钡铼技术物联网关1 小时前
告别外置电路:IEPE接口如何重构精密测量生态?
linux·arm开发·5g·边缘计算
程序猿(雷霆之王)1 小时前
Linux——冯 • 诺依曼体系结构&操作系统初识
linux·运维·服务器