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 */
293 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
294 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
295 GST_DEBUG ("Loading target from '%s'", profile_file_name);
296 loaded = gst_encoding_target_load_from_file (profile_file_name, NULL);
297 fail_unless (loaded != NULL);
298 g_free (profile_file_name);
300 GST_DEBUG ("Checking targets are equal");
301 /* Check targets are identical */
302 /* 1. at the target level */
303 fail_unless_equals_string (gst_encoding_target_get_name (orig),
304 gst_encoding_target_get_name (loaded));
305 fail_unless_equals_string (gst_encoding_target_get_category (orig),
306 gst_encoding_target_get_category (loaded));
307 fail_unless_equals_string (gst_encoding_target_get_description (orig),
308 gst_encoding_target_get_description (loaded));
309 fail_unless_equals_int (g_list_length ((GList *)
310 gst_encoding_target_get_profiles (loaded)), 1);
312 /* 2. at the profile level */
314 (GstEncodingProfile *) gst_encoding_target_get_profiles (loaded)->data;
316 (GstEncodingProfile *) gst_encoding_target_get_profiles (orig)->data;
318 fail_unless_equals_int (G_TYPE_FROM_INSTANCE (profloaded),
319 G_TYPE_FROM_INSTANCE (proforig));
320 GST_DEBUG ("Comparing loaded:%p to original:%p", profloaded, proforig);
321 fail_unless (gst_encoding_profile_is_equal (profloaded, proforig));
323 gst_encoding_target_unref (orig);
324 gst_encoding_target_unref (loaded);
330 test_individual_target (GstEncodingTarget * target)
332 GstEncodingProfile *prof;
333 GstCaps *tmpcaps, *tmpcaps2;
334 GstEncodingProfile *sprof1, *sprof2;
336 GST_DEBUG ("Checking the target properties");
337 /* Check the target */
338 fail_unless_equals_string (gst_encoding_target_get_name (target),
340 fail_unless_equals_string (gst_encoding_target_get_category (target),
342 fail_unless_equals_string (gst_encoding_target_get_description (target),
343 "Plenty of pony glitter profiles");
345 GST_DEBUG ("Checking the number of profiles the target contains");
346 fail_unless_equals_int (g_list_length ((GList *)
347 gst_encoding_target_get_profiles (target)), 1);
350 GST_DEBUG ("Checking the container profile");
351 /* Check the profile */
352 prof = (GstEncodingProfile *) gst_encoding_target_get_profiles (target)->data;
353 tmpcaps = gst_caps_from_string ("animal/x-pony");
354 CHECK_PROFILE (prof, "pony", "I don't want a description !", tmpcaps, NULL, 0,
356 gst_caps_unref (tmpcaps);
358 GST_DEBUG ("Checking the container profile has 2 stream profiles");
359 /* Check the stream profiles */
360 fail_unless_equals_int (g_list_length ((GList *)
361 gst_encoding_container_profile_get_profiles (
362 (GstEncodingContainerProfile *) prof)), 2);
364 GST_DEBUG ("Checking the container profile has the audio/x-pony-song stream");
365 tmpcaps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
366 tmpcaps2 = gst_caps_from_string ("audio/x-raw-int,channels=1,rate=44100");
368 (GstEncodingProfile *) gst_encoding_audio_profile_new (tmpcaps, NULL,
370 fail_unless (gst_encoding_container_profile_contains_profile (
371 (GstEncodingContainerProfile *) prof, sprof1));
372 gst_encoding_profile_unref (sprof1);
373 gst_caps_unref (tmpcaps);
374 gst_caps_unref (tmpcaps2);
376 GST_DEBUG ("Checking the container profile has the video//x-glitter stream");
377 tmpcaps = gst_caps_from_string ("video/x-glitter,sparkling=True");
380 ("video/x-raw-yuv,width=640,height=480,framerate=15/1");
381 sprof2 = (GstEncodingProfile *)
382 gst_encoding_video_profile_new (tmpcaps, "seriously glittery", tmpcaps2,
384 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
386 fail_unless (gst_encoding_container_profile_contains_profile (
387 (GstEncodingContainerProfile *) prof, sprof2));
388 gst_encoding_profile_unref (sprof2);
389 gst_caps_unref (tmpcaps);
390 gst_caps_unref (tmpcaps2);
393 GST_START_TEST (test_loading_profile)
395 GstEncodingTarget *target;
396 gchar *profile_file_name;
397 GstEncodingProfile *profile;
399 GValue strvalue = { 0, };
400 GValue objectvalue = { 0, };
402 /* Test loading using short method and all arguments */
403 target = gst_encoding_target_load ("myponytarget", "herding", NULL);
404 fail_unless (target != NULL);
405 test_individual_target (target);
406 gst_encoding_target_unref (target);
408 /* Test loading using short method and no category */
409 target = gst_encoding_target_load ("myponytarget", NULL, NULL);
410 fail_unless (target != NULL);
411 test_individual_target (target);
412 gst_encoding_target_unref (target);
414 /* Test loading using fully specified path */
416 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
417 "encoding-profiles", "herding", "myponytarget.gep", NULL);
419 GST_DEBUG ("Loading target from '%s'", profile_file_name);
420 target = gst_encoding_target_load_from_file (profile_file_name, NULL);
421 g_free (profile_file_name);
422 fail_unless (target != NULL);
423 test_individual_target (target);
424 gst_encoding_target_unref (target);
426 /* Test getting the profiles directly
427 * First without category */
428 profile = gst_encoding_profile_find ("myponytarget", "pony", NULL);
429 fail_unless (profile != NULL);
430 tmpcaps = gst_caps_from_string ("animal/x-pony");
431 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
433 gst_caps_unref (tmpcaps);
434 gst_encoding_profile_unref (profile);
436 /* Then with a specific category */
437 profile = gst_encoding_profile_find ("myponytarget", "pony", "herding");
438 fail_unless (profile != NULL);
439 tmpcaps = gst_caps_from_string ("animal/x-pony");
440 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
442 gst_caps_unref (tmpcaps);
443 gst_encoding_profile_unref (profile);
445 /* For my next trick, I will need the assistance of a GValue */
446 g_value_init (&strvalue, G_TYPE_STRING);
447 g_value_init (&objectvalue, GST_TYPE_ENCODING_PROFILE);
448 g_value_set_static_string (&strvalue, "myponytarget/pony");
449 fail_unless (g_value_transform (&strvalue, &objectvalue));
450 profile = (GstEncodingProfile *) g_value_dup_object (&objectvalue);
451 fail_if (profile == NULL);
452 g_value_unset (&strvalue);
453 g_value_unset (&objectvalue);
454 tmpcaps = gst_caps_from_string ("animal/x-pony");
455 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
457 gst_caps_unref (tmpcaps);
458 gst_encoding_profile_unref (profile);
460 /* Let's go crazy for error detection */
461 fail_if (gst_encoding_profile_find ("myponytarget", "whales", NULL));
462 fail_if (gst_encoding_profile_find ("myponytarget", "whales", "herding"));
463 fail_if (gst_encoding_profile_find ("myponytarget", "", NULL));
464 fail_if (gst_encoding_profile_find ("", "pony", NULL));
469 GST_START_TEST (test_target_list)
475 /* Make sure we get our test category in the available categories */
476 categories = gst_encoding_list_available_categories ();
477 fail_if (categories == NULL);
478 fail_if (g_list_find_custom (categories, "herding",
479 (GCompareFunc) g_strcmp0) == NULL);
480 g_list_foreach (categories, (GFunc) g_free, NULL);
481 g_list_free (categories);
483 /* Try getting all available targets with a specified category */
484 targets = gst_encoding_list_all_targets ("herding");
485 fail_if (targets == NULL);
486 for (tmp = targets; tmp; tmp = tmp->next) {
487 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
488 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
491 /* If tmp is NULL, it means we iterated the whole list without finding
493 fail_if (tmp == NULL);
494 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
495 g_list_free (targets);
497 /* Try getting all available targets without a specified category */
498 targets = gst_encoding_list_all_targets (NULL);
499 fail_if (targets == NULL);
500 for (tmp = targets; tmp; tmp = tmp->next) {
501 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
502 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
505 /* If tmp is NULL, it means we iterated the whole list without finding
507 fail_if (tmp == NULL);
508 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
509 g_list_free (targets);
515 static const gchar *profile_string = "\
516 [GStreamer Encoding Target]\n\
519 description=Plenty of pony glitter profiles\n\
524 description=I don't want a description !\n\
525 format=animal/x-pony\n\
527 [streamprofile-pony11]\n\
530 format=audio/x-pony-song,pretty=True\n\
531 restriction=audio/x-raw-int,channels=1,rate=44100\n\
534 [streamprofile-pony12]\n\
537 preset=seriously glittery\n\
538 format=video/x-glitter,sparkling=True\n\
539 restriction=video/x-raw-yuv,width=640,height=480,framerate=15/1\n\
541 variableframerate=true\n\
545 remove_profile_file (void)
547 gchar *profile_file_name;
550 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
551 "encoding-profiles", "herding", "myponytarget.gep", NULL);
552 g_unlink (profile_file_name);
553 g_free (profile_file_name);
555 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
556 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
557 g_unlink (profile_file_name);
558 g_free (profile_file_name);
562 create_profile_file (void)
564 gchar *profile_file_name;
566 GError *error = NULL;
569 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
570 "encoding-profiles", "herding", NULL);
572 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
573 "encoding-profiles", "herding", "myponytarget.gep", NULL);
574 g_mkdir_with_parents (profile_dir, S_IRUSR | S_IWUSR | S_IXUSR);
575 if (!g_file_set_contents (profile_file_name, profile_string,
576 strlen (profile_string), &error))
577 GST_WARNING ("Couldn't write contents to file : %s", error->message);
578 g_free (profile_dir);
579 g_free (profile_file_name);
585 create_profile_file ();
591 remove_profile_file ();
598 Suite *s = suite_create ("profile support library");
599 TCase *tc_chain = tcase_create ("general");
603 /* cehck if we can create profiles */
604 gst_dir = g_build_filename (g_get_user_data_dir (), "gstreamer-0.11", NULL);
605 can_write = (g_access (gst_dir, R_OK | W_OK | X_OK) == 0);
608 suite_add_tcase (s, tc_chain);
610 tcase_add_test (tc_chain, test_profile_creation);
611 tcase_add_test (tc_chain, test_profile_input_caps);
612 tcase_add_test (tc_chain, test_target_naming);
613 tcase_add_test (tc_chain, test_target_profile);
615 tcase_add_test (tc_chain, test_loading_profile);
616 tcase_add_test (tc_chain, test_saving_profile);
617 tcase_add_test (tc_chain, test_target_list);
620 tcase_add_unchecked_fixture (tc_chain, test_setup, test_teardown);
625 GST_CHECK_MAIN (profile);