summaryrefslogtreecommitdiff
path: root/tests/check/pipelines/basetime.c
blob: 401b1b8cc2f86cb2cb883d173aa07a22a7cec1c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* GStreamer
 *
 * unit test for audiotestsrc basetime handling
 *
 * Copyright (C) 2009 Maemo Multimedia <multimedia at maemo dot org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/check/gstcheck.h>

#ifndef GST_DISABLE_PARSE

static GstClockTime old_ts = GST_CLOCK_TIME_NONE;

static gboolean
break_mainloop (gpointer data)
{
  GMainLoop *loop;

  loop = (GMainLoop *) data;
  g_main_loop_quit (loop);

  return FALSE;
}

static GstPadProbeReturn
buffer_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{
  GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
  GstClockTime new_ts = GST_BUFFER_TIMESTAMP (buffer);

  GST_LOG ("ts = %" GST_TIME_FORMAT, GST_TIME_ARGS (new_ts));
  if (old_ts != GST_CLOCK_TIME_NONE) {
    fail_unless (new_ts != old_ts,
        "Two buffers had same timestamp: %" GST_TIME_FORMAT,
        GST_TIME_ARGS (old_ts));
  }
  old_ts = new_ts;

  return TRUE;
}

GST_START_TEST (test_basetime_calculation)
{
  GstElement *p1, *bin;
  GstElement *asrc, *asink;
  GstPad *pad;
  GMainLoop *loop;

  loop = g_main_loop_new (NULL, FALSE);

  /* The "main" pipeline */
  p1 = gst_parse_launch ("fakesrc ! fakesink", NULL);
  fail_if (p1 == NULL);

  /* Create a sub-bin that is activated only in "certain situations" */
  asrc = gst_element_factory_make ("audiotestsrc", NULL);
  if (asrc == NULL) {
    GST_WARNING ("Cannot run test. 'audiotestsrc' not available");
    gst_element_set_state (p1, GST_STATE_NULL);
    gst_object_unref (p1);
    return;
  }
  asink = gst_element_factory_make ("fakesink", NULL);

  bin = gst_bin_new ("audiobin");
  gst_bin_add_many (GST_BIN (bin), asrc, asink, NULL);
  gst_element_link (asrc, asink);

  gst_bin_add (GST_BIN (p1), bin);
  gst_element_set_state (p1, GST_STATE_READY);

  pad = gst_element_get_static_pad (asink, "sink");
  fail_unless (pad != NULL, "Could not get pad out of sink");

  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, buffer_probe_cb, NULL,
      NULL);
  gst_element_set_locked_state (bin, TRUE);

  /* Run main pipeline first */
  gst_element_set_state (p1, GST_STATE_PLAYING);
  g_timeout_add (2 * 1000, break_mainloop, loop);
  g_main_loop_run (loop);

  /* Now activate the audio pipeline */
  gst_element_set_locked_state (bin, FALSE);
  gst_element_set_state (p1, GST_STATE_PAUSED);

  /* Normally our custom audiobin would send this message */
  gst_element_post_message (asrc,
      gst_message_new_clock_provide (GST_OBJECT (asrc), NULL, TRUE));

  /* At this point a new clock is selected */
  gst_element_set_state (p1, GST_STATE_PLAYING);

  g_timeout_add (2 * 1000, break_mainloop, loop);
  g_main_loop_run (loop);

  gst_object_unref (pad);
  gst_element_set_state (p1, GST_STATE_NULL);
  gst_object_unref (p1);

  g_main_loop_unref (loop);
}

GST_END_TEST;

#endif /* #ifndef GST_DISABLE_PARSE */

static Suite *
baseaudiosrc_suite (void)
{
  Suite *s = suite_create ("baseaudiosrc");
  TCase *tc_chain = tcase_create ("general");

  /* timeout 6 sec */
  tcase_set_timeout (tc_chain, 6);
  suite_add_tcase (s, tc_chain);

#ifndef GST_DISABLE_PARSE
  tcase_add_test (tc_chain, test_basetime_calculation);
#endif

  return s;
}

int
main (int argc, char **argv)
{
  int nf;

  Suite *s = baseaudiosrc_suite ();
  SRunner *sr = srunner_create (s);

  gst_check_init (&argc, &argv);

  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}