摄像头原始数据读取——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;
}
相关推荐
wang6021252187 小时前
Git部署项目配置密钥-Linux系统
linux·运维·git
三月微暖寻春笋7 小时前
【和春笋一起学C++】(五十三)使用指向对象的指针
c++·初始化·指向对象的指针
xlp666hub7 小时前
链表与它在 Linux 内核中的实现
linux·数据结构
倔强的石头1068 小时前
【Linux指南】进程控制系列(四)进程替换 ——exec 系列函数全解析与应用
linux·运维·bash
悾说8 小时前
xRDP实现Linux图形化通过Windows RDP访问Linux远程桌面
linux·运维·windows
又见野草8 小时前
C++入门基础(初阶)
开发语言·c++
Yu_Lijing8 小时前
基于C++的《Head First设计模式》笔记——命令模式
c++·笔记·设计模式
EasyCVR8 小时前
视频汇聚平台EasyCVR助力农贸市场迈向“智慧监管”新时代
音视频
艾莉丝努力练剑8 小时前
【QT】环境搭建收尾:认识Qt Creator
运维·开发语言·c++·人工智能·qt·qt creator·qt5
tianyuanwo8 小时前
解决Anolis/CentOS 8下Python 3.11 SELinux模块缺失:从原理到实战的完整指南
linux·centos·python3.11