如何通过 gstreamer 获取 h264 帧

问题描述

我熟悉 ffmpeg,但不熟悉 GStreamer。我知道怎么通过ffmpeg获取H264帧,比如我可以通过AVPacket获取H264帧。但是我不知道如何使用GStreamer来获取h264的帧。我不打算将H264数据直接保存为本地文件,因为我需要做其他处理。谁能给我一些示例代码?我会很感激的。这是我从其他人的代码中学到的。

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <unistd.h>
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>

typedef struct {
    GstPipeline *pipeline;
    GstAppSrc  *src;
    GstElement *filter1;
    GstElement *encoder;
    GstElement *filter2;
    GstElement *parser;
    GstElement *qtmux;
    GstElement *sink;

    GstClockTime timestamp;
    guint sourceid;
} gst_app_t;

static gst_app_t gst_app;

int main()
{
    gst_app_t *app = &gst_app;
    GstStateChangeReturn state_ret;
    gst_init(NULL,NULL); //Initialize Gstreamer
    app->timestamp = 0; //Set timestamp to 0

    //Create pipeline,and pipeline elements
    app->pipeline = (GstPipeline*)gst_pipeline_new("mypipeline");
    app->src    =   (GstAppSrc*)gst_element_factory_make("appsrc","mysrc");
    app->filter1 =  gst_element_factory_make ("capsfilter","myfilter1");
    app->encoder =  gst_element_factory_make ("omxh264enc","myomx");
    app->filter2 =  gst_element_factory_make ("capsfilter","myfilter2");
    app->parser =   gst_element_factory_make("h264parse","myparser");
    app->qtmux =    gst_element_factory_make("qtmux","mymux");
    app->sink =     gst_element_factory_make ("filesink",NULL);
    
    if( !app->pipeline || 
        !app->src      || !app->filter1 || 
        !app->encoder  || !app->filter2 || 
        !app->parser   || !app->qtmux    || 
        !app->sink    )  {
        printf("Error creating pipeline elements!\n");
        exit(2);
    }

    //Attach elements to pipeline
    gst_bin_add_many(
        GST_BIN(app->pipeline),(GstElement*)app->src,app->filter1,app->encoder,app->filter2,app->parser,app->qtmux,app->sink,NULL);

    //Set pipeline element attributes
    g_object_set (app->src,"format",GST_FORMAT_TIME,NULL);
    GstCaps *filtercaps1 = gst_caps_new_simple ("video/x-raw",G_TYPE_STRING,"I420","width",G_TYPE_INT,1280,"height",720,"framerate",GST_TYPE_FRACTION,1,NULL);
    g_object_set (G_OBJECT (app->filter1),"caps",filtercaps1,NULL);
    GstCaps *filtercaps2 = gst_caps_new_simple ("video/x-h264","stream-format","byte-stream",NULL);
    g_object_set (G_OBJECT (app->filter2),filtercaps2,NULL);
    g_object_set (G_OBJECT (app->sink),"location","output.h264",NULL);

    //Link elements together
    g_assert( gst_element_link_many(
        (GstElement*)app->src,NULL ) );

    //Play the pipeline
    state_ret = gst_element_set_state((GstElement*)app->pipeline,GST_STATE_PLAYING);
    g_assert(state_ret == GST_STATE_CHANGE_ASYNC);

    //Get a pointer to the test input
    FILE *testfile = fopen("test.yuv","rb");   
    g_assert(testfile != NULL);

    //Push the data from buffer to gstpipeline 100 times
    for(int i = 0; i < 100; i++) {
        char* filebuffer = (char*)malloc (1382400); //Allocate memory for framebuffer
        if (filebuffer == NULL) {printf("Memory error\n"); exit (2);} //Errorcheck
        size_t bytesread = fread(filebuffer,(1382400),testfile); //Read to filebuffer
        //printf("File Read: %zu bytes\n",bytesread);

        GstBuffer *pushbuffer; //Actual databuffer
        GstFlowReturn ret; //Return value
        pushbuffer = gst_buffer_new_wrapped (filebuffer,1382400); //Wrap the data

        //Set frame timestamp
        GST_BUFFER_PTS      (pushbuffer) = app->timestamp;
        GST_BUFFER_DTS      (pushbuffer) = app->timestamp;  
        GST_BUFFER_DURATION (pushbuffer) = gst_util_uint64_scale_int (1,GST_SECOND,1);
        app->timestamp += GST_BUFFER_DURATION (pushbuffer);
        //printf("Frame is at %lu\n",app->timestamp);

        ret = gst_app_src_push_buffer( app->src,pushbuffer); //Push data into pipeline

        g_assert(ret ==  GST_FLOW_OK);
    }
    usleep(100000);
    
    //Declare end of stream
    gst_app_src_end_of_stream (GST_APP_SRC (app->src));
    printf("End Program.\n");

return 0;

}

