简体   繁体   English

Gstreamer x264enc 缓冲区大小无效 c

[英]Gstreamer x264enc invalid buffer size c

I've been trying to use this gstreamer c code(my system is running on ubuntu 20.04 with GStreamer 1.16.2 and gcc 9.4.0):我一直在尝试使用这个 gstreamer c 代码(我的系统运行在 ubuntu 20.04 上,GStreamer 1.16.2 和 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;
}

But the following error happens which is actually referring to x264enc, and btw this code is edited, at first it was jetson compatible and had nvv4l2h264enc:但是发生了以下错误,它实际上是指 x264enc,顺便说一句,这段代码是经过编辑的,起初它与 jetson 兼容并且有 nvv4l2h264enc:

无效的缓冲区大小错误 gst x264enc

The test video that I'm trying to stream has the following properties:我正在尝试 stream 的测试视频具有以下属性:

在此处输入图像描述

First note that gstreamer booleans are not typed with capitals, so use true and false or just use 0 and 1.首先请注意 gstreamer 布尔值不是用大写字母键入的,因此使用truefalse或仅使用 0 和 1。

x264enc may not be that fast on Jetson. x264enc 在 Jetson 上可能没有那么快。 You may try the following:您可以尝试以下操作:

#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;
}

This is what I've successfully tested on Jetson (AGX Xavier running L4T R32.6.1).这是我在 Jetson(运行 L4T R32.6.1 的 AGX Xavier)上成功测试的结果。 You would adapt to your file source and receiver address/port, and save as test_transcode_MKV_to_RTPH264.c then build with:您将适应您的文件源和接收方地址/端口,并保存为 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`

and test streaming:和测试流:

./test_transcode_MKV_to_RTPH264 

Then if receiver has gstreamer installed, you should be able to display with something like:然后,如果接收器安装了 gstreamer,您应该能够显示如下内容:

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

For receiving RTPH264 with FFMPEG or VLC, you may have to create a SDP file.要使用 FFMPEG 或 VLC 接收 RTPH264,您可能必须创建一个 SDP 文件。

EDIT: for non-NVIDIA case, you may try:编辑:对于非 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM