gstsegment.c 40.2 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3
 *               2009 Wim Taymans <wim.taymans@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * gstsegment.c: Unit test for segments
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
21
 */
22 23 24
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
25 26 27

#include <gst/check/gstcheck.h>

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#define check_times(segment, position, stream_time, running_time) G_STMT_START { \
  guint64 st, rt, pos; \
  \
  st = gst_segment_to_stream_time ((segment), (segment)->format, (position)); \
  rt = gst_segment_to_running_time ((segment), (segment)->format, (position)); \
  GST_DEBUG ("position %" G_GUINT64_FORMAT ", st %" G_GUINT64_FORMAT ", rt %" \
      G_GUINT64_FORMAT, (guint64) (position), (guint64) (stream_time), (guint64) (running_time)); \
  \
  fail_unless_equals_int64 (st, (stream_time)); \
  fail_unless_equals_int64 (rt, (running_time)); \
  if ((stream_time) != -1) { \
    pos = gst_segment_position_from_stream_time ((segment), (segment)->format, st); \
    fail_unless_equals_int64 (pos, (position)); \
  } \
  \
  if ((running_time) != -1) { \
    pos = gst_segment_position_from_running_time ((segment), (segment)->format, rt); \
    fail_unless_equals_int64 (pos, (position)); \
  } \
} G_STMT_END;
Wim Taymans's avatar
Wim Taymans committed
48

49 50 51 52 53
/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_nosize)
{
  GstSegment segment;
  gboolean res;
Wim Taymans's avatar
Wim Taymans committed
54
  guint64 cstart, cstop;
55
  gboolean update;
56 57 58 59

  gst_segment_init (&segment, GST_FORMAT_BYTES);

  /* configure segment to start 100 */
Wim Taymans's avatar
Wim Taymans committed
60
  gst_segment_do_seek (&segment, 1.0,
61
      GST_FORMAT_BYTES,
62 63
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
Stefan Sauer's avatar
Stefan Sauer committed
64 65 66
  fail_unless_equals_uint64 (segment.start, 100);
  fail_unless_equals_uint64 (segment.position, 100);
  fail_unless_equals_uint64 (segment.stop, -1);
67
  fail_unless (update == TRUE);
Wim Taymans's avatar
Wim Taymans committed
68 69
  /* appended after current position 0 */
  check_times (&segment, 100, 100, 0);
70 71 72 73 74 75

  /* do some clipping on the open range */
  /* completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
  fail_unless (res == FALSE);

76
  /* touching lower bound, still outside of the segment */
77 78 79 80 81 82
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* partially inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
83 84
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 150);
85 86 87 88 89

  /* inside, touching lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      100, 150, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
90 91
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 150);
92

93 94 95 96 97
  /* special case, 0 duration and outside segment */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 90, 90, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* special case, 0 duration and touching lower bound, i.e. inside segment */
98 99
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      100, 100, &cstart, &cstop);
100
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
101 102
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 100);
103 104 105 106

  /* special case, 0 duration and inside the segment */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      120, 120, &cstart, &cstop);
107
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
108 109
  fail_unless_equals_uint64 (cstart, 120);
  fail_unless_equals_uint64 (cstop, 120);
110

111 112 113 114
  /* completely inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      150, 200, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
115 116
  fail_unless_equals_uint64 (cstart, 150);
  fail_unless_equals_uint64 (cstop, 200);
117 118 119 120 121

  /* invalid start */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

122
  /* start outside, we don't know the stop */
123 124
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
125 126
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_int64 (cstop, -1);
127 128 129 130

  /* start on lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
131 132
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_int64 (cstop, -1);
133 134 135 136

  /* start inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
137 138
  fail_unless_equals_uint64 (cstart, 150);
  fail_unless_equals_int64 (cstop, -1);
139

Wim Taymans's avatar
Wim Taymans committed
140 141 142 143
  /* move to 150, this is a running_time of 50 */
  segment.position = 150;
  check_times (&segment, 150, 150, 50);

144
  /* add 100 to start, set stop to 300 */
Wim Taymans's avatar
Wim Taymans committed
145
  gst_segment_do_seek (&segment, 1.0,
146
      GST_FORMAT_BYTES,
147
      GST_SEEK_FLAG_NONE,
Wim Taymans's avatar
Wim Taymans committed
148
      GST_SEEK_TYPE_SET, 100 + 100, GST_SEEK_TYPE_SET, 300, &update);
Stefan Sauer's avatar
Stefan Sauer committed
149 150 151 152
  fail_unless_equals_uint64 (segment.start, 200);
  fail_unless_equals_uint64 (segment.position, 200);
  fail_unless_equals_uint64 (segment.stop, 300);
  fail_unless_equals_uint64 (segment.base, 50);
153
  fail_unless (update == TRUE);
Wim Taymans's avatar
Wim Taymans committed
154
  check_times (&segment, 200, 200, 50);
