Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / multifile.c
1 /* GStreamer unit test for multifile plugin
2  *
3  * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
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 <glib/gstdio.h>
26 #include <unistd.h>
27
28 #include <gst/check/gstcheck.h>
29 #include <gst/video/video.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 static void
34 run_pipeline (GstElement * pipeline)
35 {
36   gst_element_set_state (pipeline, GST_STATE_PAUSED);
37   gst_element_get_state (pipeline, NULL, NULL, -1);
38   gst_element_set_state (pipeline, GST_STATE_PLAYING);
39   /* FIXME too lazy */
40   g_usleep (1000000);
41   gst_element_set_state (pipeline, GST_STATE_NULL);
42 }
43
44 #if !GLIB_CHECK_VERSION(2,30,0)
45 static gchar *
46 g_mkdtemp (gchar * template)
47 {
48   gchar *tmpdir;
49
50   tmpdir = mkdtemp (template);
51   if (tmpdir == NULL) {
52     g_free (template);
53   }
54   return tmpdir;
55 }
56 #endif
57
58 GST_START_TEST (test_multifilesink_key_frame)
59 {
60   GstElement *pipeline;
61   GstElement *mfs;
62   int i;
63   const gchar *tmpdir;
64   gchar *my_tmpdir;
65   gchar *template;
66   gchar *mfs_pattern;
67
68   tmpdir = g_get_tmp_dir ();
69   template = g_build_filename (tmpdir, "multifile-test-XXXXXX", NULL);
70   my_tmpdir = g_mkdtemp (template);
71   fail_if (my_tmpdir == NULL);
72
73   pipeline =
74       gst_parse_launch
75       ("videotestsrc num-buffers=10 ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240 ! multifilesink name=mfs",
76       NULL);
77   fail_if (pipeline == NULL);
78   mfs = gst_bin_get_by_name (GST_BIN (pipeline), "mfs");
79   fail_if (mfs == NULL);
80   mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
81   g_object_set (G_OBJECT (mfs), "location", mfs_pattern, NULL);
82   g_object_unref (mfs);
83   run_pipeline (pipeline);
84   gst_object_unref (pipeline);
85
86   for (i = 0; i < 10; i++) {
87     char *s;
88
89     s = g_strdup_printf (mfs_pattern, i);
90     fail_if (g_remove (s) != 0);
91     g_free (s);
92   }
93   fail_if (g_remove (my_tmpdir) != 0);
94
95   g_free (mfs_pattern);
96   g_free (my_tmpdir);
97 }
98
99 GST_END_TEST;
100
101 GST_START_TEST (test_multifilesink_max_files)
102 {
103   GstElement *pipeline;
104   GstElement *mfs;
105   int i;
106   const gchar *tmpdir;
107   gchar *my_tmpdir;
108   gchar *template;
109   gchar *mfs_pattern;
110
111   tmpdir = g_get_tmp_dir ();
112   template = g_build_filename (tmpdir, "multifile-test-XXXXXX", NULL);
113   my_tmpdir = g_mkdtemp (template);
114   fail_if (my_tmpdir == NULL);
115
116   pipeline =
117       gst_parse_launch
118       ("videotestsrc num-buffers=10 ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240 ! multifilesink name=mfs",
119       NULL);
120   fail_if (pipeline == NULL);
121   mfs = gst_bin_get_by_name (GST_BIN (pipeline), "mfs");
122   fail_if (mfs == NULL);
123   mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
124   g_object_set (G_OBJECT (mfs), "location", mfs_pattern, "max-files", 3, NULL);
125   g_object_unref (mfs);
126   run_pipeline (pipeline);
127   gst_object_unref (pipeline);
128
129   for (i = 0; i < 7; i++) {
130     char *s;
131
132     s = g_strdup_printf (mfs_pattern, i);
133     fail_unless (g_remove (s) != 0);
134     g_free (s);
135   }
136   for (i = 7; i < 10; i++) {
137     char *s;
138
139     s = g_strdup_printf (mfs_pattern, i);
140     fail_if (g_remove (s) != 0);
141     g_free (s);
142   }
143   fail_if (g_remove (my_tmpdir) != 0);
144
145   g_free (mfs_pattern);
146   g_free (my_tmpdir);
147 }
148
149 GST_END_TEST;
150
151 GST_START_TEST (test_multifilesink_key_unit)
152 {
153   GstElement *mfs;
154   int i;
155   const gchar *tmpdir;
156   gchar *my_tmpdir;
157   gchar *template;
158   gchar *mfs_pattern;
159   GstBuffer *buf;
160   GstPad *sink;
161
162   tmpdir = g_get_tmp_dir ();
163   template = g_build_filename (tmpdir, "multifile-test-XXXXXX", NULL);
164   my_tmpdir = g_mkdtemp (template);
165   fail_if (my_tmpdir == NULL);
166
167   mfs = gst_element_factory_make ("multifilesink", NULL);
168   fail_if (mfs == NULL);
169   mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
170   g_object_set (G_OBJECT (mfs), "location", mfs_pattern, "next-file", 3, NULL);
171   fail_if (gst_element_set_state (mfs,
172           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
173
174   sink = gst_element_get_static_pad (mfs, "sink");
175   buf = gst_buffer_new_and_alloc (4);
176
177   memcpy (GST_BUFFER_DATA (buf), "foo", 4);
178   fail_if (gst_pad_chain (sink, gst_buffer_ref (buf)) != GST_FLOW_OK);
179
180   memcpy (GST_BUFFER_DATA (buf), "bar", 4);
181   fail_if (gst_pad_chain (sink, gst_buffer_ref (buf)) != GST_FLOW_OK);
182
183   fail_unless (gst_pad_send_event (sink,
184           gst_video_event_new_downstream_force_key_unit (GST_CLOCK_TIME_NONE,
185               GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, TRUE, 1)));
186
187   memcpy (GST_BUFFER_DATA (buf), "baz", 4);
188   fail_if (gst_pad_chain (sink, buf) != GST_FLOW_OK);
189
190   fail_if (gst_element_set_state (mfs,
191           GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE);
192
193   for (i = 0; i < 2; i++) {
194     char *s;
195
196     s = g_strdup_printf (mfs_pattern, i);
197     fail_if (g_remove (s) != 0);
198     g_free (s);
199   }
200   fail_if (g_remove (my_tmpdir) != 0);
201
202   g_free (mfs_pattern);
203   g_free (my_tmpdir);
204   gst_object_unref (sink);
205   gst_object_unref (mfs);
206 }
207
208 GST_END_TEST;
209
210 GST_START_TEST (test_multifilesrc)
211 {
212   GstElement *pipeline;
213   GstElement *mfs;
214   int i;
215   const gchar *tmpdir;
216   gchar *my_tmpdir;
217   gchar *template;
218   gchar *mfs_pattern;
219
220   tmpdir = g_get_tmp_dir ();
221   template = g_build_filename (tmpdir, "multifile-test-XXXXXX", NULL);
222   my_tmpdir = g_mkdtemp (template);
223   fail_if (my_tmpdir == NULL);
224
225   pipeline =
226       gst_parse_launch
227       ("videotestsrc num-buffers=10 ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240 ! multifilesink name=mfs",
228       NULL);
229   fail_if (pipeline == NULL);
230   mfs = gst_bin_get_by_name (GST_BIN (pipeline), "mfs");
231   fail_if (mfs == NULL);
232   mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
233   g_object_set (G_OBJECT (mfs), "location", mfs_pattern, NULL);
234   g_free (mfs_pattern);
235   g_object_unref (mfs);
236   run_pipeline (pipeline);
237   gst_object_unref (pipeline);
238
239   pipeline =
240       gst_parse_launch
241       ("multifilesrc ! video/x-raw-yuv,format=(fourcc)I420,width=320,height=240,framerate=10/1 ! fakesink",
242       NULL);
243   fail_if (pipeline == NULL);
244   mfs = gst_bin_get_by_name (GST_BIN (pipeline), "multifilesrc0");
245   fail_if (mfs == NULL);
246   mfs_pattern = g_build_filename (my_tmpdir, "%05d", NULL);
247   g_object_set (G_OBJECT (mfs), "location", mfs_pattern, NULL);
248   g_object_unref (mfs);
249   run_pipeline (pipeline);
250   gst_object_unref (pipeline);
251
252   for (i = 0; i < 10; i++) {
253     char *s;
254
255     s = g_strdup_printf (mfs_pattern, i);
256     fail_if (g_remove (s) != 0);
257     g_free (s);
258   }
259   fail_if (g_remove (my_tmpdir) != 0);
260
261   g_free (mfs_pattern);
262   g_free (my_tmpdir);
263 }
264
265 GST_END_TEST;
266
267 static Suite *
268 libvisual_suite (void)
269 {
270   Suite *s = suite_create ("multifile");
271   TCase *tc_chain = tcase_create ("general");
272
273   suite_add_tcase (s, tc_chain);
274
275   tcase_add_test (tc_chain, test_multifilesink_key_frame);
276   tcase_add_test (tc_chain, test_multifilesink_max_files);
277   tcase_add_test (tc_chain, test_multifilesink_key_unit);
278   tcase_add_test (tc_chain, test_multifilesrc);
279
280   return s;
281 }
282
283 GST_CHECK_MAIN (libvisual);