1 /* GStreamer zbar element unit test
2  *
3  * Copyright (C) 2010 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 #include <gst/check/gstcheck.h>
22 
23 static GstElement *
setup_pipeline(void)24 setup_pipeline (void)
25 {
26   GstElement *pipeline, *src, *dec, *csp, *zbar, *sink;
27   gchar *path;
28 
29   pipeline = gst_pipeline_new ("pipeline");
30 
31   src = gst_element_factory_make ("filesrc", NULL);
32   dec = gst_element_factory_make ("pngdec", NULL);
33   csp = gst_element_factory_make ("videoconvert", NULL);
34   zbar = gst_element_factory_make ("zbar", "zbar");
35   sink = gst_element_factory_make ("fakesink", NULL);
36 
37   path = g_build_filename (GST_TEST_FILES_PATH, "barcode.png", NULL);
38   GST_LOG ("reading file '%s'", path);
39   g_object_set (src, "location", path, NULL);
40   g_free (path);
41 
42   gst_bin_add_many (GST_BIN (pipeline), src, dec, csp, zbar, sink, NULL);
43   fail_unless (gst_element_link_many (src, dec, csp, zbar, sink, NULL));
44 
45   return pipeline;
46 }
47 
48 static GstMessage *
get_zbar_msg_until_eos(GstElement * pipeline)49 get_zbar_msg_until_eos (GstElement * pipeline)
50 {
51   GstMessage *zbar_msg = NULL;
52 
53   do {
54     GstMessage *msg;
55 
56     msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline), -1,
57         GST_MESSAGE_ELEMENT | GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
58 
59     GST_INFO ("message: %" GST_PTR_FORMAT, msg);
60 
61     fail_if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
62 
63     if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS) {
64       gst_message_unref (msg);
65       break;
66     }
67 
68     if (!g_strcmp0 (GST_OBJECT_NAME (GST_MESSAGE_SRC (msg)), "zbar")
69         && zbar_msg == NULL) {
70       zbar_msg = msg;
71     } else {
72       gst_message_unref (msg);
73     }
74   } while (1);
75   return zbar_msg;
76 }
77 
78 
GST_START_TEST(test_still_image)79 GST_START_TEST (test_still_image)
80 {
81   GstMessage *zbar_msg;
82   const GstStructure *s;
83   GstElement *pipeline;
84   const gchar *type, *symbol;
85   int qual;
86 
87   pipeline = setup_pipeline ();
88 
89   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
90       GST_STATE_CHANGE_ASYNC);
91 
92   zbar_msg = get_zbar_msg_until_eos (pipeline);
93   fail_unless (zbar_msg != NULL);
94 
95   s = gst_message_get_structure (zbar_msg);
96   fail_unless (s != NULL);
97 
98   fail_unless (gst_structure_has_name (s, "barcode"));
99   fail_unless (gst_structure_has_field (s, "timestamp"));
100   fail_unless (gst_structure_has_field (s, "type"));
101   fail_unless (gst_structure_has_field (s, "symbol"));
102   fail_unless (gst_structure_has_field (s, "quality"));
103   fail_unless (gst_structure_get_int (s, "quality", &qual));
104   fail_unless (qual >= 90);
105   type = gst_structure_get_string (s, "type");
106   fail_unless_equals_string (type, "EAN-13");
107   symbol = gst_structure_get_string (s, "symbol");
108   fail_unless_equals_string (symbol, "9876543210128");
109 
110   fail_if (gst_structure_has_field (s, "frame"));
111 
112   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
113       GST_STATE_CHANGE_SUCCESS);
114 
115   gst_object_unref (pipeline);
116   gst_message_unref (zbar_msg);
117 }
118 
119 GST_END_TEST;
120 
GST_START_TEST(test_still_image_with_sample)121 GST_START_TEST (test_still_image_with_sample)
122 {
123   GstMessage *zbar_msg = NULL;
124   const GstStructure *s;
125   GstElement *pipeline;
126   GstSample *sample;
127 
128   pipeline = setup_pipeline ();
129   gst_child_proxy_set ((GstChildProxy *) pipeline, "zbar::attach-frame", TRUE,
130       NULL);
131 
132 
133   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
134       GST_STATE_CHANGE_ASYNC);
135 
136   zbar_msg = get_zbar_msg_until_eos (pipeline);
137   fail_unless (zbar_msg != NULL);
138 
139   s = gst_message_get_structure (zbar_msg);
140   fail_unless (s != NULL);
141 
142   fail_unless (gst_structure_get (s, "frame", GST_TYPE_SAMPLE, &sample, NULL));
143   fail_unless (gst_sample_get_buffer (sample));
144   fail_unless (gst_sample_get_caps (sample));
145   gst_sample_unref (sample);
146 
147   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
148       GST_STATE_CHANGE_SUCCESS);
149 
150   gst_object_unref (pipeline);
151   gst_message_unref (zbar_msg);
152 }
153 
154 GST_END_TEST;
155 
156 static Suite *
zbar_suite(void)157 zbar_suite (void)
158 {
159   Suite *s = suite_create ("zbar");
160   TCase *tc_chain = tcase_create ("general");
161 
162   suite_add_tcase (s, tc_chain);
163 
164   if (!gst_registry_check_feature_version (gst_registry_get (), "pngdec", 0, 10,
165           25)) {
166     GST_INFO ("Skipping test, pngdec either not available or too old");
167   } else {
168     tcase_add_test (tc_chain, test_still_image);
169     tcase_add_test (tc_chain, test_still_image_with_sample);
170   }
171 
172   return s;
173 }
174 
175 GST_CHECK_MAIN (zbar);
176