这是代码源的链接 link

解决方法

您的示例用于将数据从应用程序提供给 GStreamer,希望使用 x264 进行编码并将结果存入文件。

您需要(我在这里猜测)是从文件中读取数据 - 比如说 movie.mp4 并将解码后的数据放入您的应用程序中 (?)

我相信您有两个选择:

1, 使用 appsink 而不是 filesink 并使用 filesrc 从文件中提供数据。因此,如果除了抓取 h264 帧之外还需要其他处理(例如通过网络播放或发送),则必须使用 tee 将管道拆分为两个输出分支,例如下面的 gst-launch 示例。输出管道的一个分支将进入例如窗口输出 - autovideosink,另一部分将进入您的应用程序。

为了演示这种拆分并仍然向您展示真正发生的事情,我将使用调试元素标识,它能够转储通过它的数据。 通过这种方式,您将学习使用这个方便的工具进行实验和验证您知道自己在做什么。这不是您需要的解决方案。

gst-launch-1.0 -q filesrc location= movie.mp4 ! qtdemux name=qt ! video/x-h264 ! h264parse ! tee name=t t. ! queue ! avdec_h264 ! videoconvert ! autovideosink t. ! queue ! identity dump=1 ! fakesink sync=true

此管道将视频播放到窗口 (autovideosink) 中,tee 的另一个分支转到名为 identity 的调试元素,该元素以 hexdump 方式转储帧(包含地址、字符表示和所有内容) . 所以你在 gst-launch 的标准输出中看到的是实际的 h264 帧(但你看不到边界或任何东西......它只是真正的原始转储)。

要了解 gst-launch 语法(主要是 name= 的别名),请查看文档的 this part

在实际代码中,您不会使用 identity 和 fakesink,而是仅将 appsink 链接到那里,并将 appsink 信号连接到 C 源代码中的回调。

这有很好的例子,我不会试图给你完整的解决方案。 This example 演示如何从应用程序接收器中获取样本。 重要的部分是:

/* The appsink has received a buffer */
static GstFlowReturn new_sample (GstElement *sink,CustomData *data) {
  GstSample *sample;

  /* Retrieve the buffer */
  g_signal_emit_by_name (sink,"pull-sample",&sample);
  if (sample) {
    /* The only thing we do in this example is print a * to indicate a received buffer */
    g_print ("*");
    gst_sample_unref (sample);
    return GST_FLOW_OK;
  }

  return GST_FLOW_ERROR;
}

// somewhere in main()
// construction and linkage of elements
g_signal_connect (data.app_sink,"new-sample",G_CALLBACK (new_sample),&data);

2, 第二种解决方案是使用仅针对缓冲区注册的焊盘探针。 Pad 探针是一种在管道中任何元素的任何焊盘上注册回调并告诉 GStreamer 您对该探针感兴趣的信息的方法。您可以要求它在每个事件、任何下游事件或通过该探测器的任何缓冲区上调用回调。在 pad probe 调用的回调中,您将提取缓冲区和该缓冲区中的实际数据。

同样有很多关于如何使用焊盘探针的例子。 一个非常好的示例包含几乎完全符合您需要的逻辑可以是 found here

重要的部分:

static GstPadProbeReturn
cb_have_data (GstPad          *pad,GstPadProbeInfo *info,gpointer         user_data)
{
// ... the code for writing the buffer data somewhere ..
}

// ... later in main()
pad = gst_element_get_static_pad (src,"src");
gst_pad_add_probe (pad,GST_PAD_PROBE_TYPE_BUFFER,(GstPadProbeCallback) cb_have_data,NULL,NULL);