Blame tests/check/pipelines/capsfilter-renegotiation.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
Packit 971217
 *
Packit 971217
 * capsfilter-renegotiation.c: Unit test for capsfilter caps renegotiation
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
/* Ideally this would be in core, but using videotestsrc makes it easier */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/check/gstcheck.h>
Packit 971217
Packit 971217
#define FIRST_CAPS  "video/x-raw,width=(int)480,height=(int)320"
Packit 971217
#define SECOND_CAPS "video/x-raw,width=(int)120,height=(int)100"
Packit 971217
#define THIRD_CAPS  "video/x-raw,width=(int)[10,50],height=(int)[100,200]"
Packit 971217
#define FOURTH_CAPS "video/x-raw,width=(int)300,height=(int)[25,75];" \
Packit 971217
                    "video/x-raw,width=(int)[30,40]," \
Packit 971217
                    "height=(int)[100,200],format=(string)YUY2"
Packit 971217
Packit 971217
int buffer_count = 0;
Packit 971217
GstCaps *current_caps = NULL;
Packit 971217
int caps_change = 0;
Packit 971217
Packit 971217
static GstPadProbeReturn
Packit 971217
buffer_probe (GstPad * pad, GstPadProbeInfo * info, gpointer data)
Packit 971217
{
Packit 971217
  GstCaps *pad_caps;
Packit 971217
  GstElement *capsfilter = GST_ELEMENT (data);
Packit 971217
  GstCaps *caps = NULL;
Packit 971217
Packit 971217
  /* increment the buffer count and check if it is time to change the caps */
Packit 971217
  buffer_count++;
Packit 971217
  if (buffer_count == 50) {
Packit 971217
    /* change the caps to another one */
Packit 971217
    caps = gst_caps_from_string (SECOND_CAPS);
Packit 971217
  } else if (buffer_count == 100) {
Packit 971217
    /* change the caps to another one, this time unfixed */
Packit 971217
    caps = gst_caps_from_string (THIRD_CAPS);
Packit 971217
  } else if (buffer_count == 150) {
Packit 971217
    /* change the caps to another one,
Packit 971217
     * this time unfixed with multiple entries */
Packit 971217
    caps = gst_caps_from_string (FOURTH_CAPS);
Packit 971217
  }
Packit 971217
  /* set the caps */
Packit 971217
  if (caps) {
Packit 971217
    g_object_set (capsfilter, "caps", caps, NULL);
Packit 971217
    gst_caps_unref (caps);
Packit 971217
  }
Packit 971217
  /* now check if the pad caps has changed since last check */
Packit 971217
  pad_caps = gst_pad_get_current_caps (pad);
Packit 971217
  if (current_caps == NULL && pad_caps != NULL) {
Packit 971217
    /* probably the first caps, this is a change */
Packit 971217
    current_caps = gst_caps_copy (pad_caps);
Packit 971217
    caps_change++;
Packit 971217
  } else if (current_caps != NULL) {
Packit 971217
    if (pad_caps == NULL) {
Packit 971217
      /* caps was set to NULL, we consider this a change */
Packit 971217
      gst_caps_unref (current_caps);
Packit 971217
      current_caps = NULL;
Packit 971217
      caps_change++;
Packit 971217
    } else {
Packit 971217
      if (!gst_caps_is_equal (current_caps, pad_caps)) {
Packit 971217
        /* a caps change */
Packit 971217
        gst_caps_unref (current_caps);
Packit 971217
        current_caps = gst_caps_copy (pad_caps);
Packit 971217
        caps_change++;
Packit 971217
      }
Packit 971217
    }
Packit 971217
  }
Packit 971217
  gst_caps_unref (pad_caps);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
/* launch line is a pipeline that must have a capsfilter named 'cf' that
Packit 971217
 * will be used to trigger the renegotiation */
Packit 971217
static void
Packit 971217
run_capsfilter_renegotiation (const gchar * launch_line)
Packit 971217
{
Packit 971217
  GstElement *capsfilter;
Packit 971217
  GstElement *sink;
Packit 971217
  GstElement *pipeline;
Packit 971217
  GstBus *bus;
Packit 971217
  GstMessage *msg;
Packit 971217
  GstPad *pad;
Packit 971217
Packit 971217
  caps_change = 0;
Packit 971217
  buffer_count = 0;
Packit 971217
  if (current_caps)
Packit 971217
    gst_caps_unref (current_caps);
Packit 971217
  current_caps = NULL;
Packit 971217
Packit 971217
  pipeline = gst_parse_launch (launch_line, NULL);
Packit 971217
  fail_unless (pipeline != NULL);
Packit 971217
Packit 971217
  capsfilter = gst_bin_get_by_name (GST_BIN (pipeline), "cf");
Packit 971217
  fail_unless (capsfilter != NULL);
Packit 971217
Packit 971217
  sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
Packit 971217
  fail_unless (sink != NULL);
Packit 971217
Packit 971217
  pad = gst_element_get_static_pad (sink, "sink");
Packit 971217
  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, buffer_probe, capsfilter,
Packit 971217
      NULL);
Packit 971217
  gst_object_unref (pad);
Packit 971217
Packit 971217
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
Packit 971217
Packit 971217
  fail_unless (gst_element_set_state (pipeline, GST_STATE_PLAYING) !=
Packit 971217
      GST_STATE_CHANGE_FAILURE);
Packit 971217
Packit 971217
  msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
Packit 971217
      GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
Packit 971217
Packit 971217
  fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_EOS);
