没有EOS的Gstreamer快照

时间:2016-01-22 13:56:42

标签: c gstreamer

我试图使用gstreamer从一个来源拍摄几张快照。使用以下代码,我成功地获取了9个文件但是从源代码中获得了EOS(这实际上是正常的,它是由num-buffers参数引起的):

m

但我的问题是我希望在这9个快照之后继续直播。我在发球台和排队能力方面寻找,但我无法做任何事情。我想我必须用一个我暂停和播放的multifilesink元素做一个动态管道,但是如何告诉它只做9个文件? (max-files = 9不起作用导致生成的文件被覆盖)

由于

1 个答案:

答案 0 :(得分:0)

当然,您需要在计数缓冲区中添加探针,并在您不需要时删除一些元素。

我在你的结构中添加了几个字段:

int count;
GstPad *blockpad;
GstElement *fakesink;

一旦我们保存了9个快照,我又创建了一个接收器来替换管道末端:

  data.fakesink = gst_element_factory_make ("fakesink", "fakesink");

我将探测添加到data.convert的srcpad:

  data.count = 0;
  data.blockpad = gst_element_get_static_pad (data.convert, "src");
  gst_pad_add_probe (data.blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM | GST_PAD_PROBE_TYPE_BUFFER,
                     pad_probe_cb, &data, NULL);

我使用了GStreamer 1.x,因此我将ffenc_pgm元素替换为avenc_pgm,将ffmpegcolorspace元素替换为identity

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

/* Structure to contain all our information, so we can pass it to callbacks */
typedef struct _CustomData {
  int count;
  GstPad *blockpad;
  GstElement *pipeline;
  GstElement *source;
  GstElement *convert;
  GstElement *sink;
  GstElement *fakesink;
  GstElement *encode;
} CustomData;

static GstPadProbeReturn
pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data) {
  CustomData *data = user_data;
  data->count++;
  printf("%d\n", data->count);
  if (data->count > 9)
  {
    gst_element_set_state (data->encode, GST_STATE_NULL);
    gst_bin_remove (GST_BIN (data->pipeline), data->encode);
    gst_element_set_state (data->sink, GST_STATE_NULL);
    gst_bin_remove (GST_BIN (data->pipeline), data->sink);

    gst_bin_add (GST_BIN (data->pipeline), data->fakesink);
    gst_element_link (data->convert, data->fakesink);
    gst_element_set_state (data->fakesink, GST_STATE_PLAYING);

    gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));

    return GST_PAD_PROBE_REMOVE;
  }
  else
    return GST_PAD_PROBE_PASS;
}

int main(int argc, char *argv[]) {
  CustomData data;
  GstBus *bus;
  GstMessage *msg;
  GstStateChangeReturn ret;
  gboolean terminate = FALSE;

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

  /* Create the elements */
  data.source = gst_element_factory_make ("videotestsrc", "source");
  data.convert = gst_element_factory_make ("identity", "convert");
  data.encode = gst_element_factory_make ("avenc_pgm", "encode");
  data.sink = gst_element_factory_make ("multifilesink", "sink");

  data.fakesink = gst_element_factory_make ("fakesink", "fakesink");

  /* Create the empty pipeline */
  data.pipeline = gst_pipeline_new ("test-pipeline");

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

  /* Build the pipeline. Note that we are NOT linking the source at this
   * point. We will do it later. */
  gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.convert , data.encode, data.sink, NULL);
  if (!gst_element_link_many (data.source, data.convert, data.encode, data.sink, NULL)) {
    g_printerr ("Elements could not be linked.\n");
    gst_object_unref (data.pipeline);
    return -1;
  }

  /* Modify the source's properties */
  g_object_set (data.source, "pattern", 0, NULL);
  g_object_set (data.source, "num-buffers", 20, NULL);
  g_object_set (data.sink, "location", "frame%05d.pgm", NULL);

  data.count = 0;
  data.blockpad = gst_element_get_static_pad (data.convert, "src");
  gst_pad_add_probe (data.blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM | GST_PAD_PROBE_TYPE_BUFFER,
                     pad_probe_cb, &data, NULL);

  /* Start playing */
    ret = gst_element_set_state (data.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 (data.pipeline);
      return -1;
    }

    /* Wait until error or EOS */
    bus = gst_element_get_bus (data.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 (data.pipeline, GST_STATE_NULL);
    gst_object_unref (data.pipeline);
    return 0;
  }