Gstreamer x264enc 无效缓冲区大小 c

Gstreamer x264enc invalid buffer size c

我一直在尝试使用这个 gstreamer c 代码(我的系统是 运行 on ubuntu 20.04 with GStreamer 1.16.2 and gcc 9.4.0):

#include <gst/gst.h>
#include <glib.h>
#include <stdio.h>

// gst-launch-1.0 videotestsrc pattern=ball ! 'video/x-raw, format=(string)I420, width=(int)1920, height=(int)1080, framerate=(fraction)30/1' ! \
// ! queue ! nvvideoconvert !  nvv4l2h264enc  bitrate=1000000 ! rtph264pay ! udpsink host=192.168.0.1 port=5000

int
main (int argc, char *argv[])
{
  GstElement *pipeline, *source, *filter, *queue, *converter, *encoder, *payer, *sink;
  GstBus *bus;
  GstMessage *msg;
  GstCaps *filtercaps;
  GstStateChangeReturn ret;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* Create the elements */
  source = gst_element_factory_make ("filesrc", "source");
  filter = gst_element_factory_make ("capsfilter","filter");
  queue = gst_element_factory_make ("queue","queue");
  converter = gst_element_factory_make ("videoconvert","converter");
  encoder = gst_element_factory_make ("x264enc","encoder");
  payer = gst_element_factory_make ("rtph264pay","payer");
  sink = gst_element_factory_make ("udpsink", "sink");
 
  /* Create the empty pipeline */
  pipeline = gst_pipeline_new ("maxim-pipeline");

  if (!pipeline || !source || !filter || !queue || !converter || !encoder || !payer || !sink) {
    g_printerr ("Not all elements could be created.\n");
    return -1;
  }

  /* Build the pipeline */
  gst_bin_add_many (GST_BIN (pipeline), source, filter, queue, converter, encoder, payer, sink, NULL);
  
  if (!gst_element_link_many (source,filter,queue,NULL)){
          g_printerr ("Source->filter->queue problem\n");
                  gst_object_unref (pipeline);
                  return -1;
  }
  
  if (!gst_element_link_many (queue,converter,encoder,NULL)){
                  g_printerr ("Queue->converter->encoder problem\n");
                  gst_object_unref (pipeline);
                  return -1;
  }


  if (!gst_element_link_many(encoder,payer,sink,NULL)){
                  g_printerr ("Encoder->payer->sink problem\n");
                  gst_object_unref (pipeline);
                  return -1;
  }

  /* Modify the properties */
  g_object_set (source, "location", "/home/thmsd/Videos/test.mkv", NULL);
  g_object_set (encoder, "bitrate", 2000000, NULL);
  
  g_object_set (sink, "host","192.168.0.1", NULL);
  g_object_set (sink, "port",5000, NULL);

 
  g_object_set (sink, "sync", "FALSE", NULL);
  
  filtercaps = gst_caps_new_simple ("video/x-raw",
      "format",G_TYPE_STRING,"I420",
          "width", G_TYPE_INT, 1920,
          "height", G_TYPE_INT, 1080,
          "framerate",GST_TYPE_FRACTION,30,1,
      NULL);
  
  g_object_set (filter, "caps", filtercaps, NULL);
  gst_caps_unref (filtercaps);


  /* Start playing */
  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Unable to set the pipeline to the playing state.\n");
    gst_object_unref (pipeline);
    return -1;
  }

  /* Wait until error or EOS */
  bus = gst_element_get_bus (pipeline);
  msg =
      gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
      GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

  /* Parse message */
  if (msg != NULL) {
    GError *err;
    gchar *debug_info;

    switch (GST_MESSAGE_TYPE (msg)) {
      case GST_MESSAGE_ERROR:
        gst_message_parse_error (msg, &err, &debug_info);
        g_printerr ("Error received from element %s: %s\n",
            GST_OBJECT_NAME (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;
      default:
        /* We should not reach here because we only asked for ERRORs and EOS */
        g_printerr ("Unexpected message received.\n");
        break;
    }
    gst_message_unref (msg);
  }

  /* Free resources */
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;
}

但是发生了以下错误,它实际上是指 x264enc,顺便说一句,这段代码是经过编辑的,起初它与 jetson 兼容并且有 nvv4l2h264enc:

我尝试流式传输的测试视频具有以下属性:

首先请注意,gstreamer 布尔值不是大写的,因此请使用 truefalse 或仅使用 0 和 1。

x264enc 在 Jetson 上可能没有那么快。您可以尝试以下方法:

#include <gst/gst.h>


int main (gint argc, gchar * argv[])
{  
    gst_init (&argc, &argv);
    GMainLoop *loop = g_main_loop_new (NULL, FALSE);
    GError *error = NULL;

    char* gst_pipeline_str = "filesrc location=/home/nvidia/Videos/bbb_sunflower_1080p_60fps_normal.mkv ! matroskademux ! parsebin ! nvv4l2decoder ! nvv4l2h264enc bitrate=20000000 insert-sps-pps=1 insert-vui=1 idrinterval=15 ! h264parse ! rtph264pay ! udpsink host=127.0.0.1 port=5004";
     
    /* x264enc may be much slower :*/
    /* 
    char* gst_pipeline_str = "filesrc location=/home/nvidia/Videos/bbb_sunflower_1080p_60fps_normal.mkv ! matroskademux ! parsebin ! nvv4l2decoder ! nvvidconv ! video/x-raw ! x264enc bitrate=20000 tune=zerolatency insert-vui=1 ! h264parse ! rtph264pay ! udpsink host=127.0.0.1 port=5004";
    */

    /* Create the pipeline */
    GstElement *pipeline = gst_parse_launch (gst_pipeline_str, &error);
    if (error || !pipeline) {
        g_error ("Failed to create pipeline\n");
        exit(-1);
    }

    /* This will output changes in terminal, you may remove it later to make it quiet. */
    g_signal_connect(pipeline, "deep-notify", G_CALLBACK(gst_object_default_deep_notify), NULL);

    /* Ok, successfully created the pipeline, now start it */
    gst_element_set_state (pipeline, GST_STATE_READY);
    gst_element_set_state (pipeline, GST_STATE_PLAYING);

    /* wait until it's up and running or failed */
    if (gst_element_get_state (pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
       g_error ("Failed to go into PLAYING state");
       exit(-2);
    }

    /* You may have to further manage bus for EOS... */
    
    g_print ("Running ...\n");
    g_main_loop_run (loop);

   return 0;
}

这是我在 Jetson (AGX Xavier 运行 L4T R32.6.1) 上成功测试的结果。 您将适应您的文件源和接收者 address/port,并另存为 test_transcode_MKV_to_RTPH264.c 然后构建:

gcc -Wall -o test_transcode_MKV_to_RTPH264 test_transcode_MKV_to_RTPH264.c `pkg-config --cflags --libs gstreamer-1.0 gobject-2.0 glib-2.0`

并测试流式传输:

./test_transcode_MKV_to_RTPH264 

然后如果接收器安装了 gstreamer,您应该能够显示类似:

gst-launch-1.0 udpsrc port=5004 ! application/x-rtp,encoding-name=H264 ! rtpjitterbuffer latency=300 ! rtph264depay ! decodebin ! autovideosink

要使用 FFMPEG 或 VLC 接收 RTPH264,您可能需要创建一个 SDP 文件。

编辑:对于 non-NVIDIA 情况,您可以尝试:

filesrc location=test_h265.mkv ! matroskademux ! h265parse ! avdec_h265 ! videoconvert ! x264enc bitrate=20000 tune=zerolatency insert-vui=1 key-int-max=30 ! h264parse ! rtph264pay ! udpsink host=127.0.0.1 port=5004