gstreamer/validate/tools/gst-validate.c

186 lines
4.5 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) 2013 Thiago Santos <thiago.sousa.santos@collabora.com>
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <gst/gst.h>
2013-08-14 19:30:39 +00:00
#include <gst/validate/validate.h>
#include <gst/validate/gst-validate-scenario.h>
#ifdef G_OS_UNIX
#include <glib-unix.h>
#endif
static gint ret = 0;
static GMainLoop *mainloop;
static GstElement *pipeline;
#ifdef G_OS_UNIX
static gboolean
intr_handler (gpointer user_data)
{
g_print ("interrupt received.\n");
g_main_loop_quit (mainloop);
/* remove signal handler */
return FALSE;
}
#endif /* G_OS_UNIX */
static gboolean
bus_callback (GstBus * bus, GstMessage * message, gpointer data)
{
GMainLoop *loop = data;
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR:
{
GError *err;
gchar *debug;
ret = -1;
gst_message_parse_error (message, &err, &debug);
g_print ("Error: %s\n", err->message);
g_error_free (err);
g_free (debug);
g_main_loop_quit (loop);
break;
}
case GST_MESSAGE_EOS:
g_main_loop_quit (loop);
break;
default:
break;
}
return TRUE;
}
int
main (int argc, gchar ** argv)
{
GSList *tmp;
GError *err = NULL;
const gchar *scenario = NULL;
gboolean list_scenarios = FALSE;
#ifdef G_OS_UNIX
guint signal_watch_id;
#endif
guint count = 0;
GOptionEntry options[] = {
{"set-scenario", '\0', 0, G_OPTION_ARG_STRING, &scenario,
2013-08-14 19:30:39 +00:00
"Let you set a scenario, it will override the GST_VALIDATE_SCENARIO "
"environment variable", NULL},
{"list-scenarios", 'l', 0, G_OPTION_ARG_NONE, &list_scenarios,
"List the avalaible scenarios that can be run", NULL},
{NULL}
};
GOptionContext *ctx;
gchar **argvn;
2013-08-14 19:30:39 +00:00
GstValidateRunner *runner;
GstValidateMonitor *monitor;
GstBus *bus;
2013-08-14 21:04:23 +00:00
g_set_prgname ("gst-validate-" GST_API_VERSION);
ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
g_option_context_add_main_entries (ctx, options, NULL);
2013-08-14 21:04:23 +00:00
g_option_context_set_summary (ctx, "Runs a gst launch pipeline, adding "
"monitors to it to identify issues in the used elements. At the end"
" a report will be printed. To view issues as they are created, set"
"the env var GST_DEBUG=validate:2 and it will be printed "
"as gstreamer debugging");
if (argc == 1) {
g_print ("%s", g_option_context_get_help (ctx, FALSE, NULL));
exit (1);
}
if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
g_printerr ("Error initializing: %s\n", err->message);
g_option_context_free (ctx);
exit (1);
}
if (scenario) {
2013-08-14 19:30:39 +00:00
g_setenv ("GST_VALIDATE_SCENARIO", scenario, TRUE);
}
g_option_context_free (ctx);
if (list_scenarios)
gst_validate_list_scenarios ();
gst_init (&argc, &argv);
gst_validate_init ();
/* Create the pipeline */
argvn = g_new0 (char *, argc);
memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
pipeline = (GstElement *) gst_parse_launchv ((const gchar **) argvn, &err);
g_free (argvn);
#ifdef G_OS_UNIX
signal_watch_id =
g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, pipeline);
#endif
2013-08-14 19:30:39 +00:00
runner = gst_validate_runner_new ();
monitor =
2013-08-14 19:30:39 +00:00
gst_validate_monitor_factory_create (GST_OBJECT_CAST (pipeline), runner,
NULL);
mainloop = g_main_loop_new (NULL, FALSE);
if (!runner) {
2013-08-14 19:30:39 +00:00
g_printerr ("Failed to setup Validate Runner\n");
exit (1);
}
bus = gst_element_get_bus (pipeline);
gst_bus_add_signal_watch (bus);
g_signal_connect (bus, "message", (GCallback) bus_callback, mainloop);
gst_object_unref (bus);
g_print ("Starting pipeline\n");
if (gst_element_set_state (pipeline,
GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
g_print ("Pipeline failed to go to PLAYING state\n");
goto exit;
}
g_print ("Pipeline started\n");
g_main_loop_run (mainloop);
for (tmp = gst_validate_runner_get_reports (runner); tmp; tmp = tmp->next) {
GstValidateReport *report = tmp->data;
gst_validate_report_printf (report);
if (ret == 0 && report->level == GST_VALIDATE_REPORT_LEVEL_CRITICAL) {
g_printerr ("Got critical error %s, setting return value to -1\n",
((GstValidateReport *) (tmp->data))->message);
ret = -1;
}
count++;
}
g_print ("Pipeline finished, issues found: %u\n", count);
exit:
gst_element_set_state (pipeline, GST_STATE_NULL);
g_main_loop_unref (mainloop);
g_object_unref (monitor);
g_object_unref (runner);
g_object_unref (pipeline);
#ifdef G_OS_UNIX
g_source_remove (signal_watch_id);
#endif
return ret;
}