如何在视频管道中动态包含/绕过过滤器?

时间:2018-01-26 22:29:52

标签: gstreamer

在gstreamer实时视频管道中,我想根据用户的命令包含或不包含过滤器。我想过几种方法可以做到这一点,但不知道哪种方法最好,意思是最简单的代码,实时工作,流畅,流畅,可靠,不需要修改垫片或元件内部的细节,不风险记忆泄漏或以其他方式鼓励某人骂人。

举个简单的例子,假设我想创建这个管道:

videotestsrc  -> agingtv -> autovideosink

但是当用户点击一个键时,将其更改为/来自:

videotestsrc  ->  autovideosink

我想到的方法包括:

  1. 动态更改管道拓扑。有关于此的文章和示例代码。 Messier比像我自己喜欢处理的gstreamer noob一样。

  2. 始终保持过滤器。拓扑不会改变。在过滤器周围使用三通和几个阀门元件,如下所示:

  3.     
        --> tee (src0)-> agingtv -> valve1 --> (sink0)(join branches) -->
                (src1)------>----> valve2 ---> (sink1)
    
    1. 然后我注意到输出选择器和输入选择器。作为一个模拟电子产品的人,这些对我来说很有意义。
    2.     
          --> outputselector (src_0)------------------>(sink_0) inputselector --->
                             (src_1)----> agingtv ---->(sink_1)
      

      有经验的gstreamer专家会选择哪一个?我还不知道的任何这些或其他东西?

1 个答案:

答案 0 :(得分:1)

以下示例代码每隔一段时间从以下管道更改,您可以添加一段代码来处理您的关键事件:

videotestsrc - > q1 - > agingtv - >视频转换 - > Q2-> autovideosink videotestsrc - > q1 - >身份 - >视频转换 - > Q2-> autovideosink

#include <gst/gst.h>

static gchar *opt_effects = NULL;

#define DEFAULT_EFFECTS "identity,agingtv"

static GstPad *blockpad;
static GstElement *conv_before;
static GstElement *conv_after;
static GstElement *cur_effect;
static GstElement *pipeline;

static GQueue effects = G_QUEUE_INIT;

static GstPadProbeReturn
event_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{
  GMainLoop *loop = user_data;
  GstElement *next;

  if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_DATA (info)) != GST_EVENT_EOS)
    return GST_PAD_PROBE_PASS;

  gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));

  /* push current effect back into the queue */
  g_queue_push_tail (&effects, gst_object_ref (cur_effect));
  /* take next effect from the queue */
  next = g_queue_pop_head (&effects);
  if (next == NULL) {
    GST_DEBUG_OBJECT (pad, "no more effects");
    g_main_loop_quit (loop);
    return GST_PAD_PROBE_DROP;
  }

  g_print ("Switching from '%s' to '%s'..\n", GST_OBJECT_NAME (cur_effect),
      GST_OBJECT_NAME (next));

  gst_element_set_state (cur_effect, GST_STATE_NULL);

  /* remove unlinks automatically */
  GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, cur_effect);
  gst_bin_remove (GST_BIN (pipeline), cur_effect);

  GST_DEBUG_OBJECT (pipeline, "adding   %" GST_PTR_FORMAT, next);
  gst_bin_add (GST_BIN (pipeline), next);

  GST_DEBUG_OBJECT (pipeline, "linking..");
  gst_element_link_many (conv_before, next, conv_after, NULL);

  gst_element_set_state (next, GST_STATE_PLAYING);

  cur_effect = next;
  GST_DEBUG_OBJECT (pipeline, "done");

  return GST_PAD_PROBE_DROP;
}

static GstPadProbeReturn
pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{
  GstPad *srcpad, *sinkpad;

  GST_DEBUG_OBJECT (pad, "pad is blocked now");

  /* remove the probe first */
  gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));

  /* install new probe for EOS */
  srcpad = gst_element_get_static_pad (cur_effect, "src");
  gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK |
      GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, event_probe_cb, user_data, NULL);
  gst_object_unref (srcpad);

  /* push EOS into the element, the probe will be fired when the
   * EOS leaves the effect and it has thus drained all of its data */
  sinkpad = gst_element_get_static_pad (cur_effect, "sink");
  gst_pad_send_event (sinkpad, gst_event_new_eos ());
  gst_object_unref (sinkpad);

  return GST_PAD_PROBE_OK;
}

static gboolean
timeout_cb (gpointer user_data)
{
  gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
      pad_probe_cb, user_data, NULL);

  return TRUE;
}

static gboolean
bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
{
  GMainLoop *loop = user_data;

  switch (GST_MESSAGE_TYPE (msg)) {
    case GST_MESSAGE_ERROR:{
      GError *err = NULL;
      gchar *dbg;

      gst_message_parse_error (msg, &err, &dbg);
      gst_object_default_error (msg->src, err, dbg);
      g_clear_error (&err);
      g_free (dbg);
      g_main_loop_quit (loop);
      break;
    }
    default:
      break;
  }
  return TRUE;
}

int
main (int argc, char **argv)
{
  GError *err = NULL;
  GMainLoop *loop;
  GstElement *src, *q1, *q2, *effect, *sink;
  gchar **effect_names, **e;

  gst_init (&argc, &argv);
  loop = g_main_loop_new (NULL, FALSE);

  effect_names = g_strsplit (DEFAULT_EFFECTS, ",", -1);

  for (e = effect_names; e != NULL && *e != NULL; ++e) {
    GstElement *el;

    el = gst_element_factory_make (*e, NULL);
    if (el) {
      g_print ("Adding effect '%s'\n", *e);
      g_queue_push_tail (&effects, el);
    }
  }

  pipeline = gst_pipeline_new ("pipeline");

  src = gst_element_factory_make ("videotestsrc", NULL);
  g_object_set (src, "is-live", TRUE, NULL);

  q1 = gst_element_factory_make ("queue", NULL);

  blockpad = gst_element_get_static_pad (q1, "src");

  conv_before = gst_element_factory_make ("videoconvert", NULL);

  effect = g_queue_pop_head (&effects);
  cur_effect = effect;

  conv_after = gst_element_factory_make ("videoconvert", NULL);

  q2 = gst_element_factory_make ("queue", NULL);

  sink = gst_element_factory_make ("autovideosink", NULL);

  gst_bin_add_many (GST_BIN (pipeline), src, q1, conv_before, effect,
      conv_after, q2, sink, NULL);

  gst_element_link_many (src, q1, conv_before, effect, conv_after,
      q2, sink, NULL);

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  loop = g_main_loop_new (NULL, FALSE);

  gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, loop);

  g_timeout_add_seconds (1, timeout_cb, loop);

  g_main_loop_run (loop);

  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);

  return 0;
}

只是一个gstreamer pro会做上面的事情,在管道中有最小的元素来减少CPU的使用。另一个选择是让管道始终如下:

videotestsrc -> agingtv -> videoconvert -> autovideosink

当用户点击按键时,按以下方式设置agingtv的以下属性:

color-aging=false
dusts=false
pits=false
scratch-lines=0

当您想要将其切换回来时,请再次将其设置为:

color-aging=true
dusts=true
pits=true
scratch-lines=7 (default is 7)