summaryrefslogtreecommitdiff
path: root/tests/check/elements/autodetect.c
blob: 578627cd61d35000acb10b93ab9567a75c0477c4 (plain)
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
117
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
229
/* GStreamer unit test for the autodetect elements
 *
 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <gst/audio/audio.h>
#include <gst/base/gstbasesink.h>
#include <gst/check/gstcheck.h>
#include <gst/video/video.h>

/* dummy sink elements */

#define GST_TYPE_FAKE_AUDIO_SINK (gst_fake_audio_sink_get_type ())
typedef GstBaseSink GstFakeAudioSink;
typedef GstBaseSinkClass GstFakeAudioSinkClass;
GType gst_fake_audio_sink_get_type (void);
G_DEFINE_TYPE (GstFakeAudioSink, gst_fake_audio_sink, GST_TYPE_BASE_SINK);

static void
gst_fake_audio_sink_class_init (GstFakeAudioSinkClass * klass)
{
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
  static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
      GST_PAD_SINK, GST_PAD_ALWAYS,
      GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL) "; "));

  gst_element_class_set_static_metadata (gstelement_class, "Fake Audio Sink",
      "Sink/Audio", "Audio sink fake for testing", "Stefan Sauer");
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&pad_template));
}

static void
gst_fake_audio_sink_init (GstFakeAudioSink * sink)
{
}

#define GST_TYPE_FAKE_VIDEO_SINK (gst_fake_video_sink_get_type ())
typedef GstBaseSink GstFakeVideoSink;
typedef GstBaseSinkClass GstFakeVideoSinkClass;
GType gst_fake_video_sink_get_type (void);
G_DEFINE_TYPE (GstFakeVideoSink, gst_fake_video_sink, GST_TYPE_BASE_SINK);

static void
gst_fake_video_sink_class_init (GstFakeVideoSinkClass * klass)
{
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
  static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("sink",
      GST_PAD_SINK, GST_PAD_ALWAYS,
      GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) "; "));

  gst_element_class_set_static_metadata (gstelement_class, "Fake Video Sink",
      "Sink/Video", "Video sink fake for testing", "Stefan Sauer");
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&pad_template));
}

static void
gst_fake_video_sink_init (GstFakeVideoSink * sink)
{
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  if (!gst_element_register (plugin, "fakeaudiosink", G_MAXINT,
          GST_TYPE_FAKE_AUDIO_SINK))
    return FALSE;
  if (!gst_element_register (plugin, "fakevideosink", G_MAXINT,
          GST_TYPE_FAKE_VIDEO_SINK))
    return FALSE;
  return TRUE;
}

/* tests */

static void
test_plugs_best (GstElement * sink, GType expected)
{
  GstStateChangeReturn state_ret;
  GList *child;

  state_ret = gst_element_set_state (sink, GST_STATE_READY);
  fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS, NULL);

  child = GST_BIN_CHILDREN (sink);
  fail_unless (child != NULL, "no elements plugged");
  ck_assert_int_eq (g_list_length (child), 1);
  fail_unless (G_OBJECT_TYPE (child), expected);

  /* clean up */
  gst_element_set_state (sink, GST_STATE_NULL);
  gst_object_unref (sink);
}

GST_START_TEST (test_autovideosink_plugs_best)
{
  GstElement *sink = gst_element_factory_make ("autovideosink", NULL);

  test_plugs_best (sink, GST_TYPE_FAKE_VIDEO_SINK);
}

GST_END_TEST;

GST_START_TEST (test_autoaudiosink_plugs_best)
{
  GstElement *sink = gst_element_factory_make ("autoaudiosink", NULL);

  test_plugs_best (sink, GST_TYPE_FAKE_AUDIO_SINK);
}

GST_END_TEST;

static void
test_ghostpad_error_case (GstCaps * caps, GstElement * sink,
    const gchar * fake_sink_name)
{
  GstStateChangeReturn state_ret;
  GstElement *pipeline, *src, *filter, *fakesink;

  pipeline = gst_pipeline_new ("pipeline");
  src = gst_element_factory_make ("fakesrc", NULL);
  filter = gst_element_factory_make ("capsfilter", NULL);

  g_object_set (filter, "caps", caps, NULL);
  gst_caps_unref (caps);

  gst_bin_add_many (GST_BIN (pipeline), src, filter, sink, NULL);

  fail_unless (gst_element_link (src, filter), "Failed to link src to filter");
  fail_unless (gst_element_link (filter, sink),
      "Failed to link filter to sink");

  /* this should fail, there's no such format */
  state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
  if (state_ret == GST_STATE_CHANGE_ASYNC) {
    /* make sure we wait for the actual success/failure to happen */
    GstState state;
    state_ret =
        gst_element_get_state (pipeline, &state, &state, GST_CLOCK_TIME_NONE);
  }
  fakesink = gst_bin_get_by_name (GST_BIN (sink), fake_sink_name);
  if (fakesink != NULL) {
    /* no real sink available */
    fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
        "pipeline _set_state() to PAUSED failed");
    gst_object_unref (fakesink);
  } else {
    /* autovideosink contains a real video sink */
    fail_unless (state_ret == GST_STATE_CHANGE_FAILURE,
        "pipeline _set_state() to PAUSED succeeded but should have failed");

    /* so, we hit an error and try to shut down the pipeline; this shouldn't
     * deadlock or block anywhere when autovideosink resets the ghostpad
     * targets etc. */
  }
  state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
  fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
      "State change on pipeline failed");

  /* clean up */
  gst_object_unref (pipeline);
}

GST_START_TEST (test_autovideosink_ghostpad_error_case)
{
  GstElement *sink = gst_element_factory_make ("autovideosink", NULL);
  GstCaps *caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING,
      "ABCD", NULL);

  test_ghostpad_error_case (caps, sink, "fake-video-sink");
}

GST_END_TEST;

GST_START_TEST (test_autoaudiosink_ghostpad_error_case)
{
  GstElement *sink = gst_element_factory_make ("autoaudiosink", NULL);
  GstCaps *caps = gst_caps_new_simple ("audio/x-raw", "format", G_TYPE_STRING,
      "ABCD", NULL);

  test_ghostpad_error_case (caps, sink, "fake-audio-sink");
}

GST_END_TEST;

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

  gst_plugin_register_static (GST_VERSION_MAJOR,
      GST_VERSION_MINOR,
      "autodetect-test",
      "autodetect test elements",
      plugin_init,
      VERSION, "LGPL", PACKAGE, PACKAGE_NAME,
      "http://gstreamer.freedesktop.org");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_autovideosink_plugs_best);
  tcase_add_test (tc_chain, test_autoaudiosink_plugs_best);
  tcase_add_test (tc_chain, test_autovideosink_ghostpad_error_case);
  tcase_add_test (tc_chain, test_autoaudiosink_ghostpad_error_case);

  return s;
}

GST_CHECK_MAIN (autodetect);