• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer unit tests for avdemux_ape
2  *
3  * Copyright (C) 2009 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 #include <gst/gst.h>
24 
25 typedef void (CheckTagsFunc) (const GstTagList * tags, const gchar * file);
26 
27 static void
pad_added_cb(GstElement * decodebin,GstPad * pad,GstBin * pipeline)28 pad_added_cb (GstElement * decodebin, GstPad * pad, GstBin * pipeline)
29 {
30   GstElement *sink;
31 
32   sink = gst_bin_get_by_name (pipeline, "fakesink");
33   fail_unless (gst_element_link (decodebin, sink));
34   gst_object_unref (sink);
35 
36   gst_element_set_state (sink, GST_STATE_PAUSED);
37 }
38 
39 static GstBusSyncReply
error_cb(GstBus * bus,GstMessage * msg,gpointer user_data)40 error_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
41 {
42   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
43     const gchar *file = (const gchar *) user_data;
44     GError *err = NULL;
45     gchar *dbg = NULL;
46 
47     gst_message_parse_error (msg, &err, &dbg);
48     g_error ("ERROR for %s: %s\n%s\n", file, err->message, dbg);
49   }
50 
51   return GST_BUS_PASS;
52 }
53 
54 static GstPadProbeReturn
event_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)55 event_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
56 {
57   GstTagList **p_tags = user_data;
58   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
59 
60   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
61     GST_INFO ("tag event: %" GST_PTR_FORMAT, event);
62     if (*p_tags == NULL) {
63       GstTagList *event_tags;
64 
65       GST_INFO ("first tag, saving");
66       gst_event_parse_tag (event, &event_tags);
67       *p_tags = gst_tag_list_copy (event_tags);
68     }
69   }
70   return GST_PAD_PROBE_OK;      /* keep the data */
71 }
72 
73 /* FIXME: push_mode not used currently */
74 static GstTagList *
read_tags_from_file(const gchar * file,gboolean push_mode)75 read_tags_from_file (const gchar * file, gboolean push_mode)
76 {
77   GstStateChangeReturn state_ret;
78   GstTagList *tags = NULL;
79   GstElement *sink, *src, *dec, *pipeline;
80   GstBus *bus;
81   GstPad *pad;
82   gchar *path;
83 
84   pipeline = gst_pipeline_new ("pipeline");
85   fail_unless (pipeline != NULL, "Failed to create pipeline!");
86 
87   src = gst_element_factory_make ("filesrc", "filesrc");
88   fail_unless (src != NULL, "Failed to create filesrc!");
89 
90   dec = gst_element_factory_make ("decodebin", "decodebin");
91   fail_unless (dec != NULL, "Failed to create decodebin!");
92 
93   sink = gst_element_factory_make ("fakesink", "fakesink");
94   fail_unless (sink != NULL, "Failed to create fakesink!");
95 
96   bus = gst_element_get_bus (pipeline);
97 
98   /* kids, don't use a sync handler for this at home, really; we do because
99    * we just want to abort and nothing else */
100   gst_bus_set_sync_handler (bus, error_cb, (gpointer) file, NULL);
101 
102   gst_bin_add_many (GST_BIN (pipeline), src, dec, sink, NULL);
103   gst_element_link_many (src, dec, NULL);
104 
105   path = g_build_filename (GST_TEST_FILES_PATH, file, NULL);
106   GST_LOG ("reading file '%s'", path);
107   g_object_set (src, "location", path, NULL);
108 
109   /* can't link uridecodebin and sink yet, do that later */
110   g_signal_connect (dec, "pad-added", G_CALLBACK (pad_added_cb), pipeline);
111 
112   /* we want to make sure there's a tag event coming out of avdemux_ape
113    * (ie. the one apedemux generated) */
114   pad = gst_element_get_static_pad (sink, "sink");
115   gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, event_probe,
116       &tags, NULL);
117   gst_object_unref (pad);
118 
119   state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
120   fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
121 
122   if (state_ret == GST_STATE_CHANGE_ASYNC) {
123     GST_LOG ("waiting for pipeline to reach PAUSED state");
124     state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
125     fail_unless_equals_int (state_ret, GST_STATE_CHANGE_SUCCESS);
126   }
127 
128   GST_LOG ("PAUSED, let's retrieve our tags");
129 
130   fail_unless (tags != NULL, "Expected tag event! (%s)", file);
131 
132   gst_object_unref (bus);
133 
134   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
135       GST_STATE_CHANGE_SUCCESS);
136   gst_object_unref (pipeline);
137 
138   g_free (path);
139 
140   GST_INFO ("%s: tags = %" GST_PTR_FORMAT, file, tags);
141   return tags;
142 }
143 
144 static void
run_check_for_file(const gchar * filename,CheckTagsFunc * check_func)145 run_check_for_file (const gchar * filename, CheckTagsFunc * check_func)
146 {
147   GstTagList *tags;
148 
149   /* first, pull-based */
150   tags = read_tags_from_file (filename, FALSE);
151   fail_unless (tags != NULL, "Failed to extract tags from '%s'", filename);
152   check_func (tags, filename);
153   gst_tag_list_unref (tags);
154 }
155 
156 #define tag_list_has_tag(taglist,tag) \
157     (gst_tag_list_get_value_index((taglist),(tag),0) != NULL)
158 
159 /* just make sure avdemux_ape forwarded the tags extracted by apedemux
160  * (should be the first tag list / tag event too) */
161 static void
check_for_apedemux_tags(const GstTagList * tags,const gchar * file)162 check_for_apedemux_tags (const GstTagList * tags, const gchar * file)
163 {
164   gchar *artist = NULL;
165 
166   fail_unless (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &artist));
167   fail_unless (artist != NULL);
168   fail_unless_equals_string (artist, "Marvin Gaye");
169   g_free (artist);
170 
171   fail_unless (tag_list_has_tag (tags, GST_TAG_CONTAINER_FORMAT));
172 
173   GST_LOG ("all good");
174 }
175 
GST_START_TEST(test_tag_caching)176 GST_START_TEST (test_tag_caching)
177 {
178 #define MIN_VERSION GST_VERSION_MAJOR, GST_VERSION_MINOR, 0
179 
180   if (!gst_registry_check_feature_version (gst_registry_get (), "apedemux",
181           MIN_VERSION)
182       || !gst_registry_check_feature_version (gst_registry_get (), "decodebin",
183           MIN_VERSION)) {
184     g_printerr ("Skipping test_tag_caching: required element apedemux or "
185         "decodebin element not found\n");
186     return;
187   }
188 
189   run_check_for_file ("586957.ape", check_for_apedemux_tags);
190 }
191 
192 GST_END_TEST;
193 
194 static Suite *
avdemux_ape_suite(void)195 avdemux_ape_suite (void)
196 {
197   Suite *s = suite_create ("avdemux_ape");
198   TCase *tc_chain = tcase_create ("general");
199 
200   suite_add_tcase (s, tc_chain);
201   tcase_add_test (tc_chain, test_tag_caching);
202 
203   return s;
204 }
205 
206 GST_CHECK_MAIN (avdemux_ape)
207