1 /* GStreamer unit test for gstprofile
3 * Copyright (C) <2009> Edward Hervey <edward.hervey@collabora.co.uk>
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.
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.
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 /* #include <fcntl.h> */
28 #include <glib/gstdio.h>
29 #include <gst/check/gstcheck.h>
31 #include <gst/pbutils/encoding-profile.h>
32 #include <gst/pbutils/encoding-target.h>
34 #define CHECK_PROFILE(profile, name, description, format, preset, presence, restriction) \
36 fail_if(profile == NULL); \
37 fail_unless_equals_string (gst_encoding_profile_get_name (profile), name); \
38 fail_unless_equals_string (gst_encoding_profile_get_description (profile), description); \
39 fail_unless (gst_caps_is_equal (gst_encoding_profile_get_format (profile), format)); \
40 fail_unless_equals_string (gst_encoding_profile_get_preset (profile), preset); \
41 fail_unless_equals_int (gst_encoding_profile_get_presence (profile), presence); \
42 fail_unless (gst_caps_is_equal (gst_encoding_profile_get_restriction (profile), restriction)); \
45 GST_START_TEST (test_profile_creation)
47 GstEncodingProfile *encprof;
48 GstEncodingAudioProfile *audioprof;
49 GstEncodingVideoProfile *videoprof;
50 GstCaps *ogg, *vorbis, *theora;
51 GstCaps *test1, *test2;
53 ogg = gst_caps_new_simple ("application/ogg", NULL);
54 vorbis = gst_caps_new_simple ("audio/x-vorbis", NULL);
55 theora = gst_caps_new_simple ("video/x-theora", NULL);
57 encprof = (GstEncodingProfile *) gst_encoding_container_profile_new ((gchar *)
58 "ogg-theora-vorbis", "dumb-profile", ogg, (gchar *) "dumb-preset");
59 CHECK_PROFILE (encprof, "ogg-theora-vorbis", "dumb-profile", ogg,
60 "dumb-preset", 0, NULL);
62 audioprof = gst_encoding_audio_profile_new (vorbis, (gchar *) "HQ", NULL, 0);
63 CHECK_PROFILE ((GstEncodingProfile *) audioprof, NULL, NULL, vorbis, "HQ", 0,
66 videoprof = gst_encoding_video_profile_new (theora, (gchar *) "HQ", NULL, 0);
67 CHECK_PROFILE ((GstEncodingProfile *) videoprof, NULL, NULL, theora, "HQ",
70 fail_unless (gst_encoding_container_profile_add_profile (
71 (GstEncodingContainerProfile *) encprof,
72 (GstEncodingProfile *) audioprof));
73 fail_unless (gst_encoding_container_profile_add_profile (
74 (GstEncodingContainerProfile *) encprof,
75 (GstEncodingProfile *) videoprof));
78 test1 = gst_caps_from_string ("video/x-theora; audio/x-vorbis");
79 test2 = gst_encoding_profile_get_input_caps (encprof);
80 fail_unless (gst_caps_is_equal (test1, test2));
81 gst_caps_unref (test1);
82 gst_caps_unref (test2);
84 gst_encoding_profile_unref (encprof);
86 gst_caps_unref (theora);
87 gst_caps_unref (vorbis);
93 GST_START_TEST (test_profile_input_caps)
95 GstEncodingProfile *sprof;
97 GstCaps *out, *restriction, *test1;
99 vorbis = gst_caps_new_simple ("audio/x-vorbis", NULL);
101 /* Simple case, no restriction */
102 sprof = (GstEncodingProfile *)
103 gst_encoding_audio_profile_new (vorbis, NULL, NULL, 0);
104 fail_if (sprof == NULL);
106 out = gst_encoding_profile_get_input_caps (sprof);
107 fail_if (out == NULL);
108 fail_unless (gst_caps_is_equal (out, vorbis));
109 gst_caps_unref (out);
110 gst_encoding_profile_unref (sprof);
112 /* One simple restriction */
113 restriction = gst_caps_from_string ("audio/x-raw-int,channels=2,rate=44100");
114 test1 = gst_caps_from_string ("audio/x-vorbis,channels=2,rate=44100");
115 fail_if (restriction == NULL);
117 sprof = (GstEncodingProfile *)
118 gst_encoding_audio_profile_new (vorbis, NULL, restriction, 0);
119 fail_if (sprof == NULL);
121 out = gst_encoding_profile_get_input_caps (sprof);
122 fail_if (out == NULL);
123 GST_DEBUG ("got caps %" GST_PTR_FORMAT, out);
124 fail_unless (gst_caps_is_equal (out, test1));
125 gst_caps_unref (out);
126 gst_caps_unref (restriction);
127 gst_caps_unref (test1);
128 gst_encoding_profile_unref (sprof);
130 gst_caps_unref (vorbis);
136 GST_START_TEST (test_target_naming)
138 GstEncodingTarget *target;
141 ASSERT_CRITICAL (target = gst_encoding_target_new (NULL, NULL, NULL, NULL));
142 fail_if (target != NULL);
143 ASSERT_CRITICAL (target =
144 gst_encoding_target_new ("donkey", NULL, NULL, NULL));
145 fail_if (target != NULL);
146 ASSERT_CRITICAL (target =
147 gst_encoding_target_new (NULL, "donkey", NULL, NULL));
148 fail_if (target != NULL);
149 ASSERT_CRITICAL (target =
150 gst_encoding_target_new (NULL, NULL, "donkey", NULL));
151 fail_if (target != NULL);
153 /* Name and Category validation */
155 /* empty non-NULL strings */
156 fail_if (gst_encoding_target_new ("", "valid", "description", NULL) != NULL);
157 fail_if (gst_encoding_target_new ("valid", "", "description", NULL) != NULL);
159 /* don't start with a lower case ASCII character */
160 fail_if (gst_encoding_target_new ("A", "valid", "description", NULL) != NULL);
161 fail_if (gst_encoding_target_new ("3", "valid", "description", NULL) != NULL);
162 fail_if (gst_encoding_target_new ("-", "valid", "description", NULL) != NULL);
163 fail_if (gst_encoding_target_new ("!", "valid", "description", NULL) != NULL);
164 fail_if (gst_encoding_target_new (" ", "valid", "description", NULL) != NULL);
165 fail_if (gst_encoding_target_new ("valid", "A", "description", NULL) != NULL);
166 fail_if (gst_encoding_target_new ("valid", "3", "description", NULL) != NULL);
167 fail_if (gst_encoding_target_new ("valid", "-", "description", NULL) != NULL);
168 fail_if (gst_encoding_target_new ("valid", "!", "description", NULL) != NULL);
169 fail_if (gst_encoding_target_new ("valid", " ", "description", NULL) != NULL);
171 /* Starting with anything else is valid */
172 target = gst_encoding_target_new ("a", "valid", "description", NULL);
173 fail_if (target == NULL);
174 gst_encoding_target_unref (target);
175 target = gst_encoding_target_new ("z", "valid", "description", NULL);
176 fail_if (target == NULL);
177 gst_encoding_target_unref (target);
178 target = gst_encoding_target_new ("valid", "a", "description", NULL);
179 fail_if (target == NULL);
180 gst_encoding_target_unref (target);
181 target = gst_encoding_target_new ("valid", "z", "description", NULL);
182 fail_if (target == NULL);
183 gst_encoding_target_unref (target);
185 /* only inner valid characters are lower-case ASCII letters *OR* digits *OR* hyphens */
186 fail_if (gst_encoding_target_new ("aA", "valid", "description",
188 fail_if (gst_encoding_target_new ("a!", "valid", "description",
190 fail_if (gst_encoding_target_new ("space donkeys", "valid", "description",
192 fail_if (gst_encoding_target_new ("howaboutùnicode", "valid", "description",
194 fail_if (gst_encoding_target_new ("valid", "aA", "description",
196 fail_if (gst_encoding_target_new ("valid", "a!", "description",
200 gst_encoding_target_new ("donkey-4-ever", "valid", "description", NULL);
201 fail_if (target == NULL);
202 gst_encoding_target_unref (target);
204 gst_encoding_target_new ("valid", "donkey-4-ever", "description", NULL);
205 fail_if (target == NULL);
206 gst_encoding_target_unref (target);
212 static GstEncodingTarget *
213 create_saveload_target (const gchar * targetname)
215 GstEncodingTarget *target;
216 GstEncodingProfile *profile, *sprof;
217 GstCaps *caps, *caps2;
219 GST_DEBUG ("Creating target");
221 target = gst_encoding_target_new (targetname, "herding",
222 "Plenty of pony glitter profiles", NULL);
223 caps = gst_caps_from_string ("animal/x-pony");
225 (GstEncodingProfile *) gst_encoding_container_profile_new ("pony",
226 "I don't want a description !", caps, NULL);
227 gst_caps_unref (caps);
228 gst_encoding_target_add_profile (target, profile);
230 caps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
231 caps2 = gst_caps_from_string ("audio/x-raw-int,channels=1,rate=44100");
233 (GstEncodingProfile *) gst_encoding_audio_profile_new (caps, NULL, caps2,
235 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
237 gst_caps_unref (caps);
238 gst_caps_unref (caps2);
240 caps = gst_caps_from_string ("video/x-glitter,sparkling=True");
243 ("video/x-raw-yuv,width=640,height=480,framerate=15/1");
244 sprof = (GstEncodingProfile *)
245 gst_encoding_video_profile_new (caps, "seriously glittery", caps2, 0);
246 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
248 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
250 gst_caps_unref (caps);
251 gst_caps_unref (caps2);
256 GST_START_TEST (test_target_profile)
258 GstEncodingTarget *target;
259 GstEncodingProfile *prof;
261 target = create_saveload_target ("myponytarget");
263 /* NULL isn't a valid profile name */
264 ASSERT_CRITICAL (gst_encoding_target_get_profile (target, NULL));
266 /* try finding a profile that doesn't exist */
267 fail_if (gst_encoding_target_get_profile (target,
268 "no-really-does-not-exist"));
270 /* try finding a profile that exists */
271 prof = gst_encoding_target_get_profile (target, "pony");
272 fail_if (prof == NULL);
274 gst_encoding_profile_unref (prof);
275 gst_encoding_target_unref (target);
280 GST_START_TEST (test_saving_profile)
282 GstEncodingTarget *orig, *loaded = NULL;
283 GstEncodingProfile *proforig, *profloaded;
284 gchar *profile_file_name;
286 /* Create and store a target */
287 orig = create_saveload_target ("myponytarget2");
288 GST_DEBUG ("Saving target 'myponytarget2'");
289 fail_unless (gst_encoding_target_save (orig, NULL));
291 /* Check we can load it */
292 profile_file_name = g_build_filename (g_get_home_dir (), ".gstreamer-0.11",
293 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
294 GST_DEBUG ("Loading target from '%s'", profile_file_name);
295 loaded = gst_encoding_target_load_from_file (profile_file_name, NULL);
296 fail_unless (loaded != NULL);
297 g_free (profile_file_name);
299 GST_DEBUG ("Checking targets are equal");
300 /* Check targets are identical */
301 /* 1. at the target level */
302 fail_unless_equals_string (gst_encoding_target_get_name (orig),
303 gst_encoding_target_get_name (loaded));
304 fail_unless_equals_string (gst_encoding_target_get_category (orig),
305 gst_encoding_target_get_category (loaded));
306 fail_unless_equals_string (gst_encoding_target_get_description (orig),
307 gst_encoding_target_get_description (loaded));
308 fail_unless_equals_int (g_list_length ((GList *)
309 gst_encoding_target_get_profiles (loaded)), 1);
311 /* 2. at the profile level */
313 (GstEncodingProfile *) gst_encoding_target_get_profiles (loaded)->data;
315 (GstEncodingProfile *) gst_encoding_target_get_profiles (orig)->data;
317 fail_unless_equals_int (G_TYPE_FROM_INSTANCE (profloaded),
318 G_TYPE_FROM_INSTANCE (proforig));
319 GST_DEBUG ("Comparing loaded:%p to original:%p", profloaded, proforig);
320 fail_unless (gst_encoding_profile_is_equal (profloaded, proforig));
322 gst_encoding_target_unref (orig);
323 gst_encoding_target_unref (loaded);
329 test_individual_target (GstEncodingTarget * target)
331 GstEncodingProfile *prof;
332 GstCaps *tmpcaps, *tmpcaps2;
333 GstEncodingProfile *sprof1, *sprof2;
335 GST_DEBUG ("Checking the target properties");
336 /* Check the target */
337 fail_unless_equals_string (gst_encoding_target_get_name (target),
339 fail_unless_equals_string (gst_encoding_target_get_category (target),
341 fail_unless_equals_string (gst_encoding_target_get_description (target),
342 "Plenty of pony glitter profiles");
344 GST_DEBUG ("Checking the number of profiles the target contains");
345 fail_unless_equals_int (g_list_length ((GList *)
346 gst_encoding_target_get_profiles (target)), 1);
349 GST_DEBUG ("Checking the container profile");
350 /* Check the profile */
351 prof = (GstEncodingProfile *) gst_encoding_target_get_profiles (target)->data;
352 tmpcaps = gst_caps_from_string ("animal/x-pony");
353 CHECK_PROFILE (prof, "pony", "I don't want a description !", tmpcaps, NULL, 0,
355 gst_caps_unref (tmpcaps);
357 GST_DEBUG ("Checking the container profile has 2 stream profiles");
358 /* Check the stream profiles */
359 fail_unless_equals_int (g_list_length ((GList *)
360 gst_encoding_container_profile_get_profiles (
361 (GstEncodingContainerProfile *) prof)), 2);
363 GST_DEBUG ("Checking the container profile has the audio/x-pony-song stream");
364 tmpcaps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
365 tmpcaps2 = gst_caps_from_string ("audio/x-raw-int,channels=1,rate=44100");
367 (GstEncodingProfile *) gst_encoding_audio_profile_new (tmpcaps, NULL,
369 fail_unless (gst_encoding_container_profile_contains_profile (
370 (GstEncodingContainerProfile *) prof, sprof1));
371 gst_encoding_profile_unref (sprof1);
372 gst_caps_unref (tmpcaps);
373 gst_caps_unref (tmpcaps2);
375 GST_DEBUG ("Checking the container profile has the video//x-glitter stream");
376 tmpcaps = gst_caps_from_string ("video/x-glitter,sparkling=True");
379 ("video/x-raw-yuv,width=640,height=480,framerate=15/1");
380 sprof2 = (GstEncodingProfile *)
381 gst_encoding_video_profile_new (tmpcaps, "seriously glittery", tmpcaps2,
383 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
385 fail_unless (gst_encoding_container_profile_contains_profile (
386 (GstEncodingContainerProfile *) prof, sprof2));
387 gst_encoding_profile_unref (sprof2);
388 gst_caps_unref (tmpcaps);
389 gst_caps_unref (tmpcaps2);
392 GST_START_TEST (test_loading_profile)
394 GstEncodingTarget *target;
395 gchar *profile_file_name;
396 GstEncodingProfile *profile;
398 GValue strvalue = { 0, };
399 GValue objectvalue = { 0, };
401 /* Test loading using short method and all arguments */
402 target = gst_encoding_target_load ("myponytarget", "herding", NULL);
403 fail_unless (target != NULL);
404 test_individual_target (target);
405 gst_encoding_target_unref (target);
407 /* Test loading using short method and no category */
408 target = gst_encoding_target_load ("myponytarget", NULL, NULL);
409 fail_unless (target != NULL);
410 test_individual_target (target);
411 gst_encoding_target_unref (target);
413 /* Test loading using fully specified path */
414 profile_file_name = g_build_filename (g_get_home_dir (), ".gstreamer-0.11",
415 "encoding-profiles", "herding", "myponytarget.gep", NULL);
417 GST_DEBUG ("Loading target from '%s'", profile_file_name);
418 target = gst_encoding_target_load_from_file (profile_file_name, NULL);
419 g_free (profile_file_name);
420 fail_unless (target != NULL);
421 test_individual_target (target);
422 gst_encoding_target_unref (target);
424 /* Test getting the profiles directly
425 * First without category */
426 profile = gst_encoding_profile_find ("myponytarget", "pony", NULL);
427 fail_unless (profile != NULL);
428 tmpcaps = gst_caps_from_string ("animal/x-pony");
429 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
431 gst_caps_unref (tmpcaps);
432 gst_encoding_profile_unref (profile);
434 /* Then with a specific category */
435 profile = gst_encoding_profile_find ("myponytarget", "pony", "herding");
436 fail_unless (profile != NULL);
437 tmpcaps = gst_caps_from_string ("animal/x-pony");
438 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
440 gst_caps_unref (tmpcaps);
441 gst_encoding_profile_unref (profile);
443 /* For my next trick, I will need the assistance of a GValue */
444 g_value_init (&strvalue, G_TYPE_STRING);
445 g_value_init (&objectvalue, GST_TYPE_ENCODING_PROFILE);
446 g_value_set_static_string (&strvalue, "myponytarget/pony");
447 fail_unless (g_value_transform (&strvalue, &objectvalue));
448 profile = (GstEncodingProfile *) g_value_dup_object (&objectvalue);
449 fail_if (profile == NULL);
450 g_value_unset (&strvalue);
451 g_value_unset (&objectvalue);
452 tmpcaps = gst_caps_from_string ("animal/x-pony");
453 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
455 gst_caps_unref (tmpcaps);
456 gst_encoding_profile_unref (profile);
458 /* Let's go crazy for error detection */
459 fail_if (gst_encoding_profile_find ("myponytarget", "whales", NULL));
460 fail_if (gst_encoding_profile_find ("myponytarget", "whales", "herding"));
461 fail_if (gst_encoding_profile_find ("myponytarget", "", NULL));
462 fail_if (gst_encoding_profile_find ("", "pony", NULL));
467 GST_START_TEST (test_target_list)
473 /* Make sure we get our test category in the available categories */
474 categories = gst_encoding_list_available_categories ();
475 fail_if (categories == NULL);
476 fail_if (g_list_find_custom (categories, "herding",
477 (GCompareFunc) g_strcmp0) == NULL);
478 g_list_foreach (categories, (GFunc) g_free, NULL);
479 g_list_free (categories);
481 /* Try getting all available targets with a specified category */
482 targets = gst_encoding_list_all_targets ("herding");
483 fail_if (targets == NULL);
484 for (tmp = targets; tmp; tmp = tmp->next) {
485 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
486 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
489 /* If tmp is NULL, it means we iterated the whole list without finding
491 fail_if (tmp == NULL);
492 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
493 g_list_free (targets);
495 /* Try getting all available targets without a specified category */
496 targets = gst_encoding_list_all_targets (NULL);
497 fail_if (targets == NULL);
498 for (tmp = targets; tmp; tmp = tmp->next) {
499 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
500 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
503 /* If tmp is NULL, it means we iterated the whole list without finding
505 fail_if (tmp == NULL);
506 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
507 g_list_free (targets);
513 static const gchar *profile_string = "\
514 [GStreamer Encoding Target]\n\
517 description=Plenty of pony glitter profiles\n\
522 description=I don't want a description !\n\
523 format=animal/x-pony\n\
525 [streamprofile-pony11]\n\
528 format=audio/x-pony-song,pretty=True\n\
529 restriction=audio/x-raw-int,channels=1,rate=44100\n\
532 [streamprofile-pony12]\n\
535 preset=seriously glittery\n\
536 format=video/x-glitter,sparkling=True\n\
537 restriction=video/x-raw-yuv,width=640,height=480,framerate=15/1\n\
539 variableframerate=true\n\
543 remove_profile_file (void)
545 gchar *profile_file_name;
547 profile_file_name = g_build_filename (g_get_home_dir (), ".gstreamer-0.11",
548 "encoding-profiles", "herding", "myponytarget.gep", NULL);
549 g_unlink (profile_file_name);
550 g_free (profile_file_name);
551 profile_file_name = g_build_filename (g_get_home_dir (), ".gstreamer-0.11",
552 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
553 g_unlink (profile_file_name);
554 g_free (profile_file_name);
558 create_profile_file (void)
560 gchar *profile_file_name;
562 GError *error = NULL;
565 g_build_filename (g_get_home_dir (), ".gstreamer-0.11",
566 "encoding-profiles", "herding", NULL);
568 g_build_filename (g_get_home_dir (), ".gstreamer-0.11",
569 "encoding-profiles", "herding", "myponytarget.gep", NULL);
570 g_mkdir_with_parents (profile_dir, S_IRUSR | S_IWUSR | S_IXUSR);
571 if (!g_file_set_contents (profile_file_name, profile_string,
572 strlen (profile_string), &error))
573 GST_WARNING ("Couldn't write contents to file : %s", error->message);
574 g_free (profile_dir);
575 g_free (profile_file_name);
581 create_profile_file ();
587 remove_profile_file ();
594 Suite *s = suite_create ("profile support library");
595 TCase *tc_chain = tcase_create ("general");
599 /* cehck if we can create profiles */
600 gst_dir = g_build_filename (g_get_home_dir (), ".gstreamer-0.11", NULL);
601 can_write = (g_access (gst_dir, R_OK | W_OK | X_OK) == 0);
604 suite_add_tcase (s, tc_chain);
606 tcase_add_test (tc_chain, test_profile_creation);
607 tcase_add_test (tc_chain, test_profile_input_caps);
608 tcase_add_test (tc_chain, test_target_naming);
609 tcase_add_test (tc_chain, test_target_profile);
611 tcase_add_test (tc_chain, test_loading_profile);
612 tcase_add_test (tc_chain, test_saving_profile);
613 tcase_add_test (tc_chain, test_target_list);
616 tcase_add_unchecked_fixture (tc_chain, test_setup, test_teardown);
621 GST_CHECK_MAIN (profile);