And correct even more valid sparse warnings.
[platform/upstream/gstreamer.git] / tests / check / elements / fdsrc.c
1 /* GStreamer
2  *
3  * unit test for fdsrc
4  *
5  * Copyright (C) <2005> Jan Schmidt <thaytan at mad dot scientist dot com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include <gst/check/gstcheck.h>
29
30 static gboolean have_eos = FALSE;
31
32 static GstPad *mysinkpad;
33
34 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS_ANY);
38
39 static gboolean
40 event_func (GstPad * pad, GstEvent * event)
41 {
42   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
43     have_eos = TRUE;
44     gst_event_unref (event);
45     return TRUE;
46   }
47
48   gst_event_unref (event);
49   return FALSE;
50 }
51
52 static GstElement *
53 setup_fdsrc (void)
54 {
55   GstElement *fdsrc;
56
57   GST_DEBUG ("setup_fdsrc");
58   fdsrc = gst_check_setup_element ("fdsrc");
59   mysinkpad = gst_check_setup_sink_pad (fdsrc, &sinktemplate, NULL);
60   gst_pad_set_event_function (mysinkpad, event_func);
61   gst_pad_set_active (mysinkpad, TRUE);
62   return fdsrc;
63 }
64
65 static void
66 cleanup_fdsrc (GstElement * fdsrc)
67 {
68   gst_pad_set_active (mysinkpad, FALSE);
69   gst_check_teardown_sink_pad (fdsrc);
70   gst_check_teardown_element (fdsrc);
71 }
72
73 GST_START_TEST (test_num_buffers)
74 {
75   GstElement *src;
76   gint pipe_fd[2];
77   gchar data[4096];
78
79   fail_if (pipe (pipe_fd) < 0);
80
81   src = setup_fdsrc ();
82   g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
83   g_object_set (G_OBJECT (src), "fd", pipe_fd[0], NULL);
84   fail_unless (gst_element_set_state (src,
85           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
86       "could not set to playing");
87
88   memset (data, 0, 4096);
89   while (!have_eos) {
90     fail_if (write (pipe_fd[1], data, 4096) < 0);
91     g_usleep (100);
92   }
93
94   fail_unless (g_list_length (buffers) == 3);
95   fail_unless (gst_element_set_state (src,
96           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
97
98   /* cleanup */
99   cleanup_fdsrc (src);
100   close (pipe_fd[0]);
101   close (pipe_fd[1]);
102   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
103   g_list_free (buffers);
104 }
105
106 GST_END_TEST;
107
108 GST_START_TEST (test_nonseeking)
109 {
110   GstElement *src;
111   GstQuery *seeking_query;
112   gint pipe_fd[2];
113   gchar data[4096];
114   gboolean seekable;
115
116   fail_if (pipe (pipe_fd) < 0);
117
118   src = setup_fdsrc ();
119   g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
120   g_object_set (G_OBJECT (src), "fd", pipe_fd[0], NULL);
121   fail_unless (gst_element_set_state (src,
122           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
123       "could not set to paused");
124
125   memset (data, 0, 4096);
126   fail_if (write (pipe_fd[1], data, 4096) < 0);
127
128   /* Test that fdsrc is non-seekable with a pipe */
129   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
130       != NULL);
131   fail_unless (gst_element_query (src, seeking_query) == TRUE);
132   gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
133   fail_unless (seekable == FALSE);
134   gst_query_unref (seeking_query);
135
136   fail_unless (gst_element_set_state (src,
137           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
138
139   /* cleanup */
140   cleanup_fdsrc (src);
141   close (pipe_fd[0]);
142   close (pipe_fd[1]);
143 }
144
145 GST_END_TEST;
146
147 GST_START_TEST (test_seeking)
148 {
149   GstElement *src;
150   gint in_fd;
151   GstQuery *seeking_query;
152   gboolean seekable;
153
154 #ifndef TESTFILE
155 #error TESTFILE not defined
156 #endif
157   fail_if ((in_fd = open (TESTFILE, O_RDONLY)) < 0);
158   src = setup_fdsrc ();
159
160   g_object_set (G_OBJECT (src), "fd", in_fd, NULL);
161   fail_unless (gst_element_set_state (src,
162           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
163       "could not set to paused");
164
165   /* Test that fdsrc is seekable with a file fd */
166   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
167       != NULL);
168   fail_unless (gst_element_query (src, seeking_query) == TRUE);
169   gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
170   fail_unless (seekable == TRUE);
171   gst_query_unref (seeking_query);
172
173   fail_unless (gst_element_set_state (src,
174           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
175
176   /* cleanup */
177   cleanup_fdsrc (src);
178   close (in_fd);
179 }
180
181 GST_END_TEST;
182
183 static Suite *
184 fdsrc_suite (void)
185 {
186   Suite *s = suite_create ("fdsrc");
187   TCase *tc_chain = tcase_create ("general");
188
189   suite_add_tcase (s, tc_chain);
190   tcase_add_test (tc_chain, test_num_buffers);
191   tcase_add_test (tc_chain, test_nonseeking);
192   tcase_add_test (tc_chain, test_seeking);
193
194   return s;
195 }
196
197 GST_CHECK_MAIN (fdsrc);