Initialize Tizen 2.3
[framework/multimedia/gst-plugins-base0.10.git] / tests / check / pipelines / basetime.c
1 /* GStreamer
2  *
3  * unit test for audiotestsrc basetime handling
4  *
5  * Copyright (C) 2009 Maemo Multimedia <multimedia at maemo dot org>
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 #ifndef GST_DISABLE_PARSE
30
31 static GstClockTime old_ts = GST_CLOCK_TIME_NONE;
32
33 static gboolean
34 break_mainloop (gpointer data)
35 {
36   GMainLoop *loop;
37
38   loop = (GMainLoop *) data;
39   g_main_loop_quit (loop);
40
41   return FALSE;
42 }
43
44 static gboolean
45 buffer_probe_cb (GstPad * pad, GstBuffer * buffer)
46 {
47   GstClockTime new_ts = GST_BUFFER_TIMESTAMP (buffer);
48
49   GST_LOG ("ts = %" GST_TIME_FORMAT, GST_TIME_ARGS (new_ts));
50   if (old_ts != GST_CLOCK_TIME_NONE) {
51     fail_unless (new_ts != old_ts,
52         "Two buffers had same timestamp: %" GST_TIME_FORMAT,
53         GST_TIME_ARGS (old_ts));
54   }
55   old_ts = new_ts;
56
57   return TRUE;
58 }
59
60 GST_START_TEST (test_basetime_calculation)
61 {
62   GstElement *p1, *bin;
63   GstElement *asrc, *asink;
64   GstPad *pad;
65   GMainLoop *loop;
66
67   loop = g_main_loop_new (NULL, FALSE);
68
69   /* The "main" pipeline */
70   p1 = gst_parse_launch ("fakesrc ! fakesink", NULL);
71   fail_if (p1 == NULL);
72
73   /* Create a sub-bin that is activated only in "certain situations" */
74   asrc = gst_element_factory_make ("audiotestsrc", NULL);
75   if (asrc == NULL) {
76     GST_WARNING ("Cannot run test. 'audiotestsrc' not available");
77     gst_element_set_state (p1, GST_STATE_NULL);
78     gst_object_unref (p1);
79     return;
80   }
81   asink = gst_element_factory_make ("fakesink", NULL);
82
83   bin = gst_bin_new ("audiobin");
84   gst_bin_add_many (GST_BIN (bin), asrc, asink, NULL);
85   gst_element_link (asrc, asink);
86
87   gst_bin_add (GST_BIN (p1), bin);
88   gst_element_set_state (p1, GST_STATE_READY);
89
90   pad = gst_element_get_static_pad (asink, "sink");
91   fail_unless (pad != NULL, "Could not get pad out of sink");
92
93   gst_pad_add_buffer_probe (pad, G_CALLBACK (buffer_probe_cb), NULL);
94   gst_element_set_locked_state (bin, TRUE);
95
96   /* Run main pipeline first */
97   gst_element_set_state (p1, GST_STATE_PLAYING);
98   g_timeout_add (2 * 1000, break_mainloop, loop);
99   g_main_loop_run (loop);
100
101   /* Now activate the audio pipeline */
102   gst_element_set_locked_state (bin, FALSE);
103   gst_element_set_state (p1, GST_STATE_PAUSED);
104
105   /* Normally our custom audiobin would send this message */
106   gst_element_post_message (asrc,
107       gst_message_new_clock_provide (GST_OBJECT (asrc), NULL, TRUE));
108
109   /* At this point a new clock is selected */
110   gst_element_set_state (p1, GST_STATE_PLAYING);
111
112   g_timeout_add (2 * 1000, break_mainloop, loop);
113   g_main_loop_run (loop);
114
115   gst_object_unref (pad);
116   gst_element_set_state (p1, GST_STATE_NULL);
117   gst_object_unref (p1);
118
119   g_main_loop_unref (loop);
120 }
121
122 GST_END_TEST;
123
124 #endif /* #ifndef GST_DISABLE_PARSE */
125
126 static Suite *
127 baseaudiosrc_suite (void)
128 {
129   Suite *s = suite_create ("baseaudiosrc");
130   TCase *tc_chain = tcase_create ("general");
131
132   /* timeout 6 sec */
133   tcase_set_timeout (tc_chain, 6);
134   suite_add_tcase (s, tc_chain);
135
136 #ifndef GST_DISABLE_PARSE
137   tcase_add_test (tc_chain, test_basetime_calculation);
138 #endif
139
140   return s;
141 }
142
143 int
144 main (int argc, char **argv)
145 {
146   int nf;
147
148   Suite *s = baseaudiosrc_suite ();
149   SRunner *sr = srunner_create (s);
150
151   gst_check_init (&argc, &argv);
152
153   srunner_run_all (sr, CK_NORMAL);
154   nf = srunner_ntests_failed (sr);
155   srunner_free (sr);
156
157   return nf;
158 }