Wim Taymans's avatar
Wim Taymans committed
155
  check_times (&segment, 250, 250, 100);
156

157
  update = FALSE;
Wim Taymans's avatar
Wim Taymans committed
158
  /* add 100 to start (to 300), set stop to 200, this is not allowed.
159
   * nothing should be updated in the segment. A g_warning is
160
   * emitted. */
Wim Taymans's avatar
Wim Taymans committed
161
  ASSERT_CRITICAL (gst_segment_do_seek (&segment, 1.0,
162
          GST_FORMAT_BYTES,
163
          GST_SEEK_FLAG_NONE,
Wim Taymans's avatar
Wim Taymans committed
164
          GST_SEEK_TYPE_SET, 200 + 100, GST_SEEK_TYPE_SET, 200, &update));
Stefan Sauer's avatar
Stefan Sauer committed
165 166 167 168
  fail_unless_equals_uint64 (segment.start, 200);
  fail_unless_equals_uint64 (segment.position, 200);
  fail_unless_equals_uint64 (segment.stop, 300);
  fail_unless_equals_uint64 (segment.base, 50);
169 170
  /* update didn't change */
  fail_unless (update == FALSE);
Wim Taymans's avatar
Wim Taymans committed
171
  check_times (&segment, 200, 200, 50);
Wim Taymans's avatar
Wim Taymans committed
172
  check_times (&segment, 250, 250, 100);
173

174
  update = TRUE;
175 176
  /* seek relative to end, should not do anything since size is
   * unknown. */
Wim Taymans's avatar
Wim Taymans committed
177
  gst_segment_do_seek (&segment, 1.0,
178
      GST_FORMAT_BYTES,
179 180
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100, &update);
Stefan Sauer's avatar
Stefan Sauer committed
181 182 183 184
  fail_unless_equals_uint64 (segment.start, 200);
  fail_unless_equals_uint64 (segment.position, 200);
  fail_unless_equals_uint64 (segment.stop, 300);
  fail_unless_equals_uint64 (segment.base, 50);
185
  fail_unless (update == FALSE);
Wim Taymans's avatar
Wim Taymans committed
186
  check_times (&segment, 250, 250, 100);
187 188 189 190 191 192 193 194 195 196 197 198

  /* completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* touching lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* partially inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
199 200
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 250);
201 202 203 204 205

  /* inside, touching lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      200, 250, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
206 207
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 250);
208 209 210 211 212

  /* completely inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      250, 290, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
213 214
  fail_unless_equals_uint64 (cstart, 250);
  fail_unless_equals_uint64 (cstop, 290);
215 216 217 218 219

  /* partially inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      250, 350, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
220 221
  fail_unless_equals_uint64 (cstart, 250);
  fail_unless_equals_uint64 (cstop, 300);
222 223 224 225 226 227 228 229

  /* invalid start */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* start outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
230 231
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 300);
232 233 234 235

  /* start on lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
236 237
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 300);
238 239 240 241

  /* start inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
242 243
  fail_unless_equals_uint64 (cstart, 250);
  fail_unless_equals_uint64 (cstop, 300);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

  /* start outside on boundary */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* start completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
  fail_unless (res == FALSE);
}

GST_END_TEST;

