Blame tests/icles/test-effect-switch.c

Packit 0652a1
/* GStreamer dynamic effect change test app
Packit 0652a1
 * Copyright (C) 2012 Tim-Philipp Müller <tim centricular net>
Packit 0652a1
Packit 0652a1
 * This library is free software; you can redistribute it and/or
Packit 0652a1
 * modify it under the terms of the GNU Library General Public
Packit 0652a1
 * License as published by the Free Software Foundation; either
Packit 0652a1
 * version 2 of the License, or (at your option) any later version.
Packit 0652a1
 *
Packit 0652a1
 * This library is distributed in the hope that it will be useful,
Packit 0652a1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0652a1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0652a1
 * Library General Public License for more details.
Packit 0652a1
 *
Packit 0652a1
 * You should have received a copy of the GNU Library General Public
Packit 0652a1
 * License along with this library; if not, write to the
Packit 0652a1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 0652a1
 * Boston, MA 02110-1301, USA.
Packit 0652a1
 *
Packit 0652a1
 * Based on python script by Thiago Sousa Santos
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
#ifdef HAVE_CONFIG_H
Packit 0652a1
#include "config.h"
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include <gst/gst.h>
Packit 0652a1
Packit 0652a1
static gchar *opt_effects = NULL;
Packit 0652a1
Packit 0652a1
#define DEFAULT_EFFECTS "identity,exclusion,navigationtest," \
Packit 0652a1
    "agingtv,videoflip,vertigotv,gaussianblur,shagadelictv,edgetv"
Packit 0652a1
Packit 0652a1
static GstPad *blockpad;
Packit 0652a1
static GstElement *conv_before;
Packit 0652a1
static GstElement *conv_after;
Packit 0652a1
static GstElement *cur_effect;
Packit 0652a1
static GstElement *pipeline;
Packit 0652a1
Packit 0652a1
static GQueue effects = G_QUEUE_INIT;
Packit 0652a1
Packit 0652a1
static GstPadProbeReturn
Packit 0652a1
event_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
Packit 0652a1
{
Packit 0652a1
  GMainLoop *loop = user_data;
Packit 0652a1
  GstElement *next;
Packit 0652a1
Packit 0652a1
  if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_DATA (info)) != GST_EVENT_EOS)
Packit 0652a1
    return GST_PAD_PROBE_OK;
Packit 0652a1
Packit 0652a1
  gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
Packit 0652a1
Packit 0652a1
  /* push current event back into the queue */
Packit 0652a1
  g_queue_push_tail (&effects, gst_object_ref (cur_effect));
Packit 0652a1
  /* take next effect from the queue */
Packit 0652a1
  next = g_queue_pop_head (&effects);
Packit 0652a1
  if (next == NULL) {
Packit 0652a1
    GST_DEBUG_OBJECT (pad, "no more effects");
Packit 0652a1
    g_main_loop_quit (loop);
Packit 0652a1
    return GST_PAD_PROBE_DROP;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  g_print ("Switching from '%s' to '%s'..\n", GST_OBJECT_NAME (cur_effect),
Packit 0652a1
      GST_OBJECT_NAME (next));
Packit 0652a1
Packit 0652a1
  gst_element_set_state (cur_effect, GST_STATE_NULL);
Packit 0652a1
Packit 0652a1
  /* remove unlinks automatically */
Packit 0652a1
  GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, cur_effect);
Packit 0652a1
  gst_bin_remove (GST_BIN (pipeline), cur_effect);
Packit 0652a1
Packit 0652a1
  GST_DEBUG_OBJECT (pipeline, "adding   %" GST_PTR_FORMAT, next);
Packit 0652a1
  gst_bin_add (GST_BIN (pipeline), next);
Packit 0652a1
Packit 0652a1
  GST_DEBUG_OBJECT (pipeline, "linking..");
Packit 0652a1
  gst_element_link_many (conv_before, next, conv_after, NULL);
Packit 0652a1
Packit 0652a1
  gst_element_set_state (next, GST_STATE_PLAYING);
