| 1 |
/* GstTestClock - A deterministic clock for GStreamer unit tests |
| 2 |
* |
| 3 |
* Copyright (C) 2008 Ole André Vadla Ravnås <[email protected]> |
| 4 |
* Copyright (C) 2012 Sebastian Rasmussen <[email protected]> |
| 5 |
* Copyright (C) 2012 Havard Graff <[email protected]> |
| 6 |
* Copyright (C) 2013 Haakon Sporsheim <[email protected]> |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the |
| 20 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 |
* Boston, MA 02111-1307, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
/** |
| 25 |
* SECTION:gsttestclock |
| 26 |
* @title: GstTestClock |
| 27 |
* @short_description: Controllable, deterministic clock for GStreamer unit tests |
| 28 |
* @see_also: #GstSystemClock, #GstClock |
| 29 |
* |
| 30 |
* GstTestClock is an implementation of #GstClock which has different |
| 31 |
* behaviour compared to #GstSystemClock. Time for #GstSystemClock advances |
| 32 |
* according to the system time, while time for #GstTestClock changes only |
| 33 |
* when gst_test_clock_set_time() or gst_test_clock_advance_time() are |
| 34 |
* called. #GstTestClock provides unit tests with the possibility to |
| 35 |
* precisely advance the time in a deterministic manner, independent of the |
| 36 |
* system time or any other external factors. |
| 37 |
* |
| 38 |
* ## Advancing the time of a #GstTestClock |
| 39 |
* |
| 40 |
* |[<!-- language="C" --> |
| 41 |
* #include <gst/gst.h> |
| 42 |
* #include <gst/check/gsttestclock.h> |
| 43 |
* |
| 44 |
* GstClock *clock; |
| 45 |
* GstTestClock *test_clock; |
| 46 |
* |
| 47 |
* clock = gst_test_clock_new (); |
| 48 |
* test_clock = GST_TEST_CLOCK (clock); |
| 49 |
* GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock))); |
| 50 |
* gst_test_clock_advance_time ( test_clock, 1 * GST_SECOND); |
| 51 |
* GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock))); |
| 52 |
* g_usleep (10 * G_USEC_PER_SEC); |
| 53 |
* GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock))); |
| 54 |
* gst_test_clock_set_time (test_clock, 42 * GST_SECOND); |
| 55 |
* GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock))); |
| 56 |
* ... |
| 57 |
* ]| |
| 58 |
* |
| 59 |
* #GstClock allows for setting up single shot or periodic clock notifications |
| 60 |
* as well as waiting for these notifications synchronously (using |
| 61 |
* gst_clock_id_wait()) or asynchronously (using gst_clock_id_wait_async() or |
| 62 |
* gst_clock_id_wait_async()). This is used by many GStreamer elements, |
| 63 |
* among them #GstBaseSrc and #GstBaseSink. |
| 64 |
* |
| 65 |
* #GstTestClock keeps track of these clock notifications. By calling |
| 66 |
* gst_test_clock_wait_for_next_pending_id() or |
| 67 |
* gst_test_clock_wait_for_multiple_pending_ids() a unit tests may wait for the |
| 68 |
* next one or several clock notifications to be requested. Additionally unit |
| 69 |
* tests may release blocked waits in a controlled fashion by calling |
| 70 |
* gst_test_clock_process_next_clock_id(). This way a unit test can control the |
| 71 |
* inaccuracy (jitter) of clock notifications, since the test can decide to |
| 72 |
* release blocked waits when the clock time has advanced exactly to, or past, |
| 73 |
* the requested clock notification time. |
| 74 |
* |
| 75 |
* There are also interfaces for determining if a notification belongs to a |
| 76 |
* #GstTestClock or not, as well as getting the number of requested clock |
| 77 |
* notifications so far. |
| 78 |
* |
| 79 |
* N.B.: When a unit test waits for a certain amount of clock notifications to |
| 80 |
* be requested in gst_test_clock_wait_for_next_pending_id() or |
| 81 |
* gst_test_clock_wait_for_multiple_pending_ids() then these functions may block |
| 82 |
* for a long time. If they block forever then the expected clock notifications |
| 83 |
* were never requested from #GstTestClock, and so the assumptions in the code |
| 84 |
* of the unit test are wrong. The unit test case runner in gstcheck is |
| 85 |
* expected to catch these cases either by the default test case timeout or the |
| 86 |
* one set for the unit test by calling tcase_set_timeout\(\). |
| 87 |
* |
| 88 |
* The sample code below assumes that the element under test will delay a |
| 89 |
* buffer pushed on the source pad by some latency until it arrives on the sink |
| 90 |
* pad. Moreover it is assumed that the element will at some point call |
| 91 |
* gst_clock_id_wait() to synchronously wait for a specific time. The first |
| 92 |
* buffer sent will arrive exactly on time only delayed by the latency. The |
| 93 |
* second buffer will arrive a little late (7ms) due to simulated jitter in the |
| 94 |
* clock notification. |
| 95 |
* |
| 96 |
* ## Demonstration of how to work with clock notifications and #GstTestClock |
| 97 |
* |
| 98 |
* |[<!-- language="C" --> |
| 99 |
* #include <gst/gst.h> |
| 100 |
* #include <gst/check/gstcheck.h> |
| 101 |
* #include <gst/check/gsttestclock.h> |
| 102 |
* |
| 103 |
* GstClockTime latency; |
| 104 |
* GstElement *element; |
| 105 |
* GstPad *srcpad; |
| 106 |
* GstClock *clock; |
| 107 |
* GstTestClock *test_clock; |
| 108 |
* GstBuffer buf; |
| 109 |
* GstClockID pending_id; |
| 110 |
* GstClockID processed_id; |
| 111 |
* |
| 112 |
* latency = 42 * GST_MSECOND; |
| 113 |
* element = create_element (latency, ...); |
| 114 |
* srcpad = get_source_pad (element); |
| 115 |
* |
| 116 |
* clock = gst_test_clock_new (); |
| 117 |
* test_clock = GST_TEST_CLOCK (clock); |
| 118 |
* gst_element_set_clock (element, clock); |
| 119 |
* |
| 120 |
* GST_INFO ("Set time, create and push the first buffer\n"); |
| 121 |
* gst_test_clock_set_time (test_clock, 0); |
| 122 |
* buf = create_test_buffer (gst_clock_get_time (clock), ...); |
| 123 |
* gst_assert_cmpint (gst_pad_push (srcpad, buf), ==, GST_FLOW_OK); |
| 124 |
* |
| 125 |
* GST_INFO ("Block until element is waiting for a clock notification\n"); |
| 126 |
* gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id); |
| 127 |
* GST_INFO ("Advance to the requested time of the clock notification\n"); |
| 128 |
* gst_test_clock_advance_time (test_clock, latency); |
| 129 |
* GST_INFO ("Release the next blocking wait and make sure it is the one from element\n"); |
| 130 |
* processed_id = gst_test_clock_process_next_clock_id (test_clock); |
| 131 |
* g_assert (processed_id == pending_id); |
| 132 |
* g_assert_cmpint (GST_CLOCK_ENTRY_STATUS (processed_id), ==, GST_CLOCK_OK); |
| 133 |
* gst_clock_id_unref (pending_id); |
| 134 |
* gst_clock_id_unref (processed_id); |
| 135 |
* |
| 136 |
* GST_INFO ("Validate that element produced an output buffer and check its timestamp\n"); |
| 137 |
* g_assert_cmpint (get_number_of_output_buffer (...), ==, 1); |
| 138 |
* buf = get_buffer_pushed_by_element (element, ...); |
| 139 |
* g_assert_cmpint (GST_BUFFER_TIMESTAMP (buf), ==, latency); |
| 140 |
* gst_buffer_unref (buf); |
| 141 |
* GST_INFO ("Check that element does not wait for any clock notification\n"); |
| 142 |
* g_assert (!gst_test_clock_peek_next_pending_id (test_clock, NULL)); |
| 143 |
* |
| 144 |
* GST_INFO ("Set time, create and push the second buffer\n"); |
| 145 |
* gst_test_clock_advance_time (test_clock, 10 * GST_SECOND); |
| 146 |
* buf = create_test_buffer (gst_clock_get_time (clock), ...); |
| 147 |
* gst_assert_cmpint (gst_pad_push (srcpad, buf), ==, GST_FLOW_OK); |
| 148 |
* |
| 149 |
* GST_INFO ("Block until element is waiting for a new clock notification\n"); |
| 150 |
* (gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id); |
| 151 |
* GST_INFO ("Advance past 7ms beyond the requested time of the clock notification\n"); |
| 152 |
* gst_test_clock_advance_time (test_clock, latency + 7 * GST_MSECOND); |
| 153 |
* GST_INFO ("Release the next blocking wait and make sure it is the one from element\n"); |
| 154 |
* processed_id = gst_test_clock_process_next_clock_id (test_clock); |
| 155 |
* g_assert (processed_id == pending_id); |
| 156 |
* g_assert_cmpint (GST_CLOCK_ENTRY_STATUS (processed_id), ==, GST_CLOCK_OK); |
| 157 |
* gst_clock_id_unref (pending_id); |
| 158 |
* gst_clock_id_unref (processed_id); |
| 159 |
* |
| 160 |
* GST_INFO ("Validate that element produced an output buffer and check its timestamp\n"); |
| 161 |
* g_assert_cmpint (get_number_of_output_buffer (...), ==, 1); |
| 162 |
* buf = get_buffer_pushed_by_element (element, ...); |
| 163 |
* g_assert_cmpint (GST_BUFFER_TIMESTAMP (buf), ==, |
| 164 |
* 10 * GST_SECOND + latency + 7 * GST_MSECOND); |
| 165 |
* gst_buffer_unref (buf); |
| 166 |
* GST_INFO ("Check that element does not wait for any clock notification\n"); |
| 167 |
* g_assert (!gst_test_clock_peek_next_pending_id (test_clock, NULL)); |
| 168 |
* ... |
| 169 |
* ]| |
| 170 |
* |
| 171 |
* Since #GstTestClock is only supposed to be used in unit tests it calls |
| 172 |
* g_assert(), g_assert_cmpint() or g_assert_cmpuint() to validate all function |
| 173 |
* arguments. This will highlight any issues with the unit test code itself. |
| 174 |
*/ |
| 175 |
|
| 176 |
#ifdef HAVE_CONFIG_H |
| 177 |
#include <config.h> |
| 178 |
#endif |
| 179 |
|
| 180 |
#include "gsttestclock.h" |
| 181 |
|
| 182 |
enum |
| 183 |
{ |
| 184 |
PROP_0, |
| 185 |
PROP_START_TIME, |
| 186 |
PROP_CLOCK_TYPE |
| 187 |
}; |
| 188 |
|
| 189 |
typedef struct _GstClockEntryContext GstClockEntryContext; |
| 190 |
|
| 191 |
struct _GstClockEntryContext |
| 192 |
{ |
| 193 |
GstClockEntry *clock_entry; |
| 194 |
GstClockTimeDiff time_diff; |
| 195 |
}; |
| 196 |
|
| 197 |
struct _GstTestClockPrivate |
| 198 |
{ |
| 199 |
GstClockType clock_type; |
| 200 |
GstClockTime start_time; |
| 201 |
GstClockTime internal_time; |
| 202 |
GList *entry_contexts; |
| 203 |
GCond entry_added_cond; |
| 204 |
GCond entry_processed_cond; |
| 205 |
}; |
| 206 |
|
| 207 |
#define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC |
| 208 |
|
| 209 |
#define GST_TEST_CLOCK_GET_PRIVATE(obj) ((GST_TEST_CLOCK_CAST (obj))->priv) |
| 210 |
|
| 211 |
GST_DEBUG_CATEGORY_STATIC (test_clock_debug); |
| 212 |
#define GST_CAT_TEST_CLOCK test_clock_debug |
| 213 |
|
| 214 |
#define _do_init \ |
| 215 |
G_STMT_START { \ |
| 216 |
GST_DEBUG_CATEGORY_INIT (test_clock_debug, "GST_TEST_CLOCK", \ |
| 217 |
GST_DEBUG_BOLD, "Test clocks for unit tests"); \ |
| 218 |
} G_STMT_END |
| 219 |
|
| 220 |
G_DEFINE_TYPE_WITH_CODE (GstTestClock, gst_test_clock, |
| 221 |
GST_TYPE_CLOCK, G_ADD_PRIVATE (GstTestClock) _do_init); |
| 222 |
|
| 223 |
static GstObjectClass *parent_class = NULL; |
| 224 |
|
| 225 |
static void gst_test_clock_constructed (GObject * object); |
| 226 |
static void gst_test_clock_dispose (GObject * object); |
| 227 |
static void gst_test_clock_finalize (GObject * object); |
| 228 |
static void gst_test_clock_get_property (GObject * object, guint property_id, |
| 229 |
GValue * value, GParamSpec * pspec); |
| 230 |
static void gst_test_clock_set_property (GObject * object, guint property_id, |
| 231 |
const GValue * value, GParamSpec * pspec); |
| 232 |
|
| 233 |
static GstClockTime gst_test_clock_get_resolution (GstClock * clock); |
| 234 |
static GstClockTime gst_test_clock_get_internal_time (GstClock * clock); |
| 235 |
static GstClockReturn gst_test_clock_wait (GstClock * clock, |
| 236 |
GstClockEntry * entry, GstClockTimeDiff * jitter); |
| 237 |
static GstClockReturn gst_test_clock_wait_async (GstClock * clock, |
| 238 |
GstClockEntry * entry); |
| 239 |
static void gst_test_clock_unschedule (GstClock * clock, GstClockEntry * entry); |
| 240 |
|
| 241 |
static gboolean gst_test_clock_peek_next_pending_id_unlocked (GstTestClock * |
| 242 |
test_clock, GstClockID * pending_id); |
| 243 |
static guint gst_test_clock_peek_id_count_unlocked (GstTestClock * test_clock); |
| 244 |
|
| 245 |
static void gst_test_clock_add_entry (GstTestClock * test_clock, |
| 246 |
GstClockEntry * entry, GstClockTimeDiff * jitter); |
| 247 |
static void gst_test_clock_remove_entry (GstTestClock * test_clock, |
| 248 |
GstClockEntry * entry); |
| 249 |
static GstClockEntryContext *gst_test_clock_lookup_entry_context (GstTestClock * |
| 250 |
test_clock, GstClockEntry * clock_entry); |
| 251 |
|
| 252 |
static gint gst_clock_entry_context_compare_func (gconstpointer a, |
| 253 |
gconstpointer b); |
| 254 |
|
| 255 |
static void |
| 256 |
gst_test_clock_class_init (GstTestClockClass * klass) |
| 257 |
{ |
| 258 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 259 |
GstClockClass *gstclock_class = GST_CLOCK_CLASS (klass); |
| 260 |
GParamSpec *pspec; |
| 261 |
|
| 262 |
parent_class = g_type_class_peek_parent (klass); |
| 263 |
|
| 264 |
gobject_class->constructed = GST_DEBUG_FUNCPTR (gst_test_clock_constructed); |
| 265 |
gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_test_clock_dispose); |
| 266 |
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_test_clock_finalize); |
| 267 |
gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_test_clock_get_property); |
| 268 |
gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_test_clock_set_property); |
| 269 |
|
| 270 |
gstclock_class->get_resolution = |
| 271 |
GST_DEBUG_FUNCPTR (gst_test_clock_get_resolution); |
| 272 |
gstclock_class->get_internal_time = |
| 273 |
GST_DEBUG_FUNCPTR (gst_test_clock_get_internal_time); |
| 274 |
gstclock_class->wait = GST_DEBUG_FUNCPTR (gst_test_clock_wait); |
| 275 |
gstclock_class->wait_async = GST_DEBUG_FUNCPTR (gst_test_clock_wait_async); |
| 276 |
gstclock_class->unschedule = GST_DEBUG_FUNCPTR (gst_test_clock_unschedule); |
| 277 |
|
| 278 |
/** |
| 279 |
* GstTestClock:start-time: |
| 280 |
* |
| 281 |
* When a #GstTestClock is constructed it will have a certain start time set. |
| 282 |
* If the clock was created using gst_test_clock_new_with_start_time() then |
| 283 |
* this property contains the value of the @start_time argument. If |
| 284 |
* gst_test_clock_new() was called the clock started at time zero, and thus |
| 285 |
* this property contains the value 0. |
| 286 |
*/ |
| 287 |
pspec = g_param_spec_uint64 ("start-time", "Start Time", |
| 288 |
"Start Time of the Clock", 0, G_MAXUINT64, 0, |
| 289 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY); |
| 290 |
g_object_class_install_property (gobject_class, PROP_START_TIME, pspec); |
| 291 |
|
| 292 |
g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE, |
| 293 |
g_param_spec_enum ("clock-type", "Clock type", |
| 294 |
"The kind of clock implementation to be reported by this clock", |
| 295 |
GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE, |
| 296 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
static void |
| 301 |
gst_test_clock_init (GstTestClock * test_clock) |
| 302 |
{ |
| 303 |
GstTestClockPrivate *priv; |
| 304 |
|
| 305 |
test_clock->priv = gst_test_clock_get_instance_private (test_clock); |
| 306 |
|
| 307 |
priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 308 |
|
| 309 |
g_cond_init (&priv->entry_added_cond); |
| 310 |
g_cond_init (&priv->entry_processed_cond); |
| 311 |
priv->clock_type = DEFAULT_CLOCK_TYPE; |
| 312 |
|
| 313 |
GST_OBJECT_FLAG_SET (test_clock, |
| 314 |
GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC | |
| 315 |
GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC | |
| 316 |
GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC | |
| 317 |
GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC); |
| 318 |
} |
| 319 |
|
| 320 |
static void |
| 321 |
gst_test_clock_constructed (GObject * object) |
| 322 |
{ |
| 323 |
GstTestClock *test_clock = GST_TEST_CLOCK (object); |
| 324 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 325 |
|
| 326 |
priv->internal_time = priv->start_time; |
| 327 |
|
| 328 |
G_OBJECT_CLASS (parent_class)->constructed (object); |
| 329 |
} |
| 330 |
|
| 331 |
static void |
| 332 |
gst_test_clock_dispose (GObject * object) |
| 333 |
{ |
| 334 |
GstTestClock *test_clock = GST_TEST_CLOCK (object); |
| 335 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 336 |
|
| 337 |
GST_OBJECT_LOCK (test_clock); |
| 338 |
|
| 339 |
while (priv->entry_contexts != NULL) { |
| 340 |
GstClockEntryContext *ctx = priv->entry_contexts->data; |
| 341 |
gst_test_clock_remove_entry (test_clock, ctx->clock_entry); |
| 342 |
} |
| 343 |
|
| 344 |
GST_OBJECT_UNLOCK (test_clock); |
| 345 |
|
| 346 |
G_OBJECT_CLASS (parent_class)->dispose (object); |
| 347 |
} |
| 348 |
|
| 349 |
static void |
| 350 |
gst_test_clock_finalize (GObject * object) |
| 351 |
{ |
| 352 |
GstTestClock *test_clock = GST_TEST_CLOCK (object); |
| 353 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 354 |
|
| 355 |
g_cond_clear (&priv->entry_added_cond); |
| 356 |
g_cond_clear (&priv->entry_processed_cond); |
| 357 |
|
| 358 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 359 |
} |
| 360 |
|
| 361 |
static void |
| 362 |
gst_test_clock_get_property (GObject * object, guint property_id, |
| 363 |
GValue * value, GParamSpec * pspec) |
| 364 |
{ |
| 365 |
GstTestClock *test_clock = GST_TEST_CLOCK (object); |
| 366 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 367 |
|
| 368 |
switch (property_id) { |
| 369 |
case PROP_START_TIME: |
| 370 |
g_value_set_uint64 (value, priv->start_time); |
| 371 |
break; |
| 372 |
case PROP_CLOCK_TYPE: |
| 373 |
g_value_set_enum (value, priv->clock_type); |
| 374 |
break; |
| 375 |
default: |
| 376 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 377 |
break; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
static void |
| 382 |
gst_test_clock_set_property (GObject * object, guint property_id, |
| 383 |
const GValue * value, GParamSpec * pspec) |
| 384 |
{ |
| 385 |
GstTestClock *test_clock = GST_TEST_CLOCK (object); |
| 386 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 387 |
|
| 388 |
switch (property_id) { |
| 389 |
case PROP_START_TIME: |
| 390 |
priv->start_time = g_value_get_uint64 (value); |
| 391 |
GST_CAT_TRACE_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 392 |
"test clock start time initialized at %" GST_TIME_FORMAT, |
| 393 |
GST_TIME_ARGS (priv->start_time)); |
| 394 |
break; |
| 395 |
case PROP_CLOCK_TYPE: |
| 396 |
priv->clock_type = (GstClockType) g_value_get_enum (value); |
| 397 |
GST_CAT_DEBUG (GST_CAT_TEST_CLOCK, "clock-type set to %d", |
| 398 |
priv->clock_type); |
| 399 |
break; |
| 400 |
default: |
| 401 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 402 |
break; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
static GstClockTime |
| 407 |
gst_test_clock_get_resolution (GstClock * clock) |
| 408 |
{ |
| 409 |
(void) clock; |
| 410 |
return 1; |
| 411 |
} |
| 412 |
|
| 413 |
static GstClockTime |
| 414 |
gst_test_clock_get_internal_time (GstClock * clock) |
| 415 |
{ |
| 416 |
GstTestClock *test_clock = GST_TEST_CLOCK (clock); |
| 417 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 418 |
GstClockTime result; |
| 419 |
|
| 420 |
GST_OBJECT_LOCK (test_clock); |
| 421 |
|
| 422 |
GST_CAT_TRACE_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 423 |
"retrieving test clock time %" GST_TIME_FORMAT, |
| 424 |
GST_TIME_ARGS (priv->internal_time)); |
| 425 |
result = priv->internal_time; |
| 426 |
|
| 427 |
GST_OBJECT_UNLOCK (test_clock); |
| 428 |
|
| 429 |
return result; |
| 430 |
} |
| 431 |
|
| 432 |
static GstClockReturn |
| 433 |
gst_test_clock_wait (GstClock * clock, |
| 434 |
GstClockEntry * entry, GstClockTimeDiff * jitter) |
| 435 |
{ |
| 436 |
GstTestClock *test_clock = GST_TEST_CLOCK (clock); |
| 437 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 438 |
|
| 439 |
GST_OBJECT_LOCK (test_clock); |
| 440 |
|
| 441 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 442 |
"requesting synchronous clock notification at %" GST_TIME_FORMAT, |
| 443 |
GST_TIME_ARGS (GST_CLOCK_ENTRY_TIME (entry))); |
| 444 |
|
| 445 |
if (GST_CLOCK_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED) |
| 446 |
goto was_unscheduled; |
| 447 |
|
| 448 |
if (gst_test_clock_lookup_entry_context (test_clock, entry) == NULL) |
| 449 |
gst_test_clock_add_entry (test_clock, entry, jitter); |
| 450 |
|
| 451 |
GST_CLOCK_ENTRY_STATUS (entry) = GST_CLOCK_BUSY; |
| 452 |
|
| 453 |
while (GST_CLOCK_ENTRY_STATUS (entry) == GST_CLOCK_BUSY) |
| 454 |
g_cond_wait (&priv->entry_processed_cond, GST_OBJECT_GET_LOCK (test_clock)); |
| 455 |
|
| 456 |
GST_OBJECT_UNLOCK (test_clock); |
| 457 |
|
| 458 |
return GST_CLOCK_ENTRY_STATUS (entry); |
| 459 |
|
| 460 |
/* ERRORS */ |
| 461 |
was_unscheduled: |
| 462 |
{ |
| 463 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 464 |
"entry was unscheduled"); |
| 465 |
GST_OBJECT_UNLOCK (test_clock); |
| 466 |
return GST_CLOCK_UNSCHEDULED; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
static GstClockReturn |
| 471 |
gst_test_clock_wait_async (GstClock * clock, GstClockEntry * entry) |
| 472 |
{ |
| 473 |
GstTestClock *test_clock = GST_TEST_CLOCK (clock); |
| 474 |
|
| 475 |
GST_OBJECT_LOCK (test_clock); |
| 476 |
|
| 477 |
if (GST_CLOCK_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED) |
| 478 |
goto was_unscheduled; |
| 479 |
|
| 480 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 481 |
"requesting asynchronous clock notification at %" GST_TIME_FORMAT, |
| 482 |
GST_TIME_ARGS (GST_CLOCK_ENTRY_TIME (entry))); |
| 483 |
|
| 484 |
gst_test_clock_add_entry (test_clock, entry, NULL); |
| 485 |
|
| 486 |
GST_OBJECT_UNLOCK (test_clock); |
| 487 |
|
| 488 |
return GST_CLOCK_OK; |
| 489 |
|
| 490 |
/* ERRORS */ |
| 491 |
was_unscheduled: |
| 492 |
{ |
| 493 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 494 |
"entry was unscheduled"); |
| 495 |
GST_OBJECT_UNLOCK (test_clock); |
| 496 |
return GST_CLOCK_UNSCHEDULED; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
static void |
| 501 |
gst_test_clock_unschedule (GstClock * clock, GstClockEntry * entry) |
| 502 |
{ |
| 503 |
GstTestClock *test_clock = GST_TEST_CLOCK (clock); |
| 504 |
|
| 505 |
GST_OBJECT_LOCK (test_clock); |
| 506 |
|
| 507 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 508 |
"unscheduling requested clock notification at %" GST_TIME_FORMAT, |
| 509 |
GST_TIME_ARGS (GST_CLOCK_ENTRY_TIME (entry))); |
| 510 |
|
| 511 |
GST_CLOCK_ENTRY_STATUS (entry) = GST_CLOCK_UNSCHEDULED; |
| 512 |
gst_test_clock_remove_entry (test_clock, entry); |
| 513 |
|
| 514 |
GST_OBJECT_UNLOCK (test_clock); |
| 515 |
} |
| 516 |
|
| 517 |
static gboolean |
| 518 |
gst_test_clock_peek_next_pending_id_unlocked (GstTestClock * test_clock, |
| 519 |
GstClockID * pending_id) |
| 520 |
{ |
| 521 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 522 |
GList *imminent_clock_id = g_list_first (priv->entry_contexts); |
| 523 |
gboolean result = FALSE; |
| 524 |
|
| 525 |
if (imminent_clock_id != NULL) { |
| 526 |
GstClockEntryContext *ctx = imminent_clock_id->data; |
| 527 |
|
| 528 |
if (pending_id != NULL) { |
| 529 |
*pending_id = gst_clock_id_ref (ctx->clock_entry); |
| 530 |
} |
| 531 |
|
| 532 |
result = TRUE; |
| 533 |
} |
| 534 |
|
| 535 |
return result; |
| 536 |
} |
| 537 |
|
| 538 |
static guint |
| 539 |
gst_test_clock_peek_id_count_unlocked (GstTestClock * test_clock) |
| 540 |
{ |
| 541 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 542 |
|
| 543 |
return g_list_length (priv->entry_contexts); |
| 544 |
} |
| 545 |
|
| 546 |
static void |
| 547 |
gst_test_clock_add_entry (GstTestClock * test_clock, |
| 548 |
GstClockEntry * entry, GstClockTimeDiff * jitter) |
| 549 |
{ |
| 550 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 551 |
GstClockTime now; |
| 552 |
GstClockEntryContext *ctx; |
| 553 |
|
| 554 |
now = gst_clock_adjust_unlocked (GST_CLOCK (test_clock), priv->internal_time); |
| 555 |
|
| 556 |
if (jitter != NULL) |
| 557 |
*jitter = GST_CLOCK_DIFF (GST_CLOCK_ENTRY_TIME (entry), now); |
| 558 |
|
| 559 |
ctx = g_new (GstClockEntryContext, 1); |
| 560 |
ctx->clock_entry = GST_CLOCK_ENTRY (gst_clock_id_ref (entry)); |
| 561 |
ctx->time_diff = GST_CLOCK_DIFF (now, GST_CLOCK_ENTRY_TIME (entry)); |
| 562 |
|
| 563 |
priv->entry_contexts = g_list_insert_sorted (priv->entry_contexts, ctx, |
| 564 |
gst_clock_entry_context_compare_func); |
| 565 |
|
| 566 |
g_cond_broadcast (&priv->entry_added_cond); |
| 567 |
} |
| 568 |
|
| 569 |
static void |
| 570 |
gst_test_clock_remove_entry (GstTestClock * test_clock, GstClockEntry * entry) |
| 571 |
{ |
| 572 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 573 |
GstClockEntryContext *ctx; |
| 574 |
|
| 575 |
ctx = gst_test_clock_lookup_entry_context (test_clock, entry); |
| 576 |
if (ctx != NULL) { |
| 577 |
gst_clock_id_unref (ctx->clock_entry); |
| 578 |
priv->entry_contexts = g_list_remove (priv->entry_contexts, ctx); |
| 579 |
g_free (ctx); |
| 580 |
|
| 581 |
g_cond_broadcast (&priv->entry_processed_cond); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
static GstClockEntryContext * |
| 586 |
gst_test_clock_lookup_entry_context (GstTestClock * test_clock, |
| 587 |
GstClockEntry * clock_entry) |
| 588 |
{ |
| 589 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 590 |
GstClockEntryContext *result = NULL; |
| 591 |
GList *cur; |
| 592 |
|
| 593 |
for (cur = priv->entry_contexts; cur != NULL; cur = cur->next) { |
| 594 |
GstClockEntryContext *ctx = cur->data; |
| 595 |
|
| 596 |
if (ctx->clock_entry == clock_entry) { |
| 597 |
result = ctx; |
| 598 |
break; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
return result; |
| 603 |
} |
| 604 |
|
| 605 |
static gint |
| 606 |
gst_clock_entry_context_compare_func (gconstpointer a, gconstpointer b) |
| 607 |
{ |
| 608 |
const GstClockEntryContext *ctx_a = a; |
| 609 |
const GstClockEntryContext *ctx_b = b; |
| 610 |
|
| 611 |
return gst_clock_id_compare_func (ctx_a->clock_entry, ctx_b->clock_entry); |
| 612 |
} |
| 613 |
|
| 614 |
static void |
| 615 |
process_entry_context_unlocked (GstTestClock * test_clock, |
| 616 |
GstClockEntryContext * ctx) |
| 617 |
{ |
| 618 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 619 |
GstClockEntry *entry = ctx->clock_entry; |
| 620 |
|
| 621 |
if (ctx->time_diff >= 0) |
| 622 |
GST_CLOCK_ENTRY_STATUS (entry) = GST_CLOCK_OK; |
| 623 |
else |
| 624 |
GST_CLOCK_ENTRY_STATUS (entry) = GST_CLOCK_EARLY; |
| 625 |
|
| 626 |
if (entry->func != NULL) { |
| 627 |
GST_OBJECT_UNLOCK (test_clock); |
| 628 |
entry->func (GST_CLOCK (test_clock), priv->internal_time, entry, |
| 629 |
entry->user_data); |
| 630 |
GST_OBJECT_LOCK (test_clock); |
| 631 |
} |
| 632 |
|
| 633 |
gst_test_clock_remove_entry (test_clock, entry); |
| 634 |
|
| 635 |
if (GST_CLOCK_ENTRY_TYPE (entry) == GST_CLOCK_ENTRY_PERIODIC) { |
| 636 |
GST_CLOCK_ENTRY_TIME (entry) += GST_CLOCK_ENTRY_INTERVAL (entry); |
| 637 |
|
| 638 |
if (entry->func != NULL) |
| 639 |
gst_test_clock_add_entry (test_clock, entry, NULL); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
static GList * |
| 644 |
gst_test_clock_get_pending_id_list_unlocked (GstTestClock * test_clock) |
| 645 |
{ |
| 646 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 647 |
GQueue queue = G_QUEUE_INIT; |
| 648 |
GList *cur; |
| 649 |
|
| 650 |
for (cur = priv->entry_contexts; cur != NULL; cur = cur->next) { |
| 651 |
GstClockEntryContext *ctx = cur->data; |
| 652 |
|
| 653 |
g_queue_push_tail (&queue, gst_clock_id_ref (ctx->clock_entry)); |
| 654 |
} |
| 655 |
|
| 656 |
return queue.head; |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* gst_test_clock_new: |
| 661 |
* |
| 662 |
* Creates a new test clock with its time set to zero. |
| 663 |
* |
| 664 |
* MT safe. |
| 665 |
* |
| 666 |
* Returns: (transfer full): a #GstTestClock cast to #GstClock. |
| 667 |
* |
| 668 |
* Since: 1.2 |
| 669 |
*/ |
| 670 |
GstClock * |
| 671 |
gst_test_clock_new (void) |
| 672 |
{ |
| 673 |
return gst_test_clock_new_with_start_time (0); |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* gst_test_clock_new_with_start_time: |
| 678 |
* @start_time: a #GstClockTime set to the desired start time of the clock. |
| 679 |
* |
| 680 |
* Creates a new test clock with its time set to the specified time. |
| 681 |
* |
| 682 |
* MT safe. |
| 683 |
* |
| 684 |
* Returns: (transfer full): a #GstTestClock cast to #GstClock. |
| 685 |
* |
| 686 |
* Since: 1.2 |
| 687 |
*/ |
| 688 |
GstClock * |
| 689 |
gst_test_clock_new_with_start_time (GstClockTime start_time) |
| 690 |
{ |
| 691 |
GstClock *clock; |
| 692 |
|
| 693 |
g_assert_cmpuint (start_time, !=, GST_CLOCK_TIME_NONE); |
| 694 |
clock = g_object_new (GST_TYPE_TEST_CLOCK, "start-time", start_time, NULL); |
| 695 |
|
| 696 |
/* Clear floating flag */ |
| 697 |
gst_object_ref_sink (clock); |
| 698 |
|
| 699 |
return clock; |
| 700 |
} |
| 701 |
|
| 702 |
static void |
| 703 |
gst_test_clock_set_time_unlocked (GstTestClock * test_clock, |
| 704 |
GstClockTime new_time) |
| 705 |
{ |
| 706 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 707 |
|
| 708 |
g_assert_cmpuint (new_time, >=, priv->internal_time); |
| 709 |
|
| 710 |
priv->internal_time = new_time; |
| 711 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 712 |
"clock set to %" GST_TIME_FORMAT, GST_TIME_ARGS (new_time)); |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* gst_test_clock_set_time: |
| 717 |
* @test_clock: a #GstTestClock of which to set the time |
| 718 |
* @new_time: a #GstClockTime later than that returned by gst_clock_get_time() |
| 719 |
* |
| 720 |
* Sets the time of @test_clock to the time given by @new_time. The time of |
| 721 |
* @test_clock is monotonically increasing, therefore providing a @new_time |
| 722 |
* which is earlier or equal to the time of the clock as given by |
| 723 |
* gst_clock_get_time() is a programming error. |
| 724 |
* |
| 725 |
* MT safe. |
| 726 |
* |
| 727 |
* Since: 1.2 |
| 728 |
*/ |
| 729 |
void |
| 730 |
gst_test_clock_set_time (GstTestClock * test_clock, GstClockTime new_time) |
| 731 |
{ |
| 732 |
g_return_if_fail (GST_IS_TEST_CLOCK (test_clock)); |
| 733 |
|
| 734 |
g_assert_cmpuint (new_time, !=, GST_CLOCK_TIME_NONE); |
| 735 |
|
| 736 |
GST_OBJECT_LOCK (test_clock); |
| 737 |
|
| 738 |
gst_test_clock_set_time_unlocked (test_clock, new_time); |
| 739 |
|
| 740 |
GST_OBJECT_UNLOCK (test_clock); |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* gst_test_clock_advance_time: |
| 745 |
* @test_clock: a #GstTestClock for which to increase the time |
| 746 |
* @delta: a positive #GstClockTimeDiff to be added to the time of the clock |
| 747 |
* |
| 748 |
* Advances the time of the @test_clock by the amount given by @delta. The |
| 749 |
* time of @test_clock is monotonically increasing, therefore providing a |
| 750 |
* @delta which is negative or zero is a programming error. |
| 751 |
* |
| 752 |
* MT safe. |
| 753 |
* |
| 754 |
* Since: 1.2 |
| 755 |
*/ |
| 756 |
void |
| 757 |
gst_test_clock_advance_time (GstTestClock * test_clock, GstClockTimeDiff delta) |
| 758 |
{ |
| 759 |
GstTestClockPrivate *priv; |
| 760 |
|
| 761 |
g_return_if_fail (GST_IS_TEST_CLOCK (test_clock)); |
| 762 |
|
| 763 |
priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 764 |
|
| 765 |
g_assert_cmpint (delta, >=, 0); |
| 766 |
g_assert_cmpuint (delta, <, G_MAXUINT64 - delta); |
| 767 |
|
| 768 |
GST_OBJECT_LOCK (test_clock); |
| 769 |
|
| 770 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 771 |
"advancing clock by %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT, |
| 772 |
GST_TIME_ARGS (delta), GST_TIME_ARGS (priv->internal_time + delta)); |
| 773 |
priv->internal_time += delta; |
| 774 |
|
| 775 |
GST_OBJECT_UNLOCK (test_clock); |
| 776 |
} |
| 777 |
|
| 778 |
/** |
| 779 |
* gst_test_clock_peek_id_count: |
| 780 |
* @test_clock: a #GstTestClock for which to count notifications |
| 781 |
* |
| 782 |
* Determine the number of pending clock notifications that have been |
| 783 |
* requested from the @test_clock. |
| 784 |
* |
| 785 |
* MT safe. |
| 786 |
* |
| 787 |
* Returns: the number of pending clock notifications. |
| 788 |
* |
| 789 |
* Since: 1.2 |
| 790 |
*/ |
| 791 |
guint |
| 792 |
gst_test_clock_peek_id_count (GstTestClock * test_clock) |
| 793 |
{ |
| 794 |
guint result; |
| 795 |
|
| 796 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), 0); |
| 797 |
|
| 798 |
GST_OBJECT_LOCK (test_clock); |
| 799 |
result = gst_test_clock_peek_id_count_unlocked (test_clock); |
| 800 |
GST_OBJECT_UNLOCK (test_clock); |
| 801 |
|
| 802 |
return result; |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* gst_test_clock_has_id: |
| 807 |
* @test_clock: a #GstTestClock to ask if it provided the notification |
| 808 |
* @id: (transfer none): a #GstClockID clock notification |
| 809 |
* |
| 810 |
* Checks whether @test_clock was requested to provide the clock notification |
| 811 |
* given by @id. |
| 812 |
* |
| 813 |
* MT safe. |
| 814 |
* |
| 815 |
* Returns: %TRUE if the clock has been asked to provide the given clock |
| 816 |
* notification, %FALSE otherwise. |
| 817 |
* |
| 818 |
* Since: 1.2 |
| 819 |
*/ |
| 820 |
gboolean |
| 821 |
gst_test_clock_has_id (GstTestClock * test_clock, GstClockID id) |
| 822 |
{ |
| 823 |
gboolean result; |
| 824 |
|
| 825 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), FALSE); |
| 826 |
g_assert (id != NULL); |
| 827 |
|
| 828 |
GST_OBJECT_LOCK (test_clock); |
| 829 |
result = gst_test_clock_lookup_entry_context (test_clock, id) != NULL; |
| 830 |
GST_OBJECT_UNLOCK (test_clock); |
| 831 |
|
| 832 |
return result; |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* gst_test_clock_peek_next_pending_id: |
| 837 |
* @test_clock: a #GstTestClock to check the clock notifications for |
| 838 |
* @pending_id: (allow-none) (out) (transfer full): a #GstClockID clock |
| 839 |
* notification to look for |
| 840 |
* |
| 841 |
* Determines if the @pending_id is the next clock notification scheduled to |
| 842 |
* be triggered given the current time of the @test_clock. |
| 843 |
* |
| 844 |
* MT safe. |
| 845 |
* |
| 846 |
* Return: %TRUE if @pending_id is the next clock notification to be |
| 847 |
* triggered, %FALSE otherwise. |
| 848 |
* |
| 849 |
* Since: 1.2 |
| 850 |
*/ |
| 851 |
gboolean |
| 852 |
gst_test_clock_peek_next_pending_id (GstTestClock * test_clock, |
| 853 |
GstClockID * pending_id) |
| 854 |
{ |
| 855 |
gboolean result; |
| 856 |
|
| 857 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), FALSE); |
| 858 |
|
| 859 |
GST_OBJECT_LOCK (test_clock); |
| 860 |
result = gst_test_clock_peek_next_pending_id_unlocked (test_clock, |
| 861 |
pending_id); |
| 862 |
GST_OBJECT_UNLOCK (test_clock); |
| 863 |
|
| 864 |
return result; |
| 865 |
} |
| 866 |
|
| 867 |
static void |
| 868 |
gst_test_clock_wait_for_next_pending_id_unlocked (GstTestClock * test_clock, |
| 869 |
GstClockID * pending_id) |
| 870 |
{ |
| 871 |
GstTestClockPrivate *priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 872 |
|
| 873 |
while (priv->entry_contexts == NULL) |
| 874 |
g_cond_wait (&priv->entry_added_cond, GST_OBJECT_GET_LOCK (test_clock)); |
| 875 |
|
| 876 |
if (!gst_test_clock_peek_next_pending_id_unlocked (test_clock, pending_id)) |
| 877 |
g_assert_not_reached (); |
| 878 |
} |
| 879 |
|
| 880 |
/** |
| 881 |
* gst_test_clock_wait_for_next_pending_id: |
| 882 |
* @test_clock: #GstTestClock for which to get the pending clock notification |
| 883 |
* @pending_id: (allow-none) (out) (transfer full): #GstClockID |
| 884 |
* with information about the pending clock notification |
| 885 |
* |
| 886 |
* Waits until a clock notification is requested from @test_clock. There is no |
| 887 |
* timeout for this wait, see the main description of #GstTestClock. A reference |
| 888 |
* to the pending clock notification is stored in @pending_id. |
| 889 |
* |
| 890 |
* MT safe. |
| 891 |
* |
| 892 |
* Since: 1.2 |
| 893 |
*/ |
| 894 |
void |
| 895 |
gst_test_clock_wait_for_next_pending_id (GstTestClock * test_clock, |
| 896 |
GstClockID * pending_id) |
| 897 |
{ |
| 898 |
g_return_if_fail (GST_IS_TEST_CLOCK (test_clock)); |
| 899 |
|
| 900 |
GST_OBJECT_LOCK (test_clock); |
| 901 |
|
| 902 |
gst_test_clock_wait_for_next_pending_id_unlocked (test_clock, pending_id); |
| 903 |
|
| 904 |
GST_OBJECT_UNLOCK (test_clock); |
| 905 |
} |
| 906 |
|
| 907 |
/** |
| 908 |
* gst_test_clock_wait_for_pending_id_count: |
| 909 |
* @test_clock: #GstTestClock for which to await having enough pending clock |
| 910 |
* @count: the number of pending clock notifications to wait for |
| 911 |
* |
| 912 |
* Blocks until at least @count clock notifications have been requested from |
| 913 |
* @test_clock. There is no timeout for this wait, see the main description of |
| 914 |
* #GstTestClock. |
| 915 |
* |
| 916 |
* Since: 1.2 |
| 917 |
* |
| 918 |
* Deprecated: use gst_test_clock_wait_for_multiple_pending_ids() instead. |
| 919 |
*/ |
| 920 |
#ifndef GST_REMOVE_DEPRECATED |
| 921 |
void |
| 922 |
gst_test_clock_wait_for_pending_id_count (GstTestClock * test_clock, |
| 923 |
guint count) |
| 924 |
{ |
| 925 |
gst_test_clock_wait_for_multiple_pending_ids (test_clock, count, NULL); |
| 926 |
} |
| 927 |
#endif |
| 928 |
|
| 929 |
static GstClockID |
| 930 |
gst_test_clock_process_next_clock_id_unlocked (GstTestClock * test_clock) |
| 931 |
{ |
| 932 |
GstTestClockPrivate *priv; |
| 933 |
GstClockID result = NULL; |
| 934 |
GstClockEntryContext *ctx = NULL; |
| 935 |
GList *cur; |
| 936 |
|
| 937 |
priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 938 |
|
| 939 |
for (cur = priv->entry_contexts; cur != NULL && result == NULL; |
| 940 |
cur = cur->next) { |
| 941 |
ctx = cur->data; |
| 942 |
|
| 943 |
if (priv->internal_time >= GST_CLOCK_ENTRY_TIME (ctx->clock_entry)) |
| 944 |
result = gst_clock_id_ref (ctx->clock_entry); |
| 945 |
} |
| 946 |
|
| 947 |
if (result != NULL) |
| 948 |
process_entry_context_unlocked (test_clock, ctx); |
| 949 |
|
| 950 |
return result; |
| 951 |
} |
| 952 |
|
| 953 |
/** |
| 954 |
* gst_test_clock_process_next_clock_id: |
| 955 |
* @test_clock: a #GstTestClock for which to retrieve the next pending clock |
| 956 |
* notification |
| 957 |
* |
| 958 |
* MT safe. |
| 959 |
* |
| 960 |
* Returns: (transfer full) (nullable): a #GstClockID containing the next pending clock |
| 961 |
* notification. |
| 962 |
* |
| 963 |
* Since: 1.2 |
| 964 |
*/ |
| 965 |
GstClockID |
| 966 |
gst_test_clock_process_next_clock_id (GstTestClock * test_clock) |
| 967 |
{ |
| 968 |
GstClockID result; |
| 969 |
|
| 970 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), NULL); |
| 971 |
|
| 972 |
GST_OBJECT_LOCK (test_clock); |
| 973 |
|
| 974 |
result = gst_test_clock_process_next_clock_id_unlocked (test_clock); |
| 975 |
|
| 976 |
GST_OBJECT_UNLOCK (test_clock); |
| 977 |
|
| 978 |
return result; |
| 979 |
} |
| 980 |
|
| 981 |
/** |
| 982 |
* gst_test_clock_get_next_entry_time: |
| 983 |
* @test_clock: a #GstTestClock to fetch the next clock notification time for |
| 984 |
* |
| 985 |
* Retrieve the requested time for the next pending clock notification. |
| 986 |
* |
| 987 |
* MT safe. |
| 988 |
* |
| 989 |
* Returns: a #GstClockTime set to the time of the next pending clock |
| 990 |
* notification. If no clock notifications have been requested |
| 991 |
* %GST_CLOCK_TIME_NONE will be returned. |
| 992 |
* |
| 993 |
* Since: 1.2 |
| 994 |
*/ |
| 995 |
GstClockTime |
| 996 |
gst_test_clock_get_next_entry_time (GstTestClock * test_clock) |
| 997 |
{ |
| 998 |
GstTestClockPrivate *priv; |
| 999 |
GstClockTime result = GST_CLOCK_TIME_NONE; |
| 1000 |
GList *imminent_clock_id; |
| 1001 |
|
| 1002 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), GST_CLOCK_TIME_NONE); |
| 1003 |
|
| 1004 |
priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 1005 |
|
| 1006 |
GST_OBJECT_LOCK (test_clock); |
| 1007 |
|
| 1008 |
/* The list of pending clock notifications is sorted by time, |
| 1009 |
so the most imminent one is the first one in the list. */ |
| 1010 |
imminent_clock_id = g_list_first (priv->entry_contexts); |
| 1011 |
if (imminent_clock_id != NULL) { |
| 1012 |
GstClockEntryContext *ctx = imminent_clock_id->data; |
| 1013 |
result = GST_CLOCK_ENTRY_TIME (ctx->clock_entry); |
| 1014 |
} |
| 1015 |
|
| 1016 |
GST_OBJECT_UNLOCK (test_clock); |
| 1017 |
|
| 1018 |
return result; |
| 1019 |
} |
| 1020 |
|
| 1021 |
/** |
| 1022 |
* gst_test_clock_wait_for_multiple_pending_ids: |
| 1023 |
* @test_clock: #GstTestClock for which to await having enough pending clock |
| 1024 |
* @count: the number of pending clock notifications to wait for |
| 1025 |
* @pending_list: (out) (element-type Gst.ClockID) (transfer full) (allow-none): Address |
| 1026 |
* of a #GList pointer variable to store the list of pending #GstClockIDs |
| 1027 |
* that expired, or %NULL |
| 1028 |
* |
| 1029 |
* Blocks until at least @count clock notifications have been requested from |
| 1030 |
* @test_clock. There is no timeout for this wait, see the main description of |
| 1031 |
* #GstTestClock. |
| 1032 |
* |
| 1033 |
* MT safe. |
| 1034 |
* |
| 1035 |
* Since: 1.4 |
| 1036 |
*/ |
| 1037 |
void |
| 1038 |
gst_test_clock_wait_for_multiple_pending_ids (GstTestClock * test_clock, |
| 1039 |
guint count, GList ** pending_list) |
| 1040 |
{ |
| 1041 |
GstTestClockPrivate *priv; |
| 1042 |
|
| 1043 |
g_return_if_fail (GST_IS_TEST_CLOCK (test_clock)); |
| 1044 |
priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 1045 |
|
| 1046 |
GST_OBJECT_LOCK (test_clock); |
| 1047 |
|
| 1048 |
while (g_list_length (priv->entry_contexts) < count) |
| 1049 |
g_cond_wait (&priv->entry_added_cond, GST_OBJECT_GET_LOCK (test_clock)); |
| 1050 |
|
| 1051 |
if (pending_list) |
| 1052 |
*pending_list = gst_test_clock_get_pending_id_list_unlocked (test_clock); |
| 1053 |
|
| 1054 |
GST_OBJECT_UNLOCK (test_clock); |
| 1055 |
} |
| 1056 |
|
| 1057 |
/** |
| 1058 |
* gst_test_clock_timed_wait_for_multiple_pending_ids: |
| 1059 |
* @test_clock: #GstTestClock for which to await having enough pending clock |
| 1060 |
* @count: the number of pending clock notifications to wait for |
| 1061 |
* @timeout_ms: the timeout in milliseconds |
| 1062 |
* @pending_list: (out) (element-type Gst.ClockID) (transfer full) (allow-none): Address |
| 1063 |
* of a #GList pointer variable to store the list of pending #GstClockIDs |
| 1064 |
* that expired, or %NULL |
| 1065 |
* |
| 1066 |
* Blocks until at least @count clock notifications have been requested from |
| 1067 |
* @test_clock, or the timeout expires. |
| 1068 |
* |
| 1069 |
* MT safe. |
| 1070 |
* |
| 1071 |
* Returns: a @gboolean %TRUE if the waits have been registered, %FALSE if not. |
| 1072 |
* (Could be that it timed out waiting or that more waits than waits was found) |
| 1073 |
* |
| 1074 |
* Since: 1.16 |
| 1075 |
*/ |
| 1076 |
gboolean |
| 1077 |
gst_test_clock_timed_wait_for_multiple_pending_ids (GstTestClock * test_clock, |
| 1078 |
guint count, guint timeout_ms, GList ** pending_list) |
| 1079 |
{ |
| 1080 |
GstTestClockPrivate *priv; |
| 1081 |
gint64 timeout = g_get_monotonic_time () + |
| 1082 |
timeout_ms * (G_USEC_PER_SEC / 1000); |
| 1083 |
gboolean ret; |
| 1084 |
|
| 1085 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), FALSE); |
| 1086 |
priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock); |
| 1087 |
|
| 1088 |
GST_OBJECT_LOCK (test_clock); |
| 1089 |
|
| 1090 |
while (g_list_length (priv->entry_contexts) < count && |
| 1091 |
g_get_monotonic_time () < timeout) { |
| 1092 |
g_cond_wait_until (&priv->entry_added_cond, |
| 1093 |
GST_OBJECT_GET_LOCK (test_clock), timeout); |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (pending_list) |
| 1097 |
*pending_list = gst_test_clock_get_pending_id_list_unlocked (test_clock); |
| 1098 |
|
| 1099 |
ret = (g_list_length (priv->entry_contexts) == count); |
| 1100 |
|
| 1101 |
GST_OBJECT_UNLOCK (test_clock); |
| 1102 |
|
| 1103 |
return ret; |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
/** |
| 1108 |
* gst_test_clock_process_id: |
| 1109 |
* @test_clock: #GstTestClock for which to process the pending IDs |
| 1110 |
* @pending_id: (transfer full): #GstClockID |
| 1111 |
* |
| 1112 |
* Processes and releases the pending ID. |
| 1113 |
* |
| 1114 |
* MT safe. |
| 1115 |
* |
| 1116 |
* Since: 1.18 |
| 1117 |
*/ |
| 1118 |
gboolean |
| 1119 |
gst_test_clock_process_id (GstTestClock * test_clock, GstClockID pending_id) |
| 1120 |
{ |
| 1121 |
GstClockEntryContext *ctx; |
| 1122 |
|
| 1123 |
gboolean result = FALSE; |
| 1124 |
|
| 1125 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), FALSE); |
| 1126 |
|
| 1127 |
GST_OBJECT_LOCK (test_clock); |
| 1128 |
|
| 1129 |
ctx = gst_test_clock_lookup_entry_context (test_clock, pending_id); |
| 1130 |
g_assert (ctx); |
| 1131 |
|
| 1132 |
if (ctx) { |
| 1133 |
process_entry_context_unlocked (test_clock, ctx); |
| 1134 |
result = TRUE; |
| 1135 |
gst_clock_id_unref (pending_id); |
| 1136 |
} |
| 1137 |
|
| 1138 |
GST_OBJECT_UNLOCK (test_clock); |
| 1139 |
|
| 1140 |
return result; |
| 1141 |
} |
| 1142 |
|
| 1143 |
/** |
| 1144 |
* gst_test_clock_process_id_list: |
| 1145 |
* @test_clock: #GstTestClock for which to process the pending IDs |
| 1146 |
* @pending_list: (element-type Gst.ClockID) (transfer none) (allow-none): List |
| 1147 |
* of pending #GstClockIDs |
| 1148 |
* |
| 1149 |
* Processes and releases the pending IDs in the list. |
| 1150 |
* |
| 1151 |
* MT safe. |
| 1152 |
* |
| 1153 |
* Since: 1.4 |
| 1154 |
*/ |
| 1155 |
guint |
| 1156 |
gst_test_clock_process_id_list (GstTestClock * test_clock, |
| 1157 |
const GList * pending_list) |
| 1158 |
{ |
| 1159 |
const GList *cur; |
| 1160 |
guint result = 0; |
| 1161 |
|
| 1162 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), 0); |
| 1163 |
|
| 1164 |
GST_OBJECT_LOCK (test_clock); |
| 1165 |
|
| 1166 |
for (cur = pending_list; cur != NULL; cur = cur->next) { |
| 1167 |
GstClockID pending_id = cur->data; |
| 1168 |
GstClockEntryContext *ctx = |
| 1169 |
gst_test_clock_lookup_entry_context (test_clock, pending_id); |
| 1170 |
if (ctx) { |
| 1171 |
process_entry_context_unlocked (test_clock, ctx); |
| 1172 |
result++; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
GST_OBJECT_UNLOCK (test_clock); |
| 1176 |
|
| 1177 |
return result; |
| 1178 |
} |
| 1179 |
|
| 1180 |
/** |
| 1181 |
* gst_test_clock_id_list_get_latest_time: |
| 1182 |
* @pending_list: (element-type Gst.ClockID) (transfer none) (allow-none): List |
| 1183 |
* of of pending #GstClockIDs |
| 1184 |
* |
| 1185 |
* Finds the latest time inside the list. |
| 1186 |
* |
| 1187 |
* MT safe. |
| 1188 |
* |
| 1189 |
* Since: 1.4 |
| 1190 |
*/ |
| 1191 |
GstClockTime |
| 1192 |
gst_test_clock_id_list_get_latest_time (const GList * pending_list) |
| 1193 |
{ |
| 1194 |
const GList *cur; |
| 1195 |
GstClockTime result = 0; |
| 1196 |
|
| 1197 |
for (cur = pending_list; cur != NULL; cur = cur->next) { |
| 1198 |
GstClockID *pending_id = cur->data; |
| 1199 |
GstClockTime time = gst_clock_id_get_time (pending_id); |
| 1200 |
if (time > result) |
| 1201 |
result = time; |
| 1202 |
} |
| 1203 |
|
| 1204 |
return result; |
| 1205 |
} |
| 1206 |
|
| 1207 |
/** |
| 1208 |
* gst_test_clock_crank: |
| 1209 |
* @test_clock: #GstTestClock to crank |
| 1210 |
* |
| 1211 |
* A "crank" consists of three steps: |
| 1212 |
* 1: Wait for a #GstClockID to be registered with the #GstTestClock. |
| 1213 |
* 2: Advance the #GstTestClock to the time the #GstClockID is waiting, unless |
| 1214 |
* the clock time is already passed the clock id (Since: 1.18). |
| 1215 |
* 3: Release the #GstClockID wait. |
| 1216 |
* A "crank" can be though of as the notion of |
| 1217 |
* manually driving the clock forward to its next logical step. |
| 1218 |
* |
| 1219 |
* Return: %TRUE if the crank was successful, %FALSE otherwise. |
| 1220 |
* |
| 1221 |
* MT safe. |
| 1222 |
* |
| 1223 |
* Since: 1.8 |
| 1224 |
*/ |
| 1225 |
gboolean |
| 1226 |
gst_test_clock_crank (GstTestClock * test_clock) |
| 1227 |
{ |
| 1228 |
GstClockID res, pending; |
| 1229 |
GstClockTime now; |
| 1230 |
gboolean result; |
| 1231 |
|
| 1232 |
g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), FALSE); |
| 1233 |
|
| 1234 |
GST_OBJECT_LOCK (test_clock); |
| 1235 |
|
| 1236 |
gst_test_clock_wait_for_next_pending_id_unlocked (test_clock, &pending); |
| 1237 |
now = test_clock->priv->internal_time; |
| 1238 |
if (gst_clock_id_get_time (pending) > now) |
| 1239 |
gst_test_clock_set_time_unlocked (test_clock, |
| 1240 |
gst_clock_id_get_time (pending)); |
| 1241 |
res = gst_test_clock_process_next_clock_id_unlocked (test_clock); |
| 1242 |
g_assert (res == pending); |
| 1243 |
GST_CAT_DEBUG_OBJECT (GST_CAT_TEST_CLOCK, test_clock, |
| 1244 |
"cranked to time %" GST_TIME_FORMAT, |
| 1245 |
GST_TIME_ARGS (test_clock->priv->internal_time)); |
| 1246 |
result = TRUE; |
| 1247 |
|
| 1248 |
GST_OBJECT_UNLOCK (test_clock); |
| 1249 |
|
| 1250 |
if (G_LIKELY (res != NULL)) |
| 1251 |
gst_clock_id_unref (res); |
| 1252 |
|
| 1253 |
gst_clock_id_unref (pending); |
| 1254 |
|
| 1255 |
return result; |
| 1256 |
} |