gstreamer1.16.3中没有GST_QUERY_H264_SPS应该如何替换
时间: 2023-08-13 19:02:41 浏览: 156
在GStreamer 1.16.3版本中,GST_QUERY_H264_SPS已经被移除,因此您需要使用其他方式来查询H.264视频流参数集(SPS)。您可以使用h264parse元素提取SPS信息,然后再以其他方式进行处理。下面是一个示例:
```c
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/video/gstvideometa.h>
// 回调函数
static GstPadProbeReturn
probe_callback(GstPad *pad, GstPadProbeInfo *info, gpointer user_data)
{
GstVideoInfo *vinfo;
GstVideoCodecState *state;
GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER(info);
gint n_sps = 0;
// 获取视频信息
vinfo = gst_video_info_new();
if (!gst_video_info_from_caps(vinfo, GST_BUFFER_CAPS(buf))) {
g_printerr("Failed to get video info from caps\n");
return GST_PAD_PROBE_OK;
}
// 获取视频状态
state = gst_video_codec_state_new();
if (!gst_video_decoder_negotiate(GST_ELEMENT(GST_PAD_PARENT(pad)), vinfo, NULL, state)) {
g_printerr("Failed to negotiate video codec state\n");
return GST_PAD_PROBE_OK;
}
// 提取SPS信息
GstVideoCodecFrame *frame = gst_video_codec_frame_new();
if (!gst_video_codec_state_frame_init(state, frame)) {
g_printerr("Failed to initialize video codec frame\n");
gst_video_codec_state_unref(state);
return GST_PAD_PROBE_OK;
}
n_sps = gst_video_codec_frame_get_n_sps(frame);
if (n_sps > 0) {
GstBuffer *sps_buf = gst_buffer_copy(gst_video_codec_frame_get_sps(frame, 0));
// 用sps_buf进行后续操作
g_print("SPS size: %d\n", GST_BUFFER_SIZE(sps_buf));
gst_buffer_unref(sps_buf);
}
// 清理
gst_video_codec_frame_unref(frame);
gst_video_codec_state_unref(state);
gst_video_info_free(vinfo);
return GST_PAD_PROBE_OK;
}
int main(int argc, char *argv[])
{
GstElement *pipeline, *src, *h264parse;
GstPad *srcpad;
gst_init(&argc, &argv);
// 创建元素
pipeline = gst_pipeline_new("test-pipeline");
src = gst_element_factory_make("filesrc", "src");
h264parse = gst_element_factory_make("h264parse", "h264parse");
// 设置元素属性
g_object_set(G_OBJECT(src), "location", "/path/to/h264file", NULL);
// 将元素添加到管道中
gst_bin_add_many(GST_BIN(pipeline), src, h264parse, NULL);
gst_element_link(src, h264parse);
// 获取h264parse元素的源端口
srcpad = gst_element_get_static_pad(h264parse, "src");
// 添加探针
gst_pad_add_probe(srcpad, GST_PAD_PROBE_TYPE_BUFFER, probe_callback, NULL, NULL);
// 启动管道
gst_element_set_state(pipeline, GST_STATE_PLAYING);
// 运行主循环
GstBus *bus = gst_element_get_bus(pipeline);
GstMessage *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;
}
```
上面的示例中,我们使用了h264parse元素提取了SPS信息,并且通过回调函数的方式进行处理。您可以根据您的需求进行修改和扩展。
阅读全文