Debian Codegraph @

tests/check/ges/test-utils.c

raw
1 /**
2 * Gstreamer Editing Services
3 *
4 * Copyright (C) <2012> Thibault Saunier <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #include "test-utils.h"
23 #include <gio/gio.h>
24
25 typedef struct _DestroyedObjectStruct
26 {
27 GObject *object;
28 gboolean destroyed;
29 } DestroyedObjectStruct;
30
31 gchar *
32 ges_test_get_audio_only_uri (void)
33 {
34 return ges_test_file_uri ("audio_only.ogg");
35 }
36
37 gchar *
38 ges_test_get_audio_video_uri (void)
39 {
40 return ges_test_file_uri ("audio_video.ogg");
41 }
42
43 gchar *
44 ges_test_get_image_uri (void)
45 {
46 return ges_test_file_uri ("image.png");
47 }
48
49 gchar *
50 ges_test_file_uri (const gchar * filename)
51 {
52 gchar *path, *uri;
53
54 path = g_build_filename (GES_TEST_FILES_PATH, filename, NULL);
55 uri = gst_filename_to_uri (path, NULL);
56
57 g_free (path);
58
59 return uri;
60 }
61
62 GESPipeline *
63 ges_test_create_pipeline (GESTimeline * timeline)
64 {
65 GESPipeline *pipeline;
66
67 pipeline = ges_pipeline_new ();
68 fail_unless (ges_pipeline_set_timeline (pipeline, timeline));
69
70 g_object_set (pipeline, "audio-sink",
71 gst_element_factory_make ("fakeaudiosink", "test-audiofakesink"),
72 "video-sink", gst_element_factory_make ("fakevideosink",
73 "test-videofakesink"), NULL);
74
75 return pipeline;
76 }
77
78 gchar *
79 ges_test_file_name (const gchar * filename)
80 {
81 return g_strjoin ("/", "file:/", g_get_current_dir (), filename, NULL);
82 }
83
84 gboolean
85 ges_generate_test_file_audio_video (const gchar * filedest,
86 const gchar * audio_enc,
87 const gchar * video_enc,
88 const gchar * mux, const gchar * video_pattern, const gchar * audio_wave)
89 {
90 GError *error = NULL;
91 GstElement *pipeline;
92 GstBus *bus;
93 GstMessage *message;
94 gchar *pipeline_str;
95 gboolean done = FALSE;
96 gboolean ret = FALSE;
97
98 if (g_file_test (filedest, G_FILE_TEST_EXISTS)) {
99 GST_INFO ("The file %s already existed.", filedest);
100 return TRUE;
101 }
102
103 pipeline_str = g_strdup_printf ("audiotestsrc num-buffers=430 wave=%s "
104 "%c %s ! %s name=m ! filesink location= %s/%s "
105 "videotestsrc pattern=%s num-buffers=300 ! %s ! m.",
106 audio_wave,
107 audio_enc ? '!' : ' ',
108 audio_enc ? audio_enc : "",
109 mux, g_get_current_dir (), filedest, video_pattern, video_enc);
110
111 pipeline = gst_parse_launch (pipeline_str, &error);
112
113 if (pipeline == NULL)
114 return FALSE;
115
116 g_free (pipeline_str);
117
118 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
119 gst_bus_add_signal_watch (bus);
120
121 gst_element_set_state (pipeline, GST_STATE_PLAYING);
122
123 while (!done) {
124 message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_CLOCK_TIME_NONE);
125 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS) {
126 done = TRUE;
127 ret = TRUE;
128 } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
129 gchar *debug = NULL;
130 GError *err = NULL;
131
132 gst_message_parse_error (message, &err, &debug);
133 done = TRUE;
134 ret = FALSE;
135 GST_ERROR ("Got error %s from %s fron the bus while generation: %s"
136 "debug infos: %s", GST_OBJECT_NAME (message->src), err->message,
137 debug ? debug : "none", filedest);
138 g_clear_error (&err);
139 g_free (debug);
140 }
141 }
142
143 gst_bus_remove_signal_watch (bus);
144 gst_object_unref (bus);
145
146 gst_element_set_state (pipeline, GST_STATE_NULL);
147 gst_object_unref (pipeline);
148
149 return ret;
150 }
151
152 static void
153 weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
154 {
155 destroyed->destroyed = TRUE;
156 }
157
158 void
159 check_destroyed (GObject * object_to_unref, GObject * first_object, ...)
160 {
161 GObject *object;
162 GList *objs = NULL, *tmp;
163 DestroyedObjectStruct *destroyed = g_new0 (DestroyedObjectStruct, 1);
164
165 destroyed->object = object_to_unref;
166 g_object_weak_ref (object_to_unref, (GWeakNotify) weak_notify, destroyed);
167 objs = g_list_prepend (objs, destroyed);
168
169 if (first_object) {
170 va_list varargs;
171
172 object = first_object;
173
174 va_start (varargs, first_object);
175 while (object) {
176 destroyed = g_new0 (DestroyedObjectStruct, 1);
177 destroyed->object = object;
178 g_object_weak_ref (object, (GWeakNotify) weak_notify, destroyed);
179 objs = g_list_prepend (objs, destroyed);
180 object = va_arg (varargs, GObject *);
181 }
182 va_end (varargs);
183 }
184 gst_object_unref (object_to_unref);
185
186 for (tmp = objs; tmp; tmp = tmp->next) {
187 fail_unless (((DestroyedObjectStruct *) tmp->data)->destroyed == TRUE,
188 "%p is not destroyed", ((DestroyedObjectStruct *) tmp->data)->object);
189 g_free (tmp->data);
190 }
191 g_list_free (objs);
192
193 }
194
195 static gboolean
196 my_bus_callback (GstBus * bus, GstMessage * message, GMainLoop * loop)
197 {
198 switch (GST_MESSAGE_TYPE (message)) {
199 case GST_MESSAGE_ERROR:{
200 GError *err;
201 gchar *debug;
202
203 gst_message_parse_error (message, &err, &debug);
204
205 g_assert_no_error (err);
206 g_error_free (err);
207 g_free (debug);
208 g_main_loop_quit (loop);
209 break;
210 }
211 case GST_MESSAGE_EOS:
212 GST_INFO ("EOS\n");
213 g_main_loop_quit (loop);
214 break;
215 default:
216 /* unhandled message */
217 break;
218 }
219 return TRUE;
220 }
221
222 gboolean
223 play_timeline (GESTimeline * timeline)
224 {
225 GstBus *bus;
226 GESPipeline *pipeline;
227 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
228
229 ges_timeline_commit (timeline);
230 pipeline = ges_pipeline_new ();
231
232 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
233 gst_bus_add_watch (bus, (GstBusFunc) my_bus_callback, loop);
234 gst_object_unref (bus);
235
236 ges_pipeline_set_timeline (pipeline, timeline);
237 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
238 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, -1);
239
240 g_main_loop_run (loop);
241
242 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
243 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, -1);
244
245 gst_object_unref (pipeline);
246
247 return TRUE;
248 }
249
250 gchar *
251 ges_test_get_tmp_uri (const gchar * filename)
252 {
253 gchar *location, *uri;
254
255 location = g_build_filename (g_get_tmp_dir (), filename, NULL);
256
257 uri = g_strconcat ("file://", location, NULL);
258 g_free (location);
259
260 return uri;
261 }
262
263 void
264 print_timeline (GESTimeline * timeline)
265 {
266 GList *layer, *clip, *clips, *group;
267
268 gst_printerr
269 ("\n\n=========================== GESTimeline: %p ==================\n",
270 timeline);
271 for (layer = timeline->layers; layer; layer = layer->next) {
272 clips = ges_layer_get_clips (layer->data);
273
274 gst_printerr ("layer %04d: ", ges_layer_get_priority (layer->data));
275 for (clip = clips; clip; clip = clip->next) {
276 gst_printerr ("{ %s [ %" G_GUINT64_FORMAT "(%" G_GUINT64_FORMAT ") %"
277 G_GUINT64_FORMAT "] } ", GES_TIMELINE_ELEMENT_NAME (clip->data),
278 GES_TIMELINE_ELEMENT_START (clip->data),
279 GES_TIMELINE_ELEMENT_INPOINT (clip->data),
280 GES_TIMELINE_ELEMENT_END (clip->data));
281 }
282 if (layer->next)
283 gst_printerr ("\n--------------------------------------------------\n");
284
285 g_list_free_full (clips, gst_object_unref);
286 }
287
288 if (ges_timeline_get_groups (timeline)) {
289 gst_printerr ("\n--------------------------------------------------\n");
290 gst_printerr ("\nGROUPS:");
291 gst_printerr ("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
292 }
293
294 for (group = ges_timeline_get_groups (timeline); group; group = group->next) {
295 gst_printerr ("%" GES_FORMAT ": ", GES_ARGS (group->data));
296 for (clip = GES_CONTAINER_CHILDREN (group->data); clip; clip = clip->next)
297 gst_printerr ("[ %s ]", GES_TIMELINE_ELEMENT_NAME (clip->data));
298 }
299
300 gst_printerr
301 ("\n=====================================================================\n");
302 }
303
304 /* append the properties found in element to list, num_props should point
305 * to the current list length.
306 */
307 GParamSpec **
308 append_children_properties (GParamSpec ** list, GESTimelineElement * element,
309 guint * num_props)
310 {
311 guint i, num;
312 GParamSpec **props =
313 ges_timeline_element_list_children_properties (element, &num);
314 fail_unless (props);
315 list = g_realloc_n (list, num + *num_props, sizeof (GParamSpec *));
316
317 for (i = 0; i < num; i++)
318 list[*num_props + i] = props[i];
319
320 g_free (props);
321 *num_props += num;
322 return list;
323 }
324
325 void
326 free_children_properties (GParamSpec ** list, guint num_props)
327 {
328 guint i;
329 for (i = 0; i < num_props; i++)
330 g_param_spec_unref (list[i]);
331 g_free (list);
332 }
333
334 void
335 ges_test_check_object_props (const gchar * file, gint line, gpointer element,
336 GstClockTime start, GstClockTime inpoint, GstClockTime duration,
337 GstClockTime max_duration, gboolean check_max_duration)
338 {
339 if (_START (element) != start)
340 _ck_assert_failed (file, line, "Start check failed",
341 "%s start is %" GST_TIME_FORMAT " != %" GST_TIME_FORMAT,
342 GES_TIMELINE_ELEMENT_NAME (element),
343 GST_TIME_ARGS (_START (element)), GST_TIME_ARGS (start), NULL);
344
345 if (_INPOINT (element) != inpoint)
346 _ck_assert_failed (file, line, "Inpoint check failed",
347 "%s inpoint is %" GST_TIME_FORMAT " != %" GST_TIME_FORMAT,
348 GES_TIMELINE_ELEMENT_NAME (element),
349 GST_TIME_ARGS (_INPOINT (element)), GST_TIME_ARGS (inpoint), NULL);
350
351 if (_DURATION (element) != duration)
352 _ck_assert_failed (file, line, "Duration check failed",
353 "%s duration is %" GST_TIME_FORMAT " != %" GST_TIME_FORMAT,
354 GES_TIMELINE_ELEMENT_NAME (element),
355 GST_TIME_ARGS (_DURATION (element)), GST_TIME_ARGS (duration), NULL);
356
357 if (check_max_duration && _MAX_DURATION (element) != max_duration)
358 _ck_assert_failed (file, line, "Max duration check failed",
359 "%s max-duration is %" GST_TIME_FORMAT " != %" GST_TIME_FORMAT,
360 GES_TIMELINE_ELEMENT_NAME (element),
361 GST_TIME_ARGS (_MAX_DURATION (element)), GST_TIME_ARGS (max_duration),
362 NULL);
363
364 _mark_point (file, line);
365 }