Blame tests/check/elements/videotestsrc.c

Packit 971217
/* GStreamer
Packit 971217
 *
Packit 971217
 * unit test for videotestsrc
Packit 971217
 *
Packit 971217
 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
Packit 971217
 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
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
#ifdef HAVE_CONFIG_H
Packit 971217
# include <config.h>
Packit 971217
#endif
Packit 971217
Packit 971217
#ifdef HAVE_VALGRIND
Packit 971217
# include <valgrind/valgrind.h>
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/check/gstcheck.h>
Packit 971217
#include <gst/check/gstharness.h>
Packit 971217
Packit 971217
/* For ease of programming we use globals to keep refs for our floating
Packit 971217
 * src and sink pads we create; otherwise we always have to do get_pad,
Packit 971217
 * get_peer, and then remove references in every test function */
Packit 971217
static GstPad *mysinkpad;
Packit 971217
Packit 971217
Packit 971217
#define CAPS_TEMPLATE_STRING            \
Packit 971217
    "video/x-raw, "                 \
Packit 971217
    "format = (string) UYVY, "          \
Packit 971217
    "width = (int) [ 1,  MAX ], "       \
Packit 971217
    "height = (int) [ 1,  MAX ], "      \
Packit 971217
    "framerate = (fraction) [ 0/1, MAX ]"
Packit 971217
Packit 971217
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
Packit 971217
    GST_PAD_SINK,
Packit 971217
    GST_PAD_ALWAYS,
Packit 971217
    GST_STATIC_CAPS (CAPS_TEMPLATE_STRING)
Packit 971217
    );