Packit 971217
  fail_unless_equals_int (caps_change, 4);
Packit 971217
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
Packit 971217
  if (current_caps)
Packit 971217
    gst_caps_unref (current_caps);
Packit 971217
  current_caps = NULL;
Packit 971217
  gst_message_unref (msg);
Packit 971217
  g_object_unref (bus);
Packit 971217
  gst_object_unref (sink);
Packit 971217
  gst_object_unref (capsfilter);
Packit 971217
  g_object_unref (G_OBJECT (pipeline));
Packit 971217
}
Packit 971217
Packit 971217
GST_START_TEST (test_capsfilter_renegotiation)
Packit 971217
{
Packit 971217
  run_capsfilter_renegotiation ("videotestsrc num-buffers=200 "
Packit 971217
      " ! capsfilter caps=\"" FIRST_CAPS "\" name=cf ! fakesink name=sink");
Packit 971217
  run_capsfilter_renegotiation ("videotestsrc num-buffers=200 "
Packit 971217
      " ! capsfilter caps=\"" FIRST_CAPS "\" name=cf ! fakesink name=sink");
Packit 971217
  run_capsfilter_renegotiation ("videotestsrc num-buffers=200 "
Packit 971217
      " ! capsfilter caps=\"video/x-raw, format=(string)I420, width=(int)100, height=(int)100\" "
Packit 971217
      " ! videoconvert ! videoscale ! capsfilter caps=\"" FIRST_CAPS
Packit 971217
      "\" name=cf " " ! fakesink name=sink");
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
static Suite *
Packit 971217
capsfilter_renegotiation_suite (void)
Packit 971217
{
Packit 971217
  Suite *s = suite_create ("CapsfilterRenegotiation");
Packit 971217
  TCase *tc_chain = tcase_create ("linear");
Packit 971217
Packit 971217
  /* time out after 60s, not the default 3 */
Packit 971217
  tcase_set_timeout (tc_chain, 60);
Packit 971217
Packit 971217
  suite_add_tcase (s, tc_chain);
Packit 971217
  tcase_add_test (tc_chain, test_capsfilter_renegotiation);
Packit 971217
  return s;
Packit 971217
}
Packit 971217
Packit 971217
GST_CHECK_MAIN (capsfilter_renegotiation);