在Gstreamer C API中对视频缩放元素使用上限

时间:2019-07-15 09:34:27

标签: c gstreamer

我正在尝试使用ximagesrc从窗口抓取视频并将其缩放到一定大小,然后再使用H.264进行编码并使用RTP流式传输到另一台计算机。我在C API中实现了管道,除非我在videoscale中添加了capsfilter元素,否则它就可以正常工作。

具体来说,我在理解如何正确使用videoscale元素以及如何以编程方式将其与videoconvert元素链接方面遇到问题。当我尝试使用gst_element_link_filtered连接videoconvertvideoscale元素以缩放至所需分辨率时,函数capsfilter返回false。

我的代码如下:

static gboolean
link_elements_with_filter (GstElement *element1, GstElement *element2)
{
  gboolean link_ok;
  GstCaps *caps;

  caps = gst_caps_from_string("video/x-raw,width=640,height=480,framerate=20/1");

  link_ok = gst_element_link_filtered (element1, element2, caps);
  gst_caps_unref (caps);

  if (!link_ok) {
      g_warning ("Failed to link element1 and element2!");
    }

  return link_ok;
}


int main(int argc, char *argv[]) {

(...)

 /* Create the elements */
  source = gst_element_factory_make ("ximagesrc", "source");
  converter = gst_element_factory_make ("videoconvert", "converter");
  scaler = gst_element_factory_make ("videoscale", "scaler");
  encoder = gst_element_factory_make("nvh264enc", "encoder");
  payloader = gst_element_factory_make("rtph264pay", "payloader");
  sink = gst_element_factory_make ("udpsink", "sink");

  g_object_set (source, "use-damage", FALSE, "xid", 0x5c0000c, NULL);
  g_object_set (encoder, "gop-size", 25, "rc-mode", 2, "bitrate", 2000, NULL);
  g_object_set (payloader, "config-interval", 1, NULL); 
  g_object_set (sink, "host", "172.17.25.248", "port", 5004, NULL);

  pipeline = gst_pipeline_new ("test-pipeline");

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


   gst_bin_add_many (GST_BIN (pipeline), source, converter, scaler, encoder, payloader, sink,   NULL);
   if ((gst_element_link (source, converter) && link_elements_with_filter(converter, scaler)
                                          && gst_element_link (scaler, encoder)
                                          && gst_element_link (encoder, payloader)
                                          && gst_element_link (payloader, sink)) != TRUE) {
    g_printerr ("Elements could not be linked.\n");
    gst_object_unref (pipeline);
    return -1;
}

(...)

运行此代码时,得到以下输出:

** (gst_server:55698): WARNING **: 11:21:57.315: Failed to link element1 and element2!
Elements could not be linked.
Process finished with exit code 255

所以我在连接videoconvertvideoscale元素时遇到问题。管道中的元素顺序是否有问题,或者我使用大写字母是否有问题?

谢谢。

编辑:

相应的gst-launch管道(工作正常)如下所示:

gst-launch-1.0 ximagesrc use-damage=FALSE xid=0x5c0000c ! videoconvert ! videoscale ! video/x-raw,width=320,height=240 ! nvh264enc gop-size=25 rc-mode=cbr bitrate=2000 ! rtph264pay config-interval=1 ! udpsink host=172.X.X.X port=5001

然后我可以在客户端计算机上启动另一个gst-launch管道并查看缩放后的视频。

编辑2: 我设置了GST_DEBUG=5并检查了调试输出。它表示videoconvert元素的源极焊盘和capsfilter元素的漏极焊盘不兼容。似乎媒体格式之间的转换存在问题。

0:00:00.175919763 57549 0x5578d0241800 DEBUG               GST_CAPS gstutils.c:3065:gst_pad_query_caps:<capsfilter0:sink> query returned video/x-raw, width=(int)640, height=(int)480, framerate=(fraction)20/1
0:00:00.175938538 57549 0x5578d0241800 DEBUG               GST_CAPS gstpad.c:2278:gst_pad_link_check_compatible_unlocked:<converter:src> src caps video/x-raw, format=(string)BGRx, width=(int)1247, height=(int)1573, framerate=(fraction)[ 1/2147483647, 2147483647/1 ], pixel-aspect-ratio=(fraction)1/1; video/x-raw, width=(int)1247, height=(int)1573, framerate=(fraction)[ 1/2147483647, 2147483647/1 ], pixel-aspect-ratio=(fraction)1/1, format=(string){ I420, YV12, YUY2, UYVY, AYUV, VUYA, RGBx, BGRx, xRGB, xBGR, RGBA, BGRA, ARGB, ABGR, RGB, BGR, Y41B, Y42B, YVYU, Y444, v210, v216, Y210, Y410, NV12, NV21, GRAY8, GRAY16_BE, GRAY16_LE, v308, RGB16, BGR16, RGB15, BGR15, UYVP, A420, RGB8P, YUV9, YVU9, IYU1, ARGB64, AYUV64, r210, I420_10BE, I420_10LE, I422_10BE, I422_10LE, Y444_10BE, Y444_10LE, GBR, GBR_10BE, GBR_10LE, NV16, NV24, NV12_64Z32, A420_10BE, A420_10LE, A422_10BE, A422_10LE, A444_10BE, A444_10LE, NV61, P010_10BE, P010_10LE, IYU2, VYUY, GBRA, GBRA_10BE, GBRA_10LE, BGR10A2_LE, GBR_12BE, GBR_12LE, GBRA_12BE, GBRA_12LE, I420_12BE, I420_12LE, I422_12BE, I422_12LE, Y444_12BE, Y444_12LE, GRAY10_LE32, NV12_10LE32, NV16_10LE32, NV12_10LE40 }
0:00:00.175947929 57549 0x5578d0241800 DEBUG               GST_CAPS gstpad.c:2280:gst_pad_link_check_compatible_unlocked:<capsfilter0:sink> sink caps video/x-raw, width=(int)640, height=(int)480, framerate=(fraction)20/1
0:00:00.175955981 57549 0x5578d0241800 DEBUG               GST_CAPS gstpad.c:2298:gst_pad_link_check_compatible_unlocked: caps are not compatible
0:00:00.175960969 57549 0x5578d0241800 INFO                GST_PADS gstpad.c:2434:gst_pad_link_prepare: caps are incompatible
0:00:00.175968231 57549 0x5578d0241800 INFO                GST_PADS gstpad.c:2529:gst_pad_link_full: link between converter:src and capsfilter0:sink failed: no common format

1 个答案:

答案 0 :(得分:1)

首先,我建议使用graphics.DrawString(string.Concat("\u202A", dString), pFont, pBrush new Rectangle(x + (width - dStringWidth), y + (2 * lHeight), dtringWidth, lHeight), sfHeader); 。您可以像通过gst_parse_launch()运行一样为应用程序创建管道。然后,您可以根据需要通过迭代或搜索来访问各个元素-尽管大多数内容都可以使用管道字符串来描述。

输入您的代码。您已在定标器之前设置了上限。这意味着定标器输入应具有这些上限。但是gst-launch-1.0定义了实际大小。如果不是您提供的确切名称,它将失败。

通常,您希望将其设置在编码器之前,然后让元素工作以找到满足上限的配置。例如:

  

ximagesrc!视频转换!视频规模! xvideo / x-raw,format = I420,width = 640,height = 480,framerate = 20/1! nvh264enc ..

此处ximagesrc将知道它必须以某种方式从videoconvert(通常为RGB格式)转换为I420,并且ximagesrc将从videoscale提供的任何分辨率扩展到640x480。建议ximagesrc以20 fps的速度捕获。