Packit 971217
Packit 971217
static GstElement *
Packit 971217
setup_videotestsrc (void)
Packit 971217
{
Packit 971217
  GstElement *videotestsrc;
Packit 971217
Packit 971217
  GST_DEBUG ("setup_videotestsrc");
Packit 971217
  videotestsrc = gst_check_setup_element ("videotestsrc");
Packit 971217
  mysinkpad = gst_check_setup_sink_pad (videotestsrc, &sinktemplate);
Packit 971217
  gst_pad_set_active (mysinkpad, TRUE);
Packit 971217
Packit 971217
  return videotestsrc;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
cleanup_videotestsrc (GstElement * videotestsrc)
Packit 971217
{
Packit 971217
  GST_DEBUG ("cleanup_videotestsrc");
Packit 971217
Packit 971217
  gst_check_drop_buffers ();
Packit 971217
Packit 971217
  gst_pad_set_active (mysinkpad, FALSE);
Packit 971217
  gst_check_teardown_sink_pad (videotestsrc);
Packit 971217
  gst_check_teardown_element (videotestsrc);
Packit 971217
}
Packit 971217
Packit 971217
GST_START_TEST (test_all_patterns)
Packit 971217
{
Packit 971217
  GstElement *videotestsrc;
Packit 971217
  GObjectClass *oclass;
Packit 971217
  GParamSpec *property;
Packit 971217
  GEnumValue *values;
Packit 971217
  guint j = 0;
Packit 971217
Packit 971217
  videotestsrc = setup_videotestsrc ();
Packit 971217
  oclass = G_OBJECT_GET_CLASS (videotestsrc);
Packit 971217
  property = g_object_class_find_property (oclass, "pattern");
Packit 971217
  fail_unless (G_IS_PARAM_SPEC_ENUM (property));
Packit 971217
  values = G_ENUM_CLASS (g_type_class_ref (property->value_type))->values;
Packit 971217
Packit 971217
  while (values[j].value_name) {
Packit 971217
    GST_DEBUG_OBJECT (videotestsrc, "testing pattern %s", values[j].value_name);
Packit 971217
Packit 971217
    g_object_set (videotestsrc, "pattern", j, NULL);
Packit 971217
Packit 971217
    fail_unless (gst_element_set_state (videotestsrc,
Packit 971217
            GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
Packit 971217
        "could not set to playing");
Packit 971217
Packit 971217
    g_mutex_lock (&check_mutex);
Packit 971217
    while (g_list_length (buffers) < 10) {
Packit 971217
      GST_DEBUG_OBJECT (videotestsrc, "Waiting for more buffers");
Packit 971217
      g_cond_wait (&check_cond, &check_mutex);
Packit 971217
    }
Packit 971217
    g_mutex_unlock (&check_mutex);
Packit 971217
Packit 971217
    gst_element_set_state (videotestsrc, GST_STATE_READY);
Packit 971217
Packit 971217
    gst_check_drop_buffers ();
Packit 971217
    ++j;
Packit 971217
  }
Packit 971217
Packit 971217
  /* cleanup */
Packit 971217
  cleanup_videotestsrc (videotestsrc);
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
static guint32
Packit 971217
right_shift_colour (guint32 mask, guint32 pixel)
Packit 971217
{
Packit 971217
  if (mask == 0)
Packit 971217
    return 0;
Packit 971217
Packit 971217
  pixel = pixel & mask;
Packit 971217
  while ((mask & 0x01) == 0) {
Packit 971217
    mask = mask >> 1;
Packit 971217
    pixel = pixel >> 1;
Packit 971217
  }
Packit 971217
Packit 971217
  return pixel;
Packit 971217
}
Packit 971217
Packit 971217
static guint8
Packit 971217
fix_expected_colour (guint32 col_mask, guint8 col_expected)
Packit 971217
{
Packit 971217
  guint32 mask;
Packit 971217
  gint last = g_bit_nth_msf (col_mask, -1);
Packit 971217
  gint first = g_bit_nth_lsf (col_mask, -1);
Packit 971217
Packit 971217
  mask = 1 << (last - first + 1);
Packit 971217
  mask -= 1;
Packit 971217
Packit 971217
  g_assert (col_expected == 0x00 || col_expected == 0xff);
Packit 971217
Packit 971217
  /* this only works because we only check for all-bits-set or no-bits-set */
Packit 971217
  return col_expected & mask;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
check_rgb_buf (const guint8 * pixels, guint32 r_mask, guint32 g_mask,
Packit 971217
    guint32 b_mask, guint32 a_mask, guint8 r_expected, guint8 g_expected,
Packit 971217
    guint8 b_expected, guint bpp, guint depth)
Packit 971217
{
Packit 971217
  guint32 pixel, red, green, blue, alpha;
Packit 971217
Packit 971217
  switch (bpp) {
Packit 971217
    case 32:
Packit 971217
      pixel = GST_READ_UINT32_BE (pixels);
Packit 971217
      break;
Packit 971217
    case 24:
Packit 971217
      pixel = (GST_READ_UINT8 (pixels) << 16) |
Packit 971217
          (GST_READ_UINT8 (pixels + 1) << 8) |
Packit 971217
          (GST_READ_UINT8 (pixels + 2) << 0);
Packit 971217
      break;
Packit 971217
    case 16:
Packit 971217
      if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
Packit 971217
        pixel = GST_READ_UINT16_LE (pixels);
Packit 971217
      else
Packit 971217
        pixel = GST_READ_UINT16_BE (pixels);
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      g_return_if_reached ();
Packit 971217
  }
Packit 971217
Packit 971217
  red = right_shift_colour (r_mask, pixel);
Packit 971217
  green = right_shift_colour (g_mask, pixel);
Packit 971217
  blue = right_shift_colour (b_mask, pixel);
Packit 971217
  alpha = right_shift_colour (a_mask, pixel);
Packit 971217
Packit 971217
  /* can't enable this by default, valgrind will complain about accessing
Packit 971217
   * uninitialised memory for the depth=24,bpp=32 formats ... */
Packit 971217
  /* GST_LOG ("pixels: 0x%02x 0x%02x 0x%02x 0x%02x => pixel = 0x%08x",
Packit 971217
     pixels[0], (guint) pixels[1], pixels[2], pixels[3], pixel); */
Packit 971217
Packit 971217
  /* fix up the mask (for rgb15/16) */
Packit 971217
  if (bpp == 16) {
Packit 971217
    r_expected = fix_expected_colour (r_mask, r_expected);
Packit 971217
    g_expected = fix_expected_colour (g_mask, g_expected);
Packit 971217
    b_expected = fix_expected_colour (b_mask, b_expected);
Packit 971217
  }
Packit 971217
Packit 971217
  fail_unless (red == r_expected, "RED: expected 0x%02x, found 0x%02x",
Packit 971217
      r_expected, red);
Packit 971217
  fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x",
Packit 971217
      g_expected, green);
Packit 971217
  fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x",
Packit 971217
      b_expected, blue);
Packit 971217
Packit 971217
  fail_unless (a_mask == 0 || alpha != 0);      /* better than nothing */
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
Packit 971217
    GstSample ** p_old_sample)
Packit 971217
{
Packit 971217
  GstCaps *caps;
Packit 971217
Packit 971217
  caps = gst_pad_get_current_caps (pad);
Packit 971217
Packit 971217
  if (*p_old_sample)
Packit 971217
    gst_sample_unref (*p_old_sample);
Packit 971217
  *p_old_sample = gst_sample_new (new_buf, caps, NULL, NULL);
Packit 971217
Packit 971217
  gst_caps_unref (caps);
Packit 971217
}
Packit 971217
Packit 971217
/* tests the positioning of pixels within the various RGB pixel layouts */
Packit 971217
GST_START_TEST (test_rgb_formats)
Packit 971217
{
Packit 971217
  const struct
Packit 971217
  {
Packit 971217
    const gchar *pattern_name;
Packit 971217
    gint pattern_enum;
Packit 971217
    guint8 r_expected;
Packit 971217
    guint8 g_expected;
Packit 971217
    guint8 b_expected;
Packit 971217
  } test_patterns[] = {
Packit 971217
    {
Packit 971217
    "white", 3, 0xff, 0xff, 0xff}, {
Packit 971217
    "red", 4, 0xff, 0x00, 0x00}, {
Packit 971217
    "green", 5, 0x00, 0xff, 0x00}, {
Packit 971217
    "blue", 6, 0x00, 0x00, 0xff}, {
Packit 971217
    "black", 2, 0x00, 0x00, 0x00}
Packit 971217
  };
Packit 971217
  const struct
Packit 971217
  {
Packit 971217
    const gchar *nick;
Packit 971217
    guint bpp, depth;
Packit 971217
    guint32 red_mask, green_mask, blue_mask, alpha_mask;
Packit 971217
  } rgb_formats[] = {
Packit 971217
    {
Packit 971217
    "RGBA", 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff}, {
Packit 971217
    "ARGB", 32, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, {
Packit 971217
    "BGRA", 32, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff}, {
Packit 971217
    "ABGR", 32, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000}, {
Packit 971217
    "RGBx", 32, 24, 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000}, {
Packit 971217
    "xRGB", 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
Packit 971217
    "BGRx", 32, 24, 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000}, {
Packit 971217
    "xBGR", 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
Packit 971217
    "RGB", 24, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
Packit 971217
    "BGR", 24, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
Packit 971217
    "RGB16", 16, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000}, {
Packit 971217
    "RGB15", 16, 15, 0x00007c00, 0x000003e0, 0x0000001f, 0x0000000}
Packit 971217
  };
Packit 971217
  GstElement *pipeline, *src, *filter, *sink;
Packit 971217
  GstCaps *template_caps;
Packit 971217
  GstSample *sample = NULL;
Packit 971217
  GstPad *srcpad;
Packit 971217
  gint p, i, e;
Packit 971217
Packit 971217
  /* test check function */
Packit 971217
  fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
Packit 971217
Packit 971217
  pipeline = gst_pipeline_new ("pipeline");
Packit 971217
  src = gst_check_setup_element ("videotestsrc");
Packit 971217
  filter = gst_check_setup_element ("capsfilter");
Packit 971217
  sink = gst_check_setup_element ("fakesink");
Packit 971217
Packit 971217
  gst_bin_add_many (GST_BIN (pipeline), src, filter, sink, NULL);
Packit 971217
Packit 971217
  fail_unless (gst_element_link (src, filter));
Packit 971217
  fail_unless (gst_element_link (filter, sink));
Packit 971217
Packit 971217
  srcpad = gst_element_get_static_pad (src, "src");
Packit 971217
  template_caps = gst_pad_get_pad_template_caps (srcpad);
Packit 971217
Packit 971217
  g_object_set (sink, "signal-handoffs", TRUE, NULL);
Packit 971217
  g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &sample);
Packit 971217
Packit 971217
  GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
Packit 971217
Packit 971217
  for (i = 0; i < G_N_ELEMENTS (rgb_formats); ++i) {
Packit 971217
    for (e = 0; e < 2; ++e) {
Packit 971217
      GstCaps *caps;
Packit 971217
Packit 971217
      caps = gst_caps_new_simple ("video/x-raw",
Packit 971217
          "format", G_TYPE_STRING, rgb_formats[i].nick,
Packit 971217
          "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
Packit 971217
          "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
Packit 971217
Packit 971217
      if (gst_caps_is_subset (caps, template_caps)) {
Packit 971217
        /* caps are supported, let's run some tests then ... */
Packit 971217
        for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
Packit 971217
          GstStateChangeReturn state_ret;
Packit 971217
          GstMapInfo map;
Packit 971217
Packit 971217
          g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
Packit 971217
Packit 971217
          GST_INFO ("%5s %u/%u %08x %08x %08x %08x, pattern=%s",
Packit 971217
              rgb_formats[i].nick, rgb_formats[i].bpp, rgb_formats[i].depth,
Packit 971217
              rgb_formats[i].red_mask, rgb_formats[i].green_mask,
Packit 971217
              rgb_formats[i].blue_mask, rgb_formats[i].alpha_mask,
Packit 971217
              test_patterns[p].pattern_name);
Packit 971217
Packit 971217
          /* now get videotestsrc to produce a buffer with the given caps */
Packit 971217
          g_object_set (filter, "caps", caps, NULL);
Packit 971217
Packit 971217
          state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
Packit 971217
          fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
Packit 971217
              "pipeline _set_state() to PAUSED failed");
Packit 971217
          state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
Packit 971217
          fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
Packit 971217
              "pipeline failed going to PAUSED state");
Packit 971217
Packit 971217
          state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
          fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
Packit 971217
Packit 971217
          fail_unless (sample != NULL);
Packit 971217
Packit 971217
          /* check buffer caps */
Packit 971217
          {
Packit 971217
            GstBuffer *buf;
Packit 971217
            GstStructure *s;
Packit 971217
            GstCaps *caps;
Packit 971217
            const gchar *format;
Packit 971217
Packit 971217
            buf = gst_sample_get_buffer (sample);
Packit 971217
            fail_unless (buf != NULL);
Packit 971217
            caps = gst_sample_get_caps (sample);
Packit 971217
            fail_unless (caps != NULL);
Packit 971217
Packit 971217
            s = gst_caps_get_structure (caps, 0);
Packit 971217
            format = gst_structure_get_string (s, "format");
Packit 971217
            fail_unless (g_str_equal (format, rgb_formats[i].nick));
Packit 971217
Packit 971217
            /* now check the first pixel */
Packit 971217
            gst_buffer_map (buf, &map, GST_MAP_READ);
Packit 971217
            check_rgb_buf (map.data, rgb_formats[i].red_mask,
Packit 971217
                rgb_formats[i].green_mask, rgb_formats[i].blue_mask,
Packit 971217
                rgb_formats[i].alpha_mask, test_patterns[p].r_expected,
Packit 971217
                test_patterns[p].g_expected, test_patterns[p].b_expected,
Packit 971217
                rgb_formats[i].bpp, rgb_formats[i].depth);
Packit 971217
            gst_buffer_unmap (buf, &map);
Packit 971217
Packit 971217
            gst_sample_unref (sample);
Packit 971217
            sample = NULL;
Packit 971217
          }
Packit 971217
        }
Packit 971217
Packit 971217
      } else {
Packit 971217
        GST_INFO ("videotestsrc doesn't support format %" GST_PTR_FORMAT, caps);
Packit 971217
      }
Packit 971217
Packit 971217
      gst_caps_unref (caps);
Packit 971217
    }
Packit 971217
  }
Packit 971217
  gst_caps_unref (template_caps);
Packit 971217
  gst_object_unref (srcpad);
Packit 971217
Packit 971217
  gst_object_unref (pipeline);
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
struct BackwardsPlaybackData
Packit 971217
{
Packit 971217
  GstClockTime last_ts;
Packit 971217
  const gchar *error_msg;
Packit 971217
};
Packit 971217
Packit 971217
static gboolean
Packit 971217
eos_watch (GstBus * bus, GstMessage * message, GMainLoop * loop)
Packit 971217
{
Packit 971217
  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS) {
Packit 971217
    g_main_loop_quit (loop);
Packit 971217
  }
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static GstPadProbeReturn
Packit 971217
backward_check_probe (GstPad * pad, GstPadProbeInfo * info, gpointer udata)
Packit 971217
{
Packit 971217
  if (info->type & GST_PAD_PROBE_TYPE_BUFFER) {
Packit 971217
    GstBuffer *buf = info->data;
Packit 971217
    struct BackwardsPlaybackData *pdata = udata;
Packit 971217
Packit 971217
    if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
Packit 971217
      if (GST_CLOCK_TIME_IS_VALID (pdata->last_ts) &&
Packit 971217
          pdata->last_ts < GST_BUFFER_TIMESTAMP (buf)) {
Packit 971217
        pdata->error_msg = "Received buffer with increasing timestamp";
Packit 971217
      }
Packit 971217
      pdata->last_ts = GST_BUFFER_TIMESTAMP (buf);
Packit 971217
    } else {
Packit 971217
      pdata->error_msg = "Received buffer without timestamp";
Packit 971217
    }
Packit 971217
Packit 971217
  }
Packit 971217
  return GST_PAD_PROBE_OK;
Packit 971217
}
Packit 971217
Packit 971217
GST_START_TEST (test_backward_playback)
Packit 971217
{
Packit 971217
  GstBus *bus;
Packit 971217
  GstElement *bin;
Packit 971217
  GError *error = NULL;
Packit 971217
  GMainLoop *loop;
Packit 971217
  guint bus_watch = 0;
Packit 971217
  GstStateChangeReturn ret;
Packit 971217
  GstElement *src;
Packit 971217
  GstPad *pad;
Packit 971217
  gulong pad_probe;
Packit 971217
  struct BackwardsPlaybackData pdata;
Packit 971217
Packit 971217
  pdata.last_ts = GST_CLOCK_TIME_NONE;
Packit 971217
  pdata.error_msg = NULL;
Packit 971217
Packit 971217
  bin = gst_parse_launch ("videotestsrc name=src ! fakesink name=sink "
Packit 971217
      "sync=true", &error);
Packit 971217
Packit 971217
  /* run until we receive EOS */
Packit 971217
  loop = g_main_loop_new (NULL, FALSE);
Packit 971217
  bus = gst_element_get_bus (bin);
Packit 971217
  bus_watch = gst_bus_add_watch (bus, (GstBusFunc) eos_watch, loop);
Packit 971217
  gst_object_unref (bus);
Packit 971217
Packit 971217
Packit 971217
  ret = gst_element_set_state (bin, GST_STATE_PAUSED);
Packit 971217
Packit 971217
  if (ret == GST_STATE_CHANGE_ASYNC) {
Packit 971217
    ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
Packit 971217
    fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not start test pipeline");
Packit 971217
  }
Packit 971217
Packit 971217
  src = gst_bin_get_by_name (GST_BIN (bin), "src");
Packit 971217
  pad = gst_element_get_static_pad (src, "src");
Packit 971217
  pad_probe = gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
Packit 971217
      (GstPadProbeCallback) backward_check_probe, &pdata, NULL);
