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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <errno.h>
33
34 #include <gst/check/gstcheck.h>
35
36 static gboolean have_eos = FALSE;
37
38 static GstPad *mysinkpad;
39
40 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
41 GST_PAD_SINK,
42 GST_PAD_ALWAYS,
43 GST_STATIC_CAPS_ANY);
44
45 static gboolean
event_func(GstPad * pad,GstObject * parent,GstEvent * event)46 event_func (GstPad * pad, GstObject * parent, GstEvent * event)
47 {
48 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
49 have_eos = TRUE;
50 }
51
52 gst_event_unref (event);
53 return TRUE;
54 }
55
56 static GstElement *
setup_fdsrc(void)57 setup_fdsrc (void)
58 {
59 GstElement *fdsrc;
60
61 GST_DEBUG ("setup_fdsrc");
62 fdsrc = gst_check_setup_element ("fdsrc");
63 mysinkpad = gst_check_setup_sink_pad (fdsrc, &sinktemplate);
64 gst_pad_set_event_function (mysinkpad, event_func);
65 gst_pad_set_active (mysinkpad, TRUE);
66 return fdsrc;
67 }
68
69 static void
cleanup_fdsrc(GstElement * fdsrc)70 cleanup_fdsrc (GstElement * fdsrc)
71 {
72 gst_pad_set_active (mysinkpad, FALSE);
73 gst_check_teardown_sink_pad (fdsrc);
74 gst_check_teardown_element (fdsrc);
75 }
76
GST_START_TEST(test_num_buffers)77 GST_START_TEST (test_num_buffers)
78 {
79 GstElement *src;
80 gint pipe_fd[2];
81 gchar data[4096];
82
83 #ifndef G_OS_WIN32
84 fail_if (pipe (pipe_fd) < 0);
85 #else
86 fail_if (_pipe (pipe_fd, 2048, _O_BINARY) < 0);
87 #endif
88
89 src = setup_fdsrc ();
90 g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
91 g_object_set (G_OBJECT (src), "fd", pipe_fd[0], NULL);
92 fail_unless (gst_element_set_state (src,
93 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
94 "could not set to playing");
95
96 #if defined (G_OS_UNIX) && defined (O_NONBLOCK)
97 {
98 int flags;
99
100 flags = fcntl (pipe_fd[1], F_GETFL, 0);
101 fcntl (pipe_fd[1], F_SETFL, flags | O_NONBLOCK);
102 }
103 #endif
104
105 memset (data, 0, 4096);
106 while (!have_eos) {
107 int ret = write (pipe_fd[1], data, 4096);
108 fail_if (ret < 0 && errno != EAGAIN);
109 g_usleep (100);
110 }
111
112 fail_unless (g_list_length (buffers) == 3);
113 fail_unless (gst_element_set_state (src,
114 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
115
116 /* cleanup */
117 cleanup_fdsrc (src);
118 close (pipe_fd[0]);
119 close (pipe_fd[1]);
120 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
121 g_list_free (buffers);
122 }
123
124 GST_END_TEST;
125
GST_START_TEST(test_nonseeking)126 GST_START_TEST (test_nonseeking)
127 {
128 GstElement *src;
129 GstQuery *seeking_query;
130 gint pipe_fd[2];
131 gchar data[4096];
132 gboolean seekable;
133
134 #ifndef G_OS_WIN32
135 fail_if (pipe (pipe_fd) < 0);
136 #else
137 fail_if (_pipe (pipe_fd, 2048, _O_BINARY) < 0);
138 #endif
139
140 src = setup_fdsrc ();
141 g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
142 g_object_set (G_OBJECT (src), "fd", pipe_fd[0], NULL);
143 fail_unless (gst_element_set_state (src,
144 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
145 "could not set to paused");
146
147 memset (data, 0, 4096);
148 fail_if (write (pipe_fd[1], data, 4096) < 0);
149
150 /* Test that fdsrc is non-seekable with a pipe */
151 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
152 != NULL);
153 fail_unless (gst_element_query (src, seeking_query) == TRUE);
154 gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
155 fail_unless (seekable == FALSE);
156 gst_query_unref (seeking_query);
157
158 fail_unless (gst_element_set_state (src,
159 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
160
161 /* cleanup */
162 cleanup_fdsrc (src);
163 close (pipe_fd[0]);
164 close (pipe_fd[1]);
165 }
166
167 GST_END_TEST;
168
GST_START_TEST(test_seeking)169 GST_START_TEST (test_seeking)
170 {
171 GstElement *src;
172 gint in_fd;
173 GstQuery *seeking_query;
174 gboolean seekable;
175
176 #ifndef TESTFILE
177 #error TESTFILE not defined
178 #endif
179 fail_if ((in_fd = open (TESTFILE, O_RDONLY)) < 0);
180 src = setup_fdsrc ();
181
182 g_object_set (G_OBJECT (src), "fd", in_fd, NULL);
183 fail_unless (gst_element_set_state (src,
184 GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
185 "could not set to paused");
186
187 /* Test that fdsrc is seekable with a file fd */
188 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
189 != NULL);
190 fail_unless (gst_element_query (src, seeking_query) == TRUE);
191 gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
192 fail_unless (seekable == TRUE);
193 gst_query_unref (seeking_query);
194
195 fail_unless (gst_element_set_state (src,
196 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
197
198 /* cleanup */
199 cleanup_fdsrc (src);
200 close (in_fd);
201 }
202
203 GST_END_TEST;
204
205 static Suite *
fdsrc_suite(void)206 fdsrc_suite (void)
207 {
208 Suite *s = suite_create ("fdsrc");
209 TCase *tc_chain = tcase_create ("general");
210
211 suite_add_tcase (s, tc_chain);
212 tcase_add_test (tc_chain, test_num_buffers);
213 tcase_add_test (tc_chain, test_nonseeking);
214 tcase_add_test (tc_chain, test_seeking);
215
216 return s;
217 }
218
219 GST_CHECK_MAIN (fdsrc);
220