tizen beta release
[profile/ivi/gst-openmax0.10.git] / tests / check_gstomx.c
1 /*
2  * Copyright (C) 2007-2009 Nokia Corporation.
3  *
4  * Author: Felipe Contreras <felipe.contreras@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation
9  * version 2.1 of the License.
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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 #define BUFFER_SIZE 0x1000
25 #define BUFFER_COUNT 0x100
26 #define FLUSH_AT 0x10
27
28 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
29     GST_PAD_SINK,
30     GST_PAD_ALWAYS,
31     GST_STATIC_CAPS_ANY);
32
33 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS_ANY);
37
38 /* some global vars, makes it easy as for the ones above */
39 static GMutex *eos_mutex;
40 static GCond *eos_cond;
41 static gboolean eos_arrived;
42
43 static gboolean
44 test_sink_event (GstPad * pad, GstEvent * event)
45 {
46
47   switch (GST_EVENT_TYPE (event)) {
48     case GST_EVENT_EOS:
49       g_mutex_lock (eos_mutex);
50       eos_arrived = TRUE;
51       g_cond_signal (eos_cond);
52       g_mutex_unlock (eos_mutex);
53       break;
54     default:
55       break;
56   }
57
58   return gst_pad_event_default (pad, event);
59 }
60
61 static void
62 helper (gboolean flush)
63 {
64   GstElement *filter;
65   GstBus *bus;
66   GstPad *mysrcpad;
67   GstPad *mysinkpad;
68
69   /* init */
70   filter = gst_check_setup_element ("omx_dummy");
71   mysrcpad = gst_check_setup_src_pad (filter, &srctemplate, NULL);
72   mysinkpad = gst_check_setup_sink_pad (filter, &sinktemplate, NULL);
73
74   gst_pad_set_active (mysrcpad, TRUE);
75   gst_pad_set_active (mysinkpad, TRUE);
76
77   /* need to know when we are eos */
78   gst_pad_set_event_function (mysinkpad, test_sink_event);
79
80   /* and notify the test run */
81   eos_mutex = g_mutex_new ();
82   eos_cond = g_cond_new ();
83   eos_arrived = FALSE;
84
85   /* start */
86
87   fail_unless_equals_int (gst_element_set_state (filter, GST_STATE_PLAYING),
88       GST_STATE_CHANGE_SUCCESS);
89
90   bus = gst_bus_new ();
91
92   gst_element_set_bus (filter, bus);
93
94   /* send buffers in order */
95   {
96     guint i;
97     for (i = 0; i < BUFFER_COUNT; i++) {
98       GstBuffer *inbuffer;
99       inbuffer = gst_buffer_new_and_alloc (BUFFER_SIZE);
100       GST_BUFFER_DATA (inbuffer)[0] = i;
101       ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
102
103       fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
104
105       if (flush && i % FLUSH_AT == 0) {
106         gst_pad_push_event (mysrcpad, gst_event_new_flush_start ());
107         gst_pad_push_event (mysrcpad, gst_event_new_flush_stop ());
108         i += FLUSH_AT;
109       }
110     }
111   }
112
113   {
114     GstMessage *message;
115
116     /* make sure there's no error on the bus */
117     message = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
118     fail_if (message);
119   }
120
121   gst_pad_push_event (mysrcpad, gst_event_new_eos ());
122   /* need to wait a bit to make sure src pad task digested all and sent eos */
123   g_mutex_lock (eos_mutex);
124   while (!eos_arrived)
125     g_cond_wait (eos_cond, eos_mutex);
126   g_mutex_unlock (eos_mutex);
127
128   /* check the order of the buffers */
129   if (!flush) {
130     GList *cur;
131     guint i;
132     for (cur = buffers, i = 0; cur; cur = g_list_next (cur), i++) {
133       GstBuffer *buffer;
134       buffer = cur->data;
135       fail_unless (GST_BUFFER_DATA (buffer)[0] == i);
136     }
137     fail_unless (i == BUFFER_COUNT);
138   }
139
140   /* cleanup */
141   gst_bus_set_flushing (bus, TRUE);
142   gst_element_set_bus (filter, NULL);
143   gst_object_unref (GST_OBJECT (bus));
144   gst_check_drop_buffers ();
145
146   /* deinit */
147   gst_element_set_state (filter, GST_STATE_NULL);
148
149   gst_pad_set_active (mysrcpad, FALSE);
150   gst_pad_set_active (mysinkpad, FALSE);
151   gst_check_teardown_src_pad (filter);
152   gst_check_teardown_sink_pad (filter);
153   gst_check_teardown_element (filter);
154
155   g_mutex_free (eos_mutex);
156   g_cond_free (eos_cond);
157 }
158
159 GST_START_TEST (test_flush)
160 {
161   helper (TRUE);
162 }
163
164 GST_END_TEST
165 GST_START_TEST (test_basic)
166 {
167   helper (FALSE);
168 }
169
170 GST_END_TEST static Suite *
171 gstomx_suite (void)
172 {
173   Suite *s = suite_create ("gstomx");
174   TCase *tc_chain = tcase_create ("general");
175
176   tcase_set_timeout (tc_chain, 10);
177   tcase_add_test (tc_chain, test_basic);
178   tcase_add_test (tc_chain, test_flush);
179   suite_add_tcase (s, tc_chain);
180
181   return s;
182 }
183
184 GST_CHECK_MAIN (gstomx);