/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_size)
{
  GstSegment segment;
  gboolean res;
Wim Taymans's avatar
Wim Taymans committed
261
  guint64 cstart, cstop;
262
  gboolean update;
263 264

  gst_segment_init (&segment, GST_FORMAT_BYTES);
Wim Taymans's avatar
Wim Taymans committed
265
  segment.duration = 200;
266 267

  /* configure segment to start 100 */
Wim Taymans's avatar
Wim Taymans committed
268
  gst_segment_do_seek (&segment, 1.0,
269
      GST_FORMAT_BYTES,
270 271
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
Stefan Sauer's avatar
Stefan Sauer committed
272 273 274
  fail_unless_equals_uint64 (segment.start, 100);
  fail_unless_equals_uint64 (segment.position, 100);
  fail_unless_equals_int64 (segment.stop, -1);
275
  fail_unless (update == TRUE);
Wim Taymans's avatar
Wim Taymans committed
276
  check_times (&segment, 100, 100, 0);
277 278 279 280 281 282 283 284 285 286 287 288 289

  /* do some clipping on the open range */
  /* completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* touching lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* partially inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
290 291
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 150);
292 293 294 295 296

  /* inside, touching lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      100, 150, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
297 298
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 150);
299 300 301 302 303

  /* completely inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      150, 200, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
304 305
  fail_unless_equals_uint64 (cstart, 150);
  fail_unless_equals_uint64 (cstop, 200);
306 307 308 309 310 311 312 313

  /* invalid start */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* start outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
314 315
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_int64 (cstop, -1);
316 317 318 319

  /* start on lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
320 321
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, -1);
322 323 324 325

  /* start inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
326 327
  fail_unless_equals_uint64 (cstart, 150);
  fail_unless_equals_uint64 (cstop, -1);
328 329

  /* add 100 to start, set stop to 300, stop clips to 200 */
Wim Taymans's avatar
Wim Taymans committed
330
  gst_segment_do_seek (&segment, 1.0,
331
      GST_FORMAT_BYTES,
332
      GST_SEEK_FLAG_NONE,
Wim Taymans's avatar
Wim Taymans committed
333
      GST_SEEK_TYPE_SET, 100 + 100, GST_SEEK_TYPE_SET, 300, &update);
Stefan Sauer's avatar
Stefan Sauer committed
334 335 336
  fail_unless_equals_uint64 (segment.start, 200);
  fail_unless_equals_uint64 (segment.position, 200);
  fail_unless_equals_uint64 (segment.stop, 200);
Wim Taymans's avatar
Wim Taymans committed
337
  check_times (&segment, 200, 200, 0);
338 339 340

  /* add 100 to start (to 300), set stop to 200, this clips start
   * to duration */
Wim Taymans's avatar
Wim Taymans committed
341
  gst_segment_do_seek (&segment, 1.0,
342
      GST_FORMAT_BYTES,
343
      GST_SEEK_FLAG_NONE,
Wim Taymans's avatar
Wim Taymans committed
344
      GST_SEEK_TYPE_SET, 200 + 100, GST_SEEK_TYPE_SET, 200, &update);
Stefan Sauer's avatar
Stefan Sauer committed
345 346 347
  fail_unless_equals_uint64 (segment.start, 200);
  fail_unless_equals_uint64 (segment.position, 200);
  fail_unless_equals_uint64 (segment.stop, 200);
348
  fail_unless (update == FALSE);
Wim Taymans's avatar
Wim Taymans committed
349
  check_times (&segment, 200, 200, 0);
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  /* special case, segment's start and stop are identical */
  /* completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* completely outside also */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      250, 300, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* stop at boundary point. it's outside because stop is exclusive */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      100, 200, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* touching boundary point. it's inside because start at segment start */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      200, 300, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
370 371
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 200);
372 373 374 375 376

  /* completely inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      200, 200, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
377 378
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 200);
379 380 381 382 383

  /* exclusively cover boundary point */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      150, 250, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
384 385
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 200);
386 387 388 389 390 391 392 393

  /* invalid start */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 200, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* start outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
394 395
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 200);
396 397 398 399

  /* start on boundary point */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
400 401
  fail_unless_equals_uint64 (cstart, 200);
  fail_unless_equals_uint64 (cstop, 200);
402 403 404 405 406

  /* start completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
  fail_unless (res == FALSE);

407
  /* seek relative to end */
Wim Taymans's avatar
Wim Taymans committed
408
  gst_segment_do_seek (&segment, 1.0,
409
      GST_FORMAT_BYTES,
410 411
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
Stefan Sauer's avatar
Stefan Sauer committed
412 413 414
  fail_unless_equals_uint64 (segment.start, 100);
  fail_unless_equals_uint64 (segment.position, 100);
  fail_unless_equals_uint64 (segment.stop, 180);
415
  fail_unless (update == TRUE);
Wim Taymans's avatar
Wim Taymans committed
416
  check_times (&segment, 150, 150, 50);
417 418 419 420 421 422 423 424 425 426 427 428

  /* completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* touching lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* partially inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
429 430
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 150);
431 432 433 434 435

  /* inside, touching lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      100, 150, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
436 437
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 150);
438 439 440 441 442

  /* completely inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      150, 170, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
443 444
  fail_unless_equals_uint64 (cstart, 150);
  fail_unless_equals_uint64 (cstop, 170);
445 446 447 448 449

  /* partially inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
      150, 250, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
450 451
  fail_unless_equals_uint64 (cstart, 150);
  fail_unless_equals_uint64 (cstop, 180);
452 453 454 455 456 457 458 459

  /* invalid start */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* start outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
460 461
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 180);
462 463 464 465

  /* start on lower bound */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
466 467
  fail_unless_equals_uint64 (cstart, 100);
  fail_unless_equals_uint64 (cstop, 180);
468 469 470 471

  /* start inside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
  fail_unless (res == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
472 473
  fail_unless_equals_uint64 (cstart, 150);
  fail_unless_equals_uint64 (cstop, 180);
474 475 476 477 478 479 480 481 482 483 484

  /* start outside on boundary */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
  fail_unless (res == FALSE);

  /* start completely outside */
  res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
  fail_unless (res == FALSE);
}

GST_END_TEST;
485

