fd0ff4d6ff9921eff369bed2975035246d1fa31e
[platform/upstream/gst-editing-services.git] / tests / check / ges / test-utils.c
1 /**
2  * Gstreamer Editing Services
3  *
4  * Copyright (C) <2012> Thibault Saunier <thibault.saunier@collabora.com>
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   gchar *uri;
35   GFile *cfile, *fdir, *f_audio_only;
36
37   cfile = g_file_new_for_path (__FILE__);
38   fdir = g_file_get_parent (cfile);
39
40   f_audio_only = g_file_get_child (fdir, "audio_only.ogg");
41   uri = g_file_get_uri (f_audio_only);
42
43   gst_object_unref (cfile);
44   gst_object_unref (fdir);
45   gst_object_unref (f_audio_only);
46
47   return uri;
48 }
49
50 gchar *
51 ges_test_get_audio_video_uri (void)
52 {
53   return ges_test_file_uri ("audio_video.ogg");
54 }
55
56 gchar *
57 ges_test_get_image_uri (void)
58 {
59   return ges_test_file_uri ("image.png");
60 }
61
62 gchar *
63 ges_test_file_uri (const gchar * filename)
64 {
65   gchar *path, *uri;
66
67   path = g_build_filename (GES_TEST_FILES_PATH, filename, NULL);
68   uri = gst_filename_to_uri (path, NULL);
69
70   g_free (path);
71
72   return uri;
73 }
74
75 GESPipeline *
76 ges_test_create_pipeline (GESTimeline * timeline)
77 {
78   GESPipeline *pipeline;
79
80   pipeline = ges_pipeline_new ();
81   fail_unless (ges_pipeline_set_timeline (pipeline, timeline));
82
83   g_object_set (pipeline, "audio-sink", gst_element_factory_make ("fakesink",
84           "test-audiofakesink"), "video-sink",
85       gst_element_factory_make ("fakesink", "test-videofakesink"), NULL);
86
87   return pipeline;
88 }
89
90 gchar *
91 ges_test_file_name (const gchar * filename)
92 {
93   return g_strjoin ("/", "file:/", g_get_current_dir (), filename, NULL);
94 }
95
96 gboolean
97 ges_generate_test_file_audio_video (const gchar * filedest,
98     const gchar * audio_enc,
99     const gchar * video_enc,
100     const gchar * mux, const gchar * video_pattern, const gchar * audio_wave)
101 {
102   GError *error = NULL;
103   GstElement *pipeline;
104   GstBus *bus;
105   GstMessage *message;
106   gchar *pipeline_str;
107   gboolean done = FALSE;
108   gboolean ret = FALSE;
109
110   if (g_file_test (filedest, G_FILE_TEST_EXISTS)) {
111     GST_INFO ("The file %s already existed.", filedest);
112     return TRUE;
113   }
114
115   pipeline_str = g_strdup_printf ("audiotestsrc num-buffers=430 wave=%s "
116       "%c %s ! %s name=m ! filesink location= %s/%s "
117       "videotestsrc pattern=%s num-buffers=300 ! %s ! m.",
118       audio_wave,
119       audio_enc ? '!' : ' ',
120       audio_enc ? audio_enc : "",
121       mux, g_get_current_dir (), filedest, video_pattern, video_enc);
122
123   pipeline = gst_parse_launch (pipeline_str, &error);
124
125   if (pipeline == NULL)
126     return FALSE;
127
128   g_free (pipeline_str);
129
130   bus = gst_element_get_bus (GST_ELEMENT (pipeline));
131   gst_bus_add_signal_watch (bus);
132
133   gst_element_set_state (pipeline, GST_STATE_PLAYING);
134
135   while (!done) {
136     message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_CLOCK_TIME_NONE);
137     if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS) {
138       done = TRUE;
139       ret = TRUE;
140     } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
141       gchar *debug = NULL;
142       GError *err = NULL;
143
144       gst_message_parse_error (message, &err, &debug);
145       done = TRUE;
146       ret = FALSE;
147       GST_ERROR ("Got error %s from %s fron the bus while generation: %s"
148           "debug infos: %s", GST_OBJECT_NAME (message->src), err->message,
149           debug ? debug : "none", filedest);
150       g_clear_error (&err);
151       g_free (debug);
152     }
153   }
154
155   gst_bus_remove_signal_watch (bus);
156   gst_object_unref (bus);
157
158   gst_element_set_state (pipeline, GST_STATE_NULL);
159   gst_object_unref (pipeline);
160
161   return ret;
162 }
163
164 static void
165 weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
166 {
167   destroyed->destroyed = TRUE;
168 }
169
170 void
171 check_destroyed (GObject * object_to_unref, GObject * first_object, ...)
172 {
173   GObject *object;
174   GList *objs = NULL, *tmp;
175   DestroyedObjectStruct *destroyed = g_slice_new0 (DestroyedObjectStruct);
176
177   destroyed->object = object_to_unref;
178   g_object_weak_ref (object_to_unref, (GWeakNotify) weak_notify, destroyed);
179   objs = g_list_prepend (objs, destroyed);
180
181   if (first_object) {
182     va_list varargs;
183
184     object = first_object;
185
186     va_start (varargs, first_object);
187     while (object) {
188       destroyed = g_slice_new0 (DestroyedObjectStruct);
189       destroyed->object = object;
190       g_object_weak_ref (object, (GWeakNotify) weak_notify, destroyed);
191       objs = g_list_prepend (objs, destroyed);
192       object = va_arg (varargs, GObject *);
193     }
194     va_end (varargs);
195   }
196   gst_object_unref (object_to_unref);
197
198   for (tmp = objs; tmp; tmp = tmp->next) {
199     fail_unless (((DestroyedObjectStruct *) tmp->data)->destroyed == TRUE,
200         "%p is not destroyed", ((DestroyedObjectStruct *) tmp->data)->object);
201     g_slice_free (DestroyedObjectStruct, tmp->data);
202   }
203   g_list_free (objs);
204
205 }
206
207 static gboolean
208 my_bus_callback (GstBus * bus, GstMessage * message, GMainLoop * loop)
209 {
210   switch (GST_MESSAGE_TYPE (message)) {
211     case GST_MESSAGE_ERROR:{
212       GError *err;
213       gchar *debug;
214
215       gst_message_parse_error (message, &err, &debug);
216
217       g_assert_no_error (err);
218       g_error_free (err);
219       g_free (debug);
220       g_main_loop_quit (loop);
221       break;
222     }
223     case GST_MESSAGE_EOS:
224       GST_INFO ("EOS\n");
225       g_main_loop_quit (loop);
226       break;
227     default:
228       /* unhandled message */
229       break;
230   }
231   return TRUE;
232 }
233
234 gboolean
235 play_timeline (GESTimeline * timeline)
236 {
237   GstBus *bus;
238   GESPipeline *pipeline;
239   GMainLoop *loop = g_main_loop_new (NULL, FALSE);
240
241   ges_timeline_commit (timeline);
242   pipeline = ges_pipeline_new ();
243
244   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
245   gst_bus_add_watch (bus, (GstBusFunc) my_bus_callback, loop);
246   gst_object_unref (bus);
247
248   ges_pipeline_set_timeline (pipeline, timeline);
249   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
250   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, -1);
251
252   g_main_loop_run (loop);
253
254   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
255   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, -1);
256
257   gst_object_unref (pipeline);
258
259   return TRUE;
260 }
261
262 gchar *
263 ges_test_get_tmp_uri (const gchar * filename)
264 {
265   gchar *location, *uri;
266
267   location = g_build_filename (g_get_tmp_dir (), filename, NULL);
268
269   uri = g_strconcat ("file://", location, NULL);
270   g_free (location);
271
272   return uri;
273 }