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