486 487 488 489 490 491
GST_START_TEST (segment_seek_reverse)
{
  GstSegment segment;
  gboolean update;

  gst_segment_init (&segment, GST_FORMAT_BYTES);
Wim Taymans's avatar
Wim Taymans committed
492
  segment.duration = 200;
493 494

  /* configure segment to stop 100 */
Wim Taymans's avatar
Wim Taymans committed
495
  gst_segment_do_seek (&segment, -1.0,
496 497 498
      GST_FORMAT_BYTES,
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 100, &update);
Stefan Sauer's avatar
Stefan Sauer committed
499 500 501 502
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 100);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 100);
503
  fail_unless (update == TRUE);
Wim Taymans's avatar
Wim Taymans committed
504 505 506
  check_times (&segment, 100, 100, 0);
  check_times (&segment, 50, 50, 50);
  check_times (&segment, 0, 0, 100);
507 508

  /* update */
Wim Taymans's avatar
Wim Taymans committed
509
  gst_segment_do_seek (&segment, -1.0,
510 511
      GST_FORMAT_BYTES,
      GST_SEEK_FLAG_NONE,
Wim Taymans's avatar
Wim Taymans committed
512
      GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_SET, 100 - 20, &update);
Stefan Sauer's avatar
Stefan Sauer committed
513 514 515 516
  fail_unless_equals_uint64 (segment.start, 10);
  fail_unless_equals_uint64 (segment.stop, 80);
  fail_unless_equals_uint64 (segment.time, 10);
  fail_unless_equals_uint64 (segment.position, 80);
517
  fail_unless (update == TRUE);
Wim Taymans's avatar
Wim Taymans committed
518 519 520
  check_times (&segment, 80, 80, 0);
  check_times (&segment, 40, 40, 40);
  check_times (&segment, 10, 10, 70);
521

Wim Taymans's avatar
Wim Taymans committed
522
  gst_segment_do_seek (&segment, -1.0,
523 524 525
      GST_FORMAT_BYTES,
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_SET, 20, GST_SEEK_TYPE_NONE, 0, &update);
Stefan Sauer's avatar
Stefan Sauer committed
526 527 528 529
  fail_unless_equals_uint64 (segment.start, 20);
  fail_unless_equals_uint64 (segment.stop, 80);
  fail_unless_equals_uint64 (segment.time, 20);
  fail_unless_equals_uint64 (segment.position, 80);
530
  fail_unless (update == FALSE);
Wim Taymans's avatar
Wim Taymans committed
531 532
  check_times (&segment, 80, 80, 0);
  check_times (&segment, 20, 20, 60);
533 534 535 536
}

GST_END_TEST;

537 538 539 540 541 542 543 544
/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_rate)
{
  GstSegment segment;
  gboolean update;

  gst_segment_init (&segment, GST_FORMAT_BYTES);

Wim Taymans's avatar
Wim Taymans committed
545
  /* configure segment to rate 2.0 */
Wim Taymans's avatar
Wim Taymans committed
546
  gst_segment_do_seek (&segment, 2.0,
Wim Taymans's avatar
Wim Taymans committed
547
      GST_FORMAT_BYTES,
548 549 550
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1, &update);
  fail_unless (segment.format == GST_FORMAT_BYTES);
Stefan Sauer's avatar
Stefan Sauer committed
551 552 553 554
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.position, 0);
  fail_unless_equals_int64 (segment.stop, -1);
  fail_unless_equals_float (segment.rate, 2.0);
555
  fail_unless (update == FALSE);
Wim Taymans's avatar
Wim Taymans committed
556
  check_times (&segment, 50, 50, 25);
557 558

  /* set a real stop position, this must happen in bytes */
Wim Taymans's avatar
Wim Taymans committed
559
  gst_segment_do_seek (&segment, 3.0,
560 561 562 563
      GST_FORMAT_BYTES,
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, 100, &update);
  fail_unless (segment.format == GST_FORMAT_BYTES);
Stefan Sauer's avatar
Stefan Sauer committed
564 565 566
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 100);
  fail_unless_equals_float (segment.rate, 3.0);
567 568 569
  /* no seek should happen, we just updated the stop position in forward
   * playback mode.*/
  fail_unless (update == FALSE);
Wim Taymans's avatar
Wim Taymans committed
570
  check_times (&segment, 60, 60, 20);
571 572 573

  /* set some duration, stop -1 END seeks will now work with the
   * duration, if the formats match */
Wim Taymans's avatar
Wim Taymans committed
574
  segment.duration = 200;
575

Wim Taymans's avatar
Wim Taymans committed
576
  /* seek to end with 0 should set the stop to the duration */
Wim Taymans's avatar
Wim Taymans committed
577
  gst_segment_do_seek (&segment, 2.0,
Wim Taymans's avatar
Wim Taymans committed
578
      GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
579
      GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
Stefan Sauer's avatar
Stefan Sauer committed
580 581
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.duration, 200);
582 583

  /* subtract 100 from the end */
Wim Taymans's avatar
Wim Taymans committed
584
  gst_segment_do_seek (&segment, 2.0,
585 586
      GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, -100, &update);
