3 * Copyright (C) 2013 Collabora Ltd.
4 * Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>
6 * gst-validate-runner.c - Validate Runner class
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
31 #include "gst-validate-internal.h"
32 #include "gst-validate-report.h"
33 #include "gst-validate-monitor-factory.h"
34 #include "gst-validate-override-registry.h"
35 #include "gst-validate-runner.h"
38 * SECTION:gst-validate-runner
39 * @short_description: Class that runs Gst Validate tests for a pipeline
41 * Allows you to test a pipeline within GstValidate. It is the object where
42 * all issue reporting is done.
44 * In the tools using GstValidate the only minimal code to be able to monitor
48 * GstPipeline *pipeline = gst_pipeline_new ("monitored-pipeline");
49 * GstValidateRunner *runner = gst_validate_runner_new ();
50 * GstValidateMonitor *monitor = gst_validate_monitor_factory_create (
51 * GST_OBJECT (pipeline), runner, NULL);
53 * // Run the pipeline and do whatever you want with it
55 * // In that same order
56 * gst_object_unref (pipeline);
57 * gst_object_unref (runner);
58 * gst_object_unref (monitor);
62 struct _GstValidateRunnerPrivate
66 GstValidateReportingLevel default_level;
68 /* A list of PatternLevel */
69 GList *report_pattern_levels;
72 /* Describes the reporting level to apply to a name pattern */
73 typedef struct _PatternLevel
75 GPatternSpec *pattern;
76 GstValidateReportingLevel level;
79 #define GST_VALIDATE_RUNNER_LOCK(r) \
81 GST_LOG_OBJECT (r, "About to lock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
82 (g_mutex_lock (&GST_VALIDATE_RUNNER_CAST(r)->priv->mutex)); \
83 GST_LOG_OBJECT (r, "Acquired lock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
86 #define GST_VALIDATE_RUNNER_UNLOCK(r) \
88 GST_LOG_OBJECT (r, "About to unlock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
89 (g_mutex_unlock (&GST_VALIDATE_RUNNER_CAST(r)->priv->mutex)); \
90 GST_LOG_OBJECT (r, "Released lock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
93 #define gst_validate_runner_parent_class parent_class
94 G_DEFINE_TYPE (GstValidateRunner, gst_validate_runner, G_TYPE_OBJECT);
104 static guint _signals[LAST_SIGNAL] = { 0 };
107 _parse_reporting_level (gchar * str, GstValidateReportingLevel * level)
115 if (g_ascii_isdigit (str[0])) {
118 l = strtoul (str, &endptr, 10);
119 if (endptr > str && endptr[0] == 0) {
120 *level = (GstValidateReportingLevel) l;
124 } else if (g_ascii_strcasecmp (str, "none") == 0) {
125 *level = GST_VALIDATE_REPORTING_LEVEL_NONE;
126 } else if (g_ascii_strcasecmp (str, "synthetic") == 0) {
127 *level = GST_VALIDATE_REPORTING_LEVEL_SYNTHETIC;
128 } else if (g_ascii_strcasecmp (str, "subchain") == 0) {
129 *level = GST_VALIDATE_REPORTING_LEVEL_SUBCHAIN;
130 } else if (g_ascii_strcasecmp (str, "monitor") == 0) {
131 *level = GST_VALIDATE_REPORTING_LEVEL_MONITOR;
132 } else if (g_ascii_strcasecmp (str, "all") == 0) {
133 *level = GST_VALIDATE_REPORTING_LEVEL_ALL;
141 _free_report_pattern_level (PatternLevel * pattern_level)
143 g_pattern_spec_free (pattern_level->pattern);
144 g_free (pattern_level);
148 _set_reporting_level_for_name (GstValidateRunner * runner,
149 const gchar * pattern, GstValidateReportingLevel level)
151 PatternLevel *pattern_level = g_malloc (sizeof (PatternLevel));
152 GPatternSpec *pattern_spec = g_pattern_spec_new (pattern);
154 pattern_level->pattern = pattern_spec;
155 pattern_level->level = level;
157 runner->priv->report_pattern_levels =
158 g_list_append (runner->priv->report_pattern_levels, pattern_level);
162 _set_report_levels_from_string (GstValidateRunner * self, const gchar * list)
169 GST_DEBUG_OBJECT (self, "setting report levels from string [%s]", list);
171 split = g_strsplit (list, ",", 0);
173 for (walk = split; *walk; walk++) {
174 if (strchr (*walk, ':')) {
175 gchar **values = g_strsplit (*walk, ":", 2);
177 if (values[0] && values[1]) {
178 GstValidateReportingLevel level;
180 if (_parse_reporting_level (values[1], &level))
181 _set_reporting_level_for_name (self, values[0], level);
186 GstValidateReportingLevel level;
188 if (_parse_reporting_level (*walk, &level))
189 self->priv->default_level = level;
197 _init_report_levels (GstValidateRunner * self)
201 env = g_getenv ("GST_VALIDATE_REPORT_LEVEL");
203 _set_report_levels_from_string (self, env);
207 gst_validate_runner_dispose (GObject * object)
209 GstValidateRunner *runner = GST_VALIDATE_RUNNER_CAST (object);
211 g_list_free_full (runner->priv->reports,
212 (GDestroyNotify) gst_validate_report_unref);
213 g_list_free_full (runner->priv->report_pattern_levels,
214 (GDestroyNotify) _free_report_pattern_level);
216 g_mutex_clear (&runner->priv->mutex);
218 G_OBJECT_CLASS (parent_class)->dispose (object);
222 gst_validate_runner_class_init (GstValidateRunnerClass * klass)
224 GObjectClass *gobject_class;
226 gobject_class = G_OBJECT_CLASS (klass);
228 gobject_class->dispose = gst_validate_runner_dispose;
230 g_type_class_add_private (klass, sizeof (GstValidateRunnerPrivate));
232 _signals[REPORT_ADDED_SIGNAL] =
233 g_signal_new ("report-added", G_TYPE_FROM_CLASS (klass),
234 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1,
235 GST_TYPE_VALIDATE_REPORT);
239 gst_validate_runner_init (GstValidateRunner * runner)
241 runner->priv = G_TYPE_INSTANCE_GET_PRIVATE (runner, GST_TYPE_VALIDATE_RUNNER,
242 GstValidateRunnerPrivate);
243 g_mutex_init (&runner->priv->mutex);
245 runner->priv->default_level = GST_VALIDATE_REPORTING_LEVEL_DEFAULT;
246 _init_report_levels (runner);
250 * gst_validate_runner_new:
252 * Create a new #GstValidateRunner
254 * Returns: A newly created #GstValidateRunner
257 gst_validate_runner_new (void)
259 return g_object_new (GST_TYPE_VALIDATE_RUNNER, NULL);
263 * gst_validate_runner_get_default_reporting_level:
265 * Returns: the default #GstValidateReportingLevel used to output a report.
267 GstValidateReportingLevel
268 gst_validate_runner_get_default_reporting_level (GstValidateRunner * runner)
270 return runner->priv->default_level;
274 * gst_validate_runner_get_reporting_level_for_name:
276 * Returns: the #GstValidateReportingLevel that will be applied for a given name.
277 * If no pattern was set for such a name, this function will return
278 * #GST_VALIDATE_REPORTING_LEVEL_UNKNOWN, and reporting for that name will
279 * default to the global reporting level.
281 GstValidateReportingLevel
282 gst_validate_runner_get_reporting_level_for_name (GstValidateRunner * runner,
287 for (tmp = runner->priv->report_pattern_levels; tmp; tmp = tmp->next) {
288 PatternLevel *pattern_level = (PatternLevel *) tmp->data;
289 if (g_pattern_match_string (pattern_level->pattern, name))
290 return pattern_level->level;
293 return GST_VALIDATE_REPORTING_LEVEL_UNKNOWN;
297 gst_validate_runner_add_report (GstValidateRunner * runner,
298 GstValidateReport * report)
301 GST_VALIDATE_RUNNER_LOCK (runner);
302 runner->priv->reports =
303 g_list_append (runner->priv->reports, gst_validate_report_ref (report));
304 GST_VALIDATE_RUNNER_UNLOCK (runner);
306 g_signal_emit (runner, _signals[REPORT_ADDED_SIGNAL], 0, report);
310 * gst_validate_runner_get_reports_count:
311 * @runner: The $GstValidateRunner to get the number of report from
313 * Get the number of reports present in the runner:
315 * Returns: The number of report present in the runner.
318 gst_validate_runner_get_reports_count (GstValidateRunner * runner)
322 g_return_val_if_fail (runner != NULL, 0);
324 GST_VALIDATE_RUNNER_LOCK (runner);
325 l = g_list_length (runner->priv->reports);
326 GST_VALIDATE_RUNNER_UNLOCK (runner);
332 gst_validate_runner_get_reports (GstValidateRunner * runner)
336 GST_VALIDATE_RUNNER_LOCK (runner);
338 g_list_copy_deep (runner->priv->reports,
339 (GCopyFunc) gst_validate_report_ref, NULL);
340 GST_VALIDATE_RUNNER_UNLOCK (runner);
346 * gst_validate_runner_printf:
347 * @runner: The #GstValidateRunner to print all the reports for
349 * Prints all the report on the terminal or on wherever set
350 * in the #GST_VALIDATE_FILE env variable.
352 * Returns: 0 if no critical error has been found and 18 if a critical
353 * error has been detected. That return value is usually to be used as
354 * exit code of the application.
357 gst_validate_runner_printf (GstValidateRunner * runner)
359 GList *reports, *tmp;
362 GList *criticals = NULL;
364 reports = gst_validate_runner_get_reports (runner);
365 for (tmp = reports; tmp; tmp = tmp->next) {
366 GstValidateReport *report = tmp->data;
368 if (gst_validate_report_should_print (report))
369 gst_validate_report_printf (report);
371 if (ret == 0 && report->level == GST_VALIDATE_REPORT_LEVEL_CRITICAL) {
372 criticals = g_list_append (criticals, tmp->data);
381 g_printerr ("\n\n==== Got criticals, Return value set to 18 ====\n");
383 for (iter = criticals; iter; iter = iter->next) {
384 g_printerr (" Critical error %s\n",
385 ((GstValidateReport *) (iter->data))->message);
390 g_list_free_full (reports, (GDestroyNotify) gst_validate_report_unref);
391 gst_validate_printf (NULL, "Issues found: %u\n", count);
392 g_list_free (criticals);