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 static inline gboolean
35 gst_caps_is_equal_unref (GstCaps * caps1, GstCaps * caps2)
39 ret = gst_caps_is_equal (caps1, caps2);
40 gst_caps_unref (caps1);
45 #define CHECK_PROFILE(profile, name, description, format, preset, presence, restriction) \
47 fail_if(profile == NULL); \
48 fail_unless_equals_string (gst_encoding_profile_get_name (profile), name); \
49 fail_unless_equals_string (gst_encoding_profile_get_description (profile), description); \
50 fail_unless (gst_caps_is_equal_unref (gst_encoding_profile_get_format (profile), format)); \
51 fail_unless_equals_string (gst_encoding_profile_get_preset (profile), preset); \
52 fail_unless_equals_int (gst_encoding_profile_get_presence (profile), presence); \
54 fail_unless (gst_caps_is_equal_unref (gst_encoding_profile_get_restriction (profile), restriction)); \
57 GST_START_TEST (test_profile_creation)
59 GstEncodingProfile *encprof;
60 GstEncodingAudioProfile *audioprof;
61 GstEncodingVideoProfile *videoprof;
62 GstCaps *ogg, *vorbis, *theora;
63 GstCaps *test1, *test2;
65 ogg = gst_caps_new_empty_simple ("application/ogg");
66 vorbis = gst_caps_new_empty_simple ("audio/x-vorbis");
67 theora = gst_caps_new_empty_simple ("video/x-theora");
69 encprof = (GstEncodingProfile *) gst_encoding_container_profile_new ((gchar *)
70 "ogg-theora-vorbis", "dumb-profile", ogg, (gchar *) "dumb-preset");
71 CHECK_PROFILE (encprof, "ogg-theora-vorbis", "dumb-profile", ogg,
72 "dumb-preset", 0, NULL);
74 audioprof = gst_encoding_audio_profile_new (vorbis, (gchar *) "HQ", NULL, 0);
75 CHECK_PROFILE ((GstEncodingProfile *) audioprof, NULL, NULL, vorbis, "HQ", 0,
78 videoprof = gst_encoding_video_profile_new (theora, (gchar *) "HQ", NULL, 0);
79 CHECK_PROFILE ((GstEncodingProfile *) videoprof, NULL, NULL, theora, "HQ",
82 fail_unless (gst_encoding_container_profile_add_profile (
83 (GstEncodingContainerProfile *) encprof,
84 (GstEncodingProfile *) audioprof));
85 fail_unless (gst_encoding_container_profile_add_profile (
86 (GstEncodingContainerProfile *) encprof,
87 (GstEncodingProfile *) videoprof));
90 test1 = gst_caps_from_string ("video/x-theora; audio/x-vorbis");
91 test2 = gst_encoding_profile_get_input_caps (encprof);
92 fail_unless (gst_caps_is_equal (test1, test2));
93 gst_caps_unref (test1);
94 gst_caps_unref (test2);
96 gst_encoding_profile_unref (encprof);
98 gst_caps_unref (theora);
99 gst_caps_unref (vorbis);
105 GST_START_TEST (test_profile_input_caps)
107 GstEncodingProfile *sprof;
109 GstCaps *out, *restriction, *test1;
111 vorbis = gst_caps_new_empty_simple ("audio/x-vorbis");
113 /* Simple case, no restriction */
114 sprof = (GstEncodingProfile *)
115 gst_encoding_audio_profile_new (vorbis, NULL, NULL, 0);
116 fail_if (sprof == NULL);
118 out = gst_encoding_profile_get_input_caps (sprof);
119 fail_if (out == NULL);
120 fail_unless (gst_caps_is_equal (out, vorbis));
121 gst_caps_unref (out);
122 gst_encoding_profile_unref (sprof);
124 /* One simple restriction */
125 restriction = gst_caps_from_string ("audio/x-raw,channels=2,rate=44100");
126 test1 = gst_caps_from_string ("audio/x-vorbis,channels=2,rate=44100");
127 fail_if (restriction == NULL);
129 sprof = (GstEncodingProfile *)
130 gst_encoding_audio_profile_new (vorbis, NULL, restriction, 0);
131 fail_if (sprof == NULL);
133 out = gst_encoding_profile_get_input_caps (sprof);
134 fail_if (out == NULL);
135 GST_DEBUG ("got caps %" GST_PTR_FORMAT, out);
136 fail_unless (gst_caps_is_equal (out, test1));
137 gst_caps_unref (out);
138 gst_caps_unref (restriction);
139 gst_caps_unref (test1);
140 gst_encoding_profile_unref (sprof);
142 gst_caps_unref (vorbis);
148 GST_START_TEST (test_target_naming)
150 GstEncodingTarget *target;
152 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
155 ASSERT_CRITICAL (target = gst_encoding_target_new (NULL, NULL, NULL, NULL));
156 fail_if (target != NULL);
157 ASSERT_CRITICAL (target =
158 gst_encoding_target_new ("donkey", NULL, NULL, NULL));
159 fail_if (target != NULL);
160 ASSERT_CRITICAL (target =
161 gst_encoding_target_new (NULL, "donkey", NULL, NULL));
162 fail_if (target != NULL);
163 ASSERT_CRITICAL (target =
164 gst_encoding_target_new (NULL, NULL, "donkey", NULL));
165 fail_if (target != NULL);
167 /* Name and Category validation */
169 /* empty non-NULL strings */
170 fail_if (gst_encoding_target_new ("", "valid", "description", NULL) != NULL);
171 fail_if (gst_encoding_target_new ("valid", "", "description", NULL) != NULL);
173 /* don't start with a lower case ASCII character */
174 fail_if (gst_encoding_target_new ("A", "valid", "description", NULL) != NULL);
175 fail_if (gst_encoding_target_new ("3", "valid", "description", NULL) != NULL);
176 fail_if (gst_encoding_target_new ("-", "valid", "description", NULL) != NULL);
177 fail_if (gst_encoding_target_new ("!", "valid", "description", NULL) != NULL);
178 fail_if (gst_encoding_target_new (" ", "valid", "description", NULL) != NULL);
179 fail_if (gst_encoding_target_new ("valid", "A", "description", NULL) != NULL);
180 fail_if (gst_encoding_target_new ("valid", "3", "description", NULL) != NULL);
181 fail_if (gst_encoding_target_new ("valid", "-", "description", NULL) != NULL);
182 fail_if (gst_encoding_target_new ("valid", "!", "description", NULL) != NULL);
183 fail_if (gst_encoding_target_new ("valid", " ", "description", NULL) != NULL);
185 /* Starting with anything else is valid */
186 target = gst_encoding_target_new ("a", "valid", "description", NULL);
187 fail_if (target == NULL);
188 gst_encoding_target_unref (target);
189 target = gst_encoding_target_new ("z", "valid", "description", NULL);
190 fail_if (target == NULL);
191 gst_encoding_target_unref (target);
192 target = gst_encoding_target_new ("valid", "a", "description", NULL);
193 fail_if (target == NULL);
194 gst_encoding_target_unref (target);
195 target = gst_encoding_target_new ("valid", "z", "description", NULL);
196 fail_if (target == NULL);
197 gst_encoding_target_unref (target);
199 /* only inner valid characters are lower-case ASCII letters *OR* digits *OR* hyphens */
200 fail_if (gst_encoding_target_new ("aA", "valid", "description",
202 fail_if (gst_encoding_target_new ("a!", "valid", "description",
204 fail_if (gst_encoding_target_new ("space donkeys", "valid", "description",
206 fail_if (gst_encoding_target_new ("howaboutùnicode", "valid", "description",
208 fail_if (gst_encoding_target_new ("valid", "aA", "description",
210 fail_if (gst_encoding_target_new ("valid", "a!", "description",
214 gst_encoding_target_new ("donkey-4-ever", "valid", "description", NULL);
215 fail_if (target == NULL);
216 gst_encoding_target_unref (target);
218 gst_encoding_target_new ("valid", "donkey-4-ever", "description", NULL);
219 fail_if (target == NULL);
220 gst_encoding_target_unref (target);
226 static GstEncodingTarget *
227 create_saveload_target (const gchar * targetname)
229 GstEncodingTarget *target;
230 GstEncodingProfile *profile, *sprof;
231 GstCaps *caps, *caps2;
233 GST_DEBUG ("Creating target");
235 target = gst_encoding_target_new (targetname, "herding",
236 "Plenty of pony glitter profiles", NULL);
237 caps = gst_caps_from_string ("animal/x-pony");
239 (GstEncodingProfile *) gst_encoding_container_profile_new ("pony",
240 "I don't want a description !", caps, NULL);
241 gst_caps_unref (caps);
242 gst_encoding_target_add_profile (target, profile);
244 caps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
245 caps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
247 (GstEncodingProfile *) gst_encoding_audio_profile_new (caps, NULL, caps2,
249 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
251 gst_caps_unref (caps);
252 gst_caps_unref (caps2);
254 caps = gst_caps_from_string ("video/x-glitter,sparkling=True");
256 gst_caps_from_string ("video/x-raw,width=640,height=480,framerate=15/1");
257 sprof = (GstEncodingProfile *)
258 gst_encoding_video_profile_new (caps, "seriously glittery", caps2, 0);
259 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
261 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
263 gst_caps_unref (caps);
264 gst_caps_unref (caps2);
269 GST_START_TEST (test_target_profile)
271 GstEncodingTarget *target;
272 GstEncodingProfile *prof;
274 target = create_saveload_target ("myponytarget");
276 /* NULL isn't a valid profile name */
277 ASSERT_CRITICAL (gst_encoding_target_get_profile (target, NULL));
279 /* try finding a profile that doesn't exist */
280 fail_if (gst_encoding_target_get_profile (target,
281 "no-really-does-not-exist"));
283 /* try finding a profile that exists */
284 prof = gst_encoding_target_get_profile (target, "pony");
285 fail_if (prof == NULL);
287 gst_encoding_profile_unref (prof);
288 gst_encoding_target_unref (target);
293 GST_START_TEST (test_saving_profile)
295 GstEncodingTarget *orig, *loaded = NULL;
296 GstEncodingProfile *proforig, *profloaded;
297 gchar *profile_file_name;
299 /* Create and store a target */
300 orig = create_saveload_target ("myponytarget2");
301 GST_DEBUG ("Saving target 'myponytarget2'");
302 fail_unless (gst_encoding_target_save (orig, NULL));
304 /* Check we can load it */
306 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
307 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
308 GST_DEBUG ("Loading target from '%s'", profile_file_name);
309 loaded = gst_encoding_target_load_from_file (profile_file_name, NULL);
310 fail_unless (loaded != NULL);
311 g_free (profile_file_name);
313 GST_DEBUG ("Checking targets are equal");
314 /* Check targets are identical */
315 /* 1. at the target level */
316 fail_unless_equals_string (gst_encoding_target_get_name (orig),
317 gst_encoding_target_get_name (loaded));
318 fail_unless_equals_string (gst_encoding_target_get_category (orig),
319 gst_encoding_target_get_category (loaded));
320 fail_unless_equals_string (gst_encoding_target_get_description (orig),
321 gst_encoding_target_get_description (loaded));
322 fail_unless_equals_int (g_list_length ((GList *)
323 gst_encoding_target_get_profiles (loaded)), 1);
325 /* 2. at the profile level */
327 (GstEncodingProfile *) gst_encoding_target_get_profiles (loaded)->data;
329 (GstEncodingProfile *) gst_encoding_target_get_profiles (orig)->data;
331 fail_unless_equals_int (G_TYPE_FROM_INSTANCE (profloaded),
332 G_TYPE_FROM_INSTANCE (proforig));
333 GST_DEBUG ("Comparing loaded:%p to original:%p", profloaded, proforig);
334 fail_unless (gst_encoding_profile_is_equal (profloaded, proforig));
336 gst_encoding_target_unref (orig);
337 gst_encoding_target_unref (loaded);
343 test_individual_target (GstEncodingTarget * target)
345 GstEncodingProfile *prof;
346 GstCaps *tmpcaps, *tmpcaps2;
347 GstEncodingProfile *sprof1, *sprof2;
349 GST_DEBUG ("Checking the target properties");
350 /* Check the target */
351 fail_unless_equals_string (gst_encoding_target_get_name (target),
353 fail_unless_equals_string (gst_encoding_target_get_category (target),
355 fail_unless_equals_string (gst_encoding_target_get_description (target),
356 "Plenty of pony glitter profiles");
358 GST_DEBUG ("Checking the number of profiles the target contains");
359 fail_unless_equals_int (g_list_length ((GList *)
360 gst_encoding_target_get_profiles (target)), 1);
363 GST_DEBUG ("Checking the container profile");
364 /* Check the profile */
365 prof = (GstEncodingProfile *) gst_encoding_target_get_profiles (target)->data;
366 tmpcaps = gst_caps_from_string ("animal/x-pony");
367 CHECK_PROFILE (prof, "pony", "I don't want a description !", tmpcaps, NULL, 0,
369 gst_caps_unref (tmpcaps);
371 GST_DEBUG ("Checking the container profile has 2 stream profiles");
372 /* Check the stream profiles */
373 fail_unless_equals_int (g_list_length ((GList *)
374 gst_encoding_container_profile_get_profiles (
375 (GstEncodingContainerProfile *) prof)), 2);
377 GST_DEBUG ("Checking the container profile has the audio/x-pony-song stream");
378 tmpcaps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
379 tmpcaps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
381 (GstEncodingProfile *) gst_encoding_audio_profile_new (tmpcaps, NULL,
383 fail_unless (gst_encoding_container_profile_contains_profile (
384 (GstEncodingContainerProfile *) prof, sprof1));
385 gst_encoding_profile_unref (sprof1);
386 gst_caps_unref (tmpcaps);
387 gst_caps_unref (tmpcaps2);
389 GST_DEBUG ("Checking the container profile has the video//x-glitter stream");
390 tmpcaps = gst_caps_from_string ("video/x-glitter,sparkling=True");
392 gst_caps_from_string ("video/x-raw,width=640,height=480,framerate=15/1");
393 sprof2 = (GstEncodingProfile *)
394 gst_encoding_video_profile_new (tmpcaps, "seriously glittery", tmpcaps2,
396 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
398 fail_unless (gst_encoding_container_profile_contains_profile (
399 (GstEncodingContainerProfile *) prof, sprof2));
400 gst_encoding_profile_unref (sprof2);
401 gst_caps_unref (tmpcaps);
402 gst_caps_unref (tmpcaps2);
405 GST_START_TEST (test_loading_profile)
407 GstEncodingTarget *target;
408 gchar *profile_file_name;
409 GstEncodingProfile *profile;
411 GValue strvalue = { 0, };
412 GValue objectvalue = { 0, };
414 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
416 /* Test loading using short method and all arguments */
417 target = gst_encoding_target_load ("myponytarget", "herding", NULL);
418 fail_unless (target != NULL);
419 test_individual_target (target);
420 gst_encoding_target_unref (target);
422 /* Test loading using short method and no category */
423 target = gst_encoding_target_load ("myponytarget", NULL, NULL);
424 fail_unless (target != NULL);
425 test_individual_target (target);
426 gst_encoding_target_unref (target);
428 /* Test loading using fully specified path */
430 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
431 "encoding-profiles", "herding", "myponytarget.gep", NULL);
433 GST_DEBUG ("Loading target from '%s'", profile_file_name);
434 target = gst_encoding_target_load_from_file (profile_file_name, NULL);
435 g_free (profile_file_name);
436 fail_unless (target != NULL);
437 test_individual_target (target);
438 gst_encoding_target_unref (target);
440 /* Test getting the profiles directly
441 * First without category */
442 profile = gst_encoding_profile_find ("myponytarget", "pony", NULL);
443 fail_unless (profile != NULL);
444 tmpcaps = gst_caps_from_string ("animal/x-pony");
445 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
447 gst_caps_unref (tmpcaps);
448 gst_encoding_profile_unref (profile);
450 /* Then with a specific category */
451 profile = gst_encoding_profile_find ("myponytarget", "pony", "herding");
452 fail_unless (profile != NULL);
453 tmpcaps = gst_caps_from_string ("animal/x-pony");
454 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
456 gst_caps_unref (tmpcaps);
457 gst_encoding_profile_unref (profile);
459 /* For my next trick, I will need the assistance of a GValue */
460 g_value_init (&strvalue, G_TYPE_STRING);
461 g_value_init (&objectvalue, GST_TYPE_ENCODING_PROFILE);
462 g_value_set_static_string (&strvalue, "myponytarget/pony");
463 fail_unless (g_value_transform (&strvalue, &objectvalue));
464 profile = (GstEncodingProfile *) g_value_dup_object (&objectvalue);
465 fail_if (profile == NULL);
466 g_value_unset (&strvalue);
467 g_value_unset (&objectvalue);
468 tmpcaps = gst_caps_from_string ("animal/x-pony");
469 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
471 gst_caps_unref (tmpcaps);
472 gst_encoding_profile_unref (profile);
474 /* Let's go crazy for error detection */
475 fail_if (gst_encoding_profile_find ("myponytarget", "whales", NULL));
476 fail_if (gst_encoding_profile_find ("myponytarget", "whales", "herding"));
477 fail_if (gst_encoding_profile_find ("myponytarget", "", NULL));
478 fail_if (gst_encoding_profile_find ("", "pony", NULL));
483 GST_START_TEST (test_target_list)
489 /* Make sure we get our test category in the available categories */
490 categories = gst_encoding_list_available_categories ();
491 fail_if (categories == NULL);
492 fail_if (g_list_find_custom (categories, "herding",
493 (GCompareFunc) g_strcmp0) == NULL);
494 g_list_foreach (categories, (GFunc) g_free, NULL);
495 g_list_free (categories);
497 /* Try getting all available targets with a specified category */
498 targets = gst_encoding_list_all_targets ("herding");
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);
511 /* Try getting all available targets without a specified category */
512 targets = gst_encoding_list_all_targets (NULL);
513 fail_if (targets == NULL);
514 for (tmp = targets; tmp; tmp = tmp->next) {
515 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
516 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
519 /* If tmp is NULL, it means we iterated the whole list without finding
521 fail_if (tmp == NULL);
522 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
523 g_list_free (targets);
529 static const gchar *profile_string = "\
530 [GStreamer Encoding Target]\n\
533 description=Plenty of pony glitter profiles\n\
538 description=I don't want a description !\n\
539 format=animal/x-pony\n\
541 [streamprofile-pony11]\n\
544 format=audio/x-pony-song,pretty=True\n\
545 restriction=audio/x-raw,channels=1,rate=44100\n\
548 [streamprofile-pony12]\n\
551 preset=seriously glittery\n\
552 format=video/x-glitter,sparkling=True\n\
553 restriction=video/x-raw,width=640,height=480,framerate=15/1\n\
555 variableframerate=true\n\
559 remove_profile_file (void)
561 gchar *profile_file_name;
564 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
565 "encoding-profiles", "herding", "myponytarget.gep", NULL);
566 g_unlink (profile_file_name);
567 g_free (profile_file_name);
569 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
570 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
571 g_unlink (profile_file_name);
572 g_free (profile_file_name);
576 create_profile_file (void)
578 gchar *profile_file_name;
580 GError *error = NULL;
583 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
584 "encoding-profiles", "herding", NULL);
586 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
587 "encoding-profiles", "herding", "myponytarget.gep", NULL);
588 g_mkdir_with_parents (profile_dir, S_IRUSR | S_IWUSR | S_IXUSR);
589 if (!g_file_set_contents (profile_file_name, profile_string,
590 strlen (profile_string), &error))
591 GST_WARNING ("Couldn't write contents to file : %s", error->message);
592 g_free (profile_dir);
593 g_free (profile_file_name);
599 create_profile_file ();
605 remove_profile_file ();
612 Suite *s = suite_create ("profile support library");
613 TCase *tc_chain = tcase_create ("general");
617 /* cehck if we can create profiles */
618 gst_dir = g_build_filename (g_get_user_data_dir (), "gstreamer-1.0", NULL);
619 can_write = (g_access (gst_dir, R_OK | W_OK | X_OK) == 0);
622 suite_add_tcase (s, tc_chain);
624 tcase_add_test (tc_chain, test_profile_creation);
625 tcase_add_test (tc_chain, test_profile_input_caps);
626 tcase_add_test (tc_chain, test_target_naming);
627 tcase_add_test (tc_chain, test_target_profile);
629 tcase_add_test (tc_chain, test_loading_profile);
630 tcase_add_test (tc_chain, test_saving_profile);
631 tcase_add_test (tc_chain, test_target_list);
634 tcase_add_unchecked_fixture (tc_chain, test_setup, test_teardown);
639 GST_CHECK_MAIN (profile);