Packit 0652a1
Packit 0652a1
  cur_effect = next;
Packit 0652a1
  GST_DEBUG_OBJECT (pipeline, "done");
Packit 0652a1
Packit 0652a1
  return GST_PAD_PROBE_DROP;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static GstPadProbeReturn
Packit 0652a1
pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
Packit 0652a1
{
Packit 0652a1
  GstPad *srcpad, *sinkpad;
Packit 0652a1
Packit 0652a1
  GST_DEBUG_OBJECT (pad, "pad is blocked now");
Packit 0652a1
Packit 0652a1
  /* remove the probe first */
Packit 0652a1
  gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));
Packit 0652a1
Packit 0652a1
  /* install new probe for EOS */
Packit 0652a1
  srcpad = gst_element_get_static_pad (cur_effect, "src");
Packit 0652a1
  gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK |
Packit 0652a1
      GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, event_probe_cb, user_data, NULL);
Packit 0652a1
  gst_object_unref (srcpad);
Packit 0652a1
Packit 0652a1
  /* push EOS into the element, the probe will be fired when the
Packit 0652a1
   * EOS leaves the effect and it has thus drained all of its data */
Packit 0652a1
  sinkpad = gst_element_get_static_pad (cur_effect, "sink");
Packit 0652a1
  gst_pad_send_event (sinkpad, gst_event_new_eos ());
Packit 0652a1
  gst_object_unref (sinkpad);
Packit 0652a1
Packit 0652a1
  return GST_PAD_PROBE_OK;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static gboolean
Packit 0652a1
timeout_cb (gpointer user_data)
Packit 0652a1
{
Packit 0652a1
  gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
Packit 0652a1
      pad_probe_cb, user_data, NULL);
Packit 0652a1
Packit 0652a1
  return TRUE;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static gboolean
