1 /* GStreamer unit tests for playbin
2 *
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
22 * with newer GLib versions (>= 2.31.0) */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <gst/check/gstcheck.h>
30 #include <gst/base/gstpushsrc.h>
31
32 #ifndef GST_DISABLE_REGISTRY
33
34 static GType gst_red_video_src_get_type (void);
35 static GType gst_codec_src_get_type (void);
36
GST_START_TEST(test_uri)37 GST_START_TEST (test_uri)
38 {
39 GstElement *playbin, *fakesink;
40 gchar *uri;
41
42 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
43 gst_red_video_src_get_type ()));
44
45 playbin = gst_element_factory_make ("playbin", "playbin");
46 fail_unless (playbin != NULL, "Failed to create playbin element");
47
48 fakesink = gst_element_factory_make ("fakesink", "fakesink");
49 fail_unless (fakesink != NULL, "Failed to create fakesink element");
50
51 g_object_set (playbin, "video-sink", fakesink, NULL);
52
53 g_object_set (playbin, "uri", "redvideo://", NULL);
54 g_object_get (playbin, "uri", &uri, NULL);
55
56 fail_unless_equals_string (uri, "redvideo://");
57 g_free (uri);
58
59 g_object_get (playbin, "current-uri", &uri, NULL);
60 fail_unless_equals_string (uri, NULL);
61
62 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
63 GST_STATE_CHANGE_ASYNC);
64 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL, -1),
65 GST_STATE_CHANGE_SUCCESS);
66
67 g_object_get (playbin, "uri", &uri, NULL);
68 fail_unless_equals_string (uri, NULL);
69 g_object_get (playbin, "current-uri", &uri, NULL);
70 fail_unless_equals_string (uri, "redvideo://");
71 g_free (uri);
72
73 gst_element_set_state (playbin, GST_STATE_NULL);
74 gst_object_unref (playbin);
75 }
76
77 GST_END_TEST;
78
79 /* make sure the audio sink is not touched for video-only streams */
GST_START_TEST(test_sink_usage_video_only_stream)80 GST_START_TEST (test_sink_usage_video_only_stream)
81 {
82 GstElement *playbin, *fakevideosink, *fakeaudiosink;
83 GstState cur_state, pending_state;
84 GstElement *source;
85 GstSample *last_sample;
86 gint nstreams;
87
88 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
89 gst_red_video_src_get_type ()));
90
91 playbin = gst_element_factory_make ("playbin", "playbin");
92 fail_unless (playbin != NULL, "Failed to create playbin element");
93
94 fakevideosink = gst_element_factory_make ("fakesink", "fakevideosink");
95 fail_unless (fakevideosink != NULL, "Failed to create fakevideosink element");
96
97 fakeaudiosink = gst_element_factory_make ("fakesink", "fakeaudiosink");
98 fail_unless (fakeaudiosink != NULL, "Failed to create fakeaudiosink element");
99
100 /* video-only stream, audiosink will error out in ready => paused if used */
101 g_object_set (fakeaudiosink, "state-error", 2, NULL);
102
103 g_object_set (playbin, "video-sink", fakevideosink, NULL);
104 g_object_set (playbin, "audio-sink", fakeaudiosink, NULL);
105
106 g_object_set (playbin, "uri", "redvideo://", NULL);
107
108 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_READY),
109 GST_STATE_CHANGE_SUCCESS);
110 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
111 GST_STATE_CHANGE_ASYNC);
112 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL, -1),
113 GST_STATE_CHANGE_SUCCESS);
114
115 fail_unless_equals_int (gst_element_get_state (fakeaudiosink, &cur_state,
116 &pending_state, 0), GST_STATE_CHANGE_SUCCESS);
117 fail_unless_equals_int (cur_state, GST_STATE_NULL);
118 fail_unless_equals_int (pending_state, GST_STATE_VOID_PENDING);
119
120 g_object_get (playbin, "n-video", &nstreams, NULL);
121 fail_unless_equals_int (nstreams, 1);
122
123 g_object_get (playbin, "n-audio", &nstreams, NULL);
124 fail_unless_equals_int (nstreams, 0);
125
126 g_object_get (playbin, "n-text", &nstreams, NULL);
127 fail_unless_equals_int (nstreams, 0);
128
129 g_object_get (playbin, "source", &source, NULL);
130 fail_unless (G_TYPE_FROM_INSTANCE (source) == gst_red_video_src_get_type ());
131 gst_object_unref (source);
132
133 g_object_get (playbin, "sample", &last_sample, NULL);
134 fail_unless (GST_IS_SAMPLE (last_sample));
135 gst_sample_unref (last_sample);
136
137 gst_element_set_state (playbin, GST_STATE_NULL);
138 gst_object_unref (playbin);
139 }
140
141 GST_END_TEST;
142
143 /* this tests async error handling when setting up the subbin */
GST_START_TEST(test_suburi_error_unknowntype)144 GST_START_TEST (test_suburi_error_unknowntype)
145 {
146 GstElement *playbin, *fakesink;
147
148 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
149 gst_red_video_src_get_type ()));
150
151 playbin = gst_element_factory_make ("playbin", "playbin");
152 fail_unless (playbin != NULL, "Failed to create playbin element");
153
154 fakesink = gst_element_factory_make ("fakesink", "fakesink");
155 fail_unless (fakesink != NULL, "Failed to create fakesink element");
156 ASSERT_OBJECT_REFCOUNT (fakesink, "fakesink after creation", 1);
157
158 g_object_set (playbin, "video-sink", fakesink, NULL);
159
160 /* suburi file format unknown: playbin should just ignore the suburi and
161 * preroll normally (if /dev/zero does not exist, this test should behave
162 * the same as test_suburi_error_invalidfile() */
163 g_object_set (playbin, "uri", "redvideo://", NULL);
164 g_object_set (playbin, "suburi", "file:///dev/zero", NULL);
165 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_READY),
166 GST_STATE_CHANGE_SUCCESS);
167 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
168 GST_STATE_CHANGE_ASYNC);
169 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL, -1),
170 GST_STATE_CHANGE_SUCCESS);
171
172 gst_element_set_state (playbin, GST_STATE_NULL);
173 gst_object_unref (playbin);
174 }
175
176 GST_END_TEST;
177
GST_START_TEST(test_suburi_error_invalidfile)178 GST_START_TEST (test_suburi_error_invalidfile)
179 {
180 GstElement *playbin, *fakesink;
181
182 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
183 gst_red_video_src_get_type ()));
184
185 playbin = gst_element_factory_make ("playbin", "playbin");
186 fail_unless (playbin != NULL, "Failed to create playbin element");
187
188 fakesink = gst_element_factory_make ("fakesink", "fakesink");
189 fail_unless (fakesink != NULL, "Failed to create fakesink element");
190 ASSERT_OBJECT_REFCOUNT (fakesink, "fakesink after creation", 1);
191
192 g_object_set (playbin, "video-sink", fakesink, NULL);
193
194 /* suburi file does not exist: playbin should just ignore the suburi and
195 * preroll normally */
196 g_object_set (playbin, "uri", "redvideo://", NULL);
197 g_object_set (playbin, "suburi", "file:///foo/bar/803129999/32x9ax1", NULL);
198 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_READY),
199 GST_STATE_CHANGE_SUCCESS);
200 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
201 GST_STATE_CHANGE_ASYNC);
202 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL, -1),
203 GST_STATE_CHANGE_SUCCESS);
204
205 gst_element_set_state (playbin, GST_STATE_NULL);
206 gst_object_unref (playbin);
207 }
208
209 GST_END_TEST;
210
GST_START_TEST(test_suburi_error_wrongproto)211 GST_START_TEST (test_suburi_error_wrongproto)
212 {
213 GstElement *playbin, *fakesink;
214
215 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
216 gst_red_video_src_get_type ()));
217
218 playbin = gst_element_factory_make ("playbin", "playbin");
219 fail_unless (playbin != NULL, "Failed to create playbin element");
220
221 fakesink = gst_element_factory_make ("fakesink", "fakesink");
222 fail_unless (fakesink != NULL, "Failed to create fakesink element");
223 ASSERT_OBJECT_REFCOUNT (fakesink, "fakesink after creation", 1);
224
225 g_object_set (playbin, "video-sink", fakesink, NULL);
226
227 /* wrong protocol for suburi: playbin should just ignore the suburi and
228 * preroll normally */
229 g_object_set (playbin, "uri", "redvideo://", NULL);
230 g_object_set (playbin, "suburi", "nosuchproto://foo.bar:80", NULL);
231 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_READY),
232 GST_STATE_CHANGE_SUCCESS);
233 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
234 GST_STATE_CHANGE_ASYNC);
235 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL, -1),
236 GST_STATE_CHANGE_SUCCESS);
237
238 gst_element_set_state (playbin, GST_STATE_NULL);
239 gst_object_unref (playbin);
240 }
241
242 GST_END_TEST;
243
244 static GstElement *
create_playbin(const gchar * uri)245 create_playbin (const gchar * uri)
246 {
247 GstElement *playbin, *fakesink1, *fakesink2;
248
249 playbin = gst_element_factory_make ("playbin", "playbin");
250 fail_unless (playbin != NULL, "Failed to create playbin element");
251
252 fakesink1 = gst_element_factory_make ("fakesink", NULL);
253 fail_unless (fakesink1 != NULL, "Failed to create fakesink element #1");
254
255 fakesink2 = gst_element_factory_make ("fakesink", NULL);
256 fail_unless (fakesink2 != NULL, "Failed to create fakesink element #2");
257
258 /* make them behave like normal sinks, even if not needed for the test */
259 g_object_set (fakesink1, "sync", TRUE, NULL);
260 g_object_set (fakesink2, "sync", TRUE, NULL);
261
262 g_object_set (playbin, "video-sink", fakesink1, NULL);
263 g_object_set (playbin, "audio-sink", fakesink2, NULL);
264
265 g_object_set (playbin, "uri", uri, NULL);
266
267 return playbin;
268 }
269
GST_START_TEST(test_missing_urisource_handler)270 GST_START_TEST (test_missing_urisource_handler)
271 {
272 GstStructure *s;
273 GstMessage *msg;
274 GstElement *playbin;
275 GError *err = NULL;
276 GstBus *bus;
277
278 playbin = create_playbin ("chocchipcookie://withahint.of/cinnamon");
279
280 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_READY),
281 GST_STATE_CHANGE_SUCCESS);
282 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
283 GST_STATE_CHANGE_FAILURE);
284
285 /* there should be at least a missing-plugin message on the bus now and an
286 * error message; the missing-plugin message should be first */
287 bus = gst_element_get_bus (playbin);
288
289 msg = gst_bus_poll (bus, GST_MESSAGE_ELEMENT | GST_MESSAGE_ERROR, -1);
290 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
291 s = (GstStructure *) gst_message_get_structure (msg);
292 fail_unless (s != NULL);
293 fail_unless (gst_structure_has_name (s, "missing-plugin"));
294 fail_unless (gst_structure_has_field_typed (s, "detail", G_TYPE_STRING));
295 fail_unless_equals_string (gst_structure_get_string (s, "detail"),
296 "chocchipcookie");
297 fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
298 fail_unless_equals_string (gst_structure_get_string (s, "type"), "urisource");
299 gst_message_unref (msg);
300
301 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, -1);
302 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ERROR);
303
304 /* make sure the error is a CORE MISSING_PLUGIN one */
305 gst_message_parse_error (msg, &err, NULL);
306 fail_unless (err != NULL);
307 fail_unless (err->domain == GST_CORE_ERROR, "error has wrong error domain "
308 "%s instead of core-error-quark", g_quark_to_string (err->domain));
309 fail_unless (err->code == GST_CORE_ERROR_MISSING_PLUGIN, "error has wrong "
310 "code %u instead of GST_CORE_ERROR_MISSING_PLUGIN", err->code);
311 g_error_free (err);
312 gst_message_unref (msg);
313 gst_object_unref (bus);
314
315 gst_element_set_state (playbin, GST_STATE_NULL);
316 gst_object_unref (playbin);
317 }
318
319 GST_END_TEST;
320
GST_START_TEST(test_missing_suburisource_handler)321 GST_START_TEST (test_missing_suburisource_handler)
322 {
323 GstStructure *s;
324 GstMessage *msg;
325 GstElement *playbin;
326 GError *err = NULL;
327 GstBus *bus;
328
329 playbin = create_playbin ("file:///does/not/exis.t");
330
331 g_object_set (playbin, "suburi", "cookie://withahint.of/cinnamon", NULL);
332
333 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_READY),
334 GST_STATE_CHANGE_SUCCESS);
335 gst_element_set_state (playbin, GST_STATE_PAUSED);
336
337 /* there should be at least a missing-plugin message on the bus now and an
338 * error message; the missing-plugin message should be first */
339 bus = gst_element_get_bus (playbin);
340
341 msg = gst_bus_poll (bus, GST_MESSAGE_ELEMENT | GST_MESSAGE_ERROR, -1);
342 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
343 s = (GstStructure *) gst_message_get_structure (msg);
344 fail_unless (s != NULL);
345 fail_unless (gst_structure_has_name (s, "missing-plugin"));
346 fail_unless (gst_structure_has_field_typed (s, "detail", G_TYPE_STRING));
347 fail_unless_equals_string (gst_structure_get_string (s, "detail"), "cookie");
348 fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
349 fail_unless_equals_string (gst_structure_get_string (s, "type"), "urisource");
350 gst_message_unref (msg);
351
352 msg = gst_bus_poll (bus, GST_MESSAGE_WARNING, -1);
353 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_WARNING);
354
355 /* make sure the *warning* is a CORE MISSING_PLUGIN one */
356 gst_message_parse_warning (msg, &err, NULL);
357 fail_unless (err != NULL);
358 fail_unless (err->domain == GST_CORE_ERROR, "error has wrong error domain "
359 "%s instead of core-error-quark", g_quark_to_string (err->domain));
360 fail_unless (err->code == GST_CORE_ERROR_MISSING_PLUGIN, "error has wrong "
361 "code %u instead of GST_CORE_ERROR_MISSING_PLUGIN", err->code);
362 g_error_free (err);
363 gst_message_unref (msg);
364
365 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, -1);
366 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ERROR);
367
368 /* make sure the error is a RESOURCE NOT_FOUND one */
369 gst_message_parse_error (msg, &err, NULL);
370 fail_unless (err != NULL);
371 fail_unless (err->domain == GST_RESOURCE_ERROR,
372 "error has wrong error domain " "%s instead of resource-error-quark",
373 g_quark_to_string (err->domain));
374 fail_unless (err->code == GST_RESOURCE_ERROR_NOT_FOUND,
375 "error has wrong " "code %u instead of GST_RESOURCE_ERROR_NOT_FOUND",
376 err->code);
377 g_error_free (err);
378 gst_message_unref (msg);
379
380 gst_object_unref (bus);
381
382 gst_element_set_state (playbin, GST_STATE_NULL);
383 gst_object_unref (playbin);
384 }
385
386 GST_END_TEST;
387
GST_START_TEST(test_missing_primary_decoder)388 GST_START_TEST (test_missing_primary_decoder)
389 {
390 GstStructure *s;
391 GstMessage *msg;
392 GstElement *playbin;
393 GError *err = NULL;
394 GstBus *bus;
395
396 fail_unless (gst_element_register (NULL, "codecsrc", GST_RANK_PRIMARY,
397 gst_codec_src_get_type ()));
398
399 playbin = create_playbin ("codec://");
400
401 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_READY),
402 GST_STATE_CHANGE_SUCCESS);
403 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
404 GST_STATE_CHANGE_ASYNC);
405
406 /* there should soon be at least a missing-plugin message on the bus and an
407 * error message; the missing-plugin message should be first */
408 bus = gst_element_get_bus (playbin);
409
410 msg = gst_bus_poll (bus, GST_MESSAGE_ELEMENT | GST_MESSAGE_ERROR, -1);
411 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
412 s = (GstStructure *) gst_message_get_structure (msg);
413 fail_unless (s != NULL);
414 fail_unless (gst_structure_has_name (s, "missing-plugin"));
415 fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
416 fail_unless_equals_string (gst_structure_get_string (s, "type"), "decoder");
417 fail_unless (gst_structure_has_field_typed (s, "detail", GST_TYPE_CAPS));
418 gst_message_unref (msg);
419
420 msg = gst_bus_poll (bus, GST_MESSAGE_WARNING, -1);
421 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_WARNING);
422
423 /* make sure the *warning* is a STREAM CODEC_NOT_FOUND one */
424 gst_message_parse_warning (msg, &err, NULL);
425 fail_unless (err != NULL);
426 fail_unless (err->domain == GST_STREAM_ERROR, "error has wrong error domain "
427 "%s instead of stream-error-quark", g_quark_to_string (err->domain));
428 fail_unless (err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND, "error has wrong "
429 "code %u instead of GST_STREAM_ERROR_CODEC_NOT_FOUND", err->code);
430 g_error_free (err);
431 gst_message_unref (msg);
432
433 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, -1);
434 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ERROR);
435
436 /* make sure the error is a CORE MISSING_PLUGIN one */
437 gst_message_parse_error (msg, &err, NULL);
438 fail_unless (err != NULL);
439 fail_unless (err->domain == GST_CORE_ERROR, "error has wrong error domain "
440 "%s instead of core-error-quark", g_quark_to_string (err->domain));
441 fail_unless (err->code == GST_CORE_ERROR_MISSING_PLUGIN, "error has wrong "
442 "code %u instead of GST_CORE_ERROR_MISSING_PLUGIN", err->code);
443 g_error_free (err);
444 gst_message_unref (msg);
445
446 gst_object_unref (bus);
447
448 gst_element_set_state (playbin, GST_STATE_NULL);
449 gst_object_unref (playbin);
450 }
451
452 GST_END_TEST;
453
GST_START_TEST(test_refcount)454 GST_START_TEST (test_refcount)
455 {
456 GstElement *playbin, *audiosink, *videosink, *vis;
457
458 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
459 gst_red_video_src_get_type ()));
460
461 playbin = gst_element_factory_make ("playbin", NULL);
462 audiosink = gst_element_factory_make ("fakesink", "myaudiosink");
463 videosink = gst_element_factory_make ("fakesink", "myvideosink");
464 vis = gst_element_factory_make ("identity", "myvis");
465
466 /* ref because we need them after we unref playbin */
467 gst_object_ref (audiosink);
468 gst_object_ref (videosink);
469 gst_object_ref (vis);
470
471 /* Sinks have floating ref only, setting the properties takes ownership. */
472 g_object_set (playbin,
473 "audio-sink", audiosink,
474 "video-sink", videosink,
475 "vis-plugin", vis, "flags", 0x01 | 0x02 | 0x08, NULL);
476
477 g_object_set (playbin, "uri", "redvideo://", NULL);
478 //"uri", "file:///home/wim/data/cdda.ogg", NULL);
479
480 ASSERT_OBJECT_REFCOUNT (playbin, "playbin", 1);
481
482 /* we have 3 refs now, one from ourselves, one from playbin and one from playsink */
483 ASSERT_OBJECT_REFCOUNT (audiosink, "myaudiosink", 3);
484 ASSERT_OBJECT_REFCOUNT (videosink, "myvideosink", 3);
485 ASSERT_OBJECT_REFCOUNT (vis, "myvis", 2);
486
487 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
488 GST_STATE_CHANGE_ASYNC);
489 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL,
490 GST_CLOCK_TIME_NONE), GST_STATE_CHANGE_SUCCESS);
491 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_NULL),
492 GST_STATE_CHANGE_SUCCESS);
493
494 ASSERT_OBJECT_REFCOUNT (playbin, "playbin", 1);
495 /* refcount of our elements is undefined, playbin might keep additional refs
496 * because it cached the elements in bins */
497 gst_object_unref (playbin);
498
499 /* now we are back to our refs */
500 ASSERT_OBJECT_REFCOUNT (audiosink, "myaudiosink", 1);
501 ASSERT_OBJECT_REFCOUNT (videosink, "myvideosink", 1);
502 ASSERT_OBJECT_REFCOUNT (vis, "myvis", 1);
503
504 gst_object_unref (audiosink);
505 gst_object_unref (videosink);
506 gst_object_unref (vis);
507 }
508
509 GST_END_TEST;
510
511 static void
source_setup(GstElement * playbin,GstElement * source,GstElement ** p_src)512 source_setup (GstElement * playbin, GstElement * source, GstElement ** p_src)
513 {
514 GST_LOG ("source-setup called, source = %s", G_OBJECT_TYPE_NAME (source));
515 *p_src = gst_object_ref (source);
516 GST_LOG ("here");
517 }
518
GST_START_TEST(test_source_setup)519 GST_START_TEST (test_source_setup)
520 {
521 GstElement *playbin, *videosink;
522 GstElement *src = NULL;
523
524 if (!gst_registry_check_feature_version (gst_registry_get (), "redvideosrc",
525 GST_VERSION_MAJOR, GST_VERSION_MINOR, 0)) {
526 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
527 gst_red_video_src_get_type ()));
528 }
529
530 playbin = gst_element_factory_make ("playbin", NULL);
531 g_object_set (playbin, "uri", "redvideo://", NULL);
532
533 videosink = gst_element_factory_make ("fakesink", "myvideosink");
534 g_object_set (playbin, "video-sink", videosink, NULL);
535
536 g_signal_connect (playbin, "source-setup", G_CALLBACK (source_setup), &src);
537
538 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
539 GST_STATE_CHANGE_ASYNC);
540 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL,
541 GST_CLOCK_TIME_NONE), GST_STATE_CHANGE_SUCCESS);
542
543 fail_unless (src != NULL);
544 fail_unless (G_OBJECT_TYPE (src) == gst_red_video_src_get_type ());
545
546 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_NULL),
547 GST_STATE_CHANGE_SUCCESS);
548
549 gst_object_unref (playbin);
550 gst_object_unref (src);
551 }
552
553 GST_END_TEST;
554
555 static void
element_setup(GstElement * playbin,GstElement * element,GQueue * elts)556 element_setup (GstElement * playbin, GstElement * element, GQueue * elts)
557 {
558 GstElementFactory *f = gst_element_get_factory (element);
559
560 g_queue_push_tail (elts, f ? GST_OBJECT_NAME (f) : GST_OBJECT_NAME (element));
561 }
562
GST_START_TEST(test_element_setup)563 GST_START_TEST (test_element_setup)
564 {
565 GstElement *playbin, *videosink;
566 GQueue elts = G_QUEUE_INIT;
567
568 if (!gst_registry_check_feature_version (gst_registry_get (), "redvideosrc",
569 GST_VERSION_MAJOR, GST_VERSION_MINOR, 0)) {
570 fail_unless (gst_element_register (NULL, "redvideosrc", GST_RANK_PRIMARY,
571 gst_red_video_src_get_type ()));
572 }
573
574 playbin = gst_element_factory_make ("playbin", NULL);
575 g_object_set (playbin, "uri", "redvideo://", NULL);
576
577 videosink = gst_element_factory_make ("fakesink", "myvideosink");
578 g_object_set (playbin, "video-sink", videosink, NULL);
579
580 g_signal_connect (playbin, "element-setup", G_CALLBACK (element_setup),
581 &elts);
582
583 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PAUSED),
584 GST_STATE_CHANGE_ASYNC);
585 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL,
586 GST_CLOCK_TIME_NONE), GST_STATE_CHANGE_SUCCESS);
587
588 #define seen_element(e) g_queue_find_custom(&elts, e, (GCompareFunc) strcmp)
589
590 fail_unless (seen_element ("redvideosrc"));
591 fail_unless (seen_element ("uridecodebin"));
592 fail_unless (seen_element ("videoconvert"));
593 fail_unless (seen_element ("videoscale"));
594 fail_unless (seen_element ("fakesink"));
595
596 #undef seen_element
597
598 g_queue_clear (&elts);
599
600 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_NULL),
601 GST_STATE_CHANGE_SUCCESS);
602
603 gst_object_unref (playbin);
604 }
605
606 GST_END_TEST;
607
608 /*** redvideo:// source ***/
609
610 static GstURIType
gst_red_video_src_uri_get_type(GType type)611 gst_red_video_src_uri_get_type (GType type)
612 {
613 return GST_URI_SRC;
614 }
615
616 static const gchar *const *
gst_red_video_src_uri_get_protocols(GType type)617 gst_red_video_src_uri_get_protocols (GType type)
618 {
619 static const gchar *protocols[] = { "redvideo", NULL };
620
621 return protocols;
622 }
623
624 static gchar *
gst_red_video_src_uri_get_uri(GstURIHandler * handler)625 gst_red_video_src_uri_get_uri (GstURIHandler * handler)
626 {
627 return g_strdup ("redvideo://");
628 }
629
630 static gboolean
gst_red_video_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)631 gst_red_video_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
632 GError ** error)
633 {
634 return (uri != NULL && g_str_has_prefix (uri, "redvideo:"));
635 }
636
637 static void
gst_red_video_src_uri_handler_init(gpointer g_iface,gpointer iface_data)638 gst_red_video_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
639 {
640 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
641
642 iface->get_type = gst_red_video_src_uri_get_type;
643 iface->get_protocols = gst_red_video_src_uri_get_protocols;
644 iface->get_uri = gst_red_video_src_uri_get_uri;
645 iface->set_uri = gst_red_video_src_uri_set_uri;
646 }
647
648 static void
gst_red_video_src_init_type(GType type)649 gst_red_video_src_init_type (GType type)
650 {
651 static const GInterfaceInfo uri_hdlr_info = {
652 gst_red_video_src_uri_handler_init, NULL, NULL
653 };
654
655 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &uri_hdlr_info);
656 }
657
658 typedef GstPushSrc GstRedVideoSrc;
659 typedef GstPushSrcClass GstRedVideoSrcClass;
660
661 G_DEFINE_TYPE_WITH_CODE (GstRedVideoSrc, gst_red_video_src,
662 GST_TYPE_PUSH_SRC, gst_red_video_src_init_type (g_define_type_id));
663
664 static GstFlowReturn
gst_red_video_src_create(GstPushSrc * src,GstBuffer ** p_buf)665 gst_red_video_src_create (GstPushSrc * src, GstBuffer ** p_buf)
666 {
667 GstBuffer *buf;
668 GstMapInfo map;
669 guint w = 64, h = 64;
670 guint size;
671
672 size = w * h * 3 / 2;
673 buf = gst_buffer_new_and_alloc (size);
674 gst_buffer_map (buf, &map, GST_MAP_WRITE);
675 memset (map.data, 76, w * h);
676 memset (map.data + (w * h), 85, (w * h) / 4);
677 memset (map.data + (w * h) + ((w * h) / 4), 255, (w * h) / 4);
678 gst_buffer_unmap (buf, &map);
679
680 *p_buf = buf;
681 return GST_FLOW_OK;
682 }
683
684 static GstCaps *
gst_red_video_src_get_caps(GstBaseSrc * src,GstCaps * filter)685 gst_red_video_src_get_caps (GstBaseSrc * src, GstCaps * filter)
686 {
687 guint w = 64, h = 64;
688 return gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING,
689 "I420", "width", G_TYPE_INT, w, "height",
690 G_TYPE_INT, h, "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
691 }
692
693 static void
gst_red_video_src_class_init(GstRedVideoSrcClass * klass)694 gst_red_video_src_class_init (GstRedVideoSrcClass * klass)
695 {
696 GstPushSrcClass *pushsrc_class = GST_PUSH_SRC_CLASS (klass);
697 GstBaseSrcClass *basesrc_class = GST_BASE_SRC_CLASS (klass);
698 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
699 GST_PAD_SRC, GST_PAD_ALWAYS,
700 GST_STATIC_CAPS ("video/x-raw, format=(string)I420")
701 );
702 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
703
704 gst_element_class_add_static_pad_template (element_class, &src_templ);
705 gst_element_class_set_metadata (element_class,
706 "Red Video Src", "Source/Video", "yep", "me");
707
708 pushsrc_class->create = gst_red_video_src_create;
709 basesrc_class->get_caps = gst_red_video_src_get_caps;
710 }
711
712 static void
gst_red_video_src_init(GstRedVideoSrc * src)713 gst_red_video_src_init (GstRedVideoSrc * src)
714 {
715 }
716
717 /*** codec:// source ***/
718
719 static GstURIType
gst_codec_src_uri_get_type(GType type)720 gst_codec_src_uri_get_type (GType type)
721 {
722 return GST_URI_SRC;
723 }
724
725 static const gchar *const *
gst_codec_src_uri_get_protocols(GType type)726 gst_codec_src_uri_get_protocols (GType type)
727 {
728 static const gchar *protocols[] = { (char *) "codec", NULL };
729
730 return protocols;
731 }
732
733 static gchar *
gst_codec_src_uri_get_uri(GstURIHandler * handler)734 gst_codec_src_uri_get_uri (GstURIHandler * handler)
735 {
736 return g_strdup ("codec://");
737 }
738
739 static gboolean
gst_codec_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)740 gst_codec_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
741 GError ** error)
742 {
743 return (uri != NULL && g_str_has_prefix (uri, "codec:"));
744 }
745
746 static void
gst_codec_src_uri_handler_init(gpointer g_iface,gpointer iface_data)747 gst_codec_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
748 {
749 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
750
751 iface->get_type = gst_codec_src_uri_get_type;
752 iface->get_protocols = gst_codec_src_uri_get_protocols;
753 iface->get_uri = gst_codec_src_uri_get_uri;
754 iface->set_uri = gst_codec_src_uri_set_uri;
755 }
756
757 static void
gst_codec_src_init_type(GType type)758 gst_codec_src_init_type (GType type)
759 {
760 static const GInterfaceInfo uri_hdlr_info = {
761 gst_codec_src_uri_handler_init, NULL, NULL
762 };
763
764 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &uri_hdlr_info);
765 }
766
767 #undef parent_class
768 #define parent_class codec_src_parent_class
769
770 typedef GstPushSrc GstCodecSrc;
771 typedef GstPushSrcClass GstCodecSrcClass;
772
773 G_DEFINE_TYPE_WITH_CODE (GstCodecSrc, gst_codec_src,
774 GST_TYPE_PUSH_SRC, gst_codec_src_init_type (g_define_type_id));
775
776 static GstFlowReturn
gst_codec_src_create(GstPushSrc * src,GstBuffer ** p_buf)777 gst_codec_src_create (GstPushSrc * src, GstBuffer ** p_buf)
778 {
779 GstBuffer *buf;
780
781 buf = gst_buffer_new_and_alloc (20);
782 gst_buffer_memset (buf, 0, 0, 20);
783
784 *p_buf = buf;
785 return GST_FLOW_OK;
786 }
787
788 static GstCaps *
gst_codec_src_get_caps(GstBaseSrc * src,GstCaps * filter)789 gst_codec_src_get_caps (GstBaseSrc * src, GstCaps * filter)
790 {
791 return gst_caps_new_empty_simple ("application/x-codec");
792 }
793
794 static void
gst_codec_src_class_init(GstCodecSrcClass * klass)795 gst_codec_src_class_init (GstCodecSrcClass * klass)
796 {
797 GstPushSrcClass *pushsrc_class = GST_PUSH_SRC_CLASS (klass);
798 GstBaseSrcClass *basesrc_class = GST_BASE_SRC_CLASS (klass);
799 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
800 GST_PAD_SRC, GST_PAD_ALWAYS,
801 GST_STATIC_CAPS ("application/x-codec")
802 );
803 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
804
805 gst_element_class_add_static_pad_template (element_class, &src_templ);
806 gst_element_class_set_metadata (element_class,
807 "Codec Src", "Source/Video", "yep", "me");
808
809 pushsrc_class->create = gst_codec_src_create;
810 basesrc_class->get_caps = gst_codec_src_get_caps;
811 }
812
813 static void
gst_codec_src_init(GstCodecSrc * src)814 gst_codec_src_init (GstCodecSrc * src)
815 {
816 }
817
818 #if 0
819 GST_START_TEST (test_appsink_twice)
820 {
821 GstElement *playbin, *appsink;
822 GstSample *sample;
823 gchar *fn, *uri;
824 int flags;
825
826 fn = g_build_filename (GST_TEST_FILES_PATH, "theora-vorbis.ogg", NULL);
827 uri = gst_filename_to_uri (fn, NULL);
828 g_free (fn);
829
830 playbin = gst_element_factory_make ("playbin", NULL);
831 g_object_set (playbin, "uri", uri, NULL);
832 g_free (uri);
833
834 /* disable video decoding/rendering (doesn't actually work yet though) */
835 g_object_get (playbin, "flags", &flags, NULL);
836 g_object_set (playbin, "flags", flags & ~1, NULL);
837
838 appsink = gst_element_factory_make ("appsink", "appsink");
839 g_object_set (playbin, "audio-sink", appsink, NULL);
840
841 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PLAYING),
842 GST_STATE_CHANGE_ASYNC);
843 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL,
844 GST_CLOCK_TIME_NONE), GST_STATE_CHANGE_SUCCESS);
845
846 do {
847 g_signal_emit_by_name (appsink, "pull-sample", &sample);
848 GST_LOG ("got sample: %p", sample);
849 if (sample)
850 gst_sample_unref (sample);
851 }
852 while (sample != NULL);
853
854 GST_INFO ("got first EOS");
855
856 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_NULL),
857 GST_STATE_CHANGE_SUCCESS);
858 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_PLAYING),
859 GST_STATE_CHANGE_ASYNC);
860 fail_unless_equals_int (gst_element_get_state (playbin, NULL, NULL,
861 GST_CLOCK_TIME_NONE), GST_STATE_CHANGE_SUCCESS);
862
863 do {
864 g_signal_emit_by_name (appsink, "pull-sample", &sample);
865 GST_LOG ("got sample: %p", sample);
866 if (sample)
867 gst_sample_unref (sample);
868 }
869 while (sample != NULL);
870
871 GST_INFO ("got second EOS");
872
873 fail_unless_equals_int (gst_element_set_state (playbin, GST_STATE_NULL),
874 GST_STATE_CHANGE_SUCCESS);
875
876 gst_object_unref (playbin);
877 }
878
879 GST_END_TEST;
880 #endif
881
882 #endif /* GST_DISABLE_REGISTRY */
883
884
885 static Suite *
playbin_suite(void)886 playbin_suite (void)
887 {
888 Suite *s = suite_create ("playbin");
889 TCase *tc_chain = tcase_create ("general");
890
891 suite_add_tcase (s, tc_chain);
892
893 #ifndef GST_DISABLE_REGISTRY
894 tcase_add_test (tc_chain, test_uri);
895 tcase_add_test (tc_chain, test_sink_usage_video_only_stream);
896 tcase_add_test (tc_chain, test_suburi_error_wrongproto);
897 tcase_add_test (tc_chain, test_suburi_error_invalidfile);
898 tcase_add_test (tc_chain, test_suburi_error_unknowntype);
899 tcase_add_test (tc_chain, test_missing_urisource_handler);
900 tcase_add_test (tc_chain, test_missing_suburisource_handler);
901 tcase_add_test (tc_chain, test_missing_primary_decoder);
902 tcase_add_test (tc_chain, test_refcount);
903 tcase_add_test (tc_chain, test_source_setup);
904 tcase_add_test (tc_chain, test_element_setup);
905
906 #if 0
907 {
908 GstRegistry *reg = gst_registry_get ();
909
910 if (gst_registry_check_feature_version (reg, "oggdemux", 1, 0, 0) &&
911 gst_registry_check_feature_version (reg, "theoradec", 1, 0, 0) &&
912 gst_registry_check_feature_version (reg, "vorbisdec", 1, 0, 0)) {
913 tcase_add_test (tc_chain, test_appsink_twice);
914 }
915 }
916 #endif
917
918 /* one day we might also want to have the following checks:
919 * tcase_add_test (tc_chain, test_missing_secondary_decoder_one_fatal);
920 * tcase_add_test (tc_chain, test_missing_secondary_decoder_two_fatal);
921 * tcase_add_test (tc_chain, test_missing_secondary_decoder_two_with_preroll);
922 */
923 #endif
924
925 return s;
926 }
927
928 GST_CHECK_MAIN (playbin);
929