2 * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3 * 2009 Wim Taymans <wim.taymans@gmail.com>
5 * gstsegment.c: Unit test for segments
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include <gst/check/gstcheck.h>
25 /* mess with the segment structure in the bytes format */
26 GST_START_TEST (segment_seek_nosize)
33 gst_segment_init (&segment, GST_FORMAT_BYTES);
35 /* configure segment to start 100 */
36 gst_segment_set_seek (&segment, 1.0,
39 GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
40 fail_unless (segment.start == 100);
41 fail_unless (segment.stop == -1);
42 fail_unless (update == TRUE);
44 /* configure segment to stop relative, should not do anything since
46 gst_segment_set_seek (&segment, 1.0,
49 GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
50 fail_unless (segment.start == 100);
51 fail_unless (segment.stop == -1);
52 fail_unless (update == FALSE);
54 /* do some clipping on the open range */
55 /* completely outside */
56 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
57 fail_unless (res == FALSE);
59 /* touching lower bound, still outside of the segment */
60 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
61 fail_unless (res == FALSE);
63 /* partially inside */
64 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
65 fail_unless (res == TRUE);
66 fail_unless (cstart == 100);
67 fail_unless (cstop == 150);
69 /* inside, touching lower bound */
70 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
71 100, 150, &cstart, &cstop);
72 fail_unless (res == TRUE);
73 fail_unless (cstart == 100);
74 fail_unless (cstop == 150);
76 /* special case, 0 duration and outside segment */
77 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 90, 90, &cstart, &cstop);
78 fail_unless (res == FALSE);
80 /* special case, 0 duration and touching lower bound, i.e. inside segment */
81 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
82 100, 100, &cstart, &cstop);
83 fail_unless (res == TRUE);
84 fail_unless (cstart == 100);
85 fail_unless (cstop == 100);
87 /* special case, 0 duration and inside the segment */
88 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
89 120, 120, &cstart, &cstop);
90 fail_unless (res == TRUE);
91 fail_unless (cstart == 120);
92 fail_unless (cstop == 120);
94 /* completely inside */
95 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
96 150, 200, &cstart, &cstop);
97 fail_unless (res == TRUE);
98 fail_unless (cstart == 150);
99 fail_unless (cstop == 200);
102 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
103 fail_unless (res == FALSE);
105 /* start outside, we don't know the stop */
106 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
107 fail_unless (res == TRUE);
108 fail_unless (cstart == 100);
109 fail_unless (cstop == -1);
111 /* start on lower bound */
112 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
113 fail_unless (res == TRUE);
114 fail_unless (cstart == 100);
115 fail_unless (cstop == -1);
118 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
119 fail_unless (res == TRUE);
120 fail_unless (cstart == 150);
121 fail_unless (cstop == -1);
123 /* add 100 to start, set stop to 300 */
124 gst_segment_set_seek (&segment, 1.0,
127 GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
128 fail_unless (segment.start == 200);
129 fail_unless (segment.stop == 300);
130 fail_unless (update == TRUE);
133 /* add 100 to start (to 300), set stop to 200, this is not allowed.
134 * nothing should be updated in the segment. A g_warning is
136 ASSERT_CRITICAL (gst_segment_set_seek (&segment, 1.0,
139 GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update));
140 fail_unless (segment.start == 200);
141 fail_unless (segment.stop == 300);
142 /* update didn't change */
143 fail_unless (update == FALSE);
146 /* seek relative to end, should not do anything since size is
148 gst_segment_set_seek (&segment, 1.0,
151 GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100, &update);
152 fail_unless (segment.start == 200);
153 fail_unless (segment.stop == 300);
154 fail_unless (update == FALSE);
156 /* completely outside */
157 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
158 fail_unless (res == FALSE);
160 /* touching lower bound */
161 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
162 fail_unless (res == FALSE);
164 /* partially inside */
165 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
166 fail_unless (res == TRUE);
167 fail_unless (cstart == 200);
168 fail_unless (cstop == 250);
170 /* inside, touching lower bound */
171 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
172 200, 250, &cstart, &cstop);
173 fail_unless (res == TRUE);
174 fail_unless (cstart == 200);
175 fail_unless (cstop == 250);
177 /* completely inside */
178 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
179 250, 290, &cstart, &cstop);
180 fail_unless (res == TRUE);
181 fail_unless (cstart == 250);
182 fail_unless (cstop == 290);
184 /* partially inside */
185 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
186 250, 350, &cstart, &cstop);
187 fail_unless (res == TRUE);
188 fail_unless (cstart == 250);
189 fail_unless (cstop == 300);
192 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
193 fail_unless (res == FALSE);
196 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
197 fail_unless (res == TRUE);
198 fail_unless (cstart == 200);
199 fail_unless (cstop == 300);
201 /* start on lower bound */
202 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
203 fail_unless (res == TRUE);
204 fail_unless (cstart == 200);
205 fail_unless (cstop == 300);
208 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
209 fail_unless (res == TRUE);
210 fail_unless (cstart == 250);
211 fail_unless (cstop == 300);
213 /* start outside on boundary */
214 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
215 fail_unless (res == FALSE);
217 /* start completely outside */
218 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
219 fail_unless (res == FALSE);
224 /* mess with the segment structure in the bytes format */
225 GST_START_TEST (segment_seek_size)
229 gint64 cstart, cstop;
232 gst_segment_init (&segment, GST_FORMAT_BYTES);
233 gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
235 /* configure segment to start 100 */
236 gst_segment_set_seek (&segment, 1.0,
239 GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
240 fail_unless (segment.start == 100);
241 fail_unless (segment.stop == -1);
242 fail_unless (update == TRUE);
244 /* configure segment to stop relative, does not update stop
245 * since we did not set it before. */
246 gst_segment_set_seek (&segment, 1.0,
249 GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
250 fail_unless (segment.start == 100);
251 fail_unless (segment.stop == -1);
252 fail_unless (update == FALSE);
254 /* do some clipping on the open range */
255 /* completely outside */
256 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
257 fail_unless (res == FALSE);
259 /* touching lower bound */
260 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
261 fail_unless (res == FALSE);
263 /* partially inside */
264 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
265 fail_unless (res == TRUE);
266 fail_unless (cstart == 100);
267 fail_unless (cstop == 150);
269 /* inside, touching lower bound */
270 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
271 100, 150, &cstart, &cstop);
272 fail_unless (res == TRUE);
273 fail_unless (cstart == 100);
274 fail_unless (cstop == 150);
276 /* completely inside */
277 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
278 150, 200, &cstart, &cstop);
279 fail_unless (res == TRUE);
280 fail_unless (cstart == 150);
281 fail_unless (cstop == 200);
283 /* partially inside, clip to size */
284 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
285 150, 300, &cstart, &cstop);
286 fail_unless (res == TRUE);
287 fail_unless (cstart == 150);
288 fail_unless (cstop == 200);
291 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
292 fail_unless (res == FALSE);
295 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
296 fail_unless (res == TRUE);
297 fail_unless (cstart == 100);
298 fail_unless (cstop == -1);
300 /* start on lower bound */
301 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
302 fail_unless (res == TRUE);
303 fail_unless (cstart == 100);
304 fail_unless (cstop == -1);
307 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
308 fail_unless (res == TRUE);
309 fail_unless (cstart == 150);
310 fail_unless (cstop == -1);
312 /* add 100 to start, set stop to 300, stop clips to 200 */
313 gst_segment_set_seek (&segment, 1.0,
316 GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
317 fail_unless (segment.start == 200);
318 fail_unless (segment.stop == 200);
320 /* add 100 to start (to 300), set stop to 200, this clips start
322 gst_segment_set_seek (&segment, 1.0,
325 GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update);
326 fail_unless (segment.start == 200);
327 fail_unless (segment.stop == 200);
328 fail_unless (update == FALSE);
330 /* seek relative to end */
331 gst_segment_set_seek (&segment, 1.0,
334 GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
335 fail_unless (segment.start == 100);
336 fail_unless (segment.stop == 180);
337 fail_unless (update == TRUE);
339 /* completely outside */
340 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
341 fail_unless (res == FALSE);
343 /* touching lower bound */
344 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
345 fail_unless (res == FALSE);
347 /* partially inside */
348 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
349 fail_unless (res == TRUE);
350 fail_unless (cstart == 100);
351 fail_unless (cstop == 150);
353 /* inside, touching lower bound */
354 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
355 100, 150, &cstart, &cstop);
356 fail_unless (res == TRUE);
357 fail_unless (cstart == 100);
358 fail_unless (cstop == 150);
360 /* completely inside */
361 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
362 150, 170, &cstart, &cstop);
363 fail_unless (res == TRUE);
364 fail_unless (cstart == 150);
365 fail_unless (cstop == 170);
367 /* partially inside */
368 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
369 150, 250, &cstart, &cstop);
370 fail_unless (res == TRUE);
371 fail_unless (cstart == 150);
372 fail_unless (cstop == 180);
375 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
376 fail_unless (res == FALSE);
379 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
380 fail_unless (res == TRUE);
381 fail_unless (cstart == 100);
382 fail_unless (cstop == 180);
384 /* start on lower bound */
385 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
386 fail_unless (res == TRUE);
387 fail_unless (cstart == 100);
388 fail_unless (cstop == 180);
391 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
392 fail_unless (res == TRUE);
393 fail_unless (cstart == 150);
394 fail_unless (cstop == 180);
396 /* start outside on boundary */
397 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
398 fail_unless (res == FALSE);
400 /* start completely outside */
401 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
402 fail_unless (res == FALSE);
407 GST_START_TEST (segment_seek_reverse)
412 gst_segment_init (&segment, GST_FORMAT_BYTES);
413 gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
415 /* configure segment to stop 100 */
416 gst_segment_set_seek (&segment, -1.0,
419 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 100, &update);
420 fail_unless (segment.start == 0);
421 fail_unless (segment.stop == 100);
422 fail_unless (segment.time == 0);
423 fail_unless (segment.last_stop == 100);
424 fail_unless (update == TRUE);
427 gst_segment_set_seek (&segment, -1.0,
430 GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_CUR, -20, &update);
431 fail_unless (segment.start == 10);
432 fail_unless (segment.stop == 80);
433 fail_unless (segment.time == 10);
434 fail_unless (segment.last_stop == 80);
435 fail_unless (update == TRUE);
437 gst_segment_set_seek (&segment, -1.0,
440 GST_SEEK_TYPE_SET, 20, GST_SEEK_TYPE_NONE, 0, &update);
441 fail_unless (segment.start == 20);
442 fail_unless (segment.stop == 80);
443 fail_unless (segment.time == 20);
444 fail_unless (segment.last_stop == 80);
445 fail_unless (update == FALSE);
450 /* mess with the segment structure in the bytes format */
451 GST_START_TEST (segment_seek_rate)
456 gst_segment_init (&segment, GST_FORMAT_BYTES);
458 /* configure segment to rate 2.0, format does not matter when we don't specify
459 * a start or stop position. */
460 gst_segment_set_seek (&segment, 2.0,
461 GST_FORMAT_UNDEFINED,
463 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1, &update);
464 fail_unless (segment.format == GST_FORMAT_BYTES);
465 fail_unless (segment.start == 0);
466 fail_unless (segment.stop == -1);
467 fail_unless (segment.rate == 2.0);
468 fail_unless (update == FALSE);
470 /* 0 is the same in all formats and should not fail */
471 gst_segment_set_seek (&segment, 2.0,
472 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
473 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1, &update);
474 fail_unless (segment.format == GST_FORMAT_BYTES);
476 /* set to -1 means start from 0 */
477 gst_segment_set_seek (&segment, 2.0,
478 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
479 GST_SEEK_TYPE_SET, -1, GST_SEEK_TYPE_NONE, -1, &update);
480 fail_unless (segment.format == GST_FORMAT_BYTES);
481 fail_unless (segment.start == 0);
483 gst_segment_set_seek (&segment, 2.0,
484 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
485 GST_SEEK_TYPE_CUR, 0, GST_SEEK_TYPE_NONE, -1, &update);
487 gst_segment_set_seek (&segment, 2.0,
488 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
489 GST_SEEK_TYPE_END, 0, GST_SEEK_TYPE_NONE, -1, &update);
491 /* -1 for end is fine too in all formats */
492 gst_segment_set_seek (&segment, 2.0,
493 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
494 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
496 /* 0 as relative end is fine too */
497 gst_segment_set_seek (&segment, 2.0,
498 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
499 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
501 gst_segment_set_seek (&segment, 2.0,
502 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
503 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
505 /* set a real stop position, this must happen in bytes */
506 gst_segment_set_seek (&segment, 3.0,
509 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, 100, &update);
510 fail_unless (segment.format == GST_FORMAT_BYTES);
511 fail_unless (segment.start == 0);
512 fail_unless (segment.stop == 100);
513 fail_unless (segment.rate == 3.0);
514 /* no seek should happen, we just updated the stop position in forward
516 fail_unless (update == FALSE);
518 /* 0 as relative end is fine too */
519 gst_segment_set_seek (&segment, 2.0,
520 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
521 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
522 fail_unless (segment.stop == 100);
524 gst_segment_set_seek (&segment, 2.0,
525 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
526 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
527 fail_unless (segment.stop == 100);
529 /* -1 for end is fine too in all formats */
530 gst_segment_set_seek (&segment, 2.0,
531 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
532 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
533 fail_unless (segment.stop == -1);
535 /* set some duration, stop -1 END seeks will now work with the
536 * duration, if the formats match */
537 gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
538 fail_unless (segment.duration == 200);
540 /* seek to end in any format with 0 should set the stop to the
542 gst_segment_set_seek (&segment, 2.0,
543 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
544 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
545 fail_unless (segment.stop == 200);
546 fail_unless (segment.duration == 200);
548 /* subtract 100 from the end */
549 gst_segment_set_seek (&segment, 2.0,
550 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
551 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, -100, &update);
552 fail_unless (segment.stop == 100);
553 fail_unless (segment.duration == 200);
555 /* add 100 to the duration, this should be clamped to the duration */
556 gst_segment_set_seek (&segment, 2.0,
557 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
558 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 100, &update);
559 fail_unless (segment.stop == 200);
560 fail_unless (segment.duration == 200);
562 /* add 300 to the start, this should be clamped to the duration */
563 gst_segment_set_seek (&segment, 2.0,
564 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
565 GST_SEEK_TYPE_CUR, 300, GST_SEEK_TYPE_END, 0, &update);
566 fail_unless (segment.start == 200);
567 fail_unless (segment.stop == 200);
568 fail_unless (segment.duration == 200);
570 /* subtract 300 from the start, this should be clamped to 0 */
571 gst_segment_set_seek (&segment, 2.0,
572 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
573 GST_SEEK_TYPE_CUR, -300, GST_SEEK_TYPE_END, 0, &update);
574 fail_unless (segment.start == 0);
575 fail_unless (segment.stop == 200);
576 fail_unless (segment.duration == 200);
581 /* mess with the segment structure in the bytes format */
582 GST_START_TEST (segment_newsegment_open)
586 gst_segment_init (&segment, GST_FORMAT_BYTES);
588 /* time should also work for starting from 0 */
589 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0, -1,
592 fail_unless (segment.rate == 1.0);
593 fail_unless (segment.format == GST_FORMAT_BYTES);
594 fail_unless (segment.flags == 0);
595 fail_unless (segment.start == 0);
596 fail_unless (segment.stop == -1);
597 fail_unless (segment.time == 0);
598 fail_unless (segment.accum == 0);
599 fail_unless (segment.last_stop == 0);
600 fail_unless (segment.duration == -1);
602 /* we set stop but in the wrong format, stop stays open. */
603 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0,
606 fail_unless (segment.start == 0);
607 fail_unless (segment.stop == -1);
608 fail_unless (segment.time == 0);
609 fail_unless (segment.accum == 0);
610 fail_unless (segment.last_stop == 0);
612 /* update, nothing changes */
613 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 0, -1,
616 fail_unless (segment.start == 0);
617 fail_unless (segment.stop == -1);
618 fail_unless (segment.time == 0);
619 fail_unless (segment.accum == 0);
620 fail_unless (segment.last_stop == 0);
623 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
624 GST_FORMAT_BYTES, 100, -1, 100);
626 fail_unless (segment.start == 100);
627 fail_unless (segment.stop == -1);
628 fail_unless (segment.time == 100);
629 fail_unless (segment.accum == 100);
630 fail_unless (segment.last_stop == 100);
632 /* last_stop 0, accum does not change */
633 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
636 fail_unless (segment.start == 0);
637 fail_unless (segment.stop == -1);
638 fail_unless (segment.time == 0);
639 fail_unless (segment.accum == 100);
641 gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
643 fail_unless (segment.last_stop == 200);
645 /* last_stop 200, accum changes */
646 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
649 fail_unless (segment.start == 0);
650 fail_unless (segment.stop == -1);
651 fail_unless (segment.time == 0);
652 fail_unless (segment.accum == 300);
653 fail_unless (segment.last_stop == 0);
659 /* mess with the segment structure in the bytes format */
660 GST_START_TEST (segment_newsegment_closed)
664 gst_segment_init (&segment, GST_FORMAT_BYTES);
666 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
667 GST_FORMAT_BYTES, 0, 200, 0);
669 fail_unless (segment.rate == 1.0);
670 fail_unless (segment.format == GST_FORMAT_BYTES);
671 fail_unless (segment.flags == 0);
672 fail_unless (segment.start == 0);
673 fail_unless (segment.stop == 200);
674 fail_unless (segment.time == 0);
675 fail_unless (segment.accum == 0);
676 fail_unless (segment.last_stop == 0);
677 fail_unless (segment.duration == -1);
679 /* assume we advanced to position 40 */
680 gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 40);
681 fail_unless (segment.last_stop == 40);
683 /* do an update to the start, last_stop is unchanged because it's bigger */
684 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 20,
687 fail_unless (segment.start == 20);
688 fail_unless (segment.stop == 200);
689 fail_unless (segment.time == 20);
690 fail_unless (segment.accum == 20);
691 fail_unless (segment.last_stop == 40);
693 /* do an update past our last_stop, it should be updated now */
694 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 50,
697 fail_unless (segment.start == 50);
698 fail_unless (segment.stop == 300);
699 fail_unless (segment.time == 50);
700 fail_unless (segment.accum == 50);
701 fail_unless (segment.last_stop == 50);
703 /* and a new accumulated one */
704 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
705 GST_FORMAT_BYTES, 100, 400, 300);
707 fail_unless (segment.start == 100);
708 fail_unless (segment.stop == 400);
709 fail_unless (segment.time == 300);
710 fail_unless (segment.accum == 300);
712 /* and a new updated one */
713 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
714 GST_FORMAT_BYTES, 100, 500, 300);
716 fail_unless (segment.start == 100);
717 fail_unless (segment.stop == 500);
718 fail_unless (segment.time == 300);
719 fail_unless (segment.accum == 300);
721 /* and a new partially updated one */
722 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
723 GST_FORMAT_BYTES, 200, 500, 400);
725 fail_unless (segment.start == 200);
726 fail_unless (segment.stop == 500);
727 fail_unless (segment.time == 400);
728 fail_unless (segment.accum == 400);
733 /* mess with the segment structure in the time format */
734 GST_START_TEST (segment_newsegment_streamtime)
739 gst_segment_init (&segment, GST_FORMAT_TIME);
741 /***************************
743 ***************************/
744 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
745 GST_FORMAT_TIME, 0, 200, 0);
747 fail_unless (segment.rate == 1.0);
748 fail_unless (segment.applied_rate == 1.0);
749 fail_unless (segment.format == GST_FORMAT_TIME);
750 fail_unless (segment.flags == 0);
751 fail_unless (segment.start == 0);
752 fail_unless (segment.stop == 200);
753 fail_unless (segment.time == 0);
754 fail_unless (segment.accum == 0);
755 fail_unless (segment.last_stop == 0);
756 fail_unless (segment.duration == -1);
758 /* invalid time gives invalid result */
759 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
760 fail_unless (result == -1);
762 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
763 fail_unless (result == 0);
765 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
766 fail_unless (result == 100);
768 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
769 fail_unless (result == 200);
771 /* outside of the segment */
772 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
773 fail_unless (result == -1);
775 /*********************
776 * time shifted by 500
777 *********************/
778 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
779 GST_FORMAT_TIME, 0, 200, 500);
781 fail_unless (segment.accum == 200);
783 /* invalid time gives invalid result */
784 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
785 fail_unless (result == -1);
787 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
788 fail_unless (result == 500);
790 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
791 fail_unless (result == 600);
793 /* outside of the segment */
794 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
795 fail_unless (result == -1);
797 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
798 fail_unless (result == -1);
800 /*********************
802 *********************/
803 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
804 GST_FORMAT_TIME, 500, 700, 0);
806 fail_unless (segment.accum == 400);
808 /* invalid time gives invalid result */
809 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
810 fail_unless (result == -1);
812 /* before segment is invalid */
813 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
814 fail_unless (result == -1);
816 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
817 fail_unless (result == 0);
819 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
820 fail_unless (result == 100);
822 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
823 fail_unless (result == 200);
825 /* outside of the segment */
826 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
827 fail_unless (result == -1);
829 /*************************************
830 * time offset by 500, shifted by 200
831 *************************************/
832 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
833 GST_FORMAT_TIME, 500, 700, 200);
835 fail_unless (segment.accum == 600);
837 /* invalid time gives invalid result */
838 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
839 fail_unless (result == -1);
841 /* before segment is invalid */
842 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
843 fail_unless (result == -1);
845 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
846 fail_unless (result == 200);
848 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
849 fail_unless (result == 300);
851 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
852 fail_unless (result == 400);
854 /* outside of the segment */
855 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
856 fail_unless (result == -1);
861 /* mess with the segment structure in the time format */
862 GST_START_TEST (segment_newsegment_streamtime_rate)
867 gst_segment_init (&segment, GST_FORMAT_TIME);
869 /***************************
870 * Normal segment rate 2.0
871 ***************************/
872 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
873 GST_FORMAT_TIME, 0, 200, 0);
875 fail_unless (segment.rate == 2.0);
876 fail_unless (segment.applied_rate == 1.0);
877 fail_unless (segment.format == GST_FORMAT_TIME);
878 fail_unless (segment.flags == 0);
879 fail_unless (segment.start == 0);
880 fail_unless (segment.stop == 200);
881 fail_unless (segment.time == 0);
882 fail_unless (segment.accum == 0);
883 fail_unless (segment.last_stop == 0);
884 fail_unless (segment.duration == -1);
886 /* invalid time gives invalid result */
887 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
888 fail_unless (result == -1);
890 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
891 fail_unless (result == 0);
893 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
894 fail_unless (result == 100);
896 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
897 fail_unless (result == 150);
899 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
900 fail_unless (result == 200);
902 /* outside of the segment */
903 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
904 fail_unless (result == -1);
906 /***************************************
907 * Normal segment rate 2.0, offset
908 ***************************************/
909 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
910 GST_FORMAT_TIME, 100, 300, 0);
912 fail_unless (segment.accum == 100);
914 /* invalid time gives invalid result */
915 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
916 fail_unless (result == -1);
918 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
919 fail_unless (result == 0);
921 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
922 fail_unless (result == 100);
924 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
925 fail_unless (result == 150);
927 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
928 fail_unless (result == 200);
930 /* outside of the segment */
931 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
932 fail_unless (result == -1);
934 /***************************************
935 * Normal segment rate -1.0, offset
936 ***************************************/
938 /* buffers will arrive from 300 to 100 in a sink, stream time
939 * calculation is unaffected by the rate */
940 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
941 GST_FORMAT_TIME, 100, 300, 0);
943 fail_unless (segment.accum == 200);
945 /* invalid time gives invalid result */
946 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
947 fail_unless (result == -1);
949 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
950 fail_unless (result == 0);
952 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
953 fail_unless (result == 100);
955 /***********************************************
956 * Normal segment rate -1.0, offset, time = 200
957 ***********************************************/
958 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
959 GST_FORMAT_TIME, 100, 300, 200);
961 /* invalid time gives invalid result */
962 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
963 fail_unless (result == -1);
965 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
966 fail_unless (result == 200);
968 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
969 fail_unless (result == 300);
971 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
972 fail_unless (result == 400);
974 /* outside of the segment */
975 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
976 fail_unless (result == -1);
981 /* mess with the segment structure in the time format */
982 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
987 gst_segment_init (&segment, GST_FORMAT_TIME);
989 /***********************************************************
990 * Normal segment rate 1.0, applied rate -1.0
991 * This means the timestamps represents a stream going backwards
992 * starting from @time to 0.
993 ************************************************************/
994 gst_segment_set_newsegment (&segment, FALSE, 1.0, -1.0,
995 GST_FORMAT_TIME, 0, 200, 200);
997 fail_unless (segment.rate == 1.0);
998 fail_unless (segment.applied_rate == -1.0);
999 fail_unless (segment.format == GST_FORMAT_TIME);
1000 fail_unless (segment.flags == 0);
1001 fail_unless (segment.start == 0);
1002 fail_unless (segment.stop == 200);
1003 fail_unless (segment.time == 200);
1004 fail_unless (segment.accum == 0);
1005 fail_unless (segment.last_stop == 0);
1006 fail_unless (segment.duration == -1);
1008 /* invalid time gives invalid result */
1009 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1010 fail_unless (result == -1);
1012 /* we count backwards from 200 */
1013 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1014 fail_unless (result == 200);
1016 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1017 fail_unless (result == 100);
1019 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1020 fail_unless (result == 50);
1022 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1023 fail_unless (result == 0);
1025 /* outside of the segment */
1026 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1027 fail_unless (result == -1);
1029 /***********************************************************
1030 * Normal segment rate 1.0, applied rate 2.0
1031 * This means the timestamps represents a stream at twice the
1033 ************************************************************/
1034 gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1035 GST_FORMAT_TIME, 0, 200, 0);
1037 fail_unless (segment.rate == 1.0);
1038 fail_unless (segment.applied_rate == 2.0);
1039 fail_unless (segment.format == GST_FORMAT_TIME);
1040 fail_unless (segment.flags == 0);
1041 fail_unless (segment.start == 0);
1042 fail_unless (segment.stop == 200);
1043 fail_unless (segment.time == 0);
1044 fail_unless (segment.accum == 200);
1045 fail_unless (segment.last_stop == 0);
1046 fail_unless (segment.duration == -1);
1048 /* invalid time gives invalid result */
1049 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1050 fail_unless (result == -1);
1052 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1053 fail_unless (result == 0);
1055 /* the stream prepresents a stream going twice as fast, the position
1056 * in the segment is therefore scaled by the applied rate */
1057 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1058 fail_unless (result == 200);
1060 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1061 fail_unless (result == 300);
1063 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1064 fail_unless (result == 400);
1066 /* outside of the segment */
1067 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1068 fail_unless (result == -1);
1070 /***********************************************************
1071 * Normal segment rate 1.0, applied rate -2.0
1072 * This means the timestamps represents a stream at twice the
1074 ************************************************************/
1075 gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1076 GST_FORMAT_TIME, 0, 200, 400);
1078 fail_unless (segment.rate == 1.0);
1079 fail_unless (segment.applied_rate == -2.0);
1080 fail_unless (segment.format == GST_FORMAT_TIME);
1081 fail_unless (segment.flags == 0);
1082 fail_unless (segment.start == 0);
1083 fail_unless (segment.stop == 200);
1084 fail_unless (segment.time == 400);
1085 /* previous segment lasted 200, rate of 2.0 was already applied */
1086 fail_unless (segment.accum == 400);
1087 fail_unless (segment.last_stop == 0);
1088 fail_unless (segment.duration == -1);
1090 /* invalid time gives invalid result */
1091 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1092 fail_unless (result == -1);
1094 /* we count backwards from 400 */
1095 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1096 fail_unless (result == 400);
1098 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1099 fail_unless (result == 200);
1101 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1102 fail_unless (result == 100);
1104 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1105 fail_unless (result == 0);
1107 /* outside of the segment */
1108 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1109 fail_unless (result == -1);
1111 /***********************************************************
1112 * Normal segment rate 1.0, applied rate -2.0
1113 * This means the timestamps represents a stream at twice the
1114 * reverse rate, start time cannot compensate the complete
1115 * duration of the segment so we stop at 0
1116 ************************************************************/
1117 gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1118 GST_FORMAT_TIME, 0, 200, 200);
1120 fail_unless (segment.rate == 1.0);
1121 fail_unless (segment.applied_rate == -2.0);
1122 fail_unless (segment.format == GST_FORMAT_TIME);
1123 fail_unless (segment.flags == 0);
1124 fail_unless (segment.start == 0);
1125 fail_unless (segment.stop == 200);
1126 fail_unless (segment.time == 200);
1127 fail_unless (segment.accum == 600);
1128 fail_unless (segment.last_stop == 0);
1129 fail_unless (segment.duration == -1);
1131 /* invalid time gives invalid result */
1132 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1133 fail_unless (result == -1);
1135 /* we count backwards from 200 */
1136 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1137 fail_unless (result == 200);
1139 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1140 fail_unless (result == 0);
1143 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1144 fail_unless (result == 0);
1146 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1147 fail_unless (result == 0);
1149 /* outside of the segment */
1150 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1151 fail_unless (result == -1);
1156 /* mess with the segment structure in the time format */
1157 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
1162 gst_segment_init (&segment, GST_FORMAT_TIME);
1164 /***********************************************************
1165 * Segment rate 2.0, applied rate 2.0
1166 * this means we have a double speed stream that we should
1167 * speed up by a factor of 2.0 some more. the resulting
1168 * stream will be played at four times the speed.
1169 ************************************************************/
1170 gst_segment_set_newsegment (&segment, FALSE, 2.0, 2.0,
1171 GST_FORMAT_TIME, 0, 200, 0);
1173 fail_unless (segment.rate == 2.0);
1174 fail_unless (segment.applied_rate == 2.0);
1175 fail_unless (segment.format == GST_FORMAT_TIME);
1176 fail_unless (segment.flags == 0);
1177 fail_unless (segment.start == 0);
1178 fail_unless (segment.stop == 200);
1179 fail_unless (segment.time == 0);
1180 fail_unless (segment.accum == 0);
1181 fail_unless (segment.last_stop == 0);
1182 fail_unless (segment.duration == -1);
1184 /* invalid time gives invalid result */
1185 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1186 fail_unless (result == -1);
1188 /* only applied rate affects our calculation of the stream time */
1189 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1190 fail_unless (result == 0);
1192 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1193 fail_unless (result == 200);
1195 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1196 fail_unless (result == 300);
1198 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1199 fail_unless (result == 400);
1201 /* outside of the segment */
1202 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1203 fail_unless (result == -1);
1205 /***********************************************************
1206 * Segment rate 2.0, applied rate -1.0
1207 * this means we have a reverse stream that we should
1208 * speed up by a factor of 2.0
1209 ************************************************************/
1210 gst_segment_set_newsegment (&segment, FALSE, 2.0, -1.0,
1211 GST_FORMAT_TIME, 0, 200, 200);
1213 fail_unless (segment.rate == 2.0);
1214 fail_unless (segment.applied_rate == -1.0);
1215 fail_unless (segment.format == GST_FORMAT_TIME);
1216 fail_unless (segment.flags == 0);
1217 fail_unless (segment.start == 0);
1218 fail_unless (segment.stop == 200);
1219 fail_unless (segment.time == 200);
1220 /* previous segment lasted 100 */
1221 fail_unless (segment.accum == 100);
1222 fail_unless (segment.last_stop == 0);
1223 fail_unless (segment.duration == -1);
1225 /* invalid time gives invalid result */
1226 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1227 fail_unless (result == -1);
1229 /* only applied rate affects our calculation of the stream time */
1230 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1231 fail_unless (result == 200);
1233 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1234 fail_unless (result == 100);
1236 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1237 fail_unless (result == 50);
1239 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1240 fail_unless (result == 0);
1242 /* outside of the segment */
1243 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1244 fail_unless (result == -1);
1246 /***********************************************************
1247 * Segment rate -1.0, applied rate -1.0
1248 * this means we have a reverse stream that we should
1249 * reverse to get the normal stream again.
1250 ************************************************************/
1251 gst_segment_set_newsegment (&segment, FALSE, -1.0, -1.0,
1252 GST_FORMAT_TIME, 0, 200, 200);
1254 fail_unless (segment.rate == -1.0);
1255 fail_unless (segment.applied_rate == -1.0);
1256 fail_unless (segment.format == GST_FORMAT_TIME);
1257 fail_unless (segment.flags == 0);
1258 fail_unless (segment.start == 0);
1259 fail_unless (segment.stop == 200);
1260 fail_unless (segment.time == 200);
1261 /* accumulated 100 of previous segment to make 200 */
1262 fail_unless (segment.accum == 200);
1263 fail_unless (segment.last_stop == 200);
1264 fail_unless (segment.duration == -1);
1266 /* invalid time gives invalid result */
1267 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1268 fail_unless (result == -1);
1270 /* only applied rate affects our calculation of the stream time */
1271 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1272 fail_unless (result == 200);
1274 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1275 fail_unless (result == 100);
1277 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1278 fail_unless (result == 50);
1280 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1281 fail_unless (result == 0);
1283 /* outside of the segment */
1284 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1285 fail_unless (result == -1);
1287 /***********************************************************
1288 * Segment rate -1.0, applied rate -1.0
1289 * this means we have a reverse stream that we should
1290 * reverse to get the normal stream again.
1291 ************************************************************/
1292 gst_segment_set_newsegment (&segment, FALSE, -1.0, 2.0,
1293 GST_FORMAT_TIME, 0, 200, 0);
1295 fail_unless (segment.rate == -1.0);
1296 fail_unless (segment.applied_rate == 2.0);
1297 fail_unless (segment.format == GST_FORMAT_TIME);
1298 fail_unless (segment.flags == 0);
1299 fail_unless (segment.start == 0);
1300 fail_unless (segment.stop == 200);
1301 fail_unless (segment.time == 0);
1302 fail_unless (segment.accum == 400);
1303 fail_unless (segment.last_stop == 200);
1304 fail_unless (segment.duration == -1);
1306 /* invalid time gives invalid result */
1307 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1308 fail_unless (result == -1);
1310 /* only applied rate affects our calculation of the stream time */
1311 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1312 fail_unless (result == 0);
1314 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1315 fail_unless (result == 200);
1317 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1318 fail_unless (result == 300);
1320 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1321 fail_unless (result == 400);
1323 /* outside of the segment */
1324 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1325 fail_unless (result == -1);
1330 /* mess with the segment structure in the time format */
1331 GST_START_TEST (segment_newsegment_runningtime)
1336 gst_segment_init (&segment, GST_FORMAT_TIME);
1338 /***************************
1340 ***************************/
1341 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1342 GST_FORMAT_TIME, 0, 200, 0);
1344 fail_unless (segment.rate == 1.0);
1345 fail_unless (segment.applied_rate == 1.0);
1346 fail_unless (segment.format == GST_FORMAT_TIME);
1347 fail_unless (segment.flags == 0);
1348 fail_unless (segment.start == 0);
1349 fail_unless (segment.stop == 200);
1350 fail_unless (segment.time == 0);
1351 fail_unless (segment.accum == 0);
1352 fail_unless (segment.last_stop == 0);
1353 fail_unless (segment.duration == -1);
1355 /* invalid time gives invalid result */
1356 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1357 fail_unless (result == -1);
1359 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1360 fail_unless (result == 0);
1361 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1362 fail_unless (result == 0);
1364 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1365 fail_unless (result == 100);
1366 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1367 fail_unless (result == 100);
1369 /* at edge is exactly the segment duration */
1370 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1371 fail_unless (result == 200);
1372 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1373 fail_unless (result == 200);
1375 /* outside of the segment */
1376 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1377 fail_unless (result == -1);
1378 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 300);
1379 fail_unless (result == -1);
1381 /***********************************************************
1382 * time shifted by 500, check if accumulation worked.
1383 * Rate convert to twice the speed which means scaling down
1384 * all positions by 2.0 in this segment.
1385 * Then time argument is not used at all here.
1386 ***********************************************************/
1387 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
1388 GST_FORMAT_TIME, 0, 200, 500);
1390 /* normal speed gives elapsed of 200 */
1391 fail_unless (segment.accum == 200);
1393 /* invalid time gives invalid result */
1394 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1395 fail_unless (result == -1);
1397 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1398 fail_unless (result == 200);
1399 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1400 fail_unless (result == 0);
1402 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1403 fail_unless (result == 250);
1404 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1405 fail_unless (result == 100);
1407 /* outside of the segment */
1408 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1409 fail_unless (result == -1);
1410 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 310);
1411 fail_unless (result == -1);
1413 /********************************************
1414 * time offset by 500
1415 * applied rate is not used for running time
1416 ********************************************/
1417 gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1418 GST_FORMAT_TIME, 500, 700, 0);
1420 /* previous segment played at double speed gives elapsed time of
1421 * 100 added to previous accum of 200 gives 300. */
1422 fail_unless (segment.accum == 300);
1424 /* invalid time gives invalid result */
1425 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1426 fail_unless (result == -1);
1428 /* before segment is invalid */
1429 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1430 fail_unless (result == -1);
1431 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 200);
1432 fail_unless (result == -1);
1434 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1435 fail_unless (result == 300);
1436 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1437 fail_unless (result == 500);
1439 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1440 fail_unless (result == 400);
1441 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1442 fail_unless (result == 600);
1444 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1445 fail_unless (result == 500);
1446 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1447 fail_unless (result == 700);
1449 /* outside of the segment */
1450 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1451 fail_unless (result == -1);
1452 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1453 fail_unless (result == -1);
1455 /**********************************************************
1456 * time offset by 500, shifted by 200
1457 * Negative rate makes the running time go backwards
1458 * relative to the segment stop position. again time
1460 **********************************************************/
1461 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1462 GST_FORMAT_TIME, 500, 700, 200);
1464 fail_unless (segment.accum == 500);
1466 /* invalid time gives invalid result */
1467 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1468 fail_unless (result == -1);
1470 /* before segment is invalid */
1471 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1472 fail_unless (result == -1);
1473 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 400);
1474 fail_unless (result == -1);
1476 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1477 fail_unless (result == 700);
1478 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1479 fail_unless (result == 500);
1481 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1482 fail_unless (result == 600);
1483 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1484 fail_unless (result == 600);
1486 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1487 fail_unless (result == 500);
1488 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1489 fail_unless (result == 700);
1491 /* outside of the segment */
1492 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1493 fail_unless (result == -1);
1494 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1495 fail_unless (result == -1);
1497 /**********************************************************
1498 * time offset by 500, shifted by 200
1499 * Negative rate makes the running time go backwards at
1500 * twice speed relative to the segment stop position. again
1502 **********************************************************/
1503 gst_segment_set_newsegment (&segment, FALSE, -2.0, -2.0,
1504 GST_FORMAT_TIME, 500, 700, 200);
1506 fail_unless (segment.accum == 700);
1508 /* invalid time gives invalid result */
1509 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1510 fail_unless (result == -1);
1512 /* before segment is invalid */
1513 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1514 fail_unless (result == -1);
1515 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1516 fail_unless (result == -1);
1518 /* total scaled segment time is 100, accum is 700, so we get 800 */
1519 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1520 fail_unless (result == 800);
1521 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1522 fail_unless (result == 500);
1524 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1525 fail_unless (result == 750);
1526 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1527 fail_unless (result == 600);
1529 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1530 fail_unless (result == 700);
1531 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1532 fail_unless (result == 700);
1534 /* outside of the segment */
1535 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1536 fail_unless (result == -1);
1537 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 900);
1538 fail_unless (result == -1);
1540 /* see if negative rate closed segment correctly */
1541 gst_segment_set_newsegment (&segment, FALSE, -2.0, -1.0,
1542 GST_FORMAT_TIME, 500, 700, 200);
1544 /* previous segment lasted 100, and was at 700 so we should get 800 */
1545 fail_unless (segment.accum == 800);
1546 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1547 fail_unless (result == 700);
1552 /* mess with the segment structure in the time format */
1553 GST_START_TEST (segment_newsegment_accum)
1558 gst_segment_init (&segment, GST_FORMAT_TIME);
1560 /***************************
1561 * Normal reverse segment
1562 ***************************/
1563 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1564 GST_FORMAT_TIME, 0, 200, 0);
1566 fail_unless (segment.rate == -1.0);
1567 fail_unless (segment.applied_rate == 1.0);
1568 fail_unless (segment.format == GST_FORMAT_TIME);
1569 fail_unless (segment.flags == 0);
1570 fail_unless (segment.start == 0);
1571 fail_unless (segment.stop == 200);
1572 fail_unless (segment.time == 0);
1573 fail_unless (segment.accum == 0);
1574 fail_unless (segment.last_stop == 200);
1575 fail_unless (segment.duration == -1);
1577 /* invalid time gives invalid result */
1578 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1579 fail_unless (result == -1);
1581 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1582 fail_unless (result == 0);
1583 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1584 fail_unless (result == 200);
1586 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1587 fail_unless (result == 50);
1588 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1589 fail_unless (result == 150);
1591 /* update segment, this accumulates 50 from the previous segment. */
1592 gst_segment_set_newsegment (&segment, TRUE, -2.0, 1.0,
1593 GST_FORMAT_TIME, 0, 150, 0);
1595 fail_unless (segment.rate == -2.0);
1596 fail_unless (segment.applied_rate == 1.0);
1597 fail_unless (segment.format == GST_FORMAT_TIME);
1598 fail_unless (segment.flags == 0);
1599 fail_unless (segment.start == 0);
1600 fail_unless (segment.stop == 150);
1601 fail_unless (segment.time == 0);
1602 fail_unless (segment.accum == 50);
1603 fail_unless (segment.last_stop == 150);
1604 fail_unless (segment.duration == -1);
1606 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1607 fail_unless (result == 50);
1608 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1609 fail_unless (result == 150);
1611 /* 50 accumulated + 50 / 2 */
1612 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1613 fail_unless (result == 75);
1614 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1615 fail_unless (result == 100);
1617 /* update segment, this does not accumulate anything. */
1618 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
1619 GST_FORMAT_TIME, 100, 200, 100);
1621 fail_unless (segment.rate == 1.0);
1622 fail_unless (segment.applied_rate == 1.0);
1623 fail_unless (segment.format == GST_FORMAT_TIME);
1624 fail_unless (segment.flags == 0);
1625 fail_unless (segment.start == 100);
1626 fail_unless (segment.stop == 200);
1627 fail_unless (segment.time == 100);
1628 fail_unless (segment.accum == 50);
1629 fail_unless (segment.last_stop == 150);
1630 fail_unless (segment.duration == -1);
1632 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1633 fail_unless (result == 50);
1634 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1635 fail_unless (result == 100);
1637 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1638 fail_unless (result == 100);
1639 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1640 fail_unless (result == 150);
1645 /* mess with the segment structure in the time format */
1646 GST_START_TEST (segment_newsegment_accum2)
1651 gst_segment_init (&segment, GST_FORMAT_TIME);
1653 /***************************
1654 * Normal reverse segment
1655 ***************************/
1656 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1657 GST_FORMAT_TIME, 0, 200, 0);
1659 fail_unless (segment.rate == -1.0);
1660 fail_unless (segment.applied_rate == 1.0);
1661 fail_unless (segment.format == GST_FORMAT_TIME);
1662 fail_unless (segment.flags == 0);
1663 fail_unless (segment.start == 0);
1664 fail_unless (segment.stop == 200);
1665 fail_unless (segment.time == 0);
1666 fail_unless (segment.accum == 0);
1667 fail_unless (segment.last_stop == 200);
1668 fail_unless (segment.duration == -1);
1670 /* invalid time gives invalid result */
1671 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1672 fail_unless (result == -1);
1673 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1674 fail_unless (result == -1);
1676 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1677 fail_unless (result == 0);
1678 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1679 fail_unless (result == 200);
1681 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1682 fail_unless (result == 50);
1683 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1684 fail_unless (result == 150);
1686 /* close segment, this accumulates nothing. */
1687 gst_segment_set_newsegment (&segment, TRUE, -1.0, 1.0,
1688 GST_FORMAT_TIME, 150, 200, 0);
1690 fail_unless (segment.rate == -1.0);
1691 fail_unless (segment.applied_rate == 1.0);
1692 fail_unless (segment.format == GST_FORMAT_TIME);
1693 fail_unless (segment.flags == 0);
1694 fail_unless (segment.start == 150);
1695 fail_unless (segment.stop == 200);
1696 fail_unless (segment.time == 0);
1697 fail_unless (segment.accum == 0);
1698 fail_unless (segment.last_stop == 200);
1699 fail_unless (segment.duration == -1);
1701 /* new segment, this accumulates 50. */
1702 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1703 GST_FORMAT_TIME, 150, 300, 150);
1705 fail_unless (segment.rate == 1.0);
1706 fail_unless (segment.applied_rate == 1.0);
1707 fail_unless (segment.format == GST_FORMAT_TIME);
1708 fail_unless (segment.flags == 0);
1709 fail_unless (segment.start == 150);
1710 fail_unless (segment.stop == 300);
1711 fail_unless (segment.time == 150);
1712 fail_unless (segment.accum == 50);
1713 fail_unless (segment.last_stop == 150);
1714 fail_unless (segment.duration == -1);
1716 /* invalid time gives invalid result */
1717 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1718 fail_unless (result == -1);
1720 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1721 fail_unless (result == 50);
1722 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1723 fail_unless (result == 150);
1725 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1726 fail_unless (result == 100);
1727 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1728 fail_unless (result == 200);
1733 GST_START_TEST (segment_copy)
1736 GstSegment segment = { 0.0, };
1738 /* this is a boxed type copy function, we support copying NULL */
1739 fail_unless (gst_segment_copy (NULL) == NULL);
1741 gst_segment_init (&segment, GST_FORMAT_TIME);
1743 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1744 GST_FORMAT_TIME, 0, 200, 0);
1746 copy = gst_segment_copy (&segment);
1747 fail_unless (copy != NULL);
1748 /* we inited the struct on the stack to zeroes, so direct comparison should
1749 * be ok here despite the padding field and regardless of implementation */
1750 fail_unless (memcmp (copy, &segment, sizeof (GstSegment)) == 0);
1751 gst_segment_free (copy);
1757 gst_segment_suite (void)
1759 Suite *s = suite_create ("GstSegment");
1760 TCase *tc_chain = tcase_create ("segments");
1762 tcase_set_timeout (tc_chain, 20);
1764 suite_add_tcase (s, tc_chain);
1765 tcase_add_test (tc_chain, segment_seek_nosize);
1766 tcase_add_test (tc_chain, segment_seek_size);
1767 tcase_add_test (tc_chain, segment_seek_reverse);
1768 tcase_add_test (tc_chain, segment_seek_rate);
1769 tcase_add_test (tc_chain, segment_newsegment_open);
1770 tcase_add_test (tc_chain, segment_newsegment_closed);
1771 tcase_add_test (tc_chain, segment_newsegment_streamtime);
1772 tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1773 tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1774 tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1775 tcase_add_test (tc_chain, segment_newsegment_runningtime);
1776 tcase_add_test (tc_chain, segment_newsegment_accum);
1777 tcase_add_test (tc_chain, segment_newsegment_accum2);
1778 tcase_add_test (tc_chain, segment_copy);
1783 GST_CHECK_MAIN (gst_segment);