Stefan Sauer's avatar
Stefan Sauer committed
587 588
  fail_unless_equals_uint64 (segment.stop, 100);
  fail_unless_equals_uint64 (segment.duration, 200);
589 590

  /* add 100 to the duration, this should be clamped to the duration */
Wim Taymans's avatar
Wim Taymans committed
591
  gst_segment_do_seek (&segment, 2.0,
592 593
      GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 100, &update);
Stefan Sauer's avatar
Stefan Sauer committed
594 595
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.duration, 200);
596 597 598 599
}

GST_END_TEST;

600 601 602 603 604 605 606 607 608 609
GST_START_TEST (segment_copy)
{
  GstSegment *copy;
  GstSegment segment = { 0.0, };

  /* this is a boxed type copy function, we support copying NULL */
  fail_unless (gst_segment_copy (NULL) == NULL);

  gst_segment_init (&segment, GST_FORMAT_TIME);

Wim Taymans's avatar
Wim Taymans committed
610 611 612 613 614
  segment.rate = -1.0;
  segment.applied_rate = 1.0;
  segment.start = 0;
  segment.stop = 200;
  segment.time = 0;
615 616 617 618 619 620 621 622 623 624 625

  copy = gst_segment_copy (&segment);
  fail_unless (copy != NULL);
  /* we inited the struct on the stack to zeroes, so direct comparison should
   * be ok here despite the padding field and regardless of implementation */
  fail_unless (memcmp (copy, &segment, sizeof (GstSegment)) == 0);
  gst_segment_free (copy);
}

GST_END_TEST;

Wim Taymans's avatar
Wim Taymans committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_noupdate)
{
  GstSegment segment;
  gboolean update;

  gst_segment_init (&segment, GST_FORMAT_TIME);

  segment.start = 0;
  segment.position = 50;
  segment.stop = 200;
  segment.time = 0;

  /* doesn't change anything */
  gst_segment_do_seek (&segment, 1.0,
      GST_FORMAT_TIME,
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_NONE, 0, GST_SEEK_TYPE_NONE, 0, &update);
  fail_unless (update == FALSE);
  fail_unless (segment.format == GST_FORMAT_TIME);
Stefan Sauer's avatar
Stefan Sauer committed
646 647 648 649 650 651
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 50);
  fail_unless_equals_uint64 (segment.offset, 50);
Wim Taymans's avatar
Wim Taymans committed
652 653 654 655 656 657 658

  gst_segment_do_seek (&segment, 2.0,
      GST_FORMAT_TIME,
      GST_SEEK_FLAG_NONE,
      GST_SEEK_TYPE_NONE, 0, GST_SEEK_TYPE_NONE, 0, &update);
  fail_unless (update == FALSE);
  fail_unless (segment.format == GST_FORMAT_TIME);
Stefan Sauer's avatar
Stefan Sauer committed
659 660 661 662 663 664
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 50);
  fail_unless_equals_uint64 (segment.offset, 50);
Wim Taymans's avatar
Wim Taymans committed
665 666 667 668 669 670 671

  gst_segment_do_seek (&segment, 1.0,
      GST_FORMAT_TIME,
      GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_NONE, 0, GST_SEEK_TYPE_NONE, 0, &update);
  fail_unless (update == FALSE);
  fail_unless (segment.format == GST_FORMAT_TIME);
Stefan Sauer's avatar
Stefan Sauer committed
672 673 674 675 676 677
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 0);
  fail_unless_equals_uint64 (segment.offset, 50);
Wim Taymans's avatar
Wim Taymans committed
678 679 680 681
}

GST_END_TEST;

682 683 684 685 686 687 688 689 690 691 692
GST_START_TEST (segment_offset)
{
  GstSegment segment;

  gst_segment_init (&segment, GST_FORMAT_TIME);

  segment.start = 0;
  segment.position = 50;
  segment.stop = 200;
  segment.time = 0;

693 694
  check_times (&segment, 20, 20, 20);
  check_times (&segment, 220, -1, -1);
695 696 697

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          0) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
698 699 700 701 702 703
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 0);
  fail_unless_equals_uint64 (segment.offset, 0);
704
  check_times (&segment, 20, 20, 20);
705 706 707

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          100) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
708 709 710 711 712 713
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 100);
  fail_unless_equals_uint64 (segment.offset, 0);
714
  check_times (&segment, 20, 20, 120);
715 716 717

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -50) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
718 719 720 721 722 723
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 50);
  fail_unless_equals_uint64 (segment.offset, 0);
724
  check_times (&segment, 20, 20, 70);
725 726 727

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -100) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
728 729 730 731 732 733
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 0);
  fail_unless_equals_uint64 (segment.offset, 50);
734 735
  check_times (&segment, 20, 20, -1);
  check_times (&segment, 200, 200, 150);
736 737 738 739

  /* can go negative */
  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -151) == FALSE);
