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,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;
140 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
143 ASSERT_CRITICAL (target = gst_encoding_target_new (NULL, NULL, NULL, NULL));
144 fail_if (target != NULL);
145 ASSERT_CRITICAL (target =
146 gst_encoding_target_new ("donkey", NULL, NULL, NULL));
147 fail_if (target != NULL);
148 ASSERT_CRITICAL (target =
149 gst_encoding_target_new (NULL, "donkey", NULL, NULL));
150 fail_if (target != NULL);
151 ASSERT_CRITICAL (target =
152 gst_encoding_target_new (NULL, NULL, "donkey", NULL));
153 fail_if (target != NULL);
155 /* Name and Category validation */
157 /* empty non-NULL strings */
158 fail_if (gst_encoding_target_new ("", "valid", "description", NULL) != NULL);
159 fail_if (gst_encoding_target_new ("valid", "", "description", NULL) != NULL);
161 /* don't start with a lower case ASCII character */
162 fail_if (gst_encoding_target_new ("A", "valid", "description", NULL) != NULL);
163 fail_if (gst_encoding_target_new ("3", "valid", "description", NULL) != NULL);
164 fail_if (gst_encoding_target_new ("-", "valid", "description", NULL) != NULL);
165 fail_if (gst_encoding_target_new ("!", "valid", "description", NULL) != NULL);
166 fail_if (gst_encoding_target_new (" ", "valid", "description", NULL) != NULL);
167 fail_if (gst_encoding_target_new ("valid", "A", "description", NULL) != NULL);
168 fail_if (gst_encoding_target_new ("valid", "3", "description", NULL) != NULL);
169 fail_if (gst_encoding_target_new ("valid", "-", "description", NULL) != NULL);
170 fail_if (gst_encoding_target_new ("valid", "!", "description", NULL) != NULL);
171 fail_if (gst_encoding_target_new ("valid", " ", "description", NULL) != NULL);
173 /* Starting with anything else is valid */
174 target = gst_encoding_target_new ("a", "valid", "description", NULL);
175 fail_if (target == NULL);
176 gst_encoding_target_unref (target);
177 target = gst_encoding_target_new ("z", "valid", "description", NULL);
178 fail_if (target == NULL);
179 gst_encoding_target_unref (target);
180 target = gst_encoding_target_new ("valid", "a", "description", NULL);
181 fail_if (target == NULL);
182 gst_encoding_target_unref (target);
183 target = gst_encoding_target_new ("valid", "z", "description", NULL);
184 fail_if (target == NULL);
185 gst_encoding_target_unref (target);
187 /* only inner valid characters are lower-case ASCII letters *OR* digits *OR* hyphens */
188 fail_if (gst_encoding_target_new ("aA", "valid", "description",
190 fail_if (gst_encoding_target_new ("a!", "valid", "description",
192 fail_if (gst_encoding_target_new ("space donkeys", "valid", "description",
194 fail_if (gst_encoding_target_new ("howaboutùnicode", "valid", "description",
196 fail_if (gst_encoding_target_new ("valid", "aA", "description",
198 fail_if (gst_encoding_target_new ("valid", "a!", "description",
202 gst_encoding_target_new ("donkey-4-ever", "valid", "description", NULL);
203 fail_if (target == NULL);
204 gst_encoding_target_unref (target);
206 gst_encoding_target_new ("valid", "donkey-4-ever", "description", NULL);
207 fail_if (target == NULL);
208 gst_encoding_target_unref (target);
214 static GstEncodingTarget *
215 create_saveload_target (const gchar * targetname)
217 GstEncodingTarget *target;
218 GstEncodingProfile *profile, *sprof;
219 GstCaps *caps, *caps2;
221 GST_DEBUG ("Creating target");
223 target = gst_encoding_target_new (targetname, "herding",
224 "Plenty of pony glitter profiles", NULL);
225 caps = gst_caps_from_string ("animal/x-pony");
227 (GstEncodingProfile *) gst_encoding_container_profile_new ("pony",
228 "I don't want a description !", caps, NULL);
229 gst_caps_unref (caps);
230 gst_encoding_target_add_profile (target, profile);
232 caps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
233 caps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
235 (GstEncodingProfile *) gst_encoding_audio_profile_new (caps, NULL, caps2,
237 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
239 gst_caps_unref (caps);
240 gst_caps_unref (caps2);
242 caps = gst_caps_from_string ("video/x-glitter,sparkling=True");
244 gst_caps_from_string ("video/x-raw,width=640,height=480,framerate=15/1");
245 sprof = (GstEncodingProfile *)
246 gst_encoding_video_profile_new (caps, "seriously glittery", caps2, 0);
247 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
249 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
251 gst_caps_unref (caps);
252 gst_caps_unref (caps2);
257 GST_START_TEST (test_target_profile)
259 GstEncodingTarget *target;
260 GstEncodingProfile *prof;
262 target = create_saveload_target ("myponytarget");
264 /* NULL isn't a valid profile name */
265 ASSERT_CRITICAL (gst_encoding_target_get_profile (target, NULL));
267 /* try finding a profile that doesn't exist */
268 fail_if (gst_encoding_target_get_profile (target,
269 "no-really-does-not-exist"));
271 /* try finding a profile that exists */
272 prof = gst_encoding_target_get_profile (target, "pony");
273 fail_if (prof == NULL);
275 gst_encoding_profile_unref (prof);
276 gst_encoding_target_unref (target);
281 GST_START_TEST (test_saving_profile)
283 GstEncodingTarget *orig, *loaded = NULL;
284 GstEncodingProfile *proforig, *profloaded;
285 gchar *profile_file_name;
287 /* Create and store a target */
288 orig = create_saveload_target ("myponytarget2");
289 GST_DEBUG ("Saving target 'myponytarget2'");
290 fail_unless (gst_encoding_target_save (orig, NULL));
292 /* Check we can load it */
294 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
295 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
296 GST_DEBUG ("Loading target from '%s'", profile_file_name);
297 loaded = gst_encoding_target_load_from_file (profile_file_name, NULL);
298 fail_unless (loaded != NULL);
299 g_free (profile_file_name);
301 GST_DEBUG ("Checking targets are equal");
302 /* Check targets are identical */
303 /* 1. at the target level */
304 fail_unless_equals_string (gst_encoding_target_get_name (orig),
305 gst_encoding_target_get_name (loaded));
306 fail_unless_equals_string (gst_encoding_target_get_category (orig),
307 gst_encoding_target_get_category (loaded));
308 fail_unless_equals_string (gst_encoding_target_get_description (orig),
309 gst_encoding_target_get_description (loaded));
310 fail_unless_equals_int (g_list_length ((GList *)
311 gst_encoding_target_get_profiles (loaded)), 1);
313 /* 2. at the profile level */
315 (GstEncodingProfile *) gst_encoding_target_get_profiles (loaded)->data;
317 (GstEncodingProfile *) gst_encoding_target_get_profiles (orig)->data;
319 fail_unless_equals_int (G_TYPE_FROM_INSTANCE (profloaded),
320 G_TYPE_FROM_INSTANCE (proforig));
321 GST_DEBUG ("Comparing loaded:%p to original:%p", profloaded, proforig);
322 fail_unless (gst_encoding_profile_is_equal (profloaded, proforig));
324 gst_encoding_target_unref (orig);
325 gst_encoding_target_unref (loaded);
331 test_individual_target (GstEncodingTarget * target)
333 GstEncodingProfile *prof;
334 GstCaps *tmpcaps, *tmpcaps2;
335 GstEncodingProfile *sprof1, *sprof2;
337 GST_DEBUG ("Checking the target properties");
338 /* Check the target */
339 fail_unless_equals_string (gst_encoding_target_get_name (target),
341 fail_unless_equals_string (gst_encoding_target_get_category (target),
343 fail_unless_equals_string (gst_encoding_target_get_description (target),
344 "Plenty of pony glitter profiles");
346 GST_DEBUG ("Checking the number of profiles the target contains");
347 fail_unless_equals_int (g_list_length ((GList *)
348 gst_encoding_target_get_profiles (target)), 1);
351 GST_DEBUG ("Checking the container profile");
352 /* Check the profile */
353 prof = (GstEncodingProfile *) gst_encoding_target_get_profiles (target)->data;
354 tmpcaps = gst_caps_from_string ("animal/x-pony");
355 CHECK_PROFILE (prof, "pony", "I don't want a description !", tmpcaps, NULL, 0,
357 gst_caps_unref (tmpcaps);
359 GST_DEBUG ("Checking the container profile has 2 stream profiles");
360 /* Check the stream profiles */
361 fail_unless_equals_int (g_list_length ((GList *)
362 gst_encoding_container_profile_get_profiles (
363 (GstEncodingContainerProfile *) prof)), 2);
365 GST_DEBUG ("Checking the container profile has the audio/x-pony-song stream");
366 tmpcaps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
367 tmpcaps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
369 (GstEncodingProfile *) gst_encoding_audio_profile_new (tmpcaps, NULL,
371 fail_unless (gst_encoding_container_profile_contains_profile (
372 (GstEncodingContainerProfile *) prof, sprof1));
373 gst_encoding_profile_unref (sprof1);
374 gst_caps_unref (tmpcaps);
375 gst_caps_unref (tmpcaps2);
377 GST_DEBUG ("Checking the container profile has the video//x-glitter stream");
378 tmpcaps = gst_caps_from_string ("video/x-glitter,sparkling=True");
380 gst_caps_from_string ("video/x-raw,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 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
404 /* Test loading using short method and all arguments */
405 target = gst_encoding_target_load ("myponytarget", "herding", NULL);
406 fail_unless (target != NULL);
407 test_individual_target (target);
408 gst_encoding_target_unref (target);
410 /* Test loading using short method and no category */
411 target = gst_encoding_target_load ("myponytarget", NULL, NULL);
412 fail_unless (target != NULL);
413 test_individual_target (target);
414 gst_encoding_target_unref (target);
416 /* Test loading using fully specified path */
418 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
419 "encoding-profiles", "herding", "myponytarget.gep", NULL);
421 GST_DEBUG ("Loading target from '%s'", profile_file_name);
422 target = gst_encoding_target_load_from_file (profile_file_name, NULL);
423 g_free (profile_file_name);
424 fail_unless (target != NULL);
425 test_individual_target (target);
426 gst_encoding_target_unref (target);
428 /* Test getting the profiles directly
429 * First without category */
430 profile = gst_encoding_profile_find ("myponytarget", "pony", NULL);
431 fail_unless (profile != NULL);
432 tmpcaps = gst_caps_from_string ("animal/x-pony");
433 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
435 gst_caps_unref (tmpcaps);
436 gst_encoding_profile_unref (profile);
438 /* Then with a specific category */
439 profile = gst_encoding_profile_find ("myponytarget", "pony", "herding");
440 fail_unless (profile != NULL);
441 tmpcaps = gst_caps_from_string ("animal/x-pony");
442 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
444 gst_caps_unref (tmpcaps);
445 gst_encoding_profile_unref (profile);
447 /* For my next trick, I will need the assistance of a GValue */
448 g_value_init (&strvalue, G_TYPE_STRING);
449 g_value_init (&objectvalue, GST_TYPE_ENCODING_PROFILE);
450 g_value_set_static_string (&strvalue, "myponytarget/pony");
451 fail_unless (g_value_transform (&strvalue, &objectvalue));
452 profile = (GstEncodingProfile *) g_value_dup_object (&objectvalue);
453 fail_if (profile == NULL);
454 g_value_unset (&strvalue);
455 g_value_unset (&objectvalue);
456 tmpcaps = gst_caps_from_string ("animal/x-pony");
457 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
459 gst_caps_unref (tmpcaps);
460 gst_encoding_profile_unref (profile);
462 /* Let's go crazy for error detection */
463 fail_if (gst_encoding_profile_find ("myponytarget", "whales", NULL));
464 fail_if (gst_encoding_profile_find ("myponytarget", "whales", "herding"));
465 fail_if (gst_encoding_profile_find ("myponytarget", "", NULL));
466 fail_if (gst_encoding_profile_find ("", "pony", NULL));
471 GST_START_TEST (test_target_list)
477 /* Make sure we get our test category in the available categories */
478 categories = gst_encoding_list_available_categories ();
479 fail_if (categories == NULL);
480 fail_if (g_list_find_custom (categories, "herding",
481 (GCompareFunc) g_strcmp0) == NULL);
482 g_list_foreach (categories, (GFunc) g_free, NULL);
483 g_list_free (categories);
485 /* Try getting all available targets with a specified category */
486 targets = gst_encoding_list_all_targets ("herding");
487 fail_if (targets == NULL);
488 for (tmp = targets; tmp; tmp = tmp->next) {
489 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
490 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
493 /* If tmp is NULL, it means we iterated the whole list without finding
495 fail_if (tmp == NULL);
496 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
497 g_list_free (targets);
499 /* Try getting all available targets without a specified category */
500 targets = gst_encoding_list_all_targets (NULL);
501 fail_if (targets == NULL);
502 for (tmp = targets; tmp; tmp = tmp->next) {
503 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
504 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
507 /* If tmp is NULL, it means we iterated the whole list without finding
509 fail_if (tmp == NULL);
510 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
511 g_list_free (targets);
517 static const gchar *profile_string = "\
518 [GStreamer Encoding Target]\n\
521 description=Plenty of pony glitter profiles\n\
526 description=I don't want a description !\n\
527 format=animal/x-pony\n\
529 [streamprofile-pony11]\n\
532 format=audio/x-pony-song,pretty=True\n\
533 restriction=audio/x-raw,channels=1,rate=44100\n\
536 [streamprofile-pony12]\n\
539 preset=seriously glittery\n\
540 format=video/x-glitter,sparkling=True\n\
541 restriction=video/x-raw,width=640,height=480,framerate=15/1\n\
543 variableframerate=true\n\
547 remove_profile_file (void)
549 gchar *profile_file_name;
552 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
553 "encoding-profiles", "herding", "myponytarget.gep", NULL);
554 g_unlink (profile_file_name);
555 g_free (profile_file_name);
557 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
558 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
559 g_unlink (profile_file_name);
560 g_free (profile_file_name);
564 create_profile_file (void)
566 gchar *profile_file_name;
568 GError *error = NULL;
571 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
572 "encoding-profiles", "herding", NULL);
574 g_build_filename (g_get_user_data_dir (), "gstreamer-0.11",
575 "encoding-profiles", "herding", "myponytarget.gep", NULL);
576 g_mkdir_with_parents (profile_dir, S_IRUSR | S_IWUSR | S_IXUSR);
577 if (!g_file_set_contents (profile_file_name, profile_string,
578 strlen (profile_string), &error))
579 GST_WARNING ("Couldn't write contents to file : %s", error->message);
580 g_free (profile_dir);
581 g_free (profile_file_name);
587 create_profile_file ();
593 remove_profile_file ();
600 Suite *s = suite_create ("profile support library");
601 TCase *tc_chain = tcase_create ("general");
605 /* cehck if we can create profiles */
606 gst_dir = g_build_filename (g_get_user_data_dir (), "gstreamer-0.11", NULL);
607 can_write = (g_access (gst_dir, R_OK | W_OK | X_OK) == 0);
610 suite_add_tcase (s, tc_chain);
612 tcase_add_test (tc_chain, test_profile_creation);
613 tcase_add_test (tc_chain, test_profile_input_caps);
614 tcase_add_test (tc_chain, test_target_naming);
615 tcase_add_test (tc_chain, test_target_profile);
617 tcase_add_test (tc_chain, test_loading_profile);
618 tcase_add_test (tc_chain, test_saving_profile);
619 tcase_add_test (tc_chain, test_target_list);
622 tcase_add_unchecked_fixture (tc_chain, test_setup, test_teardown);
627 GST_CHECK_MAIN (profile);