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); \
43 fail_unless (gst_caps_is_equal (gst_encoding_profile_get_restriction (profile), restriction)); \
46 GST_START_TEST (test_profile_creation)
48 GstEncodingProfile *encprof;
49 GstEncodingAudioProfile *audioprof;
50 GstEncodingVideoProfile *videoprof;
51 GstCaps *ogg, *vorbis, *theora;
52 GstCaps *test1, *test2;
54 ogg = gst_caps_new_empty_simple ("application/ogg");
55 vorbis = gst_caps_new_empty_simple ("audio/x-vorbis");
56 theora = gst_caps_new_empty_simple ("video/x-theora");
58 encprof = (GstEncodingProfile *) gst_encoding_container_profile_new ((gchar *)
59 "ogg-theora-vorbis", "dumb-profile", ogg, (gchar *) "dumb-preset");
60 CHECK_PROFILE (encprof, "ogg-theora-vorbis", "dumb-profile", ogg,
61 "dumb-preset", 0, NULL);
63 audioprof = gst_encoding_audio_profile_new (vorbis, (gchar *) "HQ", NULL, 0);
64 CHECK_PROFILE ((GstEncodingProfile *) audioprof, NULL, NULL, vorbis, "HQ", 0,
67 videoprof = gst_encoding_video_profile_new (theora, (gchar *) "HQ", NULL, 0);
68 CHECK_PROFILE ((GstEncodingProfile *) videoprof, NULL, NULL, theora, "HQ",
71 fail_unless (gst_encoding_container_profile_add_profile (
72 (GstEncodingContainerProfile *) encprof,
73 (GstEncodingProfile *) audioprof));
74 fail_unless (gst_encoding_container_profile_add_profile (
75 (GstEncodingContainerProfile *) encprof,
76 (GstEncodingProfile *) videoprof));
79 test1 = gst_caps_from_string ("video/x-theora; audio/x-vorbis");
80 test2 = gst_encoding_profile_get_input_caps (encprof);
81 fail_unless (gst_caps_is_equal (test1, test2));
82 gst_caps_unref (test1);
83 gst_caps_unref (test2);
85 gst_encoding_profile_unref (encprof);
87 gst_caps_unref (theora);
88 gst_caps_unref (vorbis);
94 GST_START_TEST (test_profile_input_caps)
96 GstEncodingProfile *sprof;
98 GstCaps *out, *restriction, *test1;
100 vorbis = gst_caps_new_empty_simple ("audio/x-vorbis");
102 /* Simple case, no restriction */
103 sprof = (GstEncodingProfile *)
104 gst_encoding_audio_profile_new (vorbis, NULL, NULL, 0);
105 fail_if (sprof == NULL);
107 out = gst_encoding_profile_get_input_caps (sprof);
108 fail_if (out == NULL);
109 fail_unless (gst_caps_is_equal (out, vorbis));
110 gst_caps_unref (out);
111 gst_encoding_profile_unref (sprof);
113 /* One simple restriction */
114 restriction = gst_caps_from_string ("audio/x-raw,channels=2,rate=44100");
115 test1 = gst_caps_from_string ("audio/x-vorbis,channels=2,rate=44100");
116 fail_if (restriction == NULL);
118 sprof = (GstEncodingProfile *)
119 gst_encoding_audio_profile_new (vorbis, NULL, restriction, 0);
120 fail_if (sprof == NULL);
122 out = gst_encoding_profile_get_input_caps (sprof);
123 fail_if (out == NULL);
124 GST_DEBUG ("got caps %" GST_PTR_FORMAT, out);
125 fail_unless (gst_caps_is_equal (out, test1));
126 gst_caps_unref (out);
127 gst_caps_unref (restriction);
128 gst_caps_unref (test1);
129 gst_encoding_profile_unref (sprof);
131 gst_caps_unref (vorbis);
137 GST_START_TEST (test_target_naming)
139 GstEncodingTarget *target;
141 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
144 ASSERT_CRITICAL (target = gst_encoding_target_new (NULL, NULL, NULL, NULL));
145 fail_if (target != NULL);
146 ASSERT_CRITICAL (target =
147 gst_encoding_target_new ("donkey", NULL, NULL, NULL));
148 fail_if (target != NULL);
149 ASSERT_CRITICAL (target =
150 gst_encoding_target_new (NULL, "donkey", NULL, NULL));
151 fail_if (target != NULL);
152 ASSERT_CRITICAL (target =
153 gst_encoding_target_new (NULL, NULL, "donkey", NULL));
154 fail_if (target != NULL);
156 /* Name and Category validation */
158 /* empty non-NULL strings */
159 fail_if (gst_encoding_target_new ("", "valid", "description", NULL) != NULL);
160 fail_if (gst_encoding_target_new ("valid", "", "description", NULL) != NULL);
162 /* don't start with a lower case ASCII character */
163 fail_if (gst_encoding_target_new ("A", "valid", "description", NULL) != NULL);
164 fail_if (gst_encoding_target_new ("3", "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", "description", NULL) != NULL);
168 fail_if (gst_encoding_target_new ("valid", "A", "description", NULL) != NULL);
169 fail_if (gst_encoding_target_new ("valid", "3", "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);
172 fail_if (gst_encoding_target_new ("valid", " ", "description", NULL) != NULL);
174 /* Starting with anything else is valid */
175 target = gst_encoding_target_new ("a", "valid", "description", NULL);
176 fail_if (target == NULL);
177 gst_encoding_target_unref (target);
178 target = gst_encoding_target_new ("z", "valid", "description", NULL);
179 fail_if (target == NULL);
180 gst_encoding_target_unref (target);
181 target = gst_encoding_target_new ("valid", "a", "description", NULL);
182 fail_if (target == NULL);
183 gst_encoding_target_unref (target);
184 target = gst_encoding_target_new ("valid", "z", "description", NULL);
185 fail_if (target == NULL);
186 gst_encoding_target_unref (target);
188 /* only inner valid characters are lower-case ASCII letters *OR* digits *OR* hyphens */
189 fail_if (gst_encoding_target_new ("aA", "valid", "description",
191 fail_if (gst_encoding_target_new ("a!", "valid", "description",
193 fail_if (gst_encoding_target_new ("space donkeys", "valid", "description",
195 fail_if (gst_encoding_target_new ("howaboutùnicode", "valid", "description",
197 fail_if (gst_encoding_target_new ("valid", "aA", "description",
199 fail_if (gst_encoding_target_new ("valid", "a!", "description",
203 gst_encoding_target_new ("donkey-4-ever", "valid", "description", NULL);
204 fail_if (target == NULL);
205 gst_encoding_target_unref (target);
207 gst_encoding_target_new ("valid", "donkey-4-ever", "description", NULL);
208 fail_if (target == NULL);
209 gst_encoding_target_unref (target);
215 static GstEncodingTarget *
216 create_saveload_target (const gchar * targetname)
218 GstEncodingTarget *target;
219 GstEncodingProfile *profile, *sprof;
220 GstCaps *caps, *caps2;
222 GST_DEBUG ("Creating target");
224 target = gst_encoding_target_new (targetname, "herding",
225 "Plenty of pony glitter profiles", NULL);
226 caps = gst_caps_from_string ("animal/x-pony");
228 (GstEncodingProfile *) gst_encoding_container_profile_new ("pony",
229 "I don't want a description !", caps, NULL);
230 gst_caps_unref (caps);
231 gst_encoding_target_add_profile (target, profile);
233 caps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
234 caps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
236 (GstEncodingProfile *) gst_encoding_audio_profile_new (caps, NULL, caps2,
238 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
240 gst_caps_unref (caps);
241 gst_caps_unref (caps2);
243 caps = gst_caps_from_string ("video/x-glitter,sparkling=True");
245 gst_caps_from_string ("video/x-raw,width=640,height=480,framerate=15/1");
246 sprof = (GstEncodingProfile *)
247 gst_encoding_video_profile_new (caps, "seriously glittery", caps2, 0);
248 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
250 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
252 gst_caps_unref (caps);
253 gst_caps_unref (caps2);
258 GST_START_TEST (test_target_profile)
260 GstEncodingTarget *target;
261 GstEncodingProfile *prof;
263 target = create_saveload_target ("myponytarget");
265 /* NULL isn't a valid profile name */
266 ASSERT_CRITICAL (gst_encoding_target_get_profile (target, NULL));
268 /* try finding a profile that doesn't exist */
269 fail_if (gst_encoding_target_get_profile (target,
270 "no-really-does-not-exist"));
272 /* try finding a profile that exists */
273 prof = gst_encoding_target_get_profile (target, "pony");
274 fail_if (prof == NULL);
276 gst_encoding_profile_unref (prof);
277 gst_encoding_target_unref (target);
282 GST_START_TEST (test_saving_profile)
284 GstEncodingTarget *orig, *loaded = NULL;
285 GstEncodingProfile *proforig, *profloaded;
286 gchar *profile_file_name;
288 /* Create and store a target */
289 orig = create_saveload_target ("myponytarget2");
290 GST_DEBUG ("Saving target 'myponytarget2'");
291 fail_unless (gst_encoding_target_save (orig, NULL));
293 /* Check we can load it */
295 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
296 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
297 GST_DEBUG ("Loading target from '%s'", profile_file_name);
298 loaded = gst_encoding_target_load_from_file (profile_file_name, NULL);
299 fail_unless (loaded != NULL);
300 g_free (profile_file_name);
302 GST_DEBUG ("Checking targets are equal");
303 /* Check targets are identical */
304 /* 1. at the target level */
305 fail_unless_equals_string (gst_encoding_target_get_name (orig),
306 gst_encoding_target_get_name (loaded));
307 fail_unless_equals_string (gst_encoding_target_get_category (orig),
308 gst_encoding_target_get_category (loaded));
309 fail_unless_equals_string (gst_encoding_target_get_description (orig),
310 gst_encoding_target_get_description (loaded));
311 fail_unless_equals_int (g_list_length ((GList *)
312 gst_encoding_target_get_profiles (loaded)), 1);
314 /* 2. at the profile level */
316 (GstEncodingProfile *) gst_encoding_target_get_profiles (loaded)->data;
318 (GstEncodingProfile *) gst_encoding_target_get_profiles (orig)->data;
320 fail_unless_equals_int (G_TYPE_FROM_INSTANCE (profloaded),
321 G_TYPE_FROM_INSTANCE (proforig));
322 GST_DEBUG ("Comparing loaded:%p to original:%p", profloaded, proforig);
323 fail_unless (gst_encoding_profile_is_equal (profloaded, proforig));
325 gst_encoding_target_unref (orig);
326 gst_encoding_target_unref (loaded);
332 test_individual_target (GstEncodingTarget * target)
334 GstEncodingProfile *prof;
335 GstCaps *tmpcaps, *tmpcaps2;
336 GstEncodingProfile *sprof1, *sprof2;
338 GST_DEBUG ("Checking the target properties");
339 /* Check the target */
340 fail_unless_equals_string (gst_encoding_target_get_name (target),
342 fail_unless_equals_string (gst_encoding_target_get_category (target),
344 fail_unless_equals_string (gst_encoding_target_get_description (target),
345 "Plenty of pony glitter profiles");
347 GST_DEBUG ("Checking the number of profiles the target contains");
348 fail_unless_equals_int (g_list_length ((GList *)
349 gst_encoding_target_get_profiles (target)), 1);
352 GST_DEBUG ("Checking the container profile");
353 /* Check the profile */
354 prof = (GstEncodingProfile *) gst_encoding_target_get_profiles (target)->data;
355 tmpcaps = gst_caps_from_string ("animal/x-pony");
356 CHECK_PROFILE (prof, "pony", "I don't want a description !", tmpcaps, NULL, 0,
358 gst_caps_unref (tmpcaps);
360 GST_DEBUG ("Checking the container profile has 2 stream profiles");
361 /* Check the stream profiles */
362 fail_unless_equals_int (g_list_length ((GList *)
363 gst_encoding_container_profile_get_profiles (
364 (GstEncodingContainerProfile *) prof)), 2);
366 GST_DEBUG ("Checking the container profile has the audio/x-pony-song stream");
367 tmpcaps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
368 tmpcaps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
370 (GstEncodingProfile *) gst_encoding_audio_profile_new (tmpcaps, NULL,
372 fail_unless (gst_encoding_container_profile_contains_profile (
373 (GstEncodingContainerProfile *) prof, sprof1));
374 gst_encoding_profile_unref (sprof1);
375 gst_caps_unref (tmpcaps);
376 gst_caps_unref (tmpcaps2);
378 GST_DEBUG ("Checking the container profile has the video//x-glitter stream");
379 tmpcaps = gst_caps_from_string ("video/x-glitter,sparkling=True");
381 gst_caps_from_string ("video/x-raw,width=640,height=480,framerate=15/1");
382 sprof2 = (GstEncodingProfile *)
383 gst_encoding_video_profile_new (tmpcaps, "seriously glittery", tmpcaps2,
385 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
387 fail_unless (gst_encoding_container_profile_contains_profile (
388 (GstEncodingContainerProfile *) prof, sprof2));
389 gst_encoding_profile_unref (sprof2);
390 gst_caps_unref (tmpcaps);
391 gst_caps_unref (tmpcaps2);
394 GST_START_TEST (test_loading_profile)
396 GstEncodingTarget *target;
397 gchar *profile_file_name;
398 GstEncodingProfile *profile;
400 GValue strvalue = { 0, };
401 GValue objectvalue = { 0, };
403 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
405 /* Test loading using short method and all arguments */
406 target = gst_encoding_target_load ("myponytarget", "herding", NULL);
407 fail_unless (target != NULL);
408 test_individual_target (target);
409 gst_encoding_target_unref (target);
411 /* Test loading using short method and no category */
412 target = gst_encoding_target_load ("myponytarget", NULL, NULL);
413 fail_unless (target != NULL);
414 test_individual_target (target);
415 gst_encoding_target_unref (target);
417 /* Test loading using fully specified path */
419 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
420 "encoding-profiles", "herding", "myponytarget.gep", NULL);
422 GST_DEBUG ("Loading target from '%s'", profile_file_name);
423 target = gst_encoding_target_load_from_file (profile_file_name, NULL);
424 g_free (profile_file_name);
425 fail_unless (target != NULL);
426 test_individual_target (target);
427 gst_encoding_target_unref (target);
429 /* Test getting the profiles directly
430 * First without category */
431 profile = gst_encoding_profile_find ("myponytarget", "pony", NULL);
432 fail_unless (profile != NULL);
433 tmpcaps = gst_caps_from_string ("animal/x-pony");
434 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
436 gst_caps_unref (tmpcaps);
437 gst_encoding_profile_unref (profile);
439 /* Then with a specific category */
440 profile = gst_encoding_profile_find ("myponytarget", "pony", "herding");
441 fail_unless (profile != NULL);
442 tmpcaps = gst_caps_from_string ("animal/x-pony");
443 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
445 gst_caps_unref (tmpcaps);
446 gst_encoding_profile_unref (profile);
448 /* For my next trick, I will need the assistance of a GValue */
449 g_value_init (&strvalue, G_TYPE_STRING);
450 g_value_init (&objectvalue, GST_TYPE_ENCODING_PROFILE);
451 g_value_set_static_string (&strvalue, "myponytarget/pony");
452 fail_unless (g_value_transform (&strvalue, &objectvalue));
453 profile = (GstEncodingProfile *) g_value_dup_object (&objectvalue);
454 fail_if (profile == NULL);
455 g_value_unset (&strvalue);
456 g_value_unset (&objectvalue);
457 tmpcaps = gst_caps_from_string ("animal/x-pony");
458 CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
460 gst_caps_unref (tmpcaps);
461 gst_encoding_profile_unref (profile);
463 /* Let's go crazy for error detection */
464 fail_if (gst_encoding_profile_find ("myponytarget", "whales", NULL));
465 fail_if (gst_encoding_profile_find ("myponytarget", "whales", "herding"));
466 fail_if (gst_encoding_profile_find ("myponytarget", "", NULL));
467 fail_if (gst_encoding_profile_find ("", "pony", NULL));
472 GST_START_TEST (test_target_list)
478 /* Make sure we get our test category in the available categories */
479 categories = gst_encoding_list_available_categories ();
480 fail_if (categories == NULL);
481 fail_if (g_list_find_custom (categories, "herding",
482 (GCompareFunc) g_strcmp0) == NULL);
483 g_list_foreach (categories, (GFunc) g_free, NULL);
484 g_list_free (categories);
486 /* Try getting all available targets with a specified category */
487 targets = gst_encoding_list_all_targets ("herding");
488 fail_if (targets == NULL);
489 for (tmp = targets; tmp; tmp = tmp->next) {
490 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
491 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
494 /* If tmp is NULL, it means we iterated the whole list without finding
496 fail_if (tmp == NULL);
497 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
498 g_list_free (targets);
500 /* Try getting all available targets without a specified category */
501 targets = gst_encoding_list_all_targets (NULL);
502 fail_if (targets == NULL);
503 for (tmp = targets; tmp; tmp = tmp->next) {
504 GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
505 if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
508 /* If tmp is NULL, it means we iterated the whole list without finding
510 fail_if (tmp == NULL);
511 g_list_foreach (targets, (GFunc) g_object_unref, NULL);
512 g_list_free (targets);
518 static const gchar *profile_string = "\
519 [GStreamer Encoding Target]\n\
522 description=Plenty of pony glitter profiles\n\
527 description=I don't want a description !\n\
528 format=animal/x-pony\n\
530 [streamprofile-pony11]\n\
533 format=audio/x-pony-song,pretty=True\n\
534 restriction=audio/x-raw,channels=1,rate=44100\n\
537 [streamprofile-pony12]\n\
540 preset=seriously glittery\n\
541 format=video/x-glitter,sparkling=True\n\
542 restriction=video/x-raw,width=640,height=480,framerate=15/1\n\
544 variableframerate=true\n\
548 remove_profile_file (void)
550 gchar *profile_file_name;
553 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
554 "encoding-profiles", "herding", "myponytarget.gep", NULL);
555 g_unlink (profile_file_name);
556 g_free (profile_file_name);
558 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
559 "encoding-profiles", "herding", "myponytarget2.gep", NULL);
560 g_unlink (profile_file_name);
561 g_free (profile_file_name);
565 create_profile_file (void)
567 gchar *profile_file_name;
569 GError *error = NULL;
572 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
573 "encoding-profiles", "herding", NULL);
575 g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
576 "encoding-profiles", "herding", "myponytarget.gep", NULL);
577 g_mkdir_with_parents (profile_dir, S_IRUSR | S_IWUSR | S_IXUSR);
578 if (!g_file_set_contents (profile_file_name, profile_string,
579 strlen (profile_string), &error))
580 GST_WARNING ("Couldn't write contents to file : %s", error->message);
581 g_free (profile_dir);
582 g_free (profile_file_name);
588 create_profile_file ();
594 remove_profile_file ();
601 Suite *s = suite_create ("profile support library");
602 TCase *tc_chain = tcase_create ("general");
606 /* cehck if we can create profiles */
607 gst_dir = g_build_filename (g_get_user_data_dir (), "gstreamer-1.0", NULL);
608 can_write = (g_access (gst_dir, R_OK | W_OK | X_OK) == 0);
611 suite_add_tcase (s, tc_chain);
613 tcase_add_test (tc_chain, test_profile_creation);
614 tcase_add_test (tc_chain, test_profile_input_caps);
615 tcase_add_test (tc_chain, test_target_naming);
616 tcase_add_test (tc_chain, test_target_profile);
618 tcase_add_test (tc_chain, test_loading_profile);
619 tcase_add_test (tc_chain, test_saving_profile);
620 tcase_add_test (tc_chain, test_target_list);
623 tcase_add_unchecked_fixture (tc_chain, test_setup, test_teardown);
628 GST_CHECK_MAIN (profile);