Stefan Sauer's avatar
Stefan Sauer committed
740 741 742 743 744 745
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 0);
  fail_unless_equals_uint64 (segment.offset, 50);
746 747
  check_times (&segment, 100, 100, 50);
  check_times (&segment, 200, 200, 150);
748 749 750

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -150) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
751 752 753 754 755 756
  fail_unless_equals_uint64 (segment.start, 0);
  fail_unless_equals_uint64 (segment.stop, 200);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 0);
  fail_unless_equals_uint64 (segment.offset, 200);
757
  check_times (&segment, 200, 200, 0);
758 759 760 761 762 763 764 765 766 767 768 769 770

  gst_segment_init (&segment, GST_FORMAT_TIME);

  segment.start = 20;
  segment.position = 50;
  segment.stop = 220;
  segment.time = 0;

  check_times (&segment, 40, 20, 20);
  check_times (&segment, 240, -1, -1);

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          0) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
771 772 773 774 775 776
  fail_unless_equals_uint64 (segment.start, 20);
  fail_unless_equals_uint64 (segment.stop, 220);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 0);
  fail_unless_equals_uint64 (segment.offset, 0);
777 778 779 780
  check_times (&segment, 40, 20, 20);

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          100) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
781 782 783 784 785 786
  fail_unless_equals_uint64 (segment.start, 20);
  fail_unless_equals_uint64 (segment.stop, 220);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 100);
  fail_unless_equals_uint64 (segment.offset, 0);
787 788 789 790
  check_times (&segment, 40, 20, 120);

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -50) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
791 792 793 794 795 796
  fail_unless_equals_uint64 (segment.start, 20);
  fail_unless_equals_uint64 (segment.stop, 220);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 50);
  fail_unless_equals_uint64 (segment.offset, 0);
797 798 799 800
  check_times (&segment, 40, 20, 70);

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -100) == TRUE);
Stefan Sauer's avatar
Stefan Sauer committed
801 802 803 804 805 806
  fail_unless_equals_uint64 (segment.start, 20);
  fail_unless_equals_uint64 (segment.stop, 220);
  fail_unless_equals_uint64 (segment.time, 0);
  fail_unless_equals_uint64 (segment.position, 50);
  fail_unless_equals_uint64 (segment.base, 0);
  fail_unless_equals_uint64 (segment.offset, 50);
807 808
  check_times (&segment, 40, 20, -1);
  check_times (&segment, 220, 200, 150);
809 810 811 812
}

GST_END_TEST;

813 814 815
GST_START_TEST (segment_full)
{
  GstSegment segment;
816
  guint64 rt, pos;
817 818 819 820 821 822 823 824 825 826 827 828

  gst_segment_init (&segment, GST_FORMAT_TIME);

  segment.start = 50;
  segment.position = 150;
  segment.stop = 200;
  segment.time = 0;

  check_times (&segment, 100, 50, 50);
  check_times (&segment, 220, -1, -1);

  fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
829
          50, &rt) == 1);
830
  fail_unless (rt == 0);
831 832 833
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, rt, &pos) == 1);
  fail_unless (pos == 50);
834
  fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
835
          200, &rt) == 1);
836
  fail_unless (rt == 150);
837 838 839
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, rt, &pos) == 1);
  fail_unless (pos == 200);
840 841 842 843 844 845 846 847 848 849 850 851
  fail_unless (!gst_segment_clip (&segment, GST_FORMAT_TIME, 40, 40, NULL,
          NULL));
  fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME, 40,
          &rt) == -1);
  fail_unless (!gst_segment_clip (&segment, GST_FORMAT_TIME, 49, 49, NULL,
          NULL));
  fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME, 49,
          &rt) == -1);
  fail_unless (!gst_segment_clip (&segment, GST_FORMAT_TIME, 201, 201, NULL,
          NULL));
  fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME, 201,
          &rt) == 1);
852 853 854
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, rt, &pos) == 1);
  fail_unless (pos == 201);
855 856 857 858 859 860

  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -50) == TRUE);
  fail_unless (segment.offset == 50);

  fail_unless (gst_segment_to_running_time_full (&segment, GST_FORMAT_TIME,
861
          50, &rt) == -1);
862 863
  GST_DEBUG ("%" G_GUINT64_FORMAT, rt);
  fail_unless (rt == 50);
864

865 866 867 868 869 870 871 872 873
  /* Forward jump will be applied directly to the base field */
  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          50) == TRUE);
  fail_unless (segment.base == 50);
  /* Now there's enough base, a negative offset should come from there */
  fail_unless (gst_segment_offset_running_time (&segment, GST_FORMAT_TIME,
          -50) == TRUE);
  fail_unless (segment.base == 0);