Packit 971217
Packit 971217
  gst_element_seek (bin, -1.0, GST_FORMAT_TIME,
Packit 971217
      GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET,
Packit 971217
      0, GST_SEEK_TYPE_SET, 1 * GST_SECOND);
Packit 971217
Packit 971217
  ret = gst_element_set_state (bin, GST_STATE_PLAYING);
Packit 971217
  fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not start test pipeline");
Packit 971217
  if (ret == GST_STATE_CHANGE_ASYNC) {
Packit 971217
    ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
Packit 971217
    fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not start test pipeline");
Packit 971217
  }
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  ret = gst_element_set_state (bin, GST_STATE_NULL);
Packit 971217
  fail_if (ret == GST_STATE_CHANGE_FAILURE, "Could not stop test pipeline");
Packit 971217
  if (ret == GST_STATE_CHANGE_ASYNC) {
Packit 971217
    ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
Packit 971217
    fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not stop test pipeline");
Packit 971217
  }
Packit 971217
Packit 971217
  if (pdata.error_msg)
Packit 971217
    fail ("%s", pdata.error_msg);
Packit 971217
Packit 971217
  /* clean up */
Packit 971217
  gst_pad_remove_probe (pad, pad_probe);
Packit 971217
  gst_object_unref (pad);
Packit 971217
  gst_object_unref (src);
