Release 1.19.2
[platform/upstream/gst-editing-services.git] / tools / ges-validate.c
1 /* GStreamer Editing Services
2  *
3  * Copyright (C) <2013> Thibault Saunier <thibault.saunier@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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "utils.h"
26 #include "ges-validate.h"
27
28 #include <string.h>
29
30 static gboolean
31 _print_position (GstElement * pipeline)
32 {
33   gint64 position = 0, duration = -1;
34
35   if (pipeline) {
36     gst_element_query_position (GST_ELEMENT (pipeline), GST_FORMAT_TIME,
37         &position);
38     gst_element_query_duration (GST_ELEMENT (pipeline), GST_FORMAT_TIME,
39         &duration);
40
41     gst_print ("<position: %" GST_TIME_FORMAT " duration: %" GST_TIME_FORMAT
42         "/>\r", GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
43   }
44
45   return TRUE;
46 }
47
48 #ifdef HAVE_GST_VALIDATE
49 #include <gst/validate/gst-validate-scenario.h>
50 #include <gst/validate/validate.h>
51 #include <gst/validate/gst-validate-utils.h>
52 #include <gst/validate/gst-validate-element-monitor.h>
53 #include <gst/validate/gst-validate-bin-monitor.h>
54
55 #define MONITOR_ON_PIPELINE "validate-monitor"
56 #define RUNNER_ON_PIPELINE "runner-monitor"
57 #define WRONG_DECODER_ADDED g_quark_from_static_string ("ges::wrong-decoder-added")
58
59 static void
60 _validate_report_added_cb (GstValidateRunner * runner,
61     GstValidateReport * report, GstPipeline * pipeline)
62 {
63   if (report->level == GST_VALIDATE_REPORT_LEVEL_CRITICAL) {
64     GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
65         GST_DEBUG_GRAPH_SHOW_ALL, "ges-launch--validate-error");
66   }
67 }
68
69 static void
70 bin_element_added (GstTracer * runner, GstClockTime ts,
71     GstBin * bin, GstElement * element, gboolean result)
72 {
73   GstObject *parent;
74   GstValidateElementMonitor *monitor =
75       g_object_get_data (G_OBJECT (element), "validate-monitor");
76
77   if (!monitor)
78     return;
79
80   if (!monitor->is_decoder)
81     return;
82
83   parent = gst_object_get_parent (GST_OBJECT (element));
84   do {
85     if (GES_IS_TRACK (parent)) {
86       GstElementClass *klass = GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS (element));
87       const gchar *klassname =
88           gst_element_class_get_metadata (klass, GST_ELEMENT_METADATA_KLASS);
89
90       if (GES_IS_AUDIO_TRACK (parent) && strstr (klassname, "Audio") == NULL) {
91         GST_VALIDATE_REPORT (monitor, WRONG_DECODER_ADDED,
92             "Adding non audio decoder %s in audio track %s.",
93             GST_OBJECT_NAME (element), GST_OBJECT_NAME (parent));
94       } else if (GES_IS_VIDEO_TRACK (parent)
95           && strstr (klassname, "Video") == NULL
96           && strstr (klassname, "Image") == NULL) {
97         GST_VALIDATE_REPORT (monitor, WRONG_DECODER_ADDED,
98             "Adding non video decoder %s in video track %s.",
99             GST_OBJECT_NAME (element), GST_OBJECT_NAME (parent));
100
101       }
102       gst_object_unref (parent);
103       break;
104     }
105
106     gst_object_unref (parent);
107     parent = gst_object_get_parent (parent);
108   } while (parent);
109 }
110
111 static void
112 ges_validate_register_issues (void)
113 {
114   gst_validate_issue_register (gst_validate_issue_new (WRONG_DECODER_ADDED,
115           "Wrong decoder type added to track.",
116           "In a specific track type we should never create decoders"
117           " for some other types (No audio decoder should be added"
118           " in a Video track).", GST_VALIDATE_REPORT_LEVEL_CRITICAL));
119 }
120
121 gboolean
122 ges_validate_activate (GstPipeline * pipeline, GESLauncher * launcher,
123     GESLauncherParsedOptions * opts)
124 {
125   GstValidateRunner *runner = NULL;
126   GstValidateMonitor *monitor = NULL;
127
128   if (!opts->enable_validate) {
129     opts->needs_set_state = TRUE;
130     g_object_set_data (G_OBJECT (pipeline), "pposition-id",
131         GUINT_TO_POINTER (g_timeout_add (200,
132                 (GSourceFunc) _print_position, pipeline)));
133     return TRUE;
134   }
135
136   gst_validate_init_debug ();
137
138   if (opts->testfile) {
139     if (opts->scenario)
140       g_error ("Can not specify scenario and testfile at the same time");
141     gst_validate_setup_test_file (opts->testfile, opts->mute);
142   } else if (opts->scenario) {
143     if (g_strcmp0 (opts->scenario, "none")) {
144       gchar *scenario_name =
145           g_strconcat (opts->scenario, "->gespipeline*", NULL);
146       g_setenv ("GST_VALIDATE_SCENARIO", scenario_name, TRUE);
147       g_free (scenario_name);
148     }
149   }
150
151   ges_validate_register_action_types ();
152   ges_validate_register_issues ();
153
154   runner = gst_validate_runner_new ();
155   gst_tracing_register_hook (GST_TRACER (runner), "bin-add-post",
156       G_CALLBACK (bin_element_added));
157   g_signal_connect (runner, "report-added",
158       G_CALLBACK (_validate_report_added_cb), pipeline);
159   monitor =
160       gst_validate_monitor_factory_create (GST_OBJECT_CAST (pipeline), runner,
161       NULL);
162   if (GST_VALIDATE_BIN_MONITOR (monitor)->scenario) {
163     GstStructure *metas =
164         GST_VALIDATE_BIN_MONITOR (monitor)->scenario->description;
165
166     if (metas) {
167       gchar **ges_options = gst_validate_utils_get_strv (metas, "ges-options");
168       if (!ges_options)
169         ges_options = gst_validate_utils_get_strv (metas, "args");
170
171       gst_structure_get_boolean (metas, "ignore-eos", &opts->ignore_eos);
172       if (ges_options) {
173         gint i;
174         gchar **ges_options_full =
175             g_new0 (gchar *, g_strv_length (ges_options) + 2);
176
177         ges_options_full[0] = g_strdup ("something");
178         for (i = 0; ges_options[i]; i++)
179           ges_options_full[i + 1] = g_strdup (ges_options[i]);
180
181         ges_launcher_parse_options (launcher, &ges_options_full, NULL, NULL,
182             NULL);
183         opts->sanitized_timeline =
184             sanitize_timeline_description (ges_options_full, opts);
185         g_strfreev (ges_options_full);
186         g_strfreev (ges_options);
187       }
188     }
189   }
190
191   gst_validate_reporter_set_handle_g_logs (GST_VALIDATE_REPORTER (monitor));
192
193   g_object_get (monitor, "handles-states", &opts->needs_set_state, NULL);
194   opts->needs_set_state = !opts->needs_set_state;
195   g_object_set_data (G_OBJECT (pipeline), MONITOR_ON_PIPELINE, monitor);
196   g_object_set_data (G_OBJECT (pipeline), RUNNER_ON_PIPELINE, runner);
197
198   return TRUE;
199 }
200
201 gint
202 ges_validate_clean (GstPipeline * pipeline)
203 {
204   gint res = 0;
205   GstValidateMonitor *monitor =
206       g_object_get_data (G_OBJECT (pipeline), MONITOR_ON_PIPELINE);
207   GstValidateRunner *runner =
208       g_object_get_data (G_OBJECT (pipeline), RUNNER_ON_PIPELINE);
209
210   if (runner)
211     res = gst_validate_runner_exit (runner, TRUE);
212   else
213     g_source_remove (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pipeline),
214                 "pposition-id")));
215
216   gst_object_unref (pipeline);
217   if (runner)
218     gst_object_unref (runner);
219   if (monitor)
220     gst_object_unref (monitor);
221
222   return res;
223 }
224
225 void
226 ges_validate_handle_request_state_change (GstMessage * message,
227     GApplication * application)
228 {
229   GstState state;
230
231   gst_message_parse_request_state (message, &state);
232
233   if (GST_IS_VALIDATE_SCENARIO (GST_MESSAGE_SRC (message))
234       && state == GST_STATE_NULL) {
235     gst_validate_printf (GST_MESSAGE_SRC (message),
236         "State change request NULL, " "quitting application\n");
237     g_application_quit (application);
238   }
239 }
240
241 gint
242 ges_validate_print_action_types (const gchar ** types, gint num_types)
243 {
244   ges_validate_register_action_types ();
245
246   if (!gst_validate_print_action_types (types, num_types)) {
247     GST_ERROR ("Could not print all wanted types");
248     return 1;
249   }
250
251   return 0;
252 }
253
254 #else
255 gboolean
256 ges_validate_activate (GstPipeline * pipeline, GESLauncher * launcher,
257     GESLauncherParsedOptions * opts)
258 {
259   if (opts->testfile) {
260     GST_WARNING ("Trying to run testfile %s, but gst-validate not supported",
261         opts->testfile);
262
263     return FALSE;
264   }
265
266   if (opts->scenario) {
267     GST_WARNING ("Trying to run scenario %s, but gst-validate not supported",
268         opts->scenario);
269
270     return FALSE;
271   }
272
273   g_object_set_data (G_OBJECT (pipeline), "pposition-id",
274       GUINT_TO_POINTER (g_timeout_add (200,
275               (GSourceFunc) _print_position, pipeline)));
276
277   opts->needs_set_state = TRUE;
278
279   return TRUE;
280 }
281
282 gint
283 ges_validate_clean (GstPipeline * pipeline)
284 {
285   g_source_remove (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pipeline),
286               "pposition-id")));
287
288   gst_object_unref (pipeline);
289
290   return 0;
291 }
292
293 void
294 ges_validate_handle_request_state_change (GstMessage * message,
295     GApplication * application)
296 {
297   return;
298 }
299
300 gint
301 ges_validate_print_action_types (const gchar ** types, gint num_types)
302 {
303   return 0;
304 }
305
306 #endif