Packit 0652a1
bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
Packit 0652a1
{
Packit 0652a1
  GMainLoop *loop = user_data;
Packit 0652a1
Packit 0652a1
  switch (GST_MESSAGE_TYPE (msg)) {
Packit 0652a1
    case GST_MESSAGE_ERROR:{
Packit 0652a1
      GError *err = NULL;
Packit 0652a1
      gchar *dbg;
Packit 0652a1
Packit 0652a1
      gst_message_parse_error (msg, &err, &dbg;;
Packit 0652a1
      gst_object_default_error (msg->src, err, dbg);
Packit 0652a1
      g_clear_error (&err;;
Packit 0652a1
      g_free (dbg);
Packit 0652a1
      g_main_loop_quit (loop);
Packit 0652a1
      break;
Packit 0652a1
    }
Packit 0652a1
    default:
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
  return TRUE;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
int
Packit 0652a1
main (int argc, char **argv)
Packit 0652a1
{
Packit 0652a1
  GOptionEntry options[] = {
Packit 0652a1
    {"effects", 'e', 0, G_OPTION_ARG_STRING, &opt_effects,
Packit 0652a1
        "Effects to use (comma-separated list of element names)", NULL},
Packit 0652a1
    {NULL}
Packit 0652a1
  };
Packit 0652a1
  GOptionContext *ctx;
Packit 0652a1
  GError *err = NULL;
Packit 0652a1
  GMainLoop *loop;
Packit 0652a1
  GstElement *src, *q1, *q2, *effect, *filter1, *filter2, *sink;
Packit 0652a1
  gchar **effect_names, **e;
Packit 0652a1
Packit 0652a1
  ctx = g_option_context_new ("");
Packit 0652a1
  g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
Packit 0652a1
  g_option_context_add_group (ctx, gst_init_get_option_group ());
Packit 0652a1
  if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
Packit 0652a1
    g_print ("Error initializing: %s\n", err->message);
Packit 0652a1
    g_option_context_free (ctx);
Packit 0652a1
    g_clear_error (&err;;
Packit 0652a1
    return 1;
Packit 0652a1
  }
Packit 0652a1
  g_option_context_free (ctx);
Packit 0652a1
Packit 0652a1
  GST_FIXME ("Multiple things to check/fix, see source code");
Packit 0652a1
Packit 0652a1
  if (opt_effects != NULL)
Packit 0652a1
    effect_names = g_strsplit (opt_effects, ",", -1);
Packit 0652a1
  else
Packit 0652a1
    effect_names = g_strsplit (DEFAULT_EFFECTS, ",", -1);
Packit 0652a1
Packit 0652a1
  for (e = effect_names; e != NULL && *e != NULL; ++e) {
Packit 0652a1
    GstElement *el;
Packit 0652a1
Packit 0652a1
    el = gst_element_factory_make (*e, NULL);
Packit 0652a1
    if (el) {
Packit 0652a1
      g_print ("Adding effect '%s'\n", *e);
Packit 0652a1
      g_queue_push_tail (&effects, el);
Packit 0652a1
    }
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  pipeline = gst_pipeline_new ("pipeline");
Packit 0652a1
Packit 0652a1
  src = gst_element_factory_make ("videotestsrc", NULL);
Packit 0652a1
  g_object_set (src, "is-live", TRUE, NULL);
Packit 0652a1
Packit 0652a1
  filter1 = gst_element_factory_make ("capsfilter", NULL);
Packit 0652a1
  gst_util_set_object_arg (G_OBJECT (filter1), "caps",
Packit 0652a1
      "video/x-raw, width=320, height=240, "
Packit 0652a1
      "format={ I420, YV12, YUY2, UYVY, AYUV, Y41B, Y42B, "
Packit 0652a1
      "YVYU, Y444, v210, v216, NV12, NV21, UYVP, A420, YUV9, YVU9, IYU1 }");
Packit 0652a1
Packit 0652a1
  q1 = gst_element_factory_make ("queue", NULL);
Packit 0652a1
Packit 0652a1
  blockpad = gst_element_get_static_pad (q1, "src");
Packit 0652a1
Packit 0652a1
  conv_before = gst_element_factory_make ("videoconvert", NULL);
Packit 0652a1
Packit 0652a1
  effect = g_queue_pop_head (&effects);
Packit 0652a1
  cur_effect = effect;
Packit 0652a1
Packit 0652a1
  conv_after = gst_element_factory_make ("videoconvert", NULL);
Packit 0652a1
Packit 0652a1
  q2 = gst_element_factory_make ("queue", NULL);
Packit 0652a1
Packit 0652a1
  filter2 = gst_element_factory_make ("capsfilter", NULL);
Packit 0652a1
  gst_util_set_object_arg (G_OBJECT (filter2), "caps",
Packit 0652a1
      "video/x-raw, width=320, height=240, "
Packit 0652a1
      "format={ RGBx, BGRx, xRGB, xBGR, RGBA, BGRA, ARGB, ABGR, RGB, BGR }");
Packit 0652a1
Packit 0652a1
  sink = gst_element_factory_make ("ximagesink", NULL);
Packit 0652a1
Packit 0652a1
  gst_bin_add_many (GST_BIN (pipeline), src, filter1, q1, conv_before, effect,
Packit 0652a1
      conv_after, q2, sink, NULL);
Packit 0652a1
Packit 0652a1
  gst_element_link_many (src, filter1, q1, conv_before, effect, conv_after,
Packit 0652a1
      q2, sink, NULL);
Packit 0652a1
Packit 0652a1
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 0652a1
Packit 0652a1
  loop = g_main_loop_new (NULL, FALSE);
Packit 0652a1
Packit 0652a1
  gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, loop);
Packit 0652a1
Packit 0652a1
  g_timeout_add_seconds (1, timeout_cb, loop);
Packit 0652a1
Packit 0652a1
  g_main_loop_run (loop);
Packit 0652a1
Packit 0652a1
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 0652a1
  gst_object_unref (pipeline);
Packit 0652a1
Packit 0652a1
  return 0;
Packit 0652a1
}