Packit 971217
  g_main_loop_unref (loop);
Packit 971217
  g_source_remove (bus_watch);
Packit 971217
  gst_object_unref (bin);
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
GST_START_TEST (test_duration_query)
Packit 971217
{
Packit 971217
  GstElement *bin;
Packit 971217
  GError *error = NULL;
Packit 971217
  GstStateChangeReturn ret;
Packit 971217
  gboolean queryret;
Packit 971217
  gint64 duration = -1;
Packit 971217
Packit 971217
  bin =
Packit 971217
      gst_parse_launch ("videotestsrc ! fakesink name=sink sync=true", &error);
Packit 971217
  ret = gst_element_set_state (bin, GST_STATE_PAUSED);
Packit 971217
  if (ret == GST_STATE_CHANGE_ASYNC) {
Packit 971217
    ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
Packit 971217
    fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not start test pipeline");
Packit 971217
  }
Packit 971217
  queryret = gst_element_query_duration (bin, GST_FORMAT_TIME, &duration);
Packit 971217
  /* should have unknown duration */
Packit 971217
  if (queryret && duration != -1) {
Packit 971217
    fail ("Should return false on duration query");
Packit 971217
  }
Packit 971217
  gst_element_set_state (bin, GST_STATE_NULL);
Packit 971217
  gst_object_unref (bin);
Packit 971217
Packit 971217
  bin = gst_parse_launch ("videotestsrc num-buffers=100 ! capsfilter "
Packit 971217
      "caps=\"video/x-raw,framerate=(fraction)10/1\" ! "
Packit 971217
      "fakesink name=sink sync=true", &error);
Packit 971217
  ret = gst_element_set_state (bin, GST_STATE_PAUSED);
Packit 971217
  if (ret == GST_STATE_CHANGE_ASYNC) {
Packit 971217
    ret = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
Packit 971217
    fail_if (ret != GST_STATE_CHANGE_SUCCESS, "Could not start test pipeline");
Packit 971217
  }
Packit 971217
  queryret = gst_element_query_duration (bin, GST_FORMAT_TIME, &duration);
Packit 971217
  fail_unless (queryret, "Duration should be returned");
Packit 971217
  fail_unless (duration == GST_SECOND * 10, "Expected duration didn't match");
Packit 971217
Packit 971217
  /* reverse playback should have no impact on duration */
Packit 971217
  gst_element_seek (bin, -1.0, GST_FORMAT_TIME,
Packit 971217
      GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET,
Packit 971217
      0, GST_SEEK_TYPE_SET, 1 * GST_SECOND);
Packit 971217
  queryret = gst_element_query_duration (bin, GST_FORMAT_TIME, &duration);
Packit 971217
  fail_unless (queryret, "Duration should be returned");
Packit 971217
  fail_unless (duration == GST_SECOND * 10, "Expected duration didn't match");
Packit 971217
Packit 971217
  gst_element_set_state (bin, GST_STATE_NULL);
Packit 971217
  gst_object_unref (bin);
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
static gchar *
Packit 971217
get_buffer_checksum (GstBuffer * buf)
Packit 971217
{
Packit 971217
  GstMapInfo map = GST_MAP_INFO_INIT;
Packit 971217
  GChecksum *md5 = g_checksum_new (G_CHECKSUM_MD5);
Packit 971217
  gchar *ret;
Packit 971217
Packit 971217
  gst_buffer_map (buf, &map, GST_MAP_READ);
Packit 971217
  g_checksum_update (md5, map.data, map.size);
Packit 971217
  gst_buffer_unmap (buf, &map);
Packit 971217
Packit 971217
  ret = g_strdup (g_checksum_get_string (md5));
Packit 971217
  g_checksum_free (md5);
Packit 971217
Packit 971217
  return ret;
Packit 971217
}
Packit 971217
Packit 971217
GST_START_TEST (test_patterns_are_deterministic)
Packit 971217
{
Packit 971217
  GType type;
Packit 971217
  GEnumClass *enum_class;
Packit 971217
  gint num_patterns;
Packit 971217
  GstHarness *h[2];
Packit 971217
  const gint num_instances = G_N_ELEMENTS (h);
Packit 971217
  const gint num_frames = 2;
Packit 971217
  gint pattern, i, frame;
Packit 971217
Packit 971217
  /* Create an element to register types used below */
Packit 971217
  gst_object_unref (gst_element_factory_make ("videotestsrc", NULL));
Packit 971217
Packit 971217
  /* Find number of patterns to check */
Packit 971217
  type = g_type_from_name ("GstVideoTestSrcPattern");
Packit 971217
  fail_unless (type != 0);
Packit 971217
  enum_class = g_type_class_ref (type);
Packit 971217
  num_patterns = enum_class->n_values;
Packit 971217
  fail_unless (num_patterns > 0);
Packit 971217
  g_type_class_unref (enum_class);
Packit 971217
Packit 971217
  /* For each pattern, make sure that all instances produce identical
Packit 971217
   * frames */
Packit 971217
  for (pattern = 0; pattern < num_patterns; pattern++) {
Packit 971217
Packit 971217
    for (i = 0; i < G_N_ELEMENTS (h); i++) {
Packit 971217
      h[i] = gst_harness_new ("videotestsrc");
Packit 971217
      g_object_set (h[i]->element, "pattern", pattern, NULL);
Packit 971217
      gst_harness_set_blocking_push_mode (h[i]);
Packit 971217
      gst_harness_play (h[i]);
Packit 971217
    }
Packit 971217
Packit 971217
    for (frame = 0; frame < num_frames; frame++) {
Packit 971217
      gchar *ref_checksum = NULL;
Packit 971217
Packit 971217
      for (i = 0; i < num_instances; i++) {
Packit 971217
        GstBuffer *buffer = gst_harness_pull (h[i]);
Packit 971217
        gchar *checksum = get_buffer_checksum (buffer);
Packit 971217
Packit 971217
        if (i == 0)
Packit 971217
          ref_checksum = g_strdup (checksum);
Packit 971217
Packit 971217
        fail_unless_equals_string (ref_checksum, checksum);
Packit 971217
Packit 971217
        g_free (checksum);
Packit 971217
        gst_buffer_unref (buffer);
Packit 971217
      }
Packit 971217
      g_free (ref_checksum);
Packit 971217
    }
Packit 971217
    for (i = 0; i < G_N_ELEMENTS (h); i++)
Packit 971217
      gst_harness_teardown (h[i]);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
Packit 971217
Packit 971217
/* FIXME: add tests for YUV formats */
Packit 971217
Packit 971217
static Suite *
Packit 971217
videotestsrc_suite (void)
Packit 971217
{
Packit 971217
  Suite *s = suite_create ("videotestsrc");
Packit 971217
  TCase *tc_chain = tcase_create ("general");
Packit 971217
Packit 971217
  suite_add_tcase (s, tc_chain);
Packit 971217
Packit 971217
#ifdef HAVE_VALGRIND
Packit 971217
  if (RUNNING_ON_VALGRIND) {
Packit 971217
    /* test_rgb_formats takes a bit longer, so increase timeout */
Packit 971217
    tcase_set_timeout (tc_chain, 5 * 60);
Packit 971217
  }
Packit 971217
#endif
Packit 971217
Packit 971217
  tcase_add_test (tc_chain, test_all_patterns);
Packit 971217
  tcase_add_test (tc_chain, test_rgb_formats);
Packit 971217
  tcase_add_test (tc_chain, test_backward_playback);
Packit 971217
  tcase_add_test (tc_chain, test_duration_query);
Packit 971217
  tcase_add_test (tc_chain, test_patterns_are_deterministic);
Packit 971217
Packit 971217
  return s;
Packit 971217
}
Packit 971217
Packit 971217
GST_CHECK_MAIN (videotestsrc);