capssetter.c 6.5 KB
Newer Older
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* GStreamer
 *
 * unit test for capssetter
 *
 * Copyright (C) <2009> Mark Nauwelaerts <mnauw@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <unistd.h>

#include <gst/check/gstcheck.h>


/* For ease of programming we use globals to keep refs for our floating
 * src and sink pads we create; otherwise we always have to do get_pad,
 * get_peer, and then remove references in every test function */
static GstPad *mysrcpad, *mysinkpad;


static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

static GstElement *
setup_capssetter (void)
{
  GstElement *capssetter;

  GST_DEBUG ("setup_capssetter");

  capssetter = gst_check_setup_element ("capssetter");
  mysrcpad = gst_check_setup_src_pad (capssetter, &srctemplate, NULL);
  mysinkpad = gst_check_setup_sink_pad (capssetter, &sinktemplate, NULL);
  gst_pad_set_active (mysrcpad, TRUE);
  gst_pad_set_active (mysinkpad, TRUE);

  return capssetter;
}

static void
cleanup_capssetter (GstElement * capssetter)
{
  GST_DEBUG ("cleanup_capssetter");

  gst_pad_set_active (mysrcpad, FALSE);
  gst_pad_set_active (mysinkpad, FALSE);
  gst_check_teardown_src_pad (capssetter);
  gst_check_teardown_sink_pad (capssetter);
  gst_check_teardown_element (capssetter);
}

static void
push_and_test (GstCaps * prop_caps, gboolean join, gboolean replace,
    GstCaps * in_caps, GstCaps * out_caps)
{
  GstElement *capssetter;
  GstBuffer *buffer;

  capssetter = setup_capssetter ();
  fail_unless (gst_element_set_state (capssetter,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");

  buffer = gst_buffer_new_and_alloc (4);
  ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
  memcpy (GST_BUFFER_DATA (buffer), "data", 4);

  gst_buffer_set_caps (buffer, in_caps);
  gst_caps_unref (in_caps);

  g_object_set (capssetter, "join", join, NULL);
  g_object_set (capssetter, "replace", replace, NULL);
  g_object_set (capssetter, "caps", prop_caps, NULL);
  gst_caps_unref (prop_caps);

  /* pushing gives away my reference ... */
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK,
      "Failed pushing buffer to capssetter");

  fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);

  /* ... but it should end up being collected on the global buffer list */
  fail_unless (g_list_length (buffers) == 1);
  buffer = g_list_first (buffers)->data;
  ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);

  fail_unless (gst_caps_is_equal (out_caps, GST_BUFFER_CAPS (buffer)));
  gst_caps_unref (out_caps);

  /* cleanup */
  cleanup_capssetter (capssetter);
}

#define SRC_WIDTH   8
#define SRC_HEIGHT 12

static GstCaps *
Benjamin Otte's avatar
Benjamin Otte committed
117
make_src_caps (void)
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
{
  return gst_caps_new_simple ("video/x-raw-yuv", "width", G_TYPE_INT, SRC_WIDTH,
      "height", G_TYPE_INT, SRC_HEIGHT, NULL);
}

/* don't try these caps mutations at home, folks */

GST_START_TEST (test_n_join_n_replace)
{
  GstCaps *in_caps, *prop_caps, *out_caps;

  in_caps = make_src_caps ();
  prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
      "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
  out_caps = gst_caps_new_simple ("video/x-raw-rgb",
      "width", G_TYPE_INT, 2 * SRC_WIDTH,
      "height", G_TYPE_INT, SRC_HEIGHT, NULL);
  push_and_test (prop_caps, FALSE, FALSE, in_caps, out_caps);
}

GST_END_TEST;

GST_START_TEST (test_n_join_replace)
{
  GstCaps *in_caps, *prop_caps, *out_caps;

  in_caps = make_src_caps ();
  prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
      "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
  out_caps = gst_caps_copy (prop_caps);
  push_and_test (prop_caps, FALSE, TRUE, in_caps, out_caps);
}

GST_END_TEST;

GST_START_TEST (test_join_n_replace_n_match)
{
  GstCaps *in_caps, *prop_caps, *out_caps;

  /* non joining caps */
  in_caps = make_src_caps ();
  prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
      "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
  out_caps = gst_caps_copy (in_caps);
  push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
}

GST_END_TEST;

GST_START_TEST (test_join_n_replace_match)
{
  GstCaps *in_caps, *prop_caps, *out_caps;

  /* joining caps */
  in_caps = make_src_caps ();
  prop_caps = gst_caps_new_simple ("video/x-raw-yuv",
      "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
  out_caps = gst_caps_new_simple ("video/x-raw-yuv",
      "width", G_TYPE_INT, 2 * SRC_WIDTH,
      "height", G_TYPE_INT, SRC_HEIGHT, NULL);
  push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
}

GST_END_TEST;

GST_START_TEST (test_join_replace_n_match)
{
  GstCaps *in_caps, *prop_caps, *out_caps;

  /* non joining caps */
  in_caps = make_src_caps ();
  prop_caps = gst_caps_new_simple ("video/x-raw-rgb",
      "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
  out_caps = gst_caps_copy (in_caps);
  push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
}

GST_END_TEST;

GST_START_TEST (test_join_replace_match)
{
  GstCaps *in_caps, *prop_caps, *out_caps;

  /* joining caps */
  in_caps = make_src_caps ();
  prop_caps = gst_caps_new_simple ("video/x-raw-yuv",
      "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
  out_caps = gst_caps_copy (prop_caps);
  push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
}

GST_END_TEST;

static Suite *
capssetter_suite (void)
{
  Suite *s = suite_create ("capssetter");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_n_join_n_replace);
  tcase_add_test (tc_chain, test_n_join_replace);
  tcase_add_test (tc_chain, test_join_n_replace_n_match);
  tcase_add_test (tc_chain, test_join_n_replace_match);
  tcase_add_test (tc_chain, test_join_replace_n_match);
  tcase_add_test (tc_chain, test_join_replace_match);

  return s;
}

GST_CHECK_MAIN (capssetter);