1 /* GStreamer unit test for gstprofile
2  *
3  * Copyright (C) <2009> Edward Hervey <edward.hervey@collabora.co.uk>
4  *
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.
9  *
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.
14  *
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <glib.h>
26 #include <glib/gstdio.h>
27 #include <gst/check/gstcheck.h>
28 
29 #include <gst/pbutils/encoding-profile.h>
30 #include <gst/pbutils/encoding-target.h>
31 
32 #ifdef G_OS_UNIX
33 #include <unistd.h>             /* For R_OK etc. */
34 #endif
35 
36 static inline gboolean
gst_caps_is_equal_unref(GstCaps * caps1,GstCaps * caps2)37 gst_caps_is_equal_unref (GstCaps * caps1, GstCaps * caps2)
38 {
39   gboolean ret;
40 
41   ret = gst_caps_is_equal (caps1, caps2);
42   gst_caps_unref (caps1);
43 
44   return ret;
45 }
46 
47 #define CHECK_PROFILE(profile, name, description, format, preset, presence, restriction) \
48   {									\
49   fail_if(profile == NULL);						\
50   fail_unless_equals_string (gst_encoding_profile_get_name (profile), name); \
51   fail_unless_equals_string (gst_encoding_profile_get_description (profile), description); \
52   fail_unless (gst_caps_is_equal_unref (gst_encoding_profile_get_format (profile), format)); \
53   fail_unless_equals_string (gst_encoding_profile_get_preset (profile), preset); \
54   fail_unless_equals_int (gst_encoding_profile_get_presence (profile), presence); \
55   if (restriction) \
56     fail_unless (gst_caps_is_equal_unref (gst_encoding_profile_get_restriction (profile), restriction)); \
57   }
58 
GST_START_TEST(test_profile_creation)59 GST_START_TEST (test_profile_creation)
60 {
61   GstEncodingProfile *encprof;
62   GstEncodingAudioProfile *audioprof;
63   GstEncodingVideoProfile *videoprof;
64   GstCaps *ogg, *vorbis, *theora;
65   GstCaps *test1, *test2;
66 
67   ogg = gst_caps_new_empty_simple ("application/ogg");
68   vorbis = gst_caps_new_empty_simple ("audio/x-vorbis");
69   theora = gst_caps_new_empty_simple ("video/x-theora");
70 
71   encprof = (GstEncodingProfile *) gst_encoding_container_profile_new ((gchar *)
72       "ogg-theora-vorbis", "dumb-profile", ogg, (gchar *) "dumb-preset");
73   CHECK_PROFILE (encprof, "ogg-theora-vorbis", "dumb-profile", ogg,
74       "dumb-preset", 0, NULL);
75 
76   audioprof = gst_encoding_audio_profile_new (vorbis, (gchar *) "HQ", NULL, 0);
77   CHECK_PROFILE ((GstEncodingProfile *) audioprof, NULL, NULL, vorbis, "HQ", 0,
78       NULL);
79 
80   videoprof = gst_encoding_video_profile_new (theora, (gchar *) "HQ", NULL, 0);
81   CHECK_PROFILE ((GstEncodingProfile *) videoprof, NULL, NULL, theora, "HQ",
82       0, NULL);
83 
84   fail_unless (gst_encoding_container_profile_add_profile (
85           (GstEncodingContainerProfile *) encprof,
86           (GstEncodingProfile *) audioprof));
87   fail_unless (gst_encoding_container_profile_add_profile (
88           (GstEncodingContainerProfile *) encprof,
89           (GstEncodingProfile *) videoprof));
90 
91   /* Test caps */
92   test1 = gst_caps_from_string ("video/x-theora; audio/x-vorbis");
93   test2 = gst_encoding_profile_get_input_caps (encprof);
94   fail_unless (gst_caps_is_equal (test1, test2));
95   gst_caps_unref (test1);
96   gst_caps_unref (test2);
97 
98   gst_encoding_profile_unref (encprof);
99   gst_caps_unref (ogg);
100   gst_caps_unref (theora);
101   gst_caps_unref (vorbis);
102 }
103 
104 GST_END_TEST;
105 
106 
GST_START_TEST(test_profile_input_caps)107 GST_START_TEST (test_profile_input_caps)
108 {
109   GstEncodingProfile *sprof;
110   GstCaps *vorbis;
111   GstCaps *out, *restriction, *test1;
112 
113   vorbis = gst_caps_new_empty_simple ("audio/x-vorbis");
114 
115   /* Simple case, no restriction */
116   sprof = (GstEncodingProfile *)
117       gst_encoding_audio_profile_new (vorbis, NULL, NULL, 0);
118   fail_if (sprof == NULL);
119 
120   out = gst_encoding_profile_get_input_caps (sprof);
121   fail_if (out == NULL);
122   fail_unless (gst_caps_is_equal (out, vorbis));
123   gst_caps_unref (out);
124   gst_encoding_profile_unref (sprof);
125 
126   /* One simple restriction */
127   restriction = gst_caps_from_string ("audio/x-raw,channels=2,rate=44100");
128   test1 = gst_caps_from_string ("audio/x-vorbis,channels=2,rate=44100");
129   fail_if (restriction == NULL);
130 
131   sprof = (GstEncodingProfile *)
132       gst_encoding_audio_profile_new (vorbis, NULL, restriction, 0);
133   fail_if (sprof == NULL);
134 
135   out = gst_encoding_profile_get_input_caps (sprof);
136   fail_if (out == NULL);
137   GST_DEBUG ("got caps %" GST_PTR_FORMAT, out);
138   fail_unless (gst_caps_is_equal (out, test1));
139   gst_caps_unref (out);
140   gst_caps_unref (restriction);
141   gst_caps_unref (test1);
142   gst_encoding_profile_unref (sprof);
143 
144   gst_caps_unref (vorbis);
145 }
146 
147 GST_END_TEST;
148 
149 
GST_START_TEST(test_target_naming)150 GST_START_TEST (test_target_naming)
151 {
152   GstEncodingTarget *target;
153 
154   gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
155 
156   /* NULL values */
157   ASSERT_CRITICAL (target = gst_encoding_target_new (NULL, NULL, NULL, NULL));
158   fail_if (target != NULL);
159   ASSERT_CRITICAL (target =
160       gst_encoding_target_new ("donkey", NULL, NULL, NULL));
161   fail_if (target != NULL);
162   ASSERT_CRITICAL (target =
163       gst_encoding_target_new (NULL, "donkey", NULL, NULL));
164   fail_if (target != NULL);
165   ASSERT_CRITICAL (target =
166       gst_encoding_target_new (NULL, NULL, "donkey", NULL));
167   fail_if (target != NULL);
168 
169   /* Name and Category validation */
170 
171   /* empty non-NULL strings */
172   fail_if (gst_encoding_target_new ("", "valid", "description", NULL) != NULL);
173   fail_if (gst_encoding_target_new ("valid", "", "description", NULL) != NULL);
174 
175   /* don't start with a lower case ASCII character */
176   fail_if (gst_encoding_target_new ("A", "valid", "description", NULL) != NULL);
177   fail_if (gst_encoding_target_new ("3", "valid", "description", NULL) != NULL);
178   fail_if (gst_encoding_target_new ("-", "valid", "description", NULL) != NULL);
179   fail_if (gst_encoding_target_new ("!", "valid", "description", NULL) != NULL);
180   fail_if (gst_encoding_target_new (" ", "valid", "description", NULL) != NULL);
181   fail_if (gst_encoding_target_new ("valid", "A", "description", NULL) != NULL);
182   fail_if (gst_encoding_target_new ("valid", "3", "description", NULL) != NULL);
183   fail_if (gst_encoding_target_new ("valid", "-", "description", NULL) != NULL);
184   fail_if (gst_encoding_target_new ("valid", "!", "description", NULL) != NULL);
185   fail_if (gst_encoding_target_new ("valid", " ", "description", NULL) != NULL);
186 
187   /* Starting with anything else is valid */
188   target = gst_encoding_target_new ("a", "valid", "description", NULL);
189   fail_if (target == NULL);
190   gst_encoding_target_unref (target);
191   target = gst_encoding_target_new ("z", "valid", "description", NULL);
192   fail_if (target == NULL);
193   gst_encoding_target_unref (target);
194   target = gst_encoding_target_new ("valid", "a", "description", NULL);
195   fail_if (target == NULL);
196   gst_encoding_target_unref (target);
197   target = gst_encoding_target_new ("valid", "z", "description", NULL);
198   fail_if (target == NULL);
199   gst_encoding_target_unref (target);
200 
201   /* only inner valid characters are lower-case ASCII letters *OR* digits *OR* hyphens */
202   fail_if (gst_encoding_target_new ("aA", "valid", "description",
203           NULL) != NULL);
204   fail_if (gst_encoding_target_new ("a!", "valid", "description",
205           NULL) != NULL);
206   fail_if (gst_encoding_target_new ("space donkeys", "valid", "description",
207           NULL) != NULL);
208   fail_if (gst_encoding_target_new ("howaboutùnicode", "valid", "description",
209           NULL) != NULL);
210   fail_if (gst_encoding_target_new ("valid", "aA", "description",
211           NULL) != NULL);
212   fail_if (gst_encoding_target_new ("valid", "a!", "description",
213           NULL) != NULL);
214 
215   target =
216       gst_encoding_target_new ("donkey-4-ever", "valid", "description", NULL);
217   fail_if (target == NULL);
218   gst_encoding_target_unref (target);
219   target =
220       gst_encoding_target_new ("valid", "donkey-4-ever", "description", NULL);
221   fail_if (target == NULL);
222   gst_encoding_target_unref (target);
223 
224 }
225 
226 GST_END_TEST;
227 
228 static GstEncodingTarget *
create_saveload_target(const gchar * targetname)229 create_saveload_target (const gchar * targetname)
230 {
231   GstEncodingTarget *target;
232   GstEncodingProfile *profile, *sprof;
233   GstCaps *caps, *caps2;
234 
235   GST_DEBUG ("Creating target");
236 
237   target = gst_encoding_target_new (targetname, "herding",
238       "Plenty of pony glitter profiles", NULL);
239   caps = gst_caps_from_string ("animal/x-pony");
240   profile =
241       (GstEncodingProfile *) gst_encoding_container_profile_new ("pony",
242       "I don't want a description !", caps, NULL);
243   gst_caps_unref (caps);
244   gst_encoding_target_add_profile (target, profile);
245 
246   caps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
247   caps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
248   sprof =
249       (GstEncodingProfile *) gst_encoding_audio_profile_new (caps, NULL, caps2,
250       1);
251   gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
252       profile, sprof);
253   gst_caps_unref (caps);
254   gst_caps_unref (caps2);
255 
256   caps = gst_caps_from_string ("video/x-glitter,sparkling=True");
257   caps2 =
258       gst_caps_from_string ("video/x-raw,width=640,height=480,framerate=15/1");
259   sprof = (GstEncodingProfile *)
260       gst_encoding_video_profile_new (caps, "seriously glittery", caps2, 0);
261   gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
262       sprof, TRUE);
263   gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile *)
264       profile, sprof);
265   gst_caps_unref (caps);
266   gst_caps_unref (caps2);
267 
268   return target;
269 }
270 
GST_START_TEST(test_target_profile)271 GST_START_TEST (test_target_profile)
272 {
273   GstEncodingTarget *target;
274   GstEncodingProfile *prof;
275 
276   target = create_saveload_target ("myponytarget");
277 
278   /* NULL isn't a valid profile name */
279   ASSERT_CRITICAL (gst_encoding_target_get_profile (target, NULL));
280 
281   /* try finding a profile that doesn't exist */
282   fail_if (gst_encoding_target_get_profile (target,
283           "no-really-does-not-exist"));
284 
285   /* try finding a profile that exists */
286   prof = gst_encoding_target_get_profile (target, "pony");
287   fail_if (prof == NULL);
288 
289   gst_encoding_profile_unref (prof);
290   gst_encoding_target_unref (target);
291 }
292 
293 GST_END_TEST;
294 
GST_START_TEST(test_saving_profile)295 GST_START_TEST (test_saving_profile)
296 {
297   GstEncodingTarget *orig, *loaded = NULL;
298   GstEncodingProfile *proforig, *profloaded;
299   gchar *profile_file_name;
300 
301   /* Create and store a target */
302   orig = create_saveload_target ("myponytarget2");
303   GST_DEBUG ("Saving target 'myponytarget2'");
304   fail_unless (gst_encoding_target_save (orig, NULL));
305 
306   /* Check we can load it */
307   profile_file_name =
308       g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
309       "encoding-profiles", "herding", "myponytarget2.gep", NULL);
310   GST_DEBUG ("Loading target from '%s'", profile_file_name);
311   loaded = gst_encoding_target_load_from_file (profile_file_name, NULL);
312   fail_unless (loaded != NULL);
313   g_free (profile_file_name);
314 
315   GST_DEBUG ("Checking targets are equal");
316   /* Check targets are identical */
317   /* 1. at the target level */
318   fail_unless_equals_string (gst_encoding_target_get_name (orig),
319       gst_encoding_target_get_name (loaded));
320   fail_unless_equals_string (gst_encoding_target_get_category (orig),
321       gst_encoding_target_get_category (loaded));
322   fail_unless_equals_string (gst_encoding_target_get_description (orig),
323       gst_encoding_target_get_description (loaded));
324   fail_unless_equals_int (g_list_length ((GList *)
325           gst_encoding_target_get_profiles (loaded)), 1);
326 
327   /* 2. at the profile level */
328   profloaded =
329       (GstEncodingProfile *) gst_encoding_target_get_profiles (loaded)->data;
330   proforig =
331       (GstEncodingProfile *) gst_encoding_target_get_profiles (orig)->data;
332 
333   fail_unless_equals_int (G_TYPE_FROM_INSTANCE (profloaded),
334       G_TYPE_FROM_INSTANCE (proforig));
335   GST_DEBUG ("Comparing loaded:%p to original:%p", profloaded, proforig);
336   fail_unless (gst_encoding_profile_is_equal (profloaded, proforig));
337 
338   gst_encoding_target_unref (orig);
339   gst_encoding_target_unref (loaded);
340 }
341 
342 GST_END_TEST;
343 
344 static void
test_individual_target(GstEncodingTarget * target)345 test_individual_target (GstEncodingTarget * target)
346 {
347   GstEncodingProfile *prof;
348   GstCaps *tmpcaps, *tmpcaps2;
349   GstEncodingProfile *sprof1, *sprof2;
350 
351   GST_DEBUG ("Checking the target properties");
352   /* Check the target  */
353   fail_unless_equals_string (gst_encoding_target_get_name (target),
354       "myponytarget");
355   fail_unless_equals_string (gst_encoding_target_get_category (target),
356       "herding");
357   fail_unless_equals_string (gst_encoding_target_get_description (target),
358       "Plenty of pony glitter profiles");
359 
360   GST_DEBUG ("Checking the number of profiles the target contains");
361   fail_unless_equals_int (g_list_length ((GList *)
362           gst_encoding_target_get_profiles (target)), 1);
363 
364 
365   GST_DEBUG ("Checking the container profile");
366   /* Check the profile */
367   prof = (GstEncodingProfile *) gst_encoding_target_get_profiles (target)->data;
368   tmpcaps = gst_caps_from_string ("animal/x-pony");
369   CHECK_PROFILE (prof, "pony", "I don't want a description !", tmpcaps, NULL, 0,
370       0);
371   gst_caps_unref (tmpcaps);
372 
373   GST_DEBUG ("Checking the container profile has 2 stream profiles");
374   /* Check the stream profiles */
375   fail_unless_equals_int (g_list_length ((GList *)
376           gst_encoding_container_profile_get_profiles (
377               (GstEncodingContainerProfile *) prof)), 2);
378 
379   GST_DEBUG ("Checking the container profile has the audio/x-pony-song stream");
380   tmpcaps = gst_caps_from_string ("audio/x-pony-song,pretty=True");
381   tmpcaps2 = gst_caps_from_string ("audio/x-raw,channels=1,rate=44100");
382   sprof1 =
383       (GstEncodingProfile *) gst_encoding_audio_profile_new (tmpcaps, NULL,
384       tmpcaps2, 1);
385   fail_unless (gst_encoding_container_profile_contains_profile (
386           (GstEncodingContainerProfile *) prof, sprof1));
387   gst_encoding_profile_unref (sprof1);
388   gst_caps_unref (tmpcaps);
389   gst_caps_unref (tmpcaps2);
390 
391   GST_DEBUG ("Checking the container profile has the video//x-glitter stream");
392   tmpcaps = gst_caps_from_string ("video/x-glitter,sparkling=True");
393   tmpcaps2 =
394       gst_caps_from_string ("video/x-raw,width=640,height=480,framerate=15/1");
395   sprof2 = (GstEncodingProfile *)
396       gst_encoding_video_profile_new (tmpcaps, "seriously glittery", tmpcaps2,
397       0);
398   gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile *)
399       sprof2, TRUE);
400   fail_unless (gst_encoding_container_profile_contains_profile (
401           (GstEncodingContainerProfile *) prof, sprof2));
402   gst_encoding_profile_unref (sprof2);
403   gst_caps_unref (tmpcaps);
404   gst_caps_unref (tmpcaps2);
405 }
406 
GST_START_TEST(test_loading_profile)407 GST_START_TEST (test_loading_profile)
408 {
409   GstEncodingTarget *target;
410   gchar *profile_file_name;
411   GstEncodingProfile *profile;
412   GstCaps *tmpcaps;
413   GValue strvalue = { 0, };
414   GValue objectvalue = { 0, };
415 
416   gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
417 
418   /* Test loading using short method and all arguments */
419   target = gst_encoding_target_load ("myponytarget", "herding", NULL);
420   fail_unless (target != NULL);
421   test_individual_target (target);
422   gst_encoding_target_unref (target);
423 
424   /* Test loading using short method and no category */
425   target = gst_encoding_target_load ("myponytarget", NULL, NULL);
426   fail_unless (target != NULL);
427   test_individual_target (target);
428   gst_encoding_target_unref (target);
429 
430   /* Test loading using fully specified path */
431   profile_file_name =
432       g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
433       "encoding-profiles", "herding", "myponytarget.gep", NULL);
434 
435   GST_DEBUG ("Loading target from '%s'", profile_file_name);
436   target = gst_encoding_target_load_from_file (profile_file_name, NULL);
437   g_free (profile_file_name);
438   fail_unless (target != NULL);
439   test_individual_target (target);
440   gst_encoding_target_unref (target);
441 
442   /* Test getting the profiles directly
443    * First without category */
444   profile = gst_encoding_profile_find ("myponytarget", "pony", NULL);
445   fail_unless (profile != NULL);
446   tmpcaps = gst_caps_from_string ("animal/x-pony");
447   CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
448       0, 0);
449   gst_caps_unref (tmpcaps);
450   gst_encoding_profile_unref (profile);
451 
452   /* Then with a specific category */
453   profile = gst_encoding_profile_find ("myponytarget", "pony", "herding");
454   fail_unless (profile != NULL);
455   tmpcaps = gst_caps_from_string ("animal/x-pony");
456   CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
457       0, 0);
458   gst_caps_unref (tmpcaps);
459   gst_encoding_profile_unref (profile);
460 
461   /* For my next trick, I will need the assistance of a GValue */
462   g_value_init (&strvalue, G_TYPE_STRING);
463   g_value_init (&objectvalue, GST_TYPE_ENCODING_PROFILE);
464   g_value_set_static_string (&strvalue, "myponytarget/pony");
465   fail_unless (g_value_transform (&strvalue, &objectvalue));
466   profile = (GstEncodingProfile *) g_value_dup_object (&objectvalue);
467   fail_if (profile == NULL);
468   g_value_unset (&strvalue);
469   g_value_unset (&objectvalue);
470   tmpcaps = gst_caps_from_string ("animal/x-pony");
471   CHECK_PROFILE (profile, "pony", "I don't want a description !", tmpcaps, NULL,
472       0, 0);
473   gst_caps_unref (tmpcaps);
474   gst_encoding_profile_unref (profile);
475 
476   /* Let's go crazy for error detection */
477   fail_if (gst_encoding_profile_find ("myponytarget", "whales", NULL));
478   fail_if (gst_encoding_profile_find ("myponytarget", "whales", "herding"));
479   fail_if (gst_encoding_profile_find ("myponytarget", "", NULL));
480   fail_if (gst_encoding_profile_find ("", "pony", NULL));
481 }
482 
483 GST_END_TEST;
484 
GST_START_TEST(test_target_list)485 GST_START_TEST (test_target_list)
486 {
487   GList *categories;
488   GList *targets;
489   GList *tmp;
490 
491   /* Make sure we get our test category in the available categories */
492   categories = gst_encoding_list_available_categories ();
493   fail_if (categories == NULL);
494   fail_if (g_list_find_custom (categories, "herding",
495           (GCompareFunc) g_strcmp0) == NULL);
496   g_list_foreach (categories, (GFunc) g_free, NULL);
497   g_list_free (categories);
498 
499   /* Try getting all available targets with a specified category */
500   targets = gst_encoding_list_all_targets ("herding");
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"))
505       break;
506   }
507   /* If tmp is NULL, it means we iterated the whole list without finding
508    * our target */
509   fail_if (tmp == NULL);
510   g_list_foreach (targets, (GFunc) g_object_unref, NULL);
511   g_list_free (targets);
512 
513   /* Try getting all available targets without a specified category */
514   targets = gst_encoding_list_all_targets (NULL);
515   fail_if (targets == NULL);
516   for (tmp = targets; tmp; tmp = tmp->next) {
517     GstEncodingTarget *target = (GstEncodingTarget *) tmp->data;
518     if (!g_strcmp0 (gst_encoding_target_get_name (target), "myponytarget"))
519       break;
520   }
521   /* If tmp is NULL, it means we iterated the whole list without finding
522    * our target */
523   fail_if (tmp == NULL);
524   g_list_foreach (targets, (GFunc) g_object_unref, NULL);
525   g_list_free (targets);
526 }
527 
528 GST_END_TEST;
529 
530 
531 static const gchar *profile_string = "\
532 [GStreamer Encoding Target]\n\
533 name=myponytarget\n\
534 category=herding\n\
535 description=Plenty of pony glitter profiles\n\
536 \n\
537 [profile-pony1]\n\
538 name=pony\n\
539 type=container\n\
540 description=I don't want a description !\n\
541 format=animal/x-pony\n\
542 \n\
543 [streamprofile-pony11]\n\
544 parent=pony\n\
545 type=audio\n\
546 format=audio/x-pony-song,pretty=True\n\
547 restriction=audio/x-raw,channels=1,rate=44100\n\
548 presence=1\n\
549 \n\
550 [streamprofile-pony12]\n\
551 parent=pony\n\
552 type=video\n\
553 preset=seriously glittery\n\
554 format=video/x-glitter,sparkling=True\n\
555 restriction=video/x-raw,width=640,height=480,framerate=15/1\n\
556 presence=0\n\
557 variableframerate=true\n\
558 ";
559 
560 static void
remove_profile_file(void)561 remove_profile_file (void)
562 {
563   gchar *profile_file_name;
564 
565   profile_file_name =
566       g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
567       "encoding-profiles", "herding", "myponytarget.gep", NULL);
568   g_unlink (profile_file_name);
569   g_free (profile_file_name);
570   profile_file_name =
571       g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
572       "encoding-profiles", "herding", "myponytarget2.gep", NULL);
573   g_unlink (profile_file_name);
574   g_free (profile_file_name);
575 }
576 
577 static void
create_profile_file(void)578 create_profile_file (void)
579 {
580   gchar *profile_file_name;
581   gchar *profile_dir;
582   GError *error = NULL;
583 
584   profile_dir =
585       g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
586       "encoding-profiles", "herding", NULL);
587   profile_file_name =
588       g_build_filename (g_get_user_data_dir (), "gstreamer-1.0",
589       "encoding-profiles", "herding", "myponytarget.gep", NULL);
590 
591   /* on Windows it will ignore the mode anyway */
592 #ifdef G_OS_WIN32
593   g_mkdir_with_parents (profile_dir, 0700);
594 #else
595   g_mkdir_with_parents (profile_dir, S_IRUSR | S_IWUSR | S_IXUSR);
596 #endif
597 
598   if (!g_file_set_contents (profile_file_name, profile_string,
599           strlen (profile_string), &error))
600     GST_WARNING ("Couldn't write contents to file : %s", error->message);
601   g_free (profile_dir);
602   g_free (profile_file_name);
603 }
604 
605 static void
test_setup(void)606 test_setup (void)
607 {
608   create_profile_file ();
609 }
610 
611 static void
test_teardown(void)612 test_teardown (void)
613 {
614   remove_profile_file ();
615 }
616 
GST_START_TEST(test_file_extension)617 GST_START_TEST (test_file_extension)
618 {
619   GstEncodingContainerProfile *cprof;
620   GstCaps *ogg, *speex, *vorbis, *theora, *id3, *mp3;
621 
622   /* 1 - ogg variants */
623   ogg = gst_caps_new_empty_simple ("application/ogg");
624   cprof = gst_encoding_container_profile_new ("myprofile", NULL, ogg, NULL);
625   gst_caps_unref (ogg);
626 
627   fail_unless_equals_string (gst_encoding_profile_get_file_extension
628       (GST_ENCODING_PROFILE (cprof)), "ogg");
629 
630   speex = gst_caps_new_empty_simple ("audio/x-speex");
631   gst_encoding_container_profile_add_profile (cprof,
632       (GstEncodingProfile *) gst_encoding_audio_profile_new (speex, NULL,
633           NULL, 1));
634   gst_caps_unref (speex);
635 
636   fail_unless_equals_string (gst_encoding_profile_get_file_extension
637       (GST_ENCODING_PROFILE (cprof)), "spx");
638 
639   vorbis = gst_caps_new_empty_simple ("audio/x-vorbis");
640   gst_encoding_container_profile_add_profile (cprof,
641       (GstEncodingProfile *) gst_encoding_audio_profile_new (vorbis, NULL,
642           NULL, 1));
643   gst_caps_unref (vorbis);
644 
645   fail_unless_equals_string (gst_encoding_profile_get_file_extension
646       (GST_ENCODING_PROFILE (cprof)), "ogg");
647 
648   theora = gst_caps_new_empty_simple ("video/x-theora");
649   gst_encoding_container_profile_add_profile (cprof,
650       (GstEncodingProfile *) gst_encoding_video_profile_new (theora, NULL,
651           NULL, 1));
652   gst_caps_unref (theora);
653 
654   fail_unless_equals_string (gst_encoding_profile_get_file_extension
655       (GST_ENCODING_PROFILE (cprof)), "ogv");
656 
657   gst_encoding_profile_unref (cprof);
658 
659   /* 2 - tag container */
660   id3 = gst_caps_new_empty_simple ("application/x-id3");
661   cprof = gst_encoding_container_profile_new ("myprofile", NULL, id3, NULL);
662   gst_caps_unref (id3);
663 
664   fail_unless (gst_encoding_profile_get_file_extension (GST_ENCODING_PROFILE
665           (cprof)) == NULL);
666 
667   mp3 = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
668       "layer", G_TYPE_INT, 3, NULL);
669   gst_encoding_container_profile_add_profile (cprof,
670       (GstEncodingProfile *) gst_encoding_audio_profile_new (mp3, NULL,
671           NULL, 1));
672   gst_caps_unref (mp3);
673 
674   fail_unless_equals_string (gst_encoding_profile_get_file_extension
675       (GST_ENCODING_PROFILE (cprof)), "mp3");
676 
677   gst_encoding_profile_unref (cprof);
678 }
679 
680 GST_END_TEST;
681 
682 static Suite *
profile_suite(void)683 profile_suite (void)
684 {
685   Suite *s = suite_create ("profile support library");
686   TCase *tc_chain = tcase_create ("general");
687   gboolean can_write;
688 
689   /* check if we can create profiles */
690 #ifdef G_OS_UNIX
691   {
692     gchar *gst_dir =
693         g_build_filename (g_get_user_data_dir (), "gstreamer-1.0", NULL);
694     can_write = (g_access (gst_dir, R_OK | W_OK | X_OK) == 0);
695     g_free (gst_dir);
696   }
697 #else
698   can_write = FALSE;            /* FIXME: fix can_write test on Windows */
699 #endif
700 
701   suite_add_tcase (s, tc_chain);
702 
703   tcase_add_test (tc_chain, test_profile_creation);
704   tcase_add_test (tc_chain, test_profile_input_caps);
705   tcase_add_test (tc_chain, test_target_naming);
706   tcase_add_test (tc_chain, test_target_profile);
707   tcase_add_test (tc_chain, test_file_extension);
708   if (can_write) {
709     tcase_add_test (tc_chain, test_loading_profile);
710     tcase_add_test (tc_chain, test_saving_profile);
711     tcase_add_test (tc_chain, test_target_list);
712   }
713 
714   tcase_add_unchecked_fixture (tc_chain, test_setup, test_teardown);
715 
716   return s;
717 }
718 
719 GST_CHECK_MAIN (profile);
720