摄像头原始数据读取——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;
}
相关推荐
程序猿编码5 分钟前
轻量级却实用:sigtrace 如何靠 ptrace 实现 Linux 信号的捕获与阻断(C/C++代码实现)
linux·c语言·c++·信号·捕获·ptrace
qq_393060477 分钟前
阿里云创建交换分区、设置内存监控预警和自动处理内存占用过大进程的脚本
linux·服务器·阿里云
曦樂~8 分钟前
【Qt】TCP连接--客户端和服务器
服务器·网络·c++·qt·tcp/ip
WoodWall12 分钟前
WebServer 02 Reactor模式
c++·后端
WaWaJie_Ngen16 分钟前
【OpenGL】模板测试(StencilTest)
c++·算法·游戏·游戏引擎·游戏程序·图形渲染
ontheway-xx16 分钟前
ffmpeg4.4.2 gcc 15.2.0 编译错误
ffmpeg·音视频
滴_咕噜咕噜17 分钟前
【MFC】数据库操作:数据库动态生成
数据库·c++·mfc
WoodWall20 分钟前
WebServer 00 重要前置知识
c++·后端
FFZero132 分钟前
【C++/Lua联合开发】 (一) Lua基础知识
c++·音视频·lua
墨尘笔尖40 分钟前
使用子进程实现 C++ 与 Python 交互式控制台
c++·windows·python