874 875 876 877 878 879 880
  segment.start = 50;
  segment.stop = 300;
  segment.position = 150;
  segment.time = 0;
  segment.offset = 0;
  gst_segment_set_running_time (&segment, GST_FORMAT_TIME, 100);
  fail_unless_equals_int (segment.base, 100);
881

882
  fail_unless (gst_segment_position_from_running_time_full (&segment,
883 884 885
          GST_FORMAT_TIME, 70, &pos) == 1);
  fail_unless_equals_int (pos, 120);

886 887 888
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 140, &pos) == 1);
  fail_unless_equals_int (pos, 190);
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979

  /* Test a non-1.0 rate that lands right before the segment, but still +ve */
  segment.rate = 1.1;
  segment.start = 100;
  segment.offset = 0;
  segment.stop = 500;
  segment.position = 40;
  segment.base = 150;
  segment.time = 10000;
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 140, &pos) == 1);
  fail_unless (pos == 89);
  /* And now one that should give a position < 0 */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 0, &pos) == -1);
  fail_unless (pos == 65);

  /* Test a non-1.0 negative rate that lands right after the (reversed) segment, but still +ve position */
  segment.rate = -2.0;
  segment.start = 100;
  segment.offset = 0;
  segment.stop = 500;
  segment.position = 150;
  segment.base = 133;
  segment.time = 10000;
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 200 + 133 + 20, &pos) == 1);
  fail_unless (pos == 60);
  /* And now one that should give a position < 0, reported as a negated value */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 200 + 133 + 70, &pos) == -1);
  fail_unless (pos == 40);

  /* Test gst_segment_position_from_running_time_full() with offsets */
  segment.rate = 2.0;
  segment.start = 100;
  segment.offset = 100;
  segment.stop = 500;
  segment.position = 150;
  segment.base = 175;
  segment.time = 10000;
  /* Position before the segment but >= 0 */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 75, &pos) == 1);
  fail_unless (pos == 0);
  fail_unless (gst_segment_position_from_running_time (&segment,
          GST_FORMAT_TIME, 75) == -1);

  /* Position before the segment and < 0 */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 65, &pos) == -1);
  fail_unless (pos == 20);      /* Actually -20 */
  fail_unless (gst_segment_position_from_running_time (&segment,
          GST_FORMAT_TIME, 65) == -1);

  /* After the segment */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 175 + 150 + 10, &pos) == 1);
  fail_unless (pos == 520);
  fail_unless (gst_segment_position_from_running_time (&segment,
          GST_FORMAT_TIME, 175 + 150 + 10) == -1);

  /* And with negative rate, so the segment is reversed */
  segment.rate = -2.0;

  /* Before the segment */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 75, &pos) == 1);
  fail_unless (pos == 600);
  fail_unless (gst_segment_position_from_running_time (&segment,
          GST_FORMAT_TIME, 75) == -1);

  /* Position after the segment and < 0 */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 400, &pos) == -1);
  fail_unless (pos == 50);      /* Actually -50 */
  fail_unless (gst_segment_position_from_running_time (&segment,
          GST_FORMAT_TIME, 400) == -1);

  /* Position after the segment and >= 0 */
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 325 + 10, &pos) == 1);
  fail_unless (pos == 80);
  fail_unless (gst_segment_position_from_running_time (&segment,
          GST_FORMAT_TIME, 325 + 10) == -1);

  /* Big offset can clip away an entire reversed segment and produce a negative position anyway */
  segment.offset = 1000;
  fail_unless (gst_segment_position_from_running_time_full (&segment,
          GST_FORMAT_TIME, 75, &pos) == -1);
  fail_unless (pos == 300);     /* Actually -300 */
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
}

GST_END_TEST;

GST_START_TEST (segment_stream_time_full)
{
  GstSegment segment;
  guint64 st, pos;

  gst_segment_init (&segment, GST_FORMAT_TIME);

  segment.start = 50;
  segment.stop = 200;
  segment.time = 30;
  segment.position = 0;

  fail_unless (gst_segment_to_stream_time_full (&segment, GST_FORMAT_TIME,
          0, &st) == -1);
  fail_unless_equals_int (st, 20);
  fail_unless (gst_segment_to_stream_time_full (&segment, GST_FORMAT_TIME,
          20, &st) == 1);
  fail_unless_equals_int (st, 0);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 0, &pos) == 1);
  fail_unless_equals_int (pos, 20);
  fail_unless (gst_segment_to_stream_time_full (&segment, GST_FORMAT_TIME,
          10, &st) == -1);
  fail_unless_equals_int (st, 10);
  fail_unless (gst_segment_to_stream_time_full (&segment, GST_FORMAT_TIME,
          40, &st) == 1);
  fail_unless_equals_int (st, 20);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, st, &pos) == 1);
  fail_unless_equals_int (pos, 40);
  segment.time = 100;
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 40, &pos) == -1);
  fail_unless_equals_int (pos, 10);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 60, &pos) == 1);
  fail_unless_equals_int (pos, 10);

  segment.start = 50;
  segment.position = 150;
  segment.stop = 200;
  segment.time = 0;
  segment.applied_rate = -1;
  segment.rate = -1;

  fail_unless (gst_segment_to_stream_time_full (&segment, GST_FORMAT_TIME,
          0, &st) == 1);
  fail_unless_equals_int (st, 200);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 200, &pos) == 1);
  fail_unless_equals_int (pos, 0);
  fail_unless (gst_segment_to_stream_time_full (&segment, GST_FORMAT_TIME,
          250, &st) == -1);
  fail_unless_equals_int (st, 50);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 200, &pos) == 1);
  fail_unless_equals_int (pos, 0);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 250, &pos) == -1);
  fail_unless_equals_int (pos, 50);

  segment.time = 70;
  fail_unless (gst_segment_to_stream_time_full (&segment, GST_FORMAT_TIME,
          250, &st) == 1);
  fail_unless_equals_int (st, 20);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 50, &pos) == 1);
  fail_unless_equals_int (pos, 220);
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 90, &pos) == 1);
  fail_unless_equals_int (pos, 180);

  segment.stop = 60;
  fail_unless (gst_segment_position_from_stream_time_full (&segment,
          GST_FORMAT_TIME, 5, &pos) == 1);
  fail_unless_equals_int (pos, 125);
1060 1061 1062
}

GST_END_TEST;
Wim Taymans's avatar
Wim Taymans committed
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
GST_START_TEST (segment_negative_rate)
{
  GstSegment segment;

  gst_segment_init (&segment, GST_FORMAT_TIME);

  segment.start = 50;
  segment.position = 150;
  segment.stop = 200;
  segment.time = 0;
  segment.applied_rate = -1;
  segment.rate = -1;

  /* somewhere in the middle */
  check_times (&segment, 100, 100, 100);
  /* after stop */
  check_times (&segment, 220, -1, -1);
  /* before start */
  check_times (&segment, 10, -1, -1);
  /* at segment start */
  check_times (&segment, 50, 150, 150);
  /* another place in the middle */
  check_times (&segment, 150, 50, 50);
  /* at segment stop */
  check_times (&segment, 200, 0, 0);

  segment.time = 100;
  segment.base = 100;
  /* somewhere in the middle */
  check_times (&segment, 100, 200, 200);
  /* at segment start */
  check_times (&segment, 50, 250, 250);
  /* another place in the middle */
  check_times (&segment, 150, 150, 150);
  /* at segment stop */
  check_times (&segment, 200, 100, 100);
}

GST_END_TEST;

GST_START_TEST (segment_negative_applied_rate)
{
  GstSegment segment;

  gst_segment_init (&segment, GST_FORMAT_TIME);

  segment.start = 50;
  segment.position = 150;
  segment.stop = 200;
  segment.time = 0;
  segment.applied_rate = -1;
  segment.rate = 1;

  /* somewhere in the middle */
  check_times (&segment, 100, 100, 50);
  /* after stop */
  check_times (&segment, 220, -1, -1);
  /* before start */
  check_times (&segment, 10, -1, -1);
  /* at segment start */
  check_times (&segment, 50, 150, 0);
  /* another place in the middle */
  check_times (&segment, 150, 50, 100);
  /* at segment stop */
  check_times (&segment, 200, 0, 150);

  segment.time = 100;
  segment.base = 100;
  /* somewhere in the middle */
  check_times (&segment, 100, 200, 150);
  /* at segment start */
  check_times (&segment, 50, 250, 100);
  /* another place in the middle */
  check_times (&segment, 150, 150, 200);
  /* at segment stop */
  check_times (&segment, 200, 100, 250);
}

GST_END_TEST;

1144
static Suite *
1145
gst_segment_suite (void)
1146 1147 1148 1149 1150 1151 1152 1153 1154
{
  Suite *s = suite_create ("GstSegment");
  TCase *tc_chain = tcase_create ("segments");

  tcase_set_timeout (tc_chain, 20);

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, segment_seek_nosize);
  tcase_add_test (tc_chain, segment_seek_size);
1155
  tcase_add_test (tc_chain, segment_seek_reverse);
1156
  tcase_add_test (tc_chain, segment_seek_rate);
1157
  tcase_add_test (tc_chain, segment_copy);
Wim Taymans's avatar
Wim Taymans committed
1158
  tcase_add_test (tc_chain, segment_seek_noupdate);
1159
  tcase_add_test (tc_chain, segment_offset);
1160
  tcase_add_test (tc_chain, segment_full);
1161 1162
  tcase_add_test (tc_chain, segment_negative_rate);
  tcase_add_test (tc_chain, segment_negative_applied_rate);
1163
  tcase_add_test (tc_chain, segment_stream_time_full);
1164

1165 1166 1167
  return s;
}

1168
GST_CHECK_MAIN (gst_segment);