1 /* GStreamer unit tests for libgstpbutils
2  *
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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 <gst/check/gstcheck.h>
26 #include <gst/pbutils/pbutils.h>
27 #include <gst/base/gstbitwriter.h>
28 
29 #include <stdio.h>
30 #include <glib/gstdio.h>
31 #include <glib/gprintf.h>
32 
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>          /* for chmod() and getpid () */
35 #endif
36 
37 #ifdef HAVE_SYS_STAT_H
38 #include <sys/stat.h>           /* for chmod() */
39 #endif
40 
41 #ifdef G_OS_UNIX
42 #include <unistd.h>             /* for getpid() */
43 #endif
44 
45 static void
missing_msg_check_getters(GstMessage * msg)46 missing_msg_check_getters (GstMessage * msg)
47 {
48   gchar *str;
49 
50   str = gst_missing_plugin_message_get_installer_detail (msg);
51   fail_unless (str != NULL);
52   fail_unless (*str != '\0');
53   fail_unless (g_str_has_prefix (str, "gstreamer|"));
54   g_free (str);
55 
56   str = gst_missing_plugin_message_get_description (msg);
57   fail_unless (str != NULL);
58   fail_unless (*str != '\0');
59   g_free (str);
60 }
61 
GST_START_TEST(test_pb_utils_post_missing_messages)62 GST_START_TEST (test_pb_utils_post_missing_messages)
63 {
64   const GstStructure *s;
65   GstElement *pipeline;
66   GstMessage *msg;
67   GstCaps *caps;
68   GstBus *bus;
69 
70   gst_pb_utils_init ();
71 
72   pipeline = gst_pipeline_new ("pipeline");
73   bus = gst_element_get_bus (pipeline);
74 
75   /* first, test common assertion failure cases */
76   ASSERT_CRITICAL (msg = gst_missing_uri_source_message_new (NULL, "http"));
77   ASSERT_CRITICAL (gst_missing_uri_source_message_new (pipeline, NULL));
78 
79   ASSERT_CRITICAL (gst_missing_uri_sink_message_new (NULL, "http"));
80   ASSERT_CRITICAL (gst_missing_uri_sink_message_new (pipeline, NULL));
81 
82   ASSERT_CRITICAL (gst_missing_element_message_new (NULL, "rgbfyltr"));
83   ASSERT_CRITICAL (gst_missing_element_message_new (pipeline, NULL));
84 
85   caps = gst_caps_new_empty_simple ("audio/x-dontexist");
86 
87   ASSERT_CRITICAL (gst_missing_decoder_message_new (NULL, caps));
88   ASSERT_CRITICAL (gst_missing_decoder_message_new (pipeline, NULL));
89 
90   ASSERT_CRITICAL (gst_missing_encoder_message_new (NULL, caps));
91   ASSERT_CRITICAL (gst_missing_encoder_message_new (pipeline, NULL));
92 
93   gst_caps_unref (caps);
94 
95   /* URI source (with existing protocol) */
96   msg = gst_missing_uri_source_message_new (pipeline, "http");
97   fail_unless (msg != NULL);
98   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
99   fail_unless (gst_message_get_structure (msg) != NULL);
100   s = gst_message_get_structure (msg);
101   fail_unless (gst_structure_has_name (s, "missing-plugin"));
102   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
103   fail_unless_equals_string (gst_structure_get_string (s, "type"), "urisource");
104   fail_unless (gst_structure_has_field_typed (s, "detail", G_TYPE_STRING));
105   fail_unless_equals_string (gst_structure_get_string (s, "detail"), "http");
106   missing_msg_check_getters (msg);
107   gst_message_unref (msg);
108 
109   /* URI sink (with existing protocol) */
110   msg = gst_missing_uri_sink_message_new (pipeline, "smb");
111   fail_unless (msg != NULL);
112   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
113   fail_unless (gst_message_get_structure (msg) != NULL);
114   s = gst_message_get_structure (msg);
115   fail_unless (gst_structure_has_name (s, "missing-plugin"));
116   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
117   fail_unless_equals_string (gst_structure_get_string (s, "type"), "urisink");
118   fail_unless (gst_structure_has_field_typed (s, "detail", G_TYPE_STRING));
119   fail_unless_equals_string (gst_structure_get_string (s, "detail"), "smb");
120   missing_msg_check_getters (msg);
121   gst_message_unref (msg);
122 
123   /* URI source (with bogus protocol) */
124   msg = gst_missing_uri_source_message_new (pipeline, "chchck");
125   fail_unless (msg != NULL);
126   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
127   fail_unless (gst_message_get_structure (msg) != NULL);
128   s = gst_message_get_structure (msg);
129   fail_unless (gst_structure_has_name (s, "missing-plugin"));
130   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
131   fail_unless_equals_string (gst_structure_get_string (s, "type"), "urisource");
132   fail_unless (gst_structure_has_field_typed (s, "detail", G_TYPE_STRING));
133   fail_unless_equals_string (gst_structure_get_string (s, "detail"), "chchck");
134   missing_msg_check_getters (msg);
135   gst_message_unref (msg);
136 
137   /* URI sink (with bogus protocol) */
138   msg = gst_missing_uri_sink_message_new (pipeline, "chchck");
139   fail_unless (msg != NULL);
140   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
141   fail_unless (gst_message_get_structure (msg) != NULL);
142   s = gst_message_get_structure (msg);
143   fail_unless (gst_structure_has_name (s, "missing-plugin"));
144   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
145   fail_unless_equals_string (gst_structure_get_string (s, "type"), "urisink");
146   fail_unless (gst_structure_has_field_typed (s, "detail", G_TYPE_STRING));
147   fail_unless_equals_string (gst_structure_get_string (s, "detail"), "chchck");
148   missing_msg_check_getters (msg);
149   gst_message_unref (msg);
150 
151   /* element */
152   msg = gst_missing_element_message_new (pipeline, "foobar");
153   fail_unless (msg != NULL);
154   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
155   fail_unless (gst_message_get_structure (msg) != NULL);
156   s = gst_message_get_structure (msg);
157   fail_unless (gst_structure_has_name (s, "missing-plugin"));
158   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
159   fail_unless_equals_string (gst_structure_get_string (s, "type"), "element");
160   fail_unless (gst_structure_has_field_typed (s, "detail", G_TYPE_STRING));
161   fail_unless_equals_string (gst_structure_get_string (s, "detail"), "foobar");
162   missing_msg_check_getters (msg);
163   gst_message_unref (msg);
164 
165   /* create bogus caps that don't exist */
166   caps = gst_caps_new_simple ("do/x-not", "exist", G_TYPE_BOOLEAN, FALSE, NULL);
167 
168   /* decoder (with unknown caps) */
169   msg = gst_missing_decoder_message_new (pipeline, caps);
170   fail_unless (msg != NULL);
171   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
172   fail_unless (gst_message_get_structure (msg) != NULL);
173   s = gst_message_get_structure (msg);
174   fail_unless (gst_structure_has_name (s, "missing-plugin"));
175   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
176   fail_unless_equals_string (gst_structure_get_string (s, "type"), "decoder");
177   fail_unless (gst_structure_has_field_typed (s, "detail", GST_TYPE_CAPS));
178   missing_msg_check_getters (msg);
179   gst_message_unref (msg);
180 
181   /* encoder (with unknown caps) */
182   msg = gst_missing_encoder_message_new (pipeline, caps);
183   fail_unless (msg != NULL);
184   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
185   fail_unless (gst_message_get_structure (msg) != NULL);
186   s = gst_message_get_structure (msg);
187   fail_unless (gst_structure_has_name (s, "missing-plugin"));
188   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
189   fail_unless_equals_string (gst_structure_get_string (s, "type"), "encoder");
190   fail_unless (gst_structure_has_field_typed (s, "detail", GST_TYPE_CAPS));
191   missing_msg_check_getters (msg);
192   gst_message_unref (msg);
193 
194   gst_caps_unref (caps);
195 
196   /* create caps that exist */
197   caps = gst_caps_new_empty_simple ("video/x-matroska");
198   /* decoder (with known caps) */
199   msg = gst_missing_decoder_message_new (pipeline, caps);
200   fail_unless (msg != NULL);
201   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
202   fail_unless (gst_message_get_structure (msg) != NULL);
203   s = gst_message_get_structure (msg);
204   fail_unless (gst_structure_has_name (s, "missing-plugin"));
205   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
206   fail_unless_equals_string (gst_structure_get_string (s, "type"), "decoder");
207   fail_unless (gst_structure_has_field_typed (s, "detail", GST_TYPE_CAPS));
208   fail_unless (gst_structure_has_field_typed (s, "name", G_TYPE_STRING));
209   fail_unless (gst_structure_get_string (s, "name") != NULL);
210   missing_msg_check_getters (msg);
211   gst_message_unref (msg);
212 
213   /* encoder (with known caps) */
214   msg = gst_missing_encoder_message_new (pipeline, caps);
215   fail_unless (msg != NULL);
216   fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
217   fail_unless (gst_message_get_structure (msg) != NULL);
218   s = gst_message_get_structure (msg);
219   fail_unless (gst_structure_has_name (s, "missing-plugin"));
220   fail_unless (gst_structure_has_field_typed (s, "type", G_TYPE_STRING));
221   fail_unless_equals_string (gst_structure_get_string (s, "type"), "encoder");
222   fail_unless (gst_structure_has_field_typed (s, "detail", GST_TYPE_CAPS));
223   fail_unless (gst_structure_has_field_typed (s, "name", G_TYPE_STRING));
224   fail_unless (gst_structure_get_string (s, "name") != NULL);
225   missing_msg_check_getters (msg);
226   gst_message_unref (msg);
227 
228   gst_caps_unref (caps);
229 
230   gst_element_set_state (pipeline, GST_STATE_NULL);
231   gst_object_unref (pipeline);
232   gst_object_unref (bus);
233 }
234 
235 GST_END_TEST;
236 
GST_START_TEST(test_pb_utils_init)237 GST_START_TEST (test_pb_utils_init)
238 {
239   /* should be fine to call multiple times */
240   gst_pb_utils_init ();
241   gst_pb_utils_init ();
242   gst_pb_utils_init ();
243   gst_pb_utils_init ();
244 }
245 
246 GST_END_TEST;
247 
248 static const gchar *caps_strings[] = {
249   /* formats with static descriptions */
250   "application/ogg", "application/vnd.rn-realmedia", "video/x-fli",
251   "video/x-flv", "video/x-matroska", "video/x-ms-asf", "video/x-msvideo",
252   "video/x-quicktime", "video/quicktime", "audio/x-ac3", "audio/ac3",
253   "audio/x-private-ac3", "audio/x-private1-ac3", "audio/x-adpcm",
254   "audio/aiff", "audio/x-alaw", "audio/amr", "audio/AMR", "audio/AMR-WB",
255   "audio/iLBC-sh", "audio/ms-gsm", "audio/qcelp", "audio/x-adpcm",
256   "audio/x-aiff", "audio/x-alac", "audio/x-amr-nb-sh", "audio/x-amr-wb-sh",
257   "audio/x-au", "audio/x-cinepak", "audio/x-dpcm", "audio/x-dts",
258   "audio/x-dv", "audio/x-flac", "audio/x-gsm", "audio/x-iec958",
259   "audio/x-iLBC", "audio/x-ircam", "audio/x-lpcm", "audio/x-private1-lpcm",
260   "audio/x-m4a", "audio/x-mod", "audio/x-mulaw", "audio/x-musepack",
261   "audio/x-nist", "audio/x-nsf", "audio/x-paris", "audio/x-qdm2",
262   "audio/x-ralf-mpeg4-generic", "audio/x-sds", "audio/x-shorten",
263   "audio/x-sid", "audio/x-sipro", "audio/x-spc", "audio/x-speex",
264   "audio/x-svx", "audio/x-tta", "audio/x-ttafile",
265   "audio/x-vnd.sony.atrac3", "audio/x-vorbis", "audio/x-voc", "audio/x-w64",
266   "audio/x-wav", "audio/x-wavpack", "audio/x-wavpack-correction",
267   "audio/x-wms", "audio/x-voxware", "audio/x-xi", "video/sp5x", "video/vivo",
268   "video/x-4xm", "video/x-apple-video", "video/x-camtasia",
269   "video/x-cdxa", "video/x-cinepak", "video/x-cirrus-logic-accupak",
270   "video/x-compressed-yuv", "subpicture/x-dvd",
271   "video/x-ffv", "video/x-flash-screen", "video/x-flash-video",
272   "video/x-h261", "video/x-huffyuv", "video/x-intel-h263", "video/x-jpeg",
273   "video/x-mjpeg", "video/x-mjpeg-b", "video/mpegts", "video/x-mng",
274   "video/x-mszh", "video/x-msvideocodec", "video/x-mve", "video/x-nut",
275   "video/x-nuv", "video/x-qdrw", "video/x-raw", "video/x-smc",
276   "video/x-smoke", "video/x-tarkin", "video/x-theora", "video/x-rle",
277   "video/x-ultimotion", "video/x-vcd", "video/x-vmnc", "video/x-vp3",
278   "video/x-vp5", "video/x-vp6", "video/x-vp6-flash", "video/x-vp7",
279   "video/x-zlib", "image/bmp", "image/x-bmp",
280   "image/x-MS-bmp", "image/gif", "image/jpeg", "image/jng", "image/png",
281   "image/pbm", "image/ppm", "image/svg+xml", "image/tiff",
282   "image/x-cmu-raster", "image/x-icon", "image/x-xcf", "image/x-pixmap",
283   "image/x-xpixmap", "image/x-quicktime", "image/x-sun-raster",
284   "image/x-tga", "video/x-dv", "video/x-dv",
285   /* some RTP formats */
286   "application/x-rtp, media=(string)video, encoding-name=(string)TimVCodec",
287   "application/x-rtp, media=(string)audio, encoding-name=(string)TimACodec",
288   "application/x-rtp, media=(string)application, encoding-name=(string)TimMux",
289   "application/x-rtp, media=(string)woohoo, encoding-name=(string)TPM",
290   /* incomplete RTP formats */
291   "application/x-rtp, media=(string)woohoo",
292   "application/x-rtp, encoding-name=(string)TPM",
293   "application/x-rtp, media=(string)woohoo",
294   /* formats with dynamic descriptions */
295   "audio/x-adpcm",
296   "audio/x-adpcm, layout=(string)dvi",
297   "audio/x-adpcm, layout=(string)swf",
298   "audio/x-adpcm, layout=(string)microsoft",
299   "audio/x-adpcm, layout=(string)quicktime",
300   "audio/mpeg, mpegversion=(int)4",
301   "audio/mpeg, mpegversion=(int)1, layer=(int)1",
302   "audio/mpeg, mpegversion=(int)1, layer=(int)2",
303   "audio/mpeg, mpegversion=(int)1, layer=(int)3",
304   "audio/mpeg, mpegversion=(int)1, layer=(int)99",
305   "audio/mpeg, mpegversion=(int)99",
306   "video/mpeg, mpegversion=(int)2, systemstream=(boolean)TRUE",
307   "video/mpeg, systemstream=(boolean)FALSE",
308   "video/mpeg, mpegversion=(int)2",
309   "video/mpeg, mpegversion=(int)1, systemstream=(boolean)FALSE",
310   "video/mpeg, mpegversion=(int)2, systemstream=(boolean)FALSE",
311   "video/mpeg, mpegversion=(int)4, systemstream=(boolean)FALSE",
312   "video/mpeg, mpegversion=(int)99, systemstream=(boolean)TRUE",
313   "video/mpeg, mpegversion=(int)99, systemstream=(boolean)FALSE",
314   "video/mpeg, mpegversion=(int)4, systemstream=(boolean)FALSE, profile=main",
315   "video/mpeg, mpegversion=(int)4, systemstream=(boolean)FALSE, profile=adsfad",
316   "video/mpeg",
317   "video/x-indeo, indeoversion=(int)3",
318   "video/x-indeo, indeoversion=(int)5",
319   "video/x-indeo",
320   "video/x-wmv, wmvversion=(int)1",
321   "video/x-wmv, wmvversion=(int)2",
322   "video/x-wmv, wmvversion=(int)3",
323   "video/x-wmv, wmvversion=(int)99",
324   "video/x-wmv",
325   "audio/x-wma, wmaversion=(int)1",
326   "audio/x-wma, wmaversion=(int)2",
327   "audio/x-wma, wmaversion=(int)3",
328   "audio/x-wma, wmaversion=(int)99",
329   "audio/x-wma",
330   "video/x-dirac",
331   "video/x-dirac, profile=(string)vc2-low-delay",
332   "video/x-dirac, profile=(string)vc2-simple",
333   "video/x-dirac, profile=(string)vc2-main",
334   "video/x-dirac, profile=(string)main",
335   "video/x-dirac, profile=(string)czvja",
336   "video/x-divx, divxversion=(int)3",
337   "video/x-divx, divxversion=(int)4",
338   "video/x-divx, divxversion=(int)5",
339   "video/x-divx, divxversion=(int)99",
340   "video/x-divx",
341   "video/x-svq, svqversion=(int)1",
342   "video/x-svq, svqversion=(int)3",
343   "video/x-svq, svqversion=(int)99",
344   "video/x-svq",
345   "video/x-h265, profile=(string)main",
346   "video/x-h265, profile=(string)xafasdf",
347   "video/x-h265",
348   "video/x-h264, variant=(string)itu",
349   "video/x-h264, variant=(string)videosoft",
350   "video/x-h264, variant=(string)foobar",
351   "video/x-h264",
352   "video/x-h264, profile=(string)foobar",
353   "video/x-h264, profile=(string)high-4:4:4-intra",
354   "video/x-h264, profile=(string)high",
355   "video/x-h263, variant=(string)itu",
356   "video/x-h263, variant=(string)lead",
357   "video/x-h263, variant=(string)microsoft",
358   "video/x-h263, variant=(string)vdolive",
359   "video/x-h263, variant=(string)vivo",
360   "video/x-h263, variant=(string)xirlink",
361   "video/x-h263, variant=(string)foobar",
362   "video/x-h263",
363   "video/x-msmpeg, msmpegversion=(int)41",
364   "video/x-msmpeg, msmpegversion=(int)42",
365   "video/x-msmpeg, msmpegversion=(int)43",
366   "video/x-msmpeg, msmpegversion=(int)99",
367   "video/x-msmpeg",
368   "video/x-pn-realvideo, rmversion=(int)1",
369   "video/x-pn-realvideo, rmversion=(int)2",
370   "video/x-pn-realvideo, rmversion=(int)3",
371   "video/x-pn-realvideo, rmversion=(int)4",
372   "video/x-pn-realvideo, rmversion=(int)99",
373   "video/x-pn-realvideo",
374   "audio/x-pn-realaudio, raversion=(int)1",
375   "audio/x-pn-realaudio, raversion=(int)2",
376   "audio/x-pn-realaudio, raversion=(int)99",
377   "audio/x-pn-realaudio",
378   "audio/x-mace, maceversion=(int)3",
379   "audio/x-mace, maceversion=(int)6",
380   "audio/x-mace, maceversion=(int)99",
381   "audio/x-mace",
382   "video/x-truemotion, trueversion=(int)1",
383   "video/x-truemotion, trueversion=(int)2",
384   "video/x-truemotion, trueversion=(int)99",
385   "video/x-truemotion",
386   "video/x-asus, asusversion=(int)1",
387   "video/x-asus, asusversion=(int)2",
388   "video/x-asus, asusversion=(int)99",
389   "video/x-asus",
390   "video/x-xan, wcversion=(int)1",
391   "video/x-xan, wcversion=(int)99",
392   "video/x-xan",
393   "video/x-ati-vcr, vcrversion=(int)1",
394   "video/x-ati-vcr, vcrversion=(int)2",
395   "video/x-ati-vcr, vcrversion=(int)99",
396   "video/x-ati-vcr",
397   /* raw audio */
398   "audio/x-raw, format=(string)S16LE, rate=(int)44100, channels=(int)2",
399   "audio/x-raw, format=(string)F32,rate=(int)22050, channels=(int)2",
400   /* raw video */
401   "video/x-raw, format=(string)RGB16, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)1/1",
402   "video/x-raw, format=(string)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1",
403   /* and a made-up format */
404   "video/x-tpm"
405 };
406 
GST_START_TEST(test_pb_utils_get_codec_description)407 GST_START_TEST (test_pb_utils_get_codec_description)
408 {
409   gint i;
410 
411   gst_pb_utils_init ();
412 
413   for (i = 0; i < G_N_ELEMENTS (caps_strings); ++i) {
414     GstCaps *caps;
415     gchar *desc;
416 
417     caps = gst_caps_from_string (caps_strings[i]);
418     fail_unless (caps != NULL, "could not create caps from string '%s'",
419         caps_strings[i]);
420     GST_LOG ("Caps %s:", caps_strings[i]);
421     desc = gst_pb_utils_get_codec_description (caps);
422     fail_unless (desc != NULL);
423     GST_LOG (" - codec   : %s", desc);
424     fail_unless (g_utf8_validate (desc, -1, NULL));
425     g_free (desc);
426     desc = gst_pb_utils_get_decoder_description (caps);
427     fail_unless (desc != NULL);
428     GST_LOG (" - decoder : %s", desc);
429     fail_unless (g_utf8_validate (desc, -1, NULL));
430     g_free (desc);
431     desc = gst_pb_utils_get_encoder_description (caps);
432     fail_unless (desc != NULL);
433     GST_LOG (" - encoder : %s", desc);
434     fail_unless (g_utf8_validate (desc, -1, NULL));
435     g_free (desc);
436     gst_caps_unref (caps);
437   }
438 }
439 
440 GST_END_TEST;
441 
442 
GST_START_TEST(test_pb_utils_taglist_add_codec_info)443 GST_START_TEST (test_pb_utils_taglist_add_codec_info)
444 {
445   GstTagList *list;
446   GstCaps *caps, *bogus_caps;
447   gchar *res;
448 
449   gst_pb_utils_init ();
450   list = gst_tag_list_new_empty ();
451   caps = gst_caps_new_empty_simple ("video/x-theora");
452   ASSERT_CRITICAL (fail_if
453       (gst_pb_utils_add_codec_description_to_tag_list (NULL,
454               GST_TAG_VIDEO_CODEC, caps)));
455   ASSERT_CRITICAL (fail_if
456       (gst_pb_utils_add_codec_description_to_tag_list (list, "asdfa", caps)));
457   ASSERT_CRITICAL (fail_if
458       (gst_pb_utils_add_codec_description_to_tag_list (list,
459               GST_TAG_IMAGE, caps)));
460   ASSERT_CRITICAL (fail_if
461       (gst_pb_utils_add_codec_description_to_tag_list (list,
462               GST_TAG_VIDEO_CODEC, NULL)));
463 
464   /* Try adding bogus caps (should fail) */
465   bogus_caps = gst_caps_new_empty_simple ("bogus/format");
466   fail_if (gst_pb_utils_add_codec_description_to_tag_list (list,
467           GST_TAG_VIDEO_CODEC, bogus_caps));
468   gst_caps_unref (bogus_caps);
469 
470   /* Try adding valid caps with known tag */
471   fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list,
472           GST_TAG_VIDEO_CODEC, caps));
473   fail_if (gst_tag_list_is_empty (list));
474   fail_unless (gst_tag_list_get_string (list, GST_TAG_VIDEO_CODEC, &res));
475   g_free (res);
476   gst_tag_list_unref (list);
477 
478   /* Try adding valid caps with auto-tag (for video, audio, subtitle, generic) */
479   list = gst_tag_list_new_empty ();
480   fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
481           caps));
482   fail_if (gst_tag_list_is_empty (list));
483   fail_unless (gst_tag_list_get_string (list, GST_TAG_VIDEO_CODEC, &res));
484   g_free (res);
485   gst_tag_list_unref (list);
486   gst_caps_unref (caps);
487 
488   list = gst_tag_list_new_empty ();
489   caps = gst_caps_new_empty_simple ("audio/x-vorbis");
490   fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
491           caps));
492   fail_if (gst_tag_list_is_empty (list));
493   fail_unless (gst_tag_list_get_string (list, GST_TAG_AUDIO_CODEC, &res));
494   g_free (res);
495   gst_tag_list_unref (list);
496   gst_caps_unref (caps);
497 
498   list = gst_tag_list_new_empty ();
499   caps = gst_caps_new_empty_simple ("subtitle/x-kate");
500   fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
501           caps));
502   fail_if (gst_tag_list_is_empty (list));
503   fail_unless (gst_tag_list_get_string (list, GST_TAG_SUBTITLE_CODEC, &res));
504   g_free (res);
505   gst_tag_list_unref (list);
506   gst_caps_unref (caps);
507 
508   list = gst_tag_list_new_empty ();
509   caps = gst_caps_new_empty_simple ("application/ogg");
510   fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
511           caps));
512   fail_if (gst_tag_list_is_empty (list));
513   fail_unless (gst_tag_list_get_string (list, GST_TAG_CONTAINER_FORMAT, &res));
514   g_free (res);
515   gst_tag_list_unref (list);
516   gst_caps_unref (caps);
517 
518   list = gst_tag_list_new_empty ();
519   caps = gst_caps_new_empty_simple ("image/bmp");
520   fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
521           caps));
522   fail_if (gst_tag_list_is_empty (list));
523   fail_unless (gst_tag_list_get_string (list, GST_TAG_CODEC, &res));
524   g_free (res);
525   gst_tag_list_unref (list);
526   gst_caps_unref (caps);
527 }
528 
529 GST_END_TEST;
530 
531 static gint marker;
532 
533 static void
result_cb(GstInstallPluginsReturn result,gpointer user_data)534 result_cb (GstInstallPluginsReturn result, gpointer user_data)
535 {
536   GST_LOG ("result = %u, user_data = %p", result, user_data);
537 
538   fail_unless (user_data == (gpointer) & marker);
539 
540   marker = result;
541 }
542 
543 #define SCRIPT_NO_XID \
544     "#!/bin/sh\n"                                  \
545     "if test x$1 != xdetail1; then exit 21; fi;\n" \
546     "if test x$2 != xdetail2; then exit 22; fi;\n" \
547     "exit 1\n"
548 
549 #define SCRIPT_WITH_XID \
550     "#!/bin/sh\n"                                  \
551     "if test x$1 != 'x--transient-for=42'; then exit 21; fi;\n"      \
552     "if test x$2 != xdetail1; then exit 22; fi;\n" \
553     "if test x$3 != xdetail2; then exit 23; fi;\n" \
554     "exit 0\n"
555 
556 /* make sure our script gets called with the right parameters */
557 static void
test_pb_utils_install_plugins_do_callout(const gchar * const * details,GstInstallPluginsContext * ctx,const gchar * script,GstInstallPluginsReturn expected_result)558 test_pb_utils_install_plugins_do_callout (const gchar * const *details,
559     GstInstallPluginsContext * ctx, const gchar * script,
560     GstInstallPluginsReturn expected_result)
561 {
562 #ifdef G_OS_UNIX
563   GstInstallPluginsReturn ret;
564   GError *err = NULL;
565   gchar *path;
566 
567   path = g_strdup_printf ("%s/gst-plugins-base-unit-test-helper.%s.%lu",
568       g_get_tmp_dir (), (g_get_user_name ())? g_get_user_name () : "nobody",
569       (gulong) getpid ());
570 
571   if (!g_file_set_contents (path, script, -1, &err)) {
572     GST_DEBUG ("Failed to write test script to %s: %s", path, err->message);
573     g_error_free (err);
574     goto done;
575   }
576 
577   if (chmod (path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
578     GST_DEBUG ("Could not set mode u+rwx on '%s'", path);
579     goto done;
580   }
581 
582   /* test gst_install_plugins_supported() I */
583   g_setenv ("GST_INSTALL_PLUGINS_HELPER", "/i/do/not/ex.ist!", 1);
584   fail_if (gst_install_plugins_supported ());
585 
586   GST_LOG ("setting GST_INSTALL_PLUGINS_HELPER to '%s'", path);
587   g_setenv ("GST_INSTALL_PLUGINS_HELPER", path, 1);
588 
589   /* test gst_install_plugins_supported() II */
590   fail_unless (gst_install_plugins_supported ());
591 
592   /* test sync callout */
593   ret = gst_install_plugins_sync (details, ctx);
594   fail_unless (ret == GST_INSTALL_PLUGINS_HELPER_MISSING ||
595       ret == expected_result,
596       "gst_install_plugins_sync() failed with unexpected ret %d, which is "
597       "neither HELPER_MISSING nor %d", ret, expected_result);
598 
599   /* test async callout */
600   marker = -333;
601   ret = gst_install_plugins_async (details, ctx, result_cb,
602       (gpointer) & marker);
603   fail_unless (ret == GST_INSTALL_PLUGINS_HELPER_MISSING ||
604       ret == GST_INSTALL_PLUGINS_STARTED_OK,
605       "gst_install_plugins_async() failed with unexpected ret %d", ret);
606   if (ret == GST_INSTALL_PLUGINS_STARTED_OK) {
607     while (marker == -333) {
608       g_usleep (500);
609       g_main_context_iteration (NULL, FALSE);
610     }
611     /* and check that the callback was called with the expected code */
612     fail_unless_equals_int (marker, expected_result);
613   }
614 
615 done:
616 
617   g_unlink (path);
618   g_free (path);
619 #endif /* G_OS_UNIX */
620 }
621 
GST_START_TEST(test_pb_utils_install_plugins)622 GST_START_TEST (test_pb_utils_install_plugins)
623 {
624   GstInstallPluginsContext *ctx;
625   GstInstallPluginsReturn ret;
626   const gchar *details[] = { "detail1", "detail2", NULL };
627   const gchar *details_multi[] = { "detail1", "detail1", "detail2", NULL };
628 
629   ctx = gst_install_plugins_context_new ();
630 
631   ASSERT_CRITICAL (ret = gst_install_plugins_sync (NULL, ctx));
632   ASSERT_CRITICAL (ret =
633       gst_install_plugins_async (NULL, ctx, result_cb, (gpointer) & marker));
634   ASSERT_CRITICAL (ret =
635       gst_install_plugins_async (details, ctx, NULL, (gpointer) & marker));
636 
637   /* make sure the functions return the right error code if the helper does
638    * not exist */
639   g_setenv ("GST_INSTALL_PLUGINS_HELPER", "/does/not/ex/is.t", 1);
640   ret = gst_install_plugins_sync (details, NULL);
641   fail_unless_equals_int (ret, GST_INSTALL_PLUGINS_HELPER_MISSING);
642 
643   marker = -333;
644   ret =
645       gst_install_plugins_async (details, NULL, result_cb, (gpointer) & marker);
646   fail_unless_equals_int (ret, GST_INSTALL_PLUGINS_HELPER_MISSING);
647   /* and check that the callback wasn't called */
648   fail_unless_equals_int (marker, -333);
649 
650   /* now make sure our scripts are actually called as expected (if possible) */
651   test_pb_utils_install_plugins_do_callout (details, NULL, SCRIPT_NO_XID,
652       GST_INSTALL_PLUGINS_NOT_FOUND);
653 
654   /* and again with context */
655   gst_install_plugins_context_set_xid (ctx, 42);
656   test_pb_utils_install_plugins_do_callout (details, ctx, SCRIPT_WITH_XID,
657       GST_INSTALL_PLUGINS_SUCCESS);
658 
659   /* and make sure that duplicate detail strings get dropped */
660   test_pb_utils_install_plugins_do_callout (details_multi, NULL, SCRIPT_NO_XID,
661       GST_INSTALL_PLUGINS_NOT_FOUND);
662 
663   /* and the same again with context */
664   gst_install_plugins_context_set_xid (ctx, 42);
665   test_pb_utils_install_plugins_do_callout (details_multi, ctx, SCRIPT_WITH_XID,
666       GST_INSTALL_PLUGINS_SUCCESS);
667 
668   /* and free the context now that we don't need it any longer */
669   gst_install_plugins_context_free (ctx);
670 
671   /* completely silly test to check gst_install_plugins_return_get_name()
672    * is somewhat well-behaved */
673   {
674     gint i;
675 
676     for (i = -99; i < 16738; ++i) {
677       const gchar *s;
678 
679       s = gst_install_plugins_return_get_name ((GstInstallPluginsReturn) i);
680       fail_unless (s != NULL);
681       /* GST_LOG ("%5d = %s", i, s); */
682     }
683   }
684 }
685 
686 GST_END_TEST;
687 
GST_START_TEST(test_pb_utils_installer_details)688 GST_START_TEST (test_pb_utils_installer_details)
689 {
690   GstMessage *msg;
691   GstElement *el;
692   GstCaps *caps;
693   gchar *detail1, *detail2;
694 
695   el = gst_pipeline_new ("dummy-element");
696 
697   /* uri source */
698   detail1 = gst_missing_uri_source_installer_detail_new ("http");
699   fail_unless (detail1 != NULL);
700   fail_unless (g_str_has_prefix (detail1, "gstreamer|1.0|"));
701   fail_unless (g_str_has_suffix (detail1, "|urisource-http"));
702   msg = gst_missing_uri_source_message_new (el, "http");
703   fail_unless (msg != NULL);
704   detail2 = gst_missing_plugin_message_get_installer_detail (msg);
705   fail_unless (detail2 != NULL);
706   gst_message_unref (msg);
707   fail_unless_equals_string (detail1, detail2);
708   g_free (detail1);
709   g_free (detail2);
710 
711   /* uri sink */
712   detail1 = gst_missing_uri_sink_installer_detail_new ("http");
713   fail_unless (detail1 != NULL);
714   fail_unless (g_str_has_prefix (detail1, "gstreamer|1.0|"));
715   fail_unless (g_str_has_suffix (detail1, "|urisink-http"));
716   msg = gst_missing_uri_sink_message_new (el, "http");
717   fail_unless (msg != NULL);
718   detail2 = gst_missing_plugin_message_get_installer_detail (msg);
719   fail_unless (detail2 != NULL);
720   gst_message_unref (msg);
721   fail_unless_equals_string (detail1, detail2);
722   g_free (detail1);
723   g_free (detail2);
724 
725   /* element */
726   detail1 = gst_missing_element_installer_detail_new ("deinterlace");
727   fail_unless (detail1 != NULL);
728   fail_unless (g_str_has_prefix (detail1, "gstreamer|1.0|"));
729   fail_unless (g_str_has_suffix (detail1, "|element-deinterlace"));
730   msg = gst_missing_element_message_new (el, "deinterlace");
731   fail_unless (msg != NULL);
732   detail2 = gst_missing_plugin_message_get_installer_detail (msg);
733   fail_unless (detail2 != NULL);
734   gst_message_unref (msg);
735   fail_unless_equals_string (detail1, detail2);
736   g_free (detail1);
737   g_free (detail2);
738 
739   /* decoder */
740   caps = gst_caps_new_simple ("audio/x-spiffy", "spiffyversion", G_TYPE_INT,
741       2, "channels", G_TYPE_INT, 6, NULL);
742   detail1 = gst_missing_decoder_installer_detail_new (caps);
743   fail_unless (detail1 != NULL);
744   fail_unless (g_str_has_prefix (detail1, "gstreamer|1.0|"));
745   fail_unless (g_str_has_suffix (detail1,
746           "|decoder-audio/x-spiffy, spiffyversion=(int)2"));
747   msg = gst_missing_decoder_message_new (el, caps);
748   fail_unless (msg != NULL);
749   detail2 = gst_missing_plugin_message_get_installer_detail (msg);
750   fail_unless (detail2 != NULL);
751   gst_message_unref (msg);
752   gst_caps_unref (caps);
753   fail_unless_equals_string (detail1, detail2);
754   g_free (detail1);
755   g_free (detail2);
756 
757   /* encoder */
758   caps = gst_caps_new_simple ("audio/x-spiffy", "spiffyversion", G_TYPE_INT,
759       2, "channels", G_TYPE_INT, 6, NULL);
760   detail1 = gst_missing_encoder_installer_detail_new (caps);
761   fail_unless (g_str_has_prefix (detail1, "gstreamer|1.0|"));
762   fail_unless (g_str_has_suffix (detail1,
763           "|encoder-audio/x-spiffy, spiffyversion=(int)2"));
764   fail_unless (detail1 != NULL);
765   msg = gst_missing_encoder_message_new (el, caps);
766   fail_unless (msg != NULL);
767   detail2 = gst_missing_plugin_message_get_installer_detail (msg);
768   fail_unless (detail2 != NULL);
769   gst_message_unref (msg);
770   gst_caps_unref (caps);
771   fail_unless_equals_string (detail1, detail2);
772   g_free (detail1);
773   g_free (detail2);
774 
775   gst_object_unref (el);
776 }
777 
778 GST_END_TEST;
779 
GST_START_TEST(test_pb_utils_versions)780 GST_START_TEST (test_pb_utils_versions)
781 {
782   gchar *s;
783   guint maj, min, mic, nano;
784 
785   gst_plugins_base_version (NULL, NULL, NULL, NULL);
786   gst_plugins_base_version (&maj, &min, &mic, &nano);
787   fail_unless_equals_int (maj, GST_PLUGINS_BASE_VERSION_MAJOR);
788   fail_unless_equals_int (min, GST_PLUGINS_BASE_VERSION_MINOR);
789   fail_unless_equals_int (mic, GST_PLUGINS_BASE_VERSION_MICRO);
790   fail_unless_equals_int (nano, GST_PLUGINS_BASE_VERSION_NANO);
791 
792   s = gst_plugins_base_version_string ();
793   if (GST_PLUGINS_BASE_VERSION_NANO == 0) {
794     fail_if (strstr (s, "GIT") || strstr (s, "git") || strstr (s, "prerel"));
795   }
796   if (GST_PLUGINS_BASE_VERSION_NANO == 1) {
797     fail_unless (strstr (s, "GIT") || strstr (s, "git"));
798   }
799   if (GST_PLUGINS_BASE_VERSION_NANO >= 2) {
800     fail_unless (strstr (s, "Prerelease") || strstr (s, "prerelease"));
801   }
802   g_free (s);
803 }
804 
805 GST_END_TEST;
806 
GST_START_TEST(test_pb_utils_aac_get_profile)807 GST_START_TEST (test_pb_utils_aac_get_profile)
808 {
809   const guint8 aac_config[] = { 0x11, 0x90, 0x56, 0xE5, 0x00 };
810   const guint8 aac_config_sre[] = { 0x17, 0x80, 0x91, 0xA2, 0x82, 0x00 };
811   const guint8 heaac_config[] = { 0x2B, 0x11, 0x88, 0x00, 0x06, 0x01, 0x02 };
812   const gchar *profile, *level;
813   guint sample_rate;
814   GstBitWriter *wr;
815   guint8 *buf;
816   guint buf_len;
817 
818   profile = gst_codec_utils_aac_get_profile (aac_config, sizeof (aac_config));
819   fail_unless (profile != NULL);
820   fail_unless_equals_string (profile, "lc");
821 
822   level = gst_codec_utils_aac_get_level (aac_config, sizeof (aac_config));
823   fail_unless_equals_string (level, "2");
824 
825   sample_rate =
826       gst_codec_utils_aac_get_sample_rate (aac_config, sizeof (aac_config));
827   fail_unless_equals_int (sample_rate, 48000);
828 
829   sample_rate =
830       gst_codec_utils_aac_get_sample_rate (aac_config_sre,
831       sizeof (aac_config_sre));
832   fail_unless_equals_int (sample_rate, 0x12345);
833 
834   profile =
835       gst_codec_utils_aac_get_profile (heaac_config, sizeof (heaac_config));
836   fail_unless (profile != NULL);
837   fail_unless_equals_string (profile, "lc");
838 
839   level = gst_codec_utils_aac_get_level (heaac_config, sizeof (heaac_config));
840   fail_unless_equals_string (level, "2");
841 
842   sample_rate =
843       gst_codec_utils_aac_get_sample_rate (heaac_config, sizeof (heaac_config));
844   fail_unless_equals_int (sample_rate, 48000);
845 
846   wr = gst_bit_writer_new ();
847   fail_if (wr == NULL);
848   gst_bit_writer_put_bits_uint8 (wr, 5, 5);     /* object_type = 5 (SBR) */
849   gst_bit_writer_put_bits_uint8 (wr, 3, 4);     /* freq_index = 3 (48KHz) */
850   gst_bit_writer_put_bits_uint8 (wr, 2, 4);     /* channel_config = 2 (L&R) */
851   gst_bit_writer_put_bits_uint8 (wr, 0x0f, 4);  /* freq_index extension */
852   gst_bit_writer_put_bits_uint32 (wr, 87654, 24);       /* freq */
853   gst_bit_writer_put_bits_uint8 (wr, 2, 5);     /* object_type = 2 (LC) */
854 
855   buf = gst_bit_writer_get_data (wr);
856   buf_len = gst_bit_writer_get_size (wr);
857   profile = gst_codec_utils_aac_get_profile (buf, buf_len);
858   fail_unless (profile != NULL);
859   fail_unless_equals_string (profile, "lc");
860   level = gst_codec_utils_aac_get_level (buf, buf_len);
861   fail_unless (level != NULL);
862   fail_unless_equals_string (level, "5");
863   sample_rate = gst_codec_utils_aac_get_sample_rate (buf, buf_len);
864   fail_unless_equals_int (sample_rate, 87654);
865   gst_bit_writer_free (wr);
866 }
867 
868 GST_END_TEST;
869 
870 static Suite *
libgstpbutils_suite(void)871 libgstpbutils_suite (void)
872 {
873   Suite *s = suite_create ("pbutils library");
874   TCase *tc_chain = tcase_create ("general");
875 
876   suite_add_tcase (s, tc_chain);
877   tcase_add_test (tc_chain, test_pb_utils_init);
878   tcase_add_test (tc_chain, test_pb_utils_post_missing_messages);
879   tcase_add_test (tc_chain, test_pb_utils_taglist_add_codec_info);
880   tcase_add_test (tc_chain, test_pb_utils_get_codec_description);
881   tcase_add_test (tc_chain, test_pb_utils_install_plugins);
882   tcase_add_test (tc_chain, test_pb_utils_installer_details);
883   tcase_add_test (tc_chain, test_pb_utils_versions);
884   tcase_add_test (tc_chain, test_pb_utils_aac_get_profile);
885   return s;
886 }
887 
888 GST_CHECK_MAIN